1 /* Driver template for the LEMON parser generator.
2 ** The author disclaims copyright to this source code.
4 /* First off, code is include which follows the "include" declaration
5 ** in the input file. */
8 /* Next is all token values, in a form suitable for use by makeheaders.
9 ** This section will be null unless lemon is run with the -m switch.
12 ** These constants (all generated automatically by the parser generator)
13 ** specify the various kinds of tokens (terminals) that the parser
16 ** Each symbol here is a terminal symbol in the grammar.
19 /* Make sure the INTERFACE macro is defined.
24 /* The next thing included is series of defines which control
25 ** various aspects of the generated parser.
26 ** YYCODETYPE is the data type used for storing terminal
27 ** and nonterminal numbers. "unsigned char" is
28 ** used if there are fewer than 250 terminals
29 ** and nonterminals. "int" is used otherwise.
30 ** YYNOCODE is a number of type YYCODETYPE which corresponds
31 ** to no legal terminal or nonterminal number. This
32 ** number is used to fill in empty slots of the hash
34 ** YYFALLBACK If defined, this indicates that one or more tokens
35 ** have fall-back values which should be used if the
36 ** original value of the token will not parse.
37 ** YYACTIONTYPE is the data type used for storing terminal
38 ** and nonterminal numbers. "unsigned char" is
39 ** used if there are fewer than 250 rules and
40 ** states combined. "int" is used otherwise.
41 ** ParseTOKENTYPE is the data type used for minor tokens given
42 ** directly to the parser from the tokenizer.
43 ** YYMINORTYPE is the data type used for all minor tokens.
44 ** This is typically a union of many types, one of
45 ** which is ParseTOKENTYPE. The entry in the union
46 ** for base tokens is called "yy0".
47 ** YYSTACKDEPTH is the maximum depth of the parser's stack.
48 ** ParseARG_SDECL A static variable declaration for the %extra_argument
49 ** ParseARG_PDECL A parameter declaration for the %extra_argument
50 ** ParseARG_STORE Code to store %extra_argument into yypParser
51 ** ParseARG_FETCH Code to extract %extra_argument from yypParser
52 ** YYNSTATE the combined number of states.
53 ** YYNRULE the number of rules in the grammar
54 ** YYERRORSYMBOL is the code number of the error symbol. If not
55 ** defined, then do no error processing.
58 #define YY_NO_ACTION (YYNSTATE+YYNRULE+2)
59 #define YY_ACCEPT_ACTION (YYNSTATE+YYNRULE+1)
60 #define YY_ERROR_ACTION (YYNSTATE+YYNRULE)
62 /* Next are that tables used to determine what action to take based on the
63 ** current state and lookahead token. These tables are used to implement
64 ** functions that take a state number and lookahead value and return an
67 ** Suppose the action integer is N. Then the action is determined as
70 ** 0 <= N < YYNSTATE Shift N. That is, push the lookahead
71 ** token onto the stack and goto state N.
73 ** YYNSTATE <= N < YYNSTATE+YYNRULE Reduce by rule N-YYNSTATE.
75 ** N == YYNSTATE+YYNRULE A syntax error has occurred.
77 ** N == YYNSTATE+YYNRULE+1 The parser accepts its input.
79 ** N == YYNSTATE+YYNRULE+2 No such action. Denotes unused
80 ** slots in the yy_action[] table.
82 ** The action table is constructed as a single large table named yy_action[].
83 ** Given state S and lookahead X, the action is computed as
85 ** yy_action[ yy_shift_ofst[S] + X ]
87 ** If the index value yy_shift_ofst[S]+X is out of range or if the value
88 ** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
89 ** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
90 ** and that yy_default[S] should be used instead.
92 ** The formula above is for computing the action when the lookahead is
93 ** a terminal symbol. If the lookahead is a non-terminal (as occurs after
94 ** a reduce action) then the yy_reduce_ofst[] array is used in place of
95 ** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
98 ** The following are the tables generated in this section:
100 ** yy_action[] A single table containing all actions.
101 ** yy_lookahead[] A table containing the lookahead for each entry in
102 ** yy_action. Used to detect hash collisions.
103 ** yy_shift_ofst[] For each state, the offset into yy_action for
104 ** shifting terminals.
105 ** yy_reduce_ofst[] For each state, the offset into yy_action for
106 ** shifting non-terminals after a reduce.
107 ** yy_default[] Default action for each state.
110 #define YY_SZ_ACTTAB (sizeof(yy_action)/sizeof(yy_action[0]))
112 /* The next table maps tokens into fallback tokens. If a construct
113 ** like the following:
115 ** %fallback ID X Y Z.
117 ** appears in the grammer, then ID becomes a fallback token for X, Y,
118 ** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
119 ** but it does not parse, the type of the token is changed to ID and
120 ** the parse is retried before an error is thrown.
123 static const YYCODETYPE yyFallback
[] = {
126 #endif /* YYFALLBACK */
128 /* The following structure represents a single element of the
129 ** parser's stack. Information stored includes:
131 ** + The state number for the parser at this level of the stack.
133 ** + The value of the token stored at this level of the stack.
134 ** (In other words, the "major" token.)
136 ** + The semantic value stored at this level of the stack. This is
137 ** the information used by the action routines in the grammar.
138 ** It is sometimes called the "minor" token.
140 struct yyStackEntry
{
141 int stateno
; /* The state-number */
142 int major
; /* The major token value. This is the code
143 ** number for the token at this stack level */
144 YYMINORTYPE minor
; /* The user-supplied minor token value. This
145 ** is the value of the token */
147 typedef struct yyStackEntry yyStackEntry
;
149 /* The state of the parser is completely contained in an instance of
150 ** the following structure */
152 int yyidx
; /* Index of top element in stack */
153 int yyerrcnt
; /* Shifts left before out of the error */
154 ParseARG_SDECL
/* A place to hold %extra_argument */
155 yyStackEntry yystack
[YYSTACKDEPTH
]; /* The parser's stack */
157 typedef struct yyParser yyParser
;
161 static FILE *yyTraceFILE
= 0;
162 static char *yyTracePrompt
= 0;
167 ** Turn parser tracing on by giving a stream to which to write the trace
168 ** and a prompt to preface each trace message. Tracing is turned off
169 ** by making either argument NULL
173 ** <li> A FILE* to which trace output should be written.
174 ** If NULL, then tracing is turned off.
175 ** <li> A prefix string written at the beginning of every
176 ** line of trace output. If NULL, then tracing is
183 void ParseTrace(FILE *TraceFILE
, char *zTracePrompt
){
184 yyTraceFILE
= TraceFILE
;
185 yyTracePrompt
= zTracePrompt
;
186 if( yyTraceFILE
==0 ) yyTracePrompt
= 0;
187 else if( yyTracePrompt
==0 ) yyTraceFILE
= 0;
192 /* For tracing shifts, the names of all terminals and nonterminals
193 ** are required. The following table supplies these names */
194 static const char *const yyTokenName
[] = {
200 /* For tracing reduce actions, the names of all rules are required.
202 static const char *const yyRuleName
[] = {
208 ** This function returns the symbolic name associated with a token
211 const char *ParseTokenName(int tokenType
){
213 if( tokenType
>0 && tokenType
<(sizeof(yyTokenName
)/sizeof(yyTokenName
[0])) ){
214 return yyTokenName
[tokenType
];
224 ** This function allocates a new parser.
225 ** The only argument is a pointer to a function which works like
229 ** A pointer to the function used to allocate memory.
232 ** A pointer to a parser. This pointer is used in subsequent calls
233 ** to Parse and ParseFree.
235 void *ParseAlloc(void *(*mallocProc
)(size_t)){
237 pParser
= (yyParser
*)(*mallocProc
)( (size_t)sizeof(yyParser
) );
244 /* The following function deletes the value associated with a
245 ** symbol. The symbol can be either a terminal or nonterminal.
246 ** "yymajor" is the symbol code, and "yypminor" is a pointer to
249 static void yy_destructor(YYCODETYPE yymajor
, YYMINORTYPE
*yypminor
){
251 /* Here is inserted the actions which take place when a
252 ** terminal or non-terminal is destroyed. This can happen
253 ** when the symbol is popped from the stack during a
254 ** reduce or during error processing or when a parser is
255 ** being destroyed before it is finished parsing.
257 ** Note: during a reduce, the only symbols destroyed are those
258 ** which appear on the RHS of the rule, but which are not used
259 ** inside the C code.
262 default: break; /* If no destructor action specified: do nothing */
267 ** Pop the parser's stack once.
269 ** If there is a destructor routine associated with the token which
270 ** is popped from the stack, then call it.
272 ** Return the major token number for the symbol popped.
274 static int yy_pop_parser_stack(yyParser
*pParser
){
276 yyStackEntry
*yytos
= &pParser
->yystack
[pParser
->yyidx
];
278 if( pParser
->yyidx
<0 ) return 0;
280 if( yyTraceFILE
&& pParser
->yyidx
>=0 ){
281 fprintf(yyTraceFILE
,"%sPopping %s\n",
283 yyTokenName
[yytos
->major
]);
286 yymajor
= yytos
->major
;
287 yy_destructor( yymajor
, &yytos
->minor
);
293 ** Deallocate and destroy a parser. Destructors are all called for
294 ** all stack elements before shutting the parser down.
298 ** <li> A pointer to the parser. This should be a pointer
299 ** obtained from ParseAlloc.
300 ** <li> A pointer to a function used to reclaim memory obtained
305 void *p
, /* The parser to be deleted */
306 void (*freeProc
)(void*) /* Function used to reclaim memory */
308 yyParser
*pParser
= (yyParser
*)p
;
309 if( pParser
==0 ) return;
310 while( pParser
->yyidx
>=0 ) yy_pop_parser_stack(pParser
);
311 (*freeProc
)((void*)pParser
);
315 ** Find the appropriate action for a parser given the terminal
316 ** look-ahead token iLookAhead.
318 ** If the look-ahead token is YYNOCODE, then check to see if the action is
319 ** independent of the look-ahead. If it is, return the action, otherwise
320 ** return YY_NO_ACTION.
322 static int yy_find_shift_action(
323 yyParser
*pParser
, /* The parser */
324 int iLookAhead
/* The look-ahead token */
327 int stateno
= pParser
->yystack
[pParser
->yyidx
].stateno
;
329 /* if( pParser->yyidx<0 ) return YY_NO_ACTION; */
330 i
= yy_shift_ofst
[stateno
];
331 if( i
==YY_SHIFT_USE_DFLT
){
332 return yy_default
[stateno
];
334 if( iLookAhead
==YYNOCODE
){
338 if( i
<0 || i
>=YY_SZ_ACTTAB
|| yy_lookahead
[i
]!=iLookAhead
){
340 int iFallback
; /* Fallback token */
341 if( iLookAhead
<sizeof(yyFallback
)/sizeof(yyFallback
[0])
342 && (iFallback
= yyFallback
[iLookAhead
])!=0 ){
345 fprintf(yyTraceFILE
, "%sFALLBACK %s => %s\n",
346 yyTracePrompt
, yyTokenName
[iLookAhead
], yyTokenName
[iFallback
]);
349 return yy_find_shift_action(pParser
, iFallback
);
352 return yy_default
[stateno
];
359 ** Find the appropriate action for a parser given the non-terminal
360 ** look-ahead token iLookAhead.
362 ** If the look-ahead token is YYNOCODE, then check to see if the action is
363 ** independent of the look-ahead. If it is, return the action, otherwise
364 ** return YY_NO_ACTION.
366 static int yy_find_reduce_action(
367 int stateno
, /* Current state number */
368 int iLookAhead
/* The look-ahead token */
371 /* int stateno = pParser->yystack[pParser->yyidx].stateno; */
373 i
= yy_reduce_ofst
[stateno
];
374 if( i
==YY_REDUCE_USE_DFLT
){
375 return yy_default
[stateno
];
377 if( iLookAhead
==YYNOCODE
){
381 if( i
<0 || i
>=YY_SZ_ACTTAB
|| yy_lookahead
[i
]!=iLookAhead
){
382 return yy_default
[stateno
];
389 ** Perform a shift action.
391 static void yy_shift(
392 yyParser
*yypParser
, /* The parser to be shifted */
393 int yyNewState
, /* The new state to shift in */
394 int yyMajor
, /* The major token to shift in */
395 YYMINORTYPE
*yypMinor
/* Pointer ot the minor token to shift in */
399 if( yypParser
->yyidx
>=YYSTACKDEPTH
){
404 fprintf(yyTraceFILE
,"%sStack Overflow!\n",yyTracePrompt
);
407 while( yypParser
->yyidx
>=0 ) yy_pop_parser_stack(yypParser
);
408 /* Here code is inserted which will execute if the parser
409 ** stack every overflows */
411 ParseARG_STORE
; /* Suppress warning about unused %extra_argument var */
414 yytos
= &yypParser
->yystack
[yypParser
->yyidx
];
415 yytos
->stateno
= yyNewState
;
416 yytos
->major
= yyMajor
;
417 yytos
->minor
= *yypMinor
;
419 if( yyTraceFILE
&& yypParser
->yyidx
>0 ){
421 fprintf(yyTraceFILE
,"%sShift %d\n",yyTracePrompt
,yyNewState
);
422 fprintf(yyTraceFILE
,"%sStack:",yyTracePrompt
);
423 for(i
=1; i
<=yypParser
->yyidx
; i
++)
424 fprintf(yyTraceFILE
," %s",yyTokenName
[yypParser
->yystack
[i
].major
]);
425 fprintf(yyTraceFILE
,"\n");
430 /* The following table contains information about every rule that
431 ** is used during the reduce.
433 static const struct {
434 YYCODETYPE lhs
; /* Symbol on the left-hand side of the rule */
435 unsigned char nrhs
; /* Number of right-hand side symbols in the rule */
440 static void yy_accept(yyParser
*); /* Forward Declaration */
443 ** Perform a reduce action and the shift that must immediately
444 ** follow the reduce.
446 static void yy_reduce(
447 yyParser
*yypParser
, /* The parser */
448 int yyruleno
/* Number of the rule by which to reduce */
450 int yygoto
; /* The next state */
451 int yyact
; /* The next action */
452 YYMINORTYPE yygotominor
; /* The LHS of the rule reduced */
453 yyStackEntry
*yymsp
; /* The top of the parser's stack */
454 int yysize
; /* Amount to pop the stack */
456 yymsp
= &yypParser
->yystack
[yypParser
->yyidx
];
458 if( yyTraceFILE
&& yyruleno
>=0
459 && yyruleno
<sizeof(yyRuleName
)/sizeof(yyRuleName
[0]) ){
460 fprintf(yyTraceFILE
, "%sReduce [%s].\n", yyTracePrompt
,
461 yyRuleName
[yyruleno
]);
466 /* Silence complaints from purify about yygotominor being uninitialized
467 ** in some cases when it is copied into the stack after the following
468 ** switch. yygotominor is uninitialized when a rule reduces that does
469 ** not set the value of its left-hand side nonterminal. Leaving the
470 ** value of the nonterminal uninitialized is utterly harmless as long
471 ** as the value is never used. So really the only thing this code
472 ** accomplishes is to quieten purify.
474 memset(&yygotominor
, 0, sizeof(yygotominor
));
478 /* Beginning here are the reduction cases. A typical example
481 ** #line <lineno> <grammarfile>
482 ** { ... } // User supplied code
483 ** #line <lineno> <thisfile>
488 yygoto
= yyRuleInfo
[yyruleno
].lhs
;
489 yysize
= yyRuleInfo
[yyruleno
].nrhs
;
490 yypParser
->yyidx
-= yysize
;
491 yyact
= yy_find_reduce_action(yymsp
[-yysize
].stateno
,yygoto
);
492 if( yyact
< YYNSTATE
){
494 /* If we are not debugging and the reduce action popped at least
495 ** one element off the stack, then we can push the new element back
496 ** onto the stack here, and skip the stack overflow test in yy_shift().
497 ** That gives a significant speed improvement. */
501 yymsp
->stateno
= yyact
;
502 yymsp
->major
= yygoto
;
503 yymsp
->minor
= yygotominor
;
507 yy_shift(yypParser
,yyact
,yygoto
,&yygotominor
);
509 }else if( yyact
== YYNSTATE
+ YYNRULE
+ 1 ){
510 yy_accept(yypParser
);
515 ** The following code executes when the parse fails
517 static void yy_parse_failed(
518 yyParser
*yypParser
/* The parser */
523 fprintf(yyTraceFILE
,"%sFail!\n",yyTracePrompt
);
526 while( yypParser
->yyidx
>=0 ) yy_pop_parser_stack(yypParser
);
527 /* Here code is inserted which will be executed whenever the
530 ParseARG_STORE
; /* Suppress warning about unused %extra_argument variable */
534 ** The following code executes when a syntax error first occurs.
536 static void yy_syntax_error(
537 yyParser
*yypParser
, /* The parser */
538 int yymajor
, /* The major type of the error token */
539 YYMINORTYPE yyminor
/* The minor type of the error token */
542 #define TOKEN (yyminor.yy0)
544 ParseARG_STORE
; /* Suppress warning about unused %extra_argument variable */
548 ** The following is executed when the parser accepts
550 static void yy_accept(
551 yyParser
*yypParser
/* The parser */
556 fprintf(yyTraceFILE
,"%sAccept!\n",yyTracePrompt
);
559 while( yypParser
->yyidx
>=0 ) yy_pop_parser_stack(yypParser
);
560 /* Here code is inserted which will be executed whenever the
563 ParseARG_STORE
; /* Suppress warning about unused %extra_argument variable */
566 /* The main parser program.
567 ** The first argument is a pointer to a structure obtained from
568 ** "ParseAlloc" which describes the current state of the parser.
569 ** The second argument is the major token number. The third is
570 ** the minor token. The fourth optional argument is whatever the
571 ** user wants (and specified in the grammar) and is available for
572 ** use by the action routines.
576 ** <li> A pointer to the parser (an opaque structure.)
577 ** <li> The major token number.
578 ** <li> The minor token number.
579 ** <li> An option argument of a grammar-specified type.
586 void *yyp
, /* The parser */
587 int yymajor
, /* The major token code number */
588 ParseTOKENTYPE yyminor
/* The value for the token */
589 ParseARG_PDECL
/* Optional %extra_argument parameter */
591 YYMINORTYPE yyminorunion
;
592 int yyact
; /* The parser action. */
593 int yyendofinput
; /* True if we are at the end of input */
594 int yyerrorhit
= 0; /* True if yymajor has invoked an error */
595 yyParser
*yypParser
; /* The parser */
597 /* (re)initialize the parser, if necessary */
598 yypParser
= (yyParser
*)yyp
;
599 if( yypParser
->yyidx
<0 ){
600 /* if( yymajor==0 ) return; // not sure why this was here... */
601 yypParser
->yyidx
= 0;
602 yypParser
->yyerrcnt
= -1;
603 yypParser
->yystack
[0].stateno
= 0;
604 yypParser
->yystack
[0].major
= 0;
606 yyminorunion
.yy0
= yyminor
;
607 yyendofinput
= (yymajor
==0);
612 fprintf(yyTraceFILE
,"%sInput %s\n",yyTracePrompt
,yyTokenName
[yymajor
]);
617 yyact
= yy_find_shift_action(yypParser
,yymajor
);
618 if( yyact
<YYNSTATE
){
619 yy_shift(yypParser
,yyact
,yymajor
,&yyminorunion
);
620 yypParser
->yyerrcnt
--;
621 if( yyendofinput
&& yypParser
->yyidx
>=0 ){
626 }else if( yyact
< YYNSTATE
+ YYNRULE
){
627 yy_reduce(yypParser
,yyact
-YYNSTATE
);
628 }else if( yyact
== YY_ERROR_ACTION
){
632 fprintf(yyTraceFILE
,"%sSyntax Error!\n",yyTracePrompt
);
636 /* A syntax error has occurred.
637 ** The response to an error depends upon whether or not the
638 ** grammar defines an error token "ERROR".
640 ** This is what we do if the grammar does define ERROR:
642 ** * Call the %syntax_error function.
644 ** * Begin popping the stack until we enter a state where
645 ** it is legal to shift the error symbol, then shift
648 ** * Set the error count to three.
650 ** * Begin accepting and shifting new tokens. No new error
651 ** processing will occur until three tokens have been
652 ** shifted successfully.
655 if( yypParser
->yyerrcnt
<0 ){
656 yy_syntax_error(yypParser
,yymajor
,yyminorunion
);
658 yymx
= yypParser
->yystack
[yypParser
->yyidx
].major
;
659 if( yymx
==YYERRORSYMBOL
|| yyerrorhit
){
662 fprintf(yyTraceFILE
,"%sDiscard input token %s\n",
663 yyTracePrompt
,yyTokenName
[yymajor
]);
666 yy_destructor(yymajor
,&yyminorunion
);
670 yypParser
->yyidx
>= 0 &&
671 yymx
!= YYERRORSYMBOL
&&
672 (yyact
= yy_find_shift_action(yypParser
,YYERRORSYMBOL
)) >= YYNSTATE
674 yy_pop_parser_stack(yypParser
);
676 if( yypParser
->yyidx
< 0 || yymajor
==0 ){
677 yy_destructor(yymajor
,&yyminorunion
);
678 yy_parse_failed(yypParser
);
680 }else if( yymx
!=YYERRORSYMBOL
){
683 yy_shift(yypParser
,yyact
,YYERRORSYMBOL
,&u2
);
686 yypParser
->yyerrcnt
= 3;
688 #else /* YYERRORSYMBOL is not defined */
689 /* This is what we do if the grammar does not define ERROR:
691 ** * Report an error message, and throw away the input token.
693 ** * If the input token is $, then fail the parse.
695 ** As before, subsequent error messages are suppressed until
696 ** three input tokens have been successfully shifted.
698 if( yypParser
->yyerrcnt
<=0 ){
699 yy_syntax_error(yypParser
,yymajor
,yyminorunion
);
701 yypParser
->yyerrcnt
= 3;
702 yy_destructor(yymajor
,&yyminorunion
);
704 yy_parse_failed(yypParser
);
709 yy_accept(yypParser
);
712 }while( yymajor
!=YYNOCODE
&& yypParser
->yyidx
>=0 );