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
){
283 if( pParser
->yyidx
<0 ) return 0;
284 yytos
= &pParser
->yystack
[pParser
->yyidx
];
286 if( yyTraceFILE
&& pParser
->yyidx
>=0 ){
287 fprintf(yyTraceFILE
,"%sPopping %s\n",
289 yyTokenName
[yytos
->major
]);
292 yymajor
= yytos
->major
;
293 yy_destructor( yymajor
, &yytos
->minor
);
299 ** Deallocate and destroy a parser. Destructors are all called for
300 ** all stack elements before shutting the parser down.
304 ** <li> A pointer to the parser. This should be a pointer
305 ** obtained from ParseAlloc.
306 ** <li> A pointer to a function used to reclaim memory obtained
311 void *p
, /* The parser to be deleted */
312 void (*freeProc
)(void*) /* Function used to reclaim memory */
314 yyParser
*pParser
= (yyParser
*)p
;
315 if( pParser
==NULL
) return;
316 while( pParser
->yyidx
>=0 ) yy_pop_parser_stack(pParser
);
317 (*freeProc
)((void*)pParser
);
321 ** Find the appropriate action for a parser given the terminal
322 ** look-ahead token iLookAhead.
324 ** If the look-ahead token is YYNOCODE, then check to see if the action is
325 ** independent of the look-ahead. If it is, return the action, otherwise
326 ** return YY_NO_ACTION.
328 static int yy_find_shift_action(
329 yyParser
*pParser
, /* The parser */
330 int iLookAhead
/* The look-ahead token */
333 int stateno
= pParser
->yystack
[pParser
->yyidx
].stateno
;
335 /* if( pParser->yyidx<0 ) return YY_NO_ACTION; */
336 i
= yy_shift_ofst
[stateno
];
337 if( i
==YY_SHIFT_USE_DFLT
){
338 return yy_default
[stateno
];
340 if( iLookAhead
==YYNOCODE
){
344 if( i
<0 || (size_t)i
>=YY_SZ_ACTTAB
|| yy_lookahead
[i
]!=iLookAhead
){
346 int iFallback
; /* Fallback token */
347 if( iLookAhead
<sizeof(yyFallback
)/sizeof(yyFallback
[0])
348 && (iFallback
= yyFallback
[iLookAhead
])!=0 ){
351 fprintf(yyTraceFILE
, "%sFALLBACK %s => %s\n",
352 yyTracePrompt
, yyTokenName
[iLookAhead
], yyTokenName
[iFallback
]);
355 return yy_find_shift_action(pParser
, iFallback
);
358 return yy_default
[stateno
];
365 ** Find the appropriate action for a parser given the non-terminal
366 ** look-ahead token iLookAhead.
368 ** If the look-ahead token is YYNOCODE, then check to see if the action is
369 ** independent of the look-ahead. If it is, return the action, otherwise
370 ** return YY_NO_ACTION.
372 static int yy_find_reduce_action(
373 yyParser
*pParser
, /* The parser */
374 int iLookAhead
/* The look-ahead token */
377 int stateno
= pParser
->yystack
[pParser
->yyidx
].stateno
;
379 i
= yy_reduce_ofst
[stateno
];
380 if( i
==YY_REDUCE_USE_DFLT
){
381 return yy_default
[stateno
];
383 if( iLookAhead
==YYNOCODE
){
387 if( i
<0 || (size_t)i
>=YY_SZ_ACTTAB
|| yy_lookahead
[i
]!=iLookAhead
){
388 return yy_default
[stateno
];
395 ** Perform a shift action.
397 static void yy_shift(
398 yyParser
*yypParser
, /* The parser to be shifted */
399 int yyNewState
, /* The new state to shift in */
400 int yyMajor
, /* The major token to shift in */
401 YYMINORTYPE
*yypMinor
/* Pointer ot the minor token to shift in */
405 if( yypParser
->yyidx
>=YYSTACKDEPTH
){
410 fprintf(yyTraceFILE
,"%sStack Overflow!\n",yyTracePrompt
);
413 while( yypParser
->yyidx
>=0 ) yy_pop_parser_stack(yypParser
);
414 /* Here code is inserted which will execute if the parser
415 ** stack every overflows */
417 ParseARG_STORE
; /* Suppress warning about unused %extra_argument var */
420 yytos
= &yypParser
->yystack
[yypParser
->yyidx
];
421 yytos
->stateno
= yyNewState
;
422 yytos
->major
= yyMajor
;
423 yytos
->minor
= *yypMinor
;
425 if( yyTraceFILE
&& yypParser
->yyidx
>0 ){
427 fprintf(yyTraceFILE
,"%sShift %d\n",yyTracePrompt
,yyNewState
);
428 fprintf(yyTraceFILE
,"%sStack:",yyTracePrompt
);
429 for(i
=1; i
<=yypParser
->yyidx
; i
++)
430 fprintf(yyTraceFILE
," %s",yyTokenName
[yypParser
->yystack
[i
].major
]);
431 fprintf(yyTraceFILE
,"\n");
436 /* The following table contains information about every rule that
437 ** is used during the reduce.
440 YYCODETYPE lhs
; /* Symbol on the left-hand side of the rule */
441 unsigned char nrhs
; /* Number of right-hand side symbols in the rule */
446 static void yy_accept(yyParser
*); /* Forward Declaration */
449 ** Perform a reduce action and the shift that must immediately
450 ** follow the reduce.
452 static void yy_reduce(
453 yyParser
*yypParser
, /* The parser */
454 int yyruleno
/* Number of the rule by which to reduce */
456 int yygoto
; /* The next state */
457 int yyact
; /* The next action */
458 YYMINORTYPE yygotominor
; /* The LHS of the rule reduced */
459 yyStackEntry
*yymsp
; /* The top of the parser's stack */
460 int yysize
; /* Amount to pop the stack */
462 yymsp
= &yypParser
->yystack
[yypParser
->yyidx
];
466 && (size_t)yyruleno
<sizeof(yyRuleName
)/sizeof(yyRuleName
[0]) ){
467 fprintf(yyTraceFILE
, "%sReduce [%s].\n", yyTracePrompt
,
468 yyRuleName
[yyruleno
]);
470 return; /*(should not happen)*/
476 /* Beginning here are the reduction cases. A typical example
479 ** #line <lineno> <grammarfile>
480 ** { ... } // User supplied code
481 ** #line <lineno> <thisfile>
486 yygoto
= yyRuleInfo
[yyruleno
].lhs
;
487 yysize
= yyRuleInfo
[yyruleno
].nrhs
;
488 yypParser
->yyidx
-= yysize
;
489 yyact
= yy_find_reduce_action(yypParser
,yygoto
);
490 if( yyact
< YYNSTATE
){
491 yy_shift(yypParser
,yyact
,yygoto
,&yygotominor
);
492 }else if( yyact
== YYNSTATE
+ YYNRULE
+ 1 ){
493 yy_accept(yypParser
);
498 ** The following code executes when the parse fails
500 static void yy_parse_failed(
501 yyParser
*yypParser
/* The parser */
506 fprintf(yyTraceFILE
,"%sFail!\n",yyTracePrompt
);
509 while( yypParser
->yyidx
>=0 ) yy_pop_parser_stack(yypParser
);
510 /* Here code is inserted which will be executed whenever the
513 ParseARG_STORE
; /* Suppress warning about unused %extra_argument variable */
517 ** The following code executes when a syntax error first occurs.
519 static void yy_syntax_error(
520 yyParser
*yypParser
, /* The parser */
521 int yymajor
, /* The major type of the error token */
522 YYMINORTYPE yyminor
/* The minor type of the error token */
527 #define TOKEN (yyminor.yy0)
529 ParseARG_STORE
; /* Suppress warning about unused %extra_argument variable */
533 ** The following is executed when the parser accepts
535 static void yy_accept(
536 yyParser
*yypParser
/* The parser */
541 fprintf(yyTraceFILE
,"%sAccept!\n",yyTracePrompt
);
544 while( yypParser
->yyidx
>=0 ) yy_pop_parser_stack(yypParser
);
545 /* Here code is inserted which will be executed whenever the
548 ParseARG_STORE
; /* Suppress warning about unused %extra_argument variable */
551 /* The main parser program.
552 ** The first argument is a pointer to a structure obtained from
553 ** "ParseAlloc" which describes the current state of the parser.
554 ** The second argument is the major token number. The third is
555 ** the minor token. The fourth optional argument is whatever the
556 ** user wants (and specified in the grammar) and is available for
557 ** use by the action routines.
561 ** <li> A pointer to the parser (an opaque structure.)
562 ** <li> The major token number.
563 ** <li> The minor token number.
564 ** <li> An option argument of a grammar-specified type.
571 void *yyp
, /* The parser */
572 int yymajor
, /* The major token code number */
573 ParseTOKENTYPE yyminor
/* The value for the token */
574 ParseARG_PDECL
/* Optional %extra_argument parameter */
576 YYMINORTYPE yyminorunion
;
577 int yyact
; /* The parser action. */
578 int yyendofinput
; /* True if we are at the end of input */
579 int yyerrorhit
= 0; /* True if yymajor has invoked an error */
580 yyParser
*yypParser
; /* The parser */
582 /* (re)initialize the parser, if necessary */
583 yypParser
= (yyParser
*)yyp
;
584 if( yypParser
->yyidx
<0 ){
585 if( yymajor
==0 ) return;
586 yypParser
->yyidx
= 0;
587 yypParser
->yyerrcnt
= -1;
588 yypParser
->yystack
[0].stateno
= 0;
589 yypParser
->yystack
[0].major
= 0;
591 yyminorunion
.yy0
= yyminor
;
592 yyendofinput
= (yymajor
==0);
597 fprintf(yyTraceFILE
,"%sInput %s\n",yyTracePrompt
,yyTokenName
[yymajor
]);
602 yyact
= yy_find_shift_action(yypParser
,yymajor
);
603 if( yyact
<YYNSTATE
){
604 yy_shift(yypParser
,yyact
,yymajor
,&yyminorunion
);
605 yypParser
->yyerrcnt
--;
606 if( yyendofinput
&& yypParser
->yyidx
>=0 ){
611 }else if( yyact
< YYNSTATE
+ YYNRULE
){
612 yy_reduce(yypParser
,yyact
-YYNSTATE
);
613 }else if( yyact
== YY_ERROR_ACTION
){
617 fprintf(yyTraceFILE
,"%sSyntax Error!\n",yyTracePrompt
);
621 /* A syntax error has occurred.
622 ** The response to an error depends upon whether or not the
623 ** grammar defines an error token "ERROR".
625 ** This is what we do if the grammar does define ERROR:
627 ** * Call the %syntax_error function.
629 ** * Begin popping the stack until we enter a state where
630 ** it is legal to shift the error symbol, then shift
633 ** * Set the error count to three.
635 ** * Begin accepting and shifting new tokens. No new error
636 ** processing will occur until three tokens have been
637 ** shifted successfully.
640 if( yypParser
->yyerrcnt
<0 ){
641 yy_syntax_error(yypParser
,yymajor
,yyminorunion
);
643 yymx
= yypParser
->yystack
[yypParser
->yyidx
].major
;
644 if( yymx
==YYERRORSYMBOL
|| yyerrorhit
){
647 fprintf(yyTraceFILE
,"%sDiscard input token %s\n",
648 yyTracePrompt
,yyTokenName
[yymajor
]);
651 yy_destructor(yymajor
,&yyminorunion
);
655 yypParser
->yyidx
>= 0 &&
656 yymx
!= YYERRORSYMBOL
&&
657 (yyact
= yy_find_shift_action(yypParser
,YYERRORSYMBOL
)) >= YYNSTATE
659 yy_pop_parser_stack(yypParser
);
661 if( yypParser
->yyidx
< 0 || yymajor
==0 ){
662 yy_destructor(yymajor
,&yyminorunion
);
663 yy_parse_failed(yypParser
);
665 }else if( yymx
!=YYERRORSYMBOL
){
668 yy_shift(yypParser
,yyact
,YYERRORSYMBOL
,&u2
);
671 yypParser
->yyerrcnt
= 3;
673 #else /* YYERRORSYMBOL is not defined */
674 /* This is what we do if the grammar does not define ERROR:
676 ** * Report an error message, and throw away the input token.
678 ** * If the input token is $, then fail the parse.
680 ** As before, subsequent error messages are suppressed until
681 ** three input tokens have been successfully shifted.
683 if( yypParser
->yyerrcnt
<=0 ){
684 yy_syntax_error(yypParser
,yymajor
,yyminorunion
);
686 yypParser
->yyerrcnt
= 3;
687 yy_destructor(yymajor
,&yyminorunion
);
689 yy_parse_failed(yypParser
);
694 yy_accept(yypParser
);
697 }while( yymajor
!=YYNOCODE
&& yypParser
->yyidx
>=0 );