编译原理词法分析语法分析

上传人:pu****.1 文档编号:467750780 上传时间:2023-06-19 格式:DOC 页数:15 大小:70.51KB
返回 下载 相关 举报
编译原理词法分析语法分析_第1页
第1页 / 共15页
编译原理词法分析语法分析_第2页
第2页 / 共15页
编译原理词法分析语法分析_第3页
第3页 / 共15页
编译原理词法分析语法分析_第4页
第4页 / 共15页
编译原理词法分析语法分析_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《编译原理词法分析语法分析》由会员分享,可在线阅读,更多相关《编译原理词法分析语法分析(15页珍藏版)》请在金锄头文库上搜索。

1、编译原理实验学号:姓名: 班级:实验一:文件读入缓冲区,每次单字符输出源码:#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 g

2、etChar()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

3、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 na

4、me 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();

5、/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

6、.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

7、 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 = OPERA

8、TOR;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

9、(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) tem

10、p.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

11、 = 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

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 建筑资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号