2 #pragma ident "%Z%%M% %I% %E% SMI"
4 /* Driver template for the LEMON parser generator.
5 ** The author disclaims copyright to this source code.
7 /* First off, code is include which follows the "include" declaration
8 ** in the input file. */
11 /* Next is all token values, in a form suitable for use by makeheaders.
12 ** This section will be null unless lemon is run with the -m switch.
15 ** These constants (all generated automatically by the parser generator)
16 ** specify the various kinds of tokens (terminals) that the parser
19 ** Each symbol here is a terminal symbol in the grammar.
22 /* Make sure the INTERFACE macro is defined.
27 /* The next thing included is series of defines which control
28 ** various aspects of the generated parser.
29 ** YYCODETYPE is the data type used for storing terminal
30 ** and nonterminal numbers. "unsigned char" is
31 ** used if there are fewer than 250 terminals
32 ** and nonterminals. "int" is used otherwise.
33 ** YYNOCODE is a number of type YYCODETYPE which corresponds
34 ** to no legal terminal or nonterminal number. This
35 ** number is used to fill in empty slots of the hash
37 ** YYFALLBACK If defined, this indicates that one or more tokens
38 ** have fall-back values which should be used if the
39 ** original value of the token will not parse.
40 ** YYACTIONTYPE is the data type used for storing terminal
41 ** and nonterminal numbers. "unsigned char" is
42 ** used if there are fewer than 250 rules and
43 ** states combined. "int" is used otherwise.
44 ** ParseTOKENTYPE is the data type used for minor tokens given
45 ** directly to the parser from the tokenizer.
46 ** YYMINORTYPE is the data type used for all minor tokens.
47 ** This is typically a union of many types, one of
48 ** which is ParseTOKENTYPE. The entry in the union
49 ** for base tokens is called "yy0".
50 ** YYSTACKDEPTH is the maximum depth of the parser's stack.
51 ** ParseARG_SDECL A static variable declaration for the %extra_argument
52 ** ParseARG_PDECL A parameter declaration for the %extra_argument
53 ** ParseARG_STORE Code to store %extra_argument into yypParser
54 ** ParseARG_FETCH Code to extract %extra_argument from yypParser
55 ** YYNSTATE the combined number of states.
56 ** YYNRULE the number of rules in the grammar
57 ** YYERRORSYMBOL is the code number of the error symbol. If not
58 ** defined, then do no error processing.
61 #define YY_NO_ACTION (YYNSTATE+YYNRULE+2)
62 #define YY_ACCEPT_ACTION (YYNSTATE+YYNRULE+1)
63 #define YY_ERROR_ACTION (YYNSTATE+YYNRULE)
65 /* Next are that tables used to determine what action to take based on the
66 ** current state and lookahead token. These tables are used to implement
67 ** functions that take a state number and lookahead value and return an
70 ** Suppose the action integer is N. Then the action is determined as
73 ** 0 <= N < YYNSTATE Shift N. That is, push the lookahead
74 ** token onto the stack and goto state N.
76 ** YYNSTATE <= N < YYNSTATE+YYNRULE Reduce by rule N-YYNSTATE.
78 ** N == YYNSTATE+YYNRULE A syntax error has occurred.
80 ** N == YYNSTATE+YYNRULE+1 The parser accepts its input.
82 ** N == YYNSTATE+YYNRULE+2 No such action. Denotes unused
83 ** slots in the yy_action[] table.
85 ** The action table is constructed as a single large table named yy_action[].
86 ** Given state S and lookahead X, the action is computed as
88 ** yy_action[ yy_shift_ofst[S] + X ]
90 ** If the index value yy_shift_ofst[S]+X is out of range or if the value
91 ** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
92 ** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
93 ** and that yy_default[S] should be used instead.
95 ** The formula above is for computing the action when the lookahead is
96 ** a terminal symbol. If the lookahead is a non-terminal (as occurs after
97 ** a reduce action) then the yy_reduce_ofst[] array is used in place of
98 ** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
101 ** The following are the tables generated in this section:
103 ** yy_action[] A single table containing all actions.
104 ** yy_lookahead[] A table containing the lookahead for each entry in
105 ** yy_action. Used to detect hash collisions.
106 ** yy_shift_ofst[] For each state, the offset into yy_action for
107 ** shifting terminals.
108 ** yy_reduce_ofst[] For each state, the offset into yy_action for
109 ** shifting non-terminals after a reduce.
110 ** yy_default[] Default action for each state.
113 #define YY_SZ_ACTTAB (sizeof(yy_action)/sizeof(yy_action[0]))
115 /* The next table maps tokens into fallback tokens. If a construct
116 ** like the following:
118 ** %fallback ID X Y Z.
120 ** appears in the grammer, then ID becomes a fallback token for X, Y,
121 ** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
122 ** but it does not parse, the type of the token is changed to ID and
123 ** the parse is retried before an error is thrown.
126 static const YYCODETYPE yyFallback
[] = {
129 #endif /* YYFALLBACK */
131 /* The following structure represents a single element of the
132 ** parser's stack. Information stored includes:
134 ** + The state number for the parser at this level of the stack.
136 ** + The value of the token stored at this level of the stack.
137 ** (In other words, the "major" token.)
139 ** + The semantic value stored at this level of the stack. This is
140 ** the information used by the action routines in the grammar.
141 ** It is sometimes called the "minor" token.
143 struct yyStackEntry
{
144 int stateno
; /* The state-number */
145 int major
; /* The major token value. This is the code
146 ** number for the token at this stack level */
147 YYMINORTYPE minor
; /* The user-supplied minor token value. This
148 ** is the value of the token */
150 typedef struct yyStackEntry yyStackEntry
;
152 /* The state of the parser is completely contained in an instance of
153 ** the following structure */
155 int yyidx
; /* Index of top element in stack */
156 int yyerrcnt
; /* Shifts left before out of the error */
157 ParseARG_SDECL
/* A place to hold %extra_argument */
158 yyStackEntry yystack
[YYSTACKDEPTH
]; /* The parser's stack */
160 typedef struct yyParser yyParser
;
164 static FILE *yyTraceFILE
= 0;
165 static char *yyTracePrompt
= 0;
170 ** Turn parser tracing on by giving a stream to which to write the trace
171 ** and a prompt to preface each trace message. Tracing is turned off
172 ** by making either argument NULL
176 ** <li> A FILE* to which trace output should be written.
177 ** If NULL, then tracing is turned off.
178 ** <li> A prefix string written at the beginning of every
179 ** line of trace output. If NULL, then tracing is
186 void ParseTrace(FILE *TraceFILE
, char *zTracePrompt
){
187 yyTraceFILE
= TraceFILE
;
188 yyTracePrompt
= zTracePrompt
;
189 if( yyTraceFILE
==0 ) yyTracePrompt
= 0;
190 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
214 const char *ParseTokenName(int tokenType
){
216 if( tokenType
>0 && tokenType
<(sizeof(yyTokenName
)/sizeof(yyTokenName
[0])) ){
217 return yyTokenName
[tokenType
];
227 ** This function allocates a new parser.
228 ** The only argument is a pointer to a function which works like
232 ** A pointer to the function used to allocate memory.
235 ** A pointer to a parser. This pointer is used in subsequent calls
236 ** to Parse and ParseFree.
238 void *ParseAlloc(void *(*mallocProc
)(size_t)){
240 pParser
= (yyParser
*)(*mallocProc
)( (size_t)sizeof(yyParser
) );
247 /* The following function deletes the value associated with a
248 ** symbol. The symbol can be either a terminal or nonterminal.
249 ** "yymajor" is the symbol code, and "yypminor" is a pointer to
252 static void yy_destructor(YYCODETYPE yymajor
, YYMINORTYPE
*yypminor
){
254 /* Here is inserted the actions which take place when a
255 ** terminal or non-terminal is destroyed. This can happen
256 ** when the symbol is popped from the stack during a
257 ** reduce or during error processing or when a parser is
258 ** being destroyed before it is finished parsing.
260 ** Note: during a reduce, the only symbols destroyed are those
261 ** which appear on the RHS of the rule, but which are not used
262 ** inside the C code.
265 default: break; /* If no destructor action specified: do nothing */
270 ** Pop the parser's stack once.
272 ** If there is a destructor routine associated with the token which
273 ** is popped from the stack, then call it.
275 ** Return the major token number for the symbol popped.
277 static int yy_pop_parser_stack(yyParser
*pParser
){
279 yyStackEntry
*yytos
= &pParser
->yystack
[pParser
->yyidx
];
281 if( pParser
->yyidx
<0 ) return 0;
283 if( yyTraceFILE
&& pParser
->yyidx
>=0 ){
284 fprintf(yyTraceFILE
,"%sPopping %s\n",
286 yyTokenName
[yytos
->major
]);
289 yymajor
= yytos
->major
;
290 yy_destructor( yymajor
, &yytos
->minor
);
296 ** Deallocate and destroy a parser. Destructors are all called for
297 ** all stack elements before shutting the parser down.
301 ** <li> A pointer to the parser. This should be a pointer
302 ** obtained from ParseAlloc.
303 ** <li> A pointer to a function used to reclaim memory obtained
308 void *p
, /* The parser to be deleted */
309 void (*freeProc
)(void*) /* Function used to reclaim memory */
311 yyParser
*pParser
= (yyParser
*)p
;
312 if( pParser
==0 ) return;
313 while( pParser
->yyidx
>=0 ) yy_pop_parser_stack(pParser
);
314 (*freeProc
)((void*)pParser
);
318 ** Find the appropriate action for a parser given the terminal
319 ** look-ahead token iLookAhead.
321 ** If the look-ahead token is YYNOCODE, then check to see if the action is
322 ** independent of the look-ahead. If it is, return the action, otherwise
323 ** return YY_NO_ACTION.
325 static int yy_find_shift_action(
326 yyParser
*pParser
, /* The parser */
327 int iLookAhead
/* The look-ahead token */
330 int stateno
= pParser
->yystack
[pParser
->yyidx
].stateno
;
332 /* if( pParser->yyidx<0 ) return YY_NO_ACTION; */
333 i
= yy_shift_ofst
[stateno
];
334 if( i
==YY_SHIFT_USE_DFLT
){
335 return yy_default
[stateno
];
337 if( iLookAhead
==YYNOCODE
){
341 if( i
<0 || i
>=YY_SZ_ACTTAB
|| yy_lookahead
[i
]!=iLookAhead
){
343 int iFallback
; /* Fallback token */
344 if( iLookAhead
<sizeof(yyFallback
)/sizeof(yyFallback
[0])
345 && (iFallback
= yyFallback
[iLookAhead
])!=0 ){
348 fprintf(yyTraceFILE
, "%sFALLBACK %s => %s\n",
349 yyTracePrompt
, yyTokenName
[iLookAhead
], yyTokenName
[iFallback
]);
352 return yy_find_shift_action(pParser
, iFallback
);
355 return yy_default
[stateno
];
362 ** Find the appropriate action for a parser given the non-terminal
363 ** look-ahead token iLookAhead.
365 ** If the look-ahead token is YYNOCODE, then check to see if the action is
366 ** independent of the look-ahead. If it is, return the action, otherwise
367 ** return YY_NO_ACTION.
369 static int yy_find_reduce_action(
370 yyParser
*pParser
, /* The parser */
371 int iLookAhead
/* The look-ahead token */
374 int stateno
= pParser
->yystack
[pParser
->yyidx
].stateno
;
376 i
= yy_reduce_ofst
[stateno
];
377 if( i
==YY_REDUCE_USE_DFLT
){
378 return yy_default
[stateno
];
380 if( iLookAhead
==YYNOCODE
){
384 if( i
<0 || i
>=YY_SZ_ACTTAB
|| yy_lookahead
[i
]!=iLookAhead
){
385 return yy_default
[stateno
];
392 ** Perform a shift action.
394 static void yy_shift(
395 yyParser
*yypParser
, /* The parser to be shifted */
396 int yyNewState
, /* The new state to shift in */
397 int yyMajor
, /* The major token to shift in */
398 YYMINORTYPE
*yypMinor
/* Pointer ot the minor token to shift in */
402 if( yypParser
->yyidx
>=YYSTACKDEPTH
){
407 fprintf(yyTraceFILE
,"%sStack Overflow!\n",yyTracePrompt
);
410 while( yypParser
->yyidx
>=0 ) yy_pop_parser_stack(yypParser
);
411 /* Here code is inserted which will execute if the parser
412 ** stack every overflows */
414 ParseARG_STORE
; /* Suppress warning about unused %extra_argument var */
417 yytos
= &yypParser
->yystack
[yypParser
->yyidx
];
418 yytos
->stateno
= yyNewState
;
419 yytos
->major
= yyMajor
;
420 yytos
->minor
= *yypMinor
;
422 if( yyTraceFILE
&& yypParser
->yyidx
>0 ){
424 fprintf(yyTraceFILE
,"%sShift %d\n",yyTracePrompt
,yyNewState
);
425 fprintf(yyTraceFILE
,"%sStack:",yyTracePrompt
);
426 for(i
=1; i
<=yypParser
->yyidx
; i
++)
427 fprintf(yyTraceFILE
," %s",yyTokenName
[yypParser
->yystack
[i
].major
]);
428 fprintf(yyTraceFILE
,"\n");
433 /* The following table contains information about every rule that
434 ** is used during the reduce.
437 YYCODETYPE lhs
; /* Symbol on the left-hand side of the rule */
438 unsigned char nrhs
; /* Number of right-hand side symbols in the rule */
443 static void yy_accept(yyParser
*); /* Forward Declaration */
446 ** Perform a reduce action and the shift that must immediately
447 ** follow the reduce.
449 static void yy_reduce(
450 yyParser
*yypParser
, /* The parser */
451 int yyruleno
/* Number of the rule by which to reduce */
453 int yygoto
; /* The next state */
454 int yyact
; /* The next action */
455 YYMINORTYPE yygotominor
; /* The LHS of the rule reduced */
456 yyStackEntry
*yymsp
; /* The top of the parser's stack */
457 int yysize
; /* Amount to pop the stack */
459 yymsp
= &yypParser
->yystack
[yypParser
->yyidx
];
461 if( yyTraceFILE
&& yyruleno
>=0
462 && yyruleno
<sizeof(yyRuleName
)/sizeof(yyRuleName
[0]) ){
463 fprintf(yyTraceFILE
, "%sReduce [%s].\n", yyTracePrompt
,
464 yyRuleName
[yyruleno
]);
469 /* Beginning here are the reduction cases. A typical example
472 ** #line <lineno> <grammarfile>
473 ** { ... } // User supplied code
474 ** #line <lineno> <thisfile>
479 yygoto
= yyRuleInfo
[yyruleno
].lhs
;
480 yysize
= yyRuleInfo
[yyruleno
].nrhs
;
481 yypParser
->yyidx
-= yysize
;
482 yyact
= yy_find_reduce_action(yypParser
,yygoto
);
483 if( yyact
< YYNSTATE
){
484 yy_shift(yypParser
,yyact
,yygoto
,&yygotominor
);
485 }else if( yyact
== YYNSTATE
+ YYNRULE
+ 1 ){
486 yy_accept(yypParser
);
491 ** The following code executes when the parse fails
493 static void yy_parse_failed(
494 yyParser
*yypParser
/* The parser */
499 fprintf(yyTraceFILE
,"%sFail!\n",yyTracePrompt
);
502 while( yypParser
->yyidx
>=0 ) yy_pop_parser_stack(yypParser
);
503 /* Here code is inserted which will be executed whenever the
506 ParseARG_STORE
; /* Suppress warning about unused %extra_argument variable */
510 ** The following code executes when a syntax error first occurs.
512 static void yy_syntax_error(
513 yyParser
*yypParser
, /* The parser */
514 int yymajor
, /* The major type of the error token */
515 YYMINORTYPE yyminor
/* The minor type of the error token */
518 #define TOKEN (yyminor.yy0)
520 ParseARG_STORE
; /* Suppress warning about unused %extra_argument variable */
524 ** The following is executed when the parser accepts
526 static void yy_accept(
527 yyParser
*yypParser
/* The parser */
532 fprintf(yyTraceFILE
,"%sAccept!\n",yyTracePrompt
);
535 while( yypParser
->yyidx
>=0 ) yy_pop_parser_stack(yypParser
);
536 /* Here code is inserted which will be executed whenever the
539 ParseARG_STORE
; /* Suppress warning about unused %extra_argument variable */
542 /* The main parser program.
543 ** The first argument is a pointer to a structure obtained from
544 ** "ParseAlloc" which describes the current state of the parser.
545 ** The second argument is the major token number. The third is
546 ** the minor token. The fourth optional argument is whatever the
547 ** user wants (and specified in the grammar) and is available for
548 ** use by the action routines.
552 ** <li> A pointer to the parser (an opaque structure.)
553 ** <li> The major token number.
554 ** <li> The minor token number.
555 ** <li> An option argument of a grammar-specified type.
562 void *yyp
, /* The parser */
563 int yymajor
, /* The major token code number */
564 ParseTOKENTYPE yyminor
/* The value for the token */
565 ParseARG_PDECL
/* Optional %extra_argument parameter */
567 YYMINORTYPE yyminorunion
;
568 int yyact
; /* The parser action. */
569 int yyendofinput
; /* True if we are at the end of input */
570 int yyerrorhit
= 0; /* True if yymajor has invoked an error */
571 yyParser
*yypParser
; /* The parser */
573 /* (re)initialize the parser, if necessary */
574 yypParser
= (yyParser
*)yyp
;
575 if( yypParser
->yyidx
<0 ){
576 if( yymajor
==0 ) return;
577 yypParser
->yyidx
= 0;
578 yypParser
->yyerrcnt
= -1;
579 yypParser
->yystack
[0].stateno
= 0;
580 yypParser
->yystack
[0].major
= 0;
582 yyminorunion
.yy0
= yyminor
;
583 yyendofinput
= (yymajor
==0);
588 fprintf(yyTraceFILE
,"%sInput %s\n",yyTracePrompt
,yyTokenName
[yymajor
]);
593 yyact
= yy_find_shift_action(yypParser
,yymajor
);
594 if( yyact
<YYNSTATE
){
595 yy_shift(yypParser
,yyact
,yymajor
,&yyminorunion
);
596 yypParser
->yyerrcnt
--;
597 if( yyendofinput
&& yypParser
->yyidx
>=0 ){
602 }else if( yyact
< YYNSTATE
+ YYNRULE
){
603 yy_reduce(yypParser
,yyact
-YYNSTATE
);
604 }else if( yyact
== YY_ERROR_ACTION
){
608 fprintf(yyTraceFILE
,"%sSyntax Error!\n",yyTracePrompt
);
612 /* A syntax error has occurred.
613 ** The response to an error depends upon whether or not the
614 ** grammar defines an error token "ERROR".
616 ** This is what we do if the grammar does define ERROR:
618 ** * Call the %syntax_error function.
620 ** * Begin popping the stack until we enter a state where
621 ** it is legal to shift the error symbol, then shift
624 ** * Set the error count to three.
626 ** * Begin accepting and shifting new tokens. No new error
627 ** processing will occur until three tokens have been
628 ** shifted successfully.
631 if( yypParser
->yyerrcnt
<0 ){
632 yy_syntax_error(yypParser
,yymajor
,yyminorunion
);
634 yymx
= yypParser
->yystack
[yypParser
->yyidx
].major
;
635 if( yymx
==YYERRORSYMBOL
|| yyerrorhit
){
638 fprintf(yyTraceFILE
,"%sDiscard input token %s\n",
639 yyTracePrompt
,yyTokenName
[yymajor
]);
642 yy_destructor(yymajor
,&yyminorunion
);
646 yypParser
->yyidx
>= 0 &&
647 yymx
!= YYERRORSYMBOL
&&
648 (yyact
= yy_find_shift_action(yypParser
,YYERRORSYMBOL
)) >= YYNSTATE
650 yy_pop_parser_stack(yypParser
);
652 if( yypParser
->yyidx
< 0 || yymajor
==0 ){
653 yy_destructor(yymajor
,&yyminorunion
);
654 yy_parse_failed(yypParser
);
656 }else if( yymx
!=YYERRORSYMBOL
){
659 yy_shift(yypParser
,yyact
,YYERRORSYMBOL
,&u2
);
662 yypParser
->yyerrcnt
= 3;
664 #else /* YYERRORSYMBOL is not defined */
665 /* This is what we do if the grammar does not define ERROR:
667 ** * Report an error message, and throw away the input token.
669 ** * If the input token is $, then fail the parse.
671 ** As before, subsequent error messages are suppressed until
672 ** three input tokens have been successfully shifted.
674 if( yypParser
->yyerrcnt
<=0 ){
675 yy_syntax_error(yypParser
,yymajor
,yyminorunion
);
677 yypParser
->yyerrcnt
= 3;
678 yy_destructor(yymajor
,&yyminorunion
);
680 yy_parse_failed(yypParser
);
685 yy_accept(yypParser
);
688 }while( yymajor
!=YYNOCODE
&& yypParser
->yyidx
>=0 );