大工软院编译上机1词法分析概要

上传人:今*** 文档编号:108101212 上传时间:2019-10-22 格式:DOC 页数:7 大小:45.50KB
返回 下载 相关 举报
大工软院编译上机1词法分析概要_第1页
第1页 / 共7页
大工软院编译上机1词法分析概要_第2页
第2页 / 共7页
大工软院编译上机1词法分析概要_第3页
第3页 / 共7页
大工软院编译上机1词法分析概要_第4页
第4页 / 共7页
大工软院编译上机1词法分析概要_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《大工软院编译上机1词法分析概要》由会员分享,可在线阅读,更多相关《大工软院编译上机1词法分析概要(7页珍藏版)》请在金锄头文库上搜索。

1、大连理工大学软件学院编译技术课程词法分析上机实验/注意:需要建立test文档,文档内为输入实验目的:对循环语句和条件判断语句编写词法分析编译程序,只能通过一遍扫描完成。(用c+实现)实验要求:(1) 关键字:for if then else while do所有关键字都是小写。(2)运算符和分隔符: : = + - * / = = ; ( ) #(3)其他标识符(ID)和整型常数(NUM),通过以下正规式定义:ID=letter(letter | digit)*NUM=digit digit*(4)空格由空白、制表符和换行符组成。空格一般用来分隔ID、NUM、运算符、分隔符和关键字,词法分析阶

2、段通常被忽略。各种词法单元对应的词法记号如下:词法单元词法记号词法单元词法记号for1:17if2:=18then320else421while523letter(letter+digit)*10=24digit digit*11=25+13;26-14(27*15)28/16#0词法分析程序的功能输入:源程序输出:二元组(词法记号,属性值/其在符号表中的位置)构成的序列。例如:对源程序x:=5; if (x0) then x:=2*x+1/3; else x:=2/x; #经词法分析后输出如下序列:(10,x)(18, :=) (11,5) (26, ;) (2, if ) (27,( )1

3、.几点说明:(1)关键字表的初值。关键字作为特殊标识符处理,把它们预先安排在一张表格中(称为关键字表),当扫描程序识别出标识符,查关键字表。如能查到匹配的单词,则该单词的关键字,否则为一般标识符。关键表为一个字符串数组,其描述如下:char *keyword6=”for”, ”if”, ”then” ,”else”,”while”, ”do” ;(2) 程序中需要用到的主要变量为 token , id和num.1)id用来存放构成词法单元的字符串;2)num用来存放整数(可以扩展到浮点数和科学计数法表示);3)token用来存放词法单元的词法记号。可以参考下面的代码:do lexical();

4、 /将词法单元对应的记号保存到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); #include#include#include#include#includeusing namespace std;string keyword6=for,if,then,else,while,do;bool isLETTER(cha

5、r temp)if(temp=a & temp=0 & temp=9) return true;elsereturn false;bool isID(string temp)bool flag=true;if(!isLETTER(temp0)flag=false;for(int i=1;itemp.length() & flag;i+)if(!(isLETTER(tempi) | isDIGIT(tempi)flag=false;return flag;bool isNUM(string temp)bool flag=true;for(int i=0;i=0 & tempi=9)flag=fa

6、lse;return flag;void getkey(string temp,int & key) if(temp=for) key=1;else if(temp=if) 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(t

7、emp=*) 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=23;else if(temp=) key=24;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

8、main()string temp;char a;int key;queue aqueue;char *array=new char20;bool flag=true; ifstream file;file.open(test.txt);if(!file)coutcant 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+

9、=a;elseflag=false;if(isLETTER(a) & 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=) arrayi+=a; a=file.get(); break; case : 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: couterror!endl; break;/case 0: cout(key,temp)endl; break;default: cout(key,temp)endl; break;return 0;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

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