-
Notifications
You must be signed in to change notification settings - Fork 1
/
lexan.c
222 lines (203 loc) · 4.33 KB
/
lexan.c
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
211
212
213
214
215
216
217
218
219
220
221
222
/*
** Lexical analyzer 词法分析器
** Last update 2012.04.18
*/
# include "9860g.h"
struct tkbox statebox ; /* token盒子 */
static int state,start ;
struct tkbox* lexan()
{
char ch;
state = start = 0 ;
while (ch = GETCH) {
switch(state){
case 0:
/* case 1, -9: 空白符 */
if (isspace(ch) || ch == ',') state = -9 ;
else state = nextry() ;
break ;
case -9:
if ( isspace(ch) ) state = -9 ;
else { RETCH ; recover() ; }
break ;
case 1:
/* case 1,2: 分号注释 */
if (ch == ';') state = 2 ;
else state = nextry() ;
break ;
case 2:
if (ch == '\n') return get(TOKEN_COMMENT) ;
else state = 2 ;
break ;
case 3:
/* case 3,4: [标题] */
if (ch == '[') state =4 ;
else state = nextry() ;
break ;
case 4:
if (ch ==']') return get (TOKEN_TITLE); /* 去除]号 */
else if (ch == '\n') return get(TOKEN_ERR) ; /* 无法识别的词素 */
else state = 4 ;
break ;
case 5:
/* case 5,6: ID元素 */
if ( isalnum(ch) ) state = 6 ;
else state = nextry();
break ;
case 6:
if ( isalnum(ch) || ch == '_') state = 6 ;
else if (ch == '.') { state = 5 ; state = nextry();} /* 有点号则为数字 */
else {
RETCH ;
return get(TOKEN_ID) ;
}
break ;
case 7:
/* case 7: 等号 */
//ch=GETCH;
if (ch == '=') return get(TOKEN_EQUAL);
else state = nextry();
break ;
case 8:
/* case 8,9: 数字 */
if (isdigit(ch) || ch == '-') state = 9 ;
else state = nextry();
break ;
case 9:
if (isdigit(ch)) state = 9 ;
else if (ch == '.') state = 9 ;
else {
RETCH ;
return get(TOKEN_NUM) ;
}
break ;
case 10:
/* case 10,11:文件路径 */
if (ch == '\\') state = 11 ;
else state = nextry();
break ;
case 11:
if (ch == '\n' || ch == ';') {
RETCH ;
return get(TOKEN_FOLDER) ;
}
else state = 11 ;
break ;
default:
break ;
}
}
return NULL ;
}
struct tkbox* lexan_num()
{
char ch;
state = start = 0 ;
while (ch = GETCH) {
switch(state){
case 0:
/* case 1, -9: 空白符 */
if (isspace(ch) || ch == ',') state = -9 ;
else state = nexnum() ;
break ;
case -9:
if ( isspace(ch) ) state = -9 ;
else { RETCH ; recover() ; }
break ;
case 8:
/* case 8,9: 数字 */
if (isdigit(ch) || ch == '-') state = 9 ;
else state = nexnum();
break ;
case 9:
if (isdigit(ch)) state = 9 ;
else if (ch == '.') state = 9 ;
else {
RETCH ;
return get(TOKEN_NUM) ;
}
break ;
default:
break ;
}
}
return NULL ;
}
/* 进入下一个可能的状态 */
int nexnum()
{
switch (start){
case 0: start = 8 ; break ;
case 8: start = 999 ; break ;
default: break ;
}
forward = begin ;
return start ;
}
int nextry()
{
switch (start){
case 0: start = 1 ; break ;
case 1: start = 3 ; break ;
case 3: start = 5 ; break ;
case 5: start = 7 ; break ;
case 7: start = 8 ; break ;
case 8: start = 10 ; break ;
case 10: start = 999 ; break ;
default: break ;
}
forward = begin ;
return start ;
}
/* 恢复指针及状态 */
int recover()
{
state = start = 0 ;
begin = forward ;
}
char key[MAXLINE] ;
/* 获取TOKEN */
struct tkbox* get(int token)
{
double value = 0.0 ;
char s[MAXLINE] = "" ;
char* str ;
if (token == TOKEN_TITLE) {
++begin ; /* 去除[号 */
--forward ; /* 去除]号 */
}
if (token != TOKEN_COMMENT){
strncpy(s, begin, forward - begin) ; /* 复制词素 */
str = strcpy(key, s);
}
switch (token){
case TOKEN_COMMENT:
token = TOKEN_COMMENT ;
value = 0 ;
str = NULL ;
break ;
case TOKEN_ERR:
token = TOKEN_ERR ;
value = -1 ;
str = str ;
break ;
case TOKEN_TITLE: case TOKEN_ID:
case TOKEN_EQUAL:
token = token ;
value = 0 ;
str = str ;
break ;
case TOKEN_NUM:
token = token ;
value = atof(str) ;
str = str ;
break ;
default:
break ;
}
statebox.str = str ;
statebox.token = token ;
statebox.value = value ;
recover() ;
return &statebox ;
}