词法分析器又称扫描器编译原理课程设计Word格式.docx
- 文档编号:13680502
- 上传时间:2022-10-12
- 格式:DOCX
- 页数:13
- 大小:86.18KB
词法分析器又称扫描器编译原理课程设计Word格式.docx
《词法分析器又称扫描器编译原理课程设计Word格式.docx》由会员分享,可在线阅读,更多相关《词法分析器又称扫描器编译原理课程设计Word格式.docx(13页珍藏版)》请在冰豆网上搜索。
2.2各种单词符号对应的种别码
单词符号
种别码
begin
1
17
if
2
=
18
then
3
20
while
4
<
21
do
5
22
end
6
23
letter(letter|digit)*
10
24
digitdigit*
11
25
*
13
;
26
/
14
(
27
+
15
)
28
-
16
#
二、需求分析
词法分析程序的功能:
输入:
所给文法的源程序input.txt文本文档。
输出:
四元组(type,name,rows,cols)构成的output.txt文本文档。
其中:
type为单词种别码;
Name为单词名称;
Rows为所在文件行号;
Cols为所在文件列号。
例如:
对源程序beginY:
=9:
ifY>
9thenY:
=2*x+1/3;
end#的源文件,经过词法分析后输出如下序列:
(1,begin)(10,x)(18,:
=)(11,9)(26,;
)(2,if)……
三、设计思路
算法的基本任务是从字符串表示的源程序中识别出具有独立意义的单词符号,其基本思想是根据扫描到单词符号的第一个字符的种类,拼出相应的单词符号。
3.1主程序示意图:
主程序示意图如图3-1所示。
其中初始包括以下两个方面:
⑴关键字表的初值。
关键字作为特殊标识符处理,把它们预先安排在一张表格中(称为关键字表),当扫描程序识别出标识符时,查关键字表。
如能查到匹配的单词,则该单词为关键字,否则为一般标识符。
关键字表为一个字符串数组,其描述如下:
Char*rwtab[6]={“begin”,“if”,“then”,“while”,“do”,“end”};
否
是
图3-1
(2)程序中需要用到的主要变量为syn,token和sum
3.2扫描子程序的算法思想:
首先设置3个变量:
①token用来存放构成单词符号的字符串;
②sum用来存放整型单词;
③syn用来存放单词符号的种别码。
扫描子程序主要部分流程如图3-2所示。
图3-2
四、详细设计
词法分析程序的C++语言程序源代码:
#include"
stdafx.h"
conio.h"
//包含getch函数的头文件
stdlib.h"
//包含exit函数的头文件
#include<
stdio.h>
string.h>
charprog[81],token[8],ch,tmp;
intsyn,p,m,n,sum,rows;
char*rwtab[6]={"
begin"
"
if"
then"
while"
do"
end"
};
voidscaner();
voidmain()
{
FILE*out;
FILE*in;
rows=1;
if((out=fopen("
d:
\\output.txt"
wt"
))==NULL)
{printf("
\nonerrorbuidingd:
\\output.txt\n"
);
getch();
exit
(1);
}
if((in=fopen("
\\input.txt"
rt"
\nConn'
tfoundd:
\\input.txt\n"
}/*printf("
\npleaseinputastring(endwith'
#'
):
\n"
*/
rewind(in);
rewind(out);
fprintf(out,"
%-5s%-10s%-5s%5s\n"
type"
name"
rows"
cols"
do{
p=0;
do
{/*scanf("
%c"
&
ch);
if(p==80)
{printf("
\nonerrorcolunm'
slengthmorethan80!
getch();
exit
(1);
}
ch=fgetc(in);
prog[p++]=ch;
}while(ch!
=EOF&
&
ch!
='
\n'
prog[p]='
tmp=ch;
p=0;
do{
scaner();
switch(syn)
{case11:
%-5d%-10d%-5d%5d\n"
syn,sum,rows,p);
break;
case-1:
printf("
youhaveinputawrongstring,rowscols=(%d,%d)\n"
rows,p);
exit(0);
default:
if(syn!
=0)fprintf(out,"
%-5d%-10s%-5d%5d\n"
syn,token,rows,p);
}while(syn!
=0);
if(tmp=='
)rows++;
=EOF);
fclose(in);
fclose(out);
voidscaner()
{sum=0;
for(m=0;
m<
8;
m++)token[m++]=NULL;
ch=prog[p++];
m=0;
while((ch=='
'
)||(ch=='
))ch=prog[p++];
if(((ch<
z'
)&
(ch>
a'
))||((ch<
Z'
A'
)))
{while(((ch<
))||((ch>
0'
(ch<
9'
{token[m++]=ch;
p--;
syn=10;
for(n=0;
n<
6;
n++)
if(strcmp(token,rwtab[n])==0)
{syn=n+1;
elseif((ch>
))
{while((ch>
{sum=sum*10+ch-'
syn=11;
elseswitch(ch)
{case'
'
token[m++]=ch;
if(ch=='
{syn=22;
token[m++]=ch;
else
{syn=20;
case'
{syn=24;
{syn=23;
+'
{syn=17;
{syn=13;
-'
{syn=29;
{syn=14;
!
ch=prog[p++];
{syn=21;
{syn=31;
{syn=25;
{syn=18;
*'
syn=15;
/'
syn=16;
('
syn=27;
)'
syn=28;
{'
syn=5;
}'
syn=6;
syn=26;
\"
syn=30;
caseEOF:
syn=0;
break;
syn=0;
syn=17;
syn=-1;
token[m++]='
\0'
}
五、运行调试与分析讨论
1、给定源程序
beginx:
=9;
ifx>
0thenx:
end#
输出结果
2、源程序(包括上式未有的while、do以及判断错误语句):
x<
=$;
a<
b<
9-x;
二、设计体会与小结
通过这次课程设计,加深了我对词法分析的理解。
也更是锻炼了我的编程能
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 词法 分析器 又称 扫描器 编译 原理 课程设计