资源预览内容
第1页 / 共15页
第2页 / 共15页
第3页 / 共15页
第4页 / 共15页
第5页 / 共15页
第6页 / 共15页
第7页 / 共15页
第8页 / 共15页
第9页 / 共15页
第10页 / 共15页
亲,该文档总共15页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
编译原理实验学号:姓名: 班级:实验一:文件读入缓冲区,每次单字符输出源码:#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 st
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号