词法分析器.docx
- 文档编号:9987690
- 上传时间:2023-02-07
- 格式:DOCX
- 页数:23
- 大小:101.22KB
词法分析器.docx
《词法分析器.docx》由会员分享,可在线阅读,更多相关《词法分析器.docx(23页珍藏版)》请在冰豆网上搜索。
词法分析器
编译原理课程设计:
词法分析器
词法分析器
一、实验目的和要求:
设计并实现一个PL/0语言(或其它语言,如C语言)的词法分析程序,加深对词法分析原理的理解。
二、实验原理:
词法分析是从左向右扫描每行源程序的符号,拼成单词,换成统一的机内表示形式——TOKEN字,送给语法分析程序。
TOKEN字是一个二元式:
(单词种别码,自身值)。
PL/0语言单词的种别码参见教材(或自行设定),单词自身值按如下规则给出:
1标识符的自身值是它在符号表的入口地址。
2常数的自身值是常数本身(或它的二进制数值)。
3关键字和界限符的自身值为本身。
三、实验步骤与要求
1、设计的词法分析器符合软件工程的要求。
2、编制程序,此程序应具有如下功能:
1)输入:
字符串(待进行词法分析的源程序),输出:
由(种别码,自身值)所组成的二元组序列。
2)功能:
a.滤空格
b.识别保留字
c.识别标识符
d.拼数
e.拼复合单词:
例如:
>=、<=、:
=
3)检查如下错误:
a.程序语言的字符集以外的非法字符
b.单词拼错,如9A88,而对于将begin拼写成begon的错误,只须把begon当成标识符即可
3、请指导教师检查程序和运行结果,评定成绩。
4、撰写并上交实验报告。
四、试验设计和算法分析:
实验原理:
程序流程:
置初值→调用扫描子程序→输出串结束→输出单词二元组→是→否→结束
词法分析主程序示意图
待分析的简单语言的词法
(1) 关键字:
begin if then while do end
所有关键字都是小写。
(2)运算符和界符:
:
= + - * / < > <= <> >= ; ( ) #
(3):
其他单词是标识符(ID)和整型常数(NUM),通过以下正规式定义:
ID=letter(letter|digit)*
NUM=digitdigit*
(4)空格由空白、制表符和换行符组成。
空格一般用来分隔ID、NUM、运算符、界符和关键字,词法分析阶段通常被忽略。
、设计的词法分析器符合软件工程的要求。
2、编制程序,此程序应具有如下功能:
4)输入:
字符串(待进行词法分析的源程序),输出:
由(种别码,自身值)所组成的二元组序列。
5)功能:
f.滤空格
g.识别保留字
h.识别标识符
i.拼数
j.拼复合单词:
例如:
>=、<=、:
=
6)检查如下错误:
c.程序语言的字符集以外的非法字符
d.单词拼错,如9A88,而对于将begin拼写成begon的错误,只须把begon当成标识符即可
词法分析程序的算法思想
算法的基本任务是从字符串表示的源程序中识别出具有独立意义的单词符号,其基本思想是根据扫描到单词符号的第一个字符的种类,拼出相应的单词符号。
三、代码:
Form1
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
namespace词法分析C
{
publicpartialclassForm1:
Form
{
privatestringstred;
privateintpc=0;
publicForm1()
{
InitializeComponent();
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
stringstr=textBox1.Text;
str=quit_express(str);
str=quit_enter(str);
str=quit_space(str);
//MessageBox.Show(str);
this.stred=str;
textBox2.Text=str;
}
//去除多余的空格
privatestringquit_space(stringstr)
{
str=str.Trim();
stringspace="";
inth=str.Length;
for(inti=0;i { space=""; for(intj=0;j { space=space+""; } str=str.Replace(space,""); } /*for(inti=0;i { Stringtemp; if(str[i]=='') { temp=str.Substring(i+1,str.Length-i-1); str=str.Remove(i); str=str+temp; } } */ returnstr; } //去除回车、换行 privatestringquit_enter(stringstr) { str=str.Replace('\n',''); returnstr; } //去除注释 privatestringquit_express(stringstr) { while(str.Contains("*/")) { str=str.Remove(str.IndexOf("/*")+2,str.IndexOf("*/")-str.IndexOf("/*")-2); str=str.Replace("/**/",""); } while(str.Contains("//")) { intfront=str.IndexOf("//"); inti=front+1; while(true) { i++; if(str[i]=='\n')break; } str=str.Remove(front+2,i-front-2); str=str.Replace("//\n",""); } returnstr; } privateboolword_analyse(stringstr)//判断是否是关键字 { string[]key={"int","char","float","double","main","return","include","scanf","printf"}; for(inti=0;i { if(key[i].CompareTo(str)==0)returntrue; } returnfalse; } privatevoidbutton2_Click(objectsender,EventArgse)//产生token序列 { for(inti=0;i { if(Isalphabet(stred[i])||stred[i]=='_') { intk=0; char[]A=newchar[20]; for(;Isalphabet(stred[i])||stred[i]=='_'||Isdigtal(stred[i]);) { A[k]=stred[i]; k++;i++; if(i==stred.Length||stred[i]=='')break; } stringB=newstring(A); //MessageBox.Show(B); if(word_analyse(B)) { addtextbox("保留字",B); } else { addtextbox("标识符",B); } i--; } elseif(Isdigtal(stred[i])) { intk=0; boolflag=false; char[]A=newchar[20]; for(;Isdigtal(stred[i])||stred[i]=='.';) { A[k]=stred[i]; k++;i++; if(i==stred.Length||stred[i]=='')break; if(stred[i]=='.')flag=true; } stringB=newstring(A); if(flag) { addtextbox("浮点数",B); } else { addtextbox("整数",B); } i--; } elseif(Isoprate(stred[i])) { if(stred[i]=='*')addtextbox("乘号","*"); elseif(stred[i]=='/')addtextbox("除号","/"); elseif(stred[i]=='-')addtextbox("减号","-"); elseif(stred[i]=='+')addtextbox("加号","+"); elseif(stred[i]=='=')addtextbox("等号","="); } elseif(Isbound(stred[i])) { stringB=""; B=B.Replace('',stred[i]); addtextbox("界符",B); } elseif(Iscommand(stred[i])) { stringB=""; B=B.Replace('',stred[i]); if(stred[i]==',')addtextbox("逗号",B); elseif(stred[i]==';')addtextbox("分号",B); elseif(stred[i]=='#')addtextbox("导入号",B); } } } privateboolIsalphabet(charc)//是不是字符 { if(c>='A'&&c<='Z'||c>='a'&&c<='z') returntrue; elsereturnfalse; } privateboolIsdigtal(charc)//是不是数字 { if(c>='0'&&c<='9') returntrue; elsereturnfalse; } privateboolIsoprate(charc)//是不是算术符号 { stringA="+-*/&|="; for(inti=0;i { if(c==A[i])returntrue; } returnfalse; } privateboolIsbound(charc)//是不是界符 { stringA="()\"{}."; for(inti=0;i { if(c==A[i])returntrue; } returnfalse; } privateboolIscommand(charc)//是不是标点符 { stringA=",;#"; for(inti=0;i { if(c==A[i])returntrue; } returnfalse; } privatevoidaddtextbox(stringexpression,stringstr) { stringtemp1="."; stringtemp2="."; stringtemp=temp1; pc++; if(pc>=10)temp=temp2; listBox1.Items.Add(pc.ToString()+temp+expression+""+str); } } } xmlversion="1.0"encoding="utf-8"? > -- MicrosoftResXSchema Version2.0 TheprimarygoalsofthisformatistoallowasimpleXMLformat thatismostlyhumanreadable.Thegenerationandparsingofthe variousdatatypesaredonethroughtheTypeConverterclasses associatedwiththedatatypes. Example: ...headers&schema... Thereareanynumberof"resheader"rowsthatcontainsimple name/valuepairs. Eachdatarowcontainsaname,andvalue.Therowalsocontainsa typeormimetype.Typecorrespondstoa.NETclassthatsupport text/valueconversionthroughtheTypeConverterarchitecture. Classesthatdon'tsupportthisareserializedandstoredwiththe mimetypeset. Themimetypeisusedforserializedobjects,andtellsthe ResXResourceReaderhowtodepersisttheobject.Thisiscurrentlynot extensible.Foragivenmimetypethevaluemustbesetaccordingly: Note-application/x-.object.binary.base64istheformat thattheResXResourceWriterwillgenerate,howeverthereadercan readanyoftheformatslistedbelow. mimetype: application/x-.object.binary.base64 value: Theobjectmustbeserializedwith : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter : andthenencodedwithbase64encoding. mimetype: application/x-.object.soap.base64 value: Theobjectmustbeserializedwith : System.Runtime.Serialization.Formatters.Soap.SoapFormatter : andthenencodedwithbase64encoding. mimetype: application/x-.object.bytearray.base64 value: Theobjectmustbeserializedintoabytearray : usingaSystem.ComponentModel.TypeConverter : andthenencodedwithbase64encoding. --> schemaid="root"xmlns=""xmlns: xsd="http: //www.w3.org/2001/XMLSchema"xmlns: msdata="urn: schemas-microsoft-com: xml-msdata"> importnamespace="http: //www.w3.org/XML/1998/namespace"/> elementname="root"msdata: IsDataSet="true"> complexType> choicemaxOccurs="unbounded"> elementname="metadata"> complexType> sequence> elementname="value"type="xsd: string"minOccurs="0"/> sequence> attributename="name"use="required"type="xsd: string"/> attributename="type"type="xsd: string"/> attributename="mimetype"type="xsd: string"/> attributeref="xml: space"/> complexType> element> elementname="assembly"> complexType> attributename="alias"type="xsd: string"/> attributename="name"type="xsd: string"/> complexType> element> elementname="data"> complexType> sequence> elementname="value"type="xsd: string"minOccurs="0"msdata: Ordinal="1"/> elementname="comment"type="xsd: string"minOccurs="0"msdata: Ordinal="2"/> sequence> attributename="name"type="xsd: string"use="required"msdata: Ordinal="1"/> attributename="type"type="xsd: string"msdata: Ordinal="3"/> attributename="mimetype"type="xsd: string"msdata: Ordinal="4"/> attributeref="xml: space"/> complexType> element> elementname="resheader"> c
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 词法 分析器