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. */
9 /* Next is all token values, in a form suitable for use by makeheaders.
10 ** This section will be null unless lemon is run with the -m switch.
13 ** These constants (all generated automatically by the parser generator)
14 ** specify the various kinds of tokens (terminals) that the parser
17 ** Each symbol here is a terminal symbol in the grammar.
20 /* Make sure the INTERFACE macro is defined.
25 /* The next thing included is series of defines which control
26 ** various aspects of the generated parser.
27 ** YYCODETYPE is the data type used for storing terminal
28 ** and nonterminal numbers. "unsigned char" is
29 ** used if there are fewer than 250 terminals
30 ** and nonterminals. "int" is used otherwise.
31 ** YYNOCODE is a number of type YYCODETYPE which corresponds
32 ** to no legal terminal or nonterminal number. This
33 ** number is used to fill in empty slots of the hash
35 ** YYFALLBACK If defined, this indicates that one or more tokens
36 ** have fall-back values which should be used if the
37 ** original value of the token will not parse.
38 ** YYACTIONTYPE is the data type used for storing terminal
39 ** and nonterminal numbers. "unsigned char" is
40 ** used if there are fewer than 250 rules and
41 ** states combined. "int" is used otherwise.
42 ** ParseTOKENTYPE is the data type used for minor tokens given
43 ** directly to the parser from the tokenizer.
44 ** YYMINORTYPE is the data type used for all minor tokens.
45 ** This is typically a union of many types, one of
46 ** which is ParseTOKENTYPE. The entry in the union
47 ** for base tokens is called "yy0".
48 ** YYSTACKDEPTH is the maximum depth of the parser's stack.
49 ** ParseARG_SDECL A static variable declaration for the %extra_argument
50 ** ParseARG_PDECL A parameter declaration for the %extra_argument
51 ** ParseARG_STORE Code to store %extra_argument into yypParser
52 ** ParseARG_FETCH Code to extract %extra_argument from yypParser
53 ** YYNSTATE the combined number of states.
54 ** YYNRULE the number of rules in the grammar
55 ** YYERRORSYMBOL is the code number of the error symbol. If not
56 ** defined, then do no error processing.
59 #define YY_NO_ACTION (YYNSTATE+YYNRULE+2)
60 #define YY_ACCEPT_ACTION (YYNSTATE+YYNRULE+1)
61 #define YY_ERROR_ACTION (YYNSTATE+YYNRULE)
63 /* Next are that tables used to determine what action to take based on the
64 ** current state and lookahead token. These tables are used to implement
65 ** functions that take a state number and lookahead value and return an
68 ** Suppose the action integer is N. Then the action is determined as
71 ** 0 <= N < YYNSTATE Shift N. That is, push the lookahead
72 ** token onto the stack and goto state N.
74 ** YYNSTATE <= N < YYNSTATE+YYNRULE Reduce by rule N-YYNSTATE.
76 ** N == YYNSTATE+YYNRULE A syntax error has occurred.
78 ** N == YYNSTATE+YYNRULE+1 The parser accepts its input.
80 ** N == YYNSTATE+YYNRULE+2 No such action. Denotes unused
81 ** slots in the yy_action[] table.
83 ** The action table is constructed as a single large table named yy_action[].
84 ** Given state S and lookahead X, the action is computed as
86 ** yy_action[ yy_shift_ofst[S] + X ]
88 ** If the index value yy_shift_ofst[S]+X is out of range or if the value
89 ** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
90 ** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
91 ** and that yy_default[S] should be used instead.
93 ** The formula above is for computing the action when the lookahead is
94 ** a terminal symbol. If the lookahead is a non-terminal (as occurs after
95 ** a reduce action) then the yy_reduce_ofst[] array is used in place of
96 ** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
99 ** The following are the tables generated in this section:
101 ** yy_action[] A single table containing all actions.
102 ** yy_lookahead[] A table containing the lookahead for each entry in
103 ** yy_action. Used to detect hash collisions.
104 ** yy_shift_ofst[] For each state, the offset into yy_action for
105 ** shifting terminals.
106 ** yy_reduce_ofst[] For each state, the offset into yy_action for
107 ** shifting non-terminals after a reduce.
108 ** yy_default[] Default action for each state.
111 #define YY_SZ_ACTTAB (sizeof(yy_action)/sizeof(yy_action[0]))
113 /* The next table maps tokens into fallback tokens. If a construct
114 ** like the following:
116 ** %fallback ID X Y Z.
118 ** appears in the grammer, then ID becomes a fallback token for X, Y,
119 ** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
120 ** but it does not parse, the type of the token is changed to ID and
121 ** the parse is retried before an error is thrown.
124 static const YYCODETYPE yyFallback
[] = {
127 #endif /* YYFALLBACK */
129 /* The following structure represents a single element of the
130 ** parser's stack. Information stored includes:
132 ** + The state number for the parser at this level of the stack.
134 ** + The value of the token stored at this level of the stack.
135 ** (In other words, the "major" token.)
137 ** + The semantic value stored at this level of the stack. This is
138 ** the information used by the action routines in the grammar.
139 ** It is sometimes called the "minor" token.
141 struct yyStackEntry
{
142 int stateno
; /* The state-number */
143 int major
; /* The major token value. This is the code
144 ** number for the token at this stack level */
145 YYMINORTYPE minor
; /* The user-supplied minor token value. This
146 ** is the value of the token */
148 typedef struct yyStackEntry yyStackEntry
;
150 /* The state of the parser is completely contained in an instance of
151 ** the following structure */
153 int yyidx
; /* Index of top element in stack */
154 int yyerrcnt
; /* Shifts left before out of the error */
155 ParseARG_SDECL
/* A place to hold %extra_argument */
156 yyStackEntry yystack
[YYSTACKDEPTH
]; /* The parser's stack */
158 typedef struct yyParser yyParser
;
162 static FILE *yyTraceFILE
= NULL
;
163 static char *yyTracePrompt
= NULL
;
168 ** Turn parser tracing on by giving a stream to which to write the trace
169 ** and a prompt to preface each trace message. Tracing is turned off
170 ** by making either argument NULL
174 ** <li> A FILE* to which trace output should be written.
175 ** If NULL, then tracing is turned off.
176 ** <li> A prefix string written at the beginning of every
177 ** line of trace output. If NULL, then tracing is
185 void ParseTrace(FILE *TraceFILE
, char *zTracePrompt
){
186 yyTraceFILE
= TraceFILE
;
187 yyTracePrompt
= zTracePrompt
;
188 if( yyTraceFILE
==0 ) yyTracePrompt
= 0;
189 else if( yyTracePrompt
==0 ) yyTraceFILE
= 0;
195 /* For tracing shifts, the names of all terminals and nonterminals
196 ** are required. The following table supplies these names */
197 static const char *yyTokenName
[] = {
203 /* For tracing reduce actions, the names of all rules are required.
205 static const char *yyRuleName
[] = {
211 ** This function returns the symbolic name associated with a token
215 const char *ParseTokenName(int tokenType
){
217 if( tokenType
>0 && (size_t)tokenType
<(sizeof(yyTokenName
)/sizeof(yyTokenName
[0])) ){
218 return yyTokenName
[tokenType
];
229 ** This function allocates a new parser.
230 ** The only argument is a pointer to a function which works like
234 ** A pointer to the function used to allocate memory.
237 ** A pointer to a parser. This pointer is used in subsequent calls
238 ** to Parse and ParseFree.
240 void *ParseAlloc(void *(*mallocProc
)(size_t)){
242 pParser
= (yyParser
*)(*mallocProc
)( (size_t)sizeof(yyParser
) );
249 /* The following function deletes the value associated with a
250 ** symbol. The symbol can be either a terminal or nonterminal.
251 ** "yymajor" is the symbol code, and "yypminor" is a pointer to
254 static void yy_destructor(YYCODETYPE yymajor
, YYMINORTYPE
*yypminor
){
256 /* Here is inserted the actions which take place when a
257 ** terminal or non-terminal is destroyed. This can happen
258 ** when the symbol is popped from the stack during a
259 ** reduce or during error processing or when a parser is
260 ** being destroyed before it is finished parsing.
262 ** Note: during a reduce, the only symbols destroyed are those
263 ** which appear on the RHS of the rule, but which are not used
264 ** inside the C code.
267 default: break; /* If no destructor action specified: do nothing */
272 ** Pop the parser's stack once.
274 ** If there is a destructor routine associated with the token which
275 ** is popped from the stack, then call it.
277 ** Return the major token number for the symbol popped.
279 static int yy_pop_parser_stack(yyParser
*pParser
){
281 yyStackEntry
*yytos
= &pParser
->yystack
[pParser
->yyidx
];
283 if( pParser
->yyidx
<0 ) return 0;
285 if( yyTraceFILE
&& pParser
->yyidx
>=0 ){
286 fprintf(yyTraceFILE
,"%sPopping %s\n",
288 yyTokenName
[yytos
->major
]);
291 yymajor
= yytos
->major
;
292 yy_destructor( yymajor
, &yytos
->minor
);
298 ** Deallocate and destroy a parser. Destructors are all called for
299 ** all stack elements before shutting the parser down.
303 ** <li> A pointer to the parser. This should be a pointer
304 ** obtained from ParseAlloc.
305 ** <li> A pointer to a function used to reclaim memory obtained
310 void *p
, /* The parser to be deleted */
311 void (*freeProc
)(void*) /* Function used to reclaim memory */
313 yyParser
*pParser
= (yyParser
*)p
;
314 if( pParser
==NULL
) return;
315 while( pParser
->yyidx
>=0 ) yy_pop_parser_stack(pParser
);
316 (*freeProc
)((void*)pParser
);
320 ** Find the appropriate action for a parser given the terminal
321 ** look-ahead token iLookAhead.
323 ** If the look-ahead token is YYNOCODE, then check to see if the action is
324 ** independent of the look-ahead. If it is, return the action, otherwise
325 ** return YY_NO_ACTION.
327 static int yy_find_shift_action(
328 yyParser
*pParser
, /* The parser */
329 int iLookAhead
/* The look-ahead token */
332 int stateno
= pParser
->yystack
[pParser
->yyidx
].stateno
;
334 /* if( pParser->yyidx<0 ) return YY_NO_ACTION; */
335 i
= yy_shift_ofst
[stateno
];
336 if( i
==YY_SHIFT_USE_DFLT
){
337 return yy_default
[stateno
];
339 if( iLookAhead
==YYNOCODE
){
343 if( i
<0 || (size_t)i
>=YY_SZ_ACTTAB
|| yy_lookahead
[i
]!=iLookAhead
){
345 int iFallback
; /* Fallback token */
346 if( iLookAhead
<sizeof(yyFallback
)/sizeof(yyFallback
[0])
347 && (iFallback
= yyFallback
[iLookAhead
])!=0 ){
350 fprintf(yyTraceFILE
, "%sFALLBACK %s => %s\n",
351 yyTracePrompt
, yyTokenName
[iLookAhead
], yyTokenName
[iFallback
]);
354 return yy_find_shift_action(pParser
, iFallback
);
357 return yy_default
[stateno
];
364 ** Find the appropriate action for a parser given the non-terminal
365 ** look-ahead token iLookAhead.
367 ** If the look-ahead token is YYNOCODE, then check to see if the action is
368 ** independent of the look-ahead. If it is, return the action, otherwise
369 ** return YY_NO_ACTION.
371 static int yy_find_reduce_action(
372 yyParser
*pParser
, /* The parser */
373 int iLookAhead
/* The look-ahead token */
376 int stateno
= pParser
->yystack
[pParser
->yyidx
].stateno
;
378 i
= yy_reduce_ofst
[stateno
];
379 if( i
==YY_REDUCE_USE_DFLT
){
380 return yy_default
[stateno
];
382 if( iLookAhead
==YYNOCODE
){
386 if( i
<0 || (size_t)i
>=YY_SZ_ACTTAB
|| yy_lookahead
[i
]!=iLookAhead
){
387 return yy_default
[stateno
];
394 ** Perform a shift action.
396 static void yy_shift(
397 yyParser
*yypParser
, /* The parser to be shifted */
398 int yyNewState
, /* The new state to shift in */
399 int yyMajor
, /* The major token to shift in */
400 YYMINORTYPE
*yypMinor
/* Pointer ot the minor token to shift in */
404 if( yypParser
->yyidx
>=YYSTACKDEPTH
){
409 fprintf(yyTraceFILE
,"%sStack Overflow!\n",yyTracePrompt
);
412 while( yypParser
->yyidx
>=0 ) yy_pop_parser_stack(yypParser
);
413 /* Here code is inserted which will execute if the parser
414 ** stack every overflows */
416 ParseARG_STORE
; /* Suppress warning about unused %extra_argument var */
419 yytos
= &yypParser
->yystack
[yypParser
->yyidx
];
420 yytos
->stateno
= yyNewState
;
421 yytos
->major
= yyMajor
;
422 yytos
->minor
= *yypMinor
;
424 if( yyTraceFILE
&& yypParser
->yyidx
>0 ){
426 fprintf(yyTraceFILE
,"%sShift %d\n",yyTracePrompt
,yyNewState
);
427 fprintf(yyTraceFILE
,"%sStack:",yyTracePrompt
);
428 for(i
=1; i
<=yypParser
->yyidx
; i
++)
429 fprintf(yyTraceFILE
," %s",yyTokenName
[yypParser
->yystack
[i
].major
]);
430 fprintf(yyTraceFILE
,"\n");
435 /* The following table contains information about every rule that
436 ** is used during the reduce.
439 YYCODETYPE lhs
; /* Symbol on the left-hand side of the rule */
440 unsigned char nrhs
; /* Number of right-hand side symbols in the rule */
445 static void yy_accept(yyParser
*); /* Forward Declaration */
448 ** Perform a reduce action and the shift that must immediately
449 ** follow the reduce.
451 static void yy_reduce(
452 yyParser
*yypParser
, /* The parser */
453 int yyruleno
/* Number of the rule by which to reduce */
455 int yygoto
; /* The next state */
456 int yyact
; /* The next action */
457 YYMINORTYPE yygotominor
; /* The LHS of the rule reduced */
458 yyStackEntry
*yymsp
; /* The top of the parser's stack */
459 int yysize
; /* Amount to pop the stack */
461 yymsp
= &yypParser
->yystack
[yypParser
->yyidx
];
463 if( yyTraceFILE
&& yyruleno
>=0
464 && (size_t)yyruleno
<sizeof(yyRuleName
)/sizeof(yyRuleName
[0]) ){
465 fprintf(yyTraceFILE
, "%sReduce [%s].\n", yyTracePrompt
,
466 yyRuleName
[yyruleno
]);
471 /* Beginning here are the reduction cases. A typical example
474 ** #line <lineno> <grammarfile>
475 ** { ... } // User supplied code
476 ** #line <lineno> <thisfile>
481 yygoto
= yyRuleInfo
[yyruleno
].lhs
;
482 yysize
= yyRuleInfo
[yyruleno
].nrhs
;
483 yypParser
->yyidx
-= yysize
;
484 yyact
= yy_find_reduce_action(yypParser
,yygoto
);
485 if( yyact
< YYNSTATE
){
486 yy_shift(yypParser
,yyact
,yygoto
,&yygotominor
);
487 }else if( yyact
== YYNSTATE
+ YYNRULE
+ 1 ){
488 yy_accept(yypParser
);
493 ** The following code executes when the parse fails
495 static void yy_parse_failed(
496 yyParser
*yypParser
/* The parser */
501 fprintf(yyTraceFILE
,"%sFail!\n",yyTracePrompt
);
504 while( yypParser
->yyidx
>=0 ) yy_pop_parser_stack(yypParser
);
505 /* Here code is inserted which will be executed whenever the
508 ParseARG_STORE
; /* Suppress warning about unused %extra_argument variable */
512 ** The following code executes when a syntax error first occurs.
514 static void yy_syntax_error(
515 yyParser
*yypParser
, /* The parser */
516 int yymajor
, /* The major type of the error token */
517 YYMINORTYPE yyminor
/* The minor type of the error token */
522 #define TOKEN (yyminor.yy0)
524 ParseARG_STORE
; /* Suppress warning about unused %extra_argument variable */
528 ** The following is executed when the parser accepts
530 static void yy_accept(
531 yyParser
*yypParser
/* The parser */
536 fprintf(yyTraceFILE
,"%sAccept!\n",yyTracePrompt
);
539 while( yypParser
->yyidx
>=0 ) yy_pop_parser_stack(yypParser
);
540 /* Here code is inserted which will be executed whenever the
543 ParseARG_STORE
; /* Suppress warning about unused %extra_argument variable */
546 /* The main parser program.
547 ** The first argument is a pointer to a structure obtained from
548 ** "ParseAlloc" which describes the current state of the parser.
549 ** The second argument is the major token number. The third is
550 ** the minor token. The fourth optional argument is whatever the
551 ** user wants (and specified in the grammar) and is available for
552 ** use by the action routines.
556 ** <li> A pointer to the parser (an opaque structure.)
557 ** <li> The major token number.
558 ** <li> The minor token number.
559 ** <li> An option argument of a grammar-specified type.
566 void *yyp
, /* The parser */
567 int yymajor
, /* The major token code number */
568 ParseTOKENTYPE yyminor
/* The value for the token */
569 ParseARG_PDECL
/* Optional %extra_argument parameter */
571 YYMINORTYPE yyminorunion
;
572 int yyact
; /* The parser action. */
573 int yyendofinput
; /* True if we are at the end of input */
574 int yyerrorhit
= 0; /* True if yymajor has invoked an error */
575 yyParser
*yypParser
; /* The parser */
577 /* (re)initialize the parser, if necessary */
578 yypParser
= (yyParser
*)yyp
;
579 if( yypParser
->yyidx
<0 ){
580 if( yymajor
==0 ) return;
581 yypParser
->yyidx
= 0;
582 yypParser
->yyerrcnt
= -1;
583 yypParser
->yystack
[0].stateno
= 0;
584 yypParser
->yystack
[0].major
= 0;
586 yyminorunion
.yy0
= yyminor
;
587 yyendofinput
= (yymajor
==0);
592 fprintf(yyTraceFILE
,"%sInput %s\n",yyTracePrompt
,yyTokenName
[yymajor
]);
597 yyact
= yy_find_shift_action(yypParser
,yymajor
);
598 if( yyact
<YYNSTATE
){
599 yy_shift(yypParser
,yyact
,yymajor
,&yyminorunion
);
600 yypParser
->yyerrcnt
--;
601 if( yyendofinput
&& yypParser
->yyidx
>=0 ){
606 }else if( yyact
< YYNSTATE
+ YYNRULE
){
607 yy_reduce(yypParser
,yyact
-YYNSTATE
);
608 }else if( yyact
== YY_ERROR_ACTION
){
612 fprintf(yyTraceFILE
,"%sSyntax Error!\n",yyTracePrompt
);
616 /* A syntax error has occurred.
617 ** The response to an error depends upon whether or not the
618 ** grammar defines an error token "ERROR".
620 ** This is what we do if the grammar does define ERROR:
622 ** * Call the %syntax_error function.
624 ** * Begin popping the stack until we enter a state where
625 ** it is legal to shift the error symbol, then shift
628 ** * Set the error count to three.
630 ** * Begin accepting and shifting new tokens. No new error
631 ** processing will occur until three tokens have been
632 ** shifted successfully.
635 if( yypParser
->yyerrcnt
<0 ){
636 yy_syntax_error(yypParser
,yymajor
,yyminorunion
);
638 yymx
= yypParser
->yystack
[yypParser
->yyidx
].major
;
639 if( yymx
==YYERRORSYMBOL
|| yyerrorhit
){
642 fprintf(yyTraceFILE
,"%sDiscard input token %s\n",
643 yyTracePrompt
,yyTokenName
[yymajor
]);
646 yy_destructor(yymajor
,&yyminorunion
);
650 yypParser
->yyidx
>= 0 &&
651 yymx
!= YYERRORSYMBOL
&&
652 (yyact
= yy_find_shift_action(yypParser
,YYERRORSYMBOL
)) >= YYNSTATE
654 yy_pop_parser_stack(yypParser
);
656 if( yypParser
->yyidx
< 0 || yymajor
==0 ){
657 yy_destructor(yymajor
,&yyminorunion
);
658 yy_parse_failed(yypParser
);
660 }else if( yymx
!=YYERRORSYMBOL
){
663 yy_shift(yypParser
,yyact
,YYERRORSYMBOL
,&u2
);
666 yypParser
->yyerrcnt
= 3;
668 #else /* YYERRORSYMBOL is not defined */
669 /* This is what we do if the grammar does not define ERROR:
671 ** * Report an error message, and throw away the input token.
673 ** * If the input token is $, then fail the parse.
675 ** As before, subsequent error messages are suppressed until
676 ** three input tokens have been successfully shifted.
678 if( yypParser
->yyerrcnt
<=0 ){
679 yy_syntax_error(yypParser
,yymajor
,yyminorunion
);
681 yypParser
->yyerrcnt
= 3;
682 yy_destructor(yymajor
,&yyminorunion
);
684 yy_parse_failed(yypParser
);
689 yy_accept(yypParser
);
692 }while( yymajor
!=YYNOCODE
&& yypParser
->yyidx
>=0 );