欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

    编译原理实验二语法分析器LL(1)实现.doc

    • 资源ID:24094176       资源大小:137.50KB        全文页数:9页
    • 资源格式: DOC        下载积分:15金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要15金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    编译原理实验二语法分析器LL(1)实现.doc

    【精品文档】如有侵权,请联系网站删除,仅供学习与交流编译原理实验二语法分析器LL(1)实现.精品文档.编译原理程序设计实验报告表达式语法分析器的设计班级:计算机1306班姓名:张涛学号:20133967实验目标:用LL(1)分析法设计实现表达式语法分析器实验内容:概要设计:通过对实验一的此法分析器的程序稍加改造,使其能够输出正确的表达式的token序列。然后利用LL(1)分析法实现语法分析。数据结构:int op=0; /当前判断进度char ch; /当前字符char nowword10="" /当前单词char operate4='+','-','*','/' /运算符char bound2='(',')' /界符struct Token int code; char ch10; /Token定义struct Token tokenlist50; /Token数组struct Token tokentemp; /临时Token变量struct Stack /分析栈定义 char *base; char *top; int stacksize;分析表及流程图关键函数:int IsLetter(char ch) /判断ch是否为字母int IsDigit(char ch) /判断ch是否为数字int Iskey(char *string) /判断是否为关键字int Isbound(char ch) /判断是否为界符int Isboundnum(char ch) /给出界符所在token值int init(STack *s) /栈初始化int pop(STack *s,char *ch) /弹栈操作int push(STack *s,char ch) /压栈操作void LL1(); /分析函数源程序代码:(加入注释)#include<stdio.h>#include<string.h>#include<ctype.h>#include<windows.h>#include <stdlib.h>int op=0; /当前判断进度char ch; /当前字符char nowword10="" /当前单词char operate4='+','-','*','/' /运算符char bound2='(',')' /界符struct Token int code; char ch10; /Token定义struct Token tokenlist50; /Token数组struct Token tokentemp; /临时Token变量struct Stack /分析栈定义 char *base; char *top; int stacksize;typedef struct Stack STack;int init(STack *s) /栈初始化 (*s).base=(char*)malloc(100*sizeof(char); if(!(*s).base) exit(0); (*s).top=(*s).base; (*s).stacksize=100; printf("初始化栈n"); return 0;int pop(STack *s,char *ch) /弹栈操作 if(*s).top=(*s).base) printf("弹栈失败n"); return 0; (*s).top-; *ch=*(*s).top); printf("%c",*ch); return 1;int push(STack *s,char ch) /压栈操作 if(*s).top-(*s).base>=(*s).stacksize) (*s).base=(char*)realloc(*s).base,(*s).stacksize+10)*sizeof(char); if(!(*s).base) exit(0); (*s).top=(*s).base+(*s).stacksize; (*s).stacksize+=10; *(*s).top=ch; *(*s).top+; return 1;void LL1();int IsLetter(char ch) /判断ch是否为字母 int i;for(i=0;i<=45;i+)if (ch>='a'&&ch<='z')|(ch>='A'&&ch<='Z')return 1; return 0;int IsDigit(char ch) /判断ch是否为数字 int i; for(i=0;i<=10;i+)if (ch>='0'&&ch<='9') return 1; return 0;int Isbound(char ch) /判断是否为界符 int i; for(i=0;i<2;i+) if(ch=boundi) return i+1; return 0;int Isoperate(char ch) /判断是否为运算符 int i; for(i=0;i<4;i+) if(ch=operatei) return i+3; return 0;int main()FILE *fp;int q=0,m=0;char sour200=" "printf("请将源文件置于以下位置并按以下方式命名:F:2.txtn");if(fp=fopen("F:2.txt","r")=NULL) printf("文件未找到!n"); else while(!feof(fp) if(isspace(ch=fgetc(fp); else sourq=ch; q+; int p=0; printf("输入句子为:n"); for(p;p<=q;p+) printf("%c",sourp); printf("n"); int state=0,nowlen=0; BOOLEAN OK=TRUE,ERR=FALSE; int i,flagpoint=0; for(i=0;i<q;i+) if(souri='#') tokenlistm.code='#' switch(state) case 0: ch=souri; if(Isbound(ch) if(ERR) printf("无法识别n"); ERR=FALSE; OK=TRUE; else if(!OK) printf("<10,%s>标识符n",nowword); tokentemp.code=10; tokentemp.ch10=nowword10; tokenlistm=tokentemp; m+; OK=TRUE; state=4; else if(IsDigit(ch) if(OK) memset(nowword,0,strlen(nowword); nowlen=0; nowwordnowlen=ch; nowlen+; state=3; OK=FALSE; break; else nowwordnowlen=ch; nowlen+; else if(IsLetter(ch) if(OK) memset(nowword,0,strlen(nowword); nowlen=0; nowwordnowlen=ch; nowlen+; OK=FALSE; else nowwordnowlen=ch; nowlen+; else if(Isoperate(ch) if(!OK) printf("<10,%s>标识符n",nowword); tokentemp.code=10; tokentemp.ch10=nowword10; tokenlistm=tokentemp; m+; OK=TRUE; printf("<%d,%c>运算符n",Isoperate(ch),ch); tokentemp.code=Isoperate(ch); tokentemp.ch10=ch; tokenlistm=tokentemp; m+; break; case 3: if(IsLetter(ch) printf("错误n"); nowwordnowlen=ch; nowlen+; ERR=FALSE; state=0; break; if(IsDigit(ch=souri) nowwordnowlen=ch; nowlen+; else if(souri='.'&&flagpoint=0) flagpoint=1; nowwordnowlen=ch; nowlen+; else printf("<20,%s>数字n",nowword); i-; state=0; OK=TRUE; tokentemp.code=20; tokentemp.ch10=nowword10; tokenlistm=tokentemp; m+; break; case 4: i-; printf("<%d,%c>界符n",Isbound(ch),ch); tokentemp.code=Isbound(ch); tokentemp.ch10=ch; tokenlistm=tokentemp; m+; state=0; OK=TRUE; break; printf("tokenlist值为%dn",m); int t=0; tokenlistm+1.code='r' m+; for(t;t<m;t+) printf("tokenlist%d值为%dn",t,tokenlistt.code); LL1(); printf("tokenlist值为%dn",m); if(op+1=m) printf("OK!"); else printf("WRONG!"); return 0;void LL1() STack s; init(&s); push(&s,'#'); push(&s,'E'); char ch; int flag=1; do pop(&s,&ch); printf("输出栈顶为%cn",ch); printf("输出栈顶为%dn",ch); printf("当前p值为%dn",op); if(ch='(')|(ch=')')|(ch='+')|(ch='-')|(ch='*')|(ch='/')|(ch=10)|(ch=20) if(tokenlistop.code=1|tokenlistop.code=20|tokenlistop.code=10|tokenlistop.code=2|tokenlistop.code=3|tokenlistop.code=4|tokenlistop.code=5|tokenlistop.code=6) op+; else printf("WRONG!"); exit(0); else if(ch='#') if(tokenlistop.code=0) flag=0; else printf("WRONG!"); exit(0); else if(ch='E') printf("进入En"); if(tokenlistop.code=10|tokenlistop.code=20|tokenlistop.code=1) push(&s,'R'); printf("将R压入栈n"); push(&s,'T'); else if(ch='R') printf("进入Rn"); if(tokenlistop.code=3|tokenlistop.code=4) push(&s,'R'); push(&s,'T'); printf("将T压入栈n"); push(&s,'+'); if(tokenlistop.code=2|tokenlistop.code=0) else if(ch='T') printf("进入Tn"); if(tokenlistop.code=10|tokenlistop.code=20|tokenlistop.code=1) push(&s,'Y'); push(&s,'F'); else if(ch='Y') printf("进入Yn"); if(tokenlistop.code=5|tokenlistop.code=6) push(&s,'Y'); push(&s,'F'); push(&s,'*'); else if(tokenlistop.code=3|tokenlistop.code=2|tokenlistop.code=0|tokenlistop.code=4) else if(ch='F') printf("进入Fn"); if(tokenlistop.code=10|tokenlistop.code=20) push(&s,10); if(tokenlistop.code=1) push(&s,')'); push(&s,'E'); push(&s,'('); else printf("WRONG!"); exit(0); while(flag);程序运行结果:(截屏)输入:(Aa+Bb)*(88.2/3)#注:如需运行请将文件放置F盘,并命名为:2.txt输出:思考问题回答: LL(1)分析法的主要问题就是要正确的将文法化为LL(1)文法。在程序实现时将分析表的动作逐一实现即可。

    注意事项

    本文(编译原理实验二语法分析器LL(1)实现.doc)为本站会员(豆****)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

    工信部备案号:黑ICP备15003705号 © 2020-2023 www.taowenge.com 淘文阁 

    收起
    展开