drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* Driver template for the LEMON parser generator. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** The author disclaims copyright to this source code. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | */ |
| 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. |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame^] | 34 | ** 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. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 37 | ** 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. |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 48 | ** 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 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 52 | ** 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 | */ |
| 80 | struct yyActionEntry { |
| 81 | YYCODETYPE lookahead; /* The value of the look-ahead token */ |
drh | b29b0a5 | 2002-02-23 19:39:46 +0000 | [diff] [blame] | 82 | YYCODETYPE next; /* Next entry + 1. Zero at end of collision chain */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 83 | YYACTIONTYPE action; /* Action to take for this look-ahead */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 84 | }; |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 85 | typedef struct yyActionEntry yyActionEntry; |
| 86 | static const yyActionEntry yyActionTable[] = { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 87 | %% |
| 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 | ** |
drh | b29b0a5 | 2002-02-23 19:39:46 +0000 | [diff] [blame] | 96 | ** + The number of entries in the action hash table. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 97 | ** |
| 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 | */ |
| 101 | struct yyStateEntry { |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 102 | const yyActionEntry *hashtbl; /* Start of the hash table in yyActionTable */ |
drh | b29b0a5 | 2002-02-23 19:39:46 +0000 | [diff] [blame] | 103 | YYCODETYPE nEntry; /* Number of entries in action hash table */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 104 | YYACTIONTYPE actionDefault; /* Default action if look-ahead not found */ |
| 105 | }; |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 106 | typedef struct yyStateEntry yyStateEntry; |
| 107 | static const yyStateEntry yyStateTable[] = { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 108 | %% |
| 109 | }; |
| 110 | |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame^] | 111 | /* 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 |
| 122 | static const YYCODETYPE yyFallback[] = { |
| 123 | %% |
| 124 | }; |
| 125 | #endif /* YYFALLBACK */ |
| 126 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 127 | /* 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 | */ |
| 139 | struct 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 | }; |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 146 | typedef struct yyStackEntry yyStackEntry; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 147 | |
| 148 | /* The state of the parser is completely contained in an instance of |
| 149 | ** the following structure */ |
| 150 | struct yyParser { |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 151 | 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 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 156 | }; |
| 157 | typedef struct yyParser yyParser; |
| 158 | |
| 159 | #ifndef NDEBUG |
| 160 | #include <stdio.h> |
| 161 | static FILE *yyTraceFILE = 0; |
| 162 | static char *yyTracePrompt = 0; |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame^] | 163 | #endif /* NDEBUG */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 164 | |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame^] | 165 | #ifndef NDEBUG |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 166 | /* |
| 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 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 183 | void 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 | } |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame^] | 189 | #endif /* NDEBUG */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 190 | |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame^] | 191 | #ifndef NDEBUG |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 192 | /* For tracing shifts, the names of all terminals and nonterminals |
| 193 | ** are required. The following table supplies these names */ |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 194 | static const char *yyTokenName[] = { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 195 | %% |
| 196 | }; |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame^] | 197 | #endif /* NDEBUG */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 198 | |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame^] | 199 | #ifndef NDEBUG |
| 200 | /* For tracing reduce actions, the names of all rules are required. |
| 201 | */ |
| 202 | static const char *yyRuleName[] = { |
| 203 | %% |
| 204 | }; |
| 205 | #endif /* NDEBUG */ |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 206 | |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 207 | /* |
| 208 | ** This function returns the symbolic name associated with a token |
| 209 | ** value. |
| 210 | */ |
| 211 | const char *ParseTokenName(int tokenType){ |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 212 | #ifndef NDEBUG |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 213 | if( tokenType>0 && tokenType<(sizeof(yyTokenName)/sizeof(yyTokenName[0])) ){ |
| 214 | return yyTokenName[tokenType]; |
| 215 | }else{ |
| 216 | return "Unknown"; |
| 217 | } |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 218 | #else |
| 219 | return ""; |
| 220 | #endif |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 221 | } |
| 222 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 223 | /* |
| 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 | */ |
drh | 7218ac7 | 2002-03-10 21:21:00 +0000 | [diff] [blame] | 235 | void *ParseAlloc(void *(*mallocProc)(size_t)){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 236 | yyParser *pParser; |
drh | 7218ac7 | 2002-03-10 21:21:00 +0000 | [diff] [blame] | 237 | pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 238 | if( pParser ){ |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 239 | pParser->yyidx = -1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 240 | } |
| 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 | */ |
| 249 | static 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 | */ |
| 274 | static int yy_pop_parser_stack(yyParser *pParser){ |
| 275 | YYCODETYPE yymajor; |
| 276 | |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 277 | if( pParser->yyidx<0 ) return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 278 | #ifndef NDEBUG |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 279 | if( yyTraceFILE && pParser->yyidx>=0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 280 | fprintf(yyTraceFILE,"%sPopping %s\n", |
| 281 | yyTracePrompt, |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 282 | yyTokenName[pParser->yytop->major]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 283 | } |
| 284 | #endif |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 285 | yymajor = pParser->yytop->major; |
| 286 | yy_destructor( yymajor, &pParser->yytop->minor); |
| 287 | pParser->yyidx--; |
| 288 | pParser->yytop--; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 289 | 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 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 304 | void ParseFree( |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 305 | void *p, /* The parser to be deleted */ |
| 306 | void (*freeProc)(void*) /* Function used to reclaim memory */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 307 | ){ |
| 308 | yyParser *pParser = (yyParser*)p; |
| 309 | if( pParser==0 ) return; |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 310 | while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser); |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 311 | (*freeProc)((void*)pParser); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 312 | } |
| 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 | */ |
| 321 | static int yy_find_parser_action( |
| 322 | yyParser *pParser, /* The parser */ |
| 323 | int iLookAhead /* The look-ahead token */ |
| 324 | ){ |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 325 | const yyStateEntry *pState; /* Appropriate entry in the state table */ |
| 326 | const yyActionEntry *pAction; /* Action appropriate for the look-ahead */ |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame^] | 327 | int iFallback; /* Fallback token */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 328 | |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 329 | /* if( pParser->yyidx<0 ) return YY_NO_ACTION; */ |
| 330 | pState = &yyStateTable[pParser->yytop->stateno]; |
drh | b29b0a5 | 2002-02-23 19:39:46 +0000 | [diff] [blame] | 331 | if( pState->nEntry==0 ){ |
| 332 | return pState->actionDefault; |
| 333 | }else if( iLookAhead!=YYNOCODE ){ |
| 334 | pAction = &pState->hashtbl[iLookAhead % pState->nEntry]; |
| 335 | while( 1 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 336 | if( pAction->lookahead==iLookAhead ) return pAction->action; |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame^] | 337 | if( pAction->next==0 ) break; |
drh | b29b0a5 | 2002-02-23 19:39:46 +0000 | [diff] [blame] | 338 | pAction = &pState->hashtbl[pAction->next-1]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 339 | } |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame^] | 340 | #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 |
drh | b29b0a5 | 2002-02-23 19:39:46 +0000 | [diff] [blame] | 352 | }else if( pState->hashtbl->lookahead!=YYNOCODE ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 353 | return YY_NO_ACTION; |
| 354 | } |
| 355 | return pState->actionDefault; |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | ** Perform a shift action. |
| 360 | */ |
| 361 | static 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 | ){ |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 367 | yypParser->yyidx++; |
| 368 | yypParser->yytop++; |
| 369 | if( yypParser->yyidx>=YYSTACKDEPTH ){ |
| 370 | ParseARG_FETCH; |
| 371 | yypParser->yyidx--; |
| 372 | yypParser->yytop--; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 373 | #ifndef NDEBUG |
| 374 | if( yyTraceFILE ){ |
| 375 | fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt); |
| 376 | } |
| 377 | #endif |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 378 | while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 379 | /* Here code is inserted which will execute if the parser |
| 380 | ** stack every overflows */ |
| 381 | %% |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 382 | ParseARG_STORE; /* Suppress warning about unused %extra_argument var */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 383 | return; |
| 384 | } |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 385 | yypParser->yytop->stateno = yyNewState; |
| 386 | yypParser->yytop->major = yyMajor; |
| 387 | yypParser->yytop->minor = *yypMinor; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 388 | #ifndef NDEBUG |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 389 | if( yyTraceFILE && yypParser->yyidx>0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 390 | int i; |
| 391 | fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState); |
| 392 | fprintf(yyTraceFILE,"%sStack:",yyTracePrompt); |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 393 | for(i=1; i<=yypParser->yyidx; i++) |
| 394 | fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 395 | 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 | */ |
| 403 | static 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 | |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 410 | static void yy_accept(yyParser*); /* Forward Declaration */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 411 | |
| 412 | /* |
| 413 | ** Perform a reduce action and the shift that must immediately |
| 414 | ** follow the reduce. |
| 415 | */ |
| 416 | static void yy_reduce( |
| 417 | yyParser *yypParser, /* The parser */ |
| 418 | int yyruleno /* Number of the rule by which to reduce */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 419 | ){ |
| 420 | int yygoto; /* The next state */ |
| 421 | int yyact; /* The next action */ |
| 422 | YYMINORTYPE yygotominor; /* The LHS of the rule reduced */ |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 423 | yyStackEntry *yymsp; /* The top of the parser's stack */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 424 | int yysize; /* Amount to pop the stack */ |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 425 | ParseARG_FETCH; |
| 426 | yymsp = yypParser->yytop; |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame^] | 427 | #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 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 435 | switch( yyruleno ){ |
| 436 | /* Beginning here are the reduction cases. A typical example |
| 437 | ** follows: |
| 438 | ** case 0: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 439 | ** #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; |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 448 | yypParser->yyidx -= yysize; |
| 449 | yypParser->yytop -= yysize; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 450 | 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 ){ |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 454 | yy_accept(yypParser); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | |
| 458 | /* |
| 459 | ** The following code executes when the parse fails |
| 460 | */ |
| 461 | static void yy_parse_failed( |
| 462 | yyParser *yypParser /* The parser */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 463 | ){ |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 464 | ParseARG_FETCH; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 465 | #ifndef NDEBUG |
| 466 | if( yyTraceFILE ){ |
| 467 | fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt); |
| 468 | } |
| 469 | #endif |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 470 | while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 471 | /* Here code is inserted which will be executed whenever the |
| 472 | ** parser fails */ |
| 473 | %% |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 474 | ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | /* |
| 478 | ** The following code executes when a syntax error first occurs. |
| 479 | */ |
| 480 | static 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 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 484 | ){ |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 485 | ParseARG_FETCH; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 486 | #define TOKEN (yyminor.yy0) |
| 487 | %% |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 488 | ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | /* |
| 492 | ** The following is executed when the parser accepts |
| 493 | */ |
| 494 | static void yy_accept( |
| 495 | yyParser *yypParser /* The parser */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 496 | ){ |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 497 | ParseARG_FETCH; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 498 | #ifndef NDEBUG |
| 499 | if( yyTraceFILE ){ |
| 500 | fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt); |
| 501 | } |
| 502 | #endif |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 503 | while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 504 | /* Here code is inserted which will be executed whenever the |
| 505 | ** parser accepts */ |
| 506 | %% |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 507 | ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 508 | } |
| 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 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 529 | void Parse( |
| 530 | void *yyp, /* The parser */ |
| 531 | int yymajor, /* The major token code number */ |
| 532 | ParseTOKENTYPE yyminor /* The value for the token */ |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 533 | ParseARG_PDECL /* Optional %extra_argument parameter */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 534 | ){ |
| 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; |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 543 | if( yypParser->yyidx<0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 544 | if( yymajor==0 ) return; |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 545 | yypParser->yyidx = 0; |
| 546 | yypParser->yyerrcnt = -1; |
| 547 | yypParser->yytop = &yypParser->yystack[0]; |
| 548 | yypParser->yytop->stateno = 0; |
| 549 | yypParser->yytop->major = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 550 | } |
| 551 | yyminorunion.yy0 = yyminor; |
| 552 | yyendofinput = (yymajor==0); |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 553 | ParseARG_STORE; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 554 | |
| 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); |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 565 | yypParser->yyerrcnt--; |
| 566 | if( yyendofinput && yypParser->yyidx>=0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 567 | yymajor = 0; |
| 568 | }else{ |
| 569 | yymajor = YYNOCODE; |
| 570 | } |
| 571 | }else if( yyact < YYNSTATE + YYNRULE ){ |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 572 | yy_reduce(yypParser,yyact-YYNSTATE); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 573 | }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 | */ |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 599 | if( yypParser->yyerrcnt<0 ){ |
| 600 | yy_syntax_error(yypParser,yymajor,yyminorunion); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 601 | } |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 602 | if( yypParser->yytop->major==YYERRORSYMBOL || yyerrorhit ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 603 | #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( |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 613 | yypParser->yyidx >= 0 && |
| 614 | yypParser->yytop->major != YYERRORSYMBOL && |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 615 | (yyact = yy_find_parser_action(yypParser,YYERRORSYMBOL)) >= YYNSTATE |
| 616 | ){ |
| 617 | yy_pop_parser_stack(yypParser); |
| 618 | } |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 619 | if( yypParser->yyidx < 0 || yymajor==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 620 | yy_destructor(yymajor,&yyminorunion); |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 621 | yy_parse_failed(yypParser); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 622 | yymajor = YYNOCODE; |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 623 | }else if( yypParser->yytop->major!=YYERRORSYMBOL ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 624 | YYMINORTYPE u2; |
| 625 | u2.YYERRSYMDT = 0; |
| 626 | yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2); |
| 627 | } |
| 628 | } |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 629 | yypParser->yyerrcnt = 3; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 630 | 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 | */ |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 641 | if( yypParser->yyerrcnt<=0 ){ |
| 642 | yy_syntax_error(yypParser,yymajor,yyminorunion); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 643 | } |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 644 | yypParser->yyerrcnt = 3; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 645 | yy_destructor(yymajor,&yyminorunion); |
| 646 | if( yyendofinput ){ |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 647 | yy_parse_failed(yypParser); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 648 | } |
| 649 | yymajor = YYNOCODE; |
| 650 | #endif |
| 651 | }else{ |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 652 | yy_accept(yypParser); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 653 | yymajor = YYNOCODE; |
| 654 | } |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 655 | }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 656 | return; |
| 657 | } |