您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 交通运输 > 编译原理课程设计(福建农林大学)
福建农林大学计算机与信息学院计算机类课程设计报告课程名称:编译原理课程设计题目:语法分析器姓名:系:计算机专业:计算机科学与技术年级:2012级学号:指导教师:2015~2016学年第一学期福建农林大学计算机与信息学院计算机类课程设计结果评定评语:成绩:指导教师签字:任务下达日期:评定日期:目录1正则表达式·································································································11.1正则表达式·························································································11.2确定化(化简)后的状态转换图··································································11.3分析程序代码······················································································11.4程序运行截图······················································································31.5小结:································································································32LL(1)分析···································································································42.1LL(1)文法···························································································42.2LL(1)预测分析表··················································································42.3分析程序代码······················································································42.4程序运行截图······················································································82.5小结··································································································93算符优先分析·····························································································103.1算符优先文法·····················································································103.2算符优先关系表··················································································103.3分析程序代码·····················································································103.4程序运行截图·····················································································133.5小结·································································································144LR分析·····································································································154.1LR文法····························································································154.2LR分析表·························································································154.3分析程序代码(代码原创)·······································································154.4程序运行截图·····················································································204.5小结·································································································20参考文献:·······························································································2011正则表达式1.1正则表达式(a|b)*(aa|bb)(a|b)*1.2确定化(化简)后的状态转换图1.3分析程序代码#includestdio.h#includestdlib.h#includestring.h#defineROW4#defineCOL2//一个二维数组,行0、1、2、3对应A、B、C、D,列0、1代表输入a、b//值代表当前位置输入a、b之后的位置intarray[ROW][COL]={{1,2},{3,2},{1,3},{3,3}};//判断句子是否符合给出的正则表达式intanalyze_result(char*buff){intindex=0;//当前位置从0(A)行开始intcur_index=0;if(buff==NULL){return-2;}while(buff[index]){if(buff[index]=='a'){cur_index=array[cur_index][0];}elseif(buff[index]=='b'){cur_index=array[cur_index][1];}else{//输入错误返回错误的位置returnindex;}index++;}//当前位置不为3(D),表示句子不符合正则表达式abCBADababab2if(cur_index!=3){return-3;}//符合正则表达式返回-1return-1;}intmain(){intret=0;intcur_index=0;charbuff[255]={'\0'};while(1){cur_index=0;printf(正则表达式为:(a|b)*(aa|bb)(a|b)*,请输入需要判断的句子:\n\t);scanf(%s,buff);fflush(stdin);if(strcmp(buff,exit)==0)break;ret=analyze_result(buff);if(ret==-1){printf(\t此句子符合正则表达式!\n\n);continue;}elseif(ret-1){printf(\t此句子不符合正则表达式!\n\n);continue;}elseif(ret-1){printf(\t输入错误,请检查!\n\t);while(cur_index!=ret){printf();cur_index++;}printf(|\n);printf(\t%s\n\n,buff);continue;}}return0;}31.4程序运行截图1.5小结:42LL(1)分析2.1LL(1)文法E→TE'E'→+TE'|εT→FT'T'→*FT'|εF→(E)|i2.2LL(1)预测分析表i+*()#EE→TE'E→TE'E'E'→+TE'E'→εE'→εTT→FT'T→FT'T'T'→εT'→*FT'T'→εT'→εFF→iF→(E)2.3分析程序代码#includestdio.h#includestring.h#defineSTACK_MAX_LEN255typedefstructstring{charstr[4];}string_t;//检测字符串是否符合表达式intdetect_result(char*buff);//得到当前字符所对应的列intget_col(charch);intpush_back(char*tmp_buff);intpop_back();//得到栈顶元素,A,B,C,D,E分别返回0,1,2,3,4,i,+,*,(,)返回-1intget_stack_top();//遍历到字符串最后一个字符#时,化简栈intsimplify_stack();//检查输入字符是否符合题意intdetect_input(char*buff);string_tproduction[5][6]={{CB,,,CB},{,+CB,,,0,0},{ED,,,ED},5{,0,*ED,,0,0},{i,,,(A)}};intbuff_index;intstack_top;charstack_str[STACK_MAX_LEN];intmain(){intret=0;charbuff[STACK_MAX_LEN]={'\0'};printf(文法:\n);printf(E→TE'\n);printf(E'→+TE'|ε\n);printf(T→FT'\n);printf(T'→*FT'|ε\n);printf(F→(E)|i\n);while(1){//重置字符串buff_index=0;memset(buff,0,STACK_MAX_LEN);//重置栈memset(stack_str,0,STACK_MAX_LEN);stack_str[0]='#';stack_str[1]='A';stack_top=2;//接收字符串输入printf(请输入需要判断的字符串(字符串以#结束,exit退出):);scanf(%s,buff);fflush(stdin);//输入合法性检查ret=detect_input(buff);if(ret==-1){break;//退出程序}elseif(ret==-2){printf(输入字符串中含有非法字符,请重新输入!\n\n);continue;}elseif(ret==-3){printf(请以'#'为字符串结尾!\n\n);continue;}ret=detect_result(buff);if(ret==-2){printf(踩空,不符合表达式\n\n);}elseif(ret==0){printf(符合表达式\n\n,ret);}elseif(ret==-1){printf(不符合表达
本文标题:编译原理课程设计(福建农林大学)
链接地址:https://www.777doc.com/doc-3400965 .html