1、编译原理实验报告编译原理实验报告指导老师: 丁老师专业班级:08软件工程3班姓 名: xxxx学 号: 0801070311xx时 间:2010年12月26日1试验目的设计编制并调试一个词法分析程序,加深对此法分析原理的理解。可以识别浮点数。2试验要求单词符号种别码单词符号 种别码单词符号种别码单词符号种别码main1=21,30“39int2+22;310100char3-23:32ERROR-1if4*2433public8else5/25=35include10while7)27=36struct11ID5028=37float12NUM2029!=38static13说明:在源程序的基
2、础上添加了许多新单词和对浮点数的识别。3程序结构程序用C编写,编译环境是VC6.0。syn 整型,记录单词符号的种别编码。tmp 字符数组,长度为256,可以缓存用户输入的字符串。token 字符数组,长度为32,存放识别出来的单词符号。ch 字符型,存放当前所取的字符。void getch() /读字符函数,从缓冲区读入下一个字符放在ch中,并指向下一个字符。void output() /打印指针所指向的字符串。void getbc() /检查ch是否为空白字符,若是则调用getch()直到ch中读入一个非空白字符。void concat() /把当前ch中字符与token中字符串连接。in
3、t letter() /判定ch中是否为字母。int digit() /判定ch中是否为数字。int ispoint() /判定ch中是否为小数点。int reserve() /对token中的字符串查表。若是返回关键字,否则返回50。void scanner() /扫描buffer字符串的词法分析函数。4总结程序可以成功运行,每次由用户输入一行字符串,程序将扫描此串,并将词法分析结果在屏幕上显示。程序也可以做到对浮点数的识别。如下程序:public static void main() int a = 0; int b = 100; float f = 1.5; char c = a; wh
4、lie(a!=z) a=a+1; f=(a+b-c)/a;5程序#include#include#include#include#define LEN sizeof(struct Node)struct Node char data; struct Node*next;struct Node*head,*p;char ch;char*key13=main,int,char,if,else,for,while,public,void,include,struct,float,static;char token20;void getch() ch=p-data; p=p-next;void out
5、put(struct Node*head) if(!head) printf(something wrong whith head node); exit(1); p=head-next; while(p-next!=NULL) printf(%c,p-data); p=p-next; printf(n);void getbc() while(ch= ) getch();void concat() unsigned int i; i=strlen(token); tokeni=ch; tokeni+1=0;int letter(char ch) return isalpha(int)ch);i
6、nt digit(char ch) return isdigit(int)ch);int ispoint(char ch) if(ch=.) return 1; else return 0;int reserve() int k; for(k=0;knext; while(Q-next!=p) Q=Q-next; p=Q;void back(int a,char*b) printf(%d,%s)n,a,b);void scaner() int c; token0=NULL; getch(); getbc(); if(letter(ch) while(letter(ch)|digit(ch) c
7、oncat(); getch(); retract(); c=reserve(); if(c!=10) back(c,token); else back(10,token); else if(digit(ch) int i=0; while(digit(ch)|ispoint(ch) if(ispoint(ch) i+; if(i=2) break; concat(); getch(); retract(); if(i=0)printf(20,%d)n,atoi(token); else printf(20,);printf(token);printf()n); else switch(ch)
8、 case+: back(22,+);break; case-: back(23,-);break; case*: back(24,*);break; case/: back(25,/); case(: back(26,();break; case): back(27,);break; case: back(28,); case: back(29,);break; case,: back(30,);break; case;: back(31,;);break; case: back(32,:);break; case=: getch(); if(ch=) back(37,=); else re
9、tract(); back(21,=); break; case: getch(); if(ch=) back(35,); else retract(); back(33,); break; case: getch(); if(ch=) back(36,=); else retract(); back(34,next=NULL; head-data= ; p=head; printf(please input your code:n); while(1) int i=0; char tmp256; gets(tmp); if(tmp0=#) break; p-next=(struct Node
10、*)malloc(LEN); if(!(head-next) printf(failed to create a new node!);exit(1); p=p-next; while(tmpi!=0)&(idata=tmpi; p-next=(struct Node*)malloc(LEN); if(!(p-next) printf(failed to create a new node!); exit(1); p=p-next; i+; p-data=n; p-next=NULL; printf(nthe code you input is:n); output(head); printf(nafter compling, the result is:n); p=head-next; while(p-next!=NULL) scaner(); system(pause); return 0;
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2