词法分析器实验报告_第1页
词法分析器实验报告_第2页
词法分析器实验报告_第3页
词法分析器实验报告_第4页
词法分析器实验报告_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

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

文档简介

1、词法分析器实验报告姓名:学号: 时间:实验内容:用flex工具生成一个PL/0语言的词法分析程序,对PL/0语言的源程序进行扫描,识别 出单词符号的类别,统计输出各种符号的信息实验目的:a)理解编译器的工作机制b)掌握编译器的构造方法c)掌握词法分析器的生成工具LEX的用法d)掌握语法分析器的生成工具YACC的用法实验环境:Window XPVisual C+ 6.0Flex.exe实验要求:1 .实现预处理功能。源程序中可能包含有对程序执行无意义的符号,要求将其剔除。2.实现词法分析功能。输入:预处理过的源程序。输出:把单词符号分为下面五类,然后统计PL0源程序中各单词符号出现的次数。1)K

2、类(关键字)2)I类(标识符)3)C类(常量)4)P类(算符及界符)5)O类(其他)3 .规则- 语句类型:赋值语句,if.then., while.do., read, write, call,复合语句 begin. end, 说明语句:const., var., procedure-13个关键字:if, then, while, do, read, write, call, begin, end, const, var, procedure, odd标识符定义为identifier,在程序中由范式定义:-identifier letter(letter|digit)*常量定义为Consta

3、nt,在程序中由范式定义:numberdigit+界符作为boundary-operator,在程序中通过列举定义:-(|)|,|;|.算符作为Operator,在程序中通过列举定义:-+|-|*|/|关系符号作为arithmetic-operator,在程序中通过列举定义-T=T=T:=T=T#TT+T-T+=T-=此外在程序中实现了错误字符,以及未知字符的定义:wrongid (digit+)letter(letter|digit)*空格、回车、换行符跳过。代码调试:根据lex的源程序的语法规则,源程序分为4个部分。声明:%#include#include#define maxname 2

4、0输出token输出token序列;主函数;void print();void main(int argc,char*argv);struct token二元组;char*idproperty; /token 属性值;char*idname; 识别的 token 名字;/源程序文件名;entitymaxnum; 定义1000个这样的token/源程序文件名;char filenamemaxname;int errnum=0;错误 token 的数目;int value;/属性值 int 型;int linenum=1;彳亍数;int count=0;/token 的个数;FILE*fpin;测

5、试文件指针;FILE*fpout;结果文件指针;%辅助定义:digit0-9lettera-zA-Znumberdigit+ identifier letter(letterldigit)*wrongid (digit+)letter(letterldigit)*newlinenwhitespacet+识别规则:”procedure”|”call”|”begin”|”end”|”var”|”const”|”iFTthen”|”while”|”do”|”read”|”write”|”odd”value=0;print();identifier value=1;print();number val

6、ue=2;print();+T-T*T/value=3;print();”|”=”|”=”|”:=”|”=”|”#”|”value=4;print();”(T)T,T;T.”value=5;print();wrongid value=6;print();newline linenum+=1;whitespace ;. value=7;print();%用户子程序:void print()count+;if(fpout=fopen(”My.txt”,”a”)=NULL)printf(cannot write the file /n);exit(0);if(value=5)正常情况下处理方式swi

7、tch(value)case 0:entitycount-1.idproperty=BasicKey”;break;case 1:entitycount-1.idproperty=identifier”;break;case 2:entitycount-1.idproperty=number”;break;case 3:entitycount-1.idproperty=arithmetic-op”;break;case 4:entitycount-1.idproperty=relation-op”;break;case 5:entitycount-1.idproperty=boundary-o

8、p”;break;entitycount-1.idname=yytext;fprintf(fpout,%dn”,count,entitycount-1.idname,entitycount-1.idproperty);else /wrongid时处理方式errnum+=1;switch(value)(case 6:entitycount-1.idproperty=Mixed number and letter:;break;case 7:entitycount-1.idproperty=Unkown operator:;break; entitycount-1.idname=yytext; f

9、printf(fpout,%dline:%d:%s%sn”,count,linenum,entitycount-1.idname,entitycount-1.idproperty); fclose(fpout); void main() printf(please input the PL/0 program file:); scanf(%s”,&filename); if(fpin=fopen(filename,r)=NULL) /打开文件 printf(cant open the file: %s,filename); exit(0); yyin=fpin;yylex(); /* yyin

10、是个FILE类型的指针,指向词法分析器要接收的待分析程序的指 针。每调用一次,yylex的返回值为当前分析的Word类型值。当文件结束时,yylex的返 回值为0。*/if(fpout=fopen(My.txt”,a)=NULL) printf(cannot write the file /n); exit(0); fprintf(fpout,n); fprintf(fpout,%d symbol(s) found. n %d error(s) found.n,count,errnum);fprintf(fpout,= =n);fclose(fpout); yywrap(); 操作过程:Win

11、dows下用felx进行编译:生成文件:lex.yy. lex.yy. 0 thenif n= 1 0 then1帝号:Zv符号:3 符号;4女符号I:5 M符号:6弋符号:号-号-号符符符V.号号号号号.号-V-导 符符符符符符符符符符符符 N . sC 类型:identifier. 、,类型;boundaiy-op 七”类型,identifier? ,气 类型:boundary-op 门”_ 类型:identifier F,类型:boundary-ap = 财 ljH,类型;identifier 孕,类型! boundary-ap 诡日in% 类型:BasTpKey .read,类型:BasicKey ,类型:boundan/-op 类型:identifier 切,类型:baundar-op T,类型:boundary-op read,类型:BasicKey X,类型:boundaiop 类型:identifier. 类型:bounda(v-op ; ,类型:boundary-op if,类型:日娈1出时添加错误代码于第二行:war a, b, nv sum

温馨提示

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

评论

0/150

提交评论