ImageVerifierCode 换一换
格式:DOCX , 页数:32 ,大小:92.26KB ,
资源ID:9484455      下载积分:1 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-9484455.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(实验报告五SLR 分析表的生成以及语法分析.docx)为本站会员(b****8)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

实验报告五SLR 分析表的生成以及语法分析.docx

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