----词法分析程序
目求
通设计编制调试具体词法分析程序加深词法分析原理理解
掌握程序设计语言源程序进行扫描程中分解类单词词法分析
方法
题目分析
实验户指定想编译C语言编写文件作词法分析程序输入数
进行词法分析中先文件头开始行单位扫描程序该行字符读入预先设定数组缓区中然该数组字符逐词分割进行词法分析词分割成关键字标识符常量运算符四种词种终产生四相应表关键字表标识符表常量表运算符表文件形式进行存储外产生编译文件指定词四表中位置
实验程
面词法分析程序中文件变量进行说明:
文件:
cpilec 程序文件
keytxt 关键字文件
operationtxt 运算符文件
idtxt 标识符文件
consttxt 常量文件
after_comtxt 编译产生文件
变量:
FILE *sfp*nfp*ifp*kfp*cfp*pfp
char ib[50][20] 标识符表(动态生成)
char cb[50][10] 常量表(动态生成)
char kb[44][10] 关键字表(预先定义)
char pb[36][5] 运算符表(预先定义)
子函数名:
int number(char s[]int i) 数字处理函数
int letter(char s[]int i) 字符处理函数
int operation(char s[]int i) 运算符处理函数
void seti (char s[]) 标识符建立函数
void setc (char s[]) 常量建立函数
void cfile(char s1[] char s2[]int m) 词词表中位置写入编译文件
void error1(char s[]) 字符处理出错报告
void error2(char s[]) 标识符处理出错报告
void error3(char s[]) 运算符处理出错报告
void openall() 开文件
void writeall() 四表写入文件
void closeall() 关闭文件
面简分析词法分析程序运行流程:
否开编译C语言文件
否
报错
判断前字符否文件结束符
结束
源程序中读入行数组缓区
否
判断前字符否’\n’
否
判断前字符否字母
转关键字标识符处理
否
判断前字符否数字
转数字处理
否
判断前字符否运算符
转运算符处理
否
程序调试
现源程序ac清单:
#include
int main(int argc char *argv[])
{char ch
int i
ch'a'
chch+32
ich
printf(d id c\nich)*印*
return 0
}
运行词法分析程序显示结果:
after_comtxt文件:
#[p2]
include[i0]
<[p14]
stdioh[i1]
>[p16]
int[k2]
main[i2]
([p7]
int[k2]
argc[i3]
[p6]
char[k0]
*[p9]
argv[i4]
[[p18]
][p21]
)[p8]
{[p23]
char[k0]
ch[i5]
[p13]
int[k2]
i[i6]
[p13]
ch[i5]
[p15]
'[p19]
a[i7]
'[p19]
[p13]
ch[i5]
[p15]
ch[i5]
+[p10]
32[c0]
[p13]
i[i6]
[p15]
ch[i5]
[p13]
printf[k33]
([p7]
[p1]
[p4]
d[i8]
id[i9]
[p4]
c[i10]
\[p20]
n[i11]
[p1]
[p6]
i[i6]
[p6]
ch[i5]
)[p8]
[p13]
return[k28]
0[c1]
[p13]
}[p25]
keytxt 关键字文件:
0 char
1 short
2 int
3 unsigned
4 long
5 float
6 double
7 struct
8 union
9 void
10 enum
11 signed
12 const
13 volatile
14 typedef
15 auto
16 register
17 static
18 extem
19 break
20 case
21 continue
22 default
23 do
24 else
25 for
26 goto
27 if
28 return
29 switch
30 while
31 sizeof
32 txt
33 printf
34 FILE
35 fopen
36 NULL
37 fclose
38 exit
39 r
40 read
41 close
42 w
43 fprintf
idtxt 标识符文件:
0 include
1 stdioh
2 main
3 argc
4 argv
5 ch
6 i
7 a
8 d
9 id
10 c
11 n
operationtxt 运算符文件:
0
1
2 #
3
4
5 &
6
7 (
8 )
9 *
10 +
11
12
13
14 <
15
16 >
17
18 [
19 '
20 \
21 ]
22
23 {
24 ||
25 }
26
27 >
28 <
29
30 ++
31
32 &&
33 *
34 *
consttxt 常量文件:
0 32
1 0
结果完全正确
词法分析程序:
#include
#include
#include
#include
#define SIZE 256
#define null 0
int line0error0mark10mark20
char *sname
FILE *sfp*nfp*ifp*kfp*cfp*pfp
**
char ib[50][20]
char cb[50][10]
char kb[44][10]{charshortintunsignedlongfloatdoublestructunionvoid
enumsignedconstvolatiletypedefautoregisterstaticextem
breakcasecontinuedefaultdoelseforgoto
ifreturnswitchwhilesizeoftxtprintfFILEfopenNULL
fcloseexitrreadclosewfprintf}
char pb[36][5]{\\#\&()*+<>['
\\]{||}><++&&**}
*定义四二元数组存放四表*
int main(int argc char *argv[])
{ char sbuff[SIZE]
char nbuff[SIZE]
int i
**
int number(char s[]int i)
int letter(char s[]int i)
int operation(char s[]int i)
void seti (char s[])
void setc (char s[])
void cfile(char s1[] char s2[]int m)
void error2(char s[])
void error1(char s[])
void error3(char s[])
void openall()
void writeall()
void closeall()
**
if(argc2)
{ printf(Please input the file you want to compile\n)
exit(1)
}
else
snameargv[1]
sfpfopen(argv[1]r)
openall()
i0
while(i<50 )
{strcpy(ib[i] )
strcpy(cb[i] )
i++
}
**
while(fgets(sbuffSIZEsfp))
{
i0
line++ *行方式读取源代码进行编译*
while(sbuff[i]'\n')
{
if(isalpha(sbuff[i])) * 处理关键字标识符 *
iletter(sbuffi)
else if(isdigit(sbuff[i]))
inumber(sbuffi) * 处理数字常量 *
else
ioperation(sbuffi) * 处理运算符 *
}
fputc('\n'nfp)
}
**
if(error)
printf(There are d errorserror) * 错误数报告 *
else
printf(Success to compile the file)
writeall()
closeall()
return 0
}
**
void openall() * 开必需文件 *
{
kfpfopen(keytxtw+) * 开关键字表 *
pfpfopen(operationtxtw+) * 开运算符表 *
cfpfopen(consttxtw+) * 开常量文件 *
ifpfopen(idtxtw+) * 开标识符文件 *
nfpfopen(after_comtxtw+) * 开编译会产生文件 *
if(kfpNULL || cfpNULL || ifpNULL || pfpNULL || nfpNULL)
{ printf(Cannot open these files) * 开文件错退出程序 *
exit(0)
}
}
**
void writeall()
{ int m0
while (m<36&&pb[m] )
{
fprintf(pfpdss\n m pb[m])
m++
}
m0
while (m<44&&kb[m] )
{
fprintf(kfpdss\n m kb[m])
m++
}
m0
while (m
fprintf(cfpdss\n m cb[m])
m++
}
m0
while (m
fprintf(ifpdss\n m ib[m])
m++
}
}
**
void closeall() * 关闭文件 *
{
fclose(kfp)
fclose(pfp)
fclose(cfp)
fclose(ifp)
fclose(nfp)
}
**
int letter(char sbuff[]int i)
{char str[10]
void cfile(char s1[] char s2[]int m)
void seti (char s[])
void setc (char s[])
int m0n0
str[n]sbuff[i]
while(isalnum(sbuff[i])||sbuff[i]'')
{ str[n++]sbuff[i++]
}
str[n]'\0'
while (m<44&&(strcmp(strkb[m])0))
{m++
}
if (m<44)
{
cfile(strkm)
}
else
{m0
while (m<50&&(strcmp(strib[m])0))
{m++
}
if (m<50)
{
cfile (strim)
}
else seti(str)
}
return i
}
**
int number(char sbuff[]int i)
{char s[2]str[10]
void cfile(char s1[] char s2[]int m)
void seti (char s[])
void setc (char s[])
void error2(char s[])
void error1(char s[])
int m0n0str[n]sbuff[i]i++n++
if(sbuff[i]'')
{str[n]sbuff[i]}
if ((sbuff[i]'E' || sbuff[i]'e') && (sbuff[i+1]'+' || sbuff[i+1]'' ||isdigit(sbuff[i+1])))
{str[n]sbuff[i]str[n++]sbuff[i++]}
while (isdigit(sbuff[i]))
{str[n++]sbuff[i++]
}
*出现十进制数处理*
str[n++]'\0'
while (m<50&&(strcmp(strcb[m])0))
{m++
}
if (isalpha(sbuff[i]))
{error2(str)
}
else
{
if (m<50)
{
cfile(strcm)
}
else setc(str)
}
return i
}
**
int operation(char sbuff[]int i)
{ void error3(char s[])
void cfile(char s1[] char s2[]int m)
void setc (char s[])
int k0m0char s[4]
if (sbuff[i]''&&sbuff[i+1]'*')
{ ii+2
while ((sbuff[i]'*'&&sbuff[i+1]''))
{i++
}
ii+2
}*注释跳*
else
{
if(sbuff[i]' ')
{
while(sbuff[i]' ')
i++
}
else
{
s[k]sbuff[i]
while (k<3)
{s[k++]sbuff[i++]
}
while (k>0)
{ s[k]'\0' m0
while (m<36&&(strcmp(spb[m])0))
{m++
}
if (m<36)
{
cfile(spm)
break
}
else {ki}
if (k1)
{ i++
error3(s)
}
}
}
}
return i
}
**
void cfile(char s1[] char s2[]int m)
{
fprintf(nfps[sd]\ns1s2m)
return
}
void seti (char s[])
{void cfile(char s1[] char s2[]int m)
strcpy(ib[mark1]s)
cfile(simark1)
mark1++
return
}
void setc (char s[])
{void cfile(char s1[] char s2[]int m)
strcpy(cb[mark2]s)
cfile(scmark2)
mark2++
return
}
**
void error1 (char s[])
{ error++
printf(line ds is an illegal letter\nlines)
return
}
void error2(char s[])
{ error++
printf(line ds is an illegal word\nlines)
return
}
void error3(char s[])
{ error++
printf(line ds is an illegal operation\nlines)
return
}
**
编程体会
A.编写程序前应该整体规划然考虑细节问题断发现问题修改程序精益求精
B 通实验更加深文件操作理解够较掌握文件操作基领
C 应该培养良程序设计风格
文档香网(httpswwwxiangdangnet)户传
《香当网》用户分享的内容,不代表《香当网》观点或立场,请自行判断内容的真实性和可靠性!
该内容是文档的文本内容,更好的格式请下载文档