《编译原理实验报告(共8页).doc》由会员分享,可在线阅读,更多相关《编译原理实验报告(共8页).doc(8页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上标准实验报告南昌航空大学实验报告二00七 年 三 月 三 日课程名称: 编译原理 实验名称: 词法分析 班级: -13 姓名: 毛红梅 同组人: 指导教师评定: 签名: 一、 实验目的通过设计编制调试一个具体的词法分析程序,加深对词法分析原理的理解。并掌握在对程序设计语言源程序进行扫描过程中将其分解为各类单词的词法分析方法。这里以开始定义的c语言子集的源程序作为词法分析程序的输入数据。在词法分析中,自文件头开始扫描源程序字符,一旦发现符合“单词”定义的源程序字符串时,将它翻译成固定长度的单词内部表示,并查填适当的信息表。经过词法分析后,源程序字符串(源程序的外部表示)
2、被翻译成具有等长信息的单词串(源程序的内部表示),并产生两个表格:常数表和标识符表,它们分别包含了源程序中的所有常数和所有标识符。二、 实验要求程序能够从左到右一个字符一个字符地读入源程序,并对构成的源程序的字符流进行扫描和分解,从而识别出一个个单词(也称单词符号或符号)。并给出单词的值和属性。三、 实验步骤下面简要分析一下词法分析程序的运行流程:主函数main(): 打开要分析的C源程序,若不能正确打开,则报错。 先从源程序中读入一个字符ch,然后进行如下处理: 1、ch是字符:转入关键字和标识符处理子函数; 2、ch是数字:转入数字处理函数; 3、ch是其他字符:转入其他字符处理子函数;
3、结束。关键字和标识符处理子函数alphaprocess(char buffer); 1、将buffer送入临时数组alphatp0,再读入一个字符至buffer; 2、判断buffer是否为字符或数字,若是,则alphatp1=buffer; 3、重复1,2,直到2判断为假;在alphatp末尾添加0; 4、调用search()子函数,在关键字表中匹配alphatp,若匹配成功,则返回序号; 5、调用search,在标识符表中匹配alphatp,若匹配成功,则返回序号; 6、在标识符表中添加alphatp,并返回序号;四、 参考源代码skh.c 程序 #include main() print
4、f (“this is my first test turbor c ”); #include #include #include #include #include #define NULL 0FILE *fp;char cbuffer;char *key8=DO,BEGIN,ELSE,END,IF,THEN,VAR,WHILE;char *border6=,;,:=,.,(,);char *arithmetic4=+,-,*,/;char *relation6=,=,;char *consts20;char *label20;int constnum=0,labelnum=0;int se
5、arch(char searchchar,int wordtype) int i=0; switch (wordtype) case 1:for (i=0;i=7;i+) if (strcmp(keyi,searchchar)=0) return(i+1); ; case 2:for (i=0;i=5;i+) if (strcmp(borderi,searchchar)=0) return(i+1); ; return(0); case 3:for (i=0;i=3;i+) if (strcmp(arithmetici,searchchar)=0) return(i+1); ; ; retur
6、n(0); ; case 4:for (i=0;i=5;i+) if (strcmp(relationi,searchchar)=0) return(i+1); ; ; return(0); ; case 5:for (i=0;i=constnum;i+) if (strcmp(constsi,searchchar)=0) return(i+1); ; constsi-1=(char *)malloc(sizeof(searchchar); strcpy(constsi-1,searchchar); constnum+; return(i); ; case 6:for (i=0;i=label
7、num;i+) if (strcmp(labeli,searchchar)=0) return(i+1); ; labeli-1=(char *)malloc(sizeof(searchchar); strcpy(labeli-1,searchchar); labelnum+; return(i); ; char alphaprocess(char buffer) int atype; int i=-1; char alphatp20; while (isalpha(buffer)|(isdigit(buffer) alphatp+i=buffer; buffer=fgetc(fp); ; a
8、lphatpi+1=0; if (atype=search(alphatp,1) printf(%s (1,%d)n,alphatp,atype-1); else atype=search(alphatp,6); printf(%s (6,%d)n,alphatp,atype-1); ; return(buffer); char digitprocess(char buffer) int i=-1; char digittp20; int dtype; while (isdigit(buffer) digittp+i=buffer; buffer=fgetc(fp); digittpi+1=0
9、; dtype=search(digittp,5); printf(%s (5,%d)n,digittp,dtype-1); return(buffer); char otherprocess(char buffer) int i=-1; char othertp20; int otype,otypetp; othertp0=buffer; othertp1=0; if (otype=search(othertp,3) printf(%s (3,%d)n,othertp,otype-1); buffer=fgetc(fp); goto out; ; if (otype=search(other
10、tp,4) buffer=fgetc(fp); othertp1=buffer; othertp2=0; if (otypetp=search(othertp,4) printf(%s (4,%d)n,othertp,otypetp-1); goto out; else othertp1=0; printf(%s (4,%d)n,othertp,otype-1); goto out; ; if (buffer=:) buffer=fgetc(fp); if (buffer=) printf(:= (2,2)n); buffer=fgetc(fp); goto out; else if (oty
11、pe=search(othertp,2) printf(%s (2,%d)n,othertp,otype-1); buffer=fgetc(fp); goto out; ; if (buffer!=n)&(buffer!= ) printf(%c error,not a wordn,buffer); buffer=fgetc(fp);out: return(buffer); void main() int i; for (i=0;i=20;i+) labeli=NULL; constsi=NULL; ; if (fp=fopen(skh.c,r)=NULL) printf(error); el
12、secbuffer = fgetc(fp);while (cbuffer!=EOF) if (isalpha(cbuffer) cbuffer=alphaprocess(cbuffer); else if (isdigit(cbuffer) cbuffer=digitprocess(cbuffer); else cbuffer=otherprocess(cbuffer); ; printf(overn); ;五、实验结果 #error ,not a word include (6,0) (4.0) stdio (6,1) . (2,3) h (6,2) (4,3)main (6,3)( (2,4) (2,5) error not a word printf (6,5)( (2,4)“ error not a word this (6,6)is (6,7)my (6,8)first (6,9)lesson (6, 10)test (6,11)turbor (6,12)c (6,13)“ error not a word ) (2,5); (2,1) not a word over 实验体会 通过该实验,本人学会了编译程序中的第一阶段所要完成的任务,从实验中更加深刻的体会词法分析的工作原理和实现过程,以及每个阶段的数据结构及存储结构。专心-专注-专业