Cminus语言词法分析器实验报告Word格式.docx
- 文档编号:16934594
- 上传时间:2022-11-27
- 格式:DOCX
- 页数:15
- 大小:52.19KB
Cminus语言词法分析器实验报告Word格式.docx
《Cminus语言词法分析器实验报告Word格式.docx》由会员分享,可在线阅读,更多相关《Cminus语言词法分析器实验报告Word格式.docx(15页珍藏版)》请在冰豆网上搜索。
及他们之间的互相转化。
熟悉把正规文法转化为正规式,把正规式转化为转为相应的DFA最后再把DFA简化,器
4.C语言的基本语法。
、实验要求
1、该个词法分析器要求至少能够识别以下几类单词:
所有的关键字都是保留字,并且必须
关键字:
elseifintreturnvoidwhile共6个,
是小写;
标识符:
识别与C语言词法规定相一致的标识符,通过下列正则表达式定义:
digit*(.digitdigit*|&
)(e(+|-|&
)digitdigit*|&
),,digit=0|..|9,包括整数,如123等;
小数,如123.45等;
科1.23e3,2.3e-9等;
ID=letter(letter|digit)*常数:
NUM=digit
letter=a|..|z|A|..|Z|
学计数法表示的常数,如
专用符号:
+-*/<
<
=>
>
===!
==,()[]{}/**/;
2、分析器的输入为由上述几类单词构成的程序,输出为该段程序的机内表示形式,即关键
字、运算符、界限符变为其对应的机内符,常数使用二进制形式,标识符使用相应的标识符
表指针表示。
3、词法分析器应当能够指出源程序中的词法错误,如不可识别的符号、错误的词法等。
四、实验结果(程序)及分析
#include<
stdio.h>
stdlib.h>
string.h>
#defineN100
typedefstruct{
charname[30];
intcode;
intaddr;
}token;
//存储刚从文件中读取的字符
}Keyword;
}symbol;
Keywordkey[6]={{"
else”,1},{"
if"
2},{"
int”,3},{"
return”,4},{"
void”,5},{"
where”,
6}};
charch;
//接受字符
FILE*source;
//源文件
FILE*keytxt;
//关键字输出文件
FILE*badgetxt;
//标识符输出文件
FILE*othertxt;
//其他单词输出文件
FILE*number;
interror_count;
//错误的个数
intaddr_count;
//标识符表的指针
intlineof;
//行号
tokencurrent;
//暂时存放读入的字符
tokenzancun;
symbolcurrentsymbol;
symbolsymboltable[N];
//标识符表
voiderror(inti);
voidmain(){
voidscan();
error_count=0;
addr_count=0;
lineof=0;
scan();
}
voidscan()(inti=0;
voidiskeyword();
//判断关键字voidisOthers()//判断其他单词voidoutput_1();
voidoutput_2();
voidoutput_others();
voidIszhushi();
voidisnumber();
if((source=fopen("
Source.txt”,"
r"
))==NULL){printf("
fileopenerror/n"
);
exit(0);
if((keytxt=fopen("
key.txt"
"
w"
if((badgetxt=fopen("
badge.txt”,"
if((othertxt=fopen("
others.txt"
))==NULL)printf("
fileopenerror\n"
if((number=fopen("
number.txt”,"
//打开源文件
//打开关键字文件
//打开标识符文件
ch=fgetc(source);
while(ch!
=EOF){
for(i=0;
i<
30;
i++)
current.name[i]='
\0'
;
if((ch>
='
a'
&
&
ch<
z'
)||(ch>
A'
Z'
)||ch=='
_'
)
iskeyword();
if(ch=='
\\'
Iszhushi();
if(ch>
0'
ch<
9'
isnumber();
else
isOthers();
fclose(source);
fclose(keytxt);
fclose(badgetxt);
fclose(othertxt);
fclose(number);
voidiskeyword(){
inti=0,k=0,j=0;
inth=0;
while(((ch>
)&
(ch<
))||((ch>
))||ch=='
||(ch>
'
)){
current.name[i]=ch;
i++;
zancun=current;
6;
i++){
for(j=0;
j<
j++){
if(current.name[j]==key[i].name[j]){
h=0;
}else{
h=1;
break;
if(h==0)
if(h==0){
current.code=key[i].code;
output_1();
strcpy(symboltable[addr_count].name,current.name);
symboltable[addr_count].code=10;
symboltable[addr_count].addr=addr_count;
addr_count++;
output_2();
voidisOthers(){
charch1;
inti;
switch(ch)(
case'
+'
:
(
current.name[0]='
current.code=13;
current.addr=-1;
output_others();
case'
-'
(
current.code=14;
*'
current.code=15;
/'
current.code=16;
ch1=fgetc(source);
if(ch1=='
)(
current.name[1]='
current.code=17;
}else(
fseek(source,-1,1);
current.code=18;
〉'
:
{
){
current.code=19;
}else{
current.code=20;
){
current.code=21;
}else{
current.code=22;
break;
current.code=23;
current.code=24;
current.code=25;
'
current.code=26;
('
inti=ftell(source);
charch1=ch;
charch2;
)'
){ch=fgetc(source);
current.name[0]=ch1;
current.name[1]=ch;
current.code=27;
fseek(source,i,0);
ch2=fgetc(source);
error_count++;
error
(1);
{'
{
current.code=28;
}'
['
current.code=29;
]'
10'
lineof++;
voidIszhushi(){
ch1=ch;
)(
for(;
if(ch==EOF)(
error
(2);
}else(
voidisnumber()(
intk=0;
voidoutput_number();
while(ch>
current.name[k++]=ch;
current.code=11;
output_number();
voidoutput_1(){
printf("
关键字:
%s,%d>
”,current.name,current.code);
putchar(10);
fprintf(keytxt,"
"
current.name,current.code);
voidoutput_2(){
标识符地址:
current.name,addr_count);
fprintf(badgetxt,"
voidoutput_others(){
其它单词:
fprintf(othertxt,"
voiderror(inti){
switch(i){
case1:
printf("
界符发生错误,发生在第%d行/n”,lineof);
case2:
注释发生错误,发生在第%d行/n"
lineof);
voidoutput_number(){
常数<
\n"
fprintf(number,"
实验结果分析,程序运行结果截图:
rSourer,lit修#军
堂件。
犒精牌格式皿童者①I
fita_b*2;
returnlMVxcti网荷
2
*
词词J词=<
地谒rl;
^5B富单
1nss:
5anykceyrtoroutinvie
关键字输出文件:
key-记事本
文伽元瞄E)«
a(o)g«
v)wa面<
iff2>
int,3>
retun%4>
标识符输出文件:
2lbadge-记事本
「五牛(Fj漏8正)唔式(o[查看瞽助面
»
!
a_b,2>
|
数字的输出文件
number-记事本
变的EE)网。
)ss(v)
1<
5,11X2,11>
其它单词输出文件:
others-记事本
婚F)林E)招式(0)查看(V)耗助(H)
0,27>
=,22Xc28>
=,22>
fi25>
}f28>
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Cminus 语言 词法 分析器 实验 报告