编译原理实验报告_第1页
编译原理实验报告_第2页
编译原理实验报告_第3页
编译原理实验报告_第4页
编译原理实验报告_第5页
已阅读5页,还剩22页未读 继续免费阅读

下载本文档

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

文档简介

中南林业科技大学 实验报告 课程名称: 编译原理 专业班级:2011 级计算机科学与技术 1 班 姓名: 朱斌俊 学号:20114390 2014 年 6 月 30 日 实验一 词法分析器的设计 一、实验目的 设计词法分析器 二、设计方法: 1. 写出该语言的词法规则。 2. 把词法规则转换为相应的状态转换图。 3. 把各转换图的初态连在一起,构成识别该语言的自动机 4. 设计扫描器 5. 把扫描器作为语法分析的一个过程,当语法分析需要一个单词时, 就调用扫描器。 6. 扫描器从初态出发,当识别一个单词后便进入终态,送出二元式。 3、实验结果 输入: 输出: 四、实验代码 #include #include #include #include #include using namespace std; #define IPTF cin #define OPTF cout /input flow struct WordType string str; int type; WordType(string s,int t)str=s,type=t; ; bool operator mp; vectorres; /翻译结果 string strCache; int cacheLen;/已经读取的数据 TL(); bool init(); bool printFile(); bool translate(); char getChar();/获取字符 char peek();/探测 string getWord(); /bool translateWordByMap(string str);/使用 map 翻译 private: bool isLetter(char ch)return (ch=a /end mpf type fstream ipf;/输入文件流 ipf.open(“inputFile.txt“,ios:in); cacheLen=0; if(!ipf) cout:iterator it; for(it=res.begin();it!=res.end();it+) otftype strtype strstrCache) /读取失败 return 0; strCache+= ; /加入一个空格分隔符 cacheLen=0; return (char)strCachecacheLen+; char TL:peek() if(strCpare(“)=0 | strCache.length()=cacheLen) if(! (IPTFstrCache) /读取失败 return 0; strCache+= ; /加入一个空格分隔符 cacheLen=0; return (char)strCachecacheLen; string TL:getWord() string strw; char ch;/输入字符 int type; /该单词的种类 bool pointFlag=0;/小数点标记位 while(1) ch=getChar(); if(ch= else if(ch= ) ch=TL:getChar(); if(ch=0) return strw; if(strw.size()=0) if(TL:isLetter(ch) | ch=_)type=1;/关键字 else if(TL:isNum(ch)type=2;/实数 else if(ch=+| ch=-)/实数 if(res.size()0 /前面的项为实数,则为运算符 else type=2;/前面的项不是数字 则为实数 else type=0; /开始对每个种类进行处理 if(type=1)/是字母开头类 if(TL:isLetter(ch) | TL:isNum(ch) | ch=_)/关键字的三种构成 strw+=ch; else cacheLen-; return strw; else if(type=2)/是数字开头类 if(strw.size()=0 else if(ch=. pointFlag=1; else if(TL:isNum(ch) strw+=ch; else /不是小数点也不是其他的数字 cacheLen-; return strw; else /ch 为其他符号(包括运算符) strw+=ch; ch=getChar(); if(mpstrw+ch!=0) return strw+ch; else cacheLen-; return strw; / end else / while return strw; bool TL:translate() string str; while(1) str=TL:getWord(); if(pare(“)=0)break; if(mpstr!=0) res.push_back(WordType(str,mpstr) );/包括类别 1,,4,5 else if(TL:isLetter(str0) | str0=_) res.push_back(WordType(str,2); /变量 2 else if(TL:isNum(str0)| str0=+ | str0=-) res.push_back(WordType(str,3) ); /常数 3 else cout #include #include using namespace std; #define SIZE 1000 char 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“,cha.origin);/*输出产生式*/ /for(j=0;j=0;j-)/*产生式逆序入栈*/ A+top=cha.arrayj; if(Atop=)/*为空则不进栈*/ top-; else printf(“sorry the input have error!n“); return; / while(Bl!=#); void inital() int m,n; /*把文法产生式赋值结构体*/ e.origin=E; strcpy(e.array,“TG“); e.length=strlen(e.array); t.origin=T; strcpy(t.array,“FS“); t.length=strlen(t.array); g.origin=G; strcpy(g.array,“+TG“); g.length=strlen(g.array); g1.origin=G; g1.array0=; g1.length=strlen(g1.array); s.origin=S; strcpy(s.array,“*FS“); s.length=strlen(s.array); s1.origin=S; s1.array0=; s1.length=strlen(s1.array); f.origin=F; strcpy(f.array,“(E)“); f.length=strlen(f.array); f1.origin=F; f1.array0=i; f1.length=strlen(f1.array); / for(m=0;m #include #include #include using namespace std; struct Item int type; double value; char ch; Item(int t,double v)type=t;value=v; Item(int t,int v)type=t;value=v; Item(int t,char c)type=t;ch=c; ; vectorpolan; int cmpch(char ch1,char ch2) int n1,n2; if(ch1=()n1=1; if(ch1=+|ch1=-)n1=2; if(ch1=*|ch1=/)n1=3; if(ch2=()n2=1; if(ch2=+|ch2=-)n2=2; if(ch2=*|ch2=/)n2=3; return n1-n2; void printPolan() for(int i=0;iready; for(i=0;i=0 for(i=0;ista; int i,j; double cur,ex; for(i=0;i #include using namespace std; template class Stack public: T sta20; int num; Stack()num=0; void push_back(T t) stanum+=t; T top() if(num0)return stanum-1; void pop() if(num0)num-; bool empty() return !num; ; struct Op int type; int idx; int go;/goto ; Stackstu; Stacksym; Stackipt; Op table_action103; Op table_goto102; Op findop(char cch,int cnum)/根据当前的字符和数字查找相应的操作并返回 int line; if(cch=a)line=0; if(cch=b)line=1; if(cch=#)line=2; return table_actioncnumline; int findgoto(char ch,int idx) int line; if(ch=S)line=0; if(ch=B)line=1; return table_gotoidxline.idx; bool cut(int idx) if(idx=0) if(sym.top()=S) sym.pop(); sym.push_back(E); stu.pop();/ return 1; else if(idx=1) if(sym.num=2 sym.pop(); sym.push_back(S); stu.pop();/ stu.pop();/ return 1; else if(idx=2) if(sym.num=2 sym.pop(); sym.push_back(B); stu.pop();/ stu.pop();/ return 1; else if(idx=3) if(sym.top()=b) sym.pop(); sym.push_back(B); stu.pop();/ return 1; return 0; int time=1; void show(Op op) printf(“%dt“,time+); for(int i=0;i=0;i-) coutipt.stai; printf(“t“); if(op.type=1) printf(“S%d“,op.idx); if(op.type=2) printf(“r%d“,op.idx); if(op.type=3) printf(“acc“); printf(“t“); void work() char cch; int cnum; bool complete=0; while(complete!=1) cch=ipt.top(); cnum=stu.top(); Op op=findop(cch,cnum); show(op); if(op.type=1) sym.push_back(ipt.top(); ipt.pop(); stu.push_back(op.idx); else if(op.type=3)/终止 return ; else cut(op.idx);/规约 /stu.pop();直接放到 cut 里面操作 op.go=findgoto(sym.top(),stu.top(); stu.push_back(op.go); coutop.go; coutendl; void init() table_action00.type=1;table_action00.idx=3; table_action01.type=1;table_action01.idx=4; table_action12.type=3;table_action12.idx=0; table_action20.type=1;table_action20.idx=6; table_action21.type=1;table_action21.idx=7; table_action30.type=1;table_action30.idx=3; table_action31.type=1;table_action31.idx=4; table_action40.type=2;table_action40.idx=3; table_action41.type=2;table_action41.idx=3; table_action52.type=2;table_acti

温馨提示

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

最新文档

评论

0/150

提交评论