当前位置:文档之家› 编译原理实验报告(C语言)

编译原理实验报告(C语言)


else if(strcmp(word,"if")==0) fprintf(out,"%c%c%s%c,%d%c\n",'(','"',word,'"',12,')'); else if(strcmp(word,"else")==0) fprintf(out,"%c%c%s%c,%d%c\n",'(','"',word,'"',13,')'); else if(strcmp(word,"switch")==0) fprintf(out,"%c%c%s%c,%d%c\n",'(','"',word,'"',14,')'); else if(strcmp(word,"case")==0) fprintf(out,"%c%c%s%c,%d%c\n",'(','"',word,'"',15,')'); else if(strcmp(word,"for")==0) fprintf(out,"%c%c%s%c,%d%c\n",'(','"',word,'"',16,')'); else if(strcmp(word,"do")==0) fprintf(out,"%c%c%s%c,%d%c\n",'(','"',word,'"',17,')'); else if(strcmp(word,"while")==0) fprintf(out,"%c%c%s%c,%d%c\n",'(','"',word,'"',18,')'); else if(strcmp(word,"goto")==0) fprintf(out,"%c%c%s%c,%d%c\n",'(','"',word,'"',19,')'); else if(strcmp(word,"continue")==0) fprintf(out,"%c%c%s%c,%d%c\n",'(','"',word,'"',20,')'); else if(strcmp(word,"break")==0) fprintf(out,"%c%c%s%c,%d%c\n",'(','"',word,'"',21,')');
/*消耗掉空格,制表符,换行符*/ while(cp==' '||cp=='\t'||cp=='\n') { cp=fgetc(in); }
/*cp 数组复位*/ i=0;
/*数字检测*/ if(cp>='0'&&cp<='9') { word[i++]=cp; cp=fgetc(in); while(cp>='0'&&cp<='9') {
else fprintf(out,"自定义标识符:%c%c%s%c,%d%c\n",'(','"',word,'"',29,')'); }
/*排错处理(只能以字母、数字、下划线构成)*/ else {
{ word[i]='\0'; /*基本保留字检测*/ if(strcmp(word,"main")==0) fprintf(out,"%c%c%s%c,%d%c\n",'(','"',word,'"',1,')'); else if(strcmp(word,"void")==0) fprintf(out,"%c%c%s%c,%d%c\n",'(','"',word,'"',2,')'); else if(strcmp(word,"int")==0) fprintf(out,"%c%c%s%c,%d%c\n",'(','"',word,'"',3,')'); else if(strcmp(word,"float")==0) fprintf(out,"%c%c%s%c,%d%c\n",'(','"',word,'"',4,')'); else if(strcmp(word,"double")==0) fprintf(out,"%c%c%s%c,%d%c\n",'(','"',word,'"',5,')'); else if(strcmp(word,"char")==0) fprintf(out,"%c%c%s%c,%d%c\n",'(','"',word,'"',6,')'); else if(strcmp(word,"struct")==0) fprintf(out,"%c%c%s%c,%d%c\n",'(','"',word,'"',7,')'); else if(strcmp(word,"const")==0) fprintf(out,"%c%c%s%c,%d%c\n",'(','"',word,'"',8,')'); else if(strcmp(word,"extern")==0) fprintf(out,"%c%c%s%c,%d%c\n",'(','"',word,'"',9,')'); else if(strcmp(word,"register")==0) fprintf(out,"%c%c%s%c,%d%c\n",'(','"',word,'"',10,')'); else if(strcmp(word,"static")==0) fprintf(out,"%c%c%s%c,%d%c\n",'(','"',word,'"',11,')');
1、cp 消耗掉空格,制表符,换行符后,cp 数组复位,开始检测 cp; 2、数字检测,对照符号表输出,若匹配成功,则返回序号; 3、字符串检测, 对照符号表输出,若匹配成功,则返回序号; 4、基本保留字检测,对照符号表输出,若匹配成功,则返回序号; 5、运算符检测,对照符号表输出,若匹配成功,则返回序号;注意这里碰到‘/’时,要判断后面是 否跟着是注释语句。是则跳过,不是则输出运算符。 6、分隔符(界符)检测,对照符号表输出,若匹配成功,则返回序号; 7、排错处理; 结束。
3.以下给定一个 C 语言的符号表的设计和结构:
C 语言基本保留
字表
main
1
if
12
sizeof
23
void
2
else
13
return
24
int
3
switch
14
float
4
case
15
double
5
for
16
char
6
do
17
struct
7
while
18
const
8
goto
19
extern register static
9
continue
10
break
11
default
20
常数
28
21
自定义字符
29
22
运算符
分隔符
‘+'
31

41
‘-’
32

42
‘*’
33

43
‘/'
34
{
44
‘<'
35
}
45
‘<=’
36
[
46
‘>’
37
]
47
‘>=’
相关主题