预览加载中,请您耐心等待几秒...
1/3
2/3
3/3

在线预览结束,喜欢就下载吧,查找使用更方便

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

实验二:自顶向下的语法分析:递归下降法 1、实验目的:编制一个递归下降分析程序,实现对词法分析程序所提供的单词序列的语法检查和结构分析。 2、实验要求: 文法(教材199页) (1)把词法分析作为语法分析的子程序实现(5分) (2)独立的语法分析程序(4分) (3)输入串以‘#’结束,输出成功(success)或出错(error)、指出出错位置(行、列以及错误类型) #include<iostream> #include<fstream> #include<string> #include<stdlib.h> #defineMAX1024 usingnamespacestd; stringDataType[]={"int","bool","char","void"}; stringKeyWords[]={"if","else","do","while","for","break","in","continue","goto", "switch","case","default","foreach","try","catch","finally","throw","return"}; stringOperator[]={"+","-","*","/","%","&&","||",">","<",">=","<=","==","!=","^","&","|", "~","<<",">>","=","+=","-=","*=","/=","%=","&=","|=","^=","<<=",">>="}; charSeperator[]={'','.','(',')','[',']',';','{','}',',','?',':'}; structIdentity { stringvalue; inttype; }; ifstreaminFile("D:\\output.txt"); introw=1; IdentityID[10000]; intnWord=0; voidExpression() { stringword; intsyn; inFile>>word>>syn; if(syn==71) { stringtmp1; inttmp2; inFile>>tmp1>>tmp2; if(tmp2!=199) { if(tmp2>=30&&tmp2<71) { inFile>>tmp1>>tmp2; if(tmp2==71) { inFile>>tmp1>>tmp2; if(tmp2!=199) cout<<"erroroccursinrow"<<row<<",miss\";\"!"<<endl; } else cout<<"erroroccursinrow"<<row<<",missidentity!"<<endl; } else cout<<"erroroccursinrow"<<row<<",missoperator!"<<endl; } } } intlook(stringvalue) { inti; for(i=0;i<nWord;i++) if(ID[i].value==value) returni; return-1; } voidGrammerAnalyse() { stringword; intsyn; inti; while(!inFile.eof()) { i=0; inFile>>word>>syn; if(word=="#") row++; else { if(syn>0&&syn<10) { stringtmp1; inttmp2; inFile>>tmp1>>tmp2; if(tmp2==71) { intindex=look(tmp1); //ID[index].type=syn; inFile>>tmp1>>tmp2; if(tmp2!=199) { if(tmp1=="=") { Expression(); } else cout<<"erroroccursinrow"<<row<<",miss\";\"!"<<endl; } } else cout<<"erroroccursinrow"<<row<<",missidentity!"<<endl