编译原理实验
报告
软件系统测试报告下载sgs报告如何下载关于路面塌陷情况报告535n,sgs报告怎么下载竣工报告下载
(一)
----词法
分析
定性数据统计分析pdf销售业绩分析模板建筑结构震害分析销售进度分析表京东商城竞争战略分析
程序
【目的要求】
通过设计编制调试一个具体的词法分析程序,加深对词法分析原理的理解。
并掌握在对程序设计语言源程序进行扫描过程中将其分解为各类单词的词法分析
方法。
【题目分析】
本实验以用户指定的想编译的以C语言编写的文件作为词法分析程序的输入数据。
在进行词法分析中,先自文件头开始以行为单位扫描程序,将该行的字符读入预先设定的一个数组缓冲区中,然后对该数组的字符逐词分割,进行词法分析,将每个词分割成关键字、标识符、常量和运算符四种词种,最终产生四个相对应的
表
关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf
,即关键字表、标识符表、常量表和运算符表,它们以文件的形式进行存储。除此之外,还产生一个编译后的文件,它指定了每个词在四个表中的位置。
【实验过程】
下面就词法分析程序中的文件和主要变量进行说明:
文件:
cpile.c 主程序文件
key.txt 关键字文件
operation.txt 运算符文件
id.txt 标识符文件
const.txt 常量文件
after_com.txt 编译后产生的文件
主要变量:
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(); 关闭所有文件
下面简要分析一下词法分析程序的运行
流程
快递问题件怎么处理流程河南自建厂房流程下载关于规范招聘需求审批流程制作流程表下载邮件下载流程设计
:
否
能
是
否
是
否
是
否
是
否
是
否
【程序调试】
现有源程序a.c清单如下:
#include
int main(int argc, char *argv[])
{char ch;
int i;
ch='a';
ch=ch+32 ;
i=ch;
printf("%d id %c\n",i,ch);/*打印*/
return 0;
}
运行词法分析程序后,显示如下结果:
after_com.txt文件:
#[p--2]
include[i--0]
<[p--14]
stdio.h[i--1]
>[p--16]
int[k--2]
main[i--2]
([p--7]
int[k--2]
argc[i--3]
,[p--6]
char[k--0]
*[p--9]
argv[i--4]
[[p--18]
][p--21]
)[p--8]
{[p--23]
char[k--0]
ch[i--5]
;[p--13]
int[k--2]
i[i--6]
;[p--13]
ch[i--5]
=[p--15]
'[p--19]
a[i--7]
'[p--19]
;[p--13]
ch[i--5]
=[p--15]
ch[i--5]
+[p--10]
32[c--0]
;[p--13]
i[i--6]
=[p--15]
ch[i--5]
;[p--13]
printf[k--33]
([p--7]
"[p--1]
%[p--4]
d[i--8]
id[i--9]
%[p--4]
c[i--10]
\[p--20]
n[i--11]
"[p--1]
,[p--6]
i[i--6]
,[p--6]
ch[i--5]
)[p--8]
;[p--13]
return[k--28]
0[c--1]
;[p--13]
}[p--25]
key.txt 关键字文件:
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
id.txt 标识符文件:
0 include
1 stdio.h
2 main
3 argc
4 argv
5 ch
6 i
7 a
8 d
9 id
10 c
11 n
operation.txt 运算符文件:
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 */
const.txt 常量文件:
0 32
1 0
结果完全正确。
词法分析程序如下:
#include
#include
#include
#include
#define SIZE 256
#define null 0
int line=0,error=0,mark1=0,mark2=0;
char *sname;
FILE *sfp,*nfp,*ifp,*kfp,*cfp,*pfp;
/*--------------------------------------------------------------------*/
char ib[50][20];
char cb[50][10];
char kb[44][10]={"char","short","int","unsigned","long","float","double","struct","union","void",
"enum","signed","const","volatile","typedef","auto","register","static","extem",
"break","case","continue","default","do","else","for","goto",
"if","return","switch","while","sizeof","txt","printf","FILE","fopen","NULL",
"fclose","exit","r","read","close","w","fprintf"};
char pb[36][5]={"!","\"","\#","\$","%","&",",","(",")","*","+","-",":",";","<","=",">","?","[","'",
"\\","]",".","{","||","}","!=",">=","<=","==","++","--","&&","/*","*/"};
/*----------定义了四个二元数组存放四个表--------------------------------*/
int main(int argc, char *argv[])
{ char sbuff[SIZE];