




下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、大连理工大学软件学院编译技术课程一一词法分析上机实验注意:需要建立test文档,文档内为输入实验目的:对循环语句和条件判断语句编写词法分析编译程序,只能通过一遍扫描完成。仙C+实现)实验要求:关键字:for if the n else while do所有关键字都是小写。(2)运算符和分隔符:: = + - * / < > <= <> >= ; ( ) #其他标识符(ID)和整型常数(NUM),通过以下正规式定义:ID=letter(letter | digit)*NUM=digit digit*空格由空白、制表符和换行符组成。空格一般用来分隔ID、NUM、
2、运算符、分隔符和关键字,词法分析阶段通常被忽略。各种词法单元对应的词法记号如下:词法单元词法记号词法单元词法记号for1:17if2:=18the n3<20else4<>21while5<=22do6>23letter(letter+digit)*10>=24digit digit*11=25+13J26-14(27*15)28/16#0词法分析程序的功能输入:源程序输出:二元组(词法记号,属性值/其在符号表中的位置)构成的序列例如:对源程序x:=5; if (x>0) the n x:=2*x+1/3; else x:=2/x;#经词法分析后输出如
3、下序列:(10, '')(18, :=) (11,5) (26, ;) (2, if )(27,()1.几点说明:(1)关键字表的初值。关键字作为特殊标识符处理,把它们预先安排在一张表格中(称为关键字 表),当扫描程序识别出标识符,查关键字表。如能查到匹配的单词,则该 单词的关键字, 否则为一般标识符。 关键表为一个字符串数组, 其描述如下: char *keyword6= ”for ”, ”if”, ”then ”,”else ”,w”hile ”, ”do”;(2) 程序中需要用到的主要变量为 token , id 和 num.1) id 用来存放构成词法单元的字符串;2)
4、 num 用来存放整数(可以扩展到浮点数和科学计数法表示) ;3) token 用来存放词法单元的词法记号。可以参考下面的代码:dolexical(); / 将词法单元对应的记号保存到 token 中,属性值保存到 num 或者 id 中switch(token) case 11: printf ("(token, %dn) ", num); break;case -1: printf("error!n");break;default: printf("(%d,%s)n", token, id);while (token!=0);#i
5、nclude<iostream>#include<string>#include<fstream>#include<queue>#include<cstdlib>using namespace std;string keyword6="for","if","then","else","while","do"bool isLETTER(char temp)if(temp>='a' &&am
6、p; temp<='z')return true;elsereturn false;bool isDIGIT(char temp)if(temp>='0' && temp<='9')return true;elsereturn false;bool isID(string temp)bool flag=true;if(!isLETTER(temp0)flag=false;for(int i=1;i<temp.length() && flag;i+)if(!(isLETTER(tempi) |
7、 isDIGIT(tempi)flag=false;return flag;bool isNUM(string temp)bool flag=true;for(int i=0;i<temp.length() && flag;i+)if(!(tempi>='0' && tempi<='9')flag=false;return flag;void getkey(string temp,int & key)if(temp="for")key=1;else if(temp="if&q
8、uot;)key=2;else if(temp="then")key=3;else if(temp="else")key=4;else if(temp="while") key=5;else if(temp="do")key=6;else if(isID(temp)key=10;else if(isNUM(temp)key=11;else if(temp="+")key=13;else if(temp="-")key=14;else if(temp="*"
9、)key=15;else if(temp="/")key=16;else if(temp=":")key=17;else if(temp=":=")key=18;else if(temp="<")key=20;else if(temp="<>")key=21;else if(temp="<=")key=22;else if(temp=">")key=23;else if(temp=">=")key=2
10、4;else if(temp="=")key=25;else if(temp="")key=26;else if(temp="(")key=27;else if(temp=")")key=28;else if(temp="#")key=0;else key=-1;int main()string temp;char a;int key;queue<string> aqueue;char *array=new char20; bool flag=true;ifstream file;
11、file.open("test.txt");if(!file)cout<<"can't open 'test.txt' "<<endl;exit(1);a=file.get();while(!file.eof() )while(a=' ')a=file.get();flag=true;int i=0;arrayi+=a;while(isDIGIT(a)a=file.get();if(isDIGIT(a)arrayi+=a;elseflag=false;if(isLETTER(a) &
12、& flag)while(isLETTER(a)|isDIGIT(a)a=file.get();if(isLETTER(a)|isDIGIT(a)arrayi+=a;elseflag=false;if(flag)switch(a)case ':' :a=file.get();if(a='=')arrayi+=a;a=file.get();break; case '<' : a=file.get();if(a='>' | a='=') arrayi+=a;a=file.get(); break;c
13、ase '>' : a=file.get();if(a='=')arrayi+=a;a=file.get();break;default : a=file.get(); break;string t(array,i);aqueue.push(t);while(!aqueue.empty()temp=aqueue.front();aqueue.pop();getkey(temp,key);switch(key)case -1: cout<<"error!"<<endl; break;/case 0: cout<<"("<<
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年测电流考试题目及答案
- 教师招聘之《小学教师招聘》押题模拟(预热题)附答案详解
- 初中成语竞赛试题及答案
- 中医知识竞赛试题及答案
- 教师招聘之《小学教师招聘》测试卷含答案详解【巩固】
- 2025年口罩佩戴试题及答案
- 讲文懂礼貌的课件
- 2025年节能协会考试题及答案
- 海员安全工作规范培训课件
- 2025内蒙古呼伦贝尔林业集团有限公司招聘工作人员5人备考及一套参考答案详解
- 企业燃气充值管理办法
- 食材库房防霉防潮卫生管理措施
- 腹透患者的饮食管理
- 人工智能辅助药物服务企业制定与实施新质生产力项目商业计划书
- 门诊部护理质控
- 财务部实习生聘用合同模板
- 2025至2030全球及中国人工智能招聘行业发展趋势分析与未来投资战略咨询研究报告
- 基金从业资格-私募股权投资基金基础知识讲义
- 社会法课件教学课件
- 麻醉恢复室患者的安全转运
- 2025年福建省中考语文试卷真题(含标准答案)
评论
0/150
提交评论