欢迎来到天天文库
浏览记录
ID:51388111
大小:427.00 KB
页数:30页
时间:2020-03-23
《编译原理 语法分析实验.doc》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、编译原理程序设计实验报告表达式语法分析器的设计实现班级:计算机1306班姓名:李佳泽学号:20133951实现表达式语法分析器的设计实现,使用递归下降子程序、LL1分析法、LR分析法,判断一个表达式是否符合某个语法。实验内容:算数表达式的变换文法:E->TE1①(L(I流程图:PUSH(#),PUSH(E)j—逆序压栈NEXT(w)4yIPUSH(i)2、TOKEN源程序代码:1•递归调用子程序#include#include#include#include#includeusingnamespacestd;#defineNOTKEY50#defineiT00#definecT01#definesT02#defineCT03char*kt[5]={”intTmainTvoidTifVchar”};char*pt[18]={M>=H<=MintTy=3;intdd=O;charv3、n[]={F;e7T;t7F};charvt[]={T;W;w7(Vy,#};char*righi[]={“Te”,“WTe“,“(E)"};intseekkt(char*TOKEN){for(inti=0;i<5;i++){if(strcmp(TOKEN,kt[i])==0){returni+4;}}returnNOTKEY;intseekpt(char*TOKEN)for(inti=0;ivl&i++)if(strcmp(TOKEN,pt[i])==0){returni+10;return-1;intmain(){4、intTOHN();intintN();intscan();intRight();intprint();TOHN();intN();scan();if(dd==O){cout«endl;cout«M分析结果:*'«endl;cout«endl;Right();}print();return0;stacks;structNode{charcontent[30];inttype;Node*next;Node*itHead;Node*CtHead;Node*ctHead;Node*stHead;Node*TOHe5、ad;Node*pp;voidE();voidT();voidF();voidTOHN(){TOHead=newNode();strcpy(TOHead->content,”");TOHead->type=0;TOHead->next=NULL;voidsetTON(char*content,inttype){Node*p=TOHead;Node*temp=newNode();while(p->next!=NULL){p=p->next;}strcpy(temp->content,content);temp->type6、=type;temp->next=NULL;p->next=temp;〃初始化所有表头结点voidintN(){itHead=newNode();strcpy(itHead->content/H,);itHead・>type=iT;itHead->next=NULL;stHead=newNode();strcpy(stHead->content/M);stHead->type=sT;stHead->next=NULL;CtHead=newNode();strcpy(CtHead->content/,n);CtHead-7、>type=CT;CtHead->next=NULL;ctHead=newNode();strcpy(ctHead->content/,M);ctHead->type=cT;ctHead->next=NULL;}〃创建新节点voidsetN(char*contentjnttype,Node*head){Node*p=head;Node*temp=newNode();intflag=0;while(p->next!=NULL){if(strcmp(content,p->next->content)==0){flag=1;8、}p=p->next;}if(flag==0){strcpy(temp->content,content);temp->type=type;temp->next=NULL;p->next=temp;}voidprint()Node*p=TOHead;p=p->next;while(p!=NULL)p=p->next;})voids
2、TOKEN源程序代码:1•递归调用子程序#include#include#include#include#includeusingnamespacestd;#defineNOTKEY50#defineiT00#definecT01#definesT02#defineCT03char*kt[5]={”intTmainTvoidTifVchar”};char*pt[18]={M>=H<=MintTy=3;intdd=O;charv
3、n[]={F;e7T;t7F};charvt[]={T;W;w7(Vy,#};char*righi[]={“Te”,“WTe“,“(E)"};intseekkt(char*TOKEN){for(inti=0;i<5;i++){if(strcmp(TOKEN,kt[i])==0){returni+4;}}returnNOTKEY;intseekpt(char*TOKEN)for(inti=0;ivl&i++)if(strcmp(TOKEN,pt[i])==0){returni+10;return-1;intmain(){
4、intTOHN();intintN();intscan();intRight();intprint();TOHN();intN();scan();if(dd==O){cout«endl;cout«M分析结果:*'«endl;cout«endl;Right();}print();return0;stacks;structNode{charcontent[30];inttype;Node*next;Node*itHead;Node*CtHead;Node*ctHead;Node*stHead;Node*TOHe
5、ad;Node*pp;voidE();voidT();voidF();voidTOHN(){TOHead=newNode();strcpy(TOHead->content,”");TOHead->type=0;TOHead->next=NULL;voidsetTON(char*content,inttype){Node*p=TOHead;Node*temp=newNode();while(p->next!=NULL){p=p->next;}strcpy(temp->content,content);temp->type
6、=type;temp->next=NULL;p->next=temp;〃初始化所有表头结点voidintN(){itHead=newNode();strcpy(itHead->content/H,);itHead・>type=iT;itHead->next=NULL;stHead=newNode();strcpy(stHead->content/M);stHead->type=sT;stHead->next=NULL;CtHead=newNode();strcpy(CtHead->content/,n);CtHead-
7、>type=CT;CtHead->next=NULL;ctHead=newNode();strcpy(ctHead->content/,M);ctHead->type=cT;ctHead->next=NULL;}〃创建新节点voidsetN(char*contentjnttype,Node*head){Node*p=head;Node*temp=newNode();intflag=0;while(p->next!=NULL){if(strcmp(content,p->next->content)==0){flag=1;
8、}p=p->next;}if(flag==0){strcpy(temp->content,content);temp->type=type;temp->next=NULL;p->next=temp;}voidprint()Node*p=TOHead;p=p->next;while(p!=NULL)p=p->next;})voids
此文档下载收益归作者所有