编译原理实验一报告Word格式文档下载.docx
- 文档编号:21251128
- 上传时间:2023-01-28
- 格式:DOCX
- 页数:19
- 大小:123.29KB
编译原理实验一报告Word格式文档下载.docx
《编译原理实验一报告Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《编译原理实验一报告Word格式文档下载.docx(19页珍藏版)》请在冰豆网上搜索。
>
46
for
4
<
47
while
5
48
do
6
49
return
7
==
50
…
!
51
#
40
%
52
+
41
&
53
-
42
……
单词符号分类:
单词是程序设计语言的基本语法单位和最小语义单位。
单词符号一般分为五大类
(1)关键字"
begin"
"
end"
if"
then"
else"
while"
write"
read"
"
do"
"
call"
const"
char"
until"
procedure"
repeat"
等
(2)运算符:
+"
-"
*"
/"
="
(3)界符:
{"
}"
["
]"
;
."
("
)"
:
(4)标识符
(5)常量
3.实验结果图:
初始界面:
显示分析结果:
out.txt结果:
换行操作:
结果如图:
实现了显示行号和注释大写
4.实验心得:
这个需要对字符有所了解。
对不同的token进行分类。
实例代码存在很多的bug,需要对它进行修改,这考验了一个人的debug能力。
5.实验代码:
#include<
stdio.h>
string.h>
stdlib.h>
structkeywords//关键字结构体
{
charname[10];
intnum;
};
structrelation//运算符、关系运算符结构体
charname[4];
structkeywordstest[18]={
{"
main"
1},{"
2},{"
int"
3},{"
for"
4},{"
5},{"
6},
retuen"
7},{"
break"
8},{"
continue"
9},{"
stdio.h"
10},
include"
11},{"
case"
12},{"
float"
13},{"
switch"
14},
void"
15},{"
16},{"
17}
};
structrelationrelation[18]={
#"
40},{"
41},{"
42},{"
43},{"
44},{"
45},{"
46},
47},{"
48},{"
49},{"
=="
50},{"
51},{"
%"
52},{"
53},
54},{"
|"
55},{"
||"
56},{"
58}
structvalue//处理变量的结构体
inttype;
intno;
charstr[20];
}value[100];
voidoptions();
//菜单
voidanalyse();
//分析结果显示
voidshowcol();
//添加行号
voidgetch(charch);
//读取为字母
voidgetnum(charch);
//读取为数字
voidgetspace(charch);
//读取空格、跳格、回车、换行等等(包括界限符)
voidgetelse(charch);
//其他类型字符
intvariable(charstr[10]);
//变量处理
charch='
1'
charstr[10];
intk=0,i=0;
charsourcefile[30];
//源文件名
charobjectfile[30];
//目标文件名
charchangefile[30];
FILE*fp,*hp,*cf;
intcount=-1;
intflag=-1;
//标志
intcol;
intmain(intargc,char*argv[])
intchoice;
options();
//显示菜单
printf("
输入进行词法分析的源文件名:
);
scanf("
%s"
sourcefile);
\n输入分析结果存入的文件名:
objectfile);
\n输入分行结果存入的文件名:
changefile);
\n"
请输入选择项(1或者2或者3):
//printf("
\n输入选项:
%d"
&
choice);
for(;
;
)
{
switch(choice)
{
case1:
analyse();
break;
case2:
showcol();
case3:
exit
(1);
}
break;
}
return0;
}
voidoptions()
***************************\n"
***"
《词法分析器》"
***\n"
1.分析结果:
2.显示行号:
3.退出程序:
voidshowcol()
if((fp=fopen(sourcefile,"
r"
))==NULL)
printf("
读文件打开错误!
exit
(1);
if((cf=fopen(changefile,"
w"
写文件打开错误!
ch='
col=1;
fprintf(cf,"
%d:
"
col++);
while(ch!
=EOF)
ch=fgetc(fp);
if(ch=='
\n'
)//显示行号
fputc(ch,cf);
fprintf(cf,"
elseif(ch=='
/'
)//在/**/内大写
ch=fgetc(fp);
if(ch=='
*'
{
do
{
ch=fgetc(fp);
if(ch<
='
z'
ch>
a'
{
ch='
A'
+ch-'
fputc(ch,cf);
}
elseif(ch=='
ch=fgetc(fp);
if(ch=='
{
fputc(ch,cf);
break;
}
else
fseek(fp,-1L,1);
}
else
}while
(1);
}
else
fclose(fp);
fclose(cf);
显示行数及大写成功!
system("
pause"
voidanalyse()
))==NULL)//读取文件
exit
(1);
if((hp=fopen(objectfile,"
))==NULL)//写入文件
写入文件打开错误!
/*
*/
=EOF)//判断是否到文件结尾
ch=fgetc(fp);
//读取磁盘字符串
if(((ch>
)&
(ch<
))||((ch>
Z'
)))
getch(ch);
}
else
if((ch>
0'
9'
))//如果读取的是数字
{
getnum(ch);
else
if((ch=='
'
)||(ch=='
\r'
\t'
))
getspace(ch);
else//其他情况
getelse(ch);
k=0;
//单个字符标志
str[k]='
\0'
//关闭读文件
fclose(hp);
//关闭写文件
voidgetch(charch)
{
for(;
str[k]=ch;
str[++k]='
if(!
(((ch>
)&
(ch<
))||((ch>
))||ch=='
.'
fseek(fp,-1L,1);
for(i=0;
i<
11;
i++)
{
if(strcmp(str,test[i].name)==0)
//测试是否为保留字
{
printf("
(%d,%s)\n"
test[i].num,test[i].name);
fprintf(hp,"
k=0;
flag=1;
value[count+1].type=test[i].num;
break;
if(i==11)//否则为变量
inttem;
tem=variable(str);
(20,%s)\n"
str);
fprintf(hp,"
(20,%d)\n"
tem);
break;
voidgetnum(charch)
if(!
((ch>
)))
fseek(fp,-1L,1);
(30,%s)\n"
fprintf(hp,"
(30,%s)\n"
voidgetspace(charch)
//判断时候是空格、跳格、回车、换行等等
//检测读取字符
//---换行写序号
//---
if(!
((ch=='
voidgetelse(charch)
//其他字符,包括界限符
switch(ch)
case'
(60,.)\n"
(60,.)\n"
case'
#'
(40,#)\n"
(40,#)\n"
'
if(flag==1)
value[count+1].type=value[count].type;
(61,,)\n"
(61,,)\n"
'
flag=0;
value[count+1].type=2;
(62,;
)\n"
(62,;
{'
(63,{)\n"
(63,{)\n"
}'
(64,})\n"
(64,})\n"
('
(65,()\n"
(65,()\n"
)'
(66,))\n"
(66,))\n"
+'
(41,+)\n"
(41,+)\n"
-'
(42,-)\n"
(42,-)\n"
(43,*)\n"
(43,*)\n"
(44,/)\n"
(44,/)\n"
str[k]=ch;
str[++k]='
ch=fgetc(fp);
if(ch!
fseek(fp,-1L,1);
printf("
(45,=)\n"
(45,=)\n"
(50,=)\n"
(50,=)\n"
(46,>
(46,>
(48,>
(48,>
(47,<
(47,0)\n"
(49,0)\n"
(51,!
=)\n"
(51,!
(58,!
(58,0)\n"
%'
(52,%)\n"
(52,%)\n"
(53,&
(53,&
(54,&
(54,&
|'
(55,|)\n"
(55,0)\n"
(56,||)\n"
(56,||)\n"
intvariable(charstr[10])
intj;
for(j=0;
j<
count+1;
j++)
if(strcmp(str,value[j].str)==0)
if(j<
count+1)
return(value[j].type*100+value[j].no);
{
count++;
if(flag==-1)
value[count].type=20;
for(inti=0;
9;
value[count].str[i]=str[i];
value[count].no=count+1;
return(value[count].type*100+value[count].no);
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 编译 原理 实验 报告
![提示](https://static.bdocx.com/images/bang_tan.gif)