免费预览已结束,剩余36页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验任务 完成以下正则文法所描述的Pascal语言子集单词符号的词法分析程序。字母 字母 数字 数字 数字 + * ; () =* = : /该语言的保留字 :begin end if then else for do while and or not 说明: 1 该语言大小写不敏感。 2 字母为a-z A-Z,数字为0-9。3可以对上述文法进行扩充和改造。4 /*/为程序的注释部分。设计要求1、给出各单词符号的类别编码。2、词法分析程序应能发现输入串中的错误。3、词法分析作为单独一遍编写,词法分析结果为二元式序列组成的中间文件。4、设计两个测试用例(尽可能完备),并给出测试结果。 demo.cpp #include #include #include #include demo.hchar token20;int lookup(char *token) for (int i = 0; i 11; i+) if (strcmp(token, KEY_WORDSi) = 0) return i+1; return 0;char getletter(FILE *fp) return tolower(fgetc(fp);void out(FILE *fp, int c, char *value) fprintf(fp, %d,%sn, c, value);void report_error(FILE *fp, char ch) fprintf(fp, There must be some error.n); fprintf(fp, %c.n, ch); fprintf(fp, n);void scanner(FILE *infile, FILE *outfile) char ch; int i, c; do do ch = getletter(infile); while(isspace(ch); if (isalpha(ch) token0 = ch; ch = getletter(infile); i = 1; while (isalnum(ch) tokeni = ch; i+; ch = getletter(infile); tokeni = 0; fseek(infile, -1, 1); c = lookup(token); if (c = 0) out(outfile, ID, token); else out(outfile, c, ); else if (isdigit(ch) token0 = ch; ch = getletter(infile); i = 1; while (isdigit(ch) tokeni = ch; i+; ch = getletter(infile); tokeni = 0; fseek(infile, -1, 1); out(outfile, INT, token); else switch(ch) case ) out(outfile, NE, ); else fseek(infile, -1, 1); out(outfile, LT, ); break; case =: out(outfile, EQ, ); break; case : ch = getletter(infile); if (ch = =) out(outfile, GE, ); else fseek(infile, -1, 1); out(outfile, GT, ); break; case : ch = getletter(infile); if (ch = =) out(outfile, FU, ); else fseek(infile, -1, 1); out(outfile, MAO, ); break; case /: ch = getletter(infile); if (ch = *) out(outfile, ZHU, ); else fseek(infile, -1, 1); out(outfile, XIE, ); break; case +: out(outfile, JIA, ); break; case -: out(outfile, JIAN, ); break; case *: out(outfile, CHEN, ); break; case ;: out(outfile, FEN, ); break; case (: out(outfile, ZUO, ); break; case ): out(outfile, YOU, ); break; default: if (ch != EOF) report_error(outfile, ch); break; while(ch != EOF); return;void main() FILE *in, *out; in = fopen(pascal.txt, r); out = fopen(result.txt, w); scanner(in, out); fprintf(out, 0,#); fclose(in); fclose(out); printf(Finished!n); 本文来自CSDN博客,转载请标明出处:/ffee/archive/2006/05/31/766483.aspx实验任务1、实现LL(1)分析中控制程序(表驱动程序);2、完成以下描述算术表达式的LL(1)文法的LL(1)分析程序(LL(1)分析表见教材)。GE: ETE EATE| TFT TMFT| F (E)|i A+|- M*|/ 说明:终结符号i为用户定义的简单变量,即标识符的定义。设计要求1、输入串应是词法分析的输出二元式序列,即某算术表达式“实验项目一”的输出结果。输出为输入串是否为该文法定义的算术表达式的判断结果。2、LL(1)分析过程应能发现输入串出错。3、设计两个测试用例(尽可能完备,正确和出错),并给出测试结果。demo.cpp #include #include #include #include demo.h#define SIZE 100char stackSIZE;int bottom = 0, top = 0;void push(char ch) if (top != SIZE) stacktop = ch; top+; char pop() if (top != bottom) top-; stacktop = 0; return stacktop; return -2;int get_id(FILE *fp) char temp100; int id; fscanf(fp, %d, &id); fgets(temp, 100, fp); return id;void translate(int id, char *a) switch(id) case 0: *a = #; break; case 12: *a = i; break; case 22: *a = +; break; case 23: *a = -; break; case 24: *a = *; break; case 21: *a = /; break; bool is_terminate(char ch) if (ch = i | ch = + | ch = - | ch = * | ch = / | ch = #) return true; return false;void trans(char ch, char a, int *i, int *j) switch (ch) case E: *i = 0; break; case e: *i = 1; break; case T: *i = 2; break; case t: *i = 3; break; case F: *i = 4; break; case A: *i = 5; break; case M: *i = 6; break; switch (a) case i: *j = 0; break; case +: *j = 1; break; case -: *j = 2; break; case *: *j = 3; break; case /: *j = 4; break; case (: *j = 5; break; case ): *j = 6; break; case #: *j = 7; break; void search_table(char ch, char a) int i, j; trans(ch, a, &i, &j); char temp20 = 0; strcpy(temp, TABLEij); if (strcmp(temp, ) = 0) pop(); return; else if (strcmp(temp, ) != 0) pop(); for (unsigned ii = 0; ii strlen(temp); ii+) push(tempii); return; else printf(Error!n); exit(0); void main() FILE *in; int id = 0; char a = 0; in = fopen(result.txt, r); push(#); push(E); id = get_id(in); translate(id, &a); while (true) if (is_terminate(stacktop-1) if (stacktop-1 = a & a = #) printf(success.n); return; else if (stacktop-1 = a) pop(); id = get_id(in); translate(id, &a); else printf(Error!n); return; else if (!is_terminate(stacktop-1) search_table(stacktop-1, a); fclose(in); 本文来自CSDN博客,转载请标明出处:/ffee/archive/2006/05/31/766487.aspx实验任务 完成以下描述算术表达式的LL(1)文法的递归下降分析程序GE: ETE EATE| TFT TMFT| F (E)|i A+|- M*|/ 说明:终结符号i为用户定义的简单变量,即标识符的定义。设计要求1、 输入串应是词法分析的输出二元式序列,即某算术表达式“实验项目一”的输出结果。输出为输入串是否为该文法定义的算术表达式的判断结果。2、 递归下降分析程序应能发现输入串出错。3、 设计两个测试用例(尽可能完备,正确和出错),并给出测试结果。demo.cpp bool E() if (T() if (E_prime() return true; return false;bool E_prime() if (A() if (T() if (E_prime() return true; else return false; else return false; return true;bool T() if (F() if (T_prime() return true; return false;bool T_prime() if (M() if (F() if (T_prime() return true; else return false; else return false; return true;bool F() if (a = () advance(); if (E() if (a = ) advance(); return true; else if (a = i) advance(); return true; return false;bool A() if (a = +) advance(); return true; else if (a = -) advance(); return true; return false;bool M() if (a = *) advance(); return true; else if (a = /) advance(); return true; return false;本文来自CSDN博客,转载请标明出处:/ffee/archive/2006/05/31/766742.aspx编译原理实验源代码 词法分析,LL(1),逆波兰式,LR(1) 1、词法分析/*cifa fenxi chengxu*/#include #include #include #include #include #define NULL 0FILE *fp;char cbuffer;char *key8=if,else,for,while,do,return,break,continue;char *border6=,;,(,);char *arithmetic4=+,-,*,/;char *relation6=,=,;char *consts20;char *label20;int constnum=0,labelnum=0;int search(char searchchar,int wordtype) int i=0; switch (wordtype) case 1:for (i=0;i=7;i+) if (strcmp(keyi,searchchar)=0) return(i+1); case 2:for (i=0;i=5;i+) if (strcmp(borderi,searchchar)=0) return(i+1); return(0); case 3:for (i=0;i=3;i+) if (strcmp(arithmetici,searchchar)=0) return(i+1); return(0); case 4:for (i=0;i=5;i+) if (strcmp(relationi,searchchar)=0) return(i+1); return(0); case 5:for (i=0;i=constnum;i+) if (strcmp(constsi,searchchar)=0) return(i+1); constsi-1=(char *)malloc(sizeof(searchchar); strcpy(constsi-1,searchchar); constnum+; return(i); case 6:for (i=0;i=labelnum;i+) if (strcmp(labeli,searchchar)=0) return(i+1); labeli-1=(char *)malloc(sizeof(searchchar); strcpy(labeli-1,searchchar); labelnum+; return(i); char alphaprocess(char buffer) int atype; int i=-1; char alphatp20; while (isalpha(buffer)|(isdigit(buffer) alphatp+i=buffer; buffer=fgetc(fp); alphatpi+1=0; if (atype=search(alphatp,1) printf(%s (1,%d)n,alphatp,atype-1); else atype=search(alphatp,6); printf(%s (6,%d)n,alphatp,atype-1); return(buffer);char digitprocess(char buffer) int i=-1; char digittp20; int dtype; while (isdigit(buffer) digittp+i=buffer; buffer=fgetc(fp); digittpi+1=0; dtype=search(digittp,5); printf(%s (5,%d)n,digittp,dtype-1); return(buffer);char otherprocess(char buffer) int i=-1; char othertp20; int otype,otypetp; othertp0=buffer; othertp1=0; if (otype=search(othertp,3) printf(%s (3,%d)n,othertp,otype-1); buffer=fgetc(fp); goto out; if (otype=search(othertp,4) buffer=fgetc(fp); othertp1=buffer; othertp2=0; if (otypetp=search(othertp,4) printf(%s (4,%d)n,othertp,otypetp-1); goto out; else othertp1=0; printf(%s (4,%d)n,othertp,otype-1); goto out; if (buffer=:) buffer=fgetc(fp); if (buffer=) printf(:= (2,2)n); buffer=fgetc(fp); goto out; else if (otype=search(othertp,2) printf(%s (2,%d)n,othertp,otype-1); buffer=fgetc(fp); goto out; if (buffer!=n)&(buffer!= ) printf(%c error,not a wordn,buffer); buffer=fgetc(fp);out: return(buffer);void main() int i; for (i=0;i=20;i+) labeli=NULL; constsi=NULL; ; if (fp=fopen(example.c,r)=NULL) printf(error); else cbuffer = fgetc(fp); while (cbuffer!=EOF) if (isalpha(cbuffer) cbuffer=alphaprocess(cbuffer); else if (isdigit(cbuffer) cbuffer=digitprocess(cbuffer); else cbuffer=otherprocess(cbuffer); printf(overn); getchar(); 2、LL(1)/*LL(1)分析法源程序,只能在VC+中运行*/#include#include#include#includechar A20;/*分析栈*/char B20;/*剩余串*/char v120=i,+,*,(,),#;/*终结符*/char v220=E,G,T,S,F;/*非终结符 */int j=0,b=0,top=0,l;/*L为输入串长度 */typedef struct type/*产生式类型定义 */ char origin;/*大写字符*/ char array5;/*产生式右边字符 */ int length;/*字符个数 */type;type e,t,g,g1,s,s1,f,f1;/*结构体变量*/type C1010;/*预测分析表 */void print()/*输出分析栈*/ int a;/*指针*/ for(a=0;a=top+1;a+) printf(%c,Aa); printf(tt);/*print*/void print1()/*输出剩余串*/ int j; for(j=0;jb;j+)/*输出对齐符*/ printf( ); for(j=b;j=l;j+) printf(%c,Bj); printf(ttt);/*print1*/void main() int m,n,k=0,flag=0,finish=0; char ch,x; type cha;/*用来接受Cmn*/ /*把文法产生式赋值结构体*/ e.origin=E; strcpy(e.array,TG); e.length=2; t.origin=T; strcpy(t.array,FS); t.length=2; g.origin=G; strcpy(g.array,+TG); g.length=3; g1.origin=G; g1.array0=; g1.length=1; s.origin=S; strcpy(s.array,*FS); s.length=3; s1.origin=S; s1.array0=; s1.length=1; f.origin=F; strcpy(f.array,(E); f.length=3; f1.origin=F; f1.array0=i; f1.length=1; for(m=0;m=4;m+)/*初始化分析表*/ for(n=0;n=5;n+) Cmn.origin=N;/*全部赋为空*/ /*填充分析表*/ C00=e;C03=e; C11=g;C14=g1;C15=g1; C20=t;C23=t; C31=s1;C32=s;C34=C35=s1; C40=f1;C43=f; printf(提示:本程序只能对由i,+,*,(,)构成的以#结束的字符串进行分析,n); printf(请输入要分析的字符串:); do/*读入分析串*/ scanf(%c,&ch); if (ch!=i) &(ch!=+) &(ch!=*)&(ch!=()&(ch!=)&(ch!=#) printf(输入串中有非法字符n); exit(1); Bj=ch; j+; while(ch!=#); l=j;/*分析串长度*/ ch=B0;/*当前分析字符*/ Atop=#; A+top=E;/*#,E进栈*/ printf(步骤tt分析栈 tt剩余字符 tt所用产生式 n); do x=Atop-;/*x为当前栈顶字符*/ printf(%d,k+); printf(tt); for(j=0;j=5;j+)/*判断是否为终结符*/ if(x=v1j) flag=1; break; if(flag=1)/*如果是终结符*/ if(x=#) finish=1;/*结束标记*
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 动火作业事故应急预案
- 生产安全事故应急救援预案
- 家具市场家具销售专员实习生面试题目及答案
- 2025年应急救援员工作人员岗位职业技能及安全基本知识考试题库及答案
- 柴油发电机设备维护与操作方案
- 建筑工程装饰装修质量验收规范详解
- 综合录井工初级试题题库汇编
- 2018中考数学一模试卷(青岛市)
- 2026年建设工程造价(土建工程)设计方案经济性比选自测试题
- 2026年智能家居设备租赁合同
- DB5133∕T 74-2023 甘孜藏餐 通 用规范
- 珠海市辅警笔试真题2025
- 跨境犯罪打击-洞察及研究
- 2026秋季中国东方航空股份有限公司综合管理部卫生保健岗位招聘考试模拟试题及答案解析
- 国际道路应急预案
- 物业保洁员职业安全培训手册
- 辽宁省工程咨询集团有限责任公司笔试题库
- 国家开放大学《市场营销策划(本)》形考任务1-4答案
- 2025年开封市市直机关计划选调公务员4人考试参考试题及答案解析
- 双相情感障碍合并躁狂发作护理查房
- 2025年乡镇社保服务中心招聘面试技巧及面试题目
评论
0/150
提交评论