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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

本文(编译原理实习设计——词法分析程序.doc)为本站会员(聆听****声音)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

编译原理实习设计——词法分析程序.doc

1、编译原理实习设计词法分析程序一实习目的构造一个小语言的词法分析程序。 二实习要求 设计一个包含简单算术表达式、赋值语句、IF语句的小语言的文法。根据此文法,构造一个词法分析程序,输入以“”为结束符的源程序,输出为各类单词表和单词串文件。源程序和输出的单词串均以文件的形式存放。单词的自身值均为其对应的表的指针,如标识符表的指针、常数表的指针等。提交实习报告和程序运行结果。三相关图表1状态转换图空白0字母非字母与数字12* 图一:标志符和关键字的识数字空白数字非数字2*01图二:整数的识别数字数字*E或D 其他数字E或D+或-数字*其他数字数字116数字754320 图三:实数的识别27*26*2

2、5*24*23(*22)*21;*20,*|1918非|17|*16&15非&14&*=13*12非11=*10#*9*8非7%*6*5*4非3!*2“双引*1单引空白0图四:界符和算符的识别(1) 2单词符号对照表单词符号助记符内码值标志符整数浮点数关键字“!=%=&|,;()+=*=/=$ID$Int$FLOAT$KeyWord$Syin$Yinhao$Not$NotQU$Weiyu$GESHICONTROL$mo-EQ$EQU$ EQ$and$Wyu$Whuo$Or$douhao$fenhao$Lpar$Rpar$Lmid$Rmid$Lbig$Rbig$Plus$Plus-Plus$Pl

3、us-EQ$dec$dec-dec$dec-EQ$Mul$Mul-EQ$Div$Div-EQ字符串值数值数值字符串值“!=%=&|,;()+=*=/=四源程序和结果1分析器程序#include #include #include #define Max 120FILE *fprogram;FILE *foutput;int WordEnd;char BufferMax*2;char ch;int IsLetter(char ch)if( (ch=a & ch=A & ch=0 & ch=9)return(1);elsereturn(0);char *Par(char par, char *st

4、r)switch (par)case (:str=$Lpar;break;case ):str= $Rpar;break;case :str= $Lmid;break;case :str= $Rmid;break;case :str= $Lbig;break;case :str= $Rbig;break;default:break;return str;void WriteBuffer()char tmpch= ;int i=0;long pos;WordEnd=0;for(int t=0; tMax*2; t+)Buffert=NULL;while(iMax)ch=fgetc(fprogra

5、m);if(ch!=EOF)if(ch=n | ch=t | ch= )ch= ;if( !(tmpch= & ch= ) )if(ch=/)ch=fgetc(fprogram);if(ch!=EOF)if( ch=*)char flo;while(1)ch=fgetc(fprogram);if(ch!=EOF)if(ch=*)flo=ch;ch=fgetc(fprogram);if(ch=EOF)break;if(ch=/)ch=fgetc(fprogram);break;elsebreak;elsepos=ftell(fprogram);fseek(fprogram, pos-2,0);i

6、f(ch=fgetc(fprogram)=EOF)break;elsebreak;Bufferi=ch;i+;tmpch=ch;elsebreak;if( IsDigit(ch) | IsLetter(ch) | ch=. | ch=E | ch=e | ch=-)while(1)ch=fgetc(fprogram);if(ch!=EOF)if(IsDigit(ch) | IsLetter(ch) | ch=. | ch=E | ch=e | ch=-)Bufferi=ch;i+;WordEnd=1;elseif(ch= | ch=t | ch=n | ch=r)break;elsepos=f

7、tell(fprogram);fseek(fprogram, pos-1,0);break;elsebreak;if(ch= | ch=+ | ch=- | ch=* | ch=& | ch= | ch=% | ch= )while(1)ch=fgetc(fprogram);if(ch!=EOF)if(ch= | ch=+ | ch=- | ch=* | ch=& | ch= | ch=% | ch= )Bufferi=ch;i+;WordEnd=1;elseif(ch= | ch=t | ch=n | ch=r )break;elsepos=ftell(fprogram);fseek(fpr

8、ogram, pos-1,0);break;elsebreak;if(ch=)while(1)ch=fgetc(fprogram);if(ch!=EOF)if(ch=n | ch=t | ch= | ch=v | ch=b | ch=f | ch=r )Bufferi=ch;i+;WordEnd=1;elseif(ch= | ch=t | ch=n | ch=r)break;elsepos=ftell(fprogram);fseek(fprogram, pos-1,0);break;elsebreak;char Get_ch(int j) return Bufferj;void Output(

9、char id, char string)fprintf(foutput,%s %sn, id,string);int IsKeyword(char string)char *KeyWords=auto,double,int,struct,break, else,long,switch,case,enum,register,typedef,char,extern,return,union,const,float,short,unsigned,continue,for,signed,void,default,goto,sizeof,volatile,do,if,while,static,main

10、;for(int i=0; i33; i+)if(strcmp(string, KeyWordsi)=0)return 1;return 0;void Proccess()int i,j=0;char c;char stringMax;for(int q=0; qMax; q+)stringq=NULL;WordEnd=1 ? i=Max*2 : i=Max;while(ji)int k=0;c=Get_ch(j);if(c=#)j+;while(1)c=Get_ch(j);if(IsLetter(c) )stringk+=c;else if(c= | c=)Output($ID, strin

11、g);for(int p=0; p | c=)Output($ID, string);for(int p=0; pk; p+)stringp=NULL;break;else if( c=. | IsLetter(c) | IsDigit(c) )stringk+=c;break;j+; c=Get_ch(+j);if(IsLetter(c) | c=_)k=0;stringk+=c;j+;while(1)c=Get_ch(j);if(IsLetter(c) | c=_ | IsDigit(c)stringk+=c;elseif(IsKeyword(string) )Output($keywor

12、d, string);elseOutput($ID, string);for(int i=0; ik; i+)stringi=NULL;break;j+;if(IsDigit(c) )k=0;stringk+=c;j+;while(1)c=Get_ch(j);if(IsDigit(c) | c=- | c=e | c=E | c=.)stringk+=c;elseOutput($Digit, string);for(int i=0; ik; i+)stringi=NULL;break;j+;if(c=;)Output($feihao, ;);if(c=( | c= | c= | c=) | c

13、= | c=)char *str;str = new char5;string0 = c;Output( Par(c,str), string );string0 = NULL;delete str;if(c=)Output($Yinhao,);if(c=*)string0=c;c=Get_ch(+j);if(c=*)string1=c;Output($Power, string);else if(c=)string1=c;Output($Mul-EQ,string);elseOutput($Mul,string);j-;string0=string1=NULL;if(c=+)string0=

14、c;c=Get_ch(+j);if(c=+)string1=c;Output($Plus-Plus, string);else if(c=)string1=c;Output($Plus-EQ,string);elseOutput($Plus,string);j-;string0=string1=NULL;if(c=-)string0=c;c=Get_ch(+j);if(c=-)string1=c;Output($Dec-Dec, string);else if(c=)string1=c;Output($Dec-EQ,string);elseOutput($Dec,string);j-;stri

15、ng0=string1=NULL;if(c=/)string0=c;c=Get_ch(+j);if(c=)string1=c;Output($Div-EQ,string);elseOutput($Div,string);j-;string0=string1=NULL;if(c=|)string0=c;c=Get_ch(+j);if(c=|)string1=c;Output($Or, string);else if(c=)string1=c;Output($Or-EQ,string);elseOutput($Whuo,string);j-;string0=string1=NULL;if(c=&)

16、string0=c;c=Get_ch(+j);if(c=&)string1=c;Output($And, string);else if(c=)string1=c;Output($And-EQ,string);elseOutput($WAnd,string);j-;string0=string1=NULL;if(c=)string0=c;c=Get_ch(+j);if(c=)string1=c;Output($Yihuo-EQ,string);elseOutput($Yihuo,string);j-;string0=string1=NULL;if(c=)string0=c;c=Get_ch(+j);if(c=)string1=c;Output($xiaoEQ,string); else if(c=)string1=c;Output($Youyi,string);elseOutput($xiaoyu,string);j-;string0=string1=NULL;

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2