福建农林大学计算机与信息学院计算机类课程设计报告课程名称:编译原理课程设计题目:语法分析器姓名:系:计算机专业:年级:学号:指导教师:2011~2012学年第一学期福建农林大学计算机与信息学院计算机类课程设计结果评定评语:成绩:指导教师签字:任务下达日期:评定日期:目录1正则表达式·································································································11.1正则表达式························································································11.2确定化(化简)后的状态转换图································································11.3分析程序代码·····················································································11.4程序运行截图·····················································································21.5小结···································································································22LL(1)分析···································································································32.1LL(1)文法·························································································32.2LL(1)预测分析表················································································32.3分析程序代码·····················································································32.4程序运行截图·····················································································62.5小结·································································································73算符优先分析······························································································83.1算符优先文法·····················································································83.2算符优先关系表··················································································83.3分析程序代码·····················································································83.4程序运行截图····················································································123.5小结································································································124LR分析·····································································································124.1LR文法····························································································124.2LR分析表·························································································124.3分析程序代码····················································································134.4程序运行截图····················································································164.5小结································································································17参考文献:·····································································································1711正则表达式1.1正则表达式(a|b)*(aa|bb)(a|b)*(注:该正规式为示例,可更改)1.2确定化(化简)后的状态转换图1.3分析程序代码程序要求:用户输入一个符号串,程序判断是否为给定方法的句子。五号字,固定值12磅行间距。程序要有注释#includestdio.h#includestring.h#includestdio.h#includeiostream#defineSTRM5#defineSTRN2classCodeParse{public:voidCodeParse::disPlayResult(intargc,char*argv[]);private:intanalyzer(char*words);};intCodeParse::analyzer(char*words){intindex=0,tab_pint=1;intmap_table[STRM][STRN]={{1,2},{2,3},{4,3},{2,4},{4,4}};while(words[index]!='\0'){if(words[index]=='a'){tab_pint=map_table[tab_pint][0];}elseif(words[index]=='b'){tab_pint=map_table[tab_pint][1];}else{printf(Erroroftheinput\n);return0;}abCBADababab2index++;}if(tab_pint==4)return1;elsereturn0;}voidCodeParse::disPlayResult(intargc,char*argv[]){if(analyzer(argv[1])0)std::coutThestrisrightstd::endl;elsestd::coutinputERRORstd::endl;}intmain(intargc,char*argv[]){if(argc!=2)std::coutThecommondiserrorstd::endl;else{CodeParsecodeParse;codeParse.disPlayResult(argc,argv);}return0;}1.4程序运行截图1.5小结:32LL(1)分析2.1LL(1)文法E→TE'(注:该文法为示例,可更改)E'→+TE'|εT→FT'T'→*FT'|εF→(E)|i2.2LL(1)预测分析表Id+*()#EE-TE’E-TE’E’E’-+TE’E’-εE’-εTT-FT’T-FT’T’T’-εT’-*FT’T’-εT’-εFF-idF-(E)2.3分析程序代码程序要求:用户输入一个符号串,程序判断是否为给定方法的句子。五号字,固定值12磅行间距。程序要有注释#includestdio.h#includestring.h#definemaxsize20#includestdio.h#includestring.h//定义产生式结构体structproduction{chartoken;inttokens[6];};//定义堆栈结构structstack{charstackwords[maxsize];intstack_top;};classLLOneParse{public:voidinitial();intanalyzer(char*words);4private:voidpush_stack(intnum);intseekkeyword(charword);intseektoken(charword);private:structproductionvariable[5];structstackastack;charproduce[7][6];};voidLLOneParse::initial(){//初始化堆栈astack.stack_top=1;astack.stackwords[0]='#';astack.stackwords[1]='E';//初始化产生式strcpy(produce[1],DT);strcpy(produce[2],DT+);strcpy(produce[3],HF);strcpy(produce[4],HF*);strcpy(produce[5],)E();strcpy(produce[6],i);//初始化二维表variable[0].token='E';variable[0].tokens[0]=1;variable[0].tokens[1]=-1;variable[0].tokens[2]=-1;variable[0].tokens[3]=1;variable[0].tokens[4]=-1;variable[0].tokens[5]=-1;variable[1].token='D';variable[1].tokens[0]=-1;variable[1].tokens[1]=2;variable[1].tokens[2]=-1;variable[1].tokens[3]=-1;variable[1].tokens[4]=0;variable[1].tokens[5]=0;variable[2].token='T';variable[2].tokens[0]=3;variable[2].tokens[1]=-1;variable[2].tokens[2]=-1;variable[2].tokens[3]=3;variable[2].tokens[4]=-1;variable[2].tokens[5]=-1;variable[3].token