lempar.c 28 KB

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