词法分析器Word格式文档下载.docx
《词法分析器Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《词法分析器Word格式文档下载.docx(23页珍藏版)》请在冰点文库上搜索。
待分析的简单语言的词法
(1)
关键字:
begin
if
then
while
do
end
所有关键字都是小写。
(2)运算符和界符:
:
=
+
-
*
/
<
>
=
;
(
)
#
(3):
其他单词是标识符(ID)和整型常数(NUM),通过以下正规式定义:
ID=letter(letter|digit)*
NUM=digitdigit*
(4)空格由空白、制表符和换行符组成。
空格一般用来分隔ID、NUM、运算符、界符和关键字,词法分析阶段通常被忽略。
、设计的词法分析器符合软件工程的要求。
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<
h;
i++)
space="
"
for(intj=0;
j<
h-i;
j++)
space=space+"
str=str.Replace(space,"
);
/*for(inti=0;
str.Length;
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'
'
//去除注释
privatestringquit_express(stringstr)
while(str.Contains("
*/"
))
str=str.Remove(str.IndexOf("
/*"
)+2,str.IndexOf("
)-str.IndexOf("
)-2);
str=str.Replace("
/**/"
"
//"
intfront=str.IndexOf("
inti=front+1;
while(true)
i++;
)break;
str=str.Remove(front+2,i-front-2);
//\n"
privateboolword_analyse(stringstr)//判断是否是关键字
string[]key={"
int"
char"
float"
double"
"
main"
"
return"
include"
scanf"
printf"
};
key.Length;
if(key[i].CompareTo(str)==0)returntrue;
returnfalse;
privatevoidbutton2_Click(objectsender,EventArgse)//产生token序列
stred.Length;
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++;
if(i==stred.Length||stred[i]=='
stringB=newstring(A);
//MessageBox.Show(B);
if(word_analyse(B))
addtextbox("
保留字"
B);
else
标识符"
i--;
elseif(Isdigtal(stred[i]))
boolflag=false;
char[]A=newchar[20];
Isdigtal(stred[i])||stred[i]=='
.'
if(i==stred.Length||stred[i]=='
if(stred[i]=='
)flag=true;
if(flag)
浮点数"
整数"
elseif(Isoprate(stred[i]))
*'
)addtextbox("
乘号"
*"
elseif(stred[i]=='
/'
除号"
/"
-'
减号"
-"
+'
加号"
+"
='
等号"
="
elseif(Isbound(stred[i]))
stringB="
B=B.Replace('
stred[i]);
界符"
elseif(Iscommand(stred[i]))
'
逗号"
'
分号"
#'
导入号"
privateboolIsalphabet(charc)//是不是字符
if(c>
='
A'
&
&
c<
Z'
||c>
a'
z'
returntrue;
elsereturnfalse;
privateboolIsdigtal(charc)//是不是数字
0'
9'
privateboolIsoprate(charc)//是不是算术符号
stringA="
+-*/&
|="
A.Length;
if(c==A[i])returntrue;
privateboolIsbound(charc)//是不是界符
()\"
{}."
privateboolIscommand(charc)//是不是标点符
;
#"
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"
root>
!
--
MicrosoftResXSchema
Version2.0
TheprimarygoalsofthisformatistoallowasimpleXMLformat
thatismostlyhumanreadable.Thegenerationandparsingofthe
variousdatatypesaredonethroughtheTypeConverterclasses
associatedwiththedatatypes.
Example:
...headers&
schema...
resheadername="
resmimetype"
text/microsoft-resx<
/resheader>
version"
2.0<
reader"
System.Resources.ResXResourceReader,System.Windows.Forms,...<
writer"
System.Resources.ResXResourceWriter,System.Windows.Forms,...<
dataname="
Name1"
value>
thisismylongstring<
/value>
comment>
thisisacomment<
/comment>
/data>
Color1"
type="
System.Drawing.Color,System.Drawing"
Blue<
Bitmap1"
mimetype="
application/x-.object.binary.base64"
[base64mimeencodedserialized.NETFrameworkobject]<
Icon1"
System.Drawing.Icon,System.Drawing"
application/x-.object.bytearray.base64"
[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.
application/x-.object.soap.base64
System.Runtime.Serialization.Formatters.Soap.SoapFormatter
application/x-.object.bytearray.base64
Theobjectmustbeserializedintoabytearray
usingaSystem.ComponentModel.TypeConverter
-->
xsd:
schemaid="
root"
xmlns="
xmlns:
xsd="
http:
//www.w3.org/2001/XMLSchema"
msdata="
urn:
schemas-microsoft-com:
xml-msdata"
importnamespace="
//www.w3.org/XML/1998/namespace"
/>
elementname="
msdata:
IsDataSet="
true"
complexType>
choicemaxOccurs="
unbounded"
metadata"
sequence>
value"
string"
minOccurs="
0"
/xsd:
attributename="
name"
use="
required"
type"
mimetype"
attributeref="
xml:
space"
element>
assembly"
alias"
data"
Ordinal="
1"
comment"
2"
3"
4"
c