home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / compcomp / yacc / yyparse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1979-12-31  |  7.8 KB  |  266 lines

  1. /************************************************************************/
  2. /*                yyparse.c                */
  3. /*      parser for yacc output                                          */
  4. /************************************************************************/
  5.  
  6. /************************************************************************/   
  7. /*                              contents                                */    
  8. /*                                                                      */    
  9. /*    yyParse        Master parse routine.                */
  10. /*  * yyA<nnn>        Action functions created from user action code. */
  11. /*                                    */
  12. /* * Local to this file.                                                */  
  13. /*                                                                      */ 
  14. /************************************************************************/ 
  15.     
  16.     
  17. /************************************************************************/ 
  18. /*                              history                                 */ 
  19. /*                                                                      */ 
  20. /* 85Nov22 CrT  Below fouled up YYACCEPT/YYERROR.  Fixed.               */ 
  21. /* 85Nov18 CrT    User action chunks are now placed in functions rather    */
  22. /*              than switch cases.  The functions are accessed by an    */ 
  23. /*              array of function pointers.  This is because many       */ 
  24. /*              compilers have low limits on the size of functions,     */ 
  25. /*              switch statements, and the number of cases in a         */ 
  26. /*              switch statement.  In addition, some compilers implement*/ 
  27. /*              switch statements with a cascade of comparisons rather  */ 
  28. /*        than a jump table.  This required making yypvt global    */
  29. /*        instead of being private to yyParse.            */
  30. /*                                    */
  31. /* 81Aug27 RBD    Modified for use with DECUS LEX             */
  32. /*              Variable "yylval" resides in yylex(), not in yypars();  */ 
  33. /*              Therefore, is defined "extern" here.                    */ 
  34. /*                                                                      */ 
  35. /*              Also, the command line processing for the Decus version */ 
  36. /*              has been changed.  A new switch has been added to allow */ 
  37. /*              specification of the "table" file name(s), and unused   */ 
  38. /*              switch processing removed.                              */ 
  39. /*                               NOTE                                   */ 
  40. /*              This probably won't run on UNIX any more.               */ 
  41. /*                                                                      */ 
  42. /* 7?????? SCJ  Created.                                                */ 
  43. /*                                                                      */ 
  44. /*                              credits                                 */ 
  45. /*      CrT=CrT                                                         */  
  46. /*      RBD=Bob Denny                                                   */  
  47. /*      SCJ=Steven C Johnson.                                           */  
  48. /*      SG =Scott Guthery                                               */  
  49. /************************************************************************/ 
  50.  
  51. # define YYFLAG -1000
  52. # define YYERROR goto yyerrlab
  53. # define YYACCEPT return(0)
  54. # define YYABORT return(1)
  55.  
  56.  
  57. YYSTYPE yyv[YYMAXDEPTH];        /* Runtime stack.              */
  58. int    yydebug   = 0;        /* 1 for debugging.           */
  59. int    yychar      = -1;     /* Current input token number. */
  60. int    yynerrs   = 0;        /* Number of errors.           */
  61. short    yyerrflag = 0;        /* Error recovery flag.        */
  62. YYSTYPE *yypvt;
  63.  
  64. yyparse() {
  65.     short           yys[ YYMAXDEPTH ];
  66.     short           yyj, yym;
  67.     register short     yystate, *yyps, yyn;
  68.     register YYSTYPE   *yypv;
  69.     register short     *yyxi;
  70.  
  71.     yystate   =  0;
  72.     yychar    = -1;
  73.     yynerrs   =  0;
  74.     yyerrflag =  0;
  75.     yyps      = &yys[-1];
  76.     yypv      = &yyv[-1];
  77.  
  78. yystack:    /* Put a state and value onto the stack: */
  79.  
  80.     if (yydebug)   printf( "state %d, char 0%o\n", yystate, yychar );
  81.  
  82.     if (++yyps> &yys[YYMAXDEPTH]) {
  83.     yyerror( "yacc stack overflow" );
  84.     return 1;
  85.     }
  86.     *yyps = yystate;
  87.     ++yypv;
  88.  
  89. #ifdef UNION
  90.    yyunion(yypv, &yyval);
  91. #else
  92.    *yypv = yyval;
  93. #endif
  94.  
  95. yynewstate:
  96.  
  97.     yyn = yypact[ yystate ];
  98.  
  99.     if (yyn <= YYFLAG)     goto yydefault;    /* Simple state. */
  100.  
  101.     if (yychar < 0)   if ((yychar = yylex())  <  0)   yychar = 0;
  102.  
  103.     if ((yyn += yychar) < 0   ||   yyn >= YYLAST)   goto yydefault;
  104.  
  105.     if (yychk[ yyn = yyact[ yyn ] ]   ==   yychar) {
  106.  
  107.     /* Valid shift: */
  108.     yychar = -1;
  109.  
  110. #ifdef UNION
  111.       yyunion(&yyval, &yylval);
  112. #else
  113.       yyval = yylval;
  114. #endif
  115.  
  116.     yystate = yyn;
  117.     if (yyerrflag > 0)   --yyerrflag;
  118.     goto yystack;
  119.     }
  120.  
  121. yydefault:
  122.     /* Default state action: */
  123.  
  124.     if ((yyn = yydef[ yystate ])   ==    -2) {
  125.  
  126.     if (yychar < 0)   if ((yychar = yylex())  <  0)    yychar = 0;
  127.  
  128.     /* Look through exception table: */
  129.  
  130.     for (
  131.         yyxi = yyexca
  132.         ;
  133.         (*yyxi != (-1))   ||   (yyxi[1] != yystate)
  134.         ;
  135.         yyxi += 2
  136.     );                        /* VOID    */
  137.  
  138.     for (yyxi += 2;   *yyxi >= 0;    yyxi += 2) {
  139.  
  140.          if (*yyxi == yychar)   break;
  141.     }
  142.  
  143.     if ((yyn = yyxi[1])   <   0)   return  0;    /* Accept. */
  144.     }
  145.  
  146.     if (!yyn) {
  147.  
  148.     /* Error.   Attempt to resume parsing: */
  149.  
  150.     switch (yyerrflag) {
  151.  
  152.     case 0:   /* Brand new error. */
  153.  
  154.         yyerror( "syntax error" );
  155. yyerrlab:
  156.         ++yynerrs;
  157.  
  158.     case 1:
  159.     case 2: /* Incompletely recovered error. Try again: */
  160.  
  161.         yyerrflag = 3;
  162.  
  163.         /* Find a state where "error" is a legal shift action: */
  164.  
  165.         while (yyps >= yys) {
  166.  
  167.         yyn = yypact[ *yyps ] + YYERRCODE;
  168.  
  169.         if (
  170.             yyn >= 0
  171.             &&
  172.             yyn < YYLAST
  173.             &&
  174.             yychk[ yyact[ yyn ]] == YYERRCODE
  175.         ) {
  176.             yystate = yyact[ yyn ];  /* Simulate a shift of "error" */
  177.             goto yystack;
  178.         }
  179.         yyn = yypact[ *yyps ];
  180.  
  181.         /* The current yyps has no shift onn "error", pop stack */
  182.  
  183.         if (yydebug) {
  184.             printf(
  185.             "Error recovery pops state %d, uncovers %d\n",
  186.             *yyps,
  187.             yyps[-1]
  188.             );
  189.         }
  190.         --yyps;
  191.         --yypv;
  192.             }
  193.  
  194.         /* There is no state on the stack with an error shift. Abort. */
  195.  
  196. yyabort:
  197.         return(1);
  198.  
  199.  
  200.     case 3:  /* No shift yet; clobber input char: */
  201.         if (yydebug) {
  202.         printf( "error recovery discards char %d\n", yychar );
  203.         }
  204.         if (yychar == 0)   goto yyabort; /* Don't discard EOF, quit. */
  205.         yychar = -1;
  206.         goto yynewstate;   /* Try again in the same state. */
  207.     }
  208.     }
  209.  
  210.     /* Reduction by production yyn: */
  211.  
  212.     if (yydebug)   printf( "reduce %d\n", yyn );
  213.     yyps -= yyr2[ yyn ];
  214.     yypvt = yypv;
  215.     yypv -= yyr2[ yyn ];
  216.  
  217. #ifdef UNION
  218.    yyunion(&yyval, &yypv[1]);
  219. #else
  220.    yyval = yypv[1];
  221. #endif
  222.  
  223.    yym    = yyn;
  224.  
  225.    /* Consult goto table to find next state: */
  226.    yyn = yyr1[    yyn ];
  227.    yyj = yypgo[ yyn ] + *yyps + 1;
  228.  
  229.    if (
  230.        yyj >= YYLAST
  231.        ||
  232.        yychk[ yystate = yyact[yyj] ] != -yyn
  233.    ) {
  234.        yystate    = yyact[ yypgo[ yyn ]];
  235.    }
  236.  
  237. #ifdef OLD
  238.    /* Invoke desired action: */
  239.    switch (yym) {
  240. /*    %A                    */
  241.    }
  242. #else
  243.     /* Invoke appropriate action function for this reduction: */
  244.     switch ((*(yyActionIndex[ yym ]))()) {
  245.     case 0: return 0;    /* YYERROR  action  */
  246.     case 1: return 1;    /* YYACCEPT action  */
  247.     default: ;
  248.     }
  249. #endif
  250.  
  251.    goto yystack;  /* Stack new state and value. */
  252.  
  253. }
  254.  
  255. /***********************************************************************/
  256. /* Null action function:                           */
  257. /***********************************************************************/ 
  258. yyANop() { return -1; }
  259.  
  260. /***********************************************************************/
  261. /* Action functions constructed from user-supplied action code chunks: */ 
  262. /***********************************************************************/ 
  263. $A
  264.  
  265.  
  266.