编译原理课程设计

上传人:熏** 文档编号:73763784 上传时间:2022-04-12 格式:DOC 页数:26 大小:145KB
收藏 版权申诉 举报 下载
编译原理课程设计_第1页
第1页 / 共26页
编译原理课程设计_第2页
第2页 / 共26页
编译原理课程设计_第3页
第3页 / 共26页
资源描述:

《编译原理课程设计》由会员分享,可在线阅读,更多相关《编译原理课程设计(26页珍藏版)》请在装配图网上搜索。

1、#include#include#include#include#include#includeusing namespace std;/*下面是一些重要数据结构的声明*/struct token/词法 token结构体 int code;/编码 int num;/递增编号 token *next;token *token_head,*token_tail;/token队列struct str/词法 string结构体 int num;/编号 string word;/字符串内容 str *next;str *string_head,*string_tail;/string队列struct i

2、van/语法 产生式结构体 char left;/产生式的左部 string right;/产生式的右部 int 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;/

3、分析栈首尾指针struct L/语义四元式的数据结构 int k; string op;/操作符 string op1;/操作数 string op2;/操作数 string result;/结果 L *next;/语义四元式向后指针 L *Ltrue;/回填true链向前指针 L *Lfalse;/回填false链向前指针;L *L_four_head,*L_four_tail,*L_true_head,*L_false_head;/四元式链,true链,false链struct symb/语义输入时符号表 string word;/变量名称 int addr;/变量地址 symb *nex

4、t;symb *symb_head,*symb_tail;/语义符号链表/*下面是与词法分析相关的一些函数的声明*/void scan();/按字符读取源文件void cifa_main();/词法分析主程序int judge(char ch);/判断输入字符的类型void out1(char ch);/写入token.txtvoid out3(char ch,string word);/写入string.txtvoid input1(token *temp);/插入结点到队列tokenvoid input3(str *temp);/插入结点到队列stringvoid output();/输出

5、三个队列的内容void outfile();/输出三个队列的内容到相应文件中/*下面是与语法分析相关的一些函数的声明*/void yufa_main();/语法分析主程序void yufa_initialize();/初始化语法分析数据结构int yufa_SLR1(int a);/语法分析主体部分int ID1(int a);/给输入字符编号,转化成action表列编号string ID10(int i);/给输入字符反编号int ID2(char ch);/给非终结状态编号,转化成go_to表列编号int ID20(char ch);/给非终结状态编号char ID21(int j);/给

6、非终结状态反编号void add(ike *temp);/给ike分析栈链表增加一个结点void del();/给ike分析栈链表删除一个结点/*下面是与语义分析相关的一些函数的声明*/void yuyi_main(int m);/语义分析主程序void add_L_four(L *temp);/向四元式链中加一个结点void add_L_true(L *temp);/向true链中加一个结点void add_L_false(L *temp);/向false链中加一个结点void add_symb(symb *temp);/向语义符号表链中加一个结点void output_yuyi();/输出

7、中间代码四元式和最后符号表string newop(int m);/把数字变成字符串string id_numtoname(int num);/把编号转换成相应的变量名int lookup(string m);/变量声明检查/*下面是一些全局变量的声明*/FILE *fp;/文件指针int wordcount;/标志符计数int err;/标志词法分析结果正确或错误int nl;/读取行数int yuyi_linshi;/语义临时变量string E_name,T_name,F_name,M_name,id_name,id1_name,id2_name,errword;/用于归约时名称传递和未

8、声明变量的输出int id_num,id1_num,id2_num,id_left,id_while,id_then,id_do;/用于记录一些特殊的字符位置信息/*主程序开始*/int main() cout*endl; cout* 说明: *endl; cout* 第一部分:词法分析 *endl; cout* 第二部分:语法分析 *endl; cout* 第三部分:语义分析 *endl; cout*endl; cifa_main();/词法 yufa_main();/语法 output_yuyi();/语义 coutnext=NULL; token_tail=new token; toke

9、n_tail-next=NULL; string_head=new str; string_head-next=NULL; string_tail=new str; string_tail-next=NULL;/初始化三个队列的首尾指针 L_four_head=new L; L_four_head-next=NULL; L_four_tail=new L; L_four_tail-k=0; L_four_tail-next=NULL; L_true_head=new L; L_true_head-Ltrue=NULL; L_false_head=new L; L_false_head-Lfal

10、se=NULL; symb_head=new symb; symb_head-next=NULL; symb_tail=new symb; symb_tail-next=NULL; yuyi_linshi=-1; id_num=0; wordcount=0;/初始化字符计数器 err=0;/初始化词法分析错误标志 nl=1;/初始化读取行数 scan(); if(err=0) char m; output(); cout词法分析正确完成!endlendlm; coutendl; if(m=y) outfile(); cout结果成功保存在token.txt和sting.txt两个文件中,请打开

11、查看endl; coutendl; void scan() coutendl; system(pause); coutendl; char ch; string word; char document50; int flag=0; coutdocument; coutendl; cout*endl; cout* 第一部分:词法分析 *endl; cout*endl; if(fp=fopen(document,rt)=NULL) err=1; cout无法找到该文件!endl; return; while(!feof(fp) word=; ch=fgetc(fp); flag=judge(ch)

12、; if(flag=1) out1(ch); 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(a=ch & ch=z) | (A=ch & ch : id=4;break; case : : id=5;break; case ; : id=6;break; case : id=7;break; case : id=8;break;

13、 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 out3(char ch,string word) token *temp; temp=new token; temp-code=-1; temp-num=-1; temp-next=NULL; str *temp1; temp1=new str; temp1-num=-1

14、; 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=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=w

15、hile) 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 if(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(

16、ch); else if(flag=4 | flag=5 | flag=6) wordcount+; temp-code=25; temp-num=wordcount; input1(temp); temp1-num=wordcount; temp1-word=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 t

17、oken_tail-next-next=temp; token_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-num = -1) coutendl; else c

18、out numnext; cout符号表内容如下:next; while(temp3!=NULL) coutnum wordnext; void outfile() ofstream fout1(token.txt);/写文件 ofstream fout3(string.txt); token *temp1; temp1=new token; temp1=token_head-next; while(temp1!=NULL) fout1code; if(temp1-num = -1) fout1endl; else fout1 numnext; str *temp3; temp3=new st

19、r; temp3=string_head-next; while(temp3!=NULL) fout3num wordnext; /*以上是词法,以下是语法*/void yufa_main() if(err=0) system(pause); coutendl; cout*endl; cout* 第二部分:语法分析 *endl; cout*next; int p,q; p=0; q=0; cout语法分析过程如下:code); p=yufa_SLR1(w); if(p=1) break; if(p=0) temp=temp-next; if(temp=NULL) q=1; /语法分析 if(q

20、=1) while(1) p=yufa_SLR1(17); if(p=3) break; /最后输入$来完成语法分析 void yufa_initialize() 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.le

21、ft=P; css1.right=id()L;R; css2.left=L; css2.right=L;D; css3.left=L; css3.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; c

22、ss11.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.right=while B do M; css16.left=S; css16.right=M; css17.left=N; css17.right=N;S; css18.left=N; css18.right=S; css19.left=R; css19.right=N; int i,j; for(i=0;i2

23、0;i+) char *css_len; css_len=&cssi.right0; cssi.len=strlen(css_len); css1.len=6; css4.len=3; css10.len=1; css11.len=3; css12.len=3; css13.len=3; css14.len=4; css15.len=4;/初始化产生式 for(i=0;i46;i+) for(j=0;j18;j+) actionij.sr=#; /初始化action表 for(i=0;i46;i+) for(j=0;jk+1;/设置if语句中then位置标志 if(i=35) j=15;id_

24、while=L_four_tail-k+1;/设置while语句中while位置标志 if(i=36) j=16;id_do=L_four_tail-k+1;/设置while语句中do位置标志 if(i=37) j=11; return(j);string ID10(int i)/反编号输入字符 string ch; if(i=0) ch=id; if(i=1) ch=(; if(i=2) ch=); if(i=3) ch=; if(i=4) ch=:; if(i=5) ch=*; if(i=6) ch=+; if(i=7) ch=; if(i=8) ch=; if(i=9) ch=; if(

25、i=10) ch=; if(i=11) ch=int; if(i=12) ch=and; if(i=13) ch=if; if(i=14) ch=then; if(i=15) ch=while; if(i=16) ch=do; if(i=17) ch=$; return(ch);int ID2(char ch)/按go_to表给非终结符编号 int j; j=-1; if(ch=P) j=0; if(ch=D) j=1; if(ch=R) j=2; if(ch=E) j=3; if(ch=T) j=4; if(ch=F) j=5; if(ch=B) j=6; if(ch=M) j=7; if(

26、ch=S) j=8; if(ch=L) j=9; if(ch=N) j=10; return(j);int ID20(char ch)/给非终结符编号 int j; j=-1; if(ch=P) j=100; if(ch=D) j=101; if(ch=R) j=102; if(ch=E) j=103; if(ch=T) j=104; if(ch=F) j=105; if(ch=B) j=106; if(ch=M) j=107; if(ch=S) j=108; if(ch=L) j=109; if(ch=N) j=1010; return(j);char ID21(int j)/反编号非终结符

27、 char ch; if(j=100 | j=0) ch=P; if(j=101 | j=1) ch=D; if(j=102 | j=2) ch=R; if(j=103 | j=3) ch=E; if(j=104 | j=4) ch=T; if(j=105 | j=5) ch=F; if(j=106 | j=6) ch=B; if(j=107 | j=7) ch=M; if(j=108 | j=8) ch=S; if(j=109 | j=9) ch=L; if(j=1010 | j=10) ch=N; return(ch);void add(ike *temp)/加一个结点 if(stack_

28、head-next=stack_tail) temp-pre=stack_head; temp-next=stack_tail; stack_head-next=temp; stack_tail-pre=temp; else temp-pre=stack_tail-pre; temp-next=stack_tail; stack_tail-pre-next=temp; stack_tail-pre=temp; void del()/删除一个结点 stack_tail-pre-pre-next=stack_tail; stack_tail-pre=stack_tail-pre-pre;int y

29、ufa_SLR1(int w) /*cout当前输入符号:ID10(w)pre-numw.sr;/动作 state_temp=actionstack_tail-pre-numw.state;/状态变化 if(sr_temp=#)/错误动作 flag=1; err=3; cout语法分析出错!next=NULL; temp-pre=NULL; temp-word=w; temp-num=state_temp; add(temp); cout/*动作(移进):*/sr_tempstate_temp /*状态转为:pre-num 栈顶符号:pre-word)*/endl; flag=0; else

30、if(sr_temp=r)/归约动作 int p=ID2(cssstate_temp.left); int q=cssstate_temp.len; for(i=0;inext=NULL; temp-pre=NULL; temp-word=ID20(cssstate_temp.left); temp-num=go_tostack_tail-pre-nump;/查go_to表 add(temp); cout/*动作(归约):*/sr_tempstate_temp cssstate_temp.leftcssstate_temp.right /*状态转为:pre-num 栈顶符号:pre-word)

31、*/endl; flag=2; yuyi_main(state_temp);/在产生树的同时进行语义分析 else if(sr_temp=)/结束动作 coutEND/*动作(归约):sr_tempstate_temp*/ cssstate_temp.leftcssstate_temp.right /*状态转为:pre-num 栈顶符号:pre-word)*/endl; flag=3; cout语法分析正确完成!endl; else/其他意外情况 flag=1; err=3; cout语法分析出错!op= ; temp-op1= ; temp-op2= ; temp-result=; temp-next=NULL; temp-Ltrue=NULL; temp-Lfalse=NULL; if(m=4)/变量声明时加入符号表链 symb *Stemp; Stemp=new symb; id_name=id_numtoname(id_num); Stemp-word=id_name; Stemp-next=NULL; add_symb(Stemp); if(m=5)/归约EE+T temp-op=+; temp-op1=E_name; temp-op2=T_name; yuyi_linshi+;/申请临时变量 E_name=t+newop(yuyi_li

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

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!