编译原理实验报告.docx
- 文档编号:23299875
- 上传时间:2023-05-16
- 格式:DOCX
- 页数:14
- 大小:66.72KB
编译原理实验报告.docx
《编译原理实验报告.docx》由会员分享,可在线阅读,更多相关《编译原理实验报告.docx(14页珍藏版)》请在冰豆网上搜索。
编译原理实验报告
编译原理实验报告
———————————————————————————————————————
指导老师:
丁老师
专业班级:
08软件工程3班
姓名:
xxxx
学号:
0801070311xx
时间:
2010年12月26日
1试验目的
设计编制并调试一个词法分析程序,加深对此法分析原理的理解。
可以识别浮点数。
2试验要求
单词符号
种别码
单词符号
种别码
单词符号
种别码
单词符号
种别码
main
1
=
21
30
“
39
int
2
+
22
;
31
‘\0
100
char
3
-
23
:
32
ERROR
-1
if
4
*
24
>
33
public
8
else
5
/
25
<
34
void
9
for
6
(
26
>=
35
include
10
while
7
)
27
<=
36
struct
11
ID
50
{
28
==
37
float
12
NUM
20
}
29
!
=
38
static
13
说明:
在源程序的基础上添加了许多新单词和对浮点数的识别。
3程序结构
程序用C编写,编译环境是VC6.0。
syn整型,记录单词符号的种别编码。
tmp字符数组,长度为256,可以缓存用户输入的字符串。
token字符数组,长度为32,存放识别出来的单词符号。
ch字符型,存放当前所取的字符。
voidgetch()//读字符函数,从缓冲区读入下一个字符放在ch中,
并指向下一个字符。
voidoutput()//打印指针所指向的字符串。
voidgetbc()//检查ch是否为空白字符,若是则调用getch()直到
ch中读入一个非空白字符。
voidconcat()//把当前ch中字符与token中字符串连接。
intletter()//判定ch中是否为字母。
intdigit()//判定ch中是否为数字。
intispoint()//判定ch中是否为小数点。
intreserve()//对token中的字符串查表。
若是返回关键字,否则返回50。
voidscanner()//扫描buffer字符串的词法分析函数。
4总结
程序可以成功运行,每次由用户输入一行字符串,程序将扫描此串,并将词法分析结果在屏幕上显示。
程序也可以做到对浮点数的识别。
如下程序:
publicstaticvoidmain()
{inta=0;
intb=100;
floatf=1.5;
charc='a';
whlie('a'!
='z')
{
a=a+1;
}
f=(a+b-c)/a;
}
5程序
#include
#include
#include
#include
#defineLENsizeof(structNode)
structNode
{chardata;
structNode*next;
};
structNode*head,*p;
charch;
char*key[13]={"main","int","char","if","else","for",
"while","public","void","include","struct","float","static"};
chartoken[20];
voidgetch()
{ch=p->data;
p=p->next;
}
voidoutput(structNode*head)
{if(!
head)
{printf("somethingwrongwhithheadnode");
exit
(1);
}
p=head->next;
while(p->next!
=NULL)
{printf("%c",p->data);
p=p->next;
}
printf("\n");
}
voidgetbc()
{while(ch=='')
getch();
}
voidconcat()
{unsignedinti;
i=strlen(token);
token[i]=ch;
token[i+1]='\0';
}
intletter(charch)
{returnisalpha((int)ch);
}
intdigit(charch)
{returnisdigit((int)ch);
}
intispoint(charch)
{if(ch=='.')return1;
elsereturn0;
}
intreserve()
{intk;
for(k=0;k<13;k++)
{if(strcmp(key[k],token)==0)
return(k+1);
}
return50;
}
voidretract()
{structNode*Q;
Q=head->next;
while(Q->next!
=p)
Q=Q->next;
p=Q;
}
voidback(inta,char*b)
{printf("(%d,%s)\n",a,b);
}
voidscaner()
{intc;
token[0]=NULL;
getch();
getbc();
if(letter(ch))
{while(letter(ch)||digit(ch))
{concat();
getch();
}
retract();
c=reserve();
if(c!
=10)back(c,token);
elseback(10,token);
}
elseif(digit(ch))
{inti=0;
while(digit(ch)||ispoint(ch))
{
if(ispoint(ch))i++;
if(i>=2)break;
concat();
getch();
}
retract();
if(i==0)printf("(20,%d)\n",atoi(token));
else
{printf("(20,");printf(token);printf(")\n");
}
}
else
switch(ch)
{case'+':
back(22,"+");break;
case'-':
back(23,"-");break;
case'*':
back(24,"*");break;
case'/':
back(25,"/");
case'(':
back(26,"(");break;
case')':
back(27,")");break;
case'{':
back(28,"{");
case'}':
back(29,"}");break;
case',':
back(30,",");break;
case';':
back(31,";");break;
case':
':
back(32,":
");break;
case'=':
getch();
if(ch=='=')
back(37,"==");
else
{retract();
back(21,"=");
}
break;
case'>':
getch();
if(ch=='=')
back(35,">");
else
{retract();
back(33,">");
}
break;
case'<':
getch();
if(ch=='=')
back(36,"<=");
else
{retract();
back(34,"<");
}
break;
case'!
':
getch();
if(ch=='=');
back(38,"!
=");
break;
case'`':
getch();
if(ch=='=')
back(39,"`");
else
{if(ch=='')
back(100,"`");
}
break;
case'#':
break;
default:
break;
}
}
intmain()
{head=(structNode*)malloc(LEN);
if(!
head)
{printf("failedtocreattheheadnode!
");
exit
(1);
}
head->next=NULL;
head->data='';
p=head;
printf("pleaseinputyourcode:
\n");
while
(1)
{inti=0;
chartmp[256];
gets(tmp);
if(tmp[0]=='#')break;
p->next=(structNode*)malloc(LEN);
if(!
(head->next))
{printf("failedtocreateanewnode!
");exit
(1);
}
p=p->next;
while((tmp[i]!
='\0')&&(i<256))
{p->data=tmp[i];
p->next=(structNode*)malloc(LEN);
if(!
(p->next))
{printf("failedtocreateanewnode!
");
exit
(1);
}
p=p->next;
i++;
}
p->data='\n';
p->next=NULL;
}
printf("\nthecodeyouinputis:
\n");
output(head);
printf("\naftercompling,theresultis:
\n");
p=head->next;
while(p->next!
=NULL)
scaner();
system("pause");
return0;
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 编译 原理 实验 报告