-
Notifications
You must be signed in to change notification settings - Fork 7
/
lex.yy.part.c
286 lines (241 loc) · 4.67 KB
/
lex.yy.part.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define dbg(fmt, arg...) fprintf(stderr, "%s "fmt"\n", __FUNCTION__, ##arg)
FILE *yyout = NULL; /* default output file */
/* error handle */
void yyerrx(char *str)
{
if (errno)
perror(str);
else
fprintf(stderr, "ERROR: %s\n", str);
exit(EXIT_FAILURE);
}
/* yylex() output text stream */
char *yytext;
int yyleng;
int yylineno;
static char *yysmark; /* start mark for yytext */
static char *yyemark; /* end mark for yytext */
char *yy_text(void)
{
return yysmark;
}
int yy_leng(void)
{
return yyemark - yysmark;
}
/* low-level I/O stream */
#define YY_AHEADSIZE 16
#define YY_BUFSIZE 4096
#define YY_LOOKAHEAD (&yyend[-YY_AHEADSIZE])
#define YY_BUF_START (&yybuf[0])
#define YY_BUF_END (&yybuf[YY_BUFSIZE])
static char yybuf[YY_BUFSIZE];
static char *yyend; /* current buffer end */
static char *yypos; /* current position of I/O stream */
static int yyfd = 0; /* default input file is standard input */
static int yyeof;
static int yyreallineno;
static int yyprevlineno;
void yy_init(void)
{
yypos = YY_BUF_START;
*yypos = '\n'; /* first line start anchor `^` */
yyend = YY_BUF_START + 1;
yysmark = yyemark = YY_BUF_START;
yyeof = 0;
yytext = "";
yyleng = 0;
yylineno = 0;
yyreallineno = 0;
/* default */
if (!yyout)
yyout = stdout;
}
int yy_lookahead(void)
{
if (yypos < yyend)
return *yypos;
else
return EOF;
}
void yy_save_buf(void)
{
char *p;
int marklen;
int poslen;
marklen = yyemark - yysmark;
poslen = yypos - yysmark;
p = YY_BUF_START;
while (yysmark < yyend)
*p++ = *yysmark++;
yysmark = YY_BUF_START;
yyemark = yysmark + marklen;
yypos = yysmark + poslen;
yyend = p;
}
void yy_fill_buf(void)
{
int n;
if (yysmark < yyend)
yy_save_buf();
n = read(yyfd, yyend, YY_BUF_END - yyend);
if (n < 0) {
yyerrx("read");
} else if (n > 0) {
yyend += n;
} else {
yyeof = 1;
}
}
void yy_advance(void)
{
if (!yyeof && yypos >= YY_LOOKAHEAD)
yy_fill_buf();
if (yypos < yyend) {
if (*yypos == '\n')
yyreallineno++;
yypos++;
}
}
void yy_move_start(void)
{
yysmark++;
}
void yy_mark_start(void)
{
yyemark = yysmark = yypos;
yylineno = yyreallineno;
}
void yy_mark_end(void)
{
yyprevlineno = yyreallineno;
yyemark = yypos;
}
void yy_back_end(void)
{
yyemark--;
}
void yy_back(void)
{
if (*yypos == '\n')
yyreallineno--;
yypos--;
}
void yy_mark_pos(void)
{
yyreallineno = yyprevlineno;
yypos = yyemark;
}
#define YY_TERMINAL '\0'
static char *yytermpos; /* terminal postion */
static char yytermchar; /* backup char for original terminal postion */
void yy_term(void)
{
yytermpos = yypos;
yytermchar = *yypos;
*yypos = YY_TERMINAL;
}
void yy_unterm(void)
{
if (yytermpos) {
*yytermpos = yytermchar;
yytermchar = YY_TERMINAL;
yytermpos = NULL;
}
}
/* state transition table */
#define yy_next(state, c) com_table[row_map[state]][col_map[c]]
!table
/* accept anchor */
#define YY_NOAC 0
#define YY_AC_START 1
#define YY_AC_END 2
#define YY_AC_BOCH 3
#define YY_AC_NONE 4
!accept
#define YYF (-1)
#ifndef ECHO
#define ECHO() fprintf(yyout, "%s", yytext)
#endif
int yylex(void)
{
static int yyfirst = 1; /* first call yylex() */
int yystate; /* current state */
int yyprev; /* prev state */
int yynstate; /* next state */
int yylastaccept; /* last accept state */
int yyanchor; /* accept anchor */
int yylook; /* gotten char */
if (yyfirst) {
yy_init();
yyfirst = 0;
}
/* init */
yy_unterm();
yystate = 0;
yyanchor = 0;
yylastaccept = -1; /* NOTE: first state(0) can be acceptted */
yy_mark_start();
while (1) {
/* get next state according to lookaheah char */
yylook = yy_lookahead();
if (yylook == EOF) {
if (yylastaccept >= 0)
yynstate = YYF;
else {
/*
* FIXME: some tailing chars will be ignored!
*/
yytext = "";
yyleng = 0;
return 0;
}
} else {
yynstate = yy_next(yystate, yylook);
}
/* dbg("%d -> %d on [%c]", yystate, yynstate, yylook); */
if (yynstate == YYF) {
if (yylastaccept >= 0)
yy_mark_pos();
else
yy_advance();
/* handle ^ , $ operator */
if (yyanchor & YY_AC_START)
/* FIXME: '\n' is ignored */
yy_move_start();
if (yyanchor & YY_AC_END) {
yy_back_end();
yy_back();
}
yy_mark_end();
yy_term();
yytext = yy_text();
yyleng = yy_leng();
/* handle accept action */
switch (yylastaccept) {
!action
default:
ECHO();
break;
}
yy_unterm();
yystate = 0;
yylastaccept = -1;
yyanchor = 0;
yy_mark_start();
} else {
yy_advance();
yyanchor = yyaccept[yynstate];
/* Acceptable? */
if (yyanchor) {
yy_mark_end();
yylastaccept = yynstate;
}
yystate = yynstate;
}
}
}