词法分析词法分析报告.doc

词法分析词法分析报告.doc

ID:55755369

大小:111.54 KB

页数:7页

时间:2020-06-05

词法分析词法分析报告.doc_第1页
词法分析词法分析报告.doc_第2页
词法分析词法分析报告.doc_第3页
词法分析词法分析报告.doc_第4页
词法分析词法分析报告.doc_第5页
资源描述:

《词法分析词法分析报告.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、词法分析报告姓名:李思婕学号:912106840301班级:91210658011.程序目的:1.巩固对词法分析的基本功能和原理的认识。2.能够应用自动机的知识进行词法分析。3.理解并处理词法分析中的异常和错误。2.程序设计分析:该程序可分为如下几步:(1)打开fin.txt文件(2)从fin.txt逐个字符读入,若遇到空格则判断为一个标示符(3)将读入的标示符与关键字进行比较,若是关键字则识别(4)若不是关键字则默认为自定义标示符(5)将分析结果输出到自定义的TXT文件内本程序自行规定:(1)关键字"program","begin","end","var",

2、"integer","real","for","if","then","else","do","while","array","procedure","function","of","boolean","const","div","mod","and","or"(2)运算符:"+","-","*","/","="(3)界符:"{","}","[","]",";",",",".","(",")",":"(4)其他标记如字符串,表示以字母开头的标识符。(5)空格、回车、换行符跳过3.程序流程图4.关键部分程序voidanalyse(FILE*fpin,FILE*f

3、pout){chararr[MAXBUF];/*输入缓冲区,存放一个单词符号*/intj=0;while((ch=fgetc(fpin))!=EOF){if(ch==''

4、

5、ch=='t'){}/*碰到空格、tab则跳过*/elseif(ch==''){Line_NO++;}/*************************字符串的处理****************************/elseif(IsLetter(ch)){while(IsLetter(ch)

6、IsDigit(ch)){if((ch<='Z')&&(ch>='A'))ch=c

7、h+32;/*忽略大小写*/arr[j]=ch;j++;ch=fgetc(fpin);}fseek(fpin,-1L,SEEK_CUR);/*输入指针回退一个字符*/arr[j]='';j=0;if(se_res(arr)){/*如果是关键字*/fprintf(fpout,"%stt%d",arr,se_res(arr));}elsefprintf(fpout,"%stt%d",arr,1);/*普通标识符*//*************************数字的处理****************************/}elsei

8、f(IsDigit(ch)){while(IsDigit(ch)){arr[j]=ch;j++;ch=fgetc(fpin);}fseek(fpin,-1L,SEEK_CUR);arr[j]='';j=0;fprintf(fpout,"%stt%d",arr,2);/*无符号整数*/}elseswitch(ch){case'+':fprintf(fpout,"%stt%d","+",41);break;case'-':fprintf(fpout,"%stt%d","-",42);break;case'*':fprintf(fpout

9、,"%stt%d","*",43);break;case'(':fprintf(fpout,"%stt%d","(",25);break;case')':fprintf(fpout,"%stt%d",")",26);break;case'[':fprintf(fpout,"%stt%d","[",27);break;case']':fprintf(fpout,"%stt%d","]",28);break;case';':fprintf(fpout,"%stt%d",";",36);break;case'=':fp

10、rintf(fpout,"%stt%d","=",38);break;case'.':fprintf(fpout,"%stt%d",".",39);break;case',':fprintf(fpout,"%stt%d",",",40);break;case':':{ch=fgetc(fpin);if(ch=='=')fprintf(fpout,"%stt%d",":=",29);else{fprintf(fpout,"%stt%d",":",30);fseek(fpin,-1L,SEEK_CUR);}}break;ca

11、se'>':{ch=fgetc(fpi

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。