计算机22班编译原理实验报告-语法分析_第1页
计算机22班编译原理实验报告-语法分析_第2页
计算机22班编译原理实验报告-语法分析_第3页
计算机22班编译原理实验报告-语法分析_第4页
计算机22班编译原理实验报告-语法分析_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

一、实验目的1、强化对系统综合工程实现能力的训练2、加强对语法分析原理、方法和基本实现技术的理解;二、实验内容:用C语言或者其他的高级语言作为宿主语言完成C语言的词法分析器的设计针对if语句的文法编写一个递归下降分析程序,输出结果为抽象语法树。注意,if语句文法中的表达式E采用四则运算表达式的文法;抽象语法树的格式自行设计,如果需要降低难度的话,也可用具体语法树而不用抽象语法树作为输出.三、实验过#include<iostream>#include<cstring>#include<cstdio>#include<fstream>#include<cmath>charprechar;//dangqianconstcharconstintid=3;intFILE*fp=fopen("C:\\Users\\hasee\\Desktop\\in.txt","r");/*文本方式打开FILE*fout=fopen("C:\\Users\\hasee\\Desktop\\out.txt","w+");/*文本方式写入class{intkind;intkind2;doublevalue;mword*next;mword*nlast;intwline;mword(intki,charna[20],boolen,double{if(kind==4||kind==5||kind==8)}void{for(int{if(strcmp(key[i],wname)==0)}}void{ elseif(kind==2)fprintf(fout,"<%0.0f,int>",value);elseif(kind==4)fprintf(fout,"<%s,operator>",wname);elseif(kind==5)fprintf(fout,"<%s,bound>",wname);elseif(kind==6)fprintf(fout,"<%s,key>",wname);elseif(kind==7)fprintf(fout,"<%s,speclchar>",wname);elseif(kind==100)fprintf(fout,"<%s,over>",wname);if(enter)fprintf(fout,"\n");}{c=fgetc(fp);returnc;}voidfgword(intki,charna[20],boolen,doubleva)//chuangjian{//if(prechar!='\n')en=0;last-if(p->kind==3)p-}voidcnum()//chang{intvis=0;//houvisweishixiaoshu{{if(vis){error[ernum]=tline;else}{ }}intif(vis)ki=1;//xiaoshudouble ki=2;//zhengshuint}voidcname()//biaoshi{while(prechar>='0'&&prechar<='9'||||prechar>='A'&&prechar<='Z'||{}}void //caozuo{charna[4];if(prechar=='=')elseif(na[0]=='-'&&prechar==na[0]){na[1]=prechar;prechar=mget();}elseif(na[0]=='+'&&prechar==na[0]){na[1]=prechar;prechar=mget();}elseif(na[0]=='|'&&prechar==na[0]){na[1]=prechar;prechar=mget();}elseif(na[0]=='&'&&prechar==na[0]){na[1]=prechar;prechar=mget();}}void //caozuo{charna[4];if(prechar=='=')elseif(na[0]=='-'&&prechar==na[0]){na[1]=prechar;prechar=mget();}elseif(na[0]=='+'&&prechar==na[0]){na[1]=prechar;prechar=mget();}elseif(na[0]=='|'&&prechar==na[0]){na[1]=prechar;prechar=mget();}elseif(na[0]=='&'&&prechar==na[0]){na[1]=prechar;prechar=mget();}}void{prechar=mget();}void //duihuanhangfude{}void{charna[5]="head";}void //shuruzhihouzuihoushanhou{//cout<<endl<<endl<<"Thereare"<<tline<<"//cout<<endl<<endl<<"Thereare"<<ernum<<"//for(intfclose(fp);/*关闭文件流*/}void{{}fprintf(fout,"\n\n所有对象:反响\n");{}}void{if(prechar>='0'&&prechar<='9')if(prechar>='a'&&prechar<='z'||prechar>='A'&&prechar<='Z')if(prechar=='|'||prechar=='&'||prechar=='!'){coprtlogic();continue;}if(prechar=='+'||prechar=='-'){coprt();continue;}if(prechar=='*'||prechar=='/'){coprt();continue;}if(prechar=='('||prechar==')'){cbound();continue;}if(prechar=='['||prechar==']'){cbound();continue;}if(prechar==':'||prechar==';'){cbound();continue;}if(prechar=='\n'){tline++;continue;}if(prechar=='\\'){cchar();continue;}if(feof(fp)){fgword(100,na,1,1);break;}}}{{case10:fprintf(fout,"L->H ||case12:fprintf(fout,"L1->--- case13:fprintf(fout,"H->I case14:fprintf(fout,"H1->&&I case15:fprintf(fout,"H1->--- case16:fprintf(fout,"H->(L case17:fprintf(fout,"I->J case18:{if(use->wname[0]=='!'){fprintf(fout,"J->! else{fprintf(fout,"J->---- case19:fprintf(fout,"K->E case20:fprintf(fout,"K1->--- case22:fprintf(fout,"K->(L case21:fprintf(fout,"K1->%s %s\n",use->wname,use-case30:fprintf(fout,"s0->S case31:fprintf(fout,"S1->S case33:fprintf(fout,"S->{SS1 {case34:fprintf(fout,"S->D case35:fprintf(fout,"D-> %s\n",use->wname,use-case36:fprintf(fout,"P-> %s\n",use-case37:fprintf(fout,"S->ID= %s%s\n",use->wname,use->next-case38:fprintf(fout,"S->if(L)SW if(\n");break;case39:fprintf(fout,"W->---- case40:fprintf(fout,"W->elseS case41:fprintf(fout,"S->while(L)S while(\n");break;case )case }case ;}}voidssinit(){}void //ZHIXIANGXIAYIGEDUI{use=use-}void{}voidvoid{if(sskind<=3)//{ %s\n",use-elseif(sskind==2)fprintf(fout,"A->id fprintf(fout,"A->id }elseif(ssnamefst=={fprintf(fout,"A->(E %s\n",use-{ %s\n",use-}else}}void{{}else{fprintf(fout,"F1->%sAF1 }{fprintf(fout,"F1->--}}void{{}else}void{{fprintf(fout,"E1->%sFE1 }elsefprintf(fout,"E1->- }void{{}}/////////////////////算术表达式分析voidlogicL();void{{}elseif{}{}}void{{}else{}}void{{}else{}{}}void{{}elseif(ssnamefst=='<'||ssnamefst=='>'||ssnamefst{}}void{{}elseif(ssnamefst=='<'||ssnamefst=='>'||ssnamefst{}{}}void{{}elseif(ssnamefst=='<'||ssnamefst=='>'||ssnamefst{}}void{{}elseif(sskind<=3||ssnamefst=='('||ssnamefst=='!'||ssnamefst=='&'||ssnamefst=='<'||ssnamefst=='>'||ssnamefst{}{}}void{{}elseif(sskind<=3||ssnamefst=='('||ssnamefst=='!'||ssnamefst=='&'||ssnamefst=='<'||ssnamefst=='>'||ssnamefst{}}voidvoid{{ }}void{{}else{}{}}void{if(sskind==3&&use->next-{{}elsesyerror();cout<<"S->id=L\n";}else{{cout<<"int/double"<<use-{}else}else}else{{{cout<<"if(L)";}else{syerror();}else}else{}else{cout<<"whlie(L)\n{\n";{{}else}else}}void{}void{use=head-}int{inta;return0;}S->S->{SS1{H1->---S->{SS1{L1->---S->if(L)Sif;L->HS->ID=hH->IL->HI->JH->IH->(LI->JE->FH->(LF->AE->FA->F->AF1->-A->4E1->-F1->-K1->---E1->-H1->---K1->---L1->---H1->---S->{SS1{ L1->---;jS->IDL->HLsS1->----}H->IW->elseI->JS->ID=fH->(LL->HE->FH->IF->AI->JA->7H->(LF1->-E->FE1->-K1->---F->AA->8F1->-F->AE1->

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论