logo资料库

LR(0)自底向上语法分析 编译原理课程设计.doc

第1页 / 共39页
第2页 / 共39页
第3页 / 共39页
第4页 / 共39页
第5页 / 共39页
第6页 / 共39页
第7页 / 共39页
第8页 / 共39页
资料共39页,剩余部分请下载后查看
 LR(0)自底向上语法分析  //fiel main.cpp #include"node.h" #include"node1.h" #include"iostream" #include #include #include using namespace std; #define n1 3 //规则的条数 #define n2 1 //非终极符的个数 #define n3 2 //终极符的个数 #define n4 10 //项目族集个数 #define left 1 //点在左边 #define middle 2 //点在中间 #define right 3 //点在右边 FILE *f; //定义一个文件变量 linklist G[n1]; //文法的规则 linklist G_temp[n1]; //文法的规则副本 string N[n2]; //非终极符 string T[n3]; linklist I[n4]; //终极符 //项目集 linklist i_temp[n4]; //项目集副本 //分析栈 stack S; string line; enum VT{a,b,O}; enum VN{A};
node item[n4]; //项目 int number = 1; //计数器 struct Form { }; string first; int second; struct Form ACTION[5][3]; //action 表 int GOTO[5] = {0}; //goto 表 struct Form_dfa { }; string first; int second; int flags; //字母 //ID 号 //有效标志位 struct Form_dfa DFA[5][5]; //G[S']活前缀的 DFA void closure(node*); //生成新的项目集 void GO(linklist&, string); //生成新的项目集族 node *check(linklist &l, string s); //生成新的项目 node *DFA_check(linklist &l, string s);//生成新的项目 void Init(); //数据初始化部分 bool Same_check(node&, node*); //查找是否有相同的项目 void new_item(int j,node *pp); //生成 I0 后面的项目集 void make_DFA(); //构造 G[S']活前缀的 DFA void make_ActionForm(); //填写 action 表 void make_GotoForm(linklist &l); //填写 goto 表 bool control(); //语法分析主控程序 void copy_item(linklist&,linklist&); //复制 int main()
{ Init(); cout << "文法 G[S']:" << endl; node *temp = G[0].Getdata(); closure(temp); I[0].show(); GO(I[0],"a"); GO(I[0],"b"); GO(I[0],"A"); GO(I[1],"A"); for(int i = 0; i < n4; i++) { } if(i_temp.Getdata()) copy_item(i_temp,I); for(int i = 0; i < n4; i++) { } if(I.Getdata()) { cout << i < I.show(); cout << "ID:" << I.GetID() << " 符号:" << I.getSingal() <<" 规约标志:"< } make_DFA(); //构造 G[S']活前缀的DFA for(int i = 1; i < 5; i++) { } make_GotoForm(I); make_ActionForm(); cout << "ACTION 表" << endl; for(int i = 0; i < 5; i++)
{ cout << i << ": "; for(int j = 0; j <3; j++) cout << ACTION[j].first << ACTION[j].second << " " ; if(j == 2) cout << endl; { } } cout << "GOTO 表" << endl; for(int i = 0; i <= 4; i++) cout << GOTO << endl; for(int i = 1; i < n4; i++) if(item.point != 0) cout << item.data << "->" << item.data1 << " " << item.point << endl; node *first = G[0].Getdata(); for(int i = 1; i < n4; i++) { } if( Same_check(item,first) ) { } cout <<"找到了!"<< endl; cout << first->data << "->" << first->data1 << " " << first->point << endl; cout << "DFA" << endl; for(int i = 0; i < 5; i++) cout << " " << i ; cout << endl; for(int i = 0; i < 5; i++) { cout << i <<": ";
for(int j = 0; j < 5; j++) cout << DFA[j].flags << DFA[j].first << DFA[j].second << " "; if(j == 4) cout << endl; { } } // ifstream constructor opens the file ifstream inClientFile( "c.txt", ios::in ); if ( !inClientFile ) { cerr << "File could not be opened "; exit( 1 ); } while ( inClientFile >> line) cout << line << endl; if(control()) cout <<"编译成功!"< else cout << "有语法错误!" << endl; system("PAUSE"); return 0; } void Init() { T[0] = "a"; T[1] = "b"; //终极符 //终极符 N[0] = "A"; //非终极符 G[0].Insert("S'","A",0,0);//文法 G[1].Insert("A","aA",0,0);
G[2].Insert("A","b",0,0); G[0].Insert("A","aA",0,0); G[0].Insert("A","b",0,0); G_temp[0].Insert("S'","A",0,0); G_temp[1].Insert("A","aA",0,0); G_temp[2].Insert("A","b",0,0); //line = "abb#"; for(int i = 0; i < n4; i++) I.setID(i); //设置每个项目集的ID 号 i_temp.setID(i); //备份 { } } void make_DFA() //构造 G[S']活前缀的DFA { for(int i = 0; i < 5; i++) //初始化DFA 表 DFA[0].flags = 0; for(int i = 1; i < 5; i++) { //对该项目集自身的关系进行分析 if(I.getFlag()!= 1) //若该项目集是非规约项目集 { node *dfa_temp1; //node *dfa_temp2; dfa_temp1 = DFA_check(i_temp,"a"); //dfa_temp2 = DFA_check(i_temp,"b"); //I[1].show(); //cout << dfa_temp1->data <<"->" << dfa_temp1->data1 << dfa_temp1->point < if( I.Find(dfa_temp1)) //若找到了 { //cout << "++&&&&&&&&&&++" << endl;
DFA[1].flags = 1; DFA[1].first = "a"; DFA[1].second = I.GetID(); } /* if( !k && I.Find(dfa_temp2)) //若找到了 { k = 1; DFA[1].flags = 1; DFA[1].first = "b"; DFA[1].second = I.GetID(); } */ } copy_item(I,i_temp); //还原数据 ////////////////////////////////////////////////////////// if(I.getFlag()== 1) //若该项目集是规约项目集 { } for(int j = 0; j < 5; j++) DFA[I.GetID()][j].flags = 0; //对该项目集与其他的项目集之间的关系进行分析 DFA[I.GetUppoint()][I.GetID()].flags = 1; DFA[I.GetUppoint()][I.GetID()].first = I.getSingal(); DFA[I.GetUppoint()][I.GetID()].second = I.GetID(); } for(int i = 2; i < 5; i++) { //for(int j = 1; j < i-1; j++) //{
//int i = 2; int j = 1; node *p = i_temp.Getdata(); (p->point)--; if( I[j].Find(p) ) //若找到相同的项目 { //cout <<"++&&&&&&&&++" << endl; DFA[j].flags = 1; DFA[j].first = I.getSingal(); DFA[j].second = I.GetID(); } copy_item(I,i_temp); //还原数据 //} } } void make_ActionForm() //填写action 表 { cout << "调用make_ActionForm" << endl; for(int i = 0; i < 5; i++) { for(int j = 0; j < 5; j++) { cout << DFA[j].flags << " "; if(j == 4)cout << endl; if(DFA[j].flags == 1) //若该元素有效 { //cout << "1111111" << endl; int k = DFA[j].second; node *DFA_temp = I[k].Getdata(); if( I[k].getFlag() != 1 ) //若该项目集是非规约项目集 { //cout << "222222222" << endl;
分享到:
收藏