




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、编译原理第三章词法分析班级:2009211311 学号姓名:schnee目录1 .实验题目和要求 32 .检测代码分析3.43 .源代码1 .实验题目和要求题目:词法分析程序的设计与实现。实验内容:设计并实现 C语言的词法分析程序,要求如下。(1)、可以识别出用 C语言编写的源程序中的每个单词符号,并以记号的形式输出每个单词符号。(2)、可以识别并读取源程序中的注释。(3)、可以统计源程序汇总的语句行数、单词个数和字符个数,其中标点和空格不计算为单词,并输出统计结果(4)、检查源程序中存在的错误,并可以报告错误所在的行列位置。(5)、发现源程序中存在的错误后,进行适当的恢复,使词法分析可以继续
2、进行,通过一次词法分析处理, 可以检查并报告源程序中存在的所有错误。实验要求:方法1 :采用C/C+作为实现语言,手工编写词法分析程序。方法2:通过编写LEX源程序,利用LEX软件工具自动生成词法分析程序。2 .检测代码分析1、H ello World 简单程序输入:CoiTipiler.cpp a.cpp x#irLclude<cstdio>firLclude-ciostreain>- frincrlude<cstdl£b>#irLcliide<c3l:rirLg>using name space 3dtei; int ma i n()pr
3、intf CrtUello Woxld 1 nTi);return 0;2、较复杂程序输入:Compilaf.cpp a.cpp x#included cstdio> 4 include<io9tieani> T i TLclude- cstcili kr- 4 include<c3tring> us Ing nairuespace std; 11n.七 main ()B(1 口七日/ b;运算符检差 a = 10; b = 1; b +;a.;a : intId b +; a /= b; a '+= 1; a -= 1; while ia 1=() /注
4、释检盍/f J.sfj osidfn Isnf/sofn*/1 printf (11 He Ila Warldl n");re-t-u.ru 二;3.异常程序输入检测Ccmpiler.cpp a.cppfr£rLclade<cstd£o>#inGlude<iG>5tKeanL>#include<c3tdlib>ini main()日 ,- 5t a;printf ("Hello World! n,T);S*return Q;三,源代码#include <cmath> #include <ccty
5、pe>#include <string>#include <vector>#include <cstdio>#include <cstdlib>#include <cstring>#include <fstream>#include <iostream>#include <algorithm>using namespace std;const int FILENAME=105;const int MAXBUF=82;const int L_END=40;const int R_END=81;c
6、onst int START=0;vector<string> Key;class funtionpublic:/ 变量声明char filenameFILENAME;ifstream f_in;char bufferMAXBUF;int l_end, r_end, forward;bool l_has, r_has;/ 开始指针/C 保留的关键字表/ 词法分析结构/ 需要词法分析的代码文件名/ 输入缓冲区/ 左半区终点,右半区终点,前进指针,/ 辅助标记位,表示是否已经填充过缓冲区vector<string> Id;/ 标识符表char C;/ 当前读入的字符/ 行数
7、,单词数,字符数/ 存放当前的字符串int linenum, wordnum, charnum;string curword;/ 函数声明void get_char();void get_nbc();void retract();void initial();void fillBuffer(int pos);void analyzer();void token_table();void note_print();void count_number();void error_report();void solve(char* file);/ 从输入缓冲区读一个字符,放入C 中, forward
8、指向下一个/ 检查当前字符是否为空字符,反复调用直到非空/ 向前指针后退一位/ 初始化要词法分析的文件/ 填充缓冲区,0 表示左 ,1 表示右/ 词法分析/ 以记号的形式输出每个单词符号/ 识别并读取源程序中的注释/ 统计源程序汇总的语句行数、单词个数和字符个数/ 检查并报告源程序中存在的所有错误/ 主调用函数void welcome()printf("n*n");*n");printf( "*Welcome to use LexicalAnalyzer*n");printf(”*nnn");void initKey()Key.cle
9、ar();Key.push_back("auto");Key.push_back("char");Key.push_back("const");Key.push_back("do");Key.push_back("break");Key.push_back("case");Key.push_back("continue");Key.push_back("default");printf( "*By schnee BUPTDat
10、e: 2011/20/10Key.push_back("double");Key.push_back("else");Key.push_back("enum");Key.push_back("extern");Key.push_back("float");Key.push_back("if");Key.push_back("int");Key.push_back("for");Key.push_back("goto"
11、);Key.push_back("long");Key.push_back("register");Key.push_back("return");Key.push_back("static");Key.push_back("short");Key.push_back("signed");Key.push_back("sizeof");Key.push_back("typedef");Key.push_back("struct
12、");Key.push_back("switch");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+;/
13、 统计行数和字符数else if(isalnum(C) charnum+;forward+;if(bufferforward=EOF)if(forward=l_end)fillBuffer(1);forward+;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
14、();/ 清空标识符表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;/ 初始化行数,单词数,字符数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_ha
15、s=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 funtion:retract()if(forward=0)l_has=true;/ 表示已经读取过文件,避免下次再次读取forward=l_end-1;elseforward-;if(forward=l_end)r_add=true;forward-;void funtion:analyzer()FIL
16、E *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_file.txt", "w");error_file=fopen("error_file.txt", "w");int i;curw
17、ord.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; i<Key.size(); i+)if(Keyi=curword)break;/ 输出每一个单词的标识符if(i<
18、;Key.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='.' |
19、 C='E' | C='e')/处理小数和指数形式curword.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_
20、file, "preproccessLine %d : ", linenum);get_char();fprintf(note_file, "%c", C);while(C!='n')get_char();fprintf(note_file, "%c", C);fprintf(note_file, "%c", C);else if(C='"')/"" 内的句子当成整个串保存起来curword.clear();get_char();while(C!=
21、9;"')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 noteLine %d : ", linenum);get_char();curword.clear();while(C!='n')curwo
22、rd.push_back(C);get_char();fprintf(note_file, "%sn", curword);else if(C='*')/ 过滤掉 /*/ 之间的段注释fprintf(note_file, "paragraph noteLine %d : ", linenum);get_char();while(true)while(C!='/')fprintf(note_file, "%c", C);get_char();get_char();if(C='*')fpri
23、ntf(note_file, "nto 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='=')fp
24、rintf(token_file, "*RELOP, LEn");elsefprintf(token_file, "*RELOP, LTn");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(C=
25、39;=')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='-
26、39;)get_char();if(C='=')fprintf(token_file, "*ASSIGN-OP, SUBn"); elsefprintf(token_file, "*CAL-OP, SUBn");retract();else if(C='*')get_char();if(C='=')fprintf(token_file, "*ASSIGN-OP, MULn"); elsefprintf(token_file, "*CAL-OP, MULn");retr
27、act();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 if(C=':' | C='(' | C=')' | C='
28、9; | 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 numbe
29、r is %dn", wordnum);fprintf(count_file, "The char number is %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)
30、fin.read(str, 1);if(str0!=EOF)printf("%c", str0);void funtion:note_print()fin.open("note_file.txt");printf("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
31、");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_file.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
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2030中国海洋工程用钢行业发展分析及发展前景与趋势预测研究报告
- 2025-2030中国浴室浓缩清洁剂行业市场发展分析及竞争格局与投资前景研究报告
- 2025-2030中国浓缩蛋白饲料行业市场现状供需分析及投资评估规划分析研究报告
- 2025-2030中国测试、检验和认证(TIC)行业市场现状供需分析及投资评估规划分析研究报告
- 2025-2030中国泰妙菌素原料药行业供需状况与发展趋势研究研究报告
- 2025-2030中国油烟机行业发展分析及投资前景预测研究报告
- 2025-2030中国汽车零部件修复服务行业市场发展趋势与前景展望战略研究报告
- 2025-2030中国汽车钢管市场竞争风险及投资运作模式分析研究报告
- 2025-2030中国汽车起重机吊臂行业市场发展分析及竞争格局与投资前景研究报告
- 2025-2030中国汽车燃油输送泵行业市场发展趋势与前景展望战略分析研究报告
- 医院培训课件:《白疕(银屑病)中医护理查房》
- 一汽-大众供应商管理流程介绍.sbx
- 招标代理机构入围 投标方案(技术方案)
- 招投标代理挂靠协议书
- 工作的时效性与时间管理课件
- 年产10万吨聚氯乙烯生产工艺设计毕业设计
- 高中18岁成人仪式主题活动设计
- 《婚姻家庭纠纷调解》课件
- 高中数学培优讲义练习(必修二):专题8.1 基本立体图形(重难点题型精讲)(教师版)
- 兵团红色经典文化在新疆高校思想政治教育中的运用研究
- 注塑机定期保养记录表2016
评论
0/150
提交评论