词法分析器

词法分析器

词法分析器功能是输入源程序,按照构词规则分解成一系列单词符号。
单词是语言中具有独立意义最小单位,包括关键字、标识符、运算符、界符和常量等。
编制词法分析器。
输出单词符号表示成二元式:(单词种别,单词符号的属性值)。单词种别通常用整数编码。
标识符一般统归为一种。常数则宜按类型(整、实、布尔等)分种。关键字可将其全体视为一种。运算符可采用一符一种的方法。界符一般用一符一种的方法。

Source code:

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include<iostream>
#include<string.h>
using namespace std;
#define MAX 10
/*
保留字|关键字:1
操作符|运算符:2
分界符:3
标识符:4
常数:5
无识别:6
*/
char ch = ' ';
char* keyWord[10] = {"int","main","break","include","begin","end","if","else","while","switch"};
char token[20];//定义获取的字符
//判断是否是关键字
bool isKey(char *token)
{
for(int i = 0;i < MAX;i++)
{
if(strcmp(token,keyWord[i]) == 0)
return true;
}
return false;
}
//判断是否是字母
bool isLetter(char letter)
{
if((letter >= 'a' && letter <= 'z')||(letter >= 'A' && letter <= 'Z'))
return true;
else
return false;
}
//判断是否是数字
bool isDigit(char digit)
{
if(digit >= '0' && digit <= '9')
return true;
else
return false;
}
//词法分析
void analyze(FILE *fpin)
{

while((ch = fgetc(fpin)) != EOF){
if(ch == ' '||ch == '\t'||ch == '\n'){}
else if(isLetter(ch)){
char token[20]={'\0'};
int i=0;
while(isLetter(ch)||isDigit(ch)){
token[i] = ch;
i++;
ch = fgetc(fpin);
}
//回退一个指针
fseek(fpin,-1L,SEEK_CUR);
if(isKey(token)){
//关键字
cout<<token<<"\t1"<<"\t关键字"<<endl;
}
else{
//标识符
cout<<token<<"\t4"<<"\t标识符"<<endl;
}
}
else if(isDigit(ch)||(ch == '.'))
{
int i=0;
char token[20]={'\0'};
while(isDigit(ch)||(ch == '.'&&isDigit(fgetc(fpin))))
{
if(ch == '.')fseek(fpin,-1L,SEEK_CUR);
token[i] = ch;
i++;
ch = fgetc(fpin);
}
fseek(fpin,-1L,SEEK_CUR);
//属于无符号常数
cout<<token<<"\t5"<<"\t常数"<<endl;
}
else switch(ch){
//运算符
case '+':{
ch = fgetc(fpin);
if(ch == '+')cout<<"++"<<"\t2"<<"\t运算符"<<endl;
else {
cout<<"+"<<"\t2"<<"\t运算符"<<endl;
fseek(fpin,-1L,SEEK_CUR);
}
}break;
case '-':{
ch = fgetc(fpin);
if(ch == '-')cout<<"--"<<"\t2"<<"\t运算符"<<endl;
else {
cout<<"-"<<"\t2"<<"\t运算符"<<endl;
fseek(fpin,-1L,SEEK_CUR);
}
}break;
case '*':cout<<ch<<"\t2"<<"\t运算符"<<endl;break;
case '/':cout<<ch<<"\t2"<<"\t运算符"<<endl;break;
//分界符
case '(':cout<<ch<<"\t3"<<"\t分界符"<<endl;break;
case ')':cout<<ch<<"\t3"<<"\t分界符"<<endl;break;
case '[':cout<<ch<<"\t3"<<"\t分界符"<<endl;break;
case ']':cout<<ch<<"\t3"<<"\t分界符"<<endl;break;
case ';':cout<<ch<<"\t3"<<"\t分界符"<<endl;break;
case '{':cout<<ch<<"\t3"<<"\t分界符"<<endl;break;
case '}':cout<<ch<<"\t3"<<"\t分界符"<<endl;break;
//运算符
case '=':{
ch = fgetc(fpin);
if(ch == '=')cout<<"=="<<"\t2"<<"\t运算符"<<endl;
else {
cout<<"="<<"\t2"<<"\t运算符"<<endl;
fseek(fpin,-1L,SEEK_CUR);
}
}break;

case '<':{
ch = fgetc(fpin);
if(ch == '=')cout<<"<="<<"\t2"<<"\t运算符"<<endl;
else {
cout<<"<"<<"\t2"<<"\t运算符"<<endl;
fseek(fpin,-1L,SEEK_CUR);
}
}break;
//无识别
default: cout<<ch<<"\t6"<<"\t无识别符"<<endl;
}
}
}
int main(){
char input[30];
FILE *fpin;
cout<<"请输入源文件名:\n"<<endl;
for(;;){
cin>>input;
if((fpin = fopen(input,"r")) != NULL)
break;
else
cout<<"路径输入错误"<<endl;
}
cout<<"****************词法分析结果********************"<<endl;
analyze(fpin);
fclose(fpin);
}

单词符号 种别码
关键字      1
运算符      2
分界符      3
标识符      4
常数          5
无法识别   6