2023年北邮大三上编译原理词法分析实验报告

上传人:豆*** 文档编号:165969723 上传时间:2022-10-30 格式:DOC 页数:18 大小:93.50KB
收藏 版权申诉 举报 下载
2023年北邮大三上编译原理词法分析实验报告_第1页
第1页 / 共18页
2023年北邮大三上编译原理词法分析实验报告_第2页
第2页 / 共18页
2023年北邮大三上编译原理词法分析实验报告_第3页
第3页 / 共18页
资源描述:

《2023年北邮大三上编译原理词法分析实验报告》由会员分享,可在线阅读,更多相关《2023年北邮大三上编译原理词法分析实验报告(18页珍藏版)》请在装配图网上搜索。

1、编译原理第三章 词法分析班级:学号:姓名:schnee目 录1.实验题目和规定32.检测代码分析33.源代码. 41. 实验题目和规定题目:词法分析程序的设计与实现。实验内容:设计并实现C语言的词法分析程序,规定如下。(1)、可以辨认出用C语言编写的源程序中的每个单词符号,并以记号的形式输出每个单词符号。(2)、可以辨认并读取源程序中的注释。(3)、可以记录源程序汇总的语句行数、单词个数和字符个数,其中标点和空格不计算为单词,并输出记录结果(4)、检查源程序中存在的错误,并可以报告错误所在的行列位置。(5)、发现源程序中存在的错误后,进行适当的恢复,使词法分析可以继续进行,通过一次词法分析解决

2、,可以检查并报告源程序中存在的所有错误。实验规定:方法1:采用C/C+作为实现语言,手工编写词法分析程序。方法2:通过编写LEX源程序,运用LEX软件工具自动生成词法分析程序。2. 检测代码分析1、 Hello World简朴程序输入:2、 较复杂程序输入:3. 异常程序输入检测三, 源代码#include #include #include #include #include #include #include #include #include #include using namespace std;const int FILENAME=105;const int MAXBUF=82;c

3、onst int L_END=40;const int R_END=81;const int START=0; /开始指针vector Key; /C保存的关键字表class funtion /词法分析结构public:/变量声明char filenameFILENAME; /需要词法分析的代码文献名ifstream f_in;char bufferMAXBUF; /输入缓冲区int l_end, r_end, forward; /左半区终点,右半区终点,前进指针,bool l_has, r_has; /辅助标记位,表达是否已经填充过缓冲区vector Id; /标记符表char C; /当前

4、读入的字符int linenum, wordnum, charnum; /行数,单词数,字符数string curword; /存放当前的字符串/函数声明void get_char(); /从输入缓冲区读一个字符,放入C中,forward指向下一个void get_nbc(); /检查当前字符是否为空字符,反复调用直到非空void retract(); /向前指针后退一位void initial(); /初始化要词法分析的文献void fillBuffer(int pos); /填充缓冲区,0表达左,1表达右void analyzer(); /词法分析void token_table(); /

5、以记号的形式输出每个单词符号void note_print(); /辨认并读取源程序中的注释void count_number(); /记录源程序汇总的语句行数、单词个数和字符个数void error_report(); /检查并报告源程序中存在的所有错误void solve(char* file); /主调用函数;void welcome()printf(n*n);printf( * Welcome to use LexicalAnalyzer *n);printf( * By schnee BUPT Date: 2023/20/10 *n);printf( *nnn);void initK

6、ey()Key.clear();Key.push_back(auto); Key.push_back(break); Key.push_back(case); Key.push_back(char); Key.push_back(const); Key.push_back(continue);Key.push_back(default); Key.push_back(do); Key.push_back(double); Key.push_back(else); Key.push_back(enum); Key.push_back(extern); Key.push_back(float);

7、Key.push_back(for); Key.push_back(goto); Key.push_back(if); Key.push_back(int); Key.push_back(long); Key.push_back(register);Key.push_back(return); Key.push_back(short); Key.push_back(signed); Key.push_back(static); Key.push_back(sizeof); Key.push_back(struct); Key.push_back(switch); Key.push_back(t

8、ypedef); Key.push_back(union); Key.push_back(unsigned);Key.push_back(void); Key.push_back(volatile);Key.push_back(while);void funtion:get_char()C=bufferforward;if(C=EOF)return ; /结束if(C=n)linenum+; /记录行数和字符数else if(isalnum(C) charnum+;forward+;if(bufferforward=EOF)if(forward=l_end)fillBuffer(1);forw

9、ard+;else if(forward=r_end)fillBuffer(0);forward=START;void funtion:get_nbc()while(C= | C=n | C=t | C=0)get_char();void funtion:initial(char* file)Id.clear(); /清空标记符表l_end=L_END;r_end=R_END; /初始化缓冲区forward=0;l_has=r_has=false;bufferl_end=bufferr_end=EOF;fillBuffer(0);linenum=wordnum=charnum=0; /初始化行

10、数,单词数,字符数void funtion:fillBuffer(int pos)if(pos=0)/填充缓冲区的左半边if(l_has=false)fin.read(buffer, l_end);if(fin.gcount()!=l_end)bufferfin.gcount()=EOF;else l_has=false;else /填充缓冲区的右半边if(r_has=false)fin.read(buffer+l_end+1, l_end);if(fin.gcount()!=l_end)bufferfin.gcount()+l_end+1=EOF;else r_has=false;void

11、funtion:retract()if(forward=0)l_has=true; /表达已经读取过文献,避免下次再次读取forward=l_end-1;elseforward-;if(forward=l_end)r_add=true;forward-;void funtion:analyzer()FILE *token_file, *note_file, *count_file, *error_file;token_file=fopen(token_file.txt, w);note_file=fopen(note_file.txt, w);count_file=fopen(count_fi

12、le.txt, w);error_file=fopen(error_file.txt, w);int i;curword.clear();get_char();get_nbc();if(C=EOF)return false;if(isalpha(C) | C=_)/关键字和标记符的解决,以字母或下划线开头curword.clear();while(isalnum(C) | C=_)curword.push_back(C);get_char();retract();wordnum+;Id.push_back(curword);for(i=0; iKey.size(); i+)if(Keyi=cu

13、rword)break;/输出每一个单词的标记符if(iKey.size() /关键字fprintf(token_file, %8d-%20s %sn, wordnum, KEY WORD, curword);elsefprintf(token_file, %8d-%20s %sn, wordnum, Identifier, curword);else if(isdigit(C)/无符号数的解决curword.clear();while(isdigit(C)curword.push_back(C);get_char();if(C=. | C=E | C=e)/解决小数和指数形式curword.

14、push_back(C);get_char();while(isdigit()curword.push_back(C);get_char();retract();wordnum+;Id.push_back(curword);fprintf(token_file, %8d-%20s %sn, wordnum, Unsigned Number, curword);else if(C=#)/过滤掉以#开头的预解决fprintf(note_file, preproccess Line %d : , linenum);get_char();fprintf(note_file, %c, C);while(

15、C!=n)get_char(); fprintf(note_file, %c, C);fprintf(note_file, %c, C);else if(C=)/内的句子当成整个串保存起来curword.clear();get_char();while(C!=)curword.push_back(C);get_char();fprintf(token_file, *string in -%sn, curword);else if(C=/)get_char();if(C=/)/过滤掉/开头的行注释fprintf(note_file, single-line note Line %d : , li

16、nenum);get_char();curword.clear();while(C!=n)curword.push_back(C);get_char();fprintf(note_file, %sn, curword);else if(C=*)/过滤掉/*/之间的段注释fprintf(note_file, paragraph note Line %d : , linenum);get_char();while(true)while(C!=/) fprintf(note_file, %c, C); get_char();get_char();if(C=*)fprintf(note_file, n

17、to Line %dn, linenum);break;fprintf(note_file, %c, C);else if(C=)fprintf(token_file, *ASSIGN-OP, DIVn);elsefprintf(token_file, *CAL-OP, DIVn);retract(); /解决各种比较,赋值,运算符号else if(C=)get_char();if(C=)fprintf(token_file, *RELOP, GEn);elsefprintf(token_file, *RELOP, GTn);retract();else if(C=)get_char();if

18、(C=)fprintf(token_file, *RELOP, EQn);elsefprintf(token_file, *ASSIGN-OP, EASYn);retract();else if(C=+)get_char();if(C=)fprintf(token_file, *ASSIGN-OP, ADDn);elsefprintf(token_file, *CAL-OP, ADDn);retract();else if(C=-)get_char();if(C=)fprintf(token_file, *ASSIGN-OP, SUBn);elsefprintf(token_file, *CA

19、L-OP, SUBn);retract();else if(C=*)get_char();if(C=)fprintf(token_file, *ASSIGN-OP, MULn);elsefprintf(token_file, *CAL-OP, MULn);retract();else if(C=!)get_char();if(C=)fprintf(token_file, *RELOP, UEn);else if(!isalpha(C) & C!=_)fprintf(error_file, Line %d: error: ! was illegal char n, linenum);else i

20、f(C=: | C=( | C=) | C=; | C= | C= | C=,)fprintf(token_file, *Other char-%cn, C);elsefprintf(error_file, Line %d: error: %c was illegal char n, linenum, C);fprintf(count_file, The Line number is %dn, linenum);fprintf(count_file, The word number is %dn, wordnum);fprintf(count_file, The char number is

21、%dn, charnum);fclose(token_file);fclose(note_file);fclose(count_file);fclose(error_file);void funtion:token_table()fin.open(token_file.txt);printf(The token_table is as following:n);char str1;while(1)fin.read(str, 1);if(str0!=EOF)printf(%c, str0);void funtion:note_print()fin.open(note_file.txt);prin

22、tf(The note is as following:n);char str1;while(1)fin.read(str, 1);if(str0!=EOF)printf(%c, str0);void funtion:count_number()fin.open(count_file.txt);printf(The count result is as following:n);char str1;while(1)fin.read(str, 1);if(str0!=EOF)printf(%c, str0);void funtion:error_report()fin.open(error_fi

23、le.txt);printf(The error report is as following:n);char str1;while(1)fin.read(str, 1);if(str0!=EOF)printf(%c, str0);void funtion:solve(char* file)filename=file;fin.open(filename);intitial();analyzer();int choice;printf(* We have analyzed %s n);printf(*0: To endn);printf(*1: To get the token tablen);

24、printf(*2: To get the note part of filen);printf(*4: To report all the error of the filen);printf(*3: To get the line num, word num and charter numnn);while(1)printf(*please input your choice: );scanf(%d, &choice);if(choice=0)break;if(choice=1)token_table();else if(choice=2)note_print();else if(choi

25、ce=3)count_number();else error_report();printf(n);void LexicalAnaylzer(char* file)funtion test;test.solve(file);int main()welcome();initKey();char fileFILENAME;while(1)printf(nDo you want to continue? (YES or NO): );scanf(%s, file);if(strcmp(file, NO)=0)printf(Thanks for your use! GoodBye next time!nn);break;printf(Please type your C file name(for example: a.cpp): );scanf(%s, file);LexicalAnalyzer(file);return 0;

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

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!