計算機與信息學院
編譯原理
實驗報告 專
業
班
級
信息安全 131 班
學生姓名及學號
馬駿 2013211869
課 程 教 學 班 號
任
課
教
師
李宏芒
實 驗 指 導 教 師
李宏芒
實 驗 地 點
實驗樓機房
2015
~2016
學年第 二 學期 驗 實驗 1
詞法分析設計 一、
實驗目得
通過本實驗得編程實踐,使學生了解詞法分析得任務,掌握詞法分析程序設 計得原理與構造方法,使學生對編譯得基本概念、原理與方法有完整得與清楚得 理解,并能正確地、熟練地運用 二、 實驗要求
1、編程時注意編程風格:空行得使用、注釋得使用、縮進得使用等。
2、將標識符填寫得相應符號表須提供給編譯程序得以后各階段使用。
3、根據測試數據進行測試。測試實例應包括以下三個部分:
全部合法得輸入。
各種組合得非法輸入。
由記號組成得句子。
4、詞法分析程序設計要求輸出形式:
例:輸入 VC++語言得實例程序:
If i=0 then
n++;
a﹤= 3b %);
輸出形式為:
單詞
二元序列
類
型
位置(行,列)
(單詞種別,單詞屬性)
for
(1,for )
關鍵字
(1,1)
i
( 6,i )
標識符
(1,2)
=
( 4,= )
關系運算符
(1,3)
12
0
( 5,0 )
常數
(1,4)
then
( 1,then)
關鍵字
(1,5)
n
(6,n )
標識符
(1,6)
++
Error
Error
(1,7)
;
( 2, ; )
分界符
(1,8)
a
(6,a )
標識符
(2,1)
﹤=
(4,<= )
關系運算符
(2,2)
3b
Error
Error
(2,4)
%
Error
Error
(2,4)
)
( 2, ) )
分界符
(2,5)
;
( 2, ; )
分界符
(2,6)
三、
實驗內容 用 VC++/VB/JAVA 語言實現對 C 語言子集得源程序進行詞法分析。通過輸 入源程序從左到右對字符串進行掃描與分解,依次輸出各個單詞得內部編碼及單 詞符號自身值;若遇到錯誤則顯示“Error”,然后跳過錯誤部分繼續顯示 ;同時 進行標識符登記符號表得管理。
以下就是實現詞法分析設計得主要工作:
(1)從源程序文件中讀入字符。
(2)統計行數與列數用于錯誤單詞得定位。
(3)刪除空格類字符,包括回車、制表符空格。
(4)按拼寫單詞,并用(內碼,屬性)二元式表示。(屬性值——token 得機內 表示)
(5)如果發現錯誤則報告出錯
7
(6)根據需要就是否填寫標識符表供以后各階段使用。
四、實驗步驟 1、根據流程圖編寫出各個模塊得源程序代碼上機調試。
2、 編制好源程序后,設計若干用例對系統進行全面得上機測試,并通過所設計 得詞法分析程序;直至能夠得到完全滿意得結果。
3、書寫實驗報告 ;實驗報告正文得內容:
功能描述:該程序具有什么功能?
程序結構描述:函數調用格式、參數含義、返回值描述、函數功能;函數 之間得調用關系圖。
詳細得算法描述(程序總體執行流程圖) 。
給出軟件得測試方法與測試結果。
實驗總結 (設計得特點、不足、收獲與體會)。
五、實驗 截圖
先創建 salary 文件 輸入 If i=0 then
n++;
a<= 3b %);
六、 核心代碼
#include<iostream> #include<string> #include<fstream> #include <sstream> using namespace std; const char* salaryfile="salary"; const int max=40; string id[max]={"do","end","for","if","printf","scanf","then","while"};// 關鍵字表 string s[max]={",",";","(",")","[","]","+","","*","/","<","<=","=",">",">=","<>"};// 分界符表 算數運算符表
關系運算符表 string k[max];// 標識符 string ci[max];// 常數 int fjfpoint=5;// 分界符表尾 int mathpoint=9;// 算數運算符表尾 int cipointer=0;// 常數表尾 int idpointer=0;// 關鍵字表尾 int kpointer=0;// 標識符表尾 int fjf;//0 不就是分界符 1 就是
int rowy=1;// 識別輸入行位置 int rowx=1;// 識別輸入列位置 int outkey=0;// 打印控制 0 為數字后有字母 其她可以 void searcht(int i,string m)// 根據已識別得首字母識別字符串 { // cout<<"enter searcht!!"<<endl;
int x;
if(i==0)// 首字符就是字母識別關鍵字
{ //
cout<<" a word!!"<<endl;
for(x=0;x<max;x++)
{
if(id[x]==m)
{
cout<<"(1,"<<id[x]<<")"<<" 字 關 鍵 字 ("<<rowy<<","<<rowx<<")"<<endl;
break;
}
}
if(x==max)// 不就是關鍵字再識別標識符
{
for(x=0;x<max;x++)
{
if(k[x]==m)
{
cout<<"(6,"<<m<<") "<<" 符 標 識 符 ("<<rowy<<","<<rowx<<")"<<endl;
break;
}
}
if(x==max)// 標識符表沒有時插入標識符
{
cout<<"(6,"<<m<<") "<<" 符 標 識 符 ("<<rowy<<","<<rowx<<")"<<endl;
k[kpointer]=m;
kpointer++;
}
}
}
if(i==1)// 識別常數
{
// cout<<"
a number!!"<<endl;
for(x=0;x<max;x++)
{
if(ci[x]==m)
{
cout<<"(5,"<<x<<")"<<endl;
break;
}
}
if(x==max)
{
cout<<"(5,"<<m<<") 常數 ("<<rowy<<","<<rowx<<")"<<endl;
ci[cipointer]=m;
cipointer++;
}
}
if(i==2)// 識別 分界符 算數運算符
關系運算符
{
// cout<<"
a signal!!"<<endl;
for(x=0;x<max;x++)
{
if(s[x]==m)
break;
}
// x;
if(x<6)
{
fjf=1;
}
if(x>5&&x<10)
{
if(outkey==1)
{
cout<<"(3,"<<s[x]<<") 符 算 數 運 算 符 ("<<rowy<<","<<rowx<<")"<<endl;
outkey=0;
}
fjf=0;
}
if(x>9&&x<max1)
{
if(outkey==1)
{
cout<<"(4,"<<s[x]<<") 符 關 系 運 算 符 ("<<rowy<<","<<rowx<<")"<<endl;
outkey=0;
}
fjf=0;
}
if(x==max)
{
if(outkey==1)
{
cout<<"Error
Error ("<<rowy<<","<<rowx<<")"<<endl;
outkey=0;
}
fjf=0;
}
} }; void wordlook(char t,string sn)// 識別首字符, 分類識別字符串 {
if(t>=48&&t<=57)
searcht(1,sn);
else
{
if((t>64&&t<91)||(t>96&&t<123))
searcht(0,sn);
else searcht(2,sn);
} }; void split(string s)// 分割字符串 { // cout<<s<<endl;
string now[max];
string sn;
int nowpointer=0;
int i=0;
int x;
int sign=2;// 非法數字標志
int diannumber=0;// 數中點得個數
for(x=0;x<s 、length;x++)
{
if((s[x]>64&&s[x]<91)||(s[x]>96&&s[x]<123)||(s[x]>=48&&s[x]<=57)||(x>0&&s[x]==46&&sign==1))// 判斷數字后跟字母還就是字母后有數字
{
if(i==0)
{
if(s[x]>=48&&s[x]<=57)
sign=1;
else sign=2;
}
else
{
if(sign==1)
{
if(s[x]>=48&&s[x]<=57||s[x]==46)
{
if(s[x]==46)
{
if(diannumber==0)
diannumber++;
else sign=0;
}
}
else sign=0;
}
}
i++;
if(x==(s 、length1))
{
sn=s 、substr(xi+1,i);
if(i>0)
{
//
cout<<sn<<" i="<<i<<endl;
cout<<sn<<" ";
if(sign==0)// 數字后有字母得情況
cout<<"
Error
Error
("<<rowy<<","<<rowx<<")"<<endl;
else // 字母開頭得字符串
{
//
cout<<"
true"<<endl;
wordlook(sn[0],sn);
rowx++;
}
}
}
}
else
{
if(x>0&&(s[x1]>64&&s[x1]<91)||(s[x1]>96&&s[x1]<123)||(s[x1]>=48&&s[x1]<=57))// 遇到分界符運算符 如果前面就是數字或字母
{
sn=s 、substr(xi,i);
if(i>0)
{
//
cout<<sn<<" i="<<i<<endl;
cout<<sn<<" ";
if(sign==0)
cout<<" Error Error ("<<rowy<<","<<rowx<<")"<<endl;
else
{
//
cout<<"
true"<<endl;
wordlook(sn[0],sn);
rowx++;
}
}
i=0;
}
string ll=s 、substr(x,1);// 判斷就是運算符還就是分界符
wordlook(s[x],ll);
if(fjf==0)// 就是運算符
{
i++;
if((s[x+1]>64&&s[x+1]<91)||(s[x+1]>96&&s[x+1]<123)||(s[x+1]>=48&&s[x+1]<=57))// 如果后面就 是數字或字母
{
sn=s 、substr(xi+1,i);
// cout<<sn<<" 運算符 i="<<i<<endl;
cout<<sn<<" ";
outkey=1;
wordlook(sn[0],sn);
rowx++;
i=0;
}
}
if(fjf==1)
{
if((s[x1]>64&&s[x1]<91)||(s[x1]>96&&s[x1]<123)||(s[x1]>=48&&s[x1]<=57))// 如 如果前面就是數字或字母
{
}
else if(i>0)
{
sn=s 、substr(xi,i);
// cout<<sn<<" 運算符 i="<<i<<endl;
cout<<sn<<" ";
outkey=1;
wordlook(sn[0],sn);
rowx++;
i=0;
}
cout<<s[x]<<" (2,"<<s[x]<<") 符 分 界 符 ("<<rowy<<","<<rowx<<")"<<endl;
rowx++;
/*
if(ll==";")
{
rowy++;
rowx=1;
}
*/
}
}
} }; int main {
int x;
string instring;// 讀入一行
string sn; /*
getline(cin,sn);// string 帶空格輸入
cout<<sn<<endl;
char t=sn[0];
if(t>=48&&t<=57)
searcht(1,sn);
else
{
if((t>64&&t<91)||(t>96&&t<123))
searcht(0,sn);
else searcht(2,sn);
} */
ifstream input
input(salaryfile); // inputfile>>noskipws;
if(!inputfile)
{
cout<<"no file"<<endl;
}
string pp;
while(!input)
{
getline(input);
istringstream istr(pp);
string ppword;
while(istr>>ppword)// 按照空格分割字符串
{
split(ppword);
} /*
int begin = 0;// 去掉字符串得所有空格
begin = pp 、find(" ",begin);
// 查找空格在 str 中第一次出現得位置
while(begin != 1)
// 表示字符串中存在空格
{
pp 、replace(begin, 1, "");
// 用空串替換 str 中從 begin 開始得 1 個字符 符
begin = pp 、find(" ",begin);
// 查找空格在替換后得 str 中第一次出現得位置
}
*/ // cout<<"good
"<<pp<<endl; // rowx++;
rowy++;// 換行
rowx=1;
}
return 0; } 七、 實驗總結
通過本次試驗使我不僅對詞法分析器有了更深得了解,而且提高了編程能力,希望在以后得學習中可以解決詞法分析更多得問題。
驗 實驗 2
LL(1) 分析法
一、 實驗目得 通過完成預測分析法得語法分析程序,了解預測分析法與遞歸子程序法得區 別與聯系。使學生了解語法分析得功能,掌握語法分析程序設計得原理與構造方 法,訓練學生掌握開發應用程序得基本方法。有利于提高學生得專業素質,為培 養適應社會多方面需要得能力。
二、 實驗要求 1、編程時注意編程風格:空行得使用、注釋得使用、縮進得使用等。
2、如果遇到錯誤得表達式,應輸出錯誤提示信息。
3、對下列文法,用 LL(1)分析法對任意輸入得符號串進行分析:
(1)E>TG
(2)G>+TG|—TG
(3)G>ε
(4)T>FS
(5)S>*FS|/FS
(6)S>ε
(7)F>(E)
(8)F>i
三、 實驗內容 根據某一文法編制調試
LL (
1 )分析程序,以便對任意輸入得符號串 進行分析。
構造預測分析表,并利用分析表與一個棧來實現對上述程序設計語言得分 析程序。
分析法得功能就是利用 LL(1)控制程序根據顯示棧棧頂內容、向前瞧符號 以及 LL(1)分析表,對輸入符號串自上而下得分析過程。
四、實驗步驟
1、根據流程圖編寫出各個模塊得源程序代碼上機調試。
2、 編制好源程序后,設計若干用例對系統進行全面得上機測試,并通過所設計 得 LL(1)分析程序;直至能夠得到完全滿意得結果。
3、書寫實驗報告 ;實驗報告正文得內容:
寫出 LL(1)分析法得思想及寫出符合 LL(1)分析法得文法。
程序結構描述:函數調用格式、參數含義、返回值描述、函數功能;函數 之間得調用關系圖。
詳細得算法描述(程序執行流程圖) 。
給出軟件得測試方法與測試結果。
實驗總結 (設計得特點、不足、收獲與體會)。
五、實驗 截圖
六、 核心代碼
#include<iostream> #include<string> using namespace std; string pp;// 輸出字符串 string hh="\r\n";// 換行 const int max=50; int endfu[max];// 終止符序號表 int endfupointer=8; char endfureal[max]={"+","","*","/","(","i",")","#"}; int unendfu[max]; int unendfupointer=5; char unendfureal[max]={"E","G","T","S","F"}; string makemath[max]={"E>TG","G>+TG","G>TG","G>$","T>FS","S>*FS","S>/FS","S>$","F>(E)","F>i"}; //0 E>TG,1 G>+TG,2 G>TG,3 G>$,4 T>FS,5 S>*FS,6 S>/FS,7 S>$,8 F>(E),9 F>i //$ 代表空串 string behavior[max]={" 初始化","POP"}; int smarttable[max][max];// 分析表 int checkendfu(char fu)// 查終結符序號 {
int x;
for(x=0;x<endfupointer;x++)
{
if(endfureal[x]==fu)
{
break;
}
}
if(x<endfupointer)
return x;
else return 1; }; int checkunendfu(char fu)// 查非終結符序號 {
int x;
for(x=0;x<unendfupointer;x++)
{
if(unendfureal[x]==fu)
break;
}
if(x<unendfupointer)
return x;
else return1; }; string checkmakemath(int x)// 查產生式表 {
return makemath[x]; }; int checksmarttable(int x,int y)// 查分析表 {
return smarttable[x][y]; }; class smartbox { public:
smartbox
{
box[0]="#";
box[1]="E";
boxpointer=1;
}
void push(char fu)
{
boxpointer++;
box[boxpointer]=fu;
}
char pop
{
char a=box[boxpointer];
if(a!="#")
{ //
cout<<"pop: "<<boxpointer<<"
"<<a<<endl; //
stringstream oss; /*
pp=pp+"pop: ";
char buffer[max];
sprintf(buffer,"%d",boxpointer);
string s=buffer;
pp=pp+" ";
pp=pp+s;
pp=pp+hh; */
boxpointer;
return a;
}
}
void check
{
if(checkendfu(box[boxpointer])!=1)
{
char a; //
cout<<box[boxpointer]<<checkendfu(box[boxpointer])<<"
";
//char buffer[max];
//sprintf(buffer //
pp=pp+box[boxpointer];
// pp=pp+checkendfu(box[boxpointer]); //
pp=pp+"
";
a=pop;
// cout<<"out "<<a<<endl;
}
}
char box[max];
int boxpointer; }; int main {
// TODO: Add extra validation here // pp=pp+" 步驟
分析棧
剩余輸入串
所用產生式
動作"+hh; /*
string s1="sdsfs
\r\nsdfsds";
string s3="aaaaaaaaaaaaa";
s3=s3+s1;
CString s2(s3 、c_str); // CString s2=CString(s1);
SetDlgItemText(IDC_EDIT2,s2);// 用 SetDlgItemText( 文本框 ID, 字符串), 將文本框得內容設置為字符串得內容、
SetDlgItemText(IDC_EDIT2,s2); */ // MessageBox(str); string str; cin>>str;
int x,y;
for(x=0;x<max;x++)
// 初始化分析表 99 為錯誤代號
for(y=0;y<max;y++)
smarttable[x][y]=99;
smarttable[0][4]=0;
smarttable[0][5]=0;
smarttable[1][0]=1;
smarttable[1][1]=2;
smarttable[1][6]=3;
smarttable[1][7]=3;
smarttable[2][4]=4;
smarttable[2][5]=4;
smarttable[3][0]=7;
smarttable[3][1]=7;
smarttable[3][2]=5;
smarttable[3][3]=5;
smarttable[3][6]=7;
smarttable[3][7]=7;
smarttable[4][4]=8;
smarttable[4][5]=9;
smartbox mark;
char fu;
char enterfu;
int endfunumber;
int unendfunumber;
string readyin;
string enter; // cin>>enter;
//enter="i+i*i#";
enter=str; //enter="(i)#";
int count=0; // 步驟
char buffer1[max];
sprintf(buffer1,"%d",count);
string s1=buffer1;
pp=pp+s1+"
";
// 分析棧
for(int qq1=0;qq1<=mark 、boxpointer;qq1++)
{
pp=pp+mark 、box[qq1];
}
for(qq1=0;qq1<10mark 、boxpointer;qq1++)
{
pp=pp+" ";
}
// 剩余輸入棧
string jiequ1=enter 、substr(0,enter 、length);
pp=pp+jiequ1;
for(int t1=0;t1<20;t1++)
{
pp=pp+" ";
}
pp=pp+"
初始化"+hh;
for(x=0;x<enter 、length;)
{
// 步驟
count++;
char buffer[max];
sprintf(buffer,"%d",count);
string s=buffer;
pp=pp+s+"
";
// 分析棧
for(int qq=0;qq<=mark 、boxpointer;qq++)
{
pp=pp+mark 、box[qq];
}
for(qq=0;qq<10mark 、boxpointer;qq++)
{
pp=pp+" ";
}
// 剩余輸入棧
string jiequ=enter 、substr(x,enter 、lengthx);
pp=pp+jiequ;
for(int t=0;t<x+10;t++)
{
pp=pp+" ";
}
enterfu=enter[x];
// cout<<"enterfu: "<<enterfu<<endl;
mark 、check;
fu=mark 、pop; //
cout<<"fu: "<<fu<<endl;
if(fu=="#")//&&enterfu=="#")
{
//
cout<<"sucessed!!!!
over!!!!"<<endl;
pp=pp+"sucessed!!!!
over!!!!"+hh;
break;
}
unendfunumber=checkunendfu(fu);
endfunumber=checkendfu(enterfu);
// cout<<unendfunumber<<endl;
// cout<<endfunumber<<endl;
if(smarttable[unendfunumber][endfunumber]==99)
{
pp=pp+"error!!";
break;
}
readyin=makemath[smarttable[unendfunumber][endfunumber]];
pp=pp+readyin;
for(int ddd=0;ddd<14readyin 、length;ddd++)
{
pp=pp+" ";
}
pp=pp+"POP,PUSH(";
for(y=readyin 、length1;y>2;y)
{
pp=pp+readyin[y];
}
pp=pp+")"+hh;
// cout<<"readyin:
"<<readyin<<endl;
int firsttime=0;
for(y=readyin 、length1;y>2;y)
{
// cout<<readyin[y]<<" ";
if(readyin[y]!="$")
{
mark 、push(readyin[y]);
if(firsttime==0)
{
if(checkendfu(readyin[y])!=1)
{
//
cout<<"now x: "<<x<<"
";
// 步驟
count++;
char buffer[max];
sprintf(buffer,"%d",count);
string s=buffer;
pp=pp+s+"
";
// 分析棧
for(int qq=0;qq<=mark 、boxpointer;qq++)
{
pp=pp+mark 、box[qq];
}
for(qq=0;qq<10mark 、boxpointer;qq++)
{
pp=pp+" ";
}
// 剩余輸入棧
string jiequ=enter 、substr(x,enter 、lengthx);
pp=pp+jiequ;
for(int t=0;t<x+10;t++)
{
pp=pp+" ";
}
pp=pp+"
GETNEXT(I)"+hh;
x++;
//
cout<<"next x: "<<x<<"
"<<endl;
firsttime=1;
}
}
}
}
mark 、check;
// cout<<endl; //
pp=pp+hh;
} cout<<pp; return 0;
//CDialog::OnOK; } 七、 實驗總結
通過本次試驗使我不僅對 ll(1)分析法有了更深得了解,而且提高了編程能力,希望在以后得學習中可以解決自動構造 follow 集得問題。
實驗 3
LR(1) 分析法
一、 實驗目得 構造 LR(1)分析程序,利用它進行語法分析,判斷給出得符號串就是否為該文 法識別得句子,了解 LR(K)分析方法就是嚴格得從左向右掃描,與自底向上得 語法分析方法。
二、 實驗要求 1、編程時注意編程風格:空行得使用、注釋得使用、縮進得使用等。
2、如果遇到錯誤得表達式,應輸出錯誤提示信息。
3、程序輸入/輸出實例:
輸入一以#結束得符號串(包括+*i#):在此位置輸入符號串
輸出過程如下:
步驟
狀態棧
符號棧
剩余輸入串
動 作
1
0
#
i+i*i#
移進
i+i*i 得 LR 分析過程
步驟
狀態棧
符號棧
輸入串
動作說明
1
0
#
i+i*i#
ACTION[0,i]=S5,狀態 5 入棧
2
05
#i
+i*i#
r6: F→i 歸約,GOTO(0,F)=3 入棧
3
03
#F
+i*i#
r4: T→F 歸約,GOTO(0,T)=3 入棧
4
02
#T
+i*i#
r2: E→T 歸約,GOTO(0,E)=1 入棧
5
01
#E
+i*i#
ACTION[1,+]=S6,狀態 6 入棧
6
016
#E+
i*i#
ACTION[6,i]=S5,狀態 5 入棧
7
0165
#E+i
*i#
r6: F→i 歸約,GOTO(6,F)=3 入棧
8
0163
#E+F
*i#
r4: T→F 歸約,GOTO(6,T)=9 入棧
9
0169
#E+T
*i#
ACTION[9,*]=S7,狀態 7 入棧
10
01697
#E+T*
i#
ACTION[7,i]=S5,狀態 5 入棧
11
016975
#E+T*i
#
r6:F→i 歸約,GOTO(7,F)=10 入棧
12
0169710
#E+T*F
#
r3: T→T*F 歸約,GOTO(6,T)=9 入棧
13
0169
#E+T
#
r1:E→E+T,GOTO(0,E)=1 入棧
14
01
#E
#
Acc:分析成功
三、 實驗內容 對下列文法,用 LR(1)分析法對任意輸入得符號串進行分析:
(1)E> E+T
(2)E>T
(3)T> T*F
(4)T>F
(5)F> (E)
(6)F> i
四、實驗步驟 1、根據流程圖編寫出各個模塊得源程序代碼上機調試。
2、編制好源程序后,設計若干用例對系統進行全面得上機測試,并通過所設計 得 LR(1)語法分析程序;直至能夠得到完全滿意得結果。
3、書寫實驗報告 ;實驗報告正文得內容:
描述 LR(1)語法分析程序得設計思想。
程序結構描述:函數調用格式、參數含義、返回值描述、函數功能;函數
之間得調用關系圖。
詳細得算法描述(程序執行流程圖) 。
給出軟件得測試方法與測試結果。
五、實驗 截圖
六、 核心代碼 #include<iostream> #include<string> using namespace std; int count=1; string pp;// 輸出字符串 string hh="\r\n";// 換行 const int max=100; char endfureal[max]={"i","+","*","(",")","#","E","T","F"}; int endfupointer=8; string creatword[max]={"E>E+T","E>T","T>T*F","T>F","F>(E)","F>i"}; //(0)E> E+T(1)E>T (2)T> T*F (3)T>F (4)F> (E) (5)F> i
// 注意 rj 時 時 j 應對應數組下標 int checkendfu(char fu)// 查終結符序號 {
int x;
for(x=0;x<=endfupointer;x++)
{
if(endfureal[x]==fu)
{
break;
}
}
if(x<=endfupointer)
return x;
else return 1; }; class actiongo// 原子動作 { public:actiongo
{
// cout<<"eroor! wrong action or goto creat"<<endl;
actype=4;
}
action(int i,int j)
{
actype=i;
number=j;
//
cout<<i<<" "<<j<<endl;
}
int actype;//0=s 1=r 2=acc 3=goto 表 4=wrong
int number; }; actiongo smarttable[max][max]; class stylebox// 狀態棧 { public:
stylebox
{
box[0]=0;
boxpointer=0;
}
void push(int style)
{
boxpointer++;
box[boxpointer]=style;
}
int pop
{
int a=box[boxpointer];
boxpointer; //
cout<<"pop now"<<endl;
return a;
}
int box[max];
int boxpointer;
}; class readybox// 已歸約棧 { public:
readybox
{
box[0]="#";
boxpointer=0;
}
void push(char fu)
{
boxpointer++;
box[boxpointer]=fu;
}
char pop
{
char a=box[boxpointer];
boxpointer;
return a;
}
char box[max];
int boxpointer; }; int program(stylebox&style,readybox&ready,char fu,int icount,string ptr,int control)// 返回就是否需要移進信號 { if(control==1) {
//////////////////////////////
char ber[max];
sprintf(ber,"%d",count);
string st=ber;
pp=pp+st+"
";
count++;
////////////////////////////////////
//
pp=pp+"
";
for(int qq=0;qq<=style 、boxpointer;qq++)
{
char bss[max];
sprintf(bss,"%d",style 、box[qq]);
string st=bss;
pp=pp+st;
}
for(qq=0;qq<10style 、boxpointer;qq++)
{
pp=pp+" ";
}
for( qq=0;qq<=ready 、boxpointer;qq++)
{
pp=pp+ready 、box[qq];
}
for(qq=0;qq<10ready 、boxpointer;qq++)
{
pp=pp+" ";
}
for(int dj=icount;dj<ptr 、length;dj++)
{
pp=pp+ptr[dj];
}
pp=pp+"
"; //
////////////////////////////////////////
}
int i=style 、pop;// 如果就是 s 則狀態還要入棧 // cout<<" 現在狀態"<<i;
int j=checkendfu(fu);
if(j==1)
{
pp=pp+"error!!!";
return 4;//wrong!!!
} // cout<<"
現在符號: "<<fu<<" "<<j<<endl;
int checkstyle=smarttable[i][j] 、actype;// 查表
if(checkstyle==4)
{
pp=pp+"error!!!";
return 4;//wrong!!!
}
if(checkstyle==0||checkstyle==3)
{
style 、push(i);// 如果就是 s 則狀態還要入棧
if(checkstyle==0)
{
//cout<<"ACTION["<<i<<","<<fu<<"]=S"<<smarttable[i][j] 、number<<",狀 狀態 態"<<smarttable[i][j] 、number<<" 入棧"<<endl;
pp=pp+"ACTION[";
char buffer[max];
sprintf(buffer,"%d",i);
string s=buffer;
pp=pp+s;
pp=pp+",";
pp=pp+fu;
pp=pp+"]=S";
char buf[max];
sprintf(buf,"%d",smarttable[i][j] 、number);
s=buf;
pp=pp+s;
pp=pp+", 狀態";
pp=pp+s+" 入棧"+hh;
}
if(checkstyle==3)
{
// cout<<i<<","<<endfureal[j]<<")="<<smarttable[i][j] 、 number<<"入棧"<<endl;
char bf[max];
sprintf(bf,"%d",i);
string s=bf;
pp=pp+s;
pp=pp+","+endfureal[j]+")=";
char bfff[max];
sprintf(bfff,"%d",smarttable[i][j] 、number);
s=bfff;
pp=pp+s+" 入棧"+hh;
}
ready 、push(fu);
style 、push(smarttable[i][j] 、number); //
cout<<style 、box[style 、boxpointer]<<endl;
return 1;// 移進
}
if(checkstyle==1)
{
string l=creatword[smarttable[i][j] 、number];
// cout<<"r"<<smarttable[i][j] 、number+1<<": "<<l<<" 歸約,GOTO(";
pp=pp+"r";
int dd=smarttable[i][j] 、number+1;
char bfc[max];
sprintf(bfc,"%d",dd);
string ss=bfc;
pp=pp+ss+": "+l+" 歸約,GOTO(";
char n;//pop 用得
int x;
// cout<<l 、length<<endl;
char sq=ready 、pop;
for(x=0;x<l 、length4;x++)// 前面 pop 一次了
{
n=style 、pop;
n=ready 、pop; //
cout<<n<<endl;
}
// cout<<l[0]<<endl;
x=program(style,ready,l[0],icount,ptr,0);// 識別非終結符
return 0;// 歸約
}
if(checkstyle==2)
{
return 2;// 接受
} } int main {
// TODO: Add extra validation here
string str;
cin>>str; //0=i 1=+ 2=* 3=( 4=) 5=# 6=E 7=T 8=F
smarttable[0][0] 、action(0,5);
smarttable[0][3] 、action(0,4);
smarttable[0][6] 、action(3,1);
smarttable[0][7] 、action(3,2);
smarttable[0][8] 、action(3,3);
smarttable[1][1] 、action(0,6);
smarttable[1][5] 、action(2,0);
smarttable[2][1] 、action(1,1);
smarttable[2][2] 、action(0,7);
smarttable[2][4] 、action(1,1);
smarttable[2][5] 、action(1,1);
smarttable[3][1] 、action(1,3);
smarttable[3][2] 、action(1,3);
smarttable[3][4] 、action(1,3);
smarttable[3][5] 、action(1,3);
smarttable[4][0] 、action(0,5);
smarttable[4][3] 、action(0,4);
smarttable[4][6] 、action(3,8);
smarttable[4][7] 、action(3,2);
smarttable[4][8] 、action(3,3);
smarttable[5][1] 、action(1,5);
smarttable[5][2] 、action(1,5);
smarttable[5][4] 、action(1,5);
smarttable[5][5] 、action(1,5);
smarttable[6][0] 、action(0,5);
smarttable[6][3] 、action(0,4);
smarttable[6][7] 、action(3,9);
smarttable[6][8] 、action(3,3);
smarttable[7][0] 、action(0,5);
smarttable[7][3] 、action(0,4);
smarttable[7][8] 、action(3,10);
smarttable[8][1] 、action(0,6);
smarttable[8][4] 、action(0,11);
smarttable[9][1] 、action(1,0);
smarttable[9][2] 、action(0,7);
smarttable[9][4] 、action(1,0);
smarttable[9][5] 、action(1,0);
smarttable[10][1] 、action(1,2);
smarttable[10][2] 、action(1,2);
smarttable[10][4] 、action(1,2);
smarttable[10][5] 、action(1,2);
smarttable[11][1] 、action(1,4);
smarttable[11][2] 、action(1,4);
smarttable[11][4] 、action(1,4);
smarttable[11][5] 、action(1,4); string enter=str;
int i;
int x;
stylebox st;
readybox ready;
for(i=0;i<enter 、length;)
{
// cout<<"enter:
"<<enter[i]<<"
"<<endl;
x=program(st,ready,enter[i],i,enter,1);
if(x==1)i++;
if(x==2)
{
// cout<<"Acc: 分析成功 功"<<endl;
pp=pp+"Acc: 分析成功"+hh;
break;
}
if(x==4)
break;
// cout<<endl;
// cout<<endl;
// pp=pp+hh;
} cout<<pp; // CDialog::OnOK; } 七、 實驗總結
通過本次試驗使我不僅對 LR(1)分析法有了更深得了解,而且提高了編程能力,希望在以后得學習中可以解決 LR(1)中確定化得問題。
推薦訪問: 合肥 工業大學 編譯上一篇:集團“三基建設”經驗交流材料
下一篇:XX企業復工復產工作方案
在偉大祖國73華誕之際,我參加了單位組織的“光影鑄魂”主題黨日活動,集中觀看了抗美援朝題材影片《長津湖》,再一次重溫這段悲壯歷史,再一次深刻感悟偉大抗美援朝精神。1950年10月,新中國剛剛成立一年,
根據省局黨組《關于舉辦習近平談治國理政(第四卷)讀書班的通知》要求,我中心通過專題學習、專題研討以及交流分享等形式,系統的對《習近平談治國理政》(第四卷)進行了深入的學習與交流,下面我就來談一談我個人
《習近平談治國理政》(第四卷)是在百年變局和世紀疫情相互疊加的大背景下,對以習近平同志為核心的黨中央治國理政重大戰略部署、重大理論創造、重大思想引領的系統呈現。它生動記錄了新一代黨中央領導集體統籌兩個
《真抓實干做好新發展階段“三農工作”》是《習近平談治國理政》第四卷中的文章,這是習近平總書記在2020年12月28日中央農村工作會議上的集體學習時的講話。文章指出,我常講,領導干部要胸懷黨和國家工作大
在《習近平談治國理政》第四卷中,習近平總書記強調,江山就是人民,人民就是江山,打江山、守江山,守的是人民的心。從嘉興南湖中駛出的小小紅船,到世界上最大的執政黨,在中國共產黨的字典里,“人民”一詞從來都
黨的十八大以來,習近平總書記以馬克思主義戰略家的博大胸襟和深謀遠慮,在治國理政和推動全球治理中牢固樹立戰略意識,在不同場合多次圍繞戰略策略的重要性,戰略和策略的關系,提高戰略思維、堅定戰略自信、強化戰
《習近平談治國理政》第四卷集中展示了以習近平同志為核心的黨中央在百年變局和世紀疫情相互疊加背景下,如何更好地堅持和發展中國特色社會主義而進行的生動實踐與理論探索;對于新時代堅持和發展什么樣的中國特色社
在黨組織的關懷下,我有幸參加了區委組織部組織的入黨積極分子培訓班。為期一周的學習,學習形式多樣,課程內容豐富,各位專家的講解細致精彩,對于我加深對黨的創新理論的認識、對黨的歷史的深入了解、對中共黨員的
《習近平談治國理政》第四卷《共建網上美好精神家園》一文中指出:網絡玩命是新形勢下社會文明的重要內容,是建設網絡強國的重要領域。截至2021年12月,我國網民規模達10 32億,較2020年12月增長4
剛剛召開的中國共產黨第十九屆中央委員會第七次全體會議上討論并通過了黨的十九屆中央委員會向中國共產黨第二十次全國代表大會的報告、黨的十九屆中央紀律檢查委員會向中國共產黨第二十次全國代表大會的工作報告和《