lempar.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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. yyTraceFILE = TraceFILE;
  201. yyTracePrompt = zTracePrompt;
  202. if( yyTraceFILE==0 ) yyTracePrompt = 0;
  203. else if( yyTracePrompt==0 ) yyTraceFILE = 0;
  204. }
  205. #endif /* NDEBUG */
  206. #ifndef NDEBUG
  207. /* For tracing shifts, the names of all terminals and nonterminals
  208. ** are required. The following table supplies these names */
  209. static const char *const yyTokenName[] = {
  210. %%
  211. };
  212. #endif /* NDEBUG */
  213. #ifndef NDEBUG
  214. /* For tracing reduce actions, the names of all rules are required.
  215. */
  216. static const char *const yyRuleName[] = {
  217. %%
  218. };
  219. #endif /* NDEBUG */
  220. #if YYSTACKDEPTH<=0
  221. /*
  222. ** Try to increase the size of the parser stack.
  223. */
  224. static void yyGrowStack(yyParser *p){
  225. int newSize;
  226. yyStackEntry *pNew;
  227. newSize = p->yystksz*2 + 100;
  228. pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
  229. if( pNew ){
  230. p->yystack = pNew;
  231. p->yystksz = newSize;
  232. #ifndef NDEBUG
  233. if( yyTraceFILE ){
  234. fprintf(yyTraceFILE,"%sStack grows to %d entries!\n",
  235. yyTracePrompt, p->yystksz);
  236. }
  237. #endif
  238. }
  239. }
  240. #endif
  241. /*
  242. ** This function allocates a new parser.
  243. ** The only argument is a pointer to a function which works like
  244. ** malloc.
  245. **
  246. ** Inputs:
  247. ** A pointer to the function used to allocate memory.
  248. **
  249. ** Outputs:
  250. ** A pointer to a parser. This pointer is used in subsequent calls
  251. ** to Parse and ParseFree.
  252. */
  253. void *ParseAlloc(void *(*mallocProc)(size_t)){
  254. yyParser *pParser;
  255. pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) );
  256. if( pParser ){
  257. pParser->yyidx = -1;
  258. #ifdef YYTRACKMAXSTACKDEPTH
  259. pParser->yyidxMax = 0;
  260. #endif
  261. #if YYSTACKDEPTH<=0
  262. pParser->yystack = NULL;
  263. pParser->yystksz = 0;
  264. yyGrowStack(pParser);
  265. #endif
  266. }
  267. return pParser;
  268. }
  269. /* The following function deletes the value associated with a
  270. ** symbol. The symbol can be either a terminal or nonterminal.
  271. ** "yymajor" is the symbol code, and "yypminor" is a pointer to
  272. ** the value.
  273. */
  274. static void yy_destructor(
  275. yyParser *yypParser, /* The parser */
  276. YYCODETYPE yymajor, /* Type code for object to destroy */
  277. YYMINORTYPE *yypminor /* The object to be destroyed */
  278. ){
  279. ParseARG_FETCH;
  280. switch( yymajor ){
  281. /* Here is inserted the actions which take place when a
  282. ** terminal or non-terminal is destroyed. This can happen
  283. ** when the symbol is popped from the stack during a
  284. ** reduce or during error processing or when a parser is
  285. ** being destroyed before it is finished parsing.
  286. **
  287. ** Note: during a reduce, the only symbols destroyed are those
  288. ** which appear on the RHS of the rule, but which are not used
  289. ** inside the C code.
  290. */
  291. %%
  292. default: break; /* If no destructor action specified: do nothing */
  293. }
  294. }
  295. /*
  296. ** Pop the parser's stack once.
  297. **
  298. ** If there is a destructor routine associated with the token which
  299. ** is popped from the stack, then call it.
  300. **
  301. ** Return the major token number for the symbol popped.
  302. */
  303. static int yy_pop_parser_stack(yyParser *pParser){
  304. YYCODETYPE yymajor;
  305. yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
  306. if( pParser->yyidx<0 ) return 0;
  307. #ifndef NDEBUG
  308. if( yyTraceFILE && pParser->yyidx>=0 ){
  309. fprintf(yyTraceFILE,"%sPopping %s\n",
  310. yyTracePrompt,
  311. yyTokenName[yytos->major]);
  312. }
  313. #endif
  314. yymajor = yytos->major;
  315. yy_destructor(pParser, yymajor, &yytos->minor);
  316. pParser->yyidx--;
  317. return yymajor;
  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. }
  343. /*
  344. ** Return the peak depth of the stack for a parser.
  345. */
  346. #ifdef YYTRACKMAXSTACKDEPTH
  347. int ParseStackPeak(void *p){
  348. yyParser *pParser = (yyParser*)p;
  349. return pParser->yyidxMax;
  350. }
  351. #endif
  352. /*
  353. ** Find the appropriate action for a parser given the terminal
  354. ** look-ahead token iLookAhead.
  355. **
  356. ** If the look-ahead token is YYNOCODE, then check to see if the action is
  357. ** independent of the look-ahead. If it is, return the action, otherwise
  358. ** return YY_NO_ACTION.
  359. */
  360. static int yy_find_shift_action(
  361. yyParser *pParser, /* The parser */
  362. YYCODETYPE iLookAhead /* The look-ahead token */
  363. ){
  364. int i;
  365. int stateno = pParser->yystack[pParser->yyidx].stateno;
  366. if( stateno>YY_SHIFT_COUNT
  367. || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){
  368. return yy_default[stateno];
  369. }
  370. assert( iLookAhead!=YYNOCODE );
  371. i += iLookAhead;
  372. if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
  373. if( iLookAhead>0 ){
  374. #ifdef YYFALLBACK
  375. YYCODETYPE iFallback; /* Fallback token */
  376. if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
  377. && (iFallback = yyFallback[iLookAhead])!=0 ){
  378. #ifndef NDEBUG
  379. if( yyTraceFILE ){
  380. fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
  381. yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
  382. }
  383. #endif
  384. return yy_find_shift_action(pParser, iFallback);
  385. }
  386. #endif
  387. #ifdef YYWILDCARD
  388. {
  389. int j = i - iLookAhead + YYWILDCARD;
  390. if(
  391. #if YY_SHIFT_MIN+YYWILDCARD<0
  392. j>=0 &&
  393. #endif
  394. #if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT
  395. j<YY_ACTTAB_COUNT &&
  396. #endif
  397. yy_lookahead[j]==YYWILDCARD
  398. ){
  399. #ifndef NDEBUG
  400. if( yyTraceFILE ){
  401. fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
  402. yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]);
  403. }
  404. #endif /* NDEBUG */
  405. return yy_action[j];
  406. }
  407. }
  408. #endif /* YYWILDCARD */
  409. }
  410. return yy_default[stateno];
  411. }else{
  412. return yy_action[i];
  413. }
  414. }
  415. /*
  416. ** Find the appropriate action for a parser given the non-terminal
  417. ** look-ahead token iLookAhead.
  418. **
  419. ** If the look-ahead token is YYNOCODE, then check to see if the action is
  420. ** independent of the look-ahead. If it is, return the action, otherwise
  421. ** return YY_NO_ACTION.
  422. */
  423. static int yy_find_reduce_action(
  424. int stateno, /* Current state number */
  425. YYCODETYPE iLookAhead /* The look-ahead token */
  426. ){
  427. int i;
  428. #ifdef YYERRORSYMBOL
  429. if( stateno>YY_REDUCE_COUNT ){
  430. return yy_default[stateno];
  431. }
  432. #else
  433. assert( stateno<=YY_REDUCE_COUNT );
  434. #endif
  435. i = yy_reduce_ofst[stateno];
  436. assert( i!=YY_REDUCE_USE_DFLT );
  437. assert( iLookAhead!=YYNOCODE );
  438. i += iLookAhead;
  439. #ifdef YYERRORSYMBOL
  440. if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
  441. return yy_default[stateno];
  442. }
  443. #else
  444. assert( i>=0 && i<YY_ACTTAB_COUNT );
  445. assert( yy_lookahead[i]==iLookAhead );
  446. #endif
  447. return yy_action[i];
  448. }
  449. /*
  450. ** The following routine is called if the stack overflows.
  451. */
  452. static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){
  453. ParseARG_FETCH;
  454. yypParser->yyidx--;
  455. #ifndef NDEBUG
  456. if( yyTraceFILE ){
  457. fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
  458. }
  459. #endif
  460. while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
  461. /* Here code is inserted which will execute if the parser
  462. ** stack every overflows */
  463. %%
  464. ParseARG_STORE; /* Suppress warning about unused %extra_argument var */
  465. }
  466. /*
  467. ** Perform a shift action.
  468. */
  469. static void yy_shift(
  470. yyParser *yypParser, /* The parser to be shifted */
  471. int yyNewState, /* The new state to shift in */
  472. int yyMajor, /* The major token to shift in */
  473. YYMINORTYPE *yypMinor /* Pointer to the minor token to shift in */
  474. ){
  475. yyStackEntry *yytos;
  476. yypParser->yyidx++;
  477. #ifdef YYTRACKMAXSTACKDEPTH
  478. if( yypParser->yyidx>yypParser->yyidxMax ){
  479. yypParser->yyidxMax = yypParser->yyidx;
  480. }
  481. #endif
  482. #if YYSTACKDEPTH>0
  483. if( yypParser->yyidx>=YYSTACKDEPTH ){
  484. yyStackOverflow(yypParser, yypMinor);
  485. return;
  486. }
  487. #else
  488. if( yypParser->yyidx>=yypParser->yystksz ){
  489. yyGrowStack(yypParser);
  490. if( yypParser->yyidx>=yypParser->yystksz ){
  491. yyStackOverflow(yypParser, yypMinor);
  492. return;
  493. }
  494. }
  495. #endif
  496. yytos = &yypParser->yystack[yypParser->yyidx];
  497. yytos->stateno = (YYACTIONTYPE)yyNewState;
  498. yytos->major = (YYCODETYPE)yyMajor;
  499. yytos->minor = *yypMinor;
  500. #ifndef NDEBUG
  501. if( yyTraceFILE && yypParser->yyidx>0 ){
  502. int i;
  503. fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
  504. fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
  505. for(i=1; i<=yypParser->yyidx; i++)
  506. fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
  507. fprintf(yyTraceFILE,"\n");
  508. }
  509. #endif
  510. }
  511. /* The following table contains information about every rule that
  512. ** is used during the reduce.
  513. */
  514. static const struct {
  515. YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */
  516. unsigned char nrhs; /* Number of right-hand side symbols in the rule */
  517. } yyRuleInfo[] = {
  518. %%
  519. };
  520. static void yy_accept(yyParser*); /* Forward Declaration */
  521. /*
  522. ** Perform a reduce action and the shift that must immediately
  523. ** follow the reduce.
  524. */
  525. static void yy_reduce(
  526. yyParser *yypParser, /* The parser */
  527. int yyruleno /* Number of the rule by which to reduce */
  528. ){
  529. int yygoto; /* The next state */
  530. int yyact; /* The next action */
  531. YYMINORTYPE yygotominor; /* The LHS of the rule reduced */
  532. yyStackEntry *yymsp; /* The top of the parser's stack */
  533. int yysize; /* Amount to pop the stack */
  534. ParseARG_FETCH;
  535. yymsp = &yypParser->yystack[yypParser->yyidx];
  536. #ifndef NDEBUG
  537. if( yyTraceFILE && yyruleno>=0
  538. && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
  539. fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt,
  540. yyRuleName[yyruleno]);
  541. }
  542. #endif /* NDEBUG */
  543. /* Silence complaints from purify about yygotominor being uninitialized
  544. ** in some cases when it is copied into the stack after the following
  545. ** switch. yygotominor is uninitialized when a rule reduces that does
  546. ** not set the value of its left-hand side nonterminal. Leaving the
  547. ** value of the nonterminal uninitialized is utterly harmless as long
  548. ** as the value is never used. So really the only thing this code
  549. ** accomplishes is to quieten purify.
  550. **
  551. ** 2007-01-16: The wireshark project (www.wireshark.org) reports that
  552. ** without this code, their parser segfaults. I'm not sure what there
  553. ** parser is doing to make this happen. This is the second bug report
  554. ** from wireshark this week. Clearly they are stressing Lemon in ways
  555. ** that it has not been previously stressed... (SQLite ticket #2172)
  556. */
  557. /*memset(&yygotominor, 0, sizeof(yygotominor));*/
  558. yygotominor = yyzerominor;
  559. switch( yyruleno ){
  560. /* Beginning here are the reduction cases. A typical example
  561. ** follows:
  562. ** case 0:
  563. ** #line <lineno> <grammarfile>
  564. ** { ... } // User supplied code
  565. ** #line <lineno> <thisfile>
  566. ** break;
  567. */
  568. %%
  569. };
  570. yygoto = yyRuleInfo[yyruleno].lhs;
  571. yysize = yyRuleInfo[yyruleno].nrhs;
  572. yypParser->yyidx -= yysize;
  573. yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto);
  574. if( yyact < YYNSTATE ){
  575. #ifdef NDEBUG
  576. /* If we are not debugging and the reduce action popped at least
  577. ** one element off the stack, then we can push the new element back
  578. ** onto the stack here, and skip the stack overflow test in yy_shift().
  579. ** That gives a significant speed improvement. */
  580. if( yysize ){
  581. yypParser->yyidx++;
  582. yymsp -= yysize-1;
  583. yymsp->stateno = (YYACTIONTYPE)yyact;
  584. yymsp->major = (YYCODETYPE)yygoto;
  585. yymsp->minor = yygotominor;
  586. }else
  587. #endif
  588. {
  589. yy_shift(yypParser,yyact,yygoto,&yygotominor);
  590. }
  591. }else{
  592. assert( yyact == YYNSTATE + YYNRULE + 1 );
  593. yy_accept(yypParser);
  594. }
  595. }
  596. /*
  597. ** The following code executes when the parse fails
  598. */
  599. #ifndef YYNOERRORRECOVERY
  600. static void yy_parse_failed(
  601. yyParser *yypParser /* The parser */
  602. ){
  603. ParseARG_FETCH;
  604. #ifndef NDEBUG
  605. if( yyTraceFILE ){
  606. fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
  607. }
  608. #endif
  609. while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
  610. /* Here code is inserted which will be executed whenever the
  611. ** parser fails */
  612. %%
  613. ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
  614. }
  615. #endif /* YYNOERRORRECOVERY */
  616. /*
  617. ** The following code executes when a syntax error first occurs.
  618. */
  619. static void yy_syntax_error(
  620. yyParser *yypParser, /* The parser */
  621. int yymajor, /* The major type of the error token */
  622. YYMINORTYPE yyminor /* The minor type of the error token */
  623. ){
  624. ParseARG_FETCH;
  625. #define TOKEN (yyminor.yy0)
  626. %%
  627. ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
  628. }
  629. /*
  630. ** The following is executed when the parser accepts
  631. */
  632. static void yy_accept(
  633. yyParser *yypParser /* The parser */
  634. ){
  635. ParseARG_FETCH;
  636. #ifndef NDEBUG
  637. if( yyTraceFILE ){
  638. fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
  639. }
  640. #endif
  641. while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
  642. /* Here code is inserted which will be executed whenever the
  643. ** parser accepts */
  644. %%
  645. ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
  646. }
  647. /* The main parser program.
  648. ** The first argument is a pointer to a structure obtained from
  649. ** "ParseAlloc" which describes the current state of the parser.
  650. ** The second argument is the major token number. The third is
  651. ** the minor token. The fourth optional argument is whatever the
  652. ** user wants (and specified in the grammar) and is available for
  653. ** use by the action routines.
  654. **
  655. ** Inputs:
  656. ** <ul>
  657. ** <li> A pointer to the parser (an opaque structure.)
  658. ** <li> The major token number.
  659. ** <li> The minor token number.
  660. ** <li> An option argument of a grammar-specified type.
  661. ** </ul>
  662. **
  663. ** Outputs:
  664. ** None.
  665. */
  666. void Parse(
  667. void *yyp, /* The parser */
  668. int yymajor, /* The major token code number */
  669. ParseTOKENTYPE yyminor /* The value for the token */
  670. ParseARG_PDECL /* Optional %extra_argument parameter */
  671. ){
  672. YYMINORTYPE yyminorunion;
  673. int yyact; /* The parser action. */
  674. int yyendofinput; /* True if we are at the end of input */
  675. #ifdef YYERRORSYMBOL
  676. int yyerrorhit = 0; /* True if yymajor has invoked an error */
  677. #endif
  678. yyParser *yypParser; /* The parser */
  679. /* (re)initialize the parser, if necessary */
  680. yypParser = (yyParser*)yyp;
  681. if( yypParser->yyidx<0 ){
  682. #if YYSTACKDEPTH<=0
  683. if( yypParser->yystksz <=0 ){
  684. /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/
  685. yyminorunion = yyzerominor;
  686. yyStackOverflow(yypParser, &yyminorunion);
  687. return;
  688. }
  689. #endif
  690. yypParser->yyidx = 0;
  691. yypParser->yyerrcnt = -1;
  692. yypParser->yystack[0].stateno = 0;
  693. yypParser->yystack[0].major = 0;
  694. }
  695. yyminorunion.yy0 = yyminor;
  696. yyendofinput = (yymajor==0);
  697. ParseARG_STORE;
  698. #ifndef NDEBUG
  699. if( yyTraceFILE ){
  700. fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
  701. }
  702. #endif
  703. do{
  704. yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor);
  705. if( yyact<YYNSTATE ){
  706. assert( !yyendofinput ); /* Impossible to shift the $ token */
  707. yy_shift(yypParser,yyact,yymajor,&yyminorunion);
  708. yypParser->yyerrcnt--;
  709. yymajor = YYNOCODE;
  710. }else if( yyact < YYNSTATE + YYNRULE ){
  711. yy_reduce(yypParser,yyact-YYNSTATE);
  712. }else{
  713. assert( yyact == YY_ERROR_ACTION );
  714. #ifdef YYERRORSYMBOL
  715. int yymx;
  716. #endif
  717. #ifndef NDEBUG
  718. if( yyTraceFILE ){
  719. fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
  720. }
  721. #endif
  722. #ifdef YYERRORSYMBOL
  723. /* A syntax error has occurred.
  724. ** The response to an error depends upon whether or not the
  725. ** grammar defines an error token "ERROR".
  726. **
  727. ** This is what we do if the grammar does define ERROR:
  728. **
  729. ** * Call the %syntax_error function.
  730. **
  731. ** * Begin popping the stack until we enter a state where
  732. ** it is legal to shift the error symbol, then shift
  733. ** the error symbol.
  734. **
  735. ** * Set the error count to three.
  736. **
  737. ** * Begin accepting and shifting new tokens. No new error
  738. ** processing will occur until three tokens have been
  739. ** shifted successfully.
  740. **
  741. */
  742. if( yypParser->yyerrcnt<0 ){
  743. yy_syntax_error(yypParser,yymajor,yyminorunion);
  744. }
  745. yymx = yypParser->yystack[yypParser->yyidx].major;
  746. if( yymx==YYERRORSYMBOL || yyerrorhit ){
  747. #ifndef NDEBUG
  748. if( yyTraceFILE ){
  749. fprintf(yyTraceFILE,"%sDiscard input token %s\n",
  750. yyTracePrompt,yyTokenName[yymajor]);
  751. }
  752. #endif
  753. yy_destructor(yypParser, (YYCODETYPE)yymajor,&yyminorunion);
  754. yymajor = YYNOCODE;
  755. }else{
  756. while(
  757. yypParser->yyidx >= 0 &&
  758. yymx != YYERRORSYMBOL &&
  759. (yyact = yy_find_reduce_action(
  760. yypParser->yystack[yypParser->yyidx].stateno,
  761. YYERRORSYMBOL)) >= YYNSTATE
  762. ){
  763. yy_pop_parser_stack(yypParser);
  764. }
  765. if( yypParser->yyidx < 0 || yymajor==0 ){
  766. yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
  767. yy_parse_failed(yypParser);
  768. yymajor = YYNOCODE;
  769. }else if( yymx!=YYERRORSYMBOL ){
  770. YYMINORTYPE u2;
  771. u2.YYERRSYMDT = 0;
  772. yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
  773. }
  774. }
  775. yypParser->yyerrcnt = 3;
  776. yyerrorhit = 1;
  777. #elif defined(YYNOERRORRECOVERY)
  778. /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
  779. ** do any kind of error recovery. Instead, simply invoke the syntax
  780. ** error routine and continue going as if nothing had happened.
  781. **
  782. ** Applications can set this macro (for example inside %include) if
  783. ** they intend to abandon the parse upon the first syntax error seen.
  784. */
  785. yy_syntax_error(yypParser,yymajor,yyminorunion);
  786. yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
  787. yymajor = YYNOCODE;
  788. #else /* YYERRORSYMBOL is not defined */
  789. /* This is what we do if the grammar does not define ERROR:
  790. **
  791. ** * Report an error message, and throw away the input token.
  792. **
  793. ** * If the input token is $, then fail the parse.
  794. **
  795. ** As before, subsequent error messages are suppressed until
  796. ** three input tokens have been successfully shifted.
  797. */
  798. if( yypParser->yyerrcnt<=0 ){
  799. yy_syntax_error(yypParser,yymajor,yyminorunion);
  800. }
  801. yypParser->yyerrcnt = 3;
  802. yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
  803. if( yyendofinput ){
  804. yy_parse_failed(yypParser);
  805. }
  806. yymajor = YYNOCODE;
  807. #endif
  808. }
  809. }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
  810. return;
  811. }