




已阅读5页,还剩10页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
编译原理实验学号:姓名: 班级:实验一:文件读入缓冲区,每次单字符输出源码:#include #include #include using namespace std;string ad1,ad2,cache;int curPos;bool getIn()coutad1;ifstream fin(ad1);if(fin.is_open()string str(istreambuf_iterator(fin), istreambuf_iterator();cache.swap(str);fin.close();curPos = 0;return true;return false;char getChar()if(curPos cache.size()return cachecurPos+;else return 0;int main()if(!getIn()return 1;char ch;while(ch = getChar()coutch;实验结果:实验二:词法分析#include #include #include #include #include #include #include #include using namespace std;const int BUFFER_SIZE = 1024;char bufferBUFFER_SIZE;enum Datatype RESERVE_WORD=1,IDENTIFIER=2,DIGIT=3,OPERATOR=4,SEPARATOR=5;/注意后面有分号const string Keyword = main,if,cout,int,return;ifstream inFile;ofstream outFile;int error;char cur,nex,ch;typedef structDatatype type;string value;Mytype;Mytype temp;queueque;inline void InputFile()string inFilename;cout Input the name of SourceFile: inFilename;inFile.open(inFilename.c_str();/string:c_str()返回的是const指针,String类中的一个函数if(!inFile)cout Cannot open the file! endl;system(pause);exit(1);return;inline void OutputFile()string outFilename;cout Input the name of DestinationFile: outFilename;outFile.open(outFilename.c_str();/string:c_str()返回的是const指针,String类中的一个函数if(!outFile)cout Cannot open the file! endl;exit(1);/同时把信息导入到文件中if(error)cout The number of errors: error endl;outFile The number of errors: error endl;while(que.size()temp = que.front();que.pop();outFile ( temp.type , temp.value ) endl;cout ( temp.type , temp.value ) endl;return;bool IsKeyword(string &str)for(int i=0;i5;i+)if(str = Keywordi) return true;return false;/注意有的运算符需要进行超前搜索才能确定bool IsOperator(char ch)if(ch=|ch=+|ch=-|ch=*|ch=/|ch=!) return true;return false;bool IsSeperator(char ch)if(ch=,|ch=;|ch=(|ch=)|ch=|ch=) return true;return false;inline void Acquire_line()/while(inFile.getline(buffer,BUFFER_SIZE,n) cout buffer endl;while(inFile.getline(buffer,BUFFER_SIZE,n)int i=0;for(i;istrlen(buffer);i+)temp.value = 0;/bug:因为定义的temp是全局变量,而temp.value是字符串,所以每次使用都有“清0”ch = bufferi;if(isspace(ch) continue;if(IsOperator(ch)/+,-,*,/,!temp.type = OPERATOR;temp.value += ch;if(i(strlen(buffer)-1)/需要读取下一个字符进行查验nex = buffer+i;if(IsOperator(nex)if(nex=) temp.value +=nex;else if(ch=+&nex=+)|(ch=-&nex=-) temp.value+=nex;else cout error: temp.value endl;if(bufferi!=) -i;que.push(temp);else if(IsSeperator(ch)temp.type = SEPARATOR;temp.value += ch;que.push(temp);else if(isdigit(ch)/一旦查看到的是一个数字,需要继续往后搜索是否有后续的数字。temp.type = DIGIT;temp.value += ch;do/再嵌套一个循环让其找完数nex = buffer+i;if(isdigit(nex) temp.value += nex;while(isdigit(nex)&istrlen(buffer);if(!isdigit(nex) -i;que.push(temp);elseif(isalpha(ch)/是字符temp.value += ch;donex = buffer+i;if(isalpha(nex) temp.value += nex;while(isalpha(nex)&istrlen(buffer);if(!isalpha(nex) -i;/剩下的就是我们定义的字符,需要先判断关键字,然后判断标识符/const string Keyword = main,if,cout,int,return;/*enum Datatype RESERVE_WORD=1,IDENTIFIER=2,DIGIT=3,OPERATOR=4,SEPARATOR=5;/注意后面有分号*/bool flag = true;for(int i=0;i5;i+)if(temp.value=Keywordi)temp.type = RESERVE_WORD;flag = false;que.push(temp);break;if(flag)temp.type = IDENTIFIER;que.push(temp);return ;/*从文件中读入一行:ifstream ff; char llMAXLEN;ff.getline(ll,MAXLEN,n);*/void Initial()error = 0;while(que.size() que.pop();return ;int main()Initial();InputFile();Acquire_line();OutputFile();return 0;实验结果:实验三:自顶向下的语法分析#include #include #include #include #include #include #include #include #include #include using namespace std;const string ID = num,NUM = num,ADD = +,SUB = -,MUL = *,DIV = /,NOTEND = NOTEND,LEFTK=(,RIGHTK = ); const string E = E,E1 = E,T = T,T1 = T,F = F;const string ENDofFILE = $,NOTHING = NULL;class Elementpublic:string name, kind;Element()Element(string a,string b):name(a),kind(b);string cache;int curPos;vector vecWord;void fread()ifstream fin(test.cpp);string tmp(istreambuf_iterator(fin), istreambuf_iterator();tmp.swap(cache);curPos = 0;char getChar()if(curPos cache.size()return cachecurPos+;else return 0;void putBack()curPos-;Element getWord(char a)string str(1,a);while(a = getChar()if(isalpha(a) | isdigit(a) | a = _)str+=a;elseputBack();break;return Element(str,ID);Element getNum(char a)string str(1,a);while(a = getChar()if(isdigit(a)str +=a;elseputBack();break;/=w=return Element(str,NUM);void wordAnalysis()vector v;char ch;while(ch = getChar()if(ch = + |ch = - |ch = * |ch = / |ch = ( |ch = ) )v.push_back(Element(string(1,ch),string(1,ch);else if(isalpha(ch) | ch = _)v.push_back(getWord(ch);else if(isdigit(ch)v.push_back(getNum(ch);v.push_back(Element(ENDofFILE,ENDofFILE);for(int i = 0;iv.size();i+),vi.kindendl;cout=wordAnalysis END=endl;v.swap(vecWord);bool is_syntaxAnalysis_succeed = true;void error(int ip,int k)coutk mistake ocurred at ip T E,E - + T E,E - - T E,E - NULL,T - F T,T - * F T,T - / F T,T - NULL,F - id,F - ( E ),;vector viaString = ,/0T,E1,/1ADD,T,E1,/2SUB,T,E1,/3NOTHING,/4F,T1,/5MUL,F,T1,/6DIV,F,T1,/7NOTHING,/8ID,/9LEFTK,E,RIGHTK,/10;vector testViaString = ,/0Element(T,NOTEND),Element(E1,NOTEND),/1Element(ADD,ADD),Element(T,NOTEND),Element(E1,NOTEND),/2Element(SUB,SUB),Element(T,NOTEND),Element(E1,NOTEND),/3Element(NOTHING,NOTHING),/4Element(F,NOTEND),Element(T1,NOTEND),/5Element(MUL,MUL),Element(F,NOTEND),Element(T1,NOTEND),/6Element(DIV,DIV),Element(F,NOTEND),Element(T1,NOTEND),/7Element(NOTHING,NOTHING),/8Element(ID,ID),/9Element(LEFTK,LEFTK),Element(E,NOTEND),Element(RIGHTK,RIGHTK),/10;mapstring,mapstring,vector mp;mpEID = testViaString1;mpELEFTK= testViaString1;mpE1ADD = testViaString2;mpE1SUB = testViaString3;mpE1RIGHTK = testViaString4;mpE1ENDofFILE = testViaString4;mpTID = testViaString5;mpTLEFTK = testViaString5;mpT1ADD = testViaString8;mpT1SUB = testViaString8;mpT1MUL = testViaString6;mpT1DIV = testViaString7;mpT1RIGHTK = testViaString8;mpT1ENDofFILE = testViaString8;mpFID = testViaString9;mpFLEFTK = testViaString10;int ip = 0;Element tmpX(E,NOTEND);stack stk;stk.push(tmpX);while(stk.empty()=false)tmpX = stk.top();/coutTMPX tmpX.name,tmpX.kindendl;stk.pop();if (tmpX.kind = ENDofFILE);else if(tmpX.kind != NOTEND )if(tmpX.kind = vecWordip.kind)coutmatch(tmpX.name:vecW )endl;ip+;else error(ip,0);break;else if(mp tmpX.name vecWordip.kind .empty() /kind ?error(ip,1);break;else auto &vec = mp tmpX.name vecWordip.kind ;couttmpX.name ;for(int i = 0;ivec.size();i+);cout=0;i-)/coutadd ;if(vec
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年事业单位工勤技能-河南-河南动物检疫员四级(中级工)历年参考题库典型考点含答案解析
- 2024版废品回收合同模板
- 2025年事业单位工勤技能-河北-河北下水道养护工三级(高级工)历年参考题库含答案解析(5套)
- 2025年事业单位工勤技能-江苏-江苏防疫员五级(初级工)历年参考题库含答案解析(5套)
- 2025年事业单位工勤技能-江苏-江苏收银员三级(高级工)历年参考题库含答案解析(5套)
- 2025年事业单位工勤技能-广西-广西汽车驾驶与维修员三级(高级工)历年参考题库典型考点含答案解析
- 2025年事业单位工勤技能-广东-广东食品检验工五级(初级工)历年参考题库含答案解析
- 2025年事业单位工勤技能-广东-广东热处理工四级(中级工)历年参考题库典型考点含答案解析
- 烹调基础知识培训课件
- 2025年驾驶证考试-客车理论考试-客车驾驶证(科目四)历年参考题库含答案解析(5套)
- CJT487-2015 城镇供热管道用焊制套筒补偿器
- 抖音火花合同电子版获取教程
- 优化方案语文必修上册
- 云南省大中型水电站情况表
- 建筑太阳能光伏发电应用技术标准 DG-TJ08-2004B-2020
- 河北传统医学师承关系合同书
- 园林制图(高职)全套教学课件
- 深圳航空公司招聘笔试真题
- 五年级(上册)道德与法治全册教案
- 2023《医疗质量控制中心管理规定》全面解读
- 人员设备资金等方面具有相应的施工能力承诺书
评论
0/150
提交评论