词法分析器.docx

上传人:b****1 文档编号:3247271 上传时间:2023-05-05 格式:DOCX 页数:23 大小:101.22KB
下载 相关 举报
词法分析器.docx_第1页
第1页 / 共23页
词法分析器.docx_第2页
第2页 / 共23页
词法分析器.docx_第3页
第3页 / 共23页
词法分析器.docx_第4页
第4页 / 共23页
词法分析器.docx_第5页
第5页 / 共23页
词法分析器.docx_第6页
第6页 / 共23页
词法分析器.docx_第7页
第7页 / 共23页
词法分析器.docx_第8页
第8页 / 共23页
词法分析器.docx_第9页
第9页 / 共23页
词法分析器.docx_第10页
第10页 / 共23页
词法分析器.docx_第11页
第11页 / 共23页
词法分析器.docx_第12页
第12页 / 共23页
词法分析器.docx_第13页
第13页 / 共23页
词法分析器.docx_第14页
第14页 / 共23页
词法分析器.docx_第15页
第15页 / 共23页
词法分析器.docx_第16页
第16页 / 共23页
词法分析器.docx_第17页
第17页 / 共23页
词法分析器.docx_第18页
第18页 / 共23页
词法分析器.docx_第19页
第19页 / 共23页
词法分析器.docx_第20页
第20页 / 共23页
亲,该文档总共23页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

词法分析器.docx

《词法分析器.docx》由会员分享,可在线阅读,更多相关《词法分析器.docx(23页珍藏版)》请在冰点文库上搜索。

词法分析器.docx

词法分析器

 

编译原理课程设计:

词法分析器

 

词法分析器

一、实验目的和要求:

设计并实现一个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...

text/microsoft-resx

2.0

System.Resources.ResXResourceReader,System.Windows.Forms,...

System.Resources.ResXResourceWriter,System.Windows.Forms,...

thisismylongstringthisisacomment

Blue

[base64mimeencodedserialized.NETFrameworkobject]

[base64mimeencodedstringrepresentingabytearrayformofthe.NETFrameworkobject]

Thisisacomment

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

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2