logo资料库

C++实现编译原理自动机、LL1文法、及LR(0)文法的代码.doc

第1页 / 共59页
第2页 / 共59页
第3页 / 共59页
第4页 / 共59页
第5页 / 共59页
第6页 / 共59页
第7页 / 共59页
第8页 / 共59页
资料共59页,剩余部分请下载后查看
编译原理实验报告 Fundamentals of Compiling Experiment Report 学生所在学院:软件学院 学生所在班级: 学生姓名: 学 号: 指导教师: 教 务 处 2017 年 11 月
2
实验一:词法分析程序 一、实验目的 设计、编制和调试一个具体的词法分析程序,加深对词法分析的理解。 二、实验要求 1.通过对 PL/0 词法分析程序(GETSYS)的分析,编制一个具有以下功能的词法分析程序: a.输入为字符串(或待进行词法分析的源程序),输出为单词串,即由(单词,类 别)所组成的二元组序列; b.有一定的错误检查能力,例如能发现 2a 这类不能作为单词的字符串。 2.提交设计报告,报告内容包括: 实验目的、要求,算法描述,程序结构,主要变量说明,程序清单,调试情况,设 计技巧,心得体会。 三、程序清单 #include #include #include using namespace std; //定义 DFA 中所有的状态表 //enum StateType{start,Num,ID,EQ,NE,NM,NL,Com, //LineCom,MuleCom1,MulCom2,Special,Done,Str}; const int Start=1; const int Num=2; const int ID=3; const int EQ=4; const int NE=5; const int NM=6; const int NL=7; const int Coms=8; const int LineCom=9; const int MulCom1=10; const int MulCom2=11; const int Special=12; const int Done=13; const int Str=14; 3
string keywords[19]={"include","define","iostream","int","float","double","mai n", "if","else","for","while","do","goto","switch","case","static", "cin","cout","const"}; string spcialWords[23]={"{","}","[","]","(",")","#",",",".",";",":","\\","'" ,"\"",">>","<<","!=","=","==","<=",">=","++","--",}; string arimetic[5]={"+","-","*","/","%"}; ifstream inFile; const int BUF_SIZE=256; int lineSize=0; string nowLine; int lineOrd=0; int charPos=0; bool isEof=false; void openfile(const char*filePath) { inFile.open(filePath); } char getNextChar() { char nextChar='\0'; if(!(charPos
lineOrd++; cout<='a'&&c<='z')||(c>='A'&&c<='z')) return true; return false; } bool isDigit(char c) { if(c>='0'&&c<='9') return true; return false; } 5
bool isNum(string token) { bool flag=true; int len=token.length(); for(int i=0;i
return false; } void coutToken(string token) { "; cout<<" if(isKeyWord(token)) { cout<
while(currentstate!=Done&&!isEof) { int n=lineOrd; char c=getNextChar(); isSave=true; switch(currentstate) { case Start: if(isDigit(c)){ currentstate=Num; }else if(isLetter(c)||c=='.'){ currentstate=ID; }else if(c==' '||c=='\t'||c=='\n'){ isSave=false; }else if(c=='!'){ currentstate=NE; }else if(c=='='){ currentstate=EQ; }else if(c=='<'){ currentstate=NM; }else if(c=='>'){ currentstate=NL; }else if(c=='/'){ currentstate=Coms; isSave=false; }else if(c=='"'){ currentstate=Str; }else{ currentstate=Done; } break; case Num: if(!isDigit(c)){ currentstate=Done; rollBackChar(1); isSave=false; } break; case ID: if(!isLetter(c)&&!isDigit(c)){ currentstate=Done; 8
分享到:
收藏