lempar.c 36 KB

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