lempar.c 33 KB

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