lempar.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. /*
  2. ** 2000-05-29
  3. **
  4. ** The author disclaims copyright to this source code. In place of
  5. ** a legal notice, here is a blessing:
  6. **
  7. ** May you do good and not evil.
  8. ** May you find forgiveness for yourself and forgive others.
  9. ** May you share freely, never taking more than you give.
  10. **
  11. *************************************************************************
  12. ** Driver template for the LEMON parser generator.
  13. **
  14. ** The "lemon" program processes an LALR(1) input grammar file, then uses
  15. ** this template to construct a parser. The "lemon" program inserts text
  16. ** at each "%%" line. Also, any "P-a-r-s-e" identifer prefix (without the
  17. ** interstitial "-" characters) contained in this template is changed into
  18. ** the value of the %name directive from the grammar. Otherwise, the content
  19. ** of this template is copied straight through into the generate parser
  20. ** source file.
  21. **
  22. ** The following is the concatenation of all %include directives from the
  23. ** input grammar file:
  24. */
  25. /************ Begin %include sections from the grammar ************************/
  26. %%
  27. /**************** End of %include directives **********************************/
  28. /* These constants specify the various numeric values for terminal symbols.
  29. ***************** Begin token definitions *************************************/
  30. %%
  31. /**************** End token definitions ***************************************/
  32. /* The next sections is a series of control #defines.
  33. ** various aspects of the generated parser.
  34. ** YYCODETYPE is the data type used to store the integer codes
  35. ** that represent terminal and non-terminal symbols.
  36. ** "unsigned char" is used if there are fewer than
  37. ** 256 symbols. Larger types otherwise.
  38. ** YYNOCODE is a number of type YYCODETYPE that is not used for
  39. ** any terminal or nonterminal symbol.
  40. ** YYFALLBACK If defined, this indicates that one or more tokens
  41. ** (also known as: "terminal symbols") have fall-back
  42. ** values which should be used if the original symbol
  43. ** would not parse. This permits keywords to sometimes
  44. ** be used as identifiers, for example.
  45. ** YYACTIONTYPE is the data type used for "action codes" - numbers
  46. ** that indicate what to do in response to the next
  47. ** token.
  48. ** ParseTOKENTYPE is the data type used for minor type for terminal
  49. ** symbols. Background: A "minor type" is a semantic
  50. ** value associated with a terminal or non-terminal
  51. ** symbols. For example, for an "ID" terminal symbol,
  52. ** the minor type might be the name of the identifier.
  53. ** Each non-terminal can have a different minor type.
  54. ** Terminal symbols all have the same minor type, though.
  55. ** This macros defines the minor type for terminal
  56. ** symbols.
  57. ** YYMINORTYPE is the data type used for all minor types.
  58. ** This is typically a union of many types, one of
  59. ** which is ParseTOKENTYPE. The entry in the union
  60. ** for terminal symbols is called "yy0".
  61. ** YYSTACKDEPTH is the maximum depth of the parser's stack. If
  62. ** zero the stack is dynamically sized using realloc()
  63. ** ParseARG_SDECL A static variable declaration for the %extra_argument
  64. ** ParseARG_PDECL A parameter declaration for the %extra_argument
  65. ** ParseARG_PARAM Code to pass %extra_argument as a subroutine parameter
  66. ** ParseARG_STORE Code to store %extra_argument into yypParser
  67. ** ParseARG_FETCH Code to extract %extra_argument from yypParser
  68. ** ParseCTX_* As ParseARG_ except for %extra_context
  69. ** YYERRORSYMBOL is the code number of the error symbol. If not
  70. ** defined, then do no error processing.
  71. ** YYNSTATE the combined number of states.
  72. ** YYNRULE the number of rules in the grammar
  73. ** YYNTOKEN Number of terminal symbols
  74. ** YY_MAX_SHIFT Maximum value for shift actions
  75. ** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
  76. ** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
  77. ** YY_ERROR_ACTION The yy_action[] code for syntax error
  78. ** YY_ACCEPT_ACTION The yy_action[] code for accept
  79. ** YY_NO_ACTION The yy_action[] code for no-op
  80. ** YY_MIN_REDUCE Minimum value for reduce actions
  81. ** YY_MAX_REDUCE Maximum value for reduce actions
  82. */
  83. #ifndef INTERFACE
  84. # define INTERFACE 1
  85. #endif
  86. /************* Begin control #defines *****************************************/
  87. %%
  88. /************* End control #defines *******************************************/
  89. #define YY_NLOOKAHEAD ((int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])))
  90. /* Define the yytestcase() macro to be a no-op if is not already defined
  91. ** otherwise.
  92. **
  93. ** Applications can choose to define yytestcase() in the %include section
  94. ** to a macro that can assist in verifying code coverage. For production
  95. ** code the yytestcase() macro should be turned off. But it is useful
  96. ** for testing.
  97. */
  98. #ifndef yytestcase
  99. # define yytestcase(X)
  100. #endif
  101. /* Next are the tables used to determine what action to take based on the
  102. ** current state and lookahead token. These tables are used to implement
  103. ** functions that take a state number and lookahead value and return an
  104. ** action integer.
  105. **
  106. ** Suppose the action integer is N. Then the action is determined as
  107. ** follows
  108. **
  109. ** 0 <= N <= YY_MAX_SHIFT Shift N. That is, push the lookahead
  110. ** token onto the stack and goto state N.
  111. **
  112. ** N between YY_MIN_SHIFTREDUCE Shift to an arbitrary state then
  113. ** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE.
  114. **
  115. ** N == YY_ERROR_ACTION A syntax error has occurred.
  116. **
  117. ** N == YY_ACCEPT_ACTION The parser accepts its input.
  118. **
  119. ** N == YY_NO_ACTION No such action. Denotes unused
  120. ** slots in the yy_action[] table.
  121. **
  122. ** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE
  123. ** and YY_MAX_REDUCE
  124. **
  125. ** The action table is constructed as a single large table named yy_action[].
  126. ** Given state S and lookahead X, the action is computed as either:
  127. **
  128. ** (A) N = yy_action[ yy_shift_ofst[S] + X ]
  129. ** (B) N = yy_default[S]
  130. **
  131. ** The (A) formula is preferred. The B formula is used instead if
  132. ** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X.
  133. **
  134. ** The formulas above are for computing the action when the lookahead is
  135. ** a terminal symbol. If the lookahead is a non-terminal (as occurs after
  136. ** a reduce action) then the yy_reduce_ofst[] array is used in place of
  137. ** the yy_shift_ofst[] array.
  138. **
  139. ** The following are the tables generated in this section:
  140. **
  141. ** yy_action[] A single table containing all actions.
  142. ** yy_lookahead[] A table containing the lookahead for each entry in
  143. ** yy_action. Used to detect hash collisions.
  144. ** yy_shift_ofst[] For each state, the offset into yy_action for
  145. ** shifting terminals.
  146. ** yy_reduce_ofst[] For each state, the offset into yy_action for
  147. ** shifting non-terminals after a reduce.
  148. ** yy_default[] Default action for each state.
  149. **
  150. *********** Begin parsing tables **********************************************/
  151. %%
  152. /********** End of lemon-generated parsing tables *****************************/
  153. /* The next table maps tokens (terminal symbols) into fallback tokens.
  154. ** If a construct like the following:
  155. **
  156. ** %fallback ID X Y Z.
  157. **
  158. ** appears in the grammar, then ID becomes a fallback token for X, Y,
  159. ** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
  160. ** but it does not parse, the type of the token is changed to ID and
  161. ** the parse is retried before an error is thrown.
  162. **
  163. ** This feature can be used, for example, to cause some keywords in a language
  164. ** to revert to identifiers if they keyword does not apply in the context where
  165. ** it appears.
  166. */
  167. #ifdef YYFALLBACK
  168. static const YYCODETYPE yyFallback[] = {
  169. %%
  170. };
  171. #endif /* YYFALLBACK */
  172. /* The following structure represents a single element of the
  173. ** parser's stack. Information stored includes:
  174. **
  175. ** + The state number for the parser at this level of the stack.
  176. **
  177. ** + The value of the token stored at this level of the stack.
  178. ** (In other words, the "major" token.)
  179. **
  180. ** + The semantic value stored at this level of the stack. This is
  181. ** the information used by the action routines in the grammar.
  182. ** It is sometimes called the "minor" token.
  183. **
  184. ** After the "shift" half of a SHIFTREDUCE action, the stateno field
  185. ** actually contains the reduce action for the second half of the
  186. ** SHIFTREDUCE.
  187. */
  188. struct yyStackEntry {
  189. YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */
  190. YYCODETYPE major; /* The major token value. This is the code
  191. ** number for the token at this stack level */
  192. YYMINORTYPE minor; /* The user-supplied minor token value. This
  193. ** is the value of the token */
  194. };
  195. typedef struct yyStackEntry yyStackEntry;
  196. /* The state of the parser is completely contained in an instance of
  197. ** the following structure */
  198. struct yyParser {
  199. yyStackEntry *yytos; /* Pointer to top element of the stack */
  200. #ifdef YYTRACKMAXSTACKDEPTH
  201. int yyhwm; /* High-water mark of the stack */
  202. #endif
  203. #ifndef YYNOERRORRECOVERY
  204. int yyerrcnt; /* Shifts left before out of the error */
  205. #endif
  206. ParseARG_SDECL /* A place to hold %extra_argument */
  207. ParseCTX_SDECL /* A place to hold %extra_context */
  208. #if YYSTACKDEPTH<=0
  209. int yystksz; /* Current side of the stack */
  210. yyStackEntry *yystack; /* The parser's stack */
  211. yyStackEntry yystk0; /* First stack entry */
  212. #else
  213. yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */
  214. yyStackEntry *yystackEnd; /* Last entry in the stack */
  215. #endif
  216. };
  217. typedef struct yyParser yyParser;
  218. #include <assert.h>
  219. #ifndef NDEBUG
  220. #include <stdio.h>
  221. static FILE *yyTraceFILE = 0;
  222. static char *yyTracePrompt = 0;
  223. #endif /* NDEBUG */
  224. #ifndef NDEBUG
  225. /*
  226. ** Turn parser tracing on by giving a stream to which to write the trace
  227. ** and a prompt to preface each trace message. Tracing is turned off
  228. ** by making either argument NULL
  229. **
  230. ** Inputs:
  231. ** <ul>
  232. ** <li> A FILE* to which trace output should be written.
  233. ** If NULL, then tracing is turned off.
  234. ** <li> A prefix string written at the beginning of every
  235. ** line of trace output. If NULL, then tracing is
  236. ** turned off.
  237. ** </ul>
  238. **
  239. ** Outputs:
  240. ** None.
  241. */
  242. void ParseTrace(FILE *TraceFILE, char *zTracePrompt){
  243. yyTraceFILE = TraceFILE;
  244. yyTracePrompt = zTracePrompt;
  245. if( yyTraceFILE==0 ) yyTracePrompt = 0;
  246. else if( yyTracePrompt==0 ) yyTraceFILE = 0;
  247. }
  248. #endif /* NDEBUG */
  249. #if defined(YYCOVERAGE) || !defined(NDEBUG)
  250. /* For tracing shifts, the names of all terminals and nonterminals
  251. ** are required. The following table supplies these names */
  252. static const char *const yyTokenName[] = {
  253. %%
  254. };
  255. #endif /* defined(YYCOVERAGE) || !defined(NDEBUG) */
  256. #ifndef NDEBUG
  257. /* For tracing reduce actions, the names of all rules are required.
  258. */
  259. static const char *const yyRuleName[] = {
  260. %%
  261. };
  262. #endif /* NDEBUG */
  263. #if YYSTACKDEPTH<=0
  264. /*
  265. ** Try to increase the size of the parser stack. Return the number
  266. ** of errors. Return 0 on success.
  267. */
  268. static int yyGrowStack(yyParser *p){
  269. int newSize;
  270. int idx;
  271. yyStackEntry *pNew;
  272. newSize = p->yystksz*2 + 100;
  273. idx = p->yytos ? (int)(p->yytos - p->yystack) : 0;
  274. if( p->yystack==&p->yystk0 ){
  275. pNew = malloc(newSize*sizeof(pNew[0]));
  276. if( pNew ) pNew[0] = p->yystk0;
  277. }else{
  278. pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
  279. }
  280. if( pNew ){
  281. p->yystack = pNew;
  282. p->yytos = &p->yystack[idx];
  283. #ifndef NDEBUG
  284. if( yyTraceFILE ){
  285. fprintf(yyTraceFILE,"%sStack grows from %d to %d entries.\n",
  286. yyTracePrompt, p->yystksz, newSize);
  287. }
  288. #endif
  289. p->yystksz = newSize;
  290. }
  291. return pNew==0;
  292. }
  293. #endif
  294. /* Datatype of the argument to the memory allocated passed as the
  295. ** second argument to ParseAlloc() below. This can be changed by
  296. ** putting an appropriate #define in the %include section of the input
  297. ** grammar.
  298. */
  299. #ifndef YYMALLOCARGTYPE
  300. # define YYMALLOCARGTYPE size_t
  301. #endif
  302. /* Initialize a new parser that has already been allocated.
  303. */
  304. void ParseInit(void *yypRawParser ParseCTX_PDECL){
  305. yyParser *yypParser = (yyParser*)yypRawParser;
  306. ParseCTX_STORE
  307. #ifdef YYTRACKMAXSTACKDEPTH
  308. yypParser->yyhwm = 0;
  309. #endif
  310. #if YYSTACKDEPTH<=0
  311. yypParser->yytos = NULL;
  312. yypParser->yystack = NULL;
  313. yypParser->yystksz = 0;
  314. if( yyGrowStack(yypParser) ){
  315. yypParser->yystack = &yypParser->yystk0;
  316. yypParser->yystksz = 1;
  317. }
  318. #endif
  319. #ifndef YYNOERRORRECOVERY
  320. yypParser->yyerrcnt = -1;
  321. #endif
  322. yypParser->yytos = yypParser->yystack;
  323. yypParser->yystack[0].stateno = 0;
  324. yypParser->yystack[0].major = 0;
  325. #if YYSTACKDEPTH>0
  326. yypParser->yystackEnd = &yypParser->yystack[YYSTACKDEPTH-1];
  327. #endif
  328. }
  329. #ifndef Parse_ENGINEALWAYSONSTACK
  330. /*
  331. ** This function allocates a new parser.
  332. ** The only argument is a pointer to a function which works like
  333. ** malloc.
  334. **
  335. ** Inputs:
  336. ** A pointer to the function used to allocate memory.
  337. **
  338. ** Outputs:
  339. ** A pointer to a parser. This pointer is used in subsequent calls
  340. ** to Parse and ParseFree.
  341. */
  342. void *ParseAlloc(void *(*mallocProc)(YYMALLOCARGTYPE) ParseCTX_PDECL){
  343. yyParser *yypParser;
  344. yypParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) );
  345. if( yypParser ){
  346. ParseCTX_STORE
  347. ParseInit(yypParser ParseCTX_PARAM);
  348. }
  349. return (void*)yypParser;
  350. }
  351. #endif /* Parse_ENGINEALWAYSONSTACK */
  352. /* The following function deletes the "minor type" or semantic value
  353. ** associated with a symbol. The symbol can be either a terminal
  354. ** or nonterminal. "yymajor" is the symbol code, and "yypminor" is
  355. ** a pointer to the value to be deleted. The code used to do the
  356. ** deletions is derived from the %destructor and/or %token_destructor
  357. ** directives of the input grammar.
  358. */
  359. static void yy_destructor(
  360. yyParser *yypParser, /* The parser */
  361. YYCODETYPE yymajor, /* Type code for object to destroy */
  362. YYMINORTYPE *yypminor /* The object to be destroyed */
  363. ){
  364. ParseARG_FETCH
  365. ParseCTX_FETCH
  366. switch( yymajor ){
  367. /* Here is inserted the actions which take place when a
  368. ** terminal or non-terminal is destroyed. This can happen
  369. ** when the symbol is popped from the stack during a
  370. ** reduce or during error processing or when a parser is
  371. ** being destroyed before it is finished parsing.
  372. **
  373. ** Note: during a reduce, the only symbols destroyed are those
  374. ** which appear on the RHS of the rule, but which are *not* used
  375. ** inside the C code.
  376. */
  377. /********* Begin destructor definitions ***************************************/
  378. %%
  379. /********* End destructor definitions *****************************************/
  380. default: break; /* If no destructor action specified: do nothing */
  381. }
  382. }
  383. /*
  384. ** Pop the parser's stack once.
  385. **
  386. ** If there is a destructor routine associated with the token which
  387. ** is popped from the stack, then call it.
  388. */
  389. static void yy_pop_parser_stack(yyParser *pParser){
  390. yyStackEntry *yytos;
  391. assert( pParser->yytos!=0 );
  392. assert( pParser->yytos > pParser->yystack );
  393. yytos = pParser->yytos--;
  394. #ifndef NDEBUG
  395. if( yyTraceFILE ){
  396. fprintf(yyTraceFILE,"%sPopping %s\n",
  397. yyTracePrompt,
  398. yyTokenName[yytos->major]);
  399. }
  400. #endif
  401. yy_destructor(pParser, yytos->major, &yytos->minor);
  402. }
  403. /*
  404. ** Clear all secondary memory allocations from the parser
  405. */
  406. void ParseFinalize(void *p){
  407. yyParser *pParser = (yyParser*)p;
  408. while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
  409. #if YYSTACKDEPTH<=0
  410. if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
  411. #endif
  412. }
  413. #ifndef Parse_ENGINEALWAYSONSTACK
  414. /*
  415. ** Deallocate and destroy a parser. Destructors are called for
  416. ** all stack elements before shutting the parser down.
  417. **
  418. ** If the YYPARSEFREENEVERNULL macro exists (for example because it
  419. ** is defined in a %include section of the input grammar) then it is
  420. ** assumed that the input pointer is never NULL.
  421. */
  422. void ParseFree(
  423. void *p, /* The parser to be deleted */
  424. void (*freeProc)(void*) /* Function used to reclaim memory */
  425. ){
  426. #ifndef YYPARSEFREENEVERNULL
  427. if( p==0 ) return;
  428. #endif
  429. ParseFinalize(p);
  430. (*freeProc)(p);
  431. }
  432. #endif /* Parse_ENGINEALWAYSONSTACK */
  433. /*
  434. ** Return the peak depth of the stack for a parser.
  435. */
  436. #ifdef YYTRACKMAXSTACKDEPTH
  437. int ParseStackPeak(void *p){
  438. yyParser *pParser = (yyParser*)p;
  439. return pParser->yyhwm;
  440. }
  441. #endif
  442. /* This array of booleans keeps track of the parser statement
  443. ** coverage. The element yycoverage[X][Y] is set when the parser
  444. ** is in state X and has a lookahead token Y. In a well-tested
  445. ** systems, every element of this matrix should end up being set.
  446. */
  447. #if defined(YYCOVERAGE)
  448. static unsigned char yycoverage[YYNSTATE][YYNTOKEN];
  449. #endif
  450. /*
  451. ** Write into out a description of every state/lookahead combination that
  452. **
  453. ** (1) has not been used by the parser, and
  454. ** (2) is not a syntax error.
  455. **
  456. ** Return the number of missed state/lookahead combinations.
  457. */
  458. #if defined(YYCOVERAGE)
  459. int ParseCoverage(FILE *out){
  460. int stateno, iLookAhead, i;
  461. int nMissed = 0;
  462. for(stateno=0; stateno<YYNSTATE; stateno++){
  463. i = yy_shift_ofst[stateno];
  464. for(iLookAhead=0; iLookAhead<YYNTOKEN; iLookAhead++){
  465. if( yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
  466. if( yycoverage[stateno][iLookAhead]==0 ) nMissed++;
  467. if( out ){
  468. fprintf(out,"State %d lookahead %s %s\n", stateno,
  469. yyTokenName[iLookAhead],
  470. yycoverage[stateno][iLookAhead] ? "ok" : "missed");
  471. }
  472. }
  473. }
  474. return nMissed;
  475. }
  476. #endif
  477. /*
  478. ** Find the appropriate action for a parser given the terminal
  479. ** look-ahead token iLookAhead.
  480. */
  481. static YYACTIONTYPE yy_find_shift_action(
  482. YYCODETYPE iLookAhead, /* The look-ahead token */
  483. YYACTIONTYPE stateno /* Current state number */
  484. ){
  485. int i;
  486. if( stateno>YY_MAX_SHIFT ) return stateno;
  487. assert( stateno <= YY_SHIFT_COUNT );
  488. #if defined(YYCOVERAGE)
  489. yycoverage[stateno][iLookAhead] = 1;
  490. #endif
  491. do{
  492. i = yy_shift_ofst[stateno];
  493. assert( i>=0 );
  494. assert( i<=YY_ACTTAB_COUNT );
  495. assert( i+YYNTOKEN<=(int)YY_NLOOKAHEAD );
  496. assert( iLookAhead!=YYNOCODE );
  497. assert( iLookAhead < YYNTOKEN );
  498. i += iLookAhead;
  499. assert( i<(int)YY_NLOOKAHEAD );
  500. if( yy_lookahead[i]!=iLookAhead ){
  501. #ifdef YYFALLBACK
  502. YYCODETYPE iFallback; /* Fallback token */
  503. assert( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0]) );
  504. iFallback = yyFallback[iLookAhead];
  505. if( iFallback!=0 ){
  506. #ifndef NDEBUG
  507. if( yyTraceFILE ){
  508. fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
  509. yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
  510. }
  511. #endif
  512. assert( yyFallback[iFallback]==0 ); /* Fallback loop must terminate */
  513. iLookAhead = iFallback;
  514. continue;
  515. }
  516. #endif
  517. #ifdef YYWILDCARD
  518. {
  519. int j = i - iLookAhead + YYWILDCARD;
  520. assert( j<(int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])) );
  521. if( yy_lookahead[j]==YYWILDCARD && iLookAhead>0 ){
  522. #ifndef NDEBUG
  523. if( yyTraceFILE ){
  524. fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
  525. yyTracePrompt, yyTokenName[iLookAhead],
  526. yyTokenName[YYWILDCARD]);
  527. }
  528. #endif /* NDEBUG */
  529. return yy_action[j];
  530. }
  531. }
  532. #endif /* YYWILDCARD */
  533. return yy_default[stateno];
  534. }else{
  535. assert( i>=0 && i<(int)(sizeof(yy_action)/sizeof(yy_action[0])) );
  536. return yy_action[i];
  537. }
  538. }while(1);
  539. }
  540. /*
  541. ** Find the appropriate action for a parser given the non-terminal
  542. ** look-ahead token iLookAhead.
  543. */
  544. static YYACTIONTYPE yy_find_reduce_action(
  545. YYACTIONTYPE stateno, /* Current state number */
  546. YYCODETYPE iLookAhead /* The look-ahead token */
  547. ){
  548. int i;
  549. #ifdef YYERRORSYMBOL
  550. if( stateno>YY_REDUCE_COUNT ){
  551. return yy_default[stateno];
  552. }
  553. #else
  554. assert( stateno<=YY_REDUCE_COUNT );
  555. #endif
  556. i = yy_reduce_ofst[stateno];
  557. assert( iLookAhead!=YYNOCODE );
  558. i += iLookAhead;
  559. #ifdef YYERRORSYMBOL
  560. if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
  561. return yy_default[stateno];
  562. }
  563. #else
  564. assert( i>=0 && i<YY_ACTTAB_COUNT );
  565. assert( yy_lookahead[i]==iLookAhead );
  566. #endif
  567. return yy_action[i];
  568. }
  569. /*
  570. ** The following routine is called if the stack overflows.
  571. */
  572. static void yyStackOverflow(yyParser *yypParser){
  573. ParseARG_FETCH
  574. ParseCTX_FETCH
  575. #ifndef NDEBUG
  576. if( yyTraceFILE ){
  577. fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
  578. }
  579. #endif
  580. while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
  581. /* Here code is inserted which will execute if the parser
  582. ** stack every overflows */
  583. /******** Begin %stack_overflow code ******************************************/
  584. %%
  585. /******** End %stack_overflow code ********************************************/
  586. ParseARG_STORE /* Suppress warning about unused %extra_argument var */
  587. ParseCTX_STORE
  588. }
  589. /*
  590. ** Print tracing information for a SHIFT action
  591. */
  592. #ifndef NDEBUG
  593. static void yyTraceShift(yyParser *yypParser, int yyNewState, const char *zTag){
  594. if( yyTraceFILE ){
  595. if( yyNewState<YYNSTATE ){
  596. fprintf(yyTraceFILE,"%s%s '%s', go to state %d\n",
  597. yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
  598. yyNewState);
  599. }else{
  600. fprintf(yyTraceFILE,"%s%s '%s', pending reduce %d\n",
  601. yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
  602. yyNewState - YY_MIN_REDUCE);
  603. }
  604. }
  605. }
  606. #else
  607. # define yyTraceShift(X,Y,Z)
  608. #endif
  609. /*
  610. ** Perform a shift action.
  611. */
  612. static void yy_shift(
  613. yyParser *yypParser, /* The parser to be shifted */
  614. YYACTIONTYPE yyNewState, /* The new state to shift in */
  615. YYCODETYPE yyMajor, /* The major token to shift in */
  616. ParseTOKENTYPE yyMinor /* The minor token to shift in */
  617. ){
  618. yyStackEntry *yytos;
  619. yypParser->yytos++;
  620. #ifdef YYTRACKMAXSTACKDEPTH
  621. if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
  622. yypParser->yyhwm++;
  623. assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) );
  624. }
  625. #endif
  626. #if YYSTACKDEPTH>0
  627. if( yypParser->yytos>yypParser->yystackEnd ){
  628. yypParser->yytos--;
  629. yyStackOverflow(yypParser);
  630. return;
  631. }
  632. #else
  633. if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){
  634. if( yyGrowStack(yypParser) ){
  635. yypParser->yytos--;
  636. yyStackOverflow(yypParser);
  637. return;
  638. }
  639. }
  640. #endif
  641. if( yyNewState > YY_MAX_SHIFT ){
  642. yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
  643. }
  644. yytos = yypParser->yytos;
  645. yytos->stateno = yyNewState;
  646. yytos->major = yyMajor;
  647. yytos->minor.yy0 = yyMinor;
  648. yyTraceShift(yypParser, yyNewState, "Shift");
  649. }
  650. /* For rule J, yyRuleInfoLhs[J] contains the symbol on the left-hand side
  651. ** of that rule */
  652. static const YYCODETYPE yyRuleInfoLhs[] = {
  653. %%
  654. };
  655. /* For rule J, yyRuleInfoNRhs[J] contains the negative of the number
  656. ** of symbols on the right-hand side of that rule. */
  657. static const signed char yyRuleInfoNRhs[] = {
  658. %%
  659. };
  660. static void yy_accept(yyParser*); /* Forward Declaration */
  661. /*
  662. ** Perform a reduce action and the shift that must immediately
  663. ** follow the reduce.
  664. **
  665. ** The yyLookahead and yyLookaheadToken parameters provide reduce actions
  666. ** access to the lookahead token (if any). The yyLookahead will be YYNOCODE
  667. ** if the lookahead token has already been consumed. As this procedure is
  668. ** only called from one place, optimizing compilers will in-line it, which
  669. ** means that the extra parameters have no performance impact.
  670. */
  671. static YYACTIONTYPE yy_reduce(
  672. yyParser *yypParser, /* The parser */
  673. unsigned int yyruleno, /* Number of the rule by which to reduce */
  674. int yyLookahead, /* Lookahead token, or YYNOCODE if none */
  675. ParseTOKENTYPE yyLookaheadToken /* Value of the lookahead token */
  676. ParseCTX_PDECL /* %extra_context */
  677. ){
  678. int yygoto; /* The next state */
  679. YYACTIONTYPE yyact; /* The next action */
  680. yyStackEntry *yymsp; /* The top of the parser's stack */
  681. int yysize; /* Amount to pop the stack */
  682. ParseARG_FETCH
  683. (void)yyLookahead;
  684. (void)yyLookaheadToken;
  685. yymsp = yypParser->yytos;
  686. switch( yyruleno ){
  687. /* Beginning here are the reduction cases. A typical example
  688. ** follows:
  689. ** case 0:
  690. ** #line <lineno> <grammarfile>
  691. ** { ... } // User supplied code
  692. ** #line <lineno> <thisfile>
  693. ** break;
  694. */
  695. /********** Begin reduce actions **********************************************/
  696. %%
  697. /********** End reduce actions ************************************************/
  698. };
  699. assert( yyruleno<sizeof(yyRuleInfoLhs)/sizeof(yyRuleInfoLhs[0]) );
  700. yygoto = yyRuleInfoLhs[yyruleno];
  701. yysize = yyRuleInfoNRhs[yyruleno];
  702. yyact = yy_find_reduce_action(yymsp[yysize].stateno,(YYCODETYPE)yygoto);
  703. /* There are no SHIFTREDUCE actions on nonterminals because the table
  704. ** generator has simplified them to pure REDUCE actions. */
  705. assert( !(yyact>YY_MAX_SHIFT && yyact<=YY_MAX_SHIFTREDUCE) );
  706. /* It is not possible for a REDUCE to be followed by an error */
  707. assert( yyact!=YY_ERROR_ACTION );
  708. yymsp += yysize+1;
  709. yypParser->yytos = yymsp;
  710. yymsp->stateno = (YYACTIONTYPE)yyact;
  711. yymsp->major = (YYCODETYPE)yygoto;
  712. yyTraceShift(yypParser, yyact, "... then shift");
  713. return yyact;
  714. }
  715. /*
  716. ** The following code executes when the parse fails
  717. */
  718. #ifndef YYNOERRORRECOVERY
  719. static void yy_parse_failed(
  720. yyParser *yypParser /* The parser */
  721. ){
  722. ParseARG_FETCH
  723. ParseCTX_FETCH
  724. #ifndef NDEBUG
  725. if( yyTraceFILE ){
  726. fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
  727. }
  728. #endif
  729. while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
  730. /* Here code is inserted which will be executed whenever the
  731. ** parser fails */
  732. /************ Begin %parse_failure code ***************************************/
  733. %%
  734. /************ End %parse_failure code *****************************************/
  735. ParseARG_STORE /* Suppress warning about unused %extra_argument variable */
  736. ParseCTX_STORE
  737. }
  738. #endif /* YYNOERRORRECOVERY */
  739. /*
  740. ** The following code executes when a syntax error first occurs.
  741. */
  742. static void yy_syntax_error(
  743. yyParser *yypParser, /* The parser */
  744. int yymajor, /* The major type of the error token */
  745. ParseTOKENTYPE yyminor /* The minor type of the error token */
  746. ){
  747. ParseARG_FETCH
  748. ParseCTX_FETCH
  749. #define TOKEN yyminor
  750. /************ Begin %syntax_error code ****************************************/
  751. %%
  752. /************ End %syntax_error code ******************************************/
  753. ParseARG_STORE /* Suppress warning about unused %extra_argument variable */
  754. ParseCTX_STORE
  755. }
  756. /*
  757. ** The following is executed when the parser accepts
  758. */
  759. static void yy_accept(
  760. yyParser *yypParser /* The parser */
  761. ){
  762. ParseARG_FETCH
  763. ParseCTX_FETCH
  764. #ifndef NDEBUG
  765. if( yyTraceFILE ){
  766. fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
  767. }
  768. #endif
  769. #ifndef YYNOERRORRECOVERY
  770. yypParser->yyerrcnt = -1;
  771. #endif
  772. assert( yypParser->yytos==yypParser->yystack );
  773. /* Here code is inserted which will be executed whenever the
  774. ** parser accepts */
  775. /*********** Begin %parse_accept code *****************************************/
  776. %%
  777. /*********** End %parse_accept code *******************************************/
  778. ParseARG_STORE /* Suppress warning about unused %extra_argument variable */
  779. ParseCTX_STORE
  780. }
  781. /* The main parser program.
  782. ** The first argument is a pointer to a structure obtained from
  783. ** "ParseAlloc" which describes the current state of the parser.
  784. ** The second argument is the major token number. The third is
  785. ** the minor token. The fourth optional argument is whatever the
  786. ** user wants (and specified in the grammar) and is available for
  787. ** use by the action routines.
  788. **
  789. ** Inputs:
  790. ** <ul>
  791. ** <li> A pointer to the parser (an opaque structure.)
  792. ** <li> The major token number.
  793. ** <li> The minor token number.
  794. ** <li> An option argument of a grammar-specified type.
  795. ** </ul>
  796. **
  797. ** Outputs:
  798. ** None.
  799. */
  800. void Parse(
  801. void *yyp, /* The parser */
  802. int yymajor, /* The major token code number */
  803. ParseTOKENTYPE yyminor /* The value for the token */
  804. ParseARG_PDECL /* Optional %extra_argument parameter */
  805. ){
  806. YYMINORTYPE yyminorunion;
  807. YYACTIONTYPE yyact; /* The parser action. */
  808. #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
  809. int yyendofinput; /* True if we are at the end of input */
  810. #endif
  811. #ifdef YYERRORSYMBOL
  812. int yyerrorhit = 0; /* True if yymajor has invoked an error */
  813. #endif
  814. yyParser *yypParser = (yyParser*)yyp; /* The parser */
  815. ParseCTX_FETCH
  816. ParseARG_STORE
  817. assert( yypParser->yytos!=0 );
  818. #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
  819. yyendofinput = (yymajor==0);
  820. #endif
  821. yyact = yypParser->yytos->stateno;
  822. #ifndef NDEBUG
  823. if( yyTraceFILE ){
  824. if( yyact < YY_MIN_REDUCE ){
  825. fprintf(yyTraceFILE,"%sInput '%s' in state %d\n",
  826. yyTracePrompt,yyTokenName[yymajor],yyact);
  827. }else{
  828. fprintf(yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
  829. yyTracePrompt,yyTokenName[yymajor],yyact-YY_MIN_REDUCE);
  830. }
  831. }
  832. #endif
  833. while(1){ /* Exit by "break" */
  834. assert( yypParser->yytos>=yypParser->yystack );
  835. assert( yyact==yypParser->yytos->stateno );
  836. yyact = yy_find_shift_action((YYCODETYPE)yymajor,yyact);
  837. if( yyact >= YY_MIN_REDUCE ){
  838. unsigned int yyruleno = yyact - YY_MIN_REDUCE; /* Reduce by this rule */
  839. #ifndef NDEBUG
  840. assert( yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) );
  841. if( yyTraceFILE ){
  842. int yysize = yyRuleInfoNRhs[yyruleno];
  843. if( yysize ){
  844. fprintf(yyTraceFILE, "%sReduce %d [%s]%s, pop back to state %d.\n",
  845. yyTracePrompt,
  846. yyruleno, yyRuleName[yyruleno],
  847. yyruleno<YYNRULE_WITH_ACTION ? "" : " without external action",
  848. yypParser->yytos[yysize].stateno);
  849. }else{
  850. fprintf(yyTraceFILE, "%sReduce %d [%s]%s.\n",
  851. yyTracePrompt, yyruleno, yyRuleName[yyruleno],
  852. yyruleno<YYNRULE_WITH_ACTION ? "" : " without external action");
  853. }
  854. }
  855. #endif /* NDEBUG */
  856. /* Check that the stack is large enough to grow by a single entry
  857. ** if the RHS of the rule is empty. This ensures that there is room
  858. ** enough on the stack to push the LHS value */
  859. if( yyRuleInfoNRhs[yyruleno]==0 ){
  860. #ifdef YYTRACKMAXSTACKDEPTH
  861. if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
  862. yypParser->yyhwm++;
  863. assert( yypParser->yyhwm ==
  864. (int)(yypParser->yytos - yypParser->yystack));
  865. }
  866. #endif
  867. #if YYSTACKDEPTH>0
  868. if( yypParser->yytos>=yypParser->yystackEnd ){
  869. yyStackOverflow(yypParser);
  870. break;
  871. }
  872. #else
  873. if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz-1] ){
  874. if( yyGrowStack(yypParser) ){
  875. yyStackOverflow(yypParser);
  876. break;
  877. }
  878. }
  879. #endif
  880. }
  881. yyact = yy_reduce(yypParser,yyruleno,yymajor,yyminor ParseCTX_PARAM);
  882. }else if( yyact <= YY_MAX_SHIFTREDUCE ){
  883. yy_shift(yypParser,yyact,(YYCODETYPE)yymajor,yyminor);
  884. #ifndef YYNOERRORRECOVERY
  885. yypParser->yyerrcnt--;
  886. #endif
  887. break;
  888. }else if( yyact==YY_ACCEPT_ACTION ){
  889. yypParser->yytos--;
  890. yy_accept(yypParser);
  891. return;
  892. }else{
  893. assert( yyact == YY_ERROR_ACTION );
  894. yyminorunion.yy0 = yyminor;
  895. #ifdef YYERRORSYMBOL
  896. int yymx;
  897. #endif
  898. #ifndef NDEBUG
  899. if( yyTraceFILE ){
  900. fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
  901. }
  902. #endif
  903. #ifdef YYERRORSYMBOL
  904. /* A syntax error has occurred.
  905. ** The response to an error depends upon whether or not the
  906. ** grammar defines an error token "ERROR".
  907. **
  908. ** This is what we do if the grammar does define ERROR:
  909. **
  910. ** * Call the %syntax_error function.
  911. **
  912. ** * Begin popping the stack until we enter a state where
  913. ** it is legal to shift the error symbol, then shift
  914. ** the error symbol.
  915. **
  916. ** * Set the error count to three.
  917. **
  918. ** * Begin accepting and shifting new tokens. No new error
  919. ** processing will occur until three tokens have been
  920. ** shifted successfully.
  921. **
  922. */
  923. if( yypParser->yyerrcnt<0 ){
  924. yy_syntax_error(yypParser,yymajor,yyminor);
  925. }
  926. yymx = yypParser->yytos->major;
  927. if( yymx==YYERRORSYMBOL || yyerrorhit ){
  928. #ifndef NDEBUG
  929. if( yyTraceFILE ){
  930. fprintf(yyTraceFILE,"%sDiscard input token %s\n",
  931. yyTracePrompt,yyTokenName[yymajor]);
  932. }
  933. #endif
  934. yy_destructor(yypParser, (YYCODETYPE)yymajor, &yyminorunion);
  935. yymajor = YYNOCODE;
  936. }else{
  937. while( yypParser->yytos > yypParser->yystack ){
  938. yyact = yy_find_reduce_action(yypParser->yytos->stateno,
  939. YYERRORSYMBOL);
  940. if( yyact<=YY_MAX_SHIFTREDUCE ) break;
  941. yy_pop_parser_stack(yypParser);
  942. }
  943. if( yypParser->yytos <= yypParser->yystack || yymajor==0 ){
  944. yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
  945. yy_parse_failed(yypParser);
  946. #ifndef YYNOERRORRECOVERY
  947. yypParser->yyerrcnt = -1;
  948. #endif
  949. yymajor = YYNOCODE;
  950. }else if( yymx!=YYERRORSYMBOL ){
  951. yy_shift(yypParser,yyact,YYERRORSYMBOL,yyminor);
  952. }
  953. }
  954. yypParser->yyerrcnt = 3;
  955. yyerrorhit = 1;
  956. if( yymajor==YYNOCODE ) break;
  957. yyact = yypParser->yytos->stateno;
  958. #elif defined(YYNOERRORRECOVERY)
  959. /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
  960. ** do any kind of error recovery. Instead, simply invoke the syntax
  961. ** error routine and continue going as if nothing had happened.
  962. **
  963. ** Applications can set this macro (for example inside %include) if
  964. ** they intend to abandon the parse upon the first syntax error seen.
  965. */
  966. yy_syntax_error(yypParser,yymajor, yyminor);
  967. yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
  968. break;
  969. #else /* YYERRORSYMBOL is not defined */
  970. /* This is what we do if the grammar does not define ERROR:
  971. **
  972. ** * Report an error message, and throw away the input token.
  973. **
  974. ** * If the input token is $, then fail the parse.
  975. **
  976. ** As before, subsequent error messages are suppressed until
  977. ** three input tokens have been successfully shifted.
  978. */
  979. if( yypParser->yyerrcnt<=0 ){
  980. yy_syntax_error(yypParser,yymajor, yyminor);
  981. }
  982. yypParser->yyerrcnt = 3;
  983. yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
  984. if( yyendofinput ){
  985. yy_parse_failed(yypParser);
  986. #ifndef YYNOERRORRECOVERY
  987. yypParser->yyerrcnt = -1;
  988. #endif
  989. }
  990. break;
  991. #endif
  992. }
  993. }
  994. #ifndef NDEBUG
  995. if( yyTraceFILE ){
  996. yyStackEntry *i;
  997. char cDiv = '[';
  998. fprintf(yyTraceFILE,"%sReturn. Stack=",yyTracePrompt);
  999. for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){
  1000. fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]);
  1001. cDiv = ' ';
  1002. }
  1003. fprintf(yyTraceFILE,"]\n");
  1004. }
  1005. #endif
  1006. return;
  1007. }
  1008. /*
  1009. ** Return the fallback token corresponding to canonical token iToken, or
  1010. ** 0 if iToken has no fallback.
  1011. */
  1012. int ParseFallback(int iToken){
  1013. #ifdef YYFALLBACK
  1014. assert( iToken<(int)(sizeof(yyFallback)/sizeof(yyFallback[0])) );
  1015. return yyFallback[iToken];
  1016. #else
  1017. (void)iToken;
  1018. return 0;
  1019. #endif
  1020. }