home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V7 / usr / lib / yaccpar < prev   
Encoding:
Text File  |  1979-01-10  |  3.2 KB  |  140 lines

  1. #
  2. # define YYFLAG -1000
  3. # define YYERROR goto yyerrlab
  4. # define YYACCEPT return(0)
  5. # define YYABORT return(1)
  6.  
  7. /*    parser for yacc output    */
  8.  
  9. int yydebug = 0; /* 1 for debugging */
  10. YYSTYPE yyv[YYMAXDEPTH]; /* where the values are stored */
  11. int yychar = -1; /* current input token number */
  12. int yynerrs = 0;  /* number of errors */
  13. short yyerrflag = 0;  /* error recovery flag */
  14.  
  15. yyparse() {
  16.  
  17.     short yys[YYMAXDEPTH];
  18.     short yyj, yym;
  19.     register YYSTYPE *yypvt;
  20.     register short yystate, *yyps, yyn;
  21.     register YYSTYPE *yypv;
  22.     register short *yyxi;
  23.  
  24.     yystate = 0;
  25.     yychar = -1;
  26.     yynerrs = 0;
  27.     yyerrflag = 0;
  28.     yyps= &yys[-1];
  29.     yypv= &yyv[-1];
  30.  
  31.  yystack:    /* put a state and value onto the stack */
  32.  
  33.     if( yydebug  ) printf( "state %d, char 0%o\n", yystate, yychar );
  34.         if( ++yyps> &yys[YYMAXDEPTH] ) { yyerror( "yacc stack overflow" ); return(1); }
  35.         *yyps = yystate;
  36.         ++yypv;
  37.         *yypv = yyval;
  38.  
  39.  yynewstate:
  40.  
  41.     yyn = yypact[yystate];
  42.  
  43.     if( yyn<= YYFLAG ) goto yydefault; /* simple state */
  44.  
  45.     if( yychar<0 ) if( (yychar=yylex())<0 ) yychar=0;
  46.     if( (yyn += yychar)<0 || yyn >= YYLAST ) goto yydefault;
  47.  
  48.     if( yychk[ yyn=yyact[ yyn ] ] == yychar ){ /* valid shift */
  49.         yychar = -1;
  50.         yyval = yylval;
  51.         yystate = yyn;
  52.         if( yyerrflag > 0 ) --yyerrflag;
  53.         goto yystack;
  54.         }
  55.  
  56.  yydefault:
  57.     /* default state action */
  58.  
  59.     if( (yyn=yydef[yystate]) == -2 ) {
  60.         if( yychar<0 ) if( (yychar=yylex())<0 ) yychar = 0;
  61.         /* look through exception table */
  62.  
  63.         for( yyxi=yyexca; (*yyxi!= (-1)) || (yyxi[1]!=yystate) ; yyxi += 2 ) ; /* VOID */
  64.  
  65.         while( *(yyxi+=2) >= 0 ){
  66.             if( *yyxi == yychar ) break;
  67.             }
  68.         if( (yyn = yyxi[1]) < 0 ) return(0);   /* accept */
  69.         }
  70.  
  71.     if( yyn == 0 ){ /* error */
  72.         /* error ... attempt to resume parsing */
  73.  
  74.         switch( yyerrflag ){
  75.  
  76.         case 0:   /* brand new error */
  77.  
  78.             yyerror( "syntax error" );
  79.         yyerrlab:
  80.             ++yynerrs;
  81.  
  82.         case 1:
  83.         case 2: /* incompletely recovered error ... try again */
  84.  
  85.             yyerrflag = 3;
  86.  
  87.             /* find a state where "error" is a legal shift action */
  88.  
  89.             while ( yyps >= yys ) {
  90.                yyn = yypact[*yyps] + YYERRCODE;
  91.                if( yyn>= 0 && yyn < YYLAST && yychk[yyact[yyn]] == YYERRCODE ){
  92.                   yystate = yyact[yyn];  /* simulate a shift of "error" */
  93.                   goto yystack;
  94.                   }
  95.                yyn = yypact[*yyps];
  96.  
  97.                /* the current yyps has no shift onn "error", pop stack */
  98.  
  99.                if( yydebug ) printf( "error recovery pops state %d, uncovers %d\n", *yyps, yyps[-1] );
  100.                --yyps;
  101.                --yypv;
  102.                }
  103.  
  104.             /* there is no state on the stack with an error shift ... abort */
  105.  
  106.     yyabort:
  107.             return(1);
  108.  
  109.  
  110.         case 3:  /* no shift yet; clobber input char */
  111.  
  112.             if( yydebug ) printf( "error recovery discards char %d\n", yychar );
  113.  
  114.             if( yychar == 0 ) goto yyabort; /* don't discard EOF, quit */
  115.             yychar = -1;
  116.             goto yynewstate;   /* try again in the same state */
  117.  
  118.             }
  119.  
  120.         }
  121.  
  122.     /* reduction by production yyn */
  123.  
  124.         if( yydebug ) printf("reduce %d\n",yyn);
  125.         yyps -= yyr2[yyn];
  126.         yypvt = yypv;
  127.         yypv -= yyr2[yyn];
  128.         yyval = yypv[1];
  129.         yym=yyn;
  130.             /* consult goto table to find next state */
  131.         yyn = yyr1[yyn];
  132.         yyj = yypgo[yyn] + *yyps + 1;
  133.         if( yyj>=YYLAST || yychk[ yystate = yyact[yyj] ] != -yyn ) yystate = yyact[yypgo[yyn]];
  134.         switch(yym){
  135.             $A
  136.         }
  137.         goto yystack;  /* stack new state and value */
  138.  
  139.     }
  140.