計算機13-3班實驗報告袁清高_第1頁
計算機13-3班實驗報告袁清高_第2頁
計算機13-3班實驗報告袁清高_第3頁
計算機13-3班實驗報告袁清高_第4頁
計算機13-3班實驗報告袁清高_第5頁
已閱讀5頁,還剩40頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、編 譯原理實驗專業(yè):計算機科學與技術學號:2013211707姓名:袁指導老師:完成時間:2015 年 6 月 14 日實驗一 詞法分析設計一、 實驗目的通過本實驗的編程實踐,使學生了解詞法分析的任務,掌握詞法分析程序設計的原理和構造方法,使學生對編譯的基本概念、原理和方法有完整和清楚的理解,并且能夠正確和熟練地運用。二、 實驗環(huán)境Windows 8.1Visual Studio 013三、 實驗原理1、 實驗數(shù)據(jù)結構keyt序號string內(nèi)容row列l(wèi)ine行l(wèi)exstring instring輸入源程序的單詞緩存lengthToken輸出緩存的長度ley outToken100輸出緩存數(shù)

2、組string k7關鍵字數(shù)組char s6分界符數(shù)組char optr6操作符數(shù)組lengthKey關鍵字數(shù)組長度lengthS分界符數(shù)組長度lengthOptr關鍵字數(shù)組長度lex()構造函數(shù)lex()析構函數(shù)bool isKey(string str)判斷是否為關鍵字bool isLetter(char c)判斷是否為字母bool isDigit(char c)判斷是否為數(shù)字bool isOptr(char c)判斷是否為運算符bool isSeparator(char c)判斷是否為分界符2、 實驗算法描述四、 實驗結果void push_back(t,line,row,string

3、st)輸出緩沖區(qū)bool isSpace(char c)判斷是否為空白void la()詞法分析函數(shù)五、 實驗總結1、 本次實驗實現(xiàn)了簡單的詞法分析,但是由于比較簡單,很多方面都沒有考慮到,用途較為局限2、 由于以前寫MFC 程序比較少,所以這次需要花很大的工夫?qū)W習MFC。實驗二 LL(1)分析法一、 實驗目的通過完成分析法的語法分析程序,了解分析法和遞歸子程序的區(qū)別和聯(lián)系。使學生了解語法分析的功能,掌握語法分析程序設計的原理和構造方法,訓練學生掌握開發(fā)應用程序的基本方法。有利于提高學生的專業(yè)素質(zhì),為培養(yǎng)適應社會多方面需要的能力二、 實驗環(huán)境Windows 8.1Visual Studio 0

4、13三、 實驗原理1、 實驗數(shù)據(jù)結構#define arrayLength 20數(shù)組長度#define stackLength 20棧的長度Tokenstep步數(shù)string stackString棧中字符串string leftString剩余字符串string production所用產(chǎn)生式string movement動作stackcount棧中元素個數(shù)char datastacklength棧中元素stack()構造函數(shù)stack()析構函數(shù)bool isEmpty()判斷是否為空bool isFull()判斷是否慢bool getTop(char & c)取棧頂元素bool push

5、(char c)存入棧bool pop()出棧string out()返回棧中元素vector vt存放輸出緩存string strToken存放規(guī)約表達式2、 程序流程圖Cllchar Vn5非終結符char Vt5終結符char ch當前字符char X獲取棧頂元素string E8分析表string G8string T8string S8string F8Cll()構造函數(shù)Cll()析構函數(shù)bool isVt(char c)判斷是否為終結符bool pr(poer)打印剩余字符串void saveToToken( step, string stackString, string lef

6、tString, string production,string movement)保存至輸出緩存void run()LL(1)分析函數(shù)四、 實驗結果五、 實驗總結1、 本次實驗實現(xiàn)了 LL(1)分析法,并對 i+i*i#進行了分析。2、 本實驗窗口部分和實驗一相同,所以這次實驗在窗口部分花費了較少的時間。實驗三 LR(1)分析法一、 實驗目的構造 LR(1)分析程序,利用它進行語法分析,判斷給出的符號穿是否為該文法識別的句子,了解 LR(1)分析文法是嚴格的從左向右掃描,和自底向上的語法分析方法二、 實驗環(huán)境Windows 8.1Visual Studio 2013三、 實驗原理1、實驗數(shù)

7、據(jù)結構2、程序流程圖#define MaxLen 20棧的長度#define Length 20數(shù)組長度tokenstep步驟string susStack狀態(tài)棧string symbolStack符號棧string inputString輸入串string movement動作說明stackstack()構造函數(shù)stack()析構函數(shù)bool empty()判斷是否為空bool full()判斷是否滿bool get_top(char& c)取棧頂元素bool push(char c)入棧bool pop()出棧string out()輸出棧中元素string out1()輸出符號棧char

8、 dataMaxLen棧中元素count棧中元素個數(shù)四、 實驗結果五、 實驗總結本次實驗實現(xiàn)了 LR(1)分析程序,并利用它進行了語法分析。附:源代碼實驗一:#include stdafx.h#include lex.hlex:lex()k0 = do;k1 = while;k2 = end;k3 = for;k4 = prf;k5 = scanf;k6 = then;s0 = ,;s1 = ;s2 = ;s3 = ;s4 = (;s5 = );optr0 = +;optr1 = -;optr2 =*;optr3 = /;lengthKey = 7;lengthS = 6;lengthOptr

9、 = 4;lengthToken = 0;lex:lex()bool lex:isKey(string str)i;for (i = 0; i = a) & (c = A) & (c = 0&c = 9)return true;return false;bool lex:isOptr(char c)i;for (i = 0; i lengthOptr; i+)if (c = optri)return true;return false;bool lex:isSeparator(char c)i;for (i = 0; i lengthS; i+)if (c = si)return true;r

10、eturn false;void lex:push_back(t,line,row,string st)key keyTemp;keyTemp.t = t;keyTemp.line = line;keyTemp.row = row;keyTemp.str = st;outTokenlengthToken = keyTemp;lengthToken+;bool lex:isSpace(char c)if (c = t | c = |c=n)return true;return false;void lex:la()lengthToken = 0;string st = ;line = 1, ro

11、w = 0, c = 0,rowTemp=0;while (c instring.length()st = ;if (instringc = ) | (instringc = t) /空格或者水平制表c+;row+;else if (instringc = r) /換行c=c+2;line+;row = 0;else if (isOptr(instringc)/運算符st = instringc;c+;row+;if (isSpace(instringc) & c = instring.length()push_back(3, line, row, st);elsewhile (c instr

12、ing.length() & !isSpace(instringc)/錯誤& !isSeparator(instringc)st += instringc;c+;rowTemp+;push_back(7, line, row, st);row = row + rowTemp;rowTemp = 0;else if (isSeparator(instringc)/ 分隔符st = instringc;row+;push_back(2, line, row, st);c+;else if (isLetter(instringc) /識別字母st = instringc;c+;row+;while

13、(isLetter(instringc) | isDigit(instringc) /標識符st += instringc;c+;rowTemp+;/判斷是否為關鍵字if (isKey(st)push_back(1, line, row, st);/標識符elsepush_back(6, line, row, st);row = row + rowTemp;rowTemp = 0;else if (isDigit(instringc)st = instringc;c+;row+;while (isDigit(instringc)st += instringc;c+;rowTemp+;if (i

14、sSpace(instringc) | isOptr(c) | isSeparator(c) & c /數(shù)字instring.length()push_back(5, line, row, st);elsewhile (c instring.length() & !isSpace(instringc)/錯誤& !isSeparator(instringc)st += instringc;c+;rowTemp+;push_back(7, line, row, st);row = row + rowTemp;rowTemp = 0;else if (instringc = =)st = instr

15、ingc;row+;push_back(4, line, row, st);c+;else if (instringc = )row+;st = instringc;c+;if (instringc = =)st += instringc;push_back(4, line, row, st);row+;c+;else push_back(4, line, row, st);elserow+;while (c instring.length() & !isSpace(instringc)/錯誤& !isSeparator(instringc)st += instringc;c+;rowTemp

16、+;push_back(7, line, row, st);row = row + rowTemp-1;rowTemp = 0;實驗二#include stdafx.h#include Cll.h#includestack:stack()count = -1;stack:stack()bool stack:isEmpty()if (count = -1)return true;return false;bool stack:isFull()if (count = stackLength - 1)return true;return false;bool stack:getTop(char& c

17、)if (isEmpty()return false;c = dount;return true;bool stack:push(char c)if (isFull()return false;count+;dount = c;return true;bool stack:pop()if (isEmpty()return false;count-;return true;string stack:out()string str = ;for (i = 0; i = count; i+)str+=datai;return str;Cll:Cll()Vn0 = E;Vn1 = G;Vn2 = T;

18、Vn3 = S;Vn4 = F;Vt0 = i;Vt1 = (;Vt2 = );Vt3 = +;Vt4 = -;Vt5 = *;Vt6 = /;Vt7 = #;E0 = TG;E1 = TG;E2 = ERROR;E3 = ERROR;E4 = ERROR;E5 = ERROR;E6 = ERROR;E7 = ERROR;G0 = ERROR;G1 = ERROR;G2 = NULL;G3 = +TG;G4 = -TG;G5 = ERROR;G6 = ERROR;G7 = NULL;T0 = FS;T1 = FS;T2 = ERROR;T3 = ERROR;T4 = ERROR;T5 = ER

19、ROR;T6 = ERROR;T7 = ERROR;S0 = ERROR;S1 = ERROR;S2 = NULL;S3 = NULL;S4 = NULL;S5 = *FS;S6 = /FS;S7 = NULL;F0 = i;F1 = (E);F2 = ERROR;F3 = ERROR;F4 = ERROR;F5 = ERROR;F6 = ERROR;F7 = ERROR;Cll:Cll()bool Cll:isVt(char c)for (i = 0; i 8; i+)if (c = Vti)return true;return false;string Cll:pr(poer)string

20、 str = ;for (i = poer - 1; i strToken.length(); i+)str+=strTokeni;return str;void Cll:saveToToken(step, string stackString, string leftString, string production,string movement)Token t;t.step = step;t.stackString = stackString;t.leftString = leftString;duction = production;t.movement = movement

21、;vt.push_back(t);void Cll:run()std:stringstream ss;bool flag = true;step = 0, poer = 0; /步驟,指針len;string st;ch = strTokenpoer+;stack s;s.push(#);s.push(E);s.getTop(X);saveToToken(step, s.out(), pr(poer), ,初始化);step+;while (flag)if (isVt(X)/規(guī)約結束if (X = #)ss #, 結束);step+;s.pop();flag = false;else if (

22、X = ch) /終結符識別,進行下一字符規(guī)約s.pop();s.getTop(X);ch = strTokenpoer+;saveToToken(step, s.out(), pr(poer), , GENTNEXT(I);step+;elseflag = false;else if (X = Vn0) /Efor (i = 0; i X1X2 的產(chǎn)生式進行入棧操作s.pop();len = Ei.length() - 1;for (j = len; j = 0; j-)s.push(Eij);st += POP,PUSH(;for (j = len; j = 0; j-)st += Eij

23、;st += );ss +Ei, st);step+;s.getTop(X);st = ;else if (X = Vn1)/同上,處理Gfor (i = 0; i 8; i+)if (ch = Vti)if (Gi = NULL)s.pop();ss ,POP);step+;s.getTop(X);else if (Gi = ERROR)flag = false;elses.pop();len = Gi.length() - 1;for (j = len; j = 0; j-)s.push(Gij);st += POP,PUSH(;for (j = len; j = 0; j-)st+= G

24、ij;st += );ss +Gi, st);step+;s.getTop(X);st = ;else if (X = Vn2) /同上 處理 Tfor (i = 0; i = 0; j-)s.push(Tij);st += POP,PUSH(;for (j = len; j = 0; j-)st+= Tij;st+= ) ;ss +Ti, st);step+;s.getTop(X);st = ;else if (X = Vn3)/同上 處理 Sfor (i = 0; i8; i+)if (ch = Vti)if (Si = NULL)s.pop();ss ,POP);step+;s.getT

25、op(X);else if (Si = ERROR)flag = false;elses.pop();len = Si.length() - 1;for (j = len; j = 0; j-)s.push(Sij);st += POP,PUSH(;for (j = len; j = 0; j-)st+= Sij;st += );ss +Si, st);step+;s.getTop(X);st = ;else if (X = Vn4)/同上 處理 Ffor (i = 0; i= 0; j-)s.push(Fij);st += POP,PUSH(;for (j = len; j = 0; j-)

26、st+= Fij;st+=);ss +Fi, st);step+;s.getTop(X);st = ;else /出錯處理flag = false;實驗三#include stdafx.h#include Clr.h#include stack:stack() count = 0;bool stack:empty() const if (count = 0)return true;return false;bool stack:full() const if (count = MaxLen)return true;return false;bool stack:get_top(char &c)

27、const if (empty()return false;elsec = dount - 1;return true;bool stack:push(const char c) if (full()return false;dount+ = c;return true;bool stack:pop() if (empty()return false;count-;return true;string stack:out() string st = ;for (i = 0; icount; i+)st+=datai;return st;string stack:out1()string st

28、= ;stringstream ss;for (i = 0; i count; i+)ss ()datai;st += ss.str();ss.str();return st;Clr:Clr()Clr:Clr()void Clr:saveToToken(step, string susStack, string symbolStack, string inputString,string movement)token t;t.step = step;t.susStack = susStack;t.symbolStack = symbolStack;t.inputString = inputSt

29、ring;t.movement = movement;vt.push_back(t);string Clr:pr(i)string st = ;for (j = i; jstrToken.length(); j+)st+=strTokenj;return st;string Clr:GOTO(i, char c)if (i = 0)if (c = E)r.push(1);return ,GOTO(0,E)=1 入棧;else if (c = T)r.push(2);return ,GOTO(0,T)=2 入棧;else if (c = F)r.push(3);return ,GOTO(0,F)

30、=3 入棧;elseflag = false;else if (i = 4)if (c = E)r.push(8);return ,GOTO(4,E)=8 入棧;else if (c = T)r.push(2);return ,GOTO(4,T)=2 入棧;else if (c = F)r.push(3);return ,GOTO(4,F)=3 入棧;elseflag = false;return ;else if (i = 6)if (c = T)r.push(9);return ,GOTO(6,T)=9 入棧;else if (c = F)r.push(3);return ,GOTO(6,

31、F)=3 入棧;elseflag = false;return ;else if (i = 7)if (c = F)r.push(10);return ,GOTO(7,F)=10 入棧;elseflag = false;return ;elseflag = false;return ;void Clr:Action0()if (ch = i)/下一個操作符為 i ,移進(po- 1), ACTION0,i=S5,狀態(tài) 5 入saveToToken(step, r.out1(), l.out(), pr棧);step+;r.push(5);l.push(ch);ch = strTokenpo+;

32、else if (ch = ()/下一個操作符為( ,移進(po- 1), ACTION0,(=S4,狀態(tài) 4 入saveToToken(step, r.out1(), l.out(), pr棧);step+;r.push(4);l.push(ch);ch = strTokenpo+;elseflag = false;void Clr:Action1()if (ch = +)/下一個操作符為 i ,移進(po- 1), ACTION1,+=S6,狀態(tài) 6saveToToken(step, r.out1(), l.out(), pr入棧);step+;r.push(6);l.push(ch);c

33、h = strTokenpo+;else if (ch = #)/分析成功flag = false;(po- 1), Acc:分析成功);saveToToken(step, r.out1(), l.out(), prstep+;elseflag = false;void Clr:Action2()if (ch = *)/下一個操作符為* ,移進saveToToken(step, r.out1(), l.out(), pr(po- 1), ACTION2,*=S7,狀態(tài) 7入棧);step+;r.push(7);l.push(ch);ch = strTokenpo+;else if (ch =

34、+) | (ch = ) | (ch = #)/下一個操作符為+,),#規(guī)約saveToToken(step, r.out1(), l.out(), pr(po- 1), r2: ET 歸約);step+;l.pop();l.push(E);r.pop();r.get_top(Y);vtvt.size() - 1.movement+=GOTO(Y), E);elseflag = false;void Clr:Action3()if (ch = +) | (ch = *) | (ch = ) | (ch = #)/下一個操作符為+,*,),#規(guī)約saveToToken(step, r.out1(

35、), l.out(), pr(po- 1), r4: TF 歸約);step+;l.pop();l.push(T);r.pop();r.get_top(Y);vtvt.size() - 1.movement += GOTO(Y), T);elseflag = false;void Clr:Action4_6_7(x)if (ch = i)/下一個操作符為 i ,移進string str;stringstream ss;str=ACTION;ss x;str += ss.str();ss.str();str+=,i=S5,狀態(tài) 5 入棧;saveToToken(step, r.out1(), l

36、.out(), pr(po- 1), str);step+;r.push(5);l.push(ch);ch = strTokenpo+;else if (ch = ()/下一個操作符為( ,移進string str;stringstream ss;str = ACTION;ss x;str += ss.str();ss.str();str += ,i=S4,狀態(tài) 4 入棧;saveToToken(step, r.out1(), l.out(), pr(po- 1), str);step+;r.push(4);l.push(ch);ch = strTokenpo+;elseflag = fals

37、e;void Clr:Action5()if (ch = +) | (ch = *) | (ch = ) | (ch = #)/下一個操作符為+,*,),#規(guī)約saveToToken(step, r.out1(), l.out(), pr(po- 1), r6: Fi 歸約);step+;l.pop();l.push(F);r.pop();r.get_top(Y);vtvt.size() - 1.movement += GOTO(Y), F);elseflag = false;void Clr:Action8()if (ch = +)/下一個操作符為+ ,移進- 1), ACTI N8,+=S

38、6,狀態(tài) 6saveToToken(step, r.out1(), l.out(), pr(po入棧);step+;r.push(6);l.push(ch);ch = strTokenpo+;else if (ch = )/下一個操作符為) ,移進- 1), ACTION8,)=S11,狀態(tài) 11saveToToken(step, r.out1(), l.out(), pr(po入棧);step+;r.push(11);l.push(ch);ch = strTokenpo+;elseflag = false;void Clr:Action9()if (ch = *)/下一個操作符為* ,移進- 1), ACTION9,*=S7,狀態(tài) 7saveToToken(step,

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論