




已阅读5页,还剩11页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
编译原理 实验报告系 别电子信息系专 业计算机科学与技术班级学号 姓 名 指导教师成 绩2010年12月28号 实验一 词法分析一 实验要求:用C语言编写程序,用于实现对给定文件 中内容进行词法分析。二 实验目的:通过编写程序实现对给定文件中内容进行 词法分析,加深对词法分析的理解,并将其掌握。三 实验源程序代码:/ 词法分析.cpp : 定义控制台应用程序的入口点。#include #include #include #include #include #include using namespace std;bool isLetter(char ch) if (ch=A & ch=a & ch=0 & ch=9) return true; else return false;bool isP(char ch) if(ch=+|ch=*|ch=-|ch=/) return true; /ch=:|ch=,|ch=|ch=;|ch=(|ch=) else return false;bool isJ(char ch)if(ch=,|ch=;|ch=.|ch=(|ch=)|ch=|ch=|ch=|ch=:|ch=|ch=|ch=|ch=#) return true; / else return false;bool isBlank(char ch) if(ch= |ch=t) return true; else return false;int main() string src,ste,s; char ch0,ch,ch12; char ktt4820=and,begin,const,div,do,else,end,function,if,integer, not,or,procedure,program,read,real,then,type,var,while,write,标识符,无符号数, ,;,:,.,(,),.,+,-,+,-,*,/,=,=,:=,#; int pos=0; FILE *fp; fp=fopen(in.txt,r); ch0=fgetc(fp); while(ch0!=EOF) /if(ch0!=t)src+=ch0; src+=ch0; ch0=fgetc(fp); src+=#; coutsrcendl; ch=srcpos+; ste= ; for(int j=0;j47;j+)coutjkttjendl; cout词法分析:n; while(ch!=#) char str20; if(ch!=n) if(isDigit(ch) /判断常数 int i=0; while(isDigit(ch)|ch=.) stri+=ch; /i+; ch=srcpos+; stri=0; ste=ste+|+22; coutstr; continue; else if(isLetter(ch) /判断字符 int i=0,j; while(isLetter(ch)|isDigit(ch) stri+=ch; /i+; ch=srcpos+; stri=0; for(j=0;j21;j+) /判断是否关键字 int t=strcmp(str,kttj); if(t=0) stringstream ss; ste+=|; ssste;ssste; break; if(j=21)ste=ste+|+21; / cout ; coutstr; continue; else if(isP(ch) /判断是否运算符 int i=0,j; stri+=ch; stri=0; for(j=34;j38;j+) int t=strcmp(str,kttj); if(t=0) stringstream ss; ste+=|; ssste;ssste; break; coutstr; ch=srcpos+; continue; else if(isJ(ch) /判断是否界符 int i=0,j; while(isJ(ch) stri+=ch; ch=srcpos+; stri=0; for(j=23;j47;j+) int t=strcmp(str,kttj); if(t=0) stringstream ss; ste+=|; ssste;ssste; break; coutstr; continue; else if(isBlank(ch) coutch; ch=srcpos+; continue; else coutsteendl; ste= ; ch=srcpos+; return 0;四 给定文件中的内容:自己输入就行五 运行结果: 实验二 递归向下分析一 实验要求:用C语言设计、编制、调试一个典型的语法分析程序,用于实现下列语法的分析GS:EeBaAAa|bAcBBdEd|aCCe|dC二 实验目的:通过对GS问法的分析实现,加深对语法分析 的理解,进一步掌握常用的语法分析方法。三 实验源程序代码: #include char inputstream50;int temp=0;int right;void e();void a();void b();void c();void main()int right=1; char stag;while(1)if(stag=y) break;cout请输入你要分析的字符串,以#结束(为空字符):inputstream;e();if(inputstreamtemp=#&right=1)cout分析成功endl;elsecout分析失败endl;coutstag;void e()if(inputstreamtemp=e)couteBaAendl;temp+;b();if(inputstreamtemp=a)couteBaAendl;temp+;a();else right=1;else right=0;void a()if(inputstreamtemp=a)coutaendl;temp+;else if(inputstreamtemp=b)coutbAcBendl;temp+;a();if(inputstreamtemp=c)coutbAcBendl; temp+;b();else right=1;else right=0;void b()if(inputstreamtemp=d)coutdEdendl;temp+;e();if(inputstreamtemp=d)coutdEdendl;temp+;else right=1;else if(inputstreamtemp=a)coutaCendl;temp+;c();else right=0;void c()if(inputstreamtemp=e)couteendl;temp+;else if(inputstreamtemp=d)coutdCendl;temp+;c();else right=0;四 运行结果:实验三 LL(1)文法分析一 实验要求:用C语言编写程序,用于实现对输入字符串 中内容进行LL(1)文法分析。二 实验目的:通过编写程序实现对输入字符串中内容进行 语法分析,加深对语法分析的理解,并将其掌握。三 实验源程序代码:#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;typedef struct typechar origin;char array5;int length;type;type e,t,g,gl,s,sl,f,fl;type C1010;void print()int a;for(a=0;atop+1;a+)printf(%c,Aa);printf(tt);void print1()int j;for(j=0;jb;j+)printf( );for(j=b;j=1;j+)printf(%c,Bj);printf(ttt);void main()int m,n,k=0,flag=0,finish=0;char ch,x;type cha;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;gl.origin=G;gl.array0= ;g.length=1;s.origin=S;strcpy(s.array,*FS);s.length=3;sl.origin=S;sl.array0= ;sl.length=1;f.origin=F;strcpy(f.array,(E);f.length=3;fl.origin=F;fl.array0=i;fl.length=1;for(m=0;m=4;m+)for(n=0;n=5;n+)Cmn.origin=N;C00=e;C03=e;C11=g;C14=gl;C15=gl;C20=t;C23=t;C31=sl;C32=s;C34=C35=sl;C40=fl;C43=f;printf(提示:本程序只能对由i,+,*,(,)构成的以#结束的字符串进行分析n);printf(请输入要分析的字符串:);doscanf(%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;printf(步骤tt分析栈tt剩余字符tt所用产生式n);dox=Atop-;printf(%d,k+);printf(tt);for(j=0;j=5;j+)if(x=v1j)flag=1;break;if(flag=1)if(x=#)finish=1;printf(acc!n);getchar();getchar();/?exit(1);if(x=ch)print();print1();printf(%c匹配n,ch);ch=B+b;flag=0;else print();print1();printf(%c出错n,ch);exit(1);elsefor(j=0;j=4;j+)if(x=v2j)m=j;break;for(j=0;j,cha.origin);for(j=0;j=0;j-)A+top=cha.arrayj;if(Atop= )top-;else print();print1();printf(%c出错n,x);exit(1);while(finish=0);四 实验结果实验四 逆波兰式一 实验要求:用C语言编写程序,用于实现对输入表达式 求值。二 实验目的:通过编写程序实现对输入表达式进行 求值得出结果,加深对逆波兰求解表达式过程理解,并将其掌握。三 实验源程序代码:#include#include#define max 100char exmax; /存储后缀表达式void trans() /将算术表达式转化为后缀表达式char strmax;/存储原算术表达式的值char stackmax;/作为栈使用char ch;int sum,i,j,t,top=0;printf(*n);printf(*输入一个求值表达式,以#号结束。*n);printf(*n);printf(算术表达式: );i=0; /获取输入表达式,以#号结束doi+;scanf(%c,&stri);while(stri!=#&i!=max);sum=i;t=1;i=1;ch=stri;i+;while(ch!=#)switch(ch)case (:top+;stacktop=ch;break;case ):while(stacktop!=()ext=stacktop;top-;t+;top-;break;case +:case -:while(top!=0&stacktop!=()ext=stacktop;top-;t+;top+;stacktop=ch;break;case *:case /:while(stacktop=*|stacktop=/)ext=stacktop;top-;t+;top+;stacktop=ch;break;case :break;default:while(ch=0&ch=9)ext=ch;t+;ch=stri;i+;i-;ext=#;t+;ch=stri;i+;while
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 执法演讲面试题目及答案
- 先进制造市场分析与经营
- 环境评价公众参与机制在2025年公众参与评价与环境保护教育中的应用报告
- 供电试岗期满考试试题及答案
- 2025年运动控制 试题及答案
- 益智课程考试题及答案
- 2025年食堂食品安全管理人员考试试题及答案
- 2025年flash动画制作考试题及答案
- 2025年法语考试题目及答案
- 2025年陕西省辅警协警笔试笔试真题含答案
- 道路运输安全员考试题库及答案
- 2025重庆某国有企业招聘新媒体运营(偏拍摄剪辑)参考题库含答案
- 2025年秋期新教材部编人教版一年级上册道德与法治教学计划+进度表
- 医院绩效考核指标体系设计与实施
- 2025年农业农村局公务员招聘面试指南与模拟题解析
- 食堂食品安全风险日管控、周排查、月调度管理制度
- 2025年秋季新学期全体教职工大会上校长讲话:汇一股心力、立两个目标、守三条底线、打四场硬仗
- 2025至2030中国水射流强化泵行业项目调研及市场前景预测评估报告
- 2025年北师大版新教材数学三年级上册教学计划(含进度表)
- 招投标技术服务及售后承诺书
- 《推销实务》中职全套教学课件
评论
0/150
提交评论