《编译原理》报告_第1页
《编译原理》报告_第2页
《编译原理》报告_第3页
《编译原理》报告_第4页
《编译原理》报告_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选优质文档-倾情为你奉上 SHANGHAI UNIVERSITY<编译原理>实验报告学 院计算机工程与科学学院组 号实验三日期2016.4.23学号姓名主要工作贡献因子一、实验目的与要求l 给出 PL/0 文法规范,要求编写 PL/0 语言的语法分析程序。l 通过设计、编制、调试一个典型的语法分析程序,实现对词法分析程序所提供的单词序列进行语法检查和结构分析,进一步掌握常用的语法分析方法。l 选择一种语法分析方法(递归子程序法、LL(1)分析法、算符优先分析法、SLR(1)分析法) ;选择常见程序语言都具备的语法结构,如赋值语句,特别是表达式,作为分析对象确定语法分析的方法。l

2、将实验二“词法分析”的输出结果,作为表达式语法分析器的输入,进行语法解析,对于语法正确的表达式,报告“语法正确” ;对于语法错误的表达式,报告“语法错误” , 指出错误原因。l 源程序中字符不区分大小写,即: “a1”和“A1”是同一个标识符。l 准备至少 10 组测试用例, 每组测试用例包括: 输入文件和输出结果。二、 实验环境C+IDE:Codeblocks三、实验内容已给 PL/0 语言文法,构造表达式部分的语法分析器。分析对象算术表达式的 BNF 定义如下:<表达式> := +|-<项><加法运算符> <项><项> := &l

3、t;因子><乘法运算符> <因子><因子> := <标识符>|<无符号整数>| (<表达式>)<加法运算符> := +|-<乘法运算符> := *|/四、 实验内容的设计与实现源程序: #include<string>#include<cstring>#include<fstream>#include<stack>using namespace std;const string basic_word="begin","

4、;call","const","do","end","if","odd","procedure","read","then","var","while","write"const string basic_word_name="beginsym","callsym","constsym","dosy

5、m","endsym","ifsym","oddsym","proceduresym","readsym","thensym","varsym","whilesym","writesym"const string operator_="+-*/<>=:"/杩愮畻绗?const string operator_="+","-","

6、;*","/","=","#","<","<=",">",">=",":="const string operator_name="plus","minus","times","slash","eql","neq","lss","leq",&qu

7、ot;gtr","geq","becomes"const string cut_str="(),;. "const string cut_str_name="lparen","rparen","comma","semicolon","period","space"class compile private: string line;/瀛樺偍姣忎竴琛屾暟鎹? string buffer;/缂撳啿鍖? /int

8、 flag;/鏍囪瘑浣? /-1:鏃犲畾涔? /0:a-z A-Z /1:0-9 /2:+-*/<>=: /3:() ,;. /4:space,tab,n /5:闈?-4绫诲埆瀛楃 public: compile(string inname,string outname) ifstream fin(inname.c_str(),ios:in); fstream fout(outname.c_str(),ios:out); int flag=-1; if (fin.good() while (getline(fin, line)/鑾峰緱涓琛? int length=line.leng

9、th(); string t2="" string temp2; buffer="" for(int i=0;i<length;i+) flag=isWhat(linei); / cout<<linei<<endl; /cout<<buffer<<endl; temp2 =linei; /cout<<flag<<endl; if(i=length-1&&(flag=0|flag=1) string temp; temp=buffer; /cout<<

10、temp<<endl; buffer="" if(whatBasicWord(temp)!="no") fout<<"("<<whatBasicWord(temp)<<","<<temp<<")n" fout<<flush; cout<<"("<<whatBasicWord(temp)<<","<<temp<<&q

11、uot;)"<<endl; else if(isNumber(temp)=true) fout<<"("<<"number"<<","<<temp<<")n" fout<<flush; cout<<"("<<"number"<<","<<temp<<")"<<endl; e

12、lse if(markCheck(temp) fout<<"("<<"ident"<<","<<temp<<")n" fout<<flush; cout<<"("<<"ident"<<","<<temp<<")"<<endl; else fout<<"("<

13、<"illegal"<<","<<temp<<")n" fout<<flush; cout<<"("<<"illegal"<<","<<temp<<")"<<endl; else if(flag=0|flag=1|flag=5) buffer+=linei; /cout<<buffer<<endl; else

14、 string temp; temp=buffer; /cout<<temp<<endl; buffer="" if(temp!="") if(t2!="") if(t2=":") cout<<"(illegal,:)"<<endl; fout<<"(illegal,:)n" else cout<<"("<<whatOperator(t2)<<",&q

15、uot;<<t2<<")"<<endl; fout<<"("<<whatOperator(t2)<<","<<t2<<")n" t2="" fout<<flush; if(whatBasicWord(temp)!="no") fout<<"("<<whatBasicWord(temp)<<","

16、<<temp<<")n" fout<<flush; cout<<"("<<whatBasicWord(temp)<<","<<temp<<")"<<endl; else if(isNumber(temp)=true) fout<<"("<<"number"<<","<<temp<<"

17、;)n" fout<<flush; cout<<"("<<"number"<<","<<temp<<")"<<endl; else if(markCheck(temp) fout<<"("<<"ident"<<","<<temp<<")n" fout<<flush; cou

18、t<<"("<<"ident"<<","<<temp<<")"<<endl; else fout<<"("<<"illegal"<<","<<temp<<")n" fout<<flush; cout<<"("<<"illegal"&

19、lt;<","<<temp<<")"<<endl; if(flag=2) if(t2!="") if(t2=":"&&temp2!="=") cout<<"(illegal,:)"<<endl; fout<<"(illegal,:)n" fout<<flush; if(whatOperator(temp2)!="no") cout&

20、lt;<"("<<whatOperator(temp2)<<","<<temp2<<")"<<endl; fout<<"("<<whatOperator(temp2)<<","<<temp2<<")n" fout<<flush; else t2=temp2; else if(t2=">"|t2="<

21、"|t2=":")&&temp2="=") cout<<"("<<whatOperator(t2+temp2)<<","<<t2+temp2<<")"<<endl; fout<<"("<<whatOperator(t2+temp2)<<","<<t2+temp2<<")n" fou

22、t<<flush; t2="" else fout<<"("<<whatOperator(t2)<<","<<t2<<")n" fout<<flush; cout<<"("<<whatOperator(t2)<<","<<t2<<")"<<endl; cout<<"("&

23、lt;<whatOperator(temp2)<<","<<temp2<<")"<<endl; fout<<"("<<whatOperator(temp2)<<","<<temp2<<")n" fout<<flush; t2="" else if(temp2="<"|temp2=">"|temp2=&

24、quot;:") t2=temp2; else cout<<"("<<whatOperator(temp2)<<","<<temp2<<")"<<endl; fout<<"("<<whatOperator(temp2)<<","<<temp2<<")n" fout<<flush; else if(flag=3) /cout&

25、lt;<"33"<<temp2<<endl; cout<<"("<<whatCutStr(temp2)<<","<<temp2<<")"<<endl; fout<<"("<<whatCutStr(temp2)<<","<<temp2<<")n" fout<<flush; else if

26、(flag=4) else cout<<"can't open file"<<endl; int isWhat(char temp)/杩斿洖鏈瓧绗殑绫诲瀷 if(temp=' '|temp='t'|temp='n') return 4; if(isLetter(temp) return 0; if(isDigital(temp) return 1; if(isOperator_(temp) return 2; if(isCutStr(temp) return 3; return 5; strin

27、g whatCutStr(string temp) int i=0; for(i;i<cut_str.length();i+) if(temp0=cut_stri) return cut_str_namei; return "no" string whatBasicWord(string temp)/鍒柇鏄惁涓烘爣璇嗙 int i=0; int length=sizeof(basic_word)/sizeof(string); for(i;i<length;i+) if(isEqual(temp,basic_wordi) return basic_word_na

28、mei; return "no" string whatOperator(string temp) int length=sizeof(operator_)/sizeof(string); for(int i=0;i<length;i+) if(temp=operator_i) return operator_namei; return "no" bool isNumber(string temp) for(int i=0;i<temp.length();i+) if(!isDigital(tempi) return false; retur

29、n true; bool isOperator_(char temp)/鏄惁涓鸿繍绠楃 int i=0,length=operator_.length(); for(i;i<length;i+) if(temp=operator_i) break; if(i=length) return false; else return true; bool isCutStr(char temp)/鏄惁涓哄垎闅旂 int i=0,length=cut_str.length(); for(i;i<length;i+) if(temp=cut_stri) break; if(i=length) r

30、eturn false; else return true; bool isLetter(char temp)/鍒柇鏄惁涓哄瓧姣? if(temp>='a'&&temp<='z')|(temp>='A'&&temp<='Z') return true; return false; bool isDigital(char temp)/鍒柇鏄惁涓烘暟瀛? if(temp>='0'&&temp<='9') return t

31、rue; return false; bool markCheck(string tt)/鏍囪瘑绗悎娉曟鏌? if(tt0!='_'&&!isLetter(tt0) return false; else /cout<<tt<<endl; for(int i=1;i<tt.length();i+) if(!isLetter(tti)&&!isDigital(tti)&&tti!='_')/鍑虹幇 return false; return true; bool isEqual(string

32、 a1,string a2)/鍒柇涓釜瀛楃鏄惁鐩哥瓑 if(a1.length()!=a2.length() return false; else int length=a1.length(); for(int i=0;i<length;i+) if(a1i!=a2i&&a1i+32!=a2i&&a1i-32!=a2i) return false; return true; /* void writeInto(string filename)/鍐欏叆鏂囦欢 ofstream fout; fout.open(filename.c_str(); for(int

33、 i=0;i<len_;i+) fout<<"("<<indent_namei<<","<<indent_timesi<<")n" fout<<flush; fout.close(); */;class Grammarprotected: char state; int pre_k; bool flag; int index; string cate; string cate_str;public: Grammar() state='A' f

34、lag=1; pre_k=0; index=0; void setCate(string c,string c_str) cate=c; cate_str=c_str; void displayStatus() /cout<<pre_k<<endl; if(pre_k!=0) if(pre_k>0) cout<<"error:lost ')' to match '('. "<<endl; else cout<<"error:to more ')' at

35、 last."<<endl; if(flag=1&&(state='C'|state='D') cout<<"grammar legal!"<<endl; else if (flag=1&&(state!='C'|state!='D') cout<<"error:lost some expression at last!"<<endl; cout<<"grammar i

36、llegal!"<<endl; else cout<<"grammar illegal!"<<endl; void start(string inname) fstream fin(inname.c_str(),ios:in); string line,word,categrey,str; if(fin.good() while(getline(fin,line) int i=1; index+; categrey=""str="" word=line1; while(word!=&quo

37、t;,") categrey+=word; word=line+i; word=line+i; while(i!=line.length()-1) str+=word; word=line+i; /cout<<cate_str<<endl; cate=categrey; cate_str=str; if(cate_str="") break; inputStr(); else cout<<"can't open file"<<endl; void inputStr() if(state=&

38、#39;A') if(cate_str!="+"&&cate_str!="-"&&cate!="ident"&&cate!="number"&&cate_str!="(") cout<<"error:The begin of a expression must be '+' or '-' or '(' or 'ident' or '

39、number'.please check:"<<cate_str<<" ("<<index<<")"<<endl; flag=0; else if(cate_str="+"|cate_str="-") state='B' else if(cate="ident"|cate="number") state='B' inputStr(); else if(cate_str="(") pre_k+; else if(state='B') if(cate_str="(") state='A' pre_k+; else if(cate="ident"|cate="number") state='C&#

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论