xref: /titanic_52/usr/src/lib/libsqlite/tool/lempar.c (revision c5c4113dfcabb1eed3d4bdf7609de5170027a794)
1*c5c4113dSnw141292 
2*c5c4113dSnw141292 #pragma ident	"%Z%%M%	%I%	%E% SMI"
3*c5c4113dSnw141292 
4*c5c4113dSnw141292 /* Driver template for the LEMON parser generator.
5*c5c4113dSnw141292 ** The author disclaims copyright to this source code.
6*c5c4113dSnw141292 */
7*c5c4113dSnw141292 /* First off, code is include which follows the "include" declaration
8*c5c4113dSnw141292 ** in the input file. */
9*c5c4113dSnw141292 #include <stdio.h>
10*c5c4113dSnw141292 %%
11*c5c4113dSnw141292 /* Next is all token values, in a form suitable for use by makeheaders.
12*c5c4113dSnw141292 ** This section will be null unless lemon is run with the -m switch.
13*c5c4113dSnw141292 */
14*c5c4113dSnw141292 /*
15*c5c4113dSnw141292 ** These constants (all generated automatically by the parser generator)
16*c5c4113dSnw141292 ** specify the various kinds of tokens (terminals) that the parser
17*c5c4113dSnw141292 ** understands.
18*c5c4113dSnw141292 **
19*c5c4113dSnw141292 ** Each symbol here is a terminal symbol in the grammar.
20*c5c4113dSnw141292 */
21*c5c4113dSnw141292 %%
22*c5c4113dSnw141292 /* Make sure the INTERFACE macro is defined.
23*c5c4113dSnw141292 */
24*c5c4113dSnw141292 #ifndef INTERFACE
25*c5c4113dSnw141292 # define INTERFACE 1
26*c5c4113dSnw141292 #endif
27*c5c4113dSnw141292 /* The next thing included is series of defines which control
28*c5c4113dSnw141292 ** various aspects of the generated parser.
29*c5c4113dSnw141292 **    YYCODETYPE         is the data type used for storing terminal
30*c5c4113dSnw141292 **                       and nonterminal numbers.  "unsigned char" is
31*c5c4113dSnw141292 **                       used if there are fewer than 250 terminals
32*c5c4113dSnw141292 **                       and nonterminals.  "int" is used otherwise.
33*c5c4113dSnw141292 **    YYNOCODE           is a number of type YYCODETYPE which corresponds
34*c5c4113dSnw141292 **                       to no legal terminal or nonterminal number.  This
35*c5c4113dSnw141292 **                       number is used to fill in empty slots of the hash
36*c5c4113dSnw141292 **                       table.
37*c5c4113dSnw141292 **    YYFALLBACK         If defined, this indicates that one or more tokens
38*c5c4113dSnw141292 **                       have fall-back values which should be used if the
39*c5c4113dSnw141292 **                       original value of the token will not parse.
40*c5c4113dSnw141292 **    YYACTIONTYPE       is the data type used for storing terminal
41*c5c4113dSnw141292 **                       and nonterminal numbers.  "unsigned char" is
42*c5c4113dSnw141292 **                       used if there are fewer than 250 rules and
43*c5c4113dSnw141292 **                       states combined.  "int" is used otherwise.
44*c5c4113dSnw141292 **    ParseTOKENTYPE     is the data type used for minor tokens given
45*c5c4113dSnw141292 **                       directly to the parser from the tokenizer.
46*c5c4113dSnw141292 **    YYMINORTYPE        is the data type used for all minor tokens.
47*c5c4113dSnw141292 **                       This is typically a union of many types, one of
48*c5c4113dSnw141292 **                       which is ParseTOKENTYPE.  The entry in the union
49*c5c4113dSnw141292 **                       for base tokens is called "yy0".
50*c5c4113dSnw141292 **    YYSTACKDEPTH       is the maximum depth of the parser's stack.
51*c5c4113dSnw141292 **    ParseARG_SDECL     A static variable declaration for the %extra_argument
52*c5c4113dSnw141292 **    ParseARG_PDECL     A parameter declaration for the %extra_argument
53*c5c4113dSnw141292 **    ParseARG_STORE     Code to store %extra_argument into yypParser
54*c5c4113dSnw141292 **    ParseARG_FETCH     Code to extract %extra_argument from yypParser
55*c5c4113dSnw141292 **    YYNSTATE           the combined number of states.
56*c5c4113dSnw141292 **    YYNRULE            the number of rules in the grammar
57*c5c4113dSnw141292 **    YYERRORSYMBOL      is the code number of the error symbol.  If not
58*c5c4113dSnw141292 **                       defined, then do no error processing.
59*c5c4113dSnw141292 */
60*c5c4113dSnw141292 %%
61*c5c4113dSnw141292 #define YY_NO_ACTION      (YYNSTATE+YYNRULE+2)
62*c5c4113dSnw141292 #define YY_ACCEPT_ACTION  (YYNSTATE+YYNRULE+1)
63*c5c4113dSnw141292 #define YY_ERROR_ACTION   (YYNSTATE+YYNRULE)
64*c5c4113dSnw141292 
65*c5c4113dSnw141292 /* Next are that tables used to determine what action to take based on the
66*c5c4113dSnw141292 ** current state and lookahead token.  These tables are used to implement
67*c5c4113dSnw141292 ** functions that take a state number and lookahead value and return an
68*c5c4113dSnw141292 ** action integer.
69*c5c4113dSnw141292 **
70*c5c4113dSnw141292 ** Suppose the action integer is N.  Then the action is determined as
71*c5c4113dSnw141292 ** follows
72*c5c4113dSnw141292 **
73*c5c4113dSnw141292 **   0 <= N < YYNSTATE                  Shift N.  That is, push the lookahead
74*c5c4113dSnw141292 **                                      token onto the stack and goto state N.
75*c5c4113dSnw141292 **
76*c5c4113dSnw141292 **   YYNSTATE <= N < YYNSTATE+YYNRULE   Reduce by rule N-YYNSTATE.
77*c5c4113dSnw141292 **
78*c5c4113dSnw141292 **   N == YYNSTATE+YYNRULE              A syntax error has occurred.
79*c5c4113dSnw141292 **
80*c5c4113dSnw141292 **   N == YYNSTATE+YYNRULE+1            The parser accepts its input.
81*c5c4113dSnw141292 **
82*c5c4113dSnw141292 **   N == YYNSTATE+YYNRULE+2            No such action.  Denotes unused
83*c5c4113dSnw141292 **                                      slots in the yy_action[] table.
84*c5c4113dSnw141292 **
85*c5c4113dSnw141292 ** The action table is constructed as a single large table named yy_action[].
86*c5c4113dSnw141292 ** Given state S and lookahead X, the action is computed as
87*c5c4113dSnw141292 **
88*c5c4113dSnw141292 **      yy_action[ yy_shift_ofst[S] + X ]
89*c5c4113dSnw141292 **
90*c5c4113dSnw141292 ** If the index value yy_shift_ofst[S]+X is out of range or if the value
91*c5c4113dSnw141292 ** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
92*c5c4113dSnw141292 ** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
93*c5c4113dSnw141292 ** and that yy_default[S] should be used instead.
94*c5c4113dSnw141292 **
95*c5c4113dSnw141292 ** The formula above is for computing the action when the lookahead is
96*c5c4113dSnw141292 ** a terminal symbol.  If the lookahead is a non-terminal (as occurs after
97*c5c4113dSnw141292 ** a reduce action) then the yy_reduce_ofst[] array is used in place of
98*c5c4113dSnw141292 ** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
99*c5c4113dSnw141292 ** YY_SHIFT_USE_DFLT.
100*c5c4113dSnw141292 **
101*c5c4113dSnw141292 ** The following are the tables generated in this section:
102*c5c4113dSnw141292 **
103*c5c4113dSnw141292 **  yy_action[]        A single table containing all actions.
104*c5c4113dSnw141292 **  yy_lookahead[]     A table containing the lookahead for each entry in
105*c5c4113dSnw141292 **                     yy_action.  Used to detect hash collisions.
106*c5c4113dSnw141292 **  yy_shift_ofst[]    For each state, the offset into yy_action for
107*c5c4113dSnw141292 **                     shifting terminals.
108*c5c4113dSnw141292 **  yy_reduce_ofst[]   For each state, the offset into yy_action for
109*c5c4113dSnw141292 **                     shifting non-terminals after a reduce.
110*c5c4113dSnw141292 **  yy_default[]       Default action for each state.
111*c5c4113dSnw141292 */
112*c5c4113dSnw141292 %%
113*c5c4113dSnw141292 #define YY_SZ_ACTTAB (sizeof(yy_action)/sizeof(yy_action[0]))
114*c5c4113dSnw141292 
115*c5c4113dSnw141292 /* The next table maps tokens into fallback tokens.  If a construct
116*c5c4113dSnw141292 ** like the following:
117*c5c4113dSnw141292 **
118*c5c4113dSnw141292 **      %fallback ID X Y Z.
119*c5c4113dSnw141292 **
120*c5c4113dSnw141292 ** appears in the grammer, then ID becomes a fallback token for X, Y,
121*c5c4113dSnw141292 ** and Z.  Whenever one of the tokens X, Y, or Z is input to the parser
122*c5c4113dSnw141292 ** but it does not parse, the type of the token is changed to ID and
123*c5c4113dSnw141292 ** the parse is retried before an error is thrown.
124*c5c4113dSnw141292 */
125*c5c4113dSnw141292 #ifdef YYFALLBACK
126*c5c4113dSnw141292 static const YYCODETYPE yyFallback[] = {
127*c5c4113dSnw141292 %%
128*c5c4113dSnw141292 };
129*c5c4113dSnw141292 #endif /* YYFALLBACK */
130*c5c4113dSnw141292 
131*c5c4113dSnw141292 /* The following structure represents a single element of the
132*c5c4113dSnw141292 ** parser's stack.  Information stored includes:
133*c5c4113dSnw141292 **
134*c5c4113dSnw141292 **   +  The state number for the parser at this level of the stack.
135*c5c4113dSnw141292 **
136*c5c4113dSnw141292 **   +  The value of the token stored at this level of the stack.
137*c5c4113dSnw141292 **      (In other words, the "major" token.)
138*c5c4113dSnw141292 **
139*c5c4113dSnw141292 **   +  The semantic value stored at this level of the stack.  This is
140*c5c4113dSnw141292 **      the information used by the action routines in the grammar.
141*c5c4113dSnw141292 **      It is sometimes called the "minor" token.
142*c5c4113dSnw141292 */
143*c5c4113dSnw141292 struct yyStackEntry {
144*c5c4113dSnw141292   int stateno;       /* The state-number */
145*c5c4113dSnw141292   int major;         /* The major token value.  This is the code
146*c5c4113dSnw141292                      ** number for the token at this stack level */
147*c5c4113dSnw141292   YYMINORTYPE minor; /* The user-supplied minor token value.  This
148*c5c4113dSnw141292                      ** is the value of the token  */
149*c5c4113dSnw141292 };
150*c5c4113dSnw141292 typedef struct yyStackEntry yyStackEntry;
151*c5c4113dSnw141292 
152*c5c4113dSnw141292 /* The state of the parser is completely contained in an instance of
153*c5c4113dSnw141292 ** the following structure */
154*c5c4113dSnw141292 struct yyParser {
155*c5c4113dSnw141292   int yyidx;                    /* Index of top element in stack */
156*c5c4113dSnw141292   int yyerrcnt;                 /* Shifts left before out of the error */
157*c5c4113dSnw141292   ParseARG_SDECL                /* A place to hold %extra_argument */
158*c5c4113dSnw141292   yyStackEntry yystack[YYSTACKDEPTH];  /* The parser's stack */
159*c5c4113dSnw141292 };
160*c5c4113dSnw141292 typedef struct yyParser yyParser;
161*c5c4113dSnw141292 
162*c5c4113dSnw141292 #ifndef NDEBUG
163*c5c4113dSnw141292 #include <stdio.h>
164*c5c4113dSnw141292 static FILE *yyTraceFILE = 0;
165*c5c4113dSnw141292 static char *yyTracePrompt = 0;
166*c5c4113dSnw141292 #endif /* NDEBUG */
167*c5c4113dSnw141292 
168*c5c4113dSnw141292 #ifndef NDEBUG
169*c5c4113dSnw141292 /*
170*c5c4113dSnw141292 ** Turn parser tracing on by giving a stream to which to write the trace
171*c5c4113dSnw141292 ** and a prompt to preface each trace message.  Tracing is turned off
172*c5c4113dSnw141292 ** by making either argument NULL
173*c5c4113dSnw141292 **
174*c5c4113dSnw141292 ** Inputs:
175*c5c4113dSnw141292 ** <ul>
176*c5c4113dSnw141292 ** <li> A FILE* to which trace output should be written.
177*c5c4113dSnw141292 **      If NULL, then tracing is turned off.
178*c5c4113dSnw141292 ** <li> A prefix string written at the beginning of every
179*c5c4113dSnw141292 **      line of trace output.  If NULL, then tracing is
180*c5c4113dSnw141292 **      turned off.
181*c5c4113dSnw141292 ** </ul>
182*c5c4113dSnw141292 **
183*c5c4113dSnw141292 ** Outputs:
184*c5c4113dSnw141292 ** None.
185*c5c4113dSnw141292 */
186*c5c4113dSnw141292 void ParseTrace(FILE *TraceFILE, char *zTracePrompt){
187*c5c4113dSnw141292   yyTraceFILE = TraceFILE;
188*c5c4113dSnw141292   yyTracePrompt = zTracePrompt;
189*c5c4113dSnw141292   if( yyTraceFILE==0 ) yyTracePrompt = 0;
190*c5c4113dSnw141292   else if( yyTracePrompt==0 ) yyTraceFILE = 0;
191*c5c4113dSnw141292 }
192*c5c4113dSnw141292 #endif /* NDEBUG */
193*c5c4113dSnw141292 
194*c5c4113dSnw141292 #ifndef NDEBUG
195*c5c4113dSnw141292 /* For tracing shifts, the names of all terminals and nonterminals
196*c5c4113dSnw141292 ** are required.  The following table supplies these names */
197*c5c4113dSnw141292 static const char *yyTokenName[] = {
198*c5c4113dSnw141292 %%
199*c5c4113dSnw141292 };
200*c5c4113dSnw141292 #endif /* NDEBUG */
201*c5c4113dSnw141292 
202*c5c4113dSnw141292 #ifndef NDEBUG
203*c5c4113dSnw141292 /* For tracing reduce actions, the names of all rules are required.
204*c5c4113dSnw141292 */
205*c5c4113dSnw141292 static const char *yyRuleName[] = {
206*c5c4113dSnw141292 %%
207*c5c4113dSnw141292 };
208*c5c4113dSnw141292 #endif /* NDEBUG */
209*c5c4113dSnw141292 
210*c5c4113dSnw141292 /*
211*c5c4113dSnw141292 ** This function returns the symbolic name associated with a token
212*c5c4113dSnw141292 ** value.
213*c5c4113dSnw141292 */
214*c5c4113dSnw141292 const char *ParseTokenName(int tokenType){
215*c5c4113dSnw141292 #ifndef NDEBUG
216*c5c4113dSnw141292   if( tokenType>0 && tokenType<(sizeof(yyTokenName)/sizeof(yyTokenName[0])) ){
217*c5c4113dSnw141292     return yyTokenName[tokenType];
218*c5c4113dSnw141292   }else{
219*c5c4113dSnw141292     return "Unknown";
220*c5c4113dSnw141292   }
221*c5c4113dSnw141292 #else
222*c5c4113dSnw141292   return "";
223*c5c4113dSnw141292 #endif
224*c5c4113dSnw141292 }
225*c5c4113dSnw141292 
226*c5c4113dSnw141292 /*
227*c5c4113dSnw141292 ** This function allocates a new parser.
228*c5c4113dSnw141292 ** The only argument is a pointer to a function which works like
229*c5c4113dSnw141292 ** malloc.
230*c5c4113dSnw141292 **
231*c5c4113dSnw141292 ** Inputs:
232*c5c4113dSnw141292 ** A pointer to the function used to allocate memory.
233*c5c4113dSnw141292 **
234*c5c4113dSnw141292 ** Outputs:
235*c5c4113dSnw141292 ** A pointer to a parser.  This pointer is used in subsequent calls
236*c5c4113dSnw141292 ** to Parse and ParseFree.
237*c5c4113dSnw141292 */
238*c5c4113dSnw141292 void *ParseAlloc(void *(*mallocProc)(size_t)){
239*c5c4113dSnw141292   yyParser *pParser;
240*c5c4113dSnw141292   pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) );
241*c5c4113dSnw141292   if( pParser ){
242*c5c4113dSnw141292     pParser->yyidx = -1;
243*c5c4113dSnw141292   }
244*c5c4113dSnw141292   return pParser;
245*c5c4113dSnw141292 }
246*c5c4113dSnw141292 
247*c5c4113dSnw141292 /* The following function deletes the value associated with a
248*c5c4113dSnw141292 ** symbol.  The symbol can be either a terminal or nonterminal.
249*c5c4113dSnw141292 ** "yymajor" is the symbol code, and "yypminor" is a pointer to
250*c5c4113dSnw141292 ** the value.
251*c5c4113dSnw141292 */
252*c5c4113dSnw141292 static void yy_destructor(YYCODETYPE yymajor, YYMINORTYPE *yypminor){
253*c5c4113dSnw141292   switch( yymajor ){
254*c5c4113dSnw141292     /* Here is inserted the actions which take place when a
255*c5c4113dSnw141292     ** terminal or non-terminal is destroyed.  This can happen
256*c5c4113dSnw141292     ** when the symbol is popped from the stack during a
257*c5c4113dSnw141292     ** reduce or during error processing or when a parser is
258*c5c4113dSnw141292     ** being destroyed before it is finished parsing.
259*c5c4113dSnw141292     **
260*c5c4113dSnw141292     ** Note: during a reduce, the only symbols destroyed are those
261*c5c4113dSnw141292     ** which appear on the RHS of the rule, but which are not used
262*c5c4113dSnw141292     ** inside the C code.
263*c5c4113dSnw141292     */
264*c5c4113dSnw141292 %%
265*c5c4113dSnw141292     default:  break;   /* If no destructor action specified: do nothing */
266*c5c4113dSnw141292   }
267*c5c4113dSnw141292 }
268*c5c4113dSnw141292 
269*c5c4113dSnw141292 /*
270*c5c4113dSnw141292 ** Pop the parser's stack once.
271*c5c4113dSnw141292 **
272*c5c4113dSnw141292 ** If there is a destructor routine associated with the token which
273*c5c4113dSnw141292 ** is popped from the stack, then call it.
274*c5c4113dSnw141292 **
275*c5c4113dSnw141292 ** Return the major token number for the symbol popped.
276*c5c4113dSnw141292 */
277*c5c4113dSnw141292 static int yy_pop_parser_stack(yyParser *pParser){
278*c5c4113dSnw141292   YYCODETYPE yymajor;
279*c5c4113dSnw141292   yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
280*c5c4113dSnw141292 
281*c5c4113dSnw141292   if( pParser->yyidx<0 ) return 0;
282*c5c4113dSnw141292 #ifndef NDEBUG
283*c5c4113dSnw141292   if( yyTraceFILE && pParser->yyidx>=0 ){
284*c5c4113dSnw141292     fprintf(yyTraceFILE,"%sPopping %s\n",
285*c5c4113dSnw141292       yyTracePrompt,
286*c5c4113dSnw141292       yyTokenName[yytos->major]);
287*c5c4113dSnw141292   }
288*c5c4113dSnw141292 #endif
289*c5c4113dSnw141292   yymajor = yytos->major;
290*c5c4113dSnw141292   yy_destructor( yymajor, &yytos->minor);
291*c5c4113dSnw141292   pParser->yyidx--;
292*c5c4113dSnw141292   return yymajor;
293*c5c4113dSnw141292 }
294*c5c4113dSnw141292 
295*c5c4113dSnw141292 /*
296*c5c4113dSnw141292 ** Deallocate and destroy a parser.  Destructors are all called for
297*c5c4113dSnw141292 ** all stack elements before shutting the parser down.
298*c5c4113dSnw141292 **
299*c5c4113dSnw141292 ** Inputs:
300*c5c4113dSnw141292 ** <ul>
301*c5c4113dSnw141292 ** <li>  A pointer to the parser.  This should be a pointer
302*c5c4113dSnw141292 **       obtained from ParseAlloc.
303*c5c4113dSnw141292 ** <li>  A pointer to a function used to reclaim memory obtained
304*c5c4113dSnw141292 **       from malloc.
305*c5c4113dSnw141292 ** </ul>
306*c5c4113dSnw141292 */
307*c5c4113dSnw141292 void ParseFree(
308*c5c4113dSnw141292   void *p,                    /* The parser to be deleted */
309*c5c4113dSnw141292   void (*freeProc)(void*)     /* Function used to reclaim memory */
310*c5c4113dSnw141292 ){
311*c5c4113dSnw141292   yyParser *pParser = (yyParser*)p;
312*c5c4113dSnw141292   if( pParser==0 ) return;
313*c5c4113dSnw141292   while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
314*c5c4113dSnw141292   (*freeProc)((void*)pParser);
315*c5c4113dSnw141292 }
316*c5c4113dSnw141292 
317*c5c4113dSnw141292 /*
318*c5c4113dSnw141292 ** Find the appropriate action for a parser given the terminal
319*c5c4113dSnw141292 ** look-ahead token iLookAhead.
320*c5c4113dSnw141292 **
321*c5c4113dSnw141292 ** If the look-ahead token is YYNOCODE, then check to see if the action is
322*c5c4113dSnw141292 ** independent of the look-ahead.  If it is, return the action, otherwise
323*c5c4113dSnw141292 ** return YY_NO_ACTION.
324*c5c4113dSnw141292 */
325*c5c4113dSnw141292 static int yy_find_shift_action(
326*c5c4113dSnw141292   yyParser *pParser,        /* The parser */
327*c5c4113dSnw141292   int iLookAhead            /* The look-ahead token */
328*c5c4113dSnw141292 ){
329*c5c4113dSnw141292   int i;
330*c5c4113dSnw141292   int stateno = pParser->yystack[pParser->yyidx].stateno;
331*c5c4113dSnw141292 
332*c5c4113dSnw141292   /* if( pParser->yyidx<0 ) return YY_NO_ACTION;  */
333*c5c4113dSnw141292   i = yy_shift_ofst[stateno];
334*c5c4113dSnw141292   if( i==YY_SHIFT_USE_DFLT ){
335*c5c4113dSnw141292     return yy_default[stateno];
336*c5c4113dSnw141292   }
337*c5c4113dSnw141292   if( iLookAhead==YYNOCODE ){
338*c5c4113dSnw141292     return YY_NO_ACTION;
339*c5c4113dSnw141292   }
340*c5c4113dSnw141292   i += iLookAhead;
341*c5c4113dSnw141292   if( i<0 || i>=YY_SZ_ACTTAB || yy_lookahead[i]!=iLookAhead ){
342*c5c4113dSnw141292 #ifdef YYFALLBACK
343*c5c4113dSnw141292     int iFallback;            /* Fallback token */
344*c5c4113dSnw141292     if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
345*c5c4113dSnw141292            && (iFallback = yyFallback[iLookAhead])!=0 ){
346*c5c4113dSnw141292 #ifndef NDEBUG
347*c5c4113dSnw141292       if( yyTraceFILE ){
348*c5c4113dSnw141292         fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
349*c5c4113dSnw141292            yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
350*c5c4113dSnw141292       }
351*c5c4113dSnw141292 #endif
352*c5c4113dSnw141292       return yy_find_shift_action(pParser, iFallback);
353*c5c4113dSnw141292     }
354*c5c4113dSnw141292 #endif
355*c5c4113dSnw141292     return yy_default[stateno];
356*c5c4113dSnw141292   }else{
357*c5c4113dSnw141292     return yy_action[i];
358*c5c4113dSnw141292   }
359*c5c4113dSnw141292 }
360*c5c4113dSnw141292 
361*c5c4113dSnw141292 /*
362*c5c4113dSnw141292 ** Find the appropriate action for a parser given the non-terminal
363*c5c4113dSnw141292 ** look-ahead token iLookAhead.
364*c5c4113dSnw141292 **
365*c5c4113dSnw141292 ** If the look-ahead token is YYNOCODE, then check to see if the action is
366*c5c4113dSnw141292 ** independent of the look-ahead.  If it is, return the action, otherwise
367*c5c4113dSnw141292 ** return YY_NO_ACTION.
368*c5c4113dSnw141292 */
369*c5c4113dSnw141292 static int yy_find_reduce_action(
370*c5c4113dSnw141292   yyParser *pParser,        /* The parser */
371*c5c4113dSnw141292   int iLookAhead            /* The look-ahead token */
372*c5c4113dSnw141292 ){
373*c5c4113dSnw141292   int i;
374*c5c4113dSnw141292   int stateno = pParser->yystack[pParser->yyidx].stateno;
375*c5c4113dSnw141292 
376*c5c4113dSnw141292   i = yy_reduce_ofst[stateno];
377*c5c4113dSnw141292   if( i==YY_REDUCE_USE_DFLT ){
378*c5c4113dSnw141292     return yy_default[stateno];
379*c5c4113dSnw141292   }
380*c5c4113dSnw141292   if( iLookAhead==YYNOCODE ){
381*c5c4113dSnw141292     return YY_NO_ACTION;
382*c5c4113dSnw141292   }
383*c5c4113dSnw141292   i += iLookAhead;
384*c5c4113dSnw141292   if( i<0 || i>=YY_SZ_ACTTAB || yy_lookahead[i]!=iLookAhead ){
385*c5c4113dSnw141292     return yy_default[stateno];
386*c5c4113dSnw141292   }else{
387*c5c4113dSnw141292     return yy_action[i];
388*c5c4113dSnw141292   }
389*c5c4113dSnw141292 }
390*c5c4113dSnw141292 
391*c5c4113dSnw141292 /*
392*c5c4113dSnw141292 ** Perform a shift action.
393*c5c4113dSnw141292 */
394*c5c4113dSnw141292 static void yy_shift(
395*c5c4113dSnw141292   yyParser *yypParser,          /* The parser to be shifted */
396*c5c4113dSnw141292   int yyNewState,               /* The new state to shift in */
397*c5c4113dSnw141292   int yyMajor,                  /* The major token to shift in */
398*c5c4113dSnw141292   YYMINORTYPE *yypMinor         /* Pointer ot the minor token to shift in */
399*c5c4113dSnw141292 ){
400*c5c4113dSnw141292   yyStackEntry *yytos;
401*c5c4113dSnw141292   yypParser->yyidx++;
402*c5c4113dSnw141292   if( yypParser->yyidx>=YYSTACKDEPTH ){
403*c5c4113dSnw141292      ParseARG_FETCH;
404*c5c4113dSnw141292      yypParser->yyidx--;
405*c5c4113dSnw141292 #ifndef NDEBUG
406*c5c4113dSnw141292      if( yyTraceFILE ){
407*c5c4113dSnw141292        fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
408*c5c4113dSnw141292      }
409*c5c4113dSnw141292 #endif
410*c5c4113dSnw141292      while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
411*c5c4113dSnw141292      /* Here code is inserted which will execute if the parser
412*c5c4113dSnw141292      ** stack every overflows */
413*c5c4113dSnw141292 %%
414*c5c4113dSnw141292      ParseARG_STORE; /* Suppress warning about unused %extra_argument var */
415*c5c4113dSnw141292      return;
416*c5c4113dSnw141292   }
417*c5c4113dSnw141292   yytos = &yypParser->yystack[yypParser->yyidx];
418*c5c4113dSnw141292   yytos->stateno = yyNewState;
419*c5c4113dSnw141292   yytos->major = yyMajor;
420*c5c4113dSnw141292   yytos->minor = *yypMinor;
421*c5c4113dSnw141292 #ifndef NDEBUG
422*c5c4113dSnw141292   if( yyTraceFILE && yypParser->yyidx>0 ){
423*c5c4113dSnw141292     int i;
424*c5c4113dSnw141292     fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
425*c5c4113dSnw141292     fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
426*c5c4113dSnw141292     for(i=1; i<=yypParser->yyidx; i++)
427*c5c4113dSnw141292       fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
428*c5c4113dSnw141292     fprintf(yyTraceFILE,"\n");
429*c5c4113dSnw141292   }
430*c5c4113dSnw141292 #endif
431*c5c4113dSnw141292 }
432*c5c4113dSnw141292 
433*c5c4113dSnw141292 /* The following table contains information about every rule that
434*c5c4113dSnw141292 ** is used during the reduce.
435*c5c4113dSnw141292 */
436*c5c4113dSnw141292 static struct {
437*c5c4113dSnw141292   YYCODETYPE lhs;         /* Symbol on the left-hand side of the rule */
438*c5c4113dSnw141292   unsigned char nrhs;     /* Number of right-hand side symbols in the rule */
439*c5c4113dSnw141292 } yyRuleInfo[] = {
440*c5c4113dSnw141292 %%
441*c5c4113dSnw141292 };
442*c5c4113dSnw141292 
443*c5c4113dSnw141292 static void yy_accept(yyParser*);  /* Forward Declaration */
444*c5c4113dSnw141292 
445*c5c4113dSnw141292 /*
446*c5c4113dSnw141292 ** Perform a reduce action and the shift that must immediately
447*c5c4113dSnw141292 ** follow the reduce.
448*c5c4113dSnw141292 */
449*c5c4113dSnw141292 static void yy_reduce(
450*c5c4113dSnw141292   yyParser *yypParser,         /* The parser */
451*c5c4113dSnw141292   int yyruleno                 /* Number of the rule by which to reduce */
452*c5c4113dSnw141292 ){
453*c5c4113dSnw141292   int yygoto;                     /* The next state */
454*c5c4113dSnw141292   int yyact;                      /* The next action */
455*c5c4113dSnw141292   YYMINORTYPE yygotominor;        /* The LHS of the rule reduced */
456*c5c4113dSnw141292   yyStackEntry *yymsp;            /* The top of the parser's stack */
457*c5c4113dSnw141292   int yysize;                     /* Amount to pop the stack */
458*c5c4113dSnw141292   ParseARG_FETCH;
459*c5c4113dSnw141292   yymsp = &yypParser->yystack[yypParser->yyidx];
460*c5c4113dSnw141292 #ifndef NDEBUG
461*c5c4113dSnw141292   if( yyTraceFILE && yyruleno>=0
462*c5c4113dSnw141292         && yyruleno<sizeof(yyRuleName)/sizeof(yyRuleName[0]) ){
463*c5c4113dSnw141292     fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt,
464*c5c4113dSnw141292       yyRuleName[yyruleno]);
465*c5c4113dSnw141292   }
466*c5c4113dSnw141292 #endif /* NDEBUG */
467*c5c4113dSnw141292 
468*c5c4113dSnw141292   switch( yyruleno ){
469*c5c4113dSnw141292   /* Beginning here are the reduction cases.  A typical example
470*c5c4113dSnw141292   ** follows:
471*c5c4113dSnw141292   **   case 0:
472*c5c4113dSnw141292   **  #line <lineno> <grammarfile>
473*c5c4113dSnw141292   **     { ... }           // User supplied code
474*c5c4113dSnw141292   **  #line <lineno> <thisfile>
475*c5c4113dSnw141292   **     break;
476*c5c4113dSnw141292   */
477*c5c4113dSnw141292 %%
478*c5c4113dSnw141292   };
479*c5c4113dSnw141292   yygoto = yyRuleInfo[yyruleno].lhs;
480*c5c4113dSnw141292   yysize = yyRuleInfo[yyruleno].nrhs;
481*c5c4113dSnw141292   yypParser->yyidx -= yysize;
482*c5c4113dSnw141292   yyact = yy_find_reduce_action(yypParser,yygoto);
483*c5c4113dSnw141292   if( yyact < YYNSTATE ){
484*c5c4113dSnw141292     yy_shift(yypParser,yyact,yygoto,&yygotominor);
485*c5c4113dSnw141292   }else if( yyact == YYNSTATE + YYNRULE + 1 ){
486*c5c4113dSnw141292     yy_accept(yypParser);
487*c5c4113dSnw141292   }
488*c5c4113dSnw141292 }
489*c5c4113dSnw141292 
490*c5c4113dSnw141292 /*
491*c5c4113dSnw141292 ** The following code executes when the parse fails
492*c5c4113dSnw141292 */
493*c5c4113dSnw141292 static void yy_parse_failed(
494*c5c4113dSnw141292   yyParser *yypParser           /* The parser */
495*c5c4113dSnw141292 ){
496*c5c4113dSnw141292   ParseARG_FETCH;
497*c5c4113dSnw141292 #ifndef NDEBUG
498*c5c4113dSnw141292   if( yyTraceFILE ){
499*c5c4113dSnw141292     fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
500*c5c4113dSnw141292   }
501*c5c4113dSnw141292 #endif
502*c5c4113dSnw141292   while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
503*c5c4113dSnw141292   /* Here code is inserted which will be executed whenever the
504*c5c4113dSnw141292   ** parser fails */
505*c5c4113dSnw141292 %%
506*c5c4113dSnw141292   ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
507*c5c4113dSnw141292 }
508*c5c4113dSnw141292 
509*c5c4113dSnw141292 /*
510*c5c4113dSnw141292 ** The following code executes when a syntax error first occurs.
511*c5c4113dSnw141292 */
512*c5c4113dSnw141292 static void yy_syntax_error(
513*c5c4113dSnw141292   yyParser *yypParser,           /* The parser */
514*c5c4113dSnw141292   int yymajor,                   /* The major type of the error token */
515*c5c4113dSnw141292   YYMINORTYPE yyminor            /* The minor type of the error token */
516*c5c4113dSnw141292 ){
517*c5c4113dSnw141292   ParseARG_FETCH;
518*c5c4113dSnw141292 #define TOKEN (yyminor.yy0)
519*c5c4113dSnw141292 %%
520*c5c4113dSnw141292   ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
521*c5c4113dSnw141292 }
522*c5c4113dSnw141292 
523*c5c4113dSnw141292 /*
524*c5c4113dSnw141292 ** The following is executed when the parser accepts
525*c5c4113dSnw141292 */
526*c5c4113dSnw141292 static void yy_accept(
527*c5c4113dSnw141292   yyParser *yypParser           /* The parser */
528*c5c4113dSnw141292 ){
529*c5c4113dSnw141292   ParseARG_FETCH;
530*c5c4113dSnw141292 #ifndef NDEBUG
531*c5c4113dSnw141292   if( yyTraceFILE ){
532*c5c4113dSnw141292     fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
533*c5c4113dSnw141292   }
534*c5c4113dSnw141292 #endif
535*c5c4113dSnw141292   while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
536*c5c4113dSnw141292   /* Here code is inserted which will be executed whenever the
537*c5c4113dSnw141292   ** parser accepts */
538*c5c4113dSnw141292 %%
539*c5c4113dSnw141292   ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
540*c5c4113dSnw141292 }
541*c5c4113dSnw141292 
542*c5c4113dSnw141292 /* The main parser program.
543*c5c4113dSnw141292 ** The first argument is a pointer to a structure obtained from
544*c5c4113dSnw141292 ** "ParseAlloc" which describes the current state of the parser.
545*c5c4113dSnw141292 ** The second argument is the major token number.  The third is
546*c5c4113dSnw141292 ** the minor token.  The fourth optional argument is whatever the
547*c5c4113dSnw141292 ** user wants (and specified in the grammar) and is available for
548*c5c4113dSnw141292 ** use by the action routines.
549*c5c4113dSnw141292 **
550*c5c4113dSnw141292 ** Inputs:
551*c5c4113dSnw141292 ** <ul>
552*c5c4113dSnw141292 ** <li> A pointer to the parser (an opaque structure.)
553*c5c4113dSnw141292 ** <li> The major token number.
554*c5c4113dSnw141292 ** <li> The minor token number.
555*c5c4113dSnw141292 ** <li> An option argument of a grammar-specified type.
556*c5c4113dSnw141292 ** </ul>
557*c5c4113dSnw141292 **
558*c5c4113dSnw141292 ** Outputs:
559*c5c4113dSnw141292 ** None.
560*c5c4113dSnw141292 */
561*c5c4113dSnw141292 void Parse(
562*c5c4113dSnw141292   void *yyp,                   /* The parser */
563*c5c4113dSnw141292   int yymajor,                 /* The major token code number */
564*c5c4113dSnw141292   ParseTOKENTYPE yyminor       /* The value for the token */
565*c5c4113dSnw141292   ParseARG_PDECL               /* Optional %extra_argument parameter */
566*c5c4113dSnw141292 ){
567*c5c4113dSnw141292   YYMINORTYPE yyminorunion;
568*c5c4113dSnw141292   int yyact;            /* The parser action. */
569*c5c4113dSnw141292   int yyendofinput;     /* True if we are at the end of input */
570*c5c4113dSnw141292   int yyerrorhit = 0;   /* True if yymajor has invoked an error */
571*c5c4113dSnw141292   yyParser *yypParser;  /* The parser */
572*c5c4113dSnw141292 
573*c5c4113dSnw141292   /* (re)initialize the parser, if necessary */
574*c5c4113dSnw141292   yypParser = (yyParser*)yyp;
575*c5c4113dSnw141292   if( yypParser->yyidx<0 ){
576*c5c4113dSnw141292     if( yymajor==0 ) return;
577*c5c4113dSnw141292     yypParser->yyidx = 0;
578*c5c4113dSnw141292     yypParser->yyerrcnt = -1;
579*c5c4113dSnw141292     yypParser->yystack[0].stateno = 0;
580*c5c4113dSnw141292     yypParser->yystack[0].major = 0;
581*c5c4113dSnw141292   }
582*c5c4113dSnw141292   yyminorunion.yy0 = yyminor;
583*c5c4113dSnw141292   yyendofinput = (yymajor==0);
584*c5c4113dSnw141292   ParseARG_STORE;
585*c5c4113dSnw141292 
586*c5c4113dSnw141292 #ifndef NDEBUG
587*c5c4113dSnw141292   if( yyTraceFILE ){
588*c5c4113dSnw141292     fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
589*c5c4113dSnw141292   }
590*c5c4113dSnw141292 #endif
591*c5c4113dSnw141292 
592*c5c4113dSnw141292   do{
593*c5c4113dSnw141292     yyact = yy_find_shift_action(yypParser,yymajor);
594*c5c4113dSnw141292     if( yyact<YYNSTATE ){
595*c5c4113dSnw141292       yy_shift(yypParser,yyact,yymajor,&yyminorunion);
596*c5c4113dSnw141292       yypParser->yyerrcnt--;
597*c5c4113dSnw141292       if( yyendofinput && yypParser->yyidx>=0 ){
598*c5c4113dSnw141292         yymajor = 0;
599*c5c4113dSnw141292       }else{
600*c5c4113dSnw141292         yymajor = YYNOCODE;
601*c5c4113dSnw141292       }
602*c5c4113dSnw141292     }else if( yyact < YYNSTATE + YYNRULE ){
603*c5c4113dSnw141292       yy_reduce(yypParser,yyact-YYNSTATE);
604*c5c4113dSnw141292     }else if( yyact == YY_ERROR_ACTION ){
605*c5c4113dSnw141292       int yymx;
606*c5c4113dSnw141292 #ifndef NDEBUG
607*c5c4113dSnw141292       if( yyTraceFILE ){
608*c5c4113dSnw141292         fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
609*c5c4113dSnw141292       }
610*c5c4113dSnw141292 #endif
611*c5c4113dSnw141292 #ifdef YYERRORSYMBOL
612*c5c4113dSnw141292       /* A syntax error has occurred.
613*c5c4113dSnw141292       ** The response to an error depends upon whether or not the
614*c5c4113dSnw141292       ** grammar defines an error token "ERROR".
615*c5c4113dSnw141292       **
616*c5c4113dSnw141292       ** This is what we do if the grammar does define ERROR:
617*c5c4113dSnw141292       **
618*c5c4113dSnw141292       **  * Call the %syntax_error function.
619*c5c4113dSnw141292       **
620*c5c4113dSnw141292       **  * Begin popping the stack until we enter a state where
621*c5c4113dSnw141292       **    it is legal to shift the error symbol, then shift
622*c5c4113dSnw141292       **    the error symbol.
623*c5c4113dSnw141292       **
624*c5c4113dSnw141292       **  * Set the error count to three.
625*c5c4113dSnw141292       **
626*c5c4113dSnw141292       **  * Begin accepting and shifting new tokens.  No new error
627*c5c4113dSnw141292       **    processing will occur until three tokens have been
628*c5c4113dSnw141292       **    shifted successfully.
629*c5c4113dSnw141292       **
630*c5c4113dSnw141292       */
631*c5c4113dSnw141292       if( yypParser->yyerrcnt<0 ){
632*c5c4113dSnw141292         yy_syntax_error(yypParser,yymajor,yyminorunion);
633*c5c4113dSnw141292       }
634*c5c4113dSnw141292       yymx = yypParser->yystack[yypParser->yyidx].major;
635*c5c4113dSnw141292       if( yymx==YYERRORSYMBOL || yyerrorhit ){
636*c5c4113dSnw141292 #ifndef NDEBUG
637*c5c4113dSnw141292         if( yyTraceFILE ){
638*c5c4113dSnw141292           fprintf(yyTraceFILE,"%sDiscard input token %s\n",
639*c5c4113dSnw141292              yyTracePrompt,yyTokenName[yymajor]);
640*c5c4113dSnw141292         }
641*c5c4113dSnw141292 #endif
642*c5c4113dSnw141292         yy_destructor(yymajor,&yyminorunion);
643*c5c4113dSnw141292         yymajor = YYNOCODE;
644*c5c4113dSnw141292       }else{
645*c5c4113dSnw141292          while(
646*c5c4113dSnw141292           yypParser->yyidx >= 0 &&
647*c5c4113dSnw141292           yymx != YYERRORSYMBOL &&
648*c5c4113dSnw141292           (yyact = yy_find_shift_action(yypParser,YYERRORSYMBOL)) >= YYNSTATE
649*c5c4113dSnw141292         ){
650*c5c4113dSnw141292           yy_pop_parser_stack(yypParser);
651*c5c4113dSnw141292         }
652*c5c4113dSnw141292         if( yypParser->yyidx < 0 || yymajor==0 ){
653*c5c4113dSnw141292           yy_destructor(yymajor,&yyminorunion);
654*c5c4113dSnw141292           yy_parse_failed(yypParser);
655*c5c4113dSnw141292           yymajor = YYNOCODE;
656*c5c4113dSnw141292         }else if( yymx!=YYERRORSYMBOL ){
657*c5c4113dSnw141292           YYMINORTYPE u2;
658*c5c4113dSnw141292           u2.YYERRSYMDT = 0;
659*c5c4113dSnw141292           yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
660*c5c4113dSnw141292         }
661*c5c4113dSnw141292       }
662*c5c4113dSnw141292       yypParser->yyerrcnt = 3;
663*c5c4113dSnw141292       yyerrorhit = 1;
664*c5c4113dSnw141292 #else  /* YYERRORSYMBOL is not defined */
665*c5c4113dSnw141292       /* This is what we do if the grammar does not define ERROR:
666*c5c4113dSnw141292       **
667*c5c4113dSnw141292       **  * Report an error message, and throw away the input token.
668*c5c4113dSnw141292       **
669*c5c4113dSnw141292       **  * If the input token is $, then fail the parse.
670*c5c4113dSnw141292       **
671*c5c4113dSnw141292       ** As before, subsequent error messages are suppressed until
672*c5c4113dSnw141292       ** three input tokens have been successfully shifted.
673*c5c4113dSnw141292       */
674*c5c4113dSnw141292       if( yypParser->yyerrcnt<=0 ){
675*c5c4113dSnw141292         yy_syntax_error(yypParser,yymajor,yyminorunion);
676*c5c4113dSnw141292       }
677*c5c4113dSnw141292       yypParser->yyerrcnt = 3;
678*c5c4113dSnw141292       yy_destructor(yymajor,&yyminorunion);
679*c5c4113dSnw141292       if( yyendofinput ){
680*c5c4113dSnw141292         yy_parse_failed(yypParser);
681*c5c4113dSnw141292       }
682*c5c4113dSnw141292       yymajor = YYNOCODE;
683*c5c4113dSnw141292 #endif
684*c5c4113dSnw141292     }else{
685*c5c4113dSnw141292       yy_accept(yypParser);
686*c5c4113dSnw141292       yymajor = YYNOCODE;
687*c5c4113dSnw141292     }
688*c5c4113dSnw141292   }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
689*c5c4113dSnw141292   return;
690*c5c4113dSnw141292 }
691