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