编译原理词法语法语义分析器设计打印版_第1页
编译原理词法语法语义分析器设计打印版_第2页
编译原理词法语法语义分析器设计打印版_第3页
编译原理词法语法语义分析器设计打印版_第4页
编译原理词法语法语义分析器设计打印版_第5页
已阅读5页,还剩28页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、编译原理词法语法语义分析器设计打印版编译原理课程设计报告小组成员 :2010年 06月 24日一. 任务及要求课程设计要实现的内容 :) 设计符号表 (1确定符号表的组织方式,一般应包括名字栏和信息栏,其中名字栏作为关键 字。要考虑能够存储有关名字的信息,并可以高效地完成如下操作 :a. 查找: 根据给定的名字,在符号表中查找其信息。如果该名字在符号表中不存在,则将其加入到符号表中,否则返回指向该名字的指针 ;b. 删除: 从符号表中删除给定名字的表项。(2) 设计词法分析器设计各单词的状态转换图,并为不同的单词设计种别码。将词法分析器设计成供语法分析器调用的子程序。功能包括 :a. 能够拼出

2、语言中的各个单词 ;b. 将拼出的标识符填入符号表 ;c. 返回( 种别码, 属性值) 。(3) 语法分析要求用递归下降分析法、预测分析法或 SLR分析法,实现对表达式、各种说明语句、控制语句进行语法分析。若语法正确,则输出一棵语法树若语法错误,要求指出出错性质和出错位置 (行号)。出错处理应设计成一个出 错处理子程序。1(词法分析器产生下述小语言的单词序列这个小语言的所有的单词符号,以及它们的种别编码和内部值如下表:单词符号 种别编码 助记符 内码值DIM 1 $DIM - IF 2 $IF - DO 3 $D0 -STOP 4 $STOP- END 5 $END -标识符6 $ID - 常

3、数(整)7 $INT 内部字符串= 8 $ASSIGN 标准二进形式 + 9 $PLUS - * 10 $STAR - * 11 $POWER -, 12$COMMA - ( 13 $LPAR - )14 $RPAR -2010022220朱恒恒2010022226刘穗清2( 语法分析器能识别由大于 >小于<加+ 减- 乘* 除/ 括号() 赋值=操作数所 组成的算术表达式,其文法如下 :使用的算法可以是:预测分析法;递归下降分析法;算符优先分析法;LR分析法 等。(1) A->EB(2) B- >>EB|vEB|&(3) E->TGG->+T

4、G|- TG|£(5) T->FS(6) S- >*FS|/FS| &(7) F->(E)|i |i=E2010022211吴晓凯3 具体的种别编码和内部值 :单词符号种别编码单词值int 1char 2float 3if 4else 5do 6while 7printf 8main 9标识符 100 内部字符串 常数(整) 200 二进制数值表示 = = 401= 402>= 403> 404<= 405< 406!= 407! 408409 +=410 +411 +412 -=413 - -414 -415 *=416 *417

5、/=418 /419 A501 ;( 502) 503 504 505 506 507508 :“ 509 %= 510% 511 , 512# 513 514 空格 515$ 04. 流程图 主流程图幵妳定义关褪宇表卅4请用户輸入字符串T陕入用户输入的字符串, 1调用扫描子程序:T输出单词二元组扫描程序流程图:(a) ,标识符词法分析流程图(b) ,数字(整)词法分析流程图(c) ,其他字符流程图吏眞初始优读取下一牛字符J W拼成标识符,.*FLAG为对应关饉宇的单 词种别丙,itaifs -返回*论(a)始化是否需垂继续读入(b) (c)主流程图赋値血申鬥網为对应羌犍字的单 词种别码谨取下

6、一个宇符,FLAG=2D0读取下一个字符-拼成数字开如返回*刘穗清 20100222265. First/FollowFirst(A)=( ,i Follow(A)=# First(B)=>,<,£ Follow(B)=#First(E)=(,i Follow(E)=>,<,),# First(F)=(,i Follow(F)=*,(,+,-First(G)=+,-, £ Follow(G)=>,<,),# First(T)=c,i Follow(T)=+,-,>,<,),# First(S)=*,(,& Follow

7、(S)=+, -,>,<,),#吴晓凯 20100222116.源程序/#include "stdafx.h"#in clude <iostream>#include<string>using namespace std;#include<stdio.h>#include<stdlib.h>#include<sstream>int i,j,k,flag,number,status; /*status which is use to judge the string is keywords or not!

8、*/char ch;char words10 = " "char program500;int flags500; /存储输入句子string cnt500;/标识符int temp=0; / 数组下标int is_right; /判断输出信息int Scan(char program)char *keywords13 = "void","main","if","then","break","int","char","floa

9、t","include","for", "while","printf","scanf" / 关键字number=0;status=0;j=0;ch=programi+; / 遍历if (ch >= 'a') && (ch <= 'z' ) /while (ch >= 'a') && (ch <= 'z' )wordsj+=ch; ch=programi+;i-;wo

10、rdsj+ = '0'for (k = 0; k < 13; k+)if (strcmp (words,keywordsk) = 0) / switch(k)case 0: flag = 1;status = 1; break;case 1: flag = 2;status = 1; break;字母判断是否为关键字case 2: flag = 3;status = 1; break;case 3: flag = 4; status = 1; break;case 4: flag = 5; status = 1; break;case 5: flag = 6; statu

11、s = 1; break;case 6: flag = 7; status = 1; break;case 7: flag = 8; status = 1; break;case 8: flag = 9; status = 1; break;case 9: flag = 10; status = 1; break;case 10: flag = 11; status = 1; break;case 11: flag = 12; status = 1; break;case 12: flag = 13; status = 1; break;if (status = 0)flag = 100; /

12、标识符 ()数字()else if (ch >= '0') && (ch <= '9') /number = 0;while (ch >= '0' ) && (ch <= '9' ) number = number*10+(ch-'0');ch = programi+;flag = 200;i-;else switch (ch) / 运算符和标点符号 case '=': if (ch = '=') wordsj+ = ch; w

13、ordsj = '0' ch = programi+; if (ch = '=') wordsj+ = ch; wordsj = '0' flag = 401; lse ei-; flag = 402; break; case'>': if (ch = '>') wordsj+ = ch; wordsj = '0' ch = programi+;if (ch = '=') wordsj+ = ch; wordsj = '0' flag = 403; els

14、ei-; flag = 404; break; case'<': if (ch = '<') wordsj+ = ch; wordsj = '0'ch = programi+; if (ch = '=')wordsj+ = ch; wordsj = '0' flag = 405; elsei-; flag = 406; break;case'!': if (ch = '!') wordsj+ = ch; wordsj = '0'ch = programi+

15、; if (ch = '=') wordsj+ = ch; wordsj = '0'flag = 407;elsei-; flag = 408; break;case'+': if (ch = '+') wordsj+ = ch;wordsj = '0' ch = programi+; if (ch = '=')wordsj+ = ch; wordsj = '0'flag = 409;else if (ch = '+')wordsj+ = ch; wordsj = &

16、#39;0'flag = 410;elsei-;flag = 411;break;case'-':if (ch = '-') wordsj+ = ch;wordsj = '0' ch = programi+; if (ch = '=')wordsj+ = ch; wordsj = '0'flag = 412;else if( ch = '-')wordsj+ = ch; wordsj = '0'flag = 413;elsei-;flag = 414;break;case&#

17、39;*':if (ch = '*') wordsj+ = ch;wordsj = '0' ch = programi+; if (ch = '=') wordsj+ = ch; wordsj = '0'flag = 415;elsei-;flag = 416; break;case'/': if (ch = '/') wordsj+ = ch; wordsj = '0'ch = programi+; if (ch = '=')wordsj+ = ch; wo

18、rdsj = '0'flag = 417; elsei-;flag = 418; break;case'A':wordsj = ch; wordsj+1 = '0' flag = 419; break;case'':wordsj = ch; wordsj+1 = '0' flag = 501;break;case'(':wordsj = ch; wordsj+1 = '0'flag = 502; break;case')': wordsj = ch; wordsj+1

19、 = '0' flag = 503; break;case'': wordsj = ch; wordsj+1 = '0' flag = 504; break;case'': wordsj = ch; wordsj+1 = '0' flag = 505; break;case'': wordsj = ch; wordsj+1 = '0' flag = 506; break;case'': wordsj = ch; wordsj+1 = '0' flag

20、= 507; break;case':': wordsj = ch; wordsj+1 = '0' flag = 508; break;case'"': wordsj = ch; wordsj+1 = '0' flag = 509; break; case'%': if (ch = '%')wordsj+ = ch; wordsj = '0' ch = programi+; if (ch = '=') wordsj+ = ch; wordsj = '

21、0'flag = 510;elsei-;flag = 511; break;case',': wordsj = ch; wordsj+1 = '0' flag = 512;break; case'#': wordsj = ch; wordsj+1 = '0' flag = 513; break; case'': wordsj = ch; wordsj+1 = '0' flag = 514; break;case' ':/ 空格 wordsj ='_' word

22、sj+1 = '0' flag = 515;break; case'$': wordsj = '#' wordsj+1 = '0' flag = 0;break; default: flag = -1;break;return flag;void e();void e1();void e2();void t();void t1();void t2();void f();void f1();void p();void e()cout<<"E->TE''"<<endl;

23、 t();e2();void e1()if(flagstemp=411) cout<<"E'->+T"<<endl;temp+;t();else if(flagstemp=414) cout<<"E'->-T"<<endl; temp+;t();elseis_right=0;void e2()if(flagstemp=411|flagstemp=414) cout<<"E''->E'E''"<&l

24、t;endl;e1();e2();else if (flagstemp!=0|flagstemp!=503)cout<v"E”->A"v<e ndl;return ;elseis_right=0;void t()cout<<"T->FT''"<<endl;f();t2();void t1()if(flagstemp=416)cout<<"T'->*F"<<endl;temp+;f();else if(flagstemp=418)cou

25、t<<"T'->/F"<<endl;temp+;f();else is_right=0;void t2()if(flagstemp=416|flagstemp=418)cout<<"T''->T'T''"<<endl;t1();t2();else if (flagstemp!=0|flagstemp!=503)cout<v"T”->A"v<e ndl;return ;else is_right=0; void

26、f()cout<<"F->PF'"<<endl;p();f1();void f1()if(flagstemp=419)cout<<"F'->AF"<<e ndl;temp+;f();else if (flagstemp!=0&&flagstemp!=503&&flagstemp!=411 &&flagstemp!=414&&flagstemp!=416&&flagstemp!=418)cout<

27、<"F'->A"<<endl;is_right=1;temp+;void p()if(flagstemp=100|flagstemp=200)cout<<"P->i"<<endl;temp+;elseif(flagstemp=502)cout<<"P->(E)"<<endl;temp+;e();if(flagstemp=503)cout<<"P->(E)"<<endl;temp+;elseis_

28、right=0;else is_right =0; / 语义分析以及中间代码生成 int ye();int ye1(int a); int ye2(int a); int yt(); int yt1(int a); int yt2(int a); int yf();int yf1(int a); int yp();int v=-1;int num=0;int ww;string strn;int nn;int newTemp()num+;nn+;stringstream stream; stream<<nn;stream>>strn;stream.clear();cnt

29、num-1="temp"cntnum-1.operator+=(strn);/ 把字符串 s 连接到当前字符串的结尾 /cntnum-1=strcat("Temp",strn);/ cntnum-1="temp"return num-1;void siyuan(int a,int b,int c,int d)/输出四元 cout<<"("<<cnta<<","<<cntb<<","<<cntc<&l

30、t;","<<cntd<<")"<<endl;int ye()int rt,t1;rt=yt();t1=rt;rt=ye2(t1);return rt;int ye1(int a)int rt,t1;t1=a;if(flagstemp=411) / 加法 temp+;int tt=v+1;v+;int t2=yt();int rr=newTemp();siyuan(tt,t1,t2,rr);rt=rr;return rt;减法 else if(flagstemp=414) /temp+;int tt=v+1;v+;in

31、t t2=yt();int rr=newTemp();siyuan(tt,t1,t2,rr);rt=rr;return rt;else return t1; int ye2(int a) int rt,t1;t1=a;if(flagstemp=411|flagstemp=414) rt=ye1(t1);t1=rt;rt=ye2(t1);return rt;else if (flagstemp!=0|flagstemp!=503) return t1;else return t1; int yt()int rt,t1;rt=yf();t1=rt; rt=yt2(t1); return rt;in

32、t yt1(int a) int rt,t1;t1=a;if(flagstemp=416) / 乘法 int tt=v+1;v+;temp+;int t2=yf();int rr=newTemp(); siyuan(tt,t1,t2,rr);rt=rr;return rt;除法else if(flagstemp=418) / temp+;int tt=v+1;v+;int t2=yf();int rr=newTemp(); siyuan(tt,t1,t2,rr);rt=rr;return rt;else return t1; int yt2(int a) int rt,t;t=a;if(flagstemp=416|flagstemp=418) rt=yt1(t);t=rt;rt=yt2(t);return rt;else return t; int yf()int t1,rt;rt=yp();t1=rt;rt=yf1(t1);return rt; int yf1(int a) /乘方int rt,t1;t

温馨提示

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

评论

0/150

提交评论