Advertisement
Guest User

lempar.c

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