blob: a0b066f9d84c15b2a002d48e55fff629fdad5afa [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.
34** YYACTIONTYPE is the data type used for storing terminal
35** and nonterminal numbers. "unsigned char" is
36** used if there are fewer than 250 rules and
37** states combined. "int" is used otherwise.
38** ParseTOKENTYPE is the data type used for minor tokens given
39** directly to the parser from the tokenizer.
40** YYMINORTYPE is the data type used for all minor tokens.
41** This is typically a union of many types, one of
42** which is ParseTOKENTYPE. The entry in the union
43** for base tokens is called "yy0".
44** YYSTACKDEPTH is the maximum depth of the parser's stack.
45** ParseARGDECL is a declaration of a 3rd argument to the
46** parser, or null if there is no extra argument.
47** ParseKRARGDECL A version of ParseARGDECL for K&R C.
48** ParseANSIARGDECL A version of ParseARGDECL for ANSI C.
49** YYNSTATE the combined number of states.
50** YYNRULE the number of rules in the grammar
51** YYERRORSYMBOL is the code number of the error symbol. If not
52** defined, then do no error processing.
53*/
54%%
55#define YY_NO_ACTION (YYNSTATE+YYNRULE+2)
56#define YY_ACCEPT_ACTION (YYNSTATE+YYNRULE+1)
57#define YY_ERROR_ACTION (YYNSTATE+YYNRULE)
58/* Next is the action table. Each entry in this table contains
59**
60** + An integer which is the number representing the look-ahead
61** token
62**
63** + An integer indicating what action to take. Number (N) between
64** 0 and YYNSTATE-1 mean shift the look-ahead and go to state N.
65** Numbers between YYNSTATE and YYNSTATE+YYNRULE-1 mean reduce by
66** rule N-YYNSTATE. Number YYNSTATE+YYNRULE means that a syntax
67** error has occurred. Number YYNSTATE+YYNRULE+1 means the parser
68** accepts its input.
69**
70** + A pointer to the next entry with the same hash value.
71**
72** The action table is really a series of hash tables. Each hash
73** table contains a number of entries which is a power of two. The
74** "state" table (which follows) contains information about the starting
75** point and size of each hash table.
76*/
77struct yyActionEntry {
78 YYCODETYPE lookahead; /* The value of the look-ahead token */
79 YYACTIONTYPE action; /* Action to take for this look-ahead */
80 struct yyActionEntry *next; /* Next look-ahead with the same hash, or NULL */
81};
82static struct yyActionEntry yyActionTable[] = {
83%%
84};
85
86/* The state table contains information needed to look up the correct
87** action in the action table, given the current state of the parser.
88** Information needed includes:
89**
90** + A pointer to the start of the action hash table in yyActionTable.
91**
92** + A mask used to hash the look-ahead token. The mask is an integer
93** which is one less than the size of the hash table.
94**
95** + The default action. This is the action to take if no entry for
96** the given look-ahead is found in the action hash table.
97*/
98struct yyStateEntry {
99 struct yyActionEntry *hashtbl; /* Start of the hash table in yyActionTable */
100 int mask; /* Mask used for hashing the look-ahead */
101 YYACTIONTYPE actionDefault; /* Default action if look-ahead not found */
102};
103static struct yyStateEntry yyStateTable[] = {
104%%
105};
106
107/* The following structure represents a single element of the
108** parser's stack. Information stored includes:
109**
110** + The state number for the parser at this level of the stack.
111**
112** + The value of the token stored at this level of the stack.
113** (In other words, the "major" token.)
114**
115** + The semantic value stored at this level of the stack. This is
116** the information used by the action routines in the grammar.
117** It is sometimes called the "minor" token.
118*/
119struct yyStackEntry {
120 int stateno; /* The state-number */
121 int major; /* The major token value. This is the code
122 ** number for the token at this stack level */
123 YYMINORTYPE minor; /* The user-supplied minor token value. This
124 ** is the value of the token */
125};
126
127/* The state of the parser is completely contained in an instance of
128** the following structure */
129struct yyParser {
130 int idx; /* Index of top element in stack */
131 int errcnt; /* Shifts left before out of the error */
132 struct yyStackEntry *top; /* Pointer to the top stack element */
133 struct yyStackEntry stack[YYSTACKDEPTH]; /* The parser's stack */
134};
135typedef struct yyParser yyParser;
136
137#ifndef NDEBUG
138#include <stdio.h>
139static FILE *yyTraceFILE = 0;
140static char *yyTracePrompt = 0;
141
142/*
143** Turn parser tracing on by giving a stream to which to write the trace
144** and a prompt to preface each trace message. Tracing is turned off
145** by making either argument NULL
146**
147** Inputs:
148** <ul>
149** <li> A FILE* to which trace output should be written.
150** If NULL, then tracing is turned off.
151** <li> A prefix string written at the beginning of every
152** line of trace output. If NULL, then tracing is
153** turned off.
154** </ul>
155**
156** Outputs:
157** None.
158*/
drh75897232000-05-29 14:26:00 +0000159void ParseTrace(FILE *TraceFILE, char *zTracePrompt){
160 yyTraceFILE = TraceFILE;
161 yyTracePrompt = zTracePrompt;
162 if( yyTraceFILE==0 ) yyTracePrompt = 0;
163 else if( yyTracePrompt==0 ) yyTraceFILE = 0;
164}
165
166/* For tracing shifts, the names of all terminals and nonterminals
167** are required. The following table supplies these names */
168static char *yyTokenName[] = {
169%%
170};
171#define YYTRACE(X) if( yyTraceFILE ) fprintf(yyTraceFILE,"%sReduce [%s].\n",yyTracePrompt,X);
172#else
173#define YYTRACE(X)
174#endif
175
drha1b351a2001-09-14 16:42:12 +0000176
drh960e8c62001-04-03 16:53:21 +0000177/*
178** This function returns the symbolic name associated with a token
179** value.
180*/
181const char *ParseTokenName(int tokenType){
drha1b351a2001-09-14 16:42:12 +0000182#ifndef NDEBUG
drh960e8c62001-04-03 16:53:21 +0000183 if( tokenType>0 && tokenType<(sizeof(yyTokenName)/sizeof(yyTokenName[0])) ){
184 return yyTokenName[tokenType];
185 }else{
186 return "Unknown";
187 }
drha1b351a2001-09-14 16:42:12 +0000188#else
189 return "";
190#endif
drh960e8c62001-04-03 16:53:21 +0000191}
192
drh75897232000-05-29 14:26:00 +0000193/*
194** This function allocates a new parser.
195** The only argument is a pointer to a function which works like
196** malloc.
197**
198** Inputs:
199** A pointer to the function used to allocate memory.
200**
201** Outputs:
202** A pointer to a parser. This pointer is used in subsequent calls
203** to Parse and ParseFree.
204*/
drh960e8c62001-04-03 16:53:21 +0000205void *ParseAlloc(void *(*mallocProc)(int)){
drh75897232000-05-29 14:26:00 +0000206 yyParser *pParser;
drh960e8c62001-04-03 16:53:21 +0000207 pParser = (yyParser*)(*mallocProc)( (int)sizeof(yyParser) );
drh75897232000-05-29 14:26:00 +0000208 if( pParser ){
209 pParser->idx = -1;
210 }
211 return pParser;
212}
213
214/* The following function deletes the value associated with a
215** symbol. The symbol can be either a terminal or nonterminal.
216** "yymajor" is the symbol code, and "yypminor" is a pointer to
217** the value.
218*/
219static void yy_destructor(YYCODETYPE yymajor, YYMINORTYPE *yypminor){
220 switch( yymajor ){
221 /* Here is inserted the actions which take place when a
222 ** terminal or non-terminal is destroyed. This can happen
223 ** when the symbol is popped from the stack during a
224 ** reduce or during error processing or when a parser is
225 ** being destroyed before it is finished parsing.
226 **
227 ** Note: during a reduce, the only symbols destroyed are those
228 ** which appear on the RHS of the rule, but which are not used
229 ** inside the C code.
230 */
231%%
232 default: break; /* If no destructor action specified: do nothing */
233 }
234}
235
236/*
237** Pop the parser's stack once.
238**
239** If there is a destructor routine associated with the token which
240** is popped from the stack, then call it.
241**
242** Return the major token number for the symbol popped.
243*/
244static int yy_pop_parser_stack(yyParser *pParser){
245 YYCODETYPE yymajor;
246
247 if( pParser->idx<0 ) return 0;
248#ifndef NDEBUG
249 if( yyTraceFILE && pParser->idx>=0 ){
250 fprintf(yyTraceFILE,"%sPopping %s\n",
251 yyTracePrompt,
252 yyTokenName[pParser->top->major]);
253 }
254#endif
255 yymajor = pParser->top->major;
256 yy_destructor( yymajor, &pParser->top->minor);
257 pParser->idx--;
258 pParser->top--;
259 return yymajor;
260}
261
262/*
263** Deallocate and destroy a parser. Destructors are all called for
264** all stack elements before shutting the parser down.
265**
266** Inputs:
267** <ul>
268** <li> A pointer to the parser. This should be a pointer
269** obtained from ParseAlloc.
270** <li> A pointer to a function used to reclaim memory obtained
271** from malloc.
272** </ul>
273*/
drh75897232000-05-29 14:26:00 +0000274void ParseFree(
drh960e8c62001-04-03 16:53:21 +0000275 void *p, /* The parser to be deleted */
276 void (*freeProc)(void*) /* Function used to reclaim memory */
drh75897232000-05-29 14:26:00 +0000277){
278 yyParser *pParser = (yyParser*)p;
279 if( pParser==0 ) return;
280 while( pParser->idx>=0 ) yy_pop_parser_stack(pParser);
drh960e8c62001-04-03 16:53:21 +0000281 (*freeProc)((void*)pParser);
drh75897232000-05-29 14:26:00 +0000282}
283
284/*
285** Find the appropriate action for a parser given the look-ahead token.
286**
287** If the look-ahead token is YYNOCODE, then check to see if the action is
288** independent of the look-ahead. If it is, return the action, otherwise
289** return YY_NO_ACTION.
290*/
291static int yy_find_parser_action(
292 yyParser *pParser, /* The parser */
293 int iLookAhead /* The look-ahead token */
294){
295 struct yyStateEntry *pState; /* Appropriate entry in the state table */
296 struct yyActionEntry *pAction; /* Action appropriate for the look-ahead */
297
298 /* if( pParser->idx<0 ) return YY_NO_ACTION; */
299 pState = &yyStateTable[pParser->top->stateno];
300 if( iLookAhead!=YYNOCODE ){
301 pAction = &pState->hashtbl[iLookAhead & pState->mask];
302 while( pAction ){
303 if( pAction->lookahead==iLookAhead ) return pAction->action;
304 pAction = pAction->next;
305 }
306 }else if( pState->mask!=0 || pState->hashtbl->lookahead!=YYNOCODE ){
307 return YY_NO_ACTION;
308 }
309 return pState->actionDefault;
310}
311
312/*
313** Perform a shift action.
314*/
315static void yy_shift(
316 yyParser *yypParser, /* The parser to be shifted */
317 int yyNewState, /* The new state to shift in */
318 int yyMajor, /* The major token to shift in */
319 YYMINORTYPE *yypMinor /* Pointer ot the minor token to shift in */
320){
321 yypParser->idx++;
322 yypParser->top++;
323 if( yypParser->idx>=YYSTACKDEPTH ){
324 yypParser->idx--;
325 yypParser->top--;
326#ifndef NDEBUG
327 if( yyTraceFILE ){
328 fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
329 }
330#endif
331 while( yypParser->idx>=0 ) yy_pop_parser_stack(yypParser);
332 /* Here code is inserted which will execute if the parser
333 ** stack every overflows */
334%%
335 return;
336 }
337 yypParser->top->stateno = yyNewState;
338 yypParser->top->major = yyMajor;
339 yypParser->top->minor = *yypMinor;
340#ifndef NDEBUG
341 if( yyTraceFILE && yypParser->idx>0 ){
342 int i;
343 fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
344 fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
345 for(i=1; i<=yypParser->idx; i++)
346 fprintf(yyTraceFILE," %s",yyTokenName[yypParser->stack[i].major]);
347 fprintf(yyTraceFILE,"\n");
348 }
349#endif
350}
351
352/* The following table contains information about every rule that
353** is used during the reduce.
354*/
355static struct {
356 YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */
357 unsigned char nrhs; /* Number of right-hand side symbols in the rule */
358} yyRuleInfo[] = {
359%%
360};
361
drh960e8c62001-04-03 16:53:21 +0000362static void yy_accept(yyParser * ParseANSIARGDECL); /* Forward Declaration */
drh75897232000-05-29 14:26:00 +0000363
364/*
365** Perform a reduce action and the shift that must immediately
366** follow the reduce.
367*/
368static void yy_reduce(
369 yyParser *yypParser, /* The parser */
370 int yyruleno /* Number of the rule by which to reduce */
371 ParseANSIARGDECL
372){
373 int yygoto; /* The next state */
374 int yyact; /* The next action */
375 YYMINORTYPE yygotominor; /* The LHS of the rule reduced */
376 struct yyStackEntry *yymsp; /* The top of the parser's stack */
377 int yysize; /* Amount to pop the stack */
378 yymsp = yypParser->top;
379 switch( yyruleno ){
380 /* Beginning here are the reduction cases. A typical example
381 ** follows:
382 ** case 0:
383 ** YYTRACE("<text of the rule>");
384 ** #line <lineno> <grammarfile>
385 ** { ... } // User supplied code
386 ** #line <lineno> <thisfile>
387 ** break;
388 */
389%%
390 };
391 yygoto = yyRuleInfo[yyruleno].lhs;
392 yysize = yyRuleInfo[yyruleno].nrhs;
393 yypParser->idx -= yysize;
394 yypParser->top -= yysize;
395 yyact = yy_find_parser_action(yypParser,yygoto);
396 if( yyact < YYNSTATE ){
397 yy_shift(yypParser,yyact,yygoto,&yygotominor);
398 }else if( yyact == YYNSTATE + YYNRULE + 1 ){
399 yy_accept(yypParser ParseARGDECL);
400 }
401}
402
403/*
404** The following code executes when the parse fails
405*/
406static void yy_parse_failed(
407 yyParser *yypParser /* The parser */
408 ParseANSIARGDECL /* Extra arguments (if any) */
409){
410#ifndef NDEBUG
411 if( yyTraceFILE ){
412 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
413 }
414#endif
415 while( yypParser->idx>=0 ) yy_pop_parser_stack(yypParser);
416 /* Here code is inserted which will be executed whenever the
417 ** parser fails */
418%%
419}
420
421/*
422** The following code executes when a syntax error first occurs.
423*/
424static void yy_syntax_error(
425 yyParser *yypParser, /* The parser */
426 int yymajor, /* The major type of the error token */
427 YYMINORTYPE yyminor /* The minor type of the error token */
428 ParseANSIARGDECL /* Extra arguments (if any) */
429){
430#define TOKEN (yyminor.yy0)
431%%
432}
433
434/*
435** The following is executed when the parser accepts
436*/
437static void yy_accept(
438 yyParser *yypParser /* The parser */
439 ParseANSIARGDECL /* Extra arguments (if any) */
440){
441#ifndef NDEBUG
442 if( yyTraceFILE ){
443 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
444 }
445#endif
446 while( yypParser->idx>=0 ) yy_pop_parser_stack(yypParser);
447 /* Here code is inserted which will be executed whenever the
448 ** parser accepts */
449%%
450}
451
452/* The main parser program.
453** The first argument is a pointer to a structure obtained from
454** "ParseAlloc" which describes the current state of the parser.
455** The second argument is the major token number. The third is
456** the minor token. The fourth optional argument is whatever the
457** user wants (and specified in the grammar) and is available for
458** use by the action routines.
459**
460** Inputs:
461** <ul>
462** <li> A pointer to the parser (an opaque structure.)
463** <li> The major token number.
464** <li> The minor token number.
465** <li> An option argument of a grammar-specified type.
466** </ul>
467**
468** Outputs:
469** None.
470*/
drh75897232000-05-29 14:26:00 +0000471void Parse(
472 void *yyp, /* The parser */
473 int yymajor, /* The major token code number */
474 ParseTOKENTYPE yyminor /* The value for the token */
475 ParseANSIARGDECL
476){
477 YYMINORTYPE yyminorunion;
478 int yyact; /* The parser action. */
479 int yyendofinput; /* True if we are at the end of input */
480 int yyerrorhit = 0; /* True if yymajor has invoked an error */
481 yyParser *yypParser; /* The parser */
482
483 /* (re)initialize the parser, if necessary */
484 yypParser = (yyParser*)yyp;
485 if( yypParser->idx<0 ){
486 if( yymajor==0 ) return;
487 yypParser->idx = 0;
488 yypParser->errcnt = -1;
489 yypParser->top = &yypParser->stack[0];
490 yypParser->top->stateno = 0;
491 yypParser->top->major = 0;
492 }
493 yyminorunion.yy0 = yyminor;
494 yyendofinput = (yymajor==0);
495
496#ifndef NDEBUG
497 if( yyTraceFILE ){
498 fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
499 }
500#endif
501
502 do{
503 yyact = yy_find_parser_action(yypParser,yymajor);
504 if( yyact<YYNSTATE ){
505 yy_shift(yypParser,yyact,yymajor,&yyminorunion);
506 yypParser->errcnt--;
507 if( yyendofinput && yypParser->idx>=0 ){
508 yymajor = 0;
509 }else{
510 yymajor = YYNOCODE;
511 }
512 }else if( yyact < YYNSTATE + YYNRULE ){
513 yy_reduce(yypParser,yyact-YYNSTATE ParseARGDECL);
514 }else if( yyact == YY_ERROR_ACTION ){
515#ifndef NDEBUG
516 if( yyTraceFILE ){
517 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
518 }
519#endif
520#ifdef YYERRORSYMBOL
521 /* A syntax error has occurred.
522 ** The response to an error depends upon whether or not the
523 ** grammar defines an error token "ERROR".
524 **
525 ** This is what we do if the grammar does define ERROR:
526 **
527 ** * Call the %syntax_error function.
528 **
529 ** * Begin popping the stack until we enter a state where
530 ** it is legal to shift the error symbol, then shift
531 ** the error symbol.
532 **
533 ** * Set the error count to three.
534 **
535 ** * Begin accepting and shifting new tokens. No new error
536 ** processing will occur until three tokens have been
537 ** shifted successfully.
538 **
539 */
540 if( yypParser->errcnt<0 ){
541 yy_syntax_error(yypParser,yymajor,yyminorunion ParseARGDECL);
542 }
543 if( yypParser->top->major==YYERRORSYMBOL || yyerrorhit ){
544#ifndef NDEBUG
545 if( yyTraceFILE ){
546 fprintf(yyTraceFILE,"%sDiscard input token %s\n",
547 yyTracePrompt,yyTokenName[yymajor]);
548 }
549#endif
550 yy_destructor(yymajor,&yyminorunion);
551 yymajor = YYNOCODE;
552 }else{
553 while(
554 yypParser->idx >= 0 &&
555 yypParser->top->major != YYERRORSYMBOL &&
556 (yyact = yy_find_parser_action(yypParser,YYERRORSYMBOL)) >= YYNSTATE
557 ){
558 yy_pop_parser_stack(yypParser);
559 }
560 if( yypParser->idx < 0 || yymajor==0 ){
561 yy_destructor(yymajor,&yyminorunion);
562 yy_parse_failed(yypParser ParseARGDECL);
563 yymajor = YYNOCODE;
564 }else if( yypParser->top->major!=YYERRORSYMBOL ){
565 YYMINORTYPE u2;
566 u2.YYERRSYMDT = 0;
567 yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
568 }
569 }
570 yypParser->errcnt = 3;
571 yyerrorhit = 1;
572#else /* YYERRORSYMBOL is not defined */
573 /* This is what we do if the grammar does not define ERROR:
574 **
575 ** * Report an error message, and throw away the input token.
576 **
577 ** * If the input token is $, then fail the parse.
578 **
579 ** As before, subsequent error messages are suppressed until
580 ** three input tokens have been successfully shifted.
581 */
582 if( yypParser->errcnt<=0 ){
583 yy_syntax_error(yypParser,yymajor,yyminorunion ParseARGDECL);
584 }
585 yypParser->errcnt = 3;
586 yy_destructor(yymajor,&yyminorunion);
587 if( yyendofinput ){
588 yy_parse_failed(yypParser ParseARGDECL);
589 }
590 yymajor = YYNOCODE;
591#endif
592 }else{
593 yy_accept(yypParser ParseARGDECL);
594 yymajor = YYNOCODE;
595 }
596 }while( yymajor!=YYNOCODE && yypParser->idx>=0 );
597 return;
598}