1、实验报告五SLR 分析表的生成以及语法分析编译原理实验报告实验序号:5实验项目名称:SLR 分析表的生成以及语法分析学号姓名专业、班实验地点指导教师实验时间一、实验目的及要求通过本实验掌握自下而上的语法分析方法,能根据文法产生其对应的SLR分析表;并能利用该分析表进行语法分析,即分析词法分析器输出的记号流,输出对应文法的产生式。二、实验设备(环境)及要求装有 VC+ 的PC机三、实验内容与步骤 源程序代码:#include#include#include#include#include#includeusing namespace std;struct token/token结构体 int c
2、ode; int num; token *next;token *token_head,*token_tail;/token队列struct number/number结构体 int num; int value; number *next;number *number_head,*number_tail;/number队列struct str/string结构体 int num; string word; str *next;str *string_head,*string_tail;/string队列struct ivan/表达式结构体 char left; string right; i
3、nt len;ivan css20;/20个表达式struct pank/action表结构体 char sr; int state;pank action4618;/action表int go_to4611;/go_to表struct ike/分析栈结构体,双链 ike *pre; int num; int word; ike *next;ike *stack_head,*stack_tail;/分析栈首尾指针void scan();/按字符读取源文件int judge(char ch);/判断输入字符的类型void out1(char ch);/写入token.txtvoid out2(c
4、har ch,string word);/写入number.txtvoid out3(char ch,string word);/写入string.txtvoid input1(token *temp);/插入结点到队列tokenvoid input2(number *temp);/插入结点到队列numbervoid input3(str *temp);/插入结点到队列stringvoid output();/输出三个队列的内容void outfile();/输出三个队列的内容到相应文件中void yufa_initialize();/初始化语法分析数据结构int yufa_main(int
5、a);/语法分析主体部分int ID1(int a);/给输入字符编号,转化成action表列编号string ID10(int i);/给输入字符反编号int ID2(char ch);/给非终结状态编号,转化成go_to表列编号int ID20(char ch);/给非终结状态编号char ID21(int j);/给非终结状态反编号void add(ike *temp);/给ike分析栈链表增加一个结点void del();/给ike分析栈链表删除一个结点FILE *fp;/文件int wordcount;/标志符计数int numcount;/整型常数计数int err;/标志词法分析
6、结果正确或错误int nl;/读取行数void main() token_head=new token; token_head-next=NULL; token_tail=new token; token_tail-next=NULL; number_head=new number; number_head-next=NULL; number_tail=new number; number_tail-next=NULL; string_head=new str; string_head-next=NULL; string_tail=new str; string_tail-next=NULL;
7、/初始化三个队列的首尾指针 wordcount=0;/初始化字符计数器 numcount=0;/初始化常数计数器 err=0;/初始化词法分析错误标志 nl=1;/初始化读取行数 scan(); if(err=0) char m; cout词法分析正确完成!endlendlm; output(); if(m=y) cout结果同时保存在token.txt、number.txt和sting.txt三个文件中,请打开查看next; int p,q; p=0; q=0; coutendl下面开始语法分析:code); p=yufa_main(w); if(p=1) break; if(p=0) te
8、mp=temp-next; if(temp=NULL) q=1; /语法分析 if(q=1) while(1) p=yufa_main(17); if(p=3) break; /最后输入$来完成语法分析 coutendl; system(pause);void scan() char ch; string word; char document50; int flag=0; coutdocument; coutendl; if(fp=fopen(document,rt)=NULL) err=1; cout无法找到该文件!endl; return; while(!feof(fp) word=;
9、ch=fgetc(fp); flag=judge(ch); if(flag=1) out1(ch); else if(flag=2) out2(ch,word); else if(flag=3) out3(ch,word); else if(flag=4 | flag=5 |flag=6) continue; else coutnl行 错误:非法字符! ch | ch=: | ch=; | ch= | ch= | ch=( | ch=) flag=1;/界符 else if(0=ch & ch=9) flag=2;/数字 else if(a=ch & ch=z) | (A=ch & ch :
10、id=4;break; case : : id=5;break; case ; : id=6;break; case : id=7;break; case : id=8;break; case ( : id=9;break; case ) : id=10;break;/界符编码 default : id=0; token *temp; temp=new token; temp-code=id; temp-num=-1; temp-next=NULL; input1(temp); return;void out2(char ch,string word) token *temp; temp=ne
11、w token; temp-code=-1; temp-num=-1; temp-next=NULL; number *temp1; temp1=new number; temp1-num=-1; temp1-value=-1; temp1-next=NULL; int flag=0; word=word+ch; ch=fgetc(fp); flag=judge(ch); if(flag=1) numcount+; temp-code=26; temp-num=numcount; input1(temp); temp1-num=numcount; int i,num_value=0,num_l
12、ength;/字符串转化为数字 char *num_head; num_head=&word0; num_length=strlen(num_head); for(i=num_length-1;i=0;i-) num_value=num_value+(int(wordi)-48)*pow(10,(num_length-i-1); temp1-value=num_value;/把数字的值赋给结点 input2(temp1); out1(ch); else if(flag=2) out2(ch,word);/形成字符串 else if(flag=3) err=1; coutnl行 错误:数字后面跟
13、字母!code=26; temp-num=numcount; input1(temp); temp1-num=numcount; int i,num_value=0,num_length;/字符串转化为数字 char *num_head; num_head=&word0; num_length=strlen(num_head); for(i=num_length-1;i=0;i-) num_value=num_value+(int(wordi)-48)*pow(10,(num_length-i-1); temp1-value=num_value;/把数字的值赋给结点 input2(temp1)
14、; return; else err=1; coutnl行 错误:非法字符! chcode=-1; temp-num=-1; temp-next=NULL; str *temp1; temp1=new str; temp1-num=-1; temp1-word=; temp1-next=NULL; int flag=0; word=word+ch; ch=fgetc(fp); flag=judge(ch); if(flag=1 | flag=4 | flag=5 | flag=6) if(word=and | word=if | word=then /*| word=else*/ | word
15、=while | word=do | word=int) if(word=and) temp-code=31; else if(word=if) temp-code=32; else if(word=then) temp-code=33; /else if(word=else) / temp-code=34; else if(word=while) temp-code=35; else if(word=do) temp-code=36; else if(word=int) temp-code=37;/关键字编码 input1(temp); if(flag=1) out1(ch); else i
16、f(flag=4 | flag=5 | flag=6) return; else if(flag=1) wordcount+; temp-code=25; temp-num=wordcount; input1(temp); temp1-num=wordcount; temp1-word=word; input3(temp1); out1(ch); else if(flag=4 | flag=5 | flag=6) wordcount+; temp-code=25; temp-num=wordcount; input1(temp); temp1-num=wordcount; temp1-word
17、=word; input3(temp1); return; else if(flag=2 | flag=3) out3(ch,word);/形成字符串 else err=1; coutnl行 错误:非法字符! chnext = NULL) token_head-next=temp; token_tail-next=temp; else token_tail-next-next=temp; token_tail-next=temp; void input2(number *temp) if(number_head-next = NULL) number_head-next=temp; numbe
18、r_tail-next=temp; else number_tail-next-next=temp; number_tail-next=temp; void input3(str *temp) if(string_head-next = NULL) string_head-next=temp; string_tail-next=temp; else string_tail-next-next=temp; string_tail-next=temp; void output() couttoken表内容如下:next; while(temp1!=NULL) coutcode; if(temp1-
19、num = -1) coutendl; else cout numnext; cout常数表内容如下:next; while(temp2!=NULL) coutnum valuenext; cout符号表内容如下:next; while(temp3!=NULL) coutnum wordnext; void outfile() ofstream fout1(token.txt);/写文件 ofstream fout2(number.txt); ofstream fout3(string.txt); token *temp1; temp1=new token; temp1=token_head-
20、next; while(temp1!=NULL) fout1code; if(temp1-num = -1) fout1endl; else fout1 numnext; number *temp2; temp2=new number; temp2=number_head-next; while(temp2!=NULL) fout2num valuenext; str *temp3; temp3=new str; temp3=string_head-next; while(temp3!=NULL) fout3num wordnext; /*以上是词法,以下是语法*/void yufa_init
21、ialize() stack_head=new ike; stack_tail=new ike; stack_head-pre=NULL; stack_head-next=stack_tail; stack_head-num=0; stack_head-word=!; stack_tail-pre=stack_head; stack_tail-next=NULL;/初始化栈分析链表 css0.left=Q; css0.right=P; css1.left=P; css1.right=id()L;R; css2.left=L; css2.right=L;D; css3.left=L; css3.
22、right=D; css4.left=D; css4.right=id:int; css5.left=E; css5.right=E+T; css6.left=E; css6.right=T; css7.left=T; css7.right=T*F; css8.left=T; css8.right=F; css9.left=F; css9.right=(E); css10.left=F; css10.right=id; css11.left=B; css11.right=B and B; css12.left=B; css12.right=idid; css13.left=M; css13.right=id=E; css14.left=S; css14.right=if B then M; css15.left=S; css15.rig
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2