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