当前位置:文档之家› 《编译原理》课程实验报告(词法分析)完整版

《编译原理》课程实验报告(词法分析)完整版

《编译原理》课程实验报告题目词法分析专业计算机指导教师签名华东理工大学信息学院计算机系2013年4月10日一.实验序号:《编译原理》第一次实验二.实验题目:词法分析三.实验日期:2013.3.27-2013.4.10四.实验环境(操作系统,开发语言)操作系统:Windows开发语言:C五.实验要求●修改词法:1)将标识符的词法改为“以大写字母或小写字母开头,后面可以跟大写字母或小写字母或数字或下划线”。

把while ((isalpha(buffer))||(isdigit(buffer)))改成while ((isalpha(buffer))||(isdigit(buffer))||buffer==’_’)2)将<条件>中的表示相等关系的单词“=”改为“= =”char *relation[6]={"<","<=","=",">",">=","<>"};把其中的=改成==即可3)将原来无小数的数改为可以有小数的数把while (isdigit(buffer))改成while (isdigit(buffer)||buffer==’.’)●用C语言开发词法分析程序。

读入用PL/0语言编写的测试用例源程序,将识别出的一个个单词组成单词流依序同时输出到屏幕和文件中。

六.实验步骤1)根据修改词法后的PL/0语言编写测试用例源程序。

2)用C语言编写词法分析程序。

读入PL/0语言的测试用例源程序,进行词法分析,将识别出的一个个单词组成单词流依序同时输出到屏幕和文件中。

3)设立断点,单步运行词法分析程序,依次单个输出单词。

分析和理解词法分析程序,解释词法分析程序中的数据和变量变化的原因和输出结果。

七.实验结果(测试用例源程序,运行结果部分截图,词法分析函数主要部分源程序PL0程序:const a=6,b=81;var x,y;procEdure p;procedure q;x:=2;beginx:=1;write(x);end;begincall p;end.C程序:#include <stdio.h>#include <ctype.h>#include <malloc.h>#include <stdlib.h>#include <string.h>#define NULL 0FILE *fp;char cbuffer;char*key[19]={"auto","break","case","char","const","continue","default","do"," double","else","enum","extern","float","for","goto","if","int","long","register"};char *border[6]={",",";","{","}","(",")"};char *arithmetic[4]={"+","-","*","/"};char *relation[6]={"<","<=","=",">",">=","<>"};char *consts[11]={"0","1","2","3","4","5","6","7","8","9","."};char *label[20];int labelnum=0;int search(char searchchar[],int wordtype){ int i=0;switch (wordtype) {case 1:for (i=0;i<=18;i++){ if (strcmp(key[i],searchchar)==0){return(1);}}return(0);break;case 2:{for (i=0;i<=5;i++){ if (strcmp(border[i],searchchar)==0)return(i+1);} return(0);}break;case 3:{for (i=0;i<=3;i++){ if (strcmp(arithmetic[i],searchchar)==0){ return(1);}}return(0);}break;case 4:{for (i=0;i<=5;i++){ if (strcmp(relation[i],searchchar)==0){ return(1);}}return(0);}break;case 5:{for (i=0;i<=10;i++){ if (strcmp(consts[i],searchchar)==0){return;}}return(0);}break;}}char alphaprocess(char buffer){ int atype;int i=-1;char alphatp[20];while ((isalpha(buffer))||(isdigit(buffer))){ alphatp[++i]=buffer;buffer=fgetc(fp);}alphatp[i+1]='\0';atype=search(alphatp,1);if(atype==1){printf("%s \t 保留字\n",alphatp);}//结束if(atype==0){printf("%s \t 标示符\n",alphatp);}return(buffer);}char digitprocess(char buffer){ int i=-1;char digittp[20];int dtype;while (isdigit(buffer)){ digittp[++i]=buffer;buffer=fgetc(fp);}digittp[i+1]='\0';dtype=search(digittp,5);if(dtype==1)printf("%s \t 数字\t %s\n",digittp,digittp);return(buffer);}char otherprocess(char buffer){ int i=-1;char othertp[20];int otype,otypetp;othertp[0]=buffer;othertp[1]='\0';otype=search(othertp,3);if (otype==1){ printf("%s \t 运算符\n",othertp);buffer=fgetc(fp);goto out;}otype=search(othertp,4);if (otype==1){ buffer=fgetc(fp);othertp[1]=buffer;othertp[2]='\0';otypetp=search(othertp,4);if (otypetp==1){ printf("%s \t 运算符\n",othertp);goto out;}elseothertp[1]='\0';printf("%s \t 运算符\n",othertp);goto out;}if (buffer==':'){ buffer=fgetc(fp);if (buffer=='=')printf(":= \t 运算符\n");buffer=fgetc(fp);goto out;}else{ if (otype=search(othertp,2)){ printf("%s \t 界符\n",othertp);buffer=fgetc(fp);goto out;}}if ((buffer!='\n')&&(buffer!=' '))printf("%c error,not a word\n",buffer);buffer=fgetc(fp);out: return(buffer);}void main(){if ((fp=fopen("example.txt","r"))==NULL)printf("error");else{cbuffer = fgetc(fp);while (cbuffer!=EOF){if (isalpha(cbuffer))cbuffer=alphaprocess(cbuffer);else if (isdigit(cbuffer))cbuffer=digitprocess(cbuffer);elsecbuffer=otherprocess(cbuffer);}printf("over\n");getchar();}}八.实验体会(词法分析程序修改的地方,解决问题的方法、心得体会等)通过本次试验,了解了词法分析的主要步骤,就是把标识符,保留字区分,并且能识别出空格,并把数据能从文件中读出来,主要识别标识符和保留字,主要通过比较参照一个事先建好的数组,里面包含了所有的保留字,不在其中的便是标识符。

相关主题