LR0分析法编译原理实验.docx

上传人:scccc 文档编号:14550412 上传时间:2022-02-08 格式:DOCX 页数:7 大小:42.50KB
返回 下载 相关 举报
LR0分析法编译原理实验.docx_第1页
第1页 / 共7页
LR0分析法编译原理实验.docx_第2页
第2页 / 共7页
LR0分析法编译原理实验.docx_第3页
第3页 / 共7页
LR0分析法编译原理实验.docx_第4页
第4页 / 共7页
LR0分析法编译原理实验.docx_第5页
第5页 / 共7页
亲,该文档总共7页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《LR0分析法编译原理实验.docx》由会员分享,可在线阅读,更多相关《LR0分析法编译原理实验.docx(7页珍藏版)》请在三一文库上搜索。

1、页脚代码清单:#include #include #include #include #include #include #include #include #include #include #define MAX 507#define DEBUG using namespace std; class WFpublic:string left,right;int back;int id;WF ( char s1 , char s2口,int x , int y )left = s1;right = s2;back = x; id = y;WF ( const string& si , con

2、st string& s2 , int x , int y )left = si;right = s2;back = x; id = y;bool operator ( const WF& a ) constif ( left = a.left ) return right a.right;return left %sn , left.c_str(), right.c_str();class Closurepublic:vector element;void print ( string str )printf ( %-15s%-15sn, str.c_str();for ( int i =

3、0 ; i element.size();i+ )elementi.print();bool operator = ( const Closure& a ) constif ( a.element.size() != element.size() ) return false;for ( int i = 0 ; i a.element.size(); i+ )if ( elementi = a.elementi) continue;else return false; return true;struct Contentint type;int num;string out;Content()

4、 type = -1; Content ( int a , int b ) :type(a),num(b) ;vector wf;mapstring,vector dic;string start = S;vector collection;vector items;char CH = $;int goMAXMAX;int toMAX;vector V;bool usedMAX;Content actionMAXMAX;int GotoMAXMAX;void make_item ()memset ( to , -1 , sizeof ( -1 );for ( int i = 0 ; i wf.

5、size() ; i+ )for ( int j =0 ; j =wfi.right.length() ; j+ )string temp = wfi.right;temp.insert ( temp.begin()+j , CH );dicwfi.left.push_back (items.size();if ( j )toitems.size()-1 = items.size();items.push_back ( WF(wfi.left , temp , i , items.size();#ifdef DEBUGputs( 项目表);for ( int i = 0 ; i %snitem

6、si.left.c_str() , itemsi.right.c_str();puts();#endifvoid make_set ()for ( int i = 0 ; i items.size() ; i+ )if ( itemsi.left0 =S &itemsi.right0 = CH )Closure temp;string& str = itemsi.right;vector& element = temp.element;element.push_back(itemsi);int x = 0;for ( x = 0 ; x str.length();x+ )if ( strx =

7、 CH )break;memset ( has , 0 , sizeof (has );hasi = 1;if ( x != str.length()-1 ) queue q;q.push( str.substr(x+1,1);while ( !q.empty() string u = q.front();q.pop();vector& id = dicu;for( int j = 0 ; j id.size(); j+ )int tx = idj; if(itemstx.right0 = CH ) if ( hastx) continue;hastx = 1; ifbool hasMAX;(

8、isupper(itemstx.right1) q.push ( itemstx.right.substr(1,1); element.push_back ( itemstx); collection.push_back ( temp ); for ( int i = 0 ; i collection.size() ; i+ ) map temp;for ( int j =0; j collectioni.element.size() ; j+ )stringstr=collectioni.elementj.right;int x = 0;for ( ; x str.length() ; x+

9、 ) if ( strx = CH ) break;if ( x = str.length()-1 ) continue;int y = strx+1; int ii;str.erase ( str.begin()+x);str.insert ( str.begin()+x+1 , CH );WF cmp = WF (collectioni.elementj.left , str , -1 , -1 );for ( int k = 0 ; k items.size(); k+ )if ( itemsk = cmp )ii = k; break; memset ( has , 0 , sizeo

10、f (has );vector& element = tempy.element;element.push_back (itemsii);hasii = 1; x+;if (x != str.length()-1 ) queue q;q.push( str.substr(x+1,1);while ( !q.empty() string u = q.front();q.pop();vector& id =dicu;for( int j = 0 ; j id.size(); j+ )int tx = idj;if(itemstx.right0 = CH )if ( hastx) continue;

11、hastx = 1;if(isupper(itemstx.right1) q.push ( itemstx.right.substr(1,1);element.push_back ( itemstx); map:iterator it = temp.begin();for ( ; it != temp.end() ; it+ ) collection.push_back (it-second);for ( int i = 0 ; i collection.size();i+ ) sort( collectioni.element.begin()collectioni.element.end()

12、;for ( int i = 0 ; i collection.size();i+ )for ( int j = i+1 ; j collection.size() ; j+ )if (collection=collection)collection.erase(collection.begin()+j );#ifdef DEBUG puts(CLOSURE);stringstream sin;for ( int i = 0 ; i collection.size() ; i+ ) sin.clear();string out;sin closure-I out;collectioni.pri

13、nt ( out ); puts();#endifvoid make_V () memset ( used , 0 , sizeof ( used );for ( int i = 0 ; i wf.size() ; i+ ) string& str = wfi.left;for ( int j = 0 ; j str.length(); j+ )if ( usedstrj ) continue;usedstrj = 1;V.push_back (strj);string& str1 = wfi.right;for ( int j = 0 ; j str1.length(); j+ )if (

14、usedstr1j ) continue;usedstr1j = 1;V.push_back (str1j);sort ( V.begin(), V.end();V.push_back ( #);void make_cmp ( vector& cmp1 , inti , char ch )for ( int j =0; j collectioni.element.size() ; j+ )stringstr=collectioni.elementj.right;int k;for ( k = 0 ; k str.length() ; k+ ) if ( strk = CH )break;if

15、( k != str.length() - 1 & strk+1=ch )str.erase ( str.begin()+k);str.insert ( str.begin()+k+1 , CH );cmp1.push_back ( WF(collectioni.elementj.left , str , -1 , -1 );sort ( cmp1.begin() , cmp1.end();void make_go ()memset ( go , -1 , sizeof ( go );int m = collection.size();for ( int t = 0 ; t V.size()

16、; t+ )char ch = Vt;for ( int i = 0 ; i m ; i+ )vector cmp1;make_cmp ( cmp1 , i , ch );cout cmp1.size() endl;if ( cmp1.size() =0 )continue;for ( int j = 0 ; j m ; j+ )vector cmp2;for ( int k = 0 ; k collectionj.element.sizeO ; k+ ) string& str = collectionj.elementk.right;int x;for ( x = 0 ; x str.le

17、ngth() ; x+ )if ( strx= CH ) break;if ( x & strx-1= ch )cmp2.push_back(WF( collectionj.elementk.left , str , -1 -1 );sort (cmp2.begin()cmp2.end();cout cmp2.size() endl;bool flag=true;if (cmp2.size() !=cmp1.size() ) continue;cout cmp1.size() endl;for ( intk = 0 ; k cmp1.size() ; k+ )if ( cmp1k = cmp2

18、k ) continue;else flag = false;cout out endl;if (flag )goich = j;#ifdef DEBUGputs(EDGE-);stringstream sin;string out;for ( int i = 0 ; i m ; i+ )for ( int j = 0 ; j m ; j+ )for ( int k = 0 ; k MAX : k+ )if ( goik = j ) sin.clear(); sin I i - (char)(k)-I out;printf ( %sn out.c_str();#endif void make_

19、table () memset ( Goto , -1 , sizeof ( Goto ); for( int i = 0 ; i collection.size() ; i+ ) for ( int j = 0 ; j V.size() ; j+ ) char ch = Vj; int x = goich; if ( x = -1 ) continue; if ( !isupper(ch)actionich = Content (0 , x );elseGotoich = x; for ( int i = 0 ; i collection.size() ; i+ ) for ( int j

20、=0; j collectioni.element.size() ; j+ ) WF&tt=collectioni.elementj; if ( tt.righttt.right.length()-1 =CH ) if ( tt.left0 = S) actioni# = Content ( 2 , -1 );elsefor ( int k = 0 ; k V.size() ; k+ )int y = Vk;actioniy =Content ( 1, tt.back );#ifdef DEBUGputs(一LR(0)分析表);printf ( %10s%5c%5s , | , V0 , |)

21、;for ( int i = 1 ; i V.size() ; i+ )printf ( %5c%5s” , Vi , |);puts ();for ( int i = 0 ; i (V.size()+1)*10 ; i+ ) printf (-);puts();stringstream sin;for ( int i = 0 ; i collection.size() ; i+ )printf (%5d%5s , i, |);for ( int j = 0 ; j V.size() ; j+ )char ch = Vj; if ( isupper(ch) if ( Gotoich = -1

22、) printf ( %10s , |);elseprintf ( %5d%5s”, Gotoich , |); else sin.clear();if ( actionich.type =-1 )printf ( %10s , |); elseContent& temp = actionich;if ( temp.type = 0 ) sin S;if ( temp.type = 1 ) sin R;if ( temp.type = 2 ) sin acc;if (temp.num != -1 ) sin temp.out;printf ( %7s%3s”, temp.out.c_str()

23、, |); puts ();for ( int i = 0 ; i (V.size()+1)*10 ; i+ ) printf (-);puts();#endifvoid print ( string s1 , string s2 , string s3 , string s4 , string s5 , string s6 , string s7 ) printf(%-15s|%-15s%-15s%-20s|%-15s%-15s%-15s n, s1.c_str() , s2.c_str() ,s3.c_str() ,s4.c_str(),s5.c_str(),s6.c_str() , s7

24、.c_str();string get_steps ( int x )stringstream sin;sin ret; return ret;template string get_stk ( vector stk ) stringstream sin;for ( int i = 0 ; i stk.size() ; i+ )sin ret;return ret;string get_shift ( WF& temp )stringstream sin;sin reduce( temp.left temp.right out;return out;void analyse ( string

25、src )print(steps,op-stack ,input,operation,sta te-stack , ACTION , GOTO);vector op_stack;vector st_stack;src+= #;op_stack.push_back ( #);st_stack.push_back ( 0 );int steps= 1;for ( int i = 0 ; i src.length() ; i+ )char u = srci;inttop=st_stackst_stack.size()-1;Content& act = actiontopu;if ( act.type

26、 = 0 )print ( get_steps ( steps+ ), get_stk ( op_stack ) , src.substr(i), shift, get_stk( st_stack ) , act.out ,);op_stack.push_back ( u );st_stack.push_back(act.num );else if ( act.type = 1 )inty=st_stackst_stack.size()-tt.right.length()-1;int x = Gotoytt.left0;print ( get_steps ( steps+ ), get_stk

27、 ( op_stack ) , src.substr(i) , get_shift(tt) ,get_stk( st_stack),act.out,get_st eps(x);for ( int j = 0 ; j tt.right.length(); j+ )st_stack.pop_back();op_stack.pop_back();op_stack.push_back(tt.left0);st_stack.push_back ( x );i-;else if ( act.type = 2 )print ( get_steps( steps+ ), get_stk( op_stack )

28、 , src.substr(i) , Accept, get_stk(st_stack) , act.out ,);else continue;int main ()int n;char sMAX;while ( scanf ( %d , &n )for ( int i = 0 ; i n ; i+ )scanf ( %s , s );int len = strlen(s),j;for ( j = 0 ; j len ; j+ )if ( sj = - ) break;sj = 0;wf.push_back ( WF ( s , s+j+2 ,-1 , -1 );#ifdef DEBUGWF&

29、 tt = wfact.num;wfwf.size()-1.print();make_table();analyse ( abbcde);#endif make_item(); make_set(); make_V(); make_go();运行结果:C:Usersg&SDQCumfrntsDEVE S-E K-aAcB& EaAcE e A/b A-bk-Ab B-d B-Miraxitor-*r*rian上田白Tg:kGOTO4ibbcd,甲ahi ftai b -dia shift02i? abtcrfeil丁如白1.昌-%b)口的aJdA玩的修iMft023由曲,二d胃厂央目一柘屋三3shift023Al血=r.if.U统机UMe#023B7由讣d;三舟shift02367fiisjlctere dll: e It - lAcO2267&1.痛Accept01

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1