lempar.c 27 KB

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