blob: 5604fe10d47b2f8049d8ae5dadb0415b47bb2851 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/* Driver template for the LEMON parser generator.
drhb19a2bc2001-09-16 00:13:26 +00002** The author disclaims copyright to this source code.
drh75897232000-05-29 14:26:00 +00003*/
4/* First off, code is include which follows the "include" declaration
5** in the input file. */
6#include <stdio.h>
7%%
8/* Next is all token values, in a form suitable for use by makeheaders.
9** This section will be null unless lemon is run with the -m switch.
10*/
11/*
12** These constants (all generated automatically by the parser generator)
13** specify the various kinds of tokens (terminals) that the parser
14** understands.
15**
16** Each symbol here is a terminal symbol in the grammar.
17*/
18%%
19/* Make sure the INTERFACE macro is defined.
20*/
21#ifndef INTERFACE
22# define INTERFACE 1
23#endif
24/* The next thing included is series of defines which control
25** various aspects of the generated parser.
26** YYCODETYPE is the data type used for storing terminal
27** and nonterminal numbers. "unsigned char" is
28** used if there are fewer than 250 terminals
29** and nonterminals. "int" is used otherwise.
30** YYNOCODE is a number of type YYCODETYPE which corresponds
31** to no legal terminal or nonterminal number. This
32** number is used to fill in empty slots of the hash
33** table.
drh0bd1f4e2002-06-06 18:54:39 +000034** YYFALLBACK If defined, this indicates that one or more tokens
35** have fall-back values which should be used if the
36** original value of the token will not parse.
drh75897232000-05-29 14:26:00 +000037** YYACTIONTYPE is the data type used for storing terminal
38** and nonterminal numbers. "unsigned char" is
39** used if there are fewer than 250 rules and
40** states combined. "int" is used otherwise.
41** ParseTOKENTYPE is the data type used for minor tokens given
42** directly to the parser from the tokenizer.
43** YYMINORTYPE is the data type used for all minor tokens.
44** This is typically a union of many types, one of
45** which is ParseTOKENTYPE. The entry in the union
46** for base tokens is called "yy0".
47** YYSTACKDEPTH is the maximum depth of the parser's stack.
drh1f245e42002-03-11 13:55:50 +000048** ParseARG_SDECL A static variable declaration for the %extra_argument
49** ParseARG_PDECL A parameter declaration for the %extra_argument
50** ParseARG_STORE Code to store %extra_argument into yypParser
51** ParseARG_FETCH Code to extract %extra_argument from yypParser
drh75897232000-05-29 14:26:00 +000052** YYNSTATE the combined number of states.
53** YYNRULE the number of rules in the grammar
54** YYERRORSYMBOL is the code number of the error symbol. If not
55** defined, then do no error processing.
56*/
57%%
58#define YY_NO_ACTION (YYNSTATE+YYNRULE+2)
59#define YY_ACCEPT_ACTION (YYNSTATE+YYNRULE+1)
60#define YY_ERROR_ACTION (YYNSTATE+YYNRULE)
61/* Next is the action table. Each entry in this table contains
62**
63** + An integer which is the number representing the look-ahead
64** token
65**
66** + An integer indicating what action to take. Number (N) between
67** 0 and YYNSTATE-1 mean shift the look-ahead and go to state N.
68** Numbers between YYNSTATE and YYNSTATE+YYNRULE-1 mean reduce by
69** rule N-YYNSTATE. Number YYNSTATE+YYNRULE means that a syntax
70** error has occurred. Number YYNSTATE+YYNRULE+1 means the parser
71** accepts its input.
72**
73** + A pointer to the next entry with the same hash value.
74**
75** The action table is really a series of hash tables. Each hash
76** table contains a number of entries which is a power of two. The
77** "state" table (which follows) contains information about the starting
78** point and size of each hash table.
79*/
80struct yyActionEntry {
81 YYCODETYPE lookahead; /* The value of the look-ahead token */
drhb29b0a52002-02-23 19:39:46 +000082 YYCODETYPE next; /* Next entry + 1. Zero at end of collision chain */
drh75897232000-05-29 14:26:00 +000083 YYACTIONTYPE action; /* Action to take for this look-ahead */
drh75897232000-05-29 14:26:00 +000084};
drh1f245e42002-03-11 13:55:50 +000085typedef struct yyActionEntry yyActionEntry;
86static const yyActionEntry yyActionTable[] = {
drh75897232000-05-29 14:26:00 +000087%%
88};
89
90/* The state table contains information needed to look up the correct
91** action in the action table, given the current state of the parser.
92** Information needed includes:
93**
94** + A pointer to the start of the action hash table in yyActionTable.
95**
drhb29b0a52002-02-23 19:39:46 +000096** + The number of entries in the action hash table.
drh75897232000-05-29 14:26:00 +000097**
98** + The default action. This is the action to take if no entry for
99** the given look-ahead is found in the action hash table.
100*/
101struct yyStateEntry {
drh1f245e42002-03-11 13:55:50 +0000102 const yyActionEntry *hashtbl; /* Start of the hash table in yyActionTable */
drhb29b0a52002-02-23 19:39:46 +0000103 YYCODETYPE nEntry; /* Number of entries in action hash table */
drh75897232000-05-29 14:26:00 +0000104 YYACTIONTYPE actionDefault; /* Default action if look-ahead not found */
105};
drh1f245e42002-03-11 13:55:50 +0000106typedef struct yyStateEntry yyStateEntry;
107static const yyStateEntry yyStateTable[] = {
drh75897232000-05-29 14:26:00 +0000108%%
109};
110
drh0bd1f4e2002-06-06 18:54:39 +0000111/* The next table maps tokens into fallback tokens. If a construct
112** like the following:
113**
114** %fallback ID X Y Z.
115**
116** appears in the grammer, then ID becomes a fallback token for X, Y,
117** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
118** but it does not parse, the type of the token is changed to ID and
119** the parse is retried before an error is thrown.
120*/
121#ifdef YYFALLBACK
122static const YYCODETYPE yyFallback[] = {
123%%
124};
125#endif /* YYFALLBACK */
126
drh75897232000-05-29 14:26:00 +0000127/* The following structure represents a single element of the
128** parser's stack. Information stored includes:
129**
130** + The state number for the parser at this level of the stack.
131**
132** + The value of the token stored at this level of the stack.
133** (In other words, the "major" token.)
134**
135** + The semantic value stored at this level of the stack. This is
136** the information used by the action routines in the grammar.
137** It is sometimes called the "minor" token.
138*/
139struct yyStackEntry {
140 int stateno; /* The state-number */
141 int major; /* The major token value. This is the code
142 ** number for the token at this stack level */
143 YYMINORTYPE minor; /* The user-supplied minor token value. This
144 ** is the value of the token */
145};
drh1f245e42002-03-11 13:55:50 +0000146typedef struct yyStackEntry yyStackEntry;
drh75897232000-05-29 14:26:00 +0000147
148/* The state of the parser is completely contained in an instance of
149** the following structure */
150struct yyParser {
drh1f245e42002-03-11 13:55:50 +0000151 int yyidx; /* Index of top element in stack */
152 int yyerrcnt; /* Shifts left before out of the error */
153 yyStackEntry *yytop; /* Pointer to the top stack element */
154 ParseARG_SDECL /* A place to hold %extra_argument */
155 yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */
drh75897232000-05-29 14:26:00 +0000156};
157typedef struct yyParser yyParser;
158
159#ifndef NDEBUG
160#include <stdio.h>
161static FILE *yyTraceFILE = 0;
162static char *yyTracePrompt = 0;
drh0bd1f4e2002-06-06 18:54:39 +0000163#endif /* NDEBUG */
drh75897232000-05-29 14:26:00 +0000164
drh0bd1f4e2002-06-06 18:54:39 +0000165#ifndef NDEBUG
drh75897232000-05-29 14:26:00 +0000166/*
167** Turn parser tracing on by giving a stream to which to write the trace
168** and a prompt to preface each trace message. Tracing is turned off
169** by making either argument NULL
170**
171** Inputs:
172** <ul>
173** <li> A FILE* to which trace output should be written.
174** If NULL, then tracing is turned off.
175** <li> A prefix string written at the beginning of every
176** line of trace output. If NULL, then tracing is
177** turned off.
178** </ul>
179**
180** Outputs:
181** None.
182*/
drh75897232000-05-29 14:26:00 +0000183void ParseTrace(FILE *TraceFILE, char *zTracePrompt){
184 yyTraceFILE = TraceFILE;
185 yyTracePrompt = zTracePrompt;
186 if( yyTraceFILE==0 ) yyTracePrompt = 0;
187 else if( yyTracePrompt==0 ) yyTraceFILE = 0;
188}
drh0bd1f4e2002-06-06 18:54:39 +0000189#endif /* NDEBUG */
drh75897232000-05-29 14:26:00 +0000190
drh0bd1f4e2002-06-06 18:54:39 +0000191#ifndef NDEBUG
drh75897232000-05-29 14:26:00 +0000192/* For tracing shifts, the names of all terminals and nonterminals
193** are required. The following table supplies these names */
drh1f245e42002-03-11 13:55:50 +0000194static const char *yyTokenName[] = {
drh75897232000-05-29 14:26:00 +0000195%%
196};
drh0bd1f4e2002-06-06 18:54:39 +0000197#endif /* NDEBUG */
drh75897232000-05-29 14:26:00 +0000198
drh0bd1f4e2002-06-06 18:54:39 +0000199#ifndef NDEBUG
200/* For tracing reduce actions, the names of all rules are required.
201*/
202static const char *yyRuleName[] = {
203%%
204};
205#endif /* NDEBUG */
drha1b351a2001-09-14 16:42:12 +0000206
drh960e8c62001-04-03 16:53:21 +0000207/*
208** This function returns the symbolic name associated with a token
209** value.
210*/
211const char *ParseTokenName(int tokenType){
drha1b351a2001-09-14 16:42:12 +0000212#ifndef NDEBUG
drh960e8c62001-04-03 16:53:21 +0000213 if( tokenType>0 && tokenType<(sizeof(yyTokenName)/sizeof(yyTokenName[0])) ){
214 return yyTokenName[tokenType];
215 }else{
216 return "Unknown";
217 }
drha1b351a2001-09-14 16:42:12 +0000218#else
219 return "";
220#endif
drh960e8c62001-04-03 16:53:21 +0000221}
222
drh75897232000-05-29 14:26:00 +0000223/*
224** This function allocates a new parser.
225** The only argument is a pointer to a function which works like
226** malloc.
227**
228** Inputs:
229** A pointer to the function used to allocate memory.
230**
231** Outputs:
232** A pointer to a parser. This pointer is used in subsequent calls
233** to Parse and ParseFree.
234*/
drh7218ac72002-03-10 21:21:00 +0000235void *ParseAlloc(void *(*mallocProc)(size_t)){
drh75897232000-05-29 14:26:00 +0000236 yyParser *pParser;
drh7218ac72002-03-10 21:21:00 +0000237 pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) );
drh75897232000-05-29 14:26:00 +0000238 if( pParser ){
drh1f245e42002-03-11 13:55:50 +0000239 pParser->yyidx = -1;
drh75897232000-05-29 14:26:00 +0000240 }
241 return pParser;
242}
243
244/* The following function deletes the value associated with a
245** symbol. The symbol can be either a terminal or nonterminal.
246** "yymajor" is the symbol code, and "yypminor" is a pointer to
247** the value.
248*/
249static void yy_destructor(YYCODETYPE yymajor, YYMINORTYPE *yypminor){
250 switch( yymajor ){
251 /* Here is inserted the actions which take place when a
252 ** terminal or non-terminal is destroyed. This can happen
253 ** when the symbol is popped from the stack during a
254 ** reduce or during error processing or when a parser is
255 ** being destroyed before it is finished parsing.
256 **
257 ** Note: during a reduce, the only symbols destroyed are those
258 ** which appear on the RHS of the rule, but which are not used
259 ** inside the C code.
260 */
261%%
262 default: break; /* If no destructor action specified: do nothing */
263 }
264}
265
266/*
267** Pop the parser's stack once.
268**
269** If there is a destructor routine associated with the token which
270** is popped from the stack, then call it.
271**
272** Return the major token number for the symbol popped.
273*/
274static int yy_pop_parser_stack(yyParser *pParser){
275 YYCODETYPE yymajor;
276
drh1f245e42002-03-11 13:55:50 +0000277 if( pParser->yyidx<0 ) return 0;
drh75897232000-05-29 14:26:00 +0000278#ifndef NDEBUG
drh1f245e42002-03-11 13:55:50 +0000279 if( yyTraceFILE && pParser->yyidx>=0 ){
drh75897232000-05-29 14:26:00 +0000280 fprintf(yyTraceFILE,"%sPopping %s\n",
281 yyTracePrompt,
drh1f245e42002-03-11 13:55:50 +0000282 yyTokenName[pParser->yytop->major]);
drh75897232000-05-29 14:26:00 +0000283 }
284#endif
drh1f245e42002-03-11 13:55:50 +0000285 yymajor = pParser->yytop->major;
286 yy_destructor( yymajor, &pParser->yytop->minor);
287 pParser->yyidx--;
288 pParser->yytop--;
drh75897232000-05-29 14:26:00 +0000289 return yymajor;
290}
291
292/*
293** Deallocate and destroy a parser. Destructors are all called for
294** all stack elements before shutting the parser down.
295**
296** Inputs:
297** <ul>
298** <li> A pointer to the parser. This should be a pointer
299** obtained from ParseAlloc.
300** <li> A pointer to a function used to reclaim memory obtained
301** from malloc.
302** </ul>
303*/
drh75897232000-05-29 14:26:00 +0000304void ParseFree(
drh960e8c62001-04-03 16:53:21 +0000305 void *p, /* The parser to be deleted */
306 void (*freeProc)(void*) /* Function used to reclaim memory */
drh75897232000-05-29 14:26:00 +0000307){
308 yyParser *pParser = (yyParser*)p;
309 if( pParser==0 ) return;
drh1f245e42002-03-11 13:55:50 +0000310 while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
drh960e8c62001-04-03 16:53:21 +0000311 (*freeProc)((void*)pParser);
drh75897232000-05-29 14:26:00 +0000312}
313
314/*
315** Find the appropriate action for a parser given the look-ahead token.
316**
317** If the look-ahead token is YYNOCODE, then check to see if the action is
318** independent of the look-ahead. If it is, return the action, otherwise
319** return YY_NO_ACTION.
320*/
321static int yy_find_parser_action(
322 yyParser *pParser, /* The parser */
323 int iLookAhead /* The look-ahead token */
324){
drh1f245e42002-03-11 13:55:50 +0000325 const yyStateEntry *pState; /* Appropriate entry in the state table */
326 const yyActionEntry *pAction; /* Action appropriate for the look-ahead */
drh0bd1f4e2002-06-06 18:54:39 +0000327 int iFallback; /* Fallback token */
drh75897232000-05-29 14:26:00 +0000328
drh1f245e42002-03-11 13:55:50 +0000329 /* if( pParser->yyidx<0 ) return YY_NO_ACTION; */
330 pState = &yyStateTable[pParser->yytop->stateno];
drhb29b0a52002-02-23 19:39:46 +0000331 if( pState->nEntry==0 ){
332 return pState->actionDefault;
333 }else if( iLookAhead!=YYNOCODE ){
334 pAction = &pState->hashtbl[iLookAhead % pState->nEntry];
335 while( 1 ){
drh75897232000-05-29 14:26:00 +0000336 if( pAction->lookahead==iLookAhead ) return pAction->action;
drh0bd1f4e2002-06-06 18:54:39 +0000337 if( pAction->next==0 ) break;
drhb29b0a52002-02-23 19:39:46 +0000338 pAction = &pState->hashtbl[pAction->next-1];
drh75897232000-05-29 14:26:00 +0000339 }
drh0bd1f4e2002-06-06 18:54:39 +0000340#ifdef YYFALLBACK
341 if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
342 && (iFallback = yyFallback[iLookAhead])!=0 ){
343#ifndef NDEBUG
344 if( yyTraceFILE ){
345 fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
346 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
347 }
348#endif
349 return yy_find_parser_action(pParser, iFallback);
350 }
351#endif
drhb29b0a52002-02-23 19:39:46 +0000352 }else if( pState->hashtbl->lookahead!=YYNOCODE ){
drh75897232000-05-29 14:26:00 +0000353 return YY_NO_ACTION;
354 }
355 return pState->actionDefault;
356}
357
358/*
359** Perform a shift action.
360*/
361static void yy_shift(
362 yyParser *yypParser, /* The parser to be shifted */
363 int yyNewState, /* The new state to shift in */
364 int yyMajor, /* The major token to shift in */
365 YYMINORTYPE *yypMinor /* Pointer ot the minor token to shift in */
366){
drh1f245e42002-03-11 13:55:50 +0000367 yypParser->yyidx++;
368 yypParser->yytop++;
369 if( yypParser->yyidx>=YYSTACKDEPTH ){
370 ParseARG_FETCH;
371 yypParser->yyidx--;
372 yypParser->yytop--;
drh75897232000-05-29 14:26:00 +0000373#ifndef NDEBUG
374 if( yyTraceFILE ){
375 fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
376 }
377#endif
drh1f245e42002-03-11 13:55:50 +0000378 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
drh75897232000-05-29 14:26:00 +0000379 /* Here code is inserted which will execute if the parser
380 ** stack every overflows */
381%%
drh1f245e42002-03-11 13:55:50 +0000382 ParseARG_STORE; /* Suppress warning about unused %extra_argument var */
drh75897232000-05-29 14:26:00 +0000383 return;
384 }
drh1f245e42002-03-11 13:55:50 +0000385 yypParser->yytop->stateno = yyNewState;
386 yypParser->yytop->major = yyMajor;
387 yypParser->yytop->minor = *yypMinor;
drh75897232000-05-29 14:26:00 +0000388#ifndef NDEBUG
drh1f245e42002-03-11 13:55:50 +0000389 if( yyTraceFILE && yypParser->yyidx>0 ){
drh75897232000-05-29 14:26:00 +0000390 int i;
391 fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
392 fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
drh1f245e42002-03-11 13:55:50 +0000393 for(i=1; i<=yypParser->yyidx; i++)
394 fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
drh75897232000-05-29 14:26:00 +0000395 fprintf(yyTraceFILE,"\n");
396 }
397#endif
398}
399
400/* The following table contains information about every rule that
401** is used during the reduce.
402*/
403static struct {
404 YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */
405 unsigned char nrhs; /* Number of right-hand side symbols in the rule */
406} yyRuleInfo[] = {
407%%
408};
409
drh1f245e42002-03-11 13:55:50 +0000410static void yy_accept(yyParser*); /* Forward Declaration */
drh75897232000-05-29 14:26:00 +0000411
412/*
413** Perform a reduce action and the shift that must immediately
414** follow the reduce.
415*/
416static void yy_reduce(
417 yyParser *yypParser, /* The parser */
418 int yyruleno /* Number of the rule by which to reduce */
drh75897232000-05-29 14:26:00 +0000419){
420 int yygoto; /* The next state */
421 int yyact; /* The next action */
422 YYMINORTYPE yygotominor; /* The LHS of the rule reduced */
drh1f245e42002-03-11 13:55:50 +0000423 yyStackEntry *yymsp; /* The top of the parser's stack */
drh75897232000-05-29 14:26:00 +0000424 int yysize; /* Amount to pop the stack */
drh1f245e42002-03-11 13:55:50 +0000425 ParseARG_FETCH;
426 yymsp = yypParser->yytop;
drh0bd1f4e2002-06-06 18:54:39 +0000427#ifndef NDEBUG
428 if( yyTraceFILE && yyruleno>=0
429 && yyruleno<sizeof(yyRuleName)/sizeof(yyRuleName[0]) ){
430 fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt,
431 yyRuleName[yyruleno]);
432 }
433#endif /* NDEBUG */
434
drh75897232000-05-29 14:26:00 +0000435 switch( yyruleno ){
436 /* Beginning here are the reduction cases. A typical example
437 ** follows:
438 ** case 0:
drh75897232000-05-29 14:26:00 +0000439 ** #line <lineno> <grammarfile>
440 ** { ... } // User supplied code
441 ** #line <lineno> <thisfile>
442 ** break;
443 */
444%%
445 };
446 yygoto = yyRuleInfo[yyruleno].lhs;
447 yysize = yyRuleInfo[yyruleno].nrhs;
drh1f245e42002-03-11 13:55:50 +0000448 yypParser->yyidx -= yysize;
449 yypParser->yytop -= yysize;
drh75897232000-05-29 14:26:00 +0000450 yyact = yy_find_parser_action(yypParser,yygoto);
451 if( yyact < YYNSTATE ){
452 yy_shift(yypParser,yyact,yygoto,&yygotominor);
453 }else if( yyact == YYNSTATE + YYNRULE + 1 ){
drh1f245e42002-03-11 13:55:50 +0000454 yy_accept(yypParser);
drh75897232000-05-29 14:26:00 +0000455 }
456}
457
458/*
459** The following code executes when the parse fails
460*/
461static void yy_parse_failed(
462 yyParser *yypParser /* The parser */
drh75897232000-05-29 14:26:00 +0000463){
drh1f245e42002-03-11 13:55:50 +0000464 ParseARG_FETCH;
drh75897232000-05-29 14:26:00 +0000465#ifndef NDEBUG
466 if( yyTraceFILE ){
467 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
468 }
469#endif
drh1f245e42002-03-11 13:55:50 +0000470 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
drh75897232000-05-29 14:26:00 +0000471 /* Here code is inserted which will be executed whenever the
472 ** parser fails */
473%%
drh1f245e42002-03-11 13:55:50 +0000474 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
drh75897232000-05-29 14:26:00 +0000475}
476
477/*
478** The following code executes when a syntax error first occurs.
479*/
480static void yy_syntax_error(
481 yyParser *yypParser, /* The parser */
482 int yymajor, /* The major type of the error token */
483 YYMINORTYPE yyminor /* The minor type of the error token */
drh75897232000-05-29 14:26:00 +0000484){
drh1f245e42002-03-11 13:55:50 +0000485 ParseARG_FETCH;
drh75897232000-05-29 14:26:00 +0000486#define TOKEN (yyminor.yy0)
487%%
drh1f245e42002-03-11 13:55:50 +0000488 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
drh75897232000-05-29 14:26:00 +0000489}
490
491/*
492** The following is executed when the parser accepts
493*/
494static void yy_accept(
495 yyParser *yypParser /* The parser */
drh75897232000-05-29 14:26:00 +0000496){
drh1f245e42002-03-11 13:55:50 +0000497 ParseARG_FETCH;
drh75897232000-05-29 14:26:00 +0000498#ifndef NDEBUG
499 if( yyTraceFILE ){
500 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
501 }
502#endif
drh1f245e42002-03-11 13:55:50 +0000503 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
drh75897232000-05-29 14:26:00 +0000504 /* Here code is inserted which will be executed whenever the
505 ** parser accepts */
506%%
drh1f245e42002-03-11 13:55:50 +0000507 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
drh75897232000-05-29 14:26:00 +0000508}
509
510/* The main parser program.
511** The first argument is a pointer to a structure obtained from
512** "ParseAlloc" which describes the current state of the parser.
513** The second argument is the major token number. The third is
514** the minor token. The fourth optional argument is whatever the
515** user wants (and specified in the grammar) and is available for
516** use by the action routines.
517**
518** Inputs:
519** <ul>
520** <li> A pointer to the parser (an opaque structure.)
521** <li> The major token number.
522** <li> The minor token number.
523** <li> An option argument of a grammar-specified type.
524** </ul>
525**
526** Outputs:
527** None.
528*/
drh75897232000-05-29 14:26:00 +0000529void Parse(
530 void *yyp, /* The parser */
531 int yymajor, /* The major token code number */
532 ParseTOKENTYPE yyminor /* The value for the token */
drh1f245e42002-03-11 13:55:50 +0000533 ParseARG_PDECL /* Optional %extra_argument parameter */
drh75897232000-05-29 14:26:00 +0000534){
535 YYMINORTYPE yyminorunion;
536 int yyact; /* The parser action. */
537 int yyendofinput; /* True if we are at the end of input */
538 int yyerrorhit = 0; /* True if yymajor has invoked an error */
539 yyParser *yypParser; /* The parser */
540
541 /* (re)initialize the parser, if necessary */
542 yypParser = (yyParser*)yyp;
drh1f245e42002-03-11 13:55:50 +0000543 if( yypParser->yyidx<0 ){
drh75897232000-05-29 14:26:00 +0000544 if( yymajor==0 ) return;
drh1f245e42002-03-11 13:55:50 +0000545 yypParser->yyidx = 0;
546 yypParser->yyerrcnt = -1;
547 yypParser->yytop = &yypParser->yystack[0];
548 yypParser->yytop->stateno = 0;
549 yypParser->yytop->major = 0;
drh75897232000-05-29 14:26:00 +0000550 }
551 yyminorunion.yy0 = yyminor;
552 yyendofinput = (yymajor==0);
drh1f245e42002-03-11 13:55:50 +0000553 ParseARG_STORE;
drh75897232000-05-29 14:26:00 +0000554
555#ifndef NDEBUG
556 if( yyTraceFILE ){
557 fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
558 }
559#endif
560
561 do{
562 yyact = yy_find_parser_action(yypParser,yymajor);
563 if( yyact<YYNSTATE ){
564 yy_shift(yypParser,yyact,yymajor,&yyminorunion);
drh1f245e42002-03-11 13:55:50 +0000565 yypParser->yyerrcnt--;
566 if( yyendofinput && yypParser->yyidx>=0 ){
drh75897232000-05-29 14:26:00 +0000567 yymajor = 0;
568 }else{
569 yymajor = YYNOCODE;
570 }
571 }else if( yyact < YYNSTATE + YYNRULE ){
drh1f245e42002-03-11 13:55:50 +0000572 yy_reduce(yypParser,yyact-YYNSTATE);
drh75897232000-05-29 14:26:00 +0000573 }else if( yyact == YY_ERROR_ACTION ){
574#ifndef NDEBUG
575 if( yyTraceFILE ){
576 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
577 }
578#endif
579#ifdef YYERRORSYMBOL
580 /* A syntax error has occurred.
581 ** The response to an error depends upon whether or not the
582 ** grammar defines an error token "ERROR".
583 **
584 ** This is what we do if the grammar does define ERROR:
585 **
586 ** * Call the %syntax_error function.
587 **
588 ** * Begin popping the stack until we enter a state where
589 ** it is legal to shift the error symbol, then shift
590 ** the error symbol.
591 **
592 ** * Set the error count to three.
593 **
594 ** * Begin accepting and shifting new tokens. No new error
595 ** processing will occur until three tokens have been
596 ** shifted successfully.
597 **
598 */
drh1f245e42002-03-11 13:55:50 +0000599 if( yypParser->yyerrcnt<0 ){
600 yy_syntax_error(yypParser,yymajor,yyminorunion);
drh75897232000-05-29 14:26:00 +0000601 }
drh1f245e42002-03-11 13:55:50 +0000602 if( yypParser->yytop->major==YYERRORSYMBOL || yyerrorhit ){
drh75897232000-05-29 14:26:00 +0000603#ifndef NDEBUG
604 if( yyTraceFILE ){
605 fprintf(yyTraceFILE,"%sDiscard input token %s\n",
606 yyTracePrompt,yyTokenName[yymajor]);
607 }
608#endif
609 yy_destructor(yymajor,&yyminorunion);
610 yymajor = YYNOCODE;
611 }else{
612 while(
drh1f245e42002-03-11 13:55:50 +0000613 yypParser->yyidx >= 0 &&
614 yypParser->yytop->major != YYERRORSYMBOL &&
drh75897232000-05-29 14:26:00 +0000615 (yyact = yy_find_parser_action(yypParser,YYERRORSYMBOL)) >= YYNSTATE
616 ){
617 yy_pop_parser_stack(yypParser);
618 }
drh1f245e42002-03-11 13:55:50 +0000619 if( yypParser->yyidx < 0 || yymajor==0 ){
drh75897232000-05-29 14:26:00 +0000620 yy_destructor(yymajor,&yyminorunion);
drh1f245e42002-03-11 13:55:50 +0000621 yy_parse_failed(yypParser);
drh75897232000-05-29 14:26:00 +0000622 yymajor = YYNOCODE;
drh1f245e42002-03-11 13:55:50 +0000623 }else if( yypParser->yytop->major!=YYERRORSYMBOL ){
drh75897232000-05-29 14:26:00 +0000624 YYMINORTYPE u2;
625 u2.YYERRSYMDT = 0;
626 yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
627 }
628 }
drh1f245e42002-03-11 13:55:50 +0000629 yypParser->yyerrcnt = 3;
drh75897232000-05-29 14:26:00 +0000630 yyerrorhit = 1;
631#else /* YYERRORSYMBOL is not defined */
632 /* This is what we do if the grammar does not define ERROR:
633 **
634 ** * Report an error message, and throw away the input token.
635 **
636 ** * If the input token is $, then fail the parse.
637 **
638 ** As before, subsequent error messages are suppressed until
639 ** three input tokens have been successfully shifted.
640 */
drh1f245e42002-03-11 13:55:50 +0000641 if( yypParser->yyerrcnt<=0 ){
642 yy_syntax_error(yypParser,yymajor,yyminorunion);
drh75897232000-05-29 14:26:00 +0000643 }
drh1f245e42002-03-11 13:55:50 +0000644 yypParser->yyerrcnt = 3;
drh75897232000-05-29 14:26:00 +0000645 yy_destructor(yymajor,&yyminorunion);
646 if( yyendofinput ){
drh1f245e42002-03-11 13:55:50 +0000647 yy_parse_failed(yypParser);
drh75897232000-05-29 14:26:00 +0000648 }
649 yymajor = YYNOCODE;
650#endif
651 }else{
drh1f245e42002-03-11 13:55:50 +0000652 yy_accept(yypParser);
drh75897232000-05-29 14:26:00 +0000653 yymajor = YYNOCODE;
654 }
drh1f245e42002-03-11 13:55:50 +0000655 }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
drh75897232000-05-29 14:26:00 +0000656 return;
657}