《编译原理 语法分析实验》由会员分享,可在线阅读,更多相关《编译原理 语法分析实验(30页珍藏版)》请在金锄头文库上搜索。
1、编译原理程序设计实验报告表达式语法分析器的设计实现班级:计算机1306班姓名:李佳泽学号:20133951实现表达式语法分析器的设计实现,使用递归下降子程序、 LL1分析法、LR分析法,判断一个表达式是否符合某个语法。 实验内容:算数表达式的变换文法:E - T E1 (L (I流程图:PUSH (#), PUSH (E)j逆序压栈NEXT(w)4yI PUSH (i) err end数据结构:struct Nodechar con ten 七3。;int type;Node * next;建立链表用于标识符IT,常数Ct ,字符存社,字符串St , TOKEN源程序代码:1递归调用子程序#i
2、 nc lude #include #i nc lude #include #include using namespace std;#define NOTKEY 50#define iT 00#define cT 01#define sT 02#define CT 03char * kt5=”intTmainTvoidTifVchar”;char*pt18=M=H=Mint Ty =3;int dd=O;charvn=F;e7T;t7F;charvt=T;W;w7(Vy, #;char * righi= “Te”,“WTe“,“(E);int seekkt(char * TOKEN)for(
3、int i=0; i5; i+)if(strcmp(TOKEN,kti) = 0)return i+4;return NOTKEY;int seekpt(char * TOKEN)for(int i=0; ivl& i+) if(strcmp(TOKEN,pti) = 0) return i+10;return -1;int main()int TOHN();int intN ();int scan();int Right();int print();TOHN();intN ();scan();if(dd=O)coutendl;coutM分析结果:*endl;coutendl;Right();
4、print();return 0;stack s;struct Nodechar content30; int type;Node * next;Node * itHead;Node * CtHead;Node * ctHead;Node * stHead;Node * TOHead;Node * pp;void E();void T();void F();void TOHN()TOHead = new Node(); strcpy(TOHead-content, ”);TOHead-type = 0;TOHead-next = NULL;void setTON(char * content,
5、int type)Node * p = TOHead ;Node * temp = new Node(); while(p-next!=NULL)p = p-next;strcpy(temp-content,content);temp-type = type;temp-next = NULL;p-next = temp;初始化所有表头结点void intN()itHead = new Node(); strcpy(itHead-content/H,); itHead type = iT; itHead-next = NULL;stHead = new Node();strcpy(stHead-
6、content/M); stHead-type = sT; stHead-next = NULL;CtHead = new Node(); strcpy(CtHead-content/,n);CtHead-type = CT;CtHead-next = NULL;ctHead = new Node();strcpy(ctHead-content/,M);ctHead-type = cT; ctHead-next = NULL;创建新节点void setN(char * content jnt type,Node * head)Node * p = head ;Node * temp = new
7、 Node();int flag = 0;while(p-next!=NULL)if(strcmp(content,p-next-content) = 0)flag = 1;p = p-next;if(flag = 0)strcpy(temp-content,content);temp-type = type;temp-next = NULL;p-next = temp; void print()Node * p = TOHead;p = p-next; while(p!=NULL)p = p-next;)void scan()I/char filename50;char ch;char a3
8、0;char * TOKEN;int i;FILE * fp;/*printfC输入语法分析的C语言程序(形如D:Wtest.txt): nu); scanf(H%sn,filename);fp = fopen(filename,r);*/fp=fopen(Mtest.txtM,r);ch = fgetc(fp);while(ch !=#)i = 0;以字母或者下划线开头,处理关键字或者标识符 if(ch=,A, & ch=,a, & ch=A & ch=,a, & ch=,0, & ch=,0, & ch=,0, & ch=,0, & ch=,0, & chv=9)ai+ = ch; ch
9、= fgetc(fp);处理 Doubleif(ch = E | ch = O)ai+ = ch; ch = fgetc(fp);if(ch = +| ch =) ai+ = ch; ch = fgetc(fp);)if(ch =*0* & chv=9)ai+ = ch; ch = fgetc(fp);TOKEN= new chari+l;memcpy(TOKEN,a,i);TOKENi = *0*;if(flag = 0)setN(TOKEN,CT,CtHead);setTON(TOKEN,CT);elsesetN(TOKEN,CT,CtHead);setTON(TOKEN,CT);fsee
10、k(fp, 1 L,SEEK_CUR);处理常量字符串else if(ch = *)ai+J = ch;ch = fgetc(fp); while(ch!=,r)ai+ = ch;ch = fgetc(fp);ai+ = ch;ch = fgetc(fp);TOKEN = new chari+1;memcpy(TOKEN,a,i);TOKENfisetN(TOKEN,sT,stHead);setTON(TOKEN.sT);fseek(fpr 1 L,SEEK_CUR);/处理常量字符else if(ch = *)ai+ = ch; ch = fgetc(fp); while(ch!=V)ai+
11、l = ch; ch = fgetc(fp);ai+ = ch;ch = fgetc(fp);TOKEN = new chari+l;memcpy(TOKEN,aj); TOKENi = *0*; setN(TOKEN,cT,ctHead);setTON(TOKEN,cT);fseek(fprlL,SEEK_CUR);结朿符elseif(ch=*!&chv=7)|(ch 二丫&ch=l&ch=f(& chv)int seektemp;if(ch=,|ch=,=,)ai+ = ch;ch 二 fgetc(fp);else ai+ =ch;ch = fgetc(fp);TOKEN = new chari+l J;memcpy (TOKENa J);TOKENi = O;seektemp=seekpt(TOKEN);if(seektemp=-1)/结束符不存在coutH结束符错误请检2Mendl;dd=l;)elsesetTON(TOKEN,seektemp); fseek(fp,lL,SEEK_CUR);ch = fgetc(fp);setTON(H#333);