Tiny语言的词法分析器.docx
- 文档编号:9006856
- 上传时间:2023-05-16
- 格式:DOCX
- 页数:13
- 大小:29.77KB
Tiny语言的词法分析器.docx
《Tiny语言的词法分析器.docx》由会员分享,可在线阅读,更多相关《Tiny语言的词法分析器.docx(13页珍藏版)》请在冰点文库上搜索。
Tiny语言的词法分析器
课程名称
编译原理课程设计
实验课时
8
实验
项目
手工构造Tiny语言的词法分析器
实验时间
7-10周
实验
熟悉Tiny语言词法;构造DFA设计数据类型、数据结构;用
C++实现Tiny语
目的
言的词法分析器
实验
Windows10专业版
环境
MicrosoftVisualStudio2013
一、Tiny语言记号
Reservedwords
SpecialSymbolsOther
if
+
实验
then
-
内容
else
*
number(1ormoredigits)
(算
end
<
法、
repeat
=
程
until
/
序、
read
()
5
步骤
write
identifier(1ormore
和方
letters)
法)
二、构造Tiny语言DFA
ID:
letter(letter)*
Number:
digit(digit)*
三、根据DFA编写词法分析器
#include
#include
#include
usingnamespacestd;
staticintrowCounter=1;//静态变量,用于存储行数
staticboolbracketExist=false;//判断注释存在与否,false为不存在
classLex
ofstreamoutput;
stringline=
Lex(stringinputLine)
line=inputLine
scan(Trim(line));rowCounter++;
}
stringTrim(string&str)//函数用于去除每行前后空格
ints=("\t");inte=(“\t");
str=(s,e-s+1);str+="\0";returnstr;
}
voidscan(stringinputLine)
{
ofstreamoutput;
("",ios:
:
app);
stringline=inputLine;
inti=0;
stringstr="";
inttemp;
stringtoken="“;
output< "< while(line[i]! ='\0')//根据DFAm描并判断 { if(line[i]=='{')//注释 { bracketExist=true; } if(bracketExist==true) { output<<"\t"< "; while(line[i]! ='}') { output< //不处理,直接输出 if (line[i+1]! = NULL { i++; } else break; } if (line[i]=='}' )//注释结束 { output< bracketExist=false } } 1 if (bracketExist== false) { //数字 while(isdigit(line[i])){ temp=temp*10+line[i]; if(! isdigit(line[i+1])) { output<<"\t"< "<<"NUM,va匸"< -'O'< }] if(line[i+1]! =NULL&isdigit(line[i+1])) i++; else break; } temp=0; //符号 while(! (isdigit(line[i])||(line[i]>='a'&&line[i]<='z')|| (line[i]>='A' &&line[i]<='Z')||line[i]=='' ||line[i]== '{'||line[i]== '}')) { token=token+line[i]; if(isdigit(line[i+1])||(line[i+1] >= 'a'&&line[i+1]<= 'z') ||(line[i+1]>= 'A'&&line[i+1]<='Z')||line[i+ 1]==' '||line[i+1]== {'II line[i+1]== '}'||line[i+1]==NULL { if(isToken(token)) while(token[j]! ='\0') { output<<"\t"< "< endl; j++; } } }] else { i++; continue; } if(line[i+1]! =NULL i++; else break; } token=""; //字母 while((line[i]>='a'&&line[i]<='z')||(line[i]>='A'&&line[i]<= 'Z')) { str=str+line[i]; if(! ((line[i+1]>='a'&&line[i+1]<='z')||(line[i+1]>= 'A'&&line[i+1]<='Z'))) { if(isResearvedWord(str))//判断是否是保留字 { output<<"\t"< "<<"ReversedWord: "< break; ": "<<"ID,name="<< output<<"\t"< str< break; }j }| if(line[i+1]! =NULLi++; } str=""; if(line[i+1]! =NULL { i++; } else break; if(line[i]==';') output<<"\t"< "< break; } } //清空,以备下一行读取 line="";str=""; temp=0; token=""; output< (); } boolisResearvedWord(strings)〃存储保留字,并判断 { stringreservedWord[8]={"if","then","else","end","repeat","until","read", "write"}; booljudge=false; for(inti=0;i<8;i++) { if(s==reservedWord[i]) { judge=true; break; } } return judge; 二} boolisToken(strings)〃存储符号,并判断 { stringtoken[10]={"+","-","*","/","=","<","(",")",";",": ="}; booljudge=false; for(inti=0;i<10;i++) { if(s==token[i]) { judge=true; break; } } return judge; } }; intmain() { ifstream input; ("");] string line[50]; inti=0; while(getline(input,line[i])) { //cout< i++; } (); cout< ! "< intj=0; remove(""); for(j=0;j { Lexlex(line[j]); } cout< ! "< return0; } 四、重要数据结构 stringline[]: 用于存储每一行的字符,并逐个读取分析。 stringtoken[]: 用于存储TINY语言的符号,并调用遍历进行判断。 stringreservedWord[]: 用于存储TINY语言的保留字,遍历进行判断,若为真, 则输出Reservedword。 staticintrowCounter: 静态变量,存储行号,每创建一个类的实例便加一。 inttemp: 用于存储数字,并输出。 staticintbracketExist: 静态变量,标记注释是否存在。 stringtoken,str分别用于临时存储读取的符号的字母串。 五、算法总结 建立Lexclass,并读取每一行,创建Lex的实例,在Lex中处理。 先判断是否在注释范围内,若是,则输出注释内容,直至产生“}”字符。 若不在注释区 内,则读取单个字符,根据DFAS行判断。 若为符号,则当下一个字符不是符号时输出;若为数字,则继续往下读,直至下一个字符不是数字为止,输出。 若为字母,继续读取,直至下一个字符不是字母,把这一串字母和预先定义的保留字比对,若是,则输出“Reservedword”,若不是,则输出“ID,name=字样<一行处理完毕,便开始创建下一行实例,直至文件尾。 数据记录和计算 Tiny测试程序 1: {Sampleprogram 1: {Sampleprogram 2: inTINYIanguage- 2: inTINYIanguage- 3: computesfactorial 结 3: computesfactorial 论 (结 4: } 果) 4: } 5: readx;{inputaninteger} 5: ReversedWord: read 5: ID,name=x 5: ; 5: {inputaninteger} 6: if0 6: ReversedWord: if 6: NUM,val=0 6: < 6: ID,name=x 6: ReversedWord: then 6: {don'tcomputeifx<=0} 7: fact: =1; 7: ID,name=fact 7: : = 7: NUM,val=1 7: ; 8: repeat 8: ReversedWord: repeat 9: fact: =fact*x; 9: ID,name=fact 9: : = 9: ID,name=fact 9: * 9: ID,name=x 10: x: =x-1; 10: ID,name=x 10: : = 10: ID,name=x 10: - 10: NUM,val=1 10: ; 11: untilx=0; 11: ReversedWord: until 11: ID,name=x 11: = 11: NUM,val=0 11: ; 12: writefact{outputfactorialofx} 12: ReversedWord: write 12: ID,name=fact 12: {outputfactorialofx} 13: end 13: ReversedWord: end 小 结 顺利完成实验,熟悉了Tiny语言和其词法。 根据语言和词法规则,顺利构造DFA 成功用C++语言,根据构造的DFA实现了Tiny词法分析器。 增强了自己的编程能力和水平技巧,尝试了很多以前没有尝试过的方法学习到了新知识。 指导老师评议 成绩评定: 指导教师签名:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Tiny 语言 词法 分析器