| 1 | /* Driver template for the LEMON parser generator. |
|---|
| 2 | ** The author disclaims copyright to this source code. |
|---|
| 3 | */ |
|---|
| 4 | /* First off, code is include which follows the "include" declaration |
|---|
| 5 | ** in the input file. */ |
|---|
| 6 | #include <stdio.h> |
|---|
| 7 | #include <assert.h> |
|---|
| 8 | %% |
|---|
| 9 | /* Next is all token values, in a form suitable for use by makeheaders. |
|---|
| 10 | ** This section will be null unless lemon is run with the -m switch. |
|---|
| 11 | */ |
|---|
| 12 | /* |
|---|
| 13 | ** These constants (all generated automatically by the parser generator) |
|---|
| 14 | ** specify the various kinds of tokens (terminals) that the parser |
|---|
| 15 | ** understands. |
|---|
| 16 | ** |
|---|
| 17 | ** Each symbol here is a terminal symbol in the grammar. |
|---|
| 18 | */ |
|---|
| 19 | %% |
|---|
| 20 | /* Make sure the INTERFACE macro is defined. |
|---|
| 21 | */ |
|---|
| 22 | #ifndef INTERFACE |
|---|
| 23 | # define INTERFACE 1 |
|---|
| 24 | #endif |
|---|
| 25 | /* The next thing included is series of defines which control |
|---|
| 26 | ** various aspects of the generated parser. |
|---|
| 27 | ** YYCODETYPE is the data type used for storing terminal |
|---|
| 28 | ** and nonterminal numbers. "unsigned char" is |
|---|
| 29 | ** used if there are fewer than 250 terminals |
|---|
| 30 | ** and nonterminals. "int" is used otherwise. |
|---|
| 31 | ** YYNOCODE is a number of type YYCODETYPE which corresponds |
|---|
| 32 | ** to no legal terminal or nonterminal number. This |
|---|
| 33 | ** number is used to fill in empty slots of the hash |
|---|
| 34 | ** table. |
|---|
| 35 | ** YYFALLBACK If defined, this indicates that one or more tokens |
|---|
| 36 | ** have fall-back values which should be used if the |
|---|
| 37 | ** original value of the token will not parse. |
|---|
| 38 | ** YYACTIONTYPE is the data type used for storing terminal |
|---|
| 39 | ** and nonterminal numbers. "unsigned char" is |
|---|
| 40 | ** used if there are fewer than 250 rules and |
|---|
| 41 | ** states combined. "int" is used otherwise. |
|---|
| 42 | ** ParseTOKENTYPE is the data type used for minor tokens given |
|---|
| 43 | ** directly to the parser from the tokenizer. |
|---|
| 44 | ** YYMINORTYPE is the data type used for all minor tokens. |
|---|
| 45 | ** This is typically a union of many types, one of |
|---|
| 46 | ** which is ParseTOKENTYPE. The entry in the union |
|---|
| 47 | ** for base tokens is called "yy0". |
|---|
| 48 | ** YYSTACKDEPTH is the maximum depth of the parser's stack. If |
|---|
| 49 | ** zero the stack is dynamically sized using realloc() |
|---|
| 50 | ** ParseARG_SDECL A static variable declaration for the %extra_argument |
|---|
| 51 | ** ParseARG_PDECL A parameter declaration for the %extra_argument |
|---|
| 52 | ** ParseARG_STORE Code to store %extra_argument into yypParser |
|---|
| 53 | ** ParseARG_FETCH Code to extract %extra_argument from yypParser |
|---|
| 54 | ** YYNSTATE the combined number of states. |
|---|
| 55 | ** YYNRULE the number of rules in the grammar |
|---|
| 56 | ** YYERRORSYMBOL is the code number of the error symbol. If not |
|---|
| 57 | ** defined, then do no error processing. |
|---|
| 58 | */ |
|---|
| 59 | %% |
|---|
| 60 | #define YY_NO_ACTION (YYNSTATE+YYNRULE+2) |
|---|
| 61 | #define YY_ACCEPT_ACTION (YYNSTATE+YYNRULE+1) |
|---|
| 62 | #define YY_ERROR_ACTION (YYNSTATE+YYNRULE) |
|---|
| 63 | |
|---|
| 64 | /* Next are that tables used to determine what action to take based on the |
|---|
| 65 | ** current state and lookahead token. These tables are used to implement |
|---|
| 66 | ** functions that take a state number and lookahead value and return an |
|---|
| 67 | ** action integer. |
|---|
| 68 | ** |
|---|
| 69 | ** Suppose the action integer is N. Then the action is determined as |
|---|
| 70 | ** follows |
|---|
| 71 | ** |
|---|
| 72 | ** 0 <= N < YYNSTATE Shift N. That is, push the lookahead |
|---|
| 73 | ** token onto the stack and goto state N. |
|---|
| 74 | ** |
|---|
| 75 | ** YYNSTATE <= N < YYNSTATE+YYNRULE Reduce by rule N-YYNSTATE. |
|---|
| 76 | ** |
|---|
| 77 | ** N == YYNSTATE+YYNRULE A syntax error has occurred. |
|---|
| 78 | ** |
|---|
| 79 | ** N == YYNSTATE+YYNRULE+1 The parser accepts its input. |
|---|
| 80 | ** |
|---|
| 81 | ** N == YYNSTATE+YYNRULE+2 No such action. Denotes unused |
|---|
| 82 | ** slots in the yy_action[] table. |
|---|
| 83 | ** |
|---|
| 84 | ** The action table is constructed as a single large table named yy_action[]. |
|---|
| 85 | ** Given state S and lookahead X, the action is computed as |
|---|
| 86 | ** |
|---|
| 87 | ** yy_action[ yy_shift_ofst[S] + X ] |
|---|
| 88 | ** |
|---|
| 89 | ** If the index value yy_shift_ofst[S]+X is out of range or if the value |
|---|
| 90 | ** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S] |
|---|
| 91 | ** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table |
|---|
| 92 | ** and that yy_default[S] should be used instead. |
|---|
| 93 | ** |
|---|
| 94 | ** The formula above is for computing the action when the lookahead is |
|---|
| 95 | ** a terminal symbol. If the lookahead is a non-terminal (as occurs after |
|---|
| 96 | ** a reduce action) then the yy_reduce_ofst[] array is used in place of |
|---|
| 97 | ** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of |
|---|
| 98 | ** YY_SHIFT_USE_DFLT. |
|---|
| 99 | ** |
|---|
| 100 | ** The following are the tables generated in this section: |
|---|
| 101 | ** |
|---|
| 102 | ** yy_action[] A single table containing all actions. |
|---|
| 103 | ** yy_lookahead[] A table containing the lookahead for each entry in |
|---|
| 104 | ** yy_action. Used to detect hash collisions. |
|---|
| 105 | ** yy_shift_ofst[] For each state, the offset into yy_action for |
|---|
| 106 | ** shifting terminals. |
|---|
| 107 | ** yy_reduce_ofst[] For each state, the offset into yy_action for |
|---|
| 108 | ** shifting non-terminals after a reduce. |
|---|
| 109 | ** yy_default[] Default action for each state. |
|---|
| 110 | */ |
|---|
| 111 | %% |
|---|
| 112 | #define YY_SZ_ACTTAB (int)(sizeof(yy_action)/sizeof(yy_action[0])) |
|---|
| 113 | |
|---|
| 114 | /* The next table maps tokens into fallback tokens. If a construct |
|---|
| 115 | ** like the following: |
|---|
| 116 | ** |
|---|
| 117 | ** %fallback ID X Y Z. |
|---|
| 118 | ** |
|---|
| 119 | ** appears in the grammer, then ID becomes a fallback token for X, Y, |
|---|
| 120 | ** and Z. Whenever one of the tokens X, Y, or Z is input to the parser |
|---|
| 121 | ** but it does not parse, the type of the token is changed to ID and |
|---|
| 122 | ** the parse is retried before an error is thrown. |
|---|
| 123 | */ |
|---|
| 124 | #ifdef YYFALLBACK |
|---|
| 125 | static const YYCODETYPE yyFallback[] = { |
|---|
| 126 | %% |
|---|
| 127 | }; |
|---|
| 128 | #endif /* YYFALLBACK */ |
|---|
| 129 | |
|---|
| 130 | /* The following structure represents a single element of the |
|---|
| 131 | ** parser's stack. Information stored includes: |
|---|
| 132 | ** |
|---|
| 133 | ** + The state number for the parser at this level of the stack. |
|---|
| 134 | ** |
|---|
| 135 | ** + The value of the token stored at this level of the stack. |
|---|
| 136 | ** (In other words, the "major" token.) |
|---|
| 137 | ** |
|---|
| 138 | ** + The semantic value stored at this level of the stack. This is |
|---|
| 139 | ** the information used by the action routines in the grammar. |
|---|
| 140 | ** It is sometimes called the "minor" token. |
|---|
| 141 | */ |
|---|
| 142 | struct yyStackEntry { |
|---|
| 143 | int stateno; /* The state-number */ |
|---|
| 144 | int major; /* The major token value. This is the code |
|---|
| 145 | ** number for the token at this stack level */ |
|---|
| 146 | YYMINORTYPE minor; /* The user-supplied minor token value. This |
|---|
| 147 | ** is the value of the token */ |
|---|
| 148 | }; |
|---|
| 149 | typedef struct yyStackEntry yyStackEntry; |
|---|
| 150 | |
|---|
| 151 | /* The state of the parser is completely contained in an instance of |
|---|
| 152 | ** the following structure */ |
|---|
| 153 | struct yyParser { |
|---|
| 154 | int yyidx; /* Index of top element in stack */ |
|---|
| 155 | int yyerrcnt; /* Shifts left before out of the error */ |
|---|
| 156 | ParseARG_SDECL /* A place to hold %extra_argument */ |
|---|
| 157 | #if YYSTACKDEPTH<=0 |
|---|
| 158 | int yystksz; /* Current side of the stack */ |
|---|
| 159 | yyStackEntry *yystack; /* The parser's stack */ |
|---|
| 160 | #else |
|---|
| 161 | yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */ |
|---|
| 162 | #endif |
|---|
| 163 | }; |
|---|
| 164 | typedef struct yyParser yyParser; |
|---|
| 165 | |
|---|
| 166 | #ifndef NDEBUG |
|---|
| 167 | #include <stdio.h> |
|---|
| 168 | static FILE *yyTraceFILE = 0; |
|---|
| 169 | static char *yyTracePrompt = 0; |
|---|
| 170 | #endif /* NDEBUG */ |
|---|
| 171 | |
|---|
| 172 | #ifndef NDEBUG |
|---|
| 173 | /* |
|---|
| 174 | ** Turn parser tracing on by giving a stream to which to write the trace |
|---|
| 175 | ** and a prompt to preface each trace message. Tracing is turned off |
|---|
| 176 | ** by making either argument NULL |
|---|
| 177 | ** |
|---|
| 178 | ** Inputs: |
|---|
| 179 | ** <ul> |
|---|
| 180 | ** <li> A FILE* to which trace output should be written. |
|---|
| 181 | ** If NULL, then tracing is turned off. |
|---|
| 182 | ** <li> A prefix string written at the beginning of every |
|---|
| 183 | ** line of trace output. If NULL, then tracing is |
|---|
| 184 | ** turned off. |
|---|
| 185 | ** </ul> |
|---|
| 186 | ** |
|---|
| 187 | ** Outputs: |
|---|
| 188 | ** None. |
|---|
| 189 | */ |
|---|
| 190 | void ParseTrace(FILE *TraceFILE, char *zTracePrompt){ |
|---|
| 191 | yyTraceFILE = TraceFILE; |
|---|
| 192 | yyTracePrompt = zTracePrompt; |
|---|
| 193 | if( yyTraceFILE==0 ) yyTracePrompt = 0; |
|---|
| 194 | else if( yyTracePrompt==0 ) yyTraceFILE = 0; |
|---|
| 195 | } |
|---|
| 196 | #endif /* NDEBUG */ |
|---|
| 197 | |
|---|
| 198 | #ifndef NDEBUG |
|---|
| 199 | /* For tracing shifts, the names of all terminals and nonterminals |
|---|
| 200 | ** are required. The following table supplies these names */ |
|---|
| 201 | static const char *const yyTokenName[] = { |
|---|
| 202 | %% |
|---|
| 203 | }; |
|---|
| 204 | #endif /* NDEBUG */ |
|---|
| 205 | |
|---|
| 206 | #ifndef NDEBUG |
|---|
| 207 | /* For tracing reduce actions, the names of all rules are required. |
|---|
| 208 | */ |
|---|
| 209 | static const char *const yyRuleName[] = { |
|---|
| 210 | %% |
|---|
| 211 | }; |
|---|
| 212 | #endif /* NDEBUG */ |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | #if YYSTACKDEPTH<=0 |
|---|
| 216 | /* |
|---|
| 217 | ** Try to increase the size of the parser stack. |
|---|
| 218 | */ |
|---|
| 219 | static void yyGrowStack(yyParser *p){ |
|---|
| 220 | int newSize; |
|---|
| 221 | yyStackEntry *pNew; |
|---|
| 222 | |
|---|
| 223 | newSize = p->yystksz*2 + 100; |
|---|
| 224 | pNew = realloc(p->yystack, newSize*sizeof(pNew[0])); |
|---|
| 225 | if( pNew ){ |
|---|
| 226 | p->yystack = pNew; |
|---|
| 227 | p->yystksz = newSize; |
|---|
| 228 | #ifndef NDEBUG |
|---|
| 229 | if( yyTraceFILE ){ |
|---|
| 230 | fprintf(yyTraceFILE,"%sStack grows to %d entries!\n", |
|---|
| 231 | yyTracePrompt, p->yystksz); |
|---|
| 232 | } |
|---|
| 233 | #endif |
|---|
| 234 | } |
|---|
| 235 | } |
|---|
| 236 | #endif |
|---|
| 237 | |
|---|
| 238 | /* MW */ |
|---|
| 239 | Lsystem *lsys; |
|---|
| 240 | bool syntaxOnly; |
|---|
| 241 | bool *syntaxOk; |
|---|
| 242 | |
|---|
| 243 | /* |
|---|
| 244 | ** This function allocates a new parser. |
|---|
| 245 | ** The only argument is a pointer to a function which works like |
|---|
| 246 | ** malloc. |
|---|
| 247 | ** |
|---|
| 248 | ** Inputs: |
|---|
| 249 | ** A pointer to the function used to allocate memory. |
|---|
| 250 | ** |
|---|
| 251 | ** Outputs: |
|---|
| 252 | ** A pointer to a parser. This pointer is used in subsequent calls |
|---|
| 253 | ** to Parse and ParseFree. |
|---|
| 254 | */ |
|---|
| 255 | void *ParseAlloc(void *(*mallocProc)(size_t), Lsystem *lsystem, bool checkSyntaxOnly = false, bool *isSyntaxOk = NULL){ |
|---|
| 256 | yyParser *pParser; |
|---|
| 257 | pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) ); |
|---|
| 258 | if( pParser ){ |
|---|
| 259 | pParser->yyidx = -1; |
|---|
| 260 | #if YYSTACKDEPTH<=0 |
|---|
| 261 | yyGrowStack(pParser); |
|---|
| 262 | #endif |
|---|
| 263 | //input = new Lsystem(); |
|---|
| 264 | lsys = lsystem; |
|---|
| 265 | syntaxOnly = checkSyntaxOnly; |
|---|
| 266 | syntaxOk = isSyntaxOk; |
|---|
| 267 | } |
|---|
| 268 | return pParser; |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | /* The following function deletes the value associated with a |
|---|
| 272 | ** symbol. The symbol can be either a terminal or nonterminal. |
|---|
| 273 | ** "yymajor" is the symbol code, and "yypminor" is a pointer to |
|---|
| 274 | ** the value. |
|---|
| 275 | */ |
|---|
| 276 | static void yy_destructor(YYCODETYPE yymajor, YYMINORTYPE *yypminor){ |
|---|
| 277 | switch( yymajor ){ |
|---|
| 278 | /* Here is inserted the actions which take place when a |
|---|
| 279 | ** terminal or non-terminal is destroyed. This can happen |
|---|
| 280 | ** when the symbol is popped from the stack during a |
|---|
| 281 | ** reduce or during error processing or when a parser is |
|---|
| 282 | ** being destroyed before it is finished parsing. |
|---|
| 283 | ** |
|---|
| 284 | ** Note: during a reduce, the only symbols destroyed are those |
|---|
| 285 | ** which appear on the RHS of the rule, but which are not used |
|---|
| 286 | ** inside the C code. |
|---|
| 287 | */ |
|---|
| 288 | %% |
|---|
| 289 | default: break; /* If no destructor action specified: do nothing */ |
|---|
| 290 | } |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | /* |
|---|
| 294 | ** Pop the parser's stack once. |
|---|
| 295 | ** |
|---|
| 296 | ** If there is a destructor routine associated with the token which |
|---|
| 297 | ** is popped from the stack, then call it. |
|---|
| 298 | ** |
|---|
| 299 | ** Return the major token number for the symbol popped. |
|---|
| 300 | */ |
|---|
| 301 | static int yy_pop_parser_stack(yyParser *pParser){ |
|---|
| 302 | YYCODETYPE yymajor; |
|---|
| 303 | yyStackEntry *yytos = &pParser->yystack[pParser->yyidx]; |
|---|
| 304 | |
|---|
| 305 | if( pParser->yyidx<0 ) return 0; |
|---|
| 306 | #ifndef NDEBUG |
|---|
| 307 | if( yyTraceFILE && pParser->yyidx>=0 ){ |
|---|
| 308 | fprintf(yyTraceFILE,"%sPopping %s\n", |
|---|
| 309 | yyTracePrompt, |
|---|
| 310 | yyTokenName[yytos->major]); |
|---|
| 311 | } |
|---|
| 312 | #endif |
|---|
| 313 | yymajor = yytos->major; |
|---|
| 314 | yy_destructor( yymajor, &yytos->minor); |
|---|
| 315 | pParser->yyidx--; |
|---|
| 316 | return yymajor; |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | /* |
|---|
| 320 | ** Deallocate and destroy a parser. Destructors are all called for |
|---|
| 321 | ** all stack elements before shutting the parser down. |
|---|
| 322 | ** |
|---|
| 323 | ** Inputs: |
|---|
| 324 | ** <ul> |
|---|
| 325 | ** <li> A pointer to the parser. This should be a pointer |
|---|
| 326 | ** obtained from ParseAlloc. |
|---|
| 327 | ** <li> A pointer to a function used to reclaim memory obtained |
|---|
| 328 | ** from malloc. |
|---|
| 329 | ** </ul> |
|---|
| 330 | */ |
|---|
| 331 | void ParseFree( |
|---|
| 332 | void *p, /* The parser to be deleted */ |
|---|
| 333 | void (*freeProc)(void*) /* Function used to reclaim memory */ |
|---|
| 334 | ){ |
|---|
| 335 | yyParser *pParser = (yyParser*)p; |
|---|
| 336 | if( pParser==0 ) return; |
|---|
| 337 | while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser); |
|---|
| 338 | #if YYSTACKDEPTH<=0 |
|---|
| 339 | free(pParser->yystack); |
|---|
| 340 | #endif |
|---|
| 341 | (*freeProc)((void*)pParser); |
|---|
| 342 | //delete lsys; |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | /* |
|---|
| 346 | ** Find the appropriate action for a parser given the terminal |
|---|
| 347 | ** look-ahead token iLookAhead. |
|---|
| 348 | ** |
|---|
| 349 | ** If the look-ahead token is YYNOCODE, then check to see if the action is |
|---|
| 350 | ** independent of the look-ahead. If it is, return the action, otherwise |
|---|
| 351 | ** return YY_NO_ACTION. |
|---|
| 352 | */ |
|---|
| 353 | static int yy_find_shift_action( |
|---|
| 354 | yyParser *pParser, /* The parser */ |
|---|
| 355 | YYCODETYPE iLookAhead /* The look-ahead token */ |
|---|
| 356 | ){ |
|---|
| 357 | int i; |
|---|
| 358 | int stateno = pParser->yystack[pParser->yyidx].stateno; |
|---|
| 359 | |
|---|
| 360 | if( stateno>YY_SHIFT_MAX || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){ |
|---|
| 361 | return yy_default[stateno]; |
|---|
| 362 | } |
|---|
| 363 | assert( iLookAhead!=YYNOCODE ); |
|---|
| 364 | i += iLookAhead; |
|---|
| 365 | if( i<0 || i>=YY_SZ_ACTTAB || yy_lookahead[i]!=iLookAhead ){ |
|---|
| 366 | if( iLookAhead>0 ){ |
|---|
| 367 | #ifdef YYFALLBACK |
|---|
| 368 | int iFallback; /* Fallback token */ |
|---|
| 369 | if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0]) |
|---|
| 370 | && (iFallback = yyFallback[iLookAhead])!=0 ){ |
|---|
| 371 | #ifndef NDEBUG |
|---|
| 372 | if( yyTraceFILE ){ |
|---|
| 373 | fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n", |
|---|
| 374 | yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]); |
|---|
| 375 | } |
|---|
| 376 | #endif |
|---|
| 377 | return yy_find_shift_action(pParser, iFallback); |
|---|
| 378 | } |
|---|
| 379 | #endif |
|---|
| 380 | #ifdef YYWILDCARD |
|---|
| 381 | { |
|---|
| 382 | int j = i - iLookAhead + YYWILDCARD; |
|---|
| 383 | if( j>=0 && j<YY_SZ_ACTTAB && yy_lookahead[j]==YYWILDCARD ){ |
|---|
| 384 | #ifndef NDEBUG |
|---|
| 385 | if( yyTraceFILE ){ |
|---|
| 386 | fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n", |
|---|
| 387 | yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]); |
|---|
| 388 | } |
|---|
| 389 | #endif /* NDEBUG */ |
|---|
| 390 | return yy_action[j]; |
|---|
| 391 | } |
|---|
| 392 | } |
|---|
| 393 | #endif /* YYWILDCARD */ |
|---|
| 394 | } |
|---|
| 395 | return yy_default[stateno]; |
|---|
| 396 | }else{ |
|---|
| 397 | return yy_action[i]; |
|---|
| 398 | } |
|---|
| 399 | } |
|---|
| 400 | |
|---|
| 401 | /* |
|---|
| 402 | ** Find the appropriate action for a parser given the non-terminal |
|---|
| 403 | ** look-ahead token iLookAhead. |
|---|
| 404 | ** |
|---|
| 405 | ** If the look-ahead token is YYNOCODE, then check to see if the action is |
|---|
| 406 | ** independent of the look-ahead. If it is, return the action, otherwise |
|---|
| 407 | ** return YY_NO_ACTION. |
|---|
| 408 | */ |
|---|
| 409 | static int yy_find_reduce_action( |
|---|
| 410 | int stateno, /* Current state number */ |
|---|
| 411 | YYCODETYPE iLookAhead /* The look-ahead token */ |
|---|
| 412 | ){ |
|---|
| 413 | int i; |
|---|
| 414 | assert( stateno<=YY_REDUCE_MAX ); |
|---|
| 415 | i = yy_reduce_ofst[stateno]; |
|---|
| 416 | assert( i!=YY_REDUCE_USE_DFLT ); |
|---|
| 417 | assert( iLookAhead!=YYNOCODE ); |
|---|
| 418 | i += iLookAhead; |
|---|
| 419 | assert( i>=0 && i<YY_SZ_ACTTAB ); |
|---|
| 420 | assert( yy_lookahead[i]==iLookAhead ); |
|---|
| 421 | return yy_action[i]; |
|---|
| 422 | } |
|---|
| 423 | |
|---|
| 424 | /* |
|---|
| 425 | ** The following routine is called if the stack overflows. |
|---|
| 426 | */ |
|---|
| 427 | static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){ |
|---|
| 428 | ParseARG_FETCH; |
|---|
| 429 | yypParser->yyidx--; |
|---|
| 430 | #ifndef NDEBUG |
|---|
| 431 | if( yyTraceFILE ){ |
|---|
| 432 | fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt); |
|---|
| 433 | } |
|---|
| 434 | #endif |
|---|
| 435 | while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser); |
|---|
| 436 | /* Here code is inserted which will execute if the parser |
|---|
| 437 | ** stack every overflows */ |
|---|
| 438 | %% |
|---|
| 439 | ParseARG_STORE; /* Suppress warning about unused %extra_argument var */ |
|---|
| 440 | } |
|---|
| 441 | |
|---|
| 442 | /* |
|---|
| 443 | ** Perform a shift action. |
|---|
| 444 | */ |
|---|
| 445 | static void yy_shift( |
|---|
| 446 | yyParser *yypParser, /* The parser to be shifted */ |
|---|
| 447 | int yyNewState, /* The new state to shift in */ |
|---|
| 448 | int yyMajor, /* The major token to shift in */ |
|---|
| 449 | YYMINORTYPE *yypMinor /* Pointer ot the minor token to shift in */ |
|---|
| 450 | ){ |
|---|
| 451 | yyStackEntry *yytos; |
|---|
| 452 | yypParser->yyidx++; |
|---|
| 453 | #if YYSTACKDEPTH>0 |
|---|
| 454 | if( yypParser->yyidx>=YYSTACKDEPTH ){ |
|---|
| 455 | yyStackOverflow(yypParser, yypMinor); |
|---|
| 456 | return; |
|---|
| 457 | } |
|---|
| 458 | #else |
|---|
| 459 | if( yypParser->yyidx>=yypParser->yystksz ){ |
|---|
| 460 | yyGrowStack(yypParser); |
|---|
| 461 | if( yypParser->yyidx>=yypParser->yystksz ){ |
|---|
| 462 | yyStackOverflow(yypParser, yypMinor); |
|---|
| 463 | return; |
|---|
| 464 | } |
|---|
| 465 | } |
|---|
| 466 | #endif |
|---|
| 467 | yytos = &yypParser->yystack[yypParser->yyidx]; |
|---|
| 468 | yytos->stateno = yyNewState; |
|---|
| 469 | yytos->major = yyMajor; |
|---|
| 470 | yytos->minor = *yypMinor; |
|---|
| 471 | #ifndef NDEBUG |
|---|
| 472 | if( yyTraceFILE && yypParser->yyidx>0 ){ |
|---|
| 473 | int i; |
|---|
| 474 | fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState); |
|---|
| 475 | fprintf(yyTraceFILE,"%sStack:",yyTracePrompt); |
|---|
| 476 | for(i=1; i<=yypParser->yyidx; i++) |
|---|
| 477 | fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]); |
|---|
| 478 | fprintf(yyTraceFILE,"\n"); |
|---|
| 479 | } |
|---|
| 480 | #endif |
|---|
| 481 | } |
|---|
| 482 | |
|---|
| 483 | /* The following table contains information about every rule that |
|---|
| 484 | ** is used during the reduce. |
|---|
| 485 | */ |
|---|
| 486 | static const struct { |
|---|
| 487 | YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */ |
|---|
| 488 | unsigned char nrhs; /* Number of right-hand side symbols in the rule */ |
|---|
| 489 | } yyRuleInfo[] = { |
|---|
| 490 | %% |
|---|
| 491 | }; |
|---|
| 492 | |
|---|
| 493 | static void yy_accept(yyParser*); /* Forward Declaration */ |
|---|
| 494 | |
|---|
| 495 | /* |
|---|
| 496 | ** Perform a reduce action and the shift that must immediately |
|---|
| 497 | ** follow the reduce. |
|---|
| 498 | */ |
|---|
| 499 | static void yy_reduce( |
|---|
| 500 | yyParser *yypParser, /* The parser */ |
|---|
| 501 | int yyruleno /* Number of the rule by which to reduce */ |
|---|
| 502 | ){ |
|---|
| 503 | int yygoto; /* The next state */ |
|---|
| 504 | int yyact; /* The next action */ |
|---|
| 505 | YYMINORTYPE yygotominor; /* The LHS of the rule reduced */ |
|---|
| 506 | yyStackEntry *yymsp; /* The top of the parser's stack */ |
|---|
| 507 | int yysize; /* Amount to pop the stack */ |
|---|
| 508 | ParseARG_FETCH; |
|---|
| 509 | yymsp = &yypParser->yystack[yypParser->yyidx]; |
|---|
| 510 | #ifndef NDEBUG |
|---|
| 511 | if( yyTraceFILE && yyruleno>=0 |
|---|
| 512 | && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){ |
|---|
| 513 | fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt, |
|---|
| 514 | yyRuleName[yyruleno]); |
|---|
| 515 | } |
|---|
| 516 | #endif /* NDEBUG */ |
|---|
| 517 | |
|---|
| 518 | /* Silence complaints from purify about yygotominor being uninitialized |
|---|
| 519 | ** in some cases when it is copied into the stack after the following |
|---|
| 520 | ** switch. yygotominor is uninitialized when a rule reduces that does |
|---|
| 521 | ** not set the value of its left-hand side nonterminal. Leaving the |
|---|
| 522 | ** value of the nonterminal uninitialized is utterly harmless as long |
|---|
| 523 | ** as the value is never used. So really the only thing this code |
|---|
| 524 | ** accomplishes is to quieten purify. |
|---|
| 525 | ** |
|---|
| 526 | ** 2007-01-16: The wireshark project (www.wireshark.org) reports that |
|---|
| 527 | ** without this code, their parser segfaults. I'm not sure what there |
|---|
| 528 | ** parser is doing to make this happen. This is the second bug report |
|---|
| 529 | ** from wireshark this week. Clearly they are stressing Lemon in ways |
|---|
| 530 | ** that it has not been previously stressed... (SQLite ticket #2172) |
|---|
| 531 | */ |
|---|
| 532 | memset(&yygotominor, 0, sizeof(yygotominor)); |
|---|
| 533 | |
|---|
| 534 | |
|---|
| 535 | switch( yyruleno ){ |
|---|
| 536 | /* Beginning here are the reduction cases. A typical example |
|---|
| 537 | ** follows: |
|---|
| 538 | ** case 0: |
|---|
| 539 | ** #line <lineno> <grammarfile> |
|---|
| 540 | ** { ... } // User supplied code |
|---|
| 541 | ** #line <lineno> <thisfile> |
|---|
| 542 | ** break; |
|---|
| 543 | */ |
|---|
| 544 | %% |
|---|
| 545 | }; |
|---|
| 546 | yygoto = yyRuleInfo[yyruleno].lhs; |
|---|
| 547 | yysize = yyRuleInfo[yyruleno].nrhs; |
|---|
| 548 | yypParser->yyidx -= yysize; |
|---|
| 549 | yyact = yy_find_reduce_action(yymsp[-yysize].stateno,yygoto); |
|---|
| 550 | if( yyact < YYNSTATE ){ |
|---|
| 551 | #ifdef NDEBUG |
|---|
| 552 | /* If we are not debugging and the reduce action popped at least |
|---|
| 553 | ** one element off the stack, then we can push the new element back |
|---|
| 554 | ** onto the stack here, and skip the stack overflow test in yy_shift(). |
|---|
| 555 | ** That gives a significant speed improvement. */ |
|---|
| 556 | if( yysize ){ |
|---|
| 557 | yypParser->yyidx++; |
|---|
| 558 | yymsp -= yysize-1; |
|---|
| 559 | yymsp->stateno = yyact; |
|---|
| 560 | yymsp->major = yygoto; |
|---|
| 561 | yymsp->minor = yygotominor; |
|---|
| 562 | }else |
|---|
| 563 | #endif |
|---|
| 564 | { |
|---|
| 565 | yy_shift(yypParser,yyact,yygoto,&yygotominor); |
|---|
| 566 | } |
|---|
| 567 | }else{ |
|---|
| 568 | assert( yyact == YYNSTATE + YYNRULE + 1 ); |
|---|
| 569 | yy_accept(yypParser); |
|---|
| 570 | } |
|---|
| 571 | } |
|---|
| 572 | |
|---|
| 573 | /* |
|---|
| 574 | ** The following code executes when the parse fails |
|---|
| 575 | */ |
|---|
| 576 | static void yy_parse_failed( |
|---|
| 577 | yyParser *yypParser /* The parser */ |
|---|
| 578 | ){ |
|---|
| 579 | ParseARG_FETCH; |
|---|
| 580 | #ifndef NDEBUG |
|---|
| 581 | if( yyTraceFILE ){ |
|---|
| 582 | fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt); |
|---|
| 583 | } |
|---|
| 584 | #endif |
|---|
| 585 | while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser); |
|---|
| 586 | /* Here code is inserted which will be executed whenever the |
|---|
| 587 | ** parser fails */ |
|---|
| 588 | %% |
|---|
| 589 | ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ |
|---|
| 590 | } |
|---|
| 591 | |
|---|
| 592 | /* |
|---|
| 593 | ** The following code executes when a syntax error first occurs. |
|---|
| 594 | */ |
|---|
| 595 | static void yy_syntax_error( |
|---|
| 596 | yyParser *yypParser, /* The parser */ |
|---|
| 597 | int yymajor, /* The major type of the error token */ |
|---|
| 598 | YYMINORTYPE yyminor /* The minor type of the error token */ |
|---|
| 599 | ){ |
|---|
| 600 | ParseARG_FETCH; |
|---|
| 601 | #define TOKEN (yyminor.yy0) |
|---|
| 602 | %% |
|---|
| 603 | ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ |
|---|
| 604 | } |
|---|
| 605 | |
|---|
| 606 | /* |
|---|
| 607 | ** The following is executed when the parser accepts |
|---|
| 608 | */ |
|---|
| 609 | static void yy_accept( |
|---|
| 610 | yyParser *yypParser /* The parser */ |
|---|
| 611 | ){ |
|---|
| 612 | ParseARG_FETCH; |
|---|
| 613 | #ifndef NDEBUG |
|---|
| 614 | if( yyTraceFILE ){ |
|---|
| 615 | fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt); |
|---|
| 616 | } |
|---|
| 617 | #endif |
|---|
| 618 | while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser); |
|---|
| 619 | /* Here code is inserted which will be executed whenever the |
|---|
| 620 | ** parser accepts */ |
|---|
| 621 | %% |
|---|
| 622 | ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ |
|---|
| 623 | } |
|---|
| 624 | |
|---|
| 625 | /* The main parser program. |
|---|
| 626 | ** The first argument is a pointer to a structure obtained from |
|---|
| 627 | ** "ParseAlloc" which describes the current state of the parser. |
|---|
| 628 | ** The second argument is the major token number. The third is |
|---|
| 629 | ** the minor token. The fourth optional argument is whatever the |
|---|
| 630 | ** user wants (and specified in the grammar) and is available for |
|---|
| 631 | ** use by the action routines. |
|---|
| 632 | ** |
|---|
| 633 | ** Inputs: |
|---|
| 634 | ** <ul> |
|---|
| 635 | ** <li> A pointer to the parser (an opaque structure.) |
|---|
| 636 | ** <li> The major token number. |
|---|
| 637 | ** <li> The minor token number. |
|---|
| 638 | ** <li> An option argument of a grammar-specified type. |
|---|
| 639 | ** </ul> |
|---|
| 640 | ** |
|---|
| 641 | ** Outputs: |
|---|
| 642 | ** None. |
|---|
| 643 | */ |
|---|
| 644 | void Parse( |
|---|
| 645 | void *yyp, /* The parser */ |
|---|
| 646 | int yymajor, /* The major token code number */ |
|---|
| 647 | ParseTOKENTYPE yyminor /* The value for the token */ |
|---|
| 648 | ParseARG_PDECL /* Optional %extra_argument parameter */ |
|---|
| 649 | ){ |
|---|
| 650 | YYMINORTYPE yyminorunion; |
|---|
| 651 | int yyact; /* The parser action. */ |
|---|
| 652 | int yyendofinput; /* True if we are at the end of input */ |
|---|
| 653 | #ifdef YYERRORSYMBOL |
|---|
| 654 | int yyerrorhit = 0; /* True if yymajor has invoked an error */ |
|---|
| 655 | #endif |
|---|
| 656 | yyParser *yypParser; /* The parser */ |
|---|
| 657 | |
|---|
| 658 | /* (re)initialize the parser, if necessary */ |
|---|
| 659 | yypParser = (yyParser*)yyp; |
|---|
| 660 | if( yypParser->yyidx<0 ){ |
|---|
| 661 | #if YYSTACKDEPTH<=0 |
|---|
| 662 | if( yypParser->yystksz <=0 ){ |
|---|
| 663 | memset(&yyminorunion, 0, sizeof(yyminorunion)); |
|---|
| 664 | yyStackOverflow(yypParser, &yyminorunion); |
|---|
| 665 | return; |
|---|
| 666 | } |
|---|
| 667 | #endif |
|---|
| 668 | yypParser->yyidx = 0; |
|---|
| 669 | yypParser->yyerrcnt = -1; |
|---|
| 670 | yypParser->yystack[0].stateno = 0; |
|---|
| 671 | yypParser->yystack[0].major = 0; |
|---|
| 672 | } |
|---|
| 673 | yyminorunion.yy0 = yyminor; |
|---|
| 674 | yyendofinput = (yymajor==0); |
|---|
| 675 | ParseARG_STORE; |
|---|
| 676 | |
|---|
| 677 | #ifndef NDEBUG |
|---|
| 678 | if( yyTraceFILE ){ |
|---|
| 679 | fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]); |
|---|
| 680 | } |
|---|
| 681 | #endif |
|---|
| 682 | |
|---|
| 683 | do{ |
|---|
| 684 | yyact = yy_find_shift_action(yypParser,yymajor); |
|---|
| 685 | if( yyact<YYNSTATE ){ |
|---|
| 686 | assert( !yyendofinput ); /* Impossible to shift the $ token */ |
|---|
| 687 | yy_shift(yypParser,yyact,yymajor,&yyminorunion); |
|---|
| 688 | yypParser->yyerrcnt--; |
|---|
| 689 | yymajor = YYNOCODE; |
|---|
| 690 | }else if( yyact < YYNSTATE + YYNRULE ){ |
|---|
| 691 | yy_reduce(yypParser,yyact-YYNSTATE); |
|---|
| 692 | }else{ |
|---|
| 693 | assert( yyact == YY_ERROR_ACTION ); |
|---|
| 694 | #ifdef YYERRORSYMBOL |
|---|
| 695 | int yymx; |
|---|
| 696 | #endif |
|---|
| 697 | #ifndef NDEBUG |
|---|
| 698 | if( yyTraceFILE ){ |
|---|
| 699 | fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt); |
|---|
| 700 | } |
|---|
| 701 | #endif |
|---|
| 702 | #ifdef YYERRORSYMBOL |
|---|
| 703 | /* A syntax error has occurred. |
|---|
| 704 | ** The response to an error depends upon whether or not the |
|---|
| 705 | ** grammar defines an error token "ERROR". |
|---|
| 706 | ** |
|---|
| 707 | ** This is what we do if the grammar does define ERROR: |
|---|
| 708 | ** |
|---|
| 709 | ** * Call the %syntax_error function. |
|---|
| 710 | ** |
|---|
| 711 | ** * Begin popping the stack until we enter a state where |
|---|
| 712 | ** it is legal to shift the error symbol, then shift |
|---|
| 713 | ** the error symbol. |
|---|
| 714 | ** |
|---|
| 715 | ** * Set the error count to three. |
|---|
| 716 | ** |
|---|
| 717 | ** * Begin accepting and shifting new tokens. No new error |
|---|
| 718 | ** processing will occur until three tokens have been |
|---|
| 719 | ** shifted successfully. |
|---|
| 720 | ** |
|---|
| 721 | */ |
|---|
| 722 | if( yypParser->yyerrcnt<0 ){ |
|---|
| 723 | yy_syntax_error(yypParser,yymajor,yyminorunion); |
|---|
| 724 | } |
|---|
| 725 | yymx = yypParser->yystack[yypParser->yyidx].major; |
|---|
| 726 | if( yymx==YYERRORSYMBOL || yyerrorhit ){ |
|---|
| 727 | #ifndef NDEBUG |
|---|
| 728 | if( yyTraceFILE ){ |
|---|
| 729 | fprintf(yyTraceFILE,"%sDiscard input token %s\n", |
|---|
| 730 | yyTracePrompt,yyTokenName[yymajor]); |
|---|
| 731 | } |
|---|
| 732 | #endif |
|---|
| 733 | yy_destructor(yymajor,&yyminorunion); |
|---|
| 734 | yymajor = YYNOCODE; |
|---|
| 735 | }else{ |
|---|
| 736 | while( |
|---|
| 737 | yypParser->yyidx >= 0 && |
|---|
| 738 | yymx != YYERRORSYMBOL && |
|---|
| 739 | (yyact = yy_find_reduce_action( |
|---|
| 740 | yypParser->yystack[yypParser->yyidx].stateno, |
|---|
| 741 | YYERRORSYMBOL)) >= YYNSTATE |
|---|
| 742 | ){ |
|---|
| 743 | yy_pop_parser_stack(yypParser); |
|---|
| 744 | } |
|---|
| 745 | if( yypParser->yyidx < 0 || yymajor==0 ){ |
|---|
| 746 | yy_destructor(yymajor,&yyminorunion); |
|---|
| 747 | yy_parse_failed(yypParser); |
|---|
| 748 | yymajor = YYNOCODE; |
|---|
| 749 | }else if( yymx!=YYERRORSYMBOL ){ |
|---|
| 750 | YYMINORTYPE u2; |
|---|
| 751 | u2.YYERRSYMDT = 0; |
|---|
| 752 | yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2); |
|---|
| 753 | } |
|---|
| 754 | } |
|---|
| 755 | yypParser->yyerrcnt = 3; |
|---|
| 756 | yyerrorhit = 1; |
|---|
| 757 | #else /* YYERRORSYMBOL is not defined */ |
|---|
| 758 | /* This is what we do if the grammar does not define ERROR: |
|---|
| 759 | ** |
|---|
| 760 | ** * Report an error message, and throw away the input token. |
|---|
| 761 | ** |
|---|
| 762 | ** * If the input token is $, then fail the parse. |
|---|
| 763 | ** |
|---|
| 764 | ** As before, subsequent error messages are suppressed until |
|---|
| 765 | ** three input tokens have been successfully shifted. |
|---|
| 766 | */ |
|---|
| 767 | if( yypParser->yyerrcnt<=0 ){ |
|---|
| 768 | yy_syntax_error(yypParser,yymajor,yyminorunion); |
|---|
| 769 | } |
|---|
| 770 | yypParser->yyerrcnt = 3; |
|---|
| 771 | yy_destructor(yymajor,&yyminorunion); |
|---|
| 772 | if( yyendofinput ){ |
|---|
| 773 | yy_parse_failed(yypParser); |
|---|
| 774 | } |
|---|
| 775 | yymajor = YYNOCODE; |
|---|
| 776 | #endif |
|---|
| 777 | } |
|---|
| 778 | }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 ); |
|---|
| 779 | return; |
|---|
| 780 | } |
|---|
| 781 | |
|---|
| 782 | |
|---|