词法分析实验报告(含代码).doc

上传人:苏美尔 文档编号:8878103 上传时间:2021-01-22 格式:DOC 页数:8 大小:88KB
返回 下载 相关 举报
词法分析实验报告(含代码).doc_第1页
第1页 / 共8页
词法分析实验报告(含代码).doc_第2页
第2页 / 共8页
词法分析实验报告(含代码).doc_第3页
第3页 / 共8页
词法分析实验报告(含代码).doc_第4页
第4页 / 共8页
词法分析实验报告(含代码).doc_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《词法分析实验报告(含代码).doc》由会员分享,可在线阅读,更多相关《词法分析实验报告(含代码).doc(8页珍藏版)》请在三一文库上搜索。

1、词法分析一、 实验目的通过本教材附录B(词法分析程序)调试,改编一个词法分析程序,加深对词法分析原理的理解。二、 实验要求2.1 待分析的简单的词法(1)保留字:if,else, for, while, do, int ,read, write,real,char(2)纯单分界符:+ * () ; : ,(3)双分界符: = ! ,&,|2.2 实现功能:(1)在命令行中输入源程序文件名(包括文件名路径)(2)输入目标文件名(包括文件名路径)(3)调用所编词法分析代码将分析结果写入目标文件代码(编译完成)三、词法分析程序的算法思想:算法的基本任务是从源程序中识别出具有独立意义的单词符号,其基本

2、思想是根据扫描到单词符号的第一个字符的种类,分析与代码中相应的单词符号。3.1 主程序示意图:调用子程序输入串结束结束是否3.2词法分析示意图:输入源程序文件名输入目标文件名文件名判断文件名判断是是否否开始读取一个字符是否为字母?标识符处理是否数字?是是否组合整数数字分界符分界符处理注释处理反斜杠错误处理其他返回四、词法分析程序的C语言程序源代码:词法分析子程序:#include #include #include #define keywordSum 10char *keywordkeywordSum=if,else,for,while,do,int,read,write,char,real

3、;/添加的保留字char,real;char singleword50=+ - * () ; , : ;char doubleword10= = ! ;char doubleword110=& &;char doubleword210=|;extern char Scanin300,Scanout300;extern FILE * fin,*fout;int TESTscan()char ch,token40;int es=0,j,n;printf (请输入源程序文件名(包括路径):);scanf (%s,Scanin);printf (请输入词法分析输出文件名(包括路径):);scanf (

4、%s,Scanout);if (fin=fopen(Scanin,r)=NULL)printf(n打开词法分析输入文件出错!n);return(1);if(fout=fopen(Scanout,w)=NULL)printf(n创建词法分析输出文件出错!n);return(2);ch=getc(fin);while(ch!=EOF)while (ch= | ch=n| ch=t)ch=getc(fin);if(isalpha(ch)token0=ch;j=1;ch=getc(fin);while(isalnum(ch)tokenj+=ch;ch=getc(fin);tokenj=0;n=0;wh

5、ile (n=keywordSum)fprintf(fout,%st%sn,ID,token);else fprintf(fout,%st%sn,token,token);else if (isdigit(ch) token0=ch;j=1; ch=getc(fin); while (isdigit(ch) tokenj+=ch; ch=getc(fin); tokenj=0; fprintf(fout,%st%sn,NUM,token);else if (strchr(singleword,ch)0)token0=ch;token1=0;ch=getc(fin);fprintf(fout,%

6、st%sn,token,token);else if (strchr(doubleword,ch)0)token0=ch;ch=getc(fin);if (ch= = )token1=ch;token2=0;ch=getc(fin);else token1=0;fprintf(fout,%st%sn,token,token);/所加代码;else if (strchr(doubleword1,ch)0)/判断&token0=ch;ch=getc(fin);if(ch=&)token1=ch;token2=0;ch=getc(fin);elsetoken1=0;fprintf(fout,%st%

7、sn,token,token);/所加代码else if (strchr(doubleword2,ch)0)/判断|token0=ch;ch=getc(fin);if(ch=|)token1=ch;token2=0;ch=getc(fin);elsetoken1=0;fprintf(fout,%st%sn,token,token);/所加代码else if (ch=/)ch=getc(fin);if (ch=*)char ch1;ch1=getc(fin);doch=ch1;ch1=getc(fin);while (ch!=*|ch1!=/)&ch1!=EOF);ch=getc(fin);el

8、setoken0=ch;token1=0;fprintf(fout,%st%sn,token,token);else token0=ch;token1=0; ch=getc(fin); es=3; fprintf(fout,%st%sn,ERROR,token); fclose(fin);fclose(fout);return(es);主程序:#include#includeextern int TESTscan();char Scanin300,Scanout300;FILE *fin,*fout;void main()int es=0;es=TESTscan(); if(es0) printf(词法分析有错,编译停止!); else printf(词法分析成功! n);五、结果分析:当源代码文件为“a.t”:int a;a=10;read;&编译结果:当源文件为a.t:int a;real;for(a=10|a=1)return 0;结果为:b.t的结果为:六、总结:词法分析的基本任务是从字符串表示的源程序中识别出具有独立意义的单词符号,其基本思想是根据扫描到单词符号的第一个字符的种类,拼出相应的单词符号。通过本试验的完成,更加加深了对词法分析原理的理解。本组成员:阳京,黄菁华,龙功成

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1