Mercurial > hg > index.cgi
comparison lwcc/parse_c.c @ 314:a3e277c58df9 ccdev
Checkpoint parser development for lwcc
Beginning of lemon based parser for C including all the infrastructure for
calling the lemon generated parser. This requires a translation layer from
the preprocessor token numbers to the lemon parser token numbers due to the
fact that lemon wants to control the token numbers. Eventually when the
lemon parser gets replaced with a hand crafted recursive descent parser,
this translation will no longer be required. However, in the interest of
getting things working sooner rather than later, using something like lemon
is beneficial.
author | William Astle <lost@l-w.ca> |
---|---|
date | Sun, 17 Nov 2013 11:59:36 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
313:73b2bfa17ab0 | 314:a3e277c58df9 |
---|---|
1 /* Driver template for the LEMON parser generator. | |
2 ** The author disclaims copyright to this source code. | |
3 */ | |
4 /* First off, code is included that follows the "include" declaration | |
5 ** in the input grammar file. */ | |
6 #include <stdio.h> | |
7 #line 1 "lwcc/parse_c.y" | |
8 | |
9 #include <assert.h> // only needed due to a bug in lemon | |
10 #include <stdio.h> | |
11 #include "parse.h" | |
12 #include "tree.h" | |
13 #line 14 "lwcc/parse_c.c" | |
14 /* Next is all token values, in a form suitable for use by makeheaders. | |
15 ** This section will be null unless lemon is run with the -m switch. | |
16 */ | |
17 /* | |
18 ** These constants (all generated automatically by the parser generator) | |
19 ** specify the various kinds of tokens (terminals) that the parser | |
20 ** understands. | |
21 ** | |
22 ** Each symbol here is a terminal symbol in the grammar. | |
23 */ | |
24 /* Make sure the INTERFACE macro is defined. | |
25 */ | |
26 #ifndef INTERFACE | |
27 # define INTERFACE 1 | |
28 #endif | |
29 /* The next thing included is series of defines which control | |
30 ** various aspects of the generated parser. | |
31 ** YYCODETYPE is the data type used for storing terminal | |
32 ** and nonterminal numbers. "unsigned char" is | |
33 ** used if there are fewer than 250 terminals | |
34 ** and nonterminals. "int" is used otherwise. | |
35 ** YYNOCODE is a number of type YYCODETYPE which corresponds | |
36 ** to no legal terminal or nonterminal number. This | |
37 ** number is used to fill in empty slots of the hash | |
38 ** table. | |
39 ** YYFALLBACK If defined, this indicates that one or more tokens | |
40 ** have fall-back values which should be used if the | |
41 ** original value of the token will not parse. | |
42 ** YYACTIONTYPE is the data type used for storing terminal | |
43 ** and nonterminal numbers. "unsigned char" is | |
44 ** used if there are fewer than 250 rules and | |
45 ** states combined. "int" is used otherwise. | |
46 ** ParseTOKENTYPE is the data type used for minor tokens given | |
47 ** directly to the parser from the tokenizer. | |
48 ** YYMINORTYPE is the data type used for all minor tokens. | |
49 ** This is typically a union of many types, one of | |
50 ** which is ParseTOKENTYPE. The entry in the union | |
51 ** for base tokens is called "yy0". | |
52 ** YYSTACKDEPTH is the maximum depth of the parser's stack. If | |
53 ** zero the stack is dynamically sized using realloc() | |
54 ** ParseARG_SDECL A static variable declaration for the %extra_argument | |
55 ** ParseARG_PDECL A parameter declaration for the %extra_argument | |
56 ** ParseARG_STORE Code to store %extra_argument into yypParser | |
57 ** ParseARG_FETCH Code to extract %extra_argument from yypParser | |
58 ** YYNSTATE the combined number of states. | |
59 ** YYNRULE the number of rules in the grammar | |
60 ** YYERRORSYMBOL is the code number of the error symbol. If not | |
61 ** defined, then do no error processing. | |
62 */ | |
63 #define YYCODETYPE unsigned char | |
64 #define YYNOCODE 30 | |
65 #define YYACTIONTYPE unsigned char | |
66 #define ParseTOKENTYPE struct tokendata * | |
67 typedef union { | |
68 int yyinit; | |
69 ParseTOKENTYPE yy0; | |
70 node_t * yy18; | |
71 } YYMINORTYPE; | |
72 #ifndef YYSTACKDEPTH | |
73 #define YYSTACKDEPTH 100 | |
74 #endif | |
75 #define ParseARG_SDECL struct parserinfo *pinfo ; | |
76 #define ParseARG_PDECL , struct parserinfo *pinfo | |
77 #define ParseARG_FETCH struct parserinfo *pinfo = yypParser->pinfo | |
78 #define ParseARG_STORE yypParser->pinfo = pinfo | |
79 #define YYNSTATE 36 | |
80 #define YYNRULE 30 | |
81 #define YY_NO_ACTION (YYNSTATE+YYNRULE+2) | |
82 #define YY_ACCEPT_ACTION (YYNSTATE+YYNRULE+1) | |
83 #define YY_ERROR_ACTION (YYNSTATE+YYNRULE) | |
84 | |
85 /* The yyzerominor constant is used to initialize instances of | |
86 ** YYMINORTYPE objects to zero. */ | |
87 static const YYMINORTYPE yyzerominor = { 0 }; | |
88 | |
89 /* Define the yytestcase() macro to be a no-op if is not already defined | |
90 ** otherwise. | |
91 ** | |
92 ** Applications can choose to define yytestcase() in the %include section | |
93 ** to a macro that can assist in verifying code coverage. For production | |
94 ** code the yytestcase() macro should be turned off. But it is useful | |
95 ** for testing. | |
96 */ | |
97 #ifndef yytestcase | |
98 # define yytestcase(X) | |
99 #endif | |
100 | |
101 | |
102 /* Next are the tables used to determine what action to take based on the | |
103 ** current state and lookahead token. These tables are used to implement | |
104 ** functions that take a state number and lookahead value and return an | |
105 ** action integer. | |
106 ** | |
107 ** Suppose the action integer is N. Then the action is determined as | |
108 ** follows | |
109 ** | |
110 ** 0 <= N < YYNSTATE Shift N. That is, push the lookahead | |
111 ** token onto the stack and goto state N. | |
112 ** | |
113 ** YYNSTATE <= N < YYNSTATE+YYNRULE Reduce by rule N-YYNSTATE. | |
114 ** | |
115 ** N == YYNSTATE+YYNRULE A syntax error has occurred. | |
116 ** | |
117 ** N == YYNSTATE+YYNRULE+1 The parser accepts its input. | |
118 ** | |
119 ** N == YYNSTATE+YYNRULE+2 No such action. Denotes unused | |
120 ** slots in the yy_action[] table. | |
121 ** | |
122 ** The action table is constructed as a single large table named yy_action[]. | |
123 ** Given state S and lookahead X, the action is computed as | |
124 ** | |
125 ** yy_action[ yy_shift_ofst[S] + X ] | |
126 ** | |
127 ** If the index value yy_shift_ofst[S]+X is out of range or if the value | |
128 ** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S] | |
129 ** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table | |
130 ** and that yy_default[S] should be used instead. | |
131 ** | |
132 ** The formula above is for computing the action when the lookahead is | |
133 ** a terminal symbol. If the lookahead is a non-terminal (as occurs after | |
134 ** a reduce action) then the yy_reduce_ofst[] array is used in place of | |
135 ** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of | |
136 ** YY_SHIFT_USE_DFLT. | |
137 ** | |
138 ** The following are the tables generated in this section: | |
139 ** | |
140 ** yy_action[] A single table containing all actions. | |
141 ** yy_lookahead[] A table containing the lookahead for each entry in | |
142 ** yy_action. Used to detect hash collisions. | |
143 ** yy_shift_ofst[] For each state, the offset into yy_action for | |
144 ** shifting terminals. | |
145 ** yy_reduce_ofst[] For each state, the offset into yy_action for | |
146 ** shifting non-terminals after a reduce. | |
147 ** yy_default[] Default action for each state. | |
148 */ | |
149 #define YY_ACTTAB_COUNT (44) | |
150 static const YYACTIONTYPE yy_action[] = { | |
151 /* 0 */ 36, 27, 26, 29, 24, 23, 22, 7, 3, 2, | |
152 /* 10 */ 16, 9, 14, 35, 34, 33, 6, 8, 25, 18, | |
153 /* 20 */ 16, 9, 14, 21, 10, 10, 32, 20, 20, 30, | |
154 /* 30 */ 67, 1, 19, 28, 15, 5, 4, 68, 11, 68, | |
155 /* 40 */ 17, 31, 13, 12, | |
156 }; | |
157 static const YYCODETYPE yy_lookahead[] = { | |
158 /* 0 */ 0, 2, 3, 16, 4, 5, 6, 7, 8, 9, | |
159 /* 10 */ 10, 11, 12, 20, 21, 22, 23, 7, 25, 26, | |
160 /* 20 */ 10, 11, 12, 6, 7, 7, 1, 10, 10, 1, | |
161 /* 30 */ 18, 19, 10, 14, 10, 24, 27, 29, 13, 29, | |
162 /* 40 */ 26, 28, 26, 15, | |
163 }; | |
164 #define YY_SHIFT_USE_DFLT (-14) | |
165 #define YY_SHIFT_COUNT (12) | |
166 #define YY_SHIFT_MIN (-13) | |
167 #define YY_SHIFT_MAX (28) | |
168 static const signed char yy_shift_ofst[] = { | |
169 /* 0 */ -14, 0, 10, 10, 28, 25, -1, 17, 18, 24, | |
170 /* 10 */ 22, 19, -13, | |
171 }; | |
172 #define YY_REDUCE_USE_DFLT (-8) | |
173 #define YY_REDUCE_COUNT (6) | |
174 #define YY_REDUCE_MIN (-7) | |
175 #define YY_REDUCE_MAX (16) | |
176 static const signed char yy_reduce_ofst[] = { | |
177 /* 0 */ 12, -7, 16, 14, 13, 9, 11, | |
178 }; | |
179 static const YYACTIONTYPE yy_default[] = { | |
180 /* 0 */ 38, 66, 51, 50, 66, 66, 66, 55, 55, 58, | |
181 /* 10 */ 57, 66, 66, 52, 61, 60, 54, 53, 49, 59, | |
182 /* 20 */ 56, 48, 47, 46, 45, 43, 44, 42, 64, 65, | |
183 /* 30 */ 63, 62, 41, 40, 39, 37, | |
184 }; | |
185 | |
186 /* The next table maps tokens into fallback tokens. If a construct | |
187 ** like the following: | |
188 ** | |
189 ** %fallback ID X Y Z. | |
190 ** | |
191 ** appears in the grammar, then ID becomes a fallback token for X, Y, | |
192 ** and Z. Whenever one of the tokens X, Y, or Z is input to the parser | |
193 ** but it does not parse, the type of the token is changed to ID and | |
194 ** the parse is retried before an error is thrown. | |
195 */ | |
196 #ifdef YYFALLBACK | |
197 static const YYCODETYPE yyFallback[] = { | |
198 }; | |
199 #endif /* YYFALLBACK */ | |
200 | |
201 /* The following structure represents a single element of the | |
202 ** parser's stack. Information stored includes: | |
203 ** | |
204 ** + The state number for the parser at this level of the stack. | |
205 ** | |
206 ** + The value of the token stored at this level of the stack. | |
207 ** (In other words, the "major" token.) | |
208 ** | |
209 ** + The semantic value stored at this level of the stack. This is | |
210 ** the information used by the action routines in the grammar. | |
211 ** It is sometimes called the "minor" token. | |
212 */ | |
213 struct yyStackEntry { | |
214 YYACTIONTYPE stateno; /* The state-number */ | |
215 YYCODETYPE major; /* The major token value. This is the code | |
216 ** number for the token at this stack level */ | |
217 YYMINORTYPE minor; /* The user-supplied minor token value. This | |
218 ** is the value of the token */ | |
219 }; | |
220 typedef struct yyStackEntry yyStackEntry; | |
221 | |
222 /* The state of the parser is completely contained in an instance of | |
223 ** the following structure */ | |
224 struct yyParser { | |
225 int yyidx; /* Index of top element in stack */ | |
226 #ifdef YYTRACKMAXSTACKDEPTH | |
227 int yyidxMax; /* Maximum value of yyidx */ | |
228 #endif | |
229 int yyerrcnt; /* Shifts left before out of the error */ | |
230 ParseARG_SDECL /* A place to hold %extra_argument */ | |
231 #if YYSTACKDEPTH<=0 | |
232 int yystksz; /* Current side of the stack */ | |
233 yyStackEntry *yystack; /* The parser's stack */ | |
234 #else | |
235 yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */ | |
236 #endif | |
237 }; | |
238 typedef struct yyParser yyParser; | |
239 | |
240 #ifndef NDEBUG | |
241 #include <stdio.h> | |
242 static FILE *yyTraceFILE = 0; | |
243 static char *yyTracePrompt = 0; | |
244 #endif /* NDEBUG */ | |
245 | |
246 #ifndef NDEBUG | |
247 /* | |
248 ** Turn parser tracing on by giving a stream to which to write the trace | |
249 ** and a prompt to preface each trace message. Tracing is turned off | |
250 ** by making either argument NULL | |
251 ** | |
252 ** Inputs: | |
253 ** <ul> | |
254 ** <li> A FILE* to which trace output should be written. | |
255 ** If NULL, then tracing is turned off. | |
256 ** <li> A prefix string written at the beginning of every | |
257 ** line of trace output. If NULL, then tracing is | |
258 ** turned off. | |
259 ** </ul> | |
260 ** | |
261 ** Outputs: | |
262 ** None. | |
263 */ | |
264 void ParseTrace(FILE *TraceFILE, char *zTracePrompt){ | |
265 yyTraceFILE = TraceFILE; | |
266 yyTracePrompt = zTracePrompt; | |
267 if( yyTraceFILE==0 ) yyTracePrompt = 0; | |
268 else if( yyTracePrompt==0 ) yyTraceFILE = 0; | |
269 } | |
270 #endif /* NDEBUG */ | |
271 | |
272 #ifndef NDEBUG | |
273 /* For tracing shifts, the names of all terminals and nonterminals | |
274 ** are required. The following table supplies these names */ | |
275 static const char *const yyTokenName[] = { | |
276 "$", "ENDS", "IDENTIFIER", "STAR", | |
277 "KW_VOID", "KW_FLOAT", "KW_DOUBLE", "KW_LONG", | |
278 "KW_UNSIGNED", "KW_SIGNED", "KW_INT", "KW_SHORT", | |
279 "KW_CHAR", "OPAREN", "CPAREN", "OBRACE", | |
280 "CBRACE", "error", "program", "rprogram", | |
281 "globaldecl", "vardecl", "fundecl", "datatype", | |
282 "ident", "typename", "baseint", "arglist", | |
283 "statementblock", | |
284 }; | |
285 #endif /* NDEBUG */ | |
286 | |
287 #ifndef NDEBUG | |
288 /* For tracing reduce actions, the names of all rules are required. | |
289 */ | |
290 static const char *const yyRuleName[] = { | |
291 /* 0 */ "program ::= rprogram", | |
292 /* 1 */ "rprogram ::= rprogram globaldecl", | |
293 /* 2 */ "rprogram ::=", | |
294 /* 3 */ "globaldecl ::= vardecl", | |
295 /* 4 */ "globaldecl ::= fundecl", | |
296 /* 5 */ "vardecl ::= datatype ident ENDS", | |
297 /* 6 */ "ident ::= IDENTIFIER", | |
298 /* 7 */ "datatype ::= typename", | |
299 /* 8 */ "datatype ::= datatype STAR", | |
300 /* 9 */ "typename ::= KW_VOID", | |
301 /* 10 */ "typename ::= KW_FLOAT", | |
302 /* 11 */ "typename ::= KW_DOUBLE", | |
303 /* 12 */ "typename ::= KW_LONG KW_DOUBLE", | |
304 /* 13 */ "typename ::= baseint", | |
305 /* 14 */ "typename ::= KW_UNSIGNED", | |
306 /* 15 */ "typename ::= KW_SIGNED", | |
307 /* 16 */ "typename ::= KW_SIGNED baseint", | |
308 /* 17 */ "typename ::= KW_UNSIGNED baseint", | |
309 /* 18 */ "baseint ::= KW_INT", | |
310 /* 19 */ "baseint ::= KW_LONG", | |
311 /* 20 */ "baseint ::= KW_LONG KW_INT", | |
312 /* 21 */ "baseint ::= KW_LONG KW_LONG", | |
313 /* 22 */ "baseint ::= KW_SHORT", | |
314 /* 23 */ "baseint ::= KW_LONG KW_LONG KW_INT", | |
315 /* 24 */ "baseint ::= KW_SHORT KW_INT", | |
316 /* 25 */ "baseint ::= KW_CHAR", | |
317 /* 26 */ "fundecl ::= datatype ident arglist statementblock", | |
318 /* 27 */ "fundecl ::= datatype ident arglist ENDS", | |
319 /* 28 */ "arglist ::= OPAREN CPAREN", | |
320 /* 29 */ "statementblock ::= OBRACE CBRACE", | |
321 }; | |
322 #endif /* NDEBUG */ | |
323 | |
324 | |
325 #if YYSTACKDEPTH<=0 | |
326 /* | |
327 ** Try to increase the size of the parser stack. | |
328 */ | |
329 static void yyGrowStack(yyParser *p){ | |
330 int newSize; | |
331 yyStackEntry *pNew; | |
332 | |
333 newSize = p->yystksz*2 + 100; | |
334 pNew = realloc(p->yystack, newSize*sizeof(pNew[0])); | |
335 if( pNew ){ | |
336 p->yystack = pNew; | |
337 p->yystksz = newSize; | |
338 #ifndef NDEBUG | |
339 if( yyTraceFILE ){ | |
340 fprintf(yyTraceFILE,"%sStack grows to %d entries!\n", | |
341 yyTracePrompt, p->yystksz); | |
342 } | |
343 #endif | |
344 } | |
345 } | |
346 #endif | |
347 | |
348 /* | |
349 ** This function allocates a new parser. | |
350 ** The only argument is a pointer to a function which works like | |
351 ** malloc. | |
352 ** | |
353 ** Inputs: | |
354 ** A pointer to the function used to allocate memory. | |
355 ** | |
356 ** Outputs: | |
357 ** A pointer to a parser. This pointer is used in subsequent calls | |
358 ** to Parse and ParseFree. | |
359 */ | |
360 void *ParseAlloc(void *(*mallocProc)(size_t)){ | |
361 yyParser *pParser; | |
362 pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) ); | |
363 if( pParser ){ | |
364 pParser->yyidx = -1; | |
365 #ifdef YYTRACKMAXSTACKDEPTH | |
366 pParser->yyidxMax = 0; | |
367 #endif | |
368 #if YYSTACKDEPTH<=0 | |
369 pParser->yystack = NULL; | |
370 pParser->yystksz = 0; | |
371 yyGrowStack(pParser); | |
372 #endif | |
373 } | |
374 return pParser; | |
375 } | |
376 | |
377 /* The following function deletes the value associated with a | |
378 ** symbol. The symbol can be either a terminal or nonterminal. | |
379 ** "yymajor" is the symbol code, and "yypminor" is a pointer to | |
380 ** the value. | |
381 */ | |
382 static void yy_destructor( | |
383 yyParser *yypParser, /* The parser */ | |
384 YYCODETYPE yymajor, /* Type code for object to destroy */ | |
385 YYMINORTYPE *yypminor /* The object to be destroyed */ | |
386 ){ | |
387 ParseARG_FETCH; | |
388 switch( yymajor ){ | |
389 /* Here is inserted the actions which take place when a | |
390 ** terminal or non-terminal is destroyed. This can happen | |
391 ** when the symbol is popped from the stack during a | |
392 ** reduce or during error processing or when a parser is | |
393 ** being destroyed before it is finished parsing. | |
394 ** | |
395 ** Note: during a reduce, the only symbols destroyed are those | |
396 ** which appear on the RHS of the rule, but which are not used | |
397 ** inside the C code. | |
398 */ | |
399 /* TERMINAL Destructor */ | |
400 case 1: /* ENDS */ | |
401 case 2: /* IDENTIFIER */ | |
402 case 3: /* STAR */ | |
403 case 4: /* KW_VOID */ | |
404 case 5: /* KW_FLOAT */ | |
405 case 6: /* KW_DOUBLE */ | |
406 case 7: /* KW_LONG */ | |
407 case 8: /* KW_UNSIGNED */ | |
408 case 9: /* KW_SIGNED */ | |
409 case 10: /* KW_INT */ | |
410 case 11: /* KW_SHORT */ | |
411 case 12: /* KW_CHAR */ | |
412 case 13: /* OPAREN */ | |
413 case 14: /* CPAREN */ | |
414 case 15: /* OBRACE */ | |
415 case 16: /* CBRACE */ | |
416 { | |
417 #line 10 "lwcc/parse_c.y" | |
418 tokendata_free((yypminor->yy0)); | |
419 #line 420 "lwcc/parse_c.c" | |
420 } | |
421 break; | |
422 default: break; /* If no destructor action specified: do nothing */ | |
423 } | |
424 } | |
425 | |
426 /* | |
427 ** Pop the parser's stack once. | |
428 ** | |
429 ** If there is a destructor routine associated with the token which | |
430 ** is popped from the stack, then call it. | |
431 ** | |
432 ** Return the major token number for the symbol popped. | |
433 */ | |
434 static int yy_pop_parser_stack(yyParser *pParser){ | |
435 YYCODETYPE yymajor; | |
436 yyStackEntry *yytos = &pParser->yystack[pParser->yyidx]; | |
437 | |
438 if( pParser->yyidx<0 ) return 0; | |
439 #ifndef NDEBUG | |
440 if( yyTraceFILE && pParser->yyidx>=0 ){ | |
441 fprintf(yyTraceFILE,"%sPopping %s\n", | |
442 yyTracePrompt, | |
443 yyTokenName[yytos->major]); | |
444 } | |
445 #endif | |
446 yymajor = yytos->major; | |
447 yy_destructor(pParser, yymajor, &yytos->minor); | |
448 pParser->yyidx--; | |
449 return yymajor; | |
450 } | |
451 | |
452 /* | |
453 ** Deallocate and destroy a parser. Destructors are all called for | |
454 ** all stack elements before shutting the parser down. | |
455 ** | |
456 ** Inputs: | |
457 ** <ul> | |
458 ** <li> A pointer to the parser. This should be a pointer | |
459 ** obtained from ParseAlloc. | |
460 ** <li> A pointer to a function used to reclaim memory obtained | |
461 ** from malloc. | |
462 ** </ul> | |
463 */ | |
464 void ParseFree( | |
465 void *p, /* The parser to be deleted */ | |
466 void (*freeProc)(void*) /* Function used to reclaim memory */ | |
467 ){ | |
468 yyParser *pParser = (yyParser*)p; | |
469 if( pParser==0 ) return; | |
470 while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser); | |
471 #if YYSTACKDEPTH<=0 | |
472 free(pParser->yystack); | |
473 #endif | |
474 (*freeProc)((void*)pParser); | |
475 } | |
476 | |
477 /* | |
478 ** Return the peak depth of the stack for a parser. | |
479 */ | |
480 #ifdef YYTRACKMAXSTACKDEPTH | |
481 int ParseStackPeak(void *p){ | |
482 yyParser *pParser = (yyParser*)p; | |
483 return pParser->yyidxMax; | |
484 } | |
485 #endif | |
486 | |
487 /* | |
488 ** Find the appropriate action for a parser given the terminal | |
489 ** look-ahead token iLookAhead. | |
490 ** | |
491 ** If the look-ahead token is YYNOCODE, then check to see if the action is | |
492 ** independent of the look-ahead. If it is, return the action, otherwise | |
493 ** return YY_NO_ACTION. | |
494 */ | |
495 static int yy_find_shift_action( | |
496 yyParser *pParser, /* The parser */ | |
497 YYCODETYPE iLookAhead /* The look-ahead token */ | |
498 ){ | |
499 int i; | |
500 int stateno = pParser->yystack[pParser->yyidx].stateno; | |
501 | |
502 if( stateno>YY_SHIFT_COUNT | |
503 || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){ | |
504 return yy_default[stateno]; | |
505 } | |
506 assert( iLookAhead!=YYNOCODE ); | |
507 i += iLookAhead; | |
508 if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){ | |
509 if( iLookAhead>0 ){ | |
510 #ifdef YYFALLBACK | |
511 YYCODETYPE iFallback; /* Fallback token */ | |
512 if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0]) | |
513 && (iFallback = yyFallback[iLookAhead])!=0 ){ | |
514 #ifndef NDEBUG | |
515 if( yyTraceFILE ){ | |
516 fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n", | |
517 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]); | |
518 } | |
519 #endif | |
520 return yy_find_shift_action(pParser, iFallback); | |
521 } | |
522 #endif | |
523 #ifdef YYWILDCARD | |
524 { | |
525 int j = i - iLookAhead + YYWILDCARD; | |
526 if( | |
527 #if YY_SHIFT_MIN+YYWILDCARD<0 | |
528 j>=0 && | |
529 #endif | |
530 #if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT | |
531 j<YY_ACTTAB_COUNT && | |
532 #endif | |
533 yy_lookahead[j]==YYWILDCARD | |
534 ){ | |
535 #ifndef NDEBUG | |
536 if( yyTraceFILE ){ | |
537 fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n", | |
538 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]); | |
539 } | |
540 #endif /* NDEBUG */ | |
541 return yy_action[j]; | |
542 } | |
543 } | |
544 #endif /* YYWILDCARD */ | |
545 } | |
546 return yy_default[stateno]; | |
547 }else{ | |
548 return yy_action[i]; | |
549 } | |
550 } | |
551 | |
552 /* | |
553 ** Find the appropriate action for a parser given the non-terminal | |
554 ** look-ahead token iLookAhead. | |
555 ** | |
556 ** If the look-ahead token is YYNOCODE, then check to see if the action is | |
557 ** independent of the look-ahead. If it is, return the action, otherwise | |
558 ** return YY_NO_ACTION. | |
559 */ | |
560 static int yy_find_reduce_action( | |
561 int stateno, /* Current state number */ | |
562 YYCODETYPE iLookAhead /* The look-ahead token */ | |
563 ){ | |
564 int i; | |
565 #ifdef YYERRORSYMBOL | |
566 if( stateno>YY_REDUCE_COUNT ){ | |
567 return yy_default[stateno]; | |
568 } | |
569 #else | |
570 assert( stateno<=YY_REDUCE_COUNT ); | |
571 #endif | |
572 i = yy_reduce_ofst[stateno]; | |
573 assert( i!=YY_REDUCE_USE_DFLT ); | |
574 assert( iLookAhead!=YYNOCODE ); | |
575 i += iLookAhead; | |
576 #ifdef YYERRORSYMBOL | |
577 if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){ | |
578 return yy_default[stateno]; | |
579 } | |
580 #else | |
581 assert( i>=0 && i<YY_ACTTAB_COUNT ); | |
582 assert( yy_lookahead[i]==iLookAhead ); | |
583 #endif | |
584 return yy_action[i]; | |
585 } | |
586 | |
587 /* | |
588 ** The following routine is called if the stack overflows. | |
589 */ | |
590 static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){ | |
591 ParseARG_FETCH; | |
592 yypParser->yyidx--; | |
593 #ifndef NDEBUG | |
594 if( yyTraceFILE ){ | |
595 fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt); | |
596 } | |
597 #endif | |
598 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser); | |
599 /* Here code is inserted which will execute if the parser | |
600 ** stack every overflows */ | |
601 #line 90 "lwcc/parse_c.y" | |
602 | |
603 fprintf(stderr, "Parser stack overflow\n"); | |
604 #line 605 "lwcc/parse_c.c" | |
605 ParseARG_STORE; /* Suppress warning about unused %extra_argument var */ | |
606 } | |
607 | |
608 /* | |
609 ** Perform a shift action. | |
610 */ | |
611 static void yy_shift( | |
612 yyParser *yypParser, /* The parser to be shifted */ | |
613 int yyNewState, /* The new state to shift in */ | |
614 int yyMajor, /* The major token to shift in */ | |
615 YYMINORTYPE *yypMinor /* Pointer to the minor token to shift in */ | |
616 ){ | |
617 yyStackEntry *yytos; | |
618 yypParser->yyidx++; | |
619 #ifdef YYTRACKMAXSTACKDEPTH | |
620 if( yypParser->yyidx>yypParser->yyidxMax ){ | |
621 yypParser->yyidxMax = yypParser->yyidx; | |
622 } | |
623 #endif | |
624 #if YYSTACKDEPTH>0 | |
625 if( yypParser->yyidx>=YYSTACKDEPTH ){ | |
626 yyStackOverflow(yypParser, yypMinor); | |
627 return; | |
628 } | |
629 #else | |
630 if( yypParser->yyidx>=yypParser->yystksz ){ | |
631 yyGrowStack(yypParser); | |
632 if( yypParser->yyidx>=yypParser->yystksz ){ | |
633 yyStackOverflow(yypParser, yypMinor); | |
634 return; | |
635 } | |
636 } | |
637 #endif | |
638 yytos = &yypParser->yystack[yypParser->yyidx]; | |
639 yytos->stateno = (YYACTIONTYPE)yyNewState; | |
640 yytos->major = (YYCODETYPE)yyMajor; | |
641 yytos->minor = *yypMinor; | |
642 #ifndef NDEBUG | |
643 if( yyTraceFILE && yypParser->yyidx>0 ){ | |
644 int i; | |
645 fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState); | |
646 fprintf(yyTraceFILE,"%sStack:",yyTracePrompt); | |
647 for(i=1; i<=yypParser->yyidx; i++) | |
648 fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]); | |
649 fprintf(yyTraceFILE,"\n"); | |
650 } | |
651 #endif | |
652 } | |
653 | |
654 /* The following table contains information about every rule that | |
655 ** is used during the reduce. | |
656 */ | |
657 static const struct { | |
658 YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */ | |
659 unsigned char nrhs; /* Number of right-hand side symbols in the rule */ | |
660 } yyRuleInfo[] = { | |
661 { 18, 1 }, | |
662 { 19, 2 }, | |
663 { 19, 0 }, | |
664 { 20, 1 }, | |
665 { 20, 1 }, | |
666 { 21, 3 }, | |
667 { 24, 1 }, | |
668 { 23, 1 }, | |
669 { 23, 2 }, | |
670 { 25, 1 }, | |
671 { 25, 1 }, | |
672 { 25, 1 }, | |
673 { 25, 2 }, | |
674 { 25, 1 }, | |
675 { 25, 1 }, | |
676 { 25, 1 }, | |
677 { 25, 2 }, | |
678 { 25, 2 }, | |
679 { 26, 1 }, | |
680 { 26, 1 }, | |
681 { 26, 2 }, | |
682 { 26, 2 }, | |
683 { 26, 1 }, | |
684 { 26, 3 }, | |
685 { 26, 2 }, | |
686 { 26, 1 }, | |
687 { 22, 4 }, | |
688 { 22, 4 }, | |
689 { 27, 2 }, | |
690 { 28, 2 }, | |
691 }; | |
692 | |
693 static void yy_accept(yyParser*); /* Forward Declaration */ | |
694 | |
695 /* | |
696 ** Perform a reduce action and the shift that must immediately | |
697 ** follow the reduce. | |
698 */ | |
699 static void yy_reduce( | |
700 yyParser *yypParser, /* The parser */ | |
701 int yyruleno /* Number of the rule by which to reduce */ | |
702 ){ | |
703 int yygoto; /* The next state */ | |
704 int yyact; /* The next action */ | |
705 YYMINORTYPE yygotominor; /* The LHS of the rule reduced */ | |
706 yyStackEntry *yymsp; /* The top of the parser's stack */ | |
707 int yysize; /* Amount to pop the stack */ | |
708 ParseARG_FETCH; | |
709 yymsp = &yypParser->yystack[yypParser->yyidx]; | |
710 #ifndef NDEBUG | |
711 if( yyTraceFILE && yyruleno>=0 | |
712 && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){ | |
713 fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt, | |
714 yyRuleName[yyruleno]); | |
715 } | |
716 #endif /* NDEBUG */ | |
717 | |
718 /* Silence complaints from purify about yygotominor being uninitialized | |
719 ** in some cases when it is copied into the stack after the following | |
720 ** switch. yygotominor is uninitialized when a rule reduces that does | |
721 ** not set the value of its left-hand side nonterminal. Leaving the | |
722 ** value of the nonterminal uninitialized is utterly harmless as long | |
723 ** as the value is never used. So really the only thing this code | |
724 ** accomplishes is to quieten purify. | |
725 ** | |
726 ** 2007-01-16: The wireshark project (www.wireshark.org) reports that | |
727 ** without this code, their parser segfaults. I'm not sure what there | |
728 ** parser is doing to make this happen. This is the second bug report | |
729 ** from wireshark this week. Clearly they are stressing Lemon in ways | |
730 ** that it has not been previously stressed... (SQLite ticket #2172) | |
731 */ | |
732 /*memset(&yygotominor, 0, sizeof(yygotominor));*/ | |
733 yygotominor = yyzerominor; | |
734 | |
735 | |
736 switch( yyruleno ){ | |
737 /* Beginning here are the reduction cases. A typical example | |
738 ** follows: | |
739 ** case 0: | |
740 ** #line <lineno> <grammarfile> | |
741 ** { ... } // User supplied code | |
742 ** #line <lineno> <thisfile> | |
743 ** break; | |
744 */ | |
745 case 0: /* program ::= rprogram */ | |
746 #line 14 "lwcc/parse_c.y" | |
747 { yygotominor.yy18 = yymsp[0].minor.yy18; pinfo -> parsetree = yygotominor.yy18; } | |
748 #line 749 "lwcc/parse_c.c" | |
749 break; | |
750 case 1: /* rprogram ::= rprogram globaldecl */ | |
751 #line 16 "lwcc/parse_c.y" | |
752 { | |
753 yygotominor.yy18 = yymsp[-1].minor.yy18; | |
754 node_addchild(yygotominor.yy18, yymsp[0].minor.yy18); | |
755 } | |
756 #line 757 "lwcc/parse_c.c" | |
757 break; | |
758 case 2: /* rprogram ::= */ | |
759 #line 20 "lwcc/parse_c.y" | |
760 { yygotominor.yy18 = node_create(NODE_PROGRAM); } | |
761 #line 762 "lwcc/parse_c.c" | |
762 break; | |
763 case 3: /* globaldecl ::= vardecl */ | |
764 case 4: /* globaldecl ::= fundecl */ yytestcase(yyruleno==4); | |
765 case 7: /* datatype ::= typename */ yytestcase(yyruleno==7); | |
766 case 13: /* typename ::= baseint */ yytestcase(yyruleno==13); | |
767 #line 22 "lwcc/parse_c.y" | |
768 { yygotominor.yy18 = yymsp[0].minor.yy18; } | |
769 #line 770 "lwcc/parse_c.c" | |
770 break; | |
771 case 5: /* vardecl ::= datatype ident ENDS */ | |
772 #line 25 "lwcc/parse_c.y" | |
773 { | |
774 yygotominor.yy18 = node_create(NODE_DECL, yymsp[-2].minor.yy18, yymsp[-1].minor.yy18); | |
775 yy_destructor(yypParser,1,&yymsp[0].minor); | |
776 } | |
777 #line 778 "lwcc/parse_c.c" | |
778 break; | |
779 case 6: /* ident ::= IDENTIFIER */ | |
780 #line 29 "lwcc/parse_c.y" | |
781 { yygotominor.yy18 = node_create(NODE_IDENT, yymsp[0].minor.yy0 -> strval); } | |
782 #line 783 "lwcc/parse_c.c" | |
783 break; | |
784 case 8: /* datatype ::= datatype STAR */ | |
785 #line 32 "lwcc/parse_c.y" | |
786 { yygotominor.yy18 = node_create(NODE_TYPE_PTR, yymsp[-1].minor.yy18); yy_destructor(yypParser,3,&yymsp[0].minor); | |
787 } | |
788 #line 789 "lwcc/parse_c.c" | |
789 break; | |
790 case 9: /* typename ::= KW_VOID */ | |
791 #line 34 "lwcc/parse_c.y" | |
792 { yygotominor.yy18 = node_create(NODE_TYPE_VOID); yy_destructor(yypParser,4,&yymsp[0].minor); | |
793 } | |
794 #line 795 "lwcc/parse_c.c" | |
795 break; | |
796 case 10: /* typename ::= KW_FLOAT */ | |
797 #line 35 "lwcc/parse_c.y" | |
798 { yygotominor.yy18 = node_create(NODE_TYPE_FLOAT); yy_destructor(yypParser,5,&yymsp[0].minor); | |
799 } | |
800 #line 801 "lwcc/parse_c.c" | |
801 break; | |
802 case 11: /* typename ::= KW_DOUBLE */ | |
803 #line 36 "lwcc/parse_c.y" | |
804 { yygotominor.yy18 = node_create(NODE_TYPE_DOUBLE); yy_destructor(yypParser,6,&yymsp[0].minor); | |
805 } | |
806 #line 807 "lwcc/parse_c.c" | |
807 break; | |
808 case 12: /* typename ::= KW_LONG KW_DOUBLE */ | |
809 #line 37 "lwcc/parse_c.y" | |
810 { yygotominor.yy18 = node_create(NODE_TYPE_LDOUBLE); yy_destructor(yypParser,7,&yymsp[-1].minor); | |
811 yy_destructor(yypParser,6,&yymsp[0].minor); | |
812 } | |
813 #line 814 "lwcc/parse_c.c" | |
814 break; | |
815 case 14: /* typename ::= KW_UNSIGNED */ | |
816 #line 39 "lwcc/parse_c.y" | |
817 { yygotominor.yy18 = node_create(NODE_TYPE_UINT); yy_destructor(yypParser,8,&yymsp[0].minor); | |
818 } | |
819 #line 820 "lwcc/parse_c.c" | |
820 break; | |
821 case 15: /* typename ::= KW_SIGNED */ | |
822 #line 40 "lwcc/parse_c.y" | |
823 { yygotominor.yy18 = node_create(NODE_TYPE_INT); yy_destructor(yypParser,9,&yymsp[0].minor); | |
824 } | |
825 #line 826 "lwcc/parse_c.c" | |
826 break; | |
827 case 16: /* typename ::= KW_SIGNED baseint */ | |
828 #line 41 "lwcc/parse_c.y" | |
829 { yygotominor.yy18 = yymsp[0].minor.yy18; if (yygotominor.yy18 -> type == NODE_TYPE_CHAR) yygotominor.yy18 -> type = NODE_TYPE_SCHAR; yy_destructor(yypParser,9,&yymsp[-1].minor); | |
830 } | |
831 #line 832 "lwcc/parse_c.c" | |
832 break; | |
833 case 17: /* typename ::= KW_UNSIGNED baseint */ | |
834 #line 42 "lwcc/parse_c.y" | |
835 { | |
836 yygotominor.yy18 = yymsp[0].minor.yy18; | |
837 switch (yygotominor.yy18 -> type) | |
838 { | |
839 case NODE_TYPE_CHAR: | |
840 yygotominor.yy18 -> type = NODE_TYPE_UCHAR; | |
841 break; | |
842 case NODE_TYPE_SHORT: | |
843 yygotominor.yy18 -> type = NODE_TYPE_USHORT; | |
844 break; | |
845 case NODE_TYPE_INT: | |
846 yygotominor.yy18 -> type = NODE_TYPE_UINT; | |
847 break; | |
848 case NODE_TYPE_LONG: | |
849 yygotominor.yy18 -> type = NODE_TYPE_ULONG; | |
850 break; | |
851 case NODE_TYPE_LONGLONG: | |
852 yygotominor.yy18 -> type = NODE_TYPE_ULONGLONG; | |
853 break; | |
854 } | |
855 yy_destructor(yypParser,8,&yymsp[-1].minor); | |
856 } | |
857 #line 858 "lwcc/parse_c.c" | |
858 break; | |
859 case 18: /* baseint ::= KW_INT */ | |
860 #line 64 "lwcc/parse_c.y" | |
861 { yygotominor.yy18 = node_create(NODE_TYPE_INT); yy_destructor(yypParser,10,&yymsp[0].minor); | |
862 } | |
863 #line 864 "lwcc/parse_c.c" | |
864 break; | |
865 case 19: /* baseint ::= KW_LONG */ | |
866 #line 65 "lwcc/parse_c.y" | |
867 { yygotominor.yy18 = node_create(NODE_TYPE_LONG); yy_destructor(yypParser,7,&yymsp[0].minor); | |
868 } | |
869 #line 870 "lwcc/parse_c.c" | |
870 break; | |
871 case 20: /* baseint ::= KW_LONG KW_INT */ | |
872 #line 66 "lwcc/parse_c.y" | |
873 { yygotominor.yy18 = node_create(NODE_TYPE_LONG); yy_destructor(yypParser,7,&yymsp[-1].minor); | |
874 yy_destructor(yypParser,10,&yymsp[0].minor); | |
875 } | |
876 #line 877 "lwcc/parse_c.c" | |
877 break; | |
878 case 21: /* baseint ::= KW_LONG KW_LONG */ | |
879 #line 67 "lwcc/parse_c.y" | |
880 { yygotominor.yy18 = node_create(NODE_TYPE_LONGLONG); yy_destructor(yypParser,7,&yymsp[-1].minor); | |
881 yy_destructor(yypParser,7,&yymsp[0].minor); | |
882 } | |
883 #line 884 "lwcc/parse_c.c" | |
884 break; | |
885 case 22: /* baseint ::= KW_SHORT */ | |
886 #line 68 "lwcc/parse_c.y" | |
887 { yygotominor.yy18 = node_create(NODE_TYPE_SHORT); yy_destructor(yypParser,11,&yymsp[0].minor); | |
888 } | |
889 #line 890 "lwcc/parse_c.c" | |
890 break; | |
891 case 23: /* baseint ::= KW_LONG KW_LONG KW_INT */ | |
892 #line 69 "lwcc/parse_c.y" | |
893 { yygotominor.yy18 = node_create(NODE_TYPE_LONGLONG); yy_destructor(yypParser,7,&yymsp[-2].minor); | |
894 yy_destructor(yypParser,7,&yymsp[-1].minor); | |
895 yy_destructor(yypParser,10,&yymsp[0].minor); | |
896 } | |
897 #line 898 "lwcc/parse_c.c" | |
898 break; | |
899 case 24: /* baseint ::= KW_SHORT KW_INT */ | |
900 #line 70 "lwcc/parse_c.y" | |
901 { yygotominor.yy18 = node_create(NODE_TYPE_SHORT); yy_destructor(yypParser,11,&yymsp[-1].minor); | |
902 yy_destructor(yypParser,10,&yymsp[0].minor); | |
903 } | |
904 #line 905 "lwcc/parse_c.c" | |
905 break; | |
906 case 25: /* baseint ::= KW_CHAR */ | |
907 #line 71 "lwcc/parse_c.y" | |
908 { yygotominor.yy18 = node_create(NODE_TYPE_CHAR); yy_destructor(yypParser,12,&yymsp[0].minor); | |
909 } | |
910 #line 911 "lwcc/parse_c.c" | |
911 break; | |
912 case 26: /* fundecl ::= datatype ident arglist statementblock */ | |
913 #line 74 "lwcc/parse_c.y" | |
914 { | |
915 yygotominor.yy18 = node_create(NODE_FUNDEF, yymsp[-3].minor.yy18, yymsp[-2].minor.yy18, yymsp[-1].minor.yy18, yymsp[0].minor.yy18); | |
916 } | |
917 #line 918 "lwcc/parse_c.c" | |
918 break; | |
919 case 27: /* fundecl ::= datatype ident arglist ENDS */ | |
920 #line 78 "lwcc/parse_c.y" | |
921 { | |
922 yygotominor.yy18 = node_create(NODE_FUNDECL, yymsp[-3].minor.yy18, yymsp[-2].minor.yy18, yymsp[-1].minor.yy18); | |
923 yy_destructor(yypParser,1,&yymsp[0].minor); | |
924 } | |
925 #line 926 "lwcc/parse_c.c" | |
926 break; | |
927 case 28: /* arglist ::= OPAREN CPAREN */ | |
928 #line 82 "lwcc/parse_c.y" | |
929 { yygotominor.yy18 = node_create(NODE_FUNARGS); yy_destructor(yypParser,13,&yymsp[-1].minor); | |
930 yy_destructor(yypParser,14,&yymsp[0].minor); | |
931 } | |
932 #line 933 "lwcc/parse_c.c" | |
933 break; | |
934 case 29: /* statementblock ::= OBRACE CBRACE */ | |
935 #line 84 "lwcc/parse_c.y" | |
936 { yygotominor.yy18 = node_create(NODE_BLOCK); yy_destructor(yypParser,15,&yymsp[-1].minor); | |
937 yy_destructor(yypParser,16,&yymsp[0].minor); | |
938 } | |
939 #line 940 "lwcc/parse_c.c" | |
940 break; | |
941 default: | |
942 break; | |
943 }; | |
944 yygoto = yyRuleInfo[yyruleno].lhs; | |
945 yysize = yyRuleInfo[yyruleno].nrhs; | |
946 yypParser->yyidx -= yysize; | |
947 yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto); | |
948 if( yyact < YYNSTATE ){ | |
949 #ifdef NDEBUG | |
950 /* If we are not debugging and the reduce action popped at least | |
951 ** one element off the stack, then we can push the new element back | |
952 ** onto the stack here, and skip the stack overflow test in yy_shift(). | |
953 ** That gives a significant speed improvement. */ | |
954 if( yysize ){ | |
955 yypParser->yyidx++; | |
956 yymsp -= yysize-1; | |
957 yymsp->stateno = (YYACTIONTYPE)yyact; | |
958 yymsp->major = (YYCODETYPE)yygoto; | |
959 yymsp->minor = yygotominor; | |
960 }else | |
961 #endif | |
962 { | |
963 yy_shift(yypParser,yyact,yygoto,&yygotominor); | |
964 } | |
965 }else{ | |
966 assert( yyact == YYNSTATE + YYNRULE + 1 ); | |
967 yy_accept(yypParser); | |
968 } | |
969 } | |
970 | |
971 /* | |
972 ** The following code executes when the parse fails | |
973 */ | |
974 #ifndef YYNOERRORRECOVERY | |
975 static void yy_parse_failed( | |
976 yyParser *yypParser /* The parser */ | |
977 ){ | |
978 ParseARG_FETCH; | |
979 #ifndef NDEBUG | |
980 if( yyTraceFILE ){ | |
981 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt); | |
982 } | |
983 #endif | |
984 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser); | |
985 /* Here code is inserted which will be executed whenever the | |
986 ** parser fails */ | |
987 #line 86 "lwcc/parse_c.y" | |
988 | |
989 fprintf(stderr, "Parse error\n"); | |
990 #line 991 "lwcc/parse_c.c" | |
991 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ | |
992 } | |
993 #endif /* YYNOERRORRECOVERY */ | |
994 | |
995 /* | |
996 ** The following code executes when a syntax error first occurs. | |
997 */ | |
998 static void yy_syntax_error( | |
999 yyParser *yypParser, /* The parser */ | |
1000 int yymajor, /* The major type of the error token */ | |
1001 YYMINORTYPE yyminor /* The minor type of the error token */ | |
1002 ){ | |
1003 ParseARG_FETCH; | |
1004 #define TOKEN (yyminor.yy0) | |
1005 #line 94 "lwcc/parse_c.y" | |
1006 | |
1007 fprintf(stderr, "Undexpected token %d: ", TOKEN -> tokid); | |
1008 tokendata_print(stderr, TOKEN); | |
1009 #line 1010 "lwcc/parse_c.c" | |
1010 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ | |
1011 } | |
1012 | |
1013 /* | |
1014 ** The following is executed when the parser accepts | |
1015 */ | |
1016 static void yy_accept( | |
1017 yyParser *yypParser /* The parser */ | |
1018 ){ | |
1019 ParseARG_FETCH; | |
1020 #ifndef NDEBUG | |
1021 if( yyTraceFILE ){ | |
1022 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt); | |
1023 } | |
1024 #endif | |
1025 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser); | |
1026 /* Here code is inserted which will be executed whenever the | |
1027 ** parser accepts */ | |
1028 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ | |
1029 } | |
1030 | |
1031 /* The main parser program. | |
1032 ** The first argument is a pointer to a structure obtained from | |
1033 ** "ParseAlloc" which describes the current state of the parser. | |
1034 ** The second argument is the major token number. The third is | |
1035 ** the minor token. The fourth optional argument is whatever the | |
1036 ** user wants (and specified in the grammar) and is available for | |
1037 ** use by the action routines. | |
1038 ** | |
1039 ** Inputs: | |
1040 ** <ul> | |
1041 ** <li> A pointer to the parser (an opaque structure.) | |
1042 ** <li> The major token number. | |
1043 ** <li> The minor token number. | |
1044 ** <li> An option argument of a grammar-specified type. | |
1045 ** </ul> | |
1046 ** | |
1047 ** Outputs: | |
1048 ** None. | |
1049 */ | |
1050 void Parse( | |
1051 void *yyp, /* The parser */ | |
1052 int yymajor, /* The major token code number */ | |
1053 ParseTOKENTYPE yyminor /* The value for the token */ | |
1054 ParseARG_PDECL /* Optional %extra_argument parameter */ | |
1055 ){ | |
1056 YYMINORTYPE yyminorunion; | |
1057 int yyact; /* The parser action. */ | |
1058 int yyendofinput; /* True if we are at the end of input */ | |
1059 #ifdef YYERRORSYMBOL | |
1060 int yyerrorhit = 0; /* True if yymajor has invoked an error */ | |
1061 #endif | |
1062 yyParser *yypParser; /* The parser */ | |
1063 | |
1064 /* (re)initialize the parser, if necessary */ | |
1065 yypParser = (yyParser*)yyp; | |
1066 if( yypParser->yyidx<0 ){ | |
1067 #if YYSTACKDEPTH<=0 | |
1068 if( yypParser->yystksz <=0 ){ | |
1069 /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/ | |
1070 yyminorunion = yyzerominor; | |
1071 yyStackOverflow(yypParser, &yyminorunion); | |
1072 return; | |
1073 } | |
1074 #endif | |
1075 yypParser->yyidx = 0; | |
1076 yypParser->yyerrcnt = -1; | |
1077 yypParser->yystack[0].stateno = 0; | |
1078 yypParser->yystack[0].major = 0; | |
1079 } | |
1080 yyminorunion.yy0 = yyminor; | |
1081 yyendofinput = (yymajor==0); | |
1082 ParseARG_STORE; | |
1083 | |
1084 #ifndef NDEBUG | |
1085 if( yyTraceFILE ){ | |
1086 fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]); | |
1087 } | |
1088 #endif | |
1089 | |
1090 do{ | |
1091 yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor); | |
1092 if( yyact<YYNSTATE ){ | |
1093 assert( !yyendofinput ); /* Impossible to shift the $ token */ | |
1094 yy_shift(yypParser,yyact,yymajor,&yyminorunion); | |
1095 yypParser->yyerrcnt--; | |
1096 yymajor = YYNOCODE; | |
1097 }else if( yyact < YYNSTATE + YYNRULE ){ | |
1098 yy_reduce(yypParser,yyact-YYNSTATE); | |
1099 }else{ | |
1100 assert( yyact == YY_ERROR_ACTION ); | |
1101 #ifdef YYERRORSYMBOL | |
1102 int yymx; | |
1103 #endif | |
1104 #ifndef NDEBUG | |
1105 if( yyTraceFILE ){ | |
1106 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt); | |
1107 } | |
1108 #endif | |
1109 #ifdef YYERRORSYMBOL | |
1110 /* A syntax error has occurred. | |
1111 ** The response to an error depends upon whether or not the | |
1112 ** grammar defines an error token "ERROR". | |
1113 ** | |
1114 ** This is what we do if the grammar does define ERROR: | |
1115 ** | |
1116 ** * Call the %syntax_error function. | |
1117 ** | |
1118 ** * Begin popping the stack until we enter a state where | |
1119 ** it is legal to shift the error symbol, then shift | |
1120 ** the error symbol. | |
1121 ** | |
1122 ** * Set the error count to three. | |
1123 ** | |
1124 ** * Begin accepting and shifting new tokens. No new error | |
1125 ** processing will occur until three tokens have been | |
1126 ** shifted successfully. | |
1127 ** | |
1128 */ | |
1129 if( yypParser->yyerrcnt<0 ){ | |
1130 yy_syntax_error(yypParser,yymajor,yyminorunion); | |
1131 } | |
1132 yymx = yypParser->yystack[yypParser->yyidx].major; | |
1133 if( yymx==YYERRORSYMBOL || yyerrorhit ){ | |
1134 #ifndef NDEBUG | |
1135 if( yyTraceFILE ){ | |
1136 fprintf(yyTraceFILE,"%sDiscard input token %s\n", | |
1137 yyTracePrompt,yyTokenName[yymajor]); | |
1138 } | |
1139 #endif | |
1140 yy_destructor(yypParser, (YYCODETYPE)yymajor,&yyminorunion); | |
1141 yymajor = YYNOCODE; | |
1142 }else{ | |
1143 while( | |
1144 yypParser->yyidx >= 0 && | |
1145 yymx != YYERRORSYMBOL && | |
1146 (yyact = yy_find_reduce_action( | |
1147 yypParser->yystack[yypParser->yyidx].stateno, | |
1148 YYERRORSYMBOL)) >= YYNSTATE | |
1149 ){ | |
1150 yy_pop_parser_stack(yypParser); | |
1151 } | |
1152 if( yypParser->yyidx < 0 || yymajor==0 ){ | |
1153 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); | |
1154 yy_parse_failed(yypParser); | |
1155 yymajor = YYNOCODE; | |
1156 }else if( yymx!=YYERRORSYMBOL ){ | |
1157 YYMINORTYPE u2; | |
1158 u2.YYERRSYMDT = 0; | |
1159 yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2); | |
1160 } | |
1161 } | |
1162 yypParser->yyerrcnt = 3; | |
1163 yyerrorhit = 1; | |
1164 #elif defined(YYNOERRORRECOVERY) | |
1165 /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to | |
1166 ** do any kind of error recovery. Instead, simply invoke the syntax | |
1167 ** error routine and continue going as if nothing had happened. | |
1168 ** | |
1169 ** Applications can set this macro (for example inside %include) if | |
1170 ** they intend to abandon the parse upon the first syntax error seen. | |
1171 */ | |
1172 yy_syntax_error(yypParser,yymajor,yyminorunion); | |
1173 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); | |
1174 yymajor = YYNOCODE; | |
1175 | |
1176 #else /* YYERRORSYMBOL is not defined */ | |
1177 /* This is what we do if the grammar does not define ERROR: | |
1178 ** | |
1179 ** * Report an error message, and throw away the input token. | |
1180 ** | |
1181 ** * If the input token is $, then fail the parse. | |
1182 ** | |
1183 ** As before, subsequent error messages are suppressed until | |
1184 ** three input tokens have been successfully shifted. | |
1185 */ | |
1186 if( yypParser->yyerrcnt<=0 ){ | |
1187 yy_syntax_error(yypParser,yymajor,yyminorunion); | |
1188 } | |
1189 yypParser->yyerrcnt = 3; | |
1190 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); | |
1191 if( yyendofinput ){ | |
1192 yy_parse_failed(yypParser); | |
1193 } | |
1194 yymajor = YYNOCODE; | |
1195 #endif | |
1196 } | |
1197 }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 ); | |
1198 return; | |
1199 } |