永发信息网

编译原理语法分析器程序设计,用C语言或C++,哪里有这个程序

答案:2  悬赏:60  手机版
解决时间 2021-02-23 20:13
  • 提问者网友:酱爆肉
  • 2021-02-23 01:45
编译原理语法分析器程序设计,用C语言或C++,哪里有这个程序
最佳答案
  • 五星知识达人网友:舊物识亽
  • 2021-02-23 02:58
1.文法简略,没有实现的部分,可以在此文法的基础上进行扩充,本程序的采用自顶向下的LL(1)文法。
2.可以自动实现求First 集和 Follow 集。
3.处终结符外(有些硬编码的成分),终结符的文法可以自定义,也就是说读者可以自定义文法。
4.为方便理解,C语言的文法描述写成中文。
5.程序将词法分析和语法分析结合起来,词法分析的结果作为语法分析的输入。
6.最终结果在控制台显示的有:词法分析、First集、Follow集、Select集,在preciateResult.txt 中写入了语法分析结果,在preciateTable.txt 中写入了预测分析表。
7.文法的词素之间必须有空格分开。
全部回答
  • 1楼网友:洎扰庸人
  • 2021-02-23 04:34
我写好的. scan.h #ifndef_scan_h_ #define_scan_h_ #include<string> #include<fstream> usingnamespacestd; typedefenum { endfile,error, else,if,int,return,void,while, id,num, assign,eq,lt,gt,le,ge,ne,add,sub,mul,div,semi,lparen,rparen,lzk,rzk,ldk,rdk,comma } tokentype; classscan { private: stringtokenstr; stringlinebuffer; ifstream*in; intlinepos; intlineno; booleof_flag; booltracescan; voidprinttoken(tokentypett,conststring&tok); public: scan(ifstream*in) { this->in=in; linepos=0; linebuffer=""; lineno=0; eof_flag=false tracescan=true; } chargetnextchar(); voidungetnextchar(); tokentypereservedlookup(string&s); voidsettracescan(boolf); boolgettracescan(); tokentypegettoken(); stringgettokenstr(); }; #endif scan.cpp #include<string> #include<fstream> #include<iostream> usingnamespacestd; #include"scan.h" typedefenum {start,innum,inid,innoteq,tempe,tempg,templ,slash,incomment1,incomment2,done} statetype; staticstruct { stringstr; tokentypetok; }reservedwords[6] ={{"else",else},{"if",if},{"int",int},{"return",return},{"void",void},{"while",while}}; charscan::getnextchar() { if(linepos>=linebuffer.size()) { if(getline(*in,linebuffer)) { linebuffer+="\n"; lineno++; linepos=0; returnlinebuffer[linepos++]; } else { eof_flag=true; returneof; } } else returnlinebuffer[linepos++]; } voidscan::ungetnextchar() { if(!eof_flag)linepos--; } tokentypescan::reservedlookup(string&s) { for(inti=0;i<6;i++) if(s==reservedwords[i].str) returnreservedwords[i].tok; returnid; } voidscan::settracescan(boolf) { tracescan=f; } boolscan::gettracescan() { returntracescan; } tokentypescan::gettoken() { tokenstr=""; tokentypecurrenttoken; statetypestate=start; while(state!=done) { boolsave=false; charc=getnextchar(); switch(state){ casestart: if(c>='0'&&c<='9'){ state=innum; save=true; } elseif((c>='a'&&c<='z')||(c>='a'&&c<='z')){ state=inid; save=true; } elseif(c==''||c=='\t'||c=='\n') { state=start; } elseif(c=='/'){ state=slash; } elseif(c=='='){ state=tempe; } elseif(c=='>') state=tempg; elseif(c=='<') state=templ; elseif(c=='!') state=innoteq; else { state=done; switch(c){ caseeof: currenttoken=endfile; break; case'+': currenttoken=add; break; case'-': currenttoken=sub; break; case'*': currenttoken=mul; break; case'(': currenttoken=lparen; break; case')': currenttoken=rparen; break; case'[': currenttoken=lzk; break; case']': currenttoken=rzk; break; case'{': currenttoken=ldk; break; case'}': currenttoken=rdk; break; case';': currenttoken=semi; break; case',': currenttoken=comma; break; default: currenttoken=error; break; } } break; caseinnum: if(c<'0'||c>'9') { ungetnextchar(); state=done; currenttoken=num; } else save=true; break; caseinid: if(!((c>='a'&&c<='z')||(c>='a'&&c<='z'))) { ungetnextchar(); state=done; currenttoken=id; } else save=true; break; caseslash: if(c!='*') { state=done; currenttoken=div; } else state=incomment1; break; caseincomment1: if(c!='*') state=incomment1; elseif(c==eof){ state=done; currenttoken=endfile; } else state=incomment2; break; caseincomment2: if(c=='*'){ state=incomment2; }elseif(c=='/'){ state=start; }elseif(c==eof){ state=done; currenttoken=endfile; }else{ state=incomment1; } break; casetempe: if(c=='='){ state=done; currenttoken=eq; }else{ state=done; ungetnextchar(); currenttoken=assign; } break; casetempg: if(c=='='){ state=done; currenttoken=ge; }else{ state=done; ungetnextchar(); currenttoken=gt; } break; casetempl: if(c=='='){ state=done; currenttoken=le; }else{ state=done; ungetnextchar(); currenttoken=lt; } break; caseinnoteq: if(c=='='){ state=done; currenttoken=ne; }else{ state=done; ungetnextchar(); currenttoken=error; } break; default: cerr<<"scannerbug:state="<<state<<endl; state=done; currenttoken=error; break; } if(save){ stringnewchar(1,c); tokenstr+=newchar; } if(state==done&&currenttoken==id) currenttoken=reservedlookup(tokenstr); } if(tracescan){ cout<<"scanatline"<<lineno<<"token:"; printtoken(currenttoken,tokenstr); cout<<endl; } returncurrenttoken; } stringscan::gettokenstr() { returntokenstr; } voidscan::printtoken(tokentypett,conststring&tok) { stringtype; switch(tt){ caseendfile: type="eof"; break; caseerror: type="error"; break; caseelse: caseif: caseint: casereturn: casevoid: casewhile: type="reservedword"; break; caseid: type="id"; break; casenum: type="num"; break; caseassign: type="="; break; caseeq: type="=="; break; caselt: type="<"; break; casegt: type=">"; break; casele: type="<="; break; casege: type=">="; break; casene: type="!="; break; caseadd: type="+"; break; casesub: type="-"; break; casemul: type="*"; break; casediv: type="/"; break; casesemi: type=";"; break; caselparen: type="("; break; caserparen: type=")"; break; caselzk: type="["; break; caserzk: type="]"; break; caseldk: type="{"; caserdk: type="}"; break; casecomma: type=","; break; default: break; } cout<<type<<":"<<tok; } main.cpp #include<iostream> #include<fstream> #include<string> usingnamespacestd; #include"scan.h" intmain(intargc,char*constargv[]){ stringfilename="/users/huanglongyin/scan_in.txt"; //cout<<"filename:"; //cin>>filename; ifstreamin(filename.c_str()); if(!in){ cerr<<"erroroccurswhenopenningfile"<<filename<<endl; return-1; } scanscan(&in); while(scan.gettoken()!=endfile); return0; }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯