-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexAna.c
More file actions
210 lines (210 loc) · 3.82 KB
/
Copy pathLexAna.c
File metadata and controls
210 lines (210 loc) · 3.82 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
bool letter(char ch)
{
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ? true : false;
}
bool digit(char ch)
{
return ch >= '0' && ch <= '9' ? true : false;
}
bool underline(char ch)
{
return ch == '_' ? true : false;
}
bool reserve(int i, char token[])
{
char *j; //存放首地址
char str[] = " int void cin cout return float double return "; //关键字前后均为空格---' key '
token[0] = ' ';
j = strstr(str, token); //找' key'
if (j) //存在,返回首地址,不存在返回NULL
return *(j + i + 1) == ' ' ? true : false;
return false;
}
void output(int i, int j, char token[])
{
while (j <= i)
printf("%c", token[j++]);
printf("\n");
}
int main()
{
FILE *fp; //读取文件
char ch; //读入单个词
char token[20] = ""; //存放待判断词
int i = 0, j = 0, c = 0, size = 0; // i来断token有效范围,j通用,c判关键字标识符,size文件大小
fp = fopen("D:\\CodeStor\\C_VScode\\LexTest.txt", "r");
if (!fp)
{
printf("file open error\n");
exit(1);
}
fseek(fp, 0, SEEK_END); //定位文件指针到文件尾。
size = ftell(fp); //获取文件指针偏移量,即文件大小。
rewind(fp); //指针指向首地址
while (size > 0)
{
do
{
ch = fgetc(fp);
if (ch == '\n')
size--;
} while (--size >= 0 && (ch == ' ' || ch == '\n' || ch == '\t')); //读取结束或为空格(占1个),换行(占2个)
if (size < 0)
break;
switch (ch)
{
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
{ //若是字母或下划线开头
i = 1; //字母数字下划线专有,为了区别标识符和关键字
while (letter(ch) || digit(ch) || underline(ch))
{
token[i] = ch;
ch = fgetc(fp);
size--;
i++;
}
size++;
fseek(fp, -1, SEEK_CUR); //回退一个位置
i--; // token[0~i]为内容
c = reserve(i, token); //判断是否为关键字,1关键字,0标识符
if (c) //关键字
printf("关键字:\t");
else //标识符
printf("标识符:\t");
output(i, 1, token);
break;
}
case '0':
{
while (digit(ch))
{
token[i] = ch;
ch = fgetc(fp);
size--;
i++;
}
size++;
fseek(fp, -1, SEEK_CUR); //回退一个位置
i--; // token[0~i]为内容
printf("数字0:\t");
output(i, 0, token);
break;
}
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
{
while (digit(ch))
{
token[i] = ch;
ch = fgetc(fp);
size--;
i++;
}
size++;
fseek(fp, -1, SEEK_CUR); //回退一个位置
i--; // token[0~i]为内容
printf("非0数:\t");
output(i, 0, token);
break;
}
case '+':
case '-':
case '*':
case '/':
case '%':
{
printf("运算符:\t%c\n", ch);
break;
}
case '(':
case ')':
case '{':
case '}':
case '[':
case ']':
case ',':
case ':':
case ';':
case '"':
{
printf("分隔符:\t%c\n", ch);
break;
}
case '=':
{
printf("赋值符:\t%c\n", ch);
break;
}
default:
printf("其它:\t%c\n", ch);
break;
}
i = 0; //用i来断token有效范围
for (int j = 0; j < 10; j++) //初始化
token[j] = '\0';
}
fclose(fp);
return 0;
}