编译原理词法分析器实验报告 (2).doc_第1页
编译原理词法分析器实验报告 (2).doc_第2页
编译原理词法分析器实验报告 (2).doc_第3页
编译原理词法分析器实验报告 (2).doc_第4页
编译原理词法分析器实验报告 (2).doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

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

文档简介

编译原理实验词法分析器姓名 * 学号 * 班级 * 完成日期 * 实验内容:自定义一种程序设计语言,或者选择已有的一种高级语言,编制它的词法分析程序。词法分析程序的实现可以采用任何一种编程语言和编程工具。从输入的源程序中,识别出各个具有独立意义的单词,即关键字、标识符、常数、运算符、界符。并依次输出各个单词的内部编码及单词符号自身值。(遇到错误时可显示“Error”,然后跳过错误部分继续显示)实验目的:加深对词法分析器的工作过程的理解;加强对词法分析方法的掌握;能够采用一种编程语言实现简单的词法分析程序;能够使用自己编写的分析程序对简单的程序段进行词法分析。实验环境:Window XP Visual C+ 6.0 Flex.exe实验要求:1. 对单词的构词规则有明确的定义;2. 编写的分析程序能够正确识别源程序中的单词符号;3. 识别出的单词以的形式保存在符号表中,正确设计和维护符号表;4.对于源程序中的词法错误,能够做出简单的错误处理,给出简单的错误提示,保证顺利完成整个源程序的词法分析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)* 常量定义为Constant,在程序中由范式定义: number digit+ 界符作为boundary-operator,在程序中通过列举定义: (|)|,|;|. 算符作为Operator,在程序中通过列举定义: +|-|*|/| 关系符号作为arithmetic-operator,在程序中通过列举定义 |=|=|:=|=|#|+|-|+=|-= 此外在程序中实现了错误字符,以及未知字符的定义: wrongid (digit+)letter(letter|digit)* 空格、回车、换行符跳过。代码调试:根据lex的源程序的语法规则,源程序分为4个部分。声明:%#include#include#define maxname 20#define maxnum 1000void print(); /输出token序列;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; /测试文件指针;FILE*fpout; /结果文件指针;%辅助定义:digit 0-9letter a-zA-Znumber digit+identifier letter(letter|digit)*wrongid (digit+)letter(letter|digit)*newline nwhitespace t+识别规则:procedure|call|begin|end|var|const|if|then|while|do|read|write|oddvalue=0;print();identifier value=1;print();number value=2;print();+|-|*|/ value=3;print();|=|=|:=|=|#| value=4;print();(|)|,|;|. 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)/正常情况下处理方式 switch(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-op;break; entitycount-1.idname=yytext; fprintf(fpout,%d n,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; fprintf(fpout,%d line:%d: %s %s n,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是个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.

温馨提示

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

评论

0/150

提交评论