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