




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、编译原理程序设计实验报告表达式语法分析器的设计班级:计算机1306班姓名:张涛学号:20133967实验目标:用LL(1)分析法设计实现表达式语法分析器实验内容:概要设计:通过对实验一的此法分析器的程序稍加改造,使其能够输出正确的表达式的token序列。然后利用LL(1)分析法实现语法分析。数据结构:int op=0; /当前判断进度char ch; /当前字符char nowword10="" /当前单词char operate4='+','-','*','/' /运算符char bound2='(&
2、#39;,')' /界符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 I
3、sbound(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>in
4、t 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 /分析栈定义 cha
5、r *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("弹栈失败
6、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+=
7、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>=&
8、#39;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=" &q
9、uot;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
10、"); 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",no
11、wword); 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,st
12、rlen(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
13、),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;
14、 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; tokenlist
15、m=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
16、!"); 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='+'
17、)|(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
18、(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("
19、;进入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');
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 行政权力的适当行使与法律约束试题及答案
- 网络管理员考试技术提升试题及答案
- 法学概论考试常见题型试题及答案
- 责任与担当的当代年轻人试题及答案
- 高考全面评估试题及答案
- 行业趋势分析与年度应对计划
- 行业动态监测月度工作计划
- 营销推广方案计划
- 企业责任与年度工作结合计划
- 难民潮对欧洲经济的挑战探讨试题及答案
- DB35T 88-2022 伐区调查设计技术规程
- 蜗牛与黄鹂鸟(课件)人音版音乐二年级上册
- 经济师考试旅游经济(中级)专业知识和实务试卷及解答参考
- 班级规章 创造和谐
- 2024全国高中数学联赛山东赛区预赛试卷(含解析)
- 2024年中国家具电商行业市场竞争格局及投资方向研究报告(智研咨询)
- 导数(30题)-2024年考前15天高考数学冲刺大题训练(新高考)含答案
- 高层建筑一栋一册消防安全档案
- 创造性思维与创新方法智慧树知到期末考试答案章节答案2024年大连理工大学
- 外科围手术期营养支持疗法
- 广东省深圳市南山区2023-2024学年四年级下学期期末科学试题
评论
0/150
提交评论