home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V6 / usr / source / yacc / lib / parser.c < prev    next >
Encoding:
C/C++ Source or Header  |  1975-05-13  |  3.2 KB  |  126 lines

  1. # define _maxdepth 150
  2.  
  3. /*    parser for yacc output    */
  4. extern int yyval; /* defined in the table file */
  5. extern int yylval; /* defined in the table file */
  6. extern int *yypv; /* defined in the table file */
  7.  
  8.  
  9. int yydebug 0; /* 1 for debugging */
  10. int yyv[_maxdepth]; /* where the values are stored */
  11. int yystate 0; /* current parser state */
  12. int yychar -1; /* current input token number */
  13. int yynerrs 0;  /* number of errors */
  14. int yyerrflag 0;  /* error recovery flag */
  15.  
  16.  
  17. yyparse() {
  18.  
  19.    extern int yygo[], yypgo[], yyr1[], yyr2[], yyact[], yypact[];
  20.    auto int *ps, s[_maxdepth];
  21.    auto int ac, n, *p;
  22.  
  23.    yystate = 0;
  24.    yychar = -1;
  25.    yynerrs = 0;
  26.    yyerrflag = 0;
  27.    ps= &s[0]-1;
  28.    yypv= &yyv[0]-1;
  29.  
  30.  stack:    /* put a state and value onto the stack */
  31.  
  32.    if( yydebug  ) printf( "state %d, value %d, char %d\n",yystate,yyval,yychar );
  33.    *++ps = yystate;
  34.    *++yypv = yyval;
  35.  
  36.  newstate:      /* set ap to point to the parsing actions for the new state */
  37.  
  38.    p = &yyact[ yypact[yystate+1] ]; 
  39.  
  40.  actn:       /* get the next action, and perform it */
  41.  
  42.    n = ( ac = *p++ ) & 07777;  /* n is the "address" of the action */
  43.  
  44.    switch( ac>>12 ) { /* switch on operation */
  45.  
  46.    case 1:        /* skip on test */
  47.       if( yydebug && (yychar<0) ){
  48.         yychar = yylex();
  49.         printf( "character %d read\n", yychar );
  50.         }
  51.       if( n != ( (yychar<0) ? ( yychar=yylex() ) : yychar ) ) ++p;
  52.       goto actn;  /* get next action */
  53.  
  54.    case 2:        /* shift */
  55.  
  56.       yystate=n;
  57.       yyval=yylval;
  58.       yychar= -1;
  59.       if( yyerrflag ) --yyerrflag;
  60.       goto stack;   /* stack new state */
  61.  
  62.    case 3:        /* reduce */
  63.  
  64.       if( yydebug ) printf("reduce %d\n",n);
  65.       ps =- yyr2[n];
  66.       yypv =- yyr2[n];
  67.       yyval=yypv[1];
  68.       yyactr(n);
  69.          /* consult goto table to find next state */
  70.       for( p= &yygo[yypgo[yyr1[n]]]; *p != *ps && *p >= 0 ; p =+ 2 ) ;
  71.       yystate = p[1];
  72.       goto stack;  /* stack new state and value */
  73.  
  74.    case 4:        /* accept */
  75.       return(0);
  76.  
  77.    case 0:   /* error ... attempt to resume parsing */
  78.  
  79.       switch( yyerrflag ){
  80.  
  81.       case 0:   /* brand new error */
  82.  
  83.          ++yynerrs;
  84.          yyerror( "syntax error" );
  85.  
  86.       case 1:
  87.       case 2: /* incompletely recovered error ... try again */
  88.  
  89.          yyerrflag = 3;
  90.  
  91.          /* find a state where "error" is a legal shift action */
  92.  
  93.          while ( ps >= s ) {
  94.             for( p= &yyact[ yypact[*ps+1] ] ; (*p>>12) == 1 ; p =+ 2 ) /* search ps actions */
  95.                 if( *p == 4352 ) goto found;
  96.  
  97.             /* the current ps has no shift onn "error", pop stack */
  98.  
  99.             if( yydebug ) printf( "error recovery pops state %d, uncovers %d\n", *ps, ps[-1] );
  100.             --ps;
  101.             --yypv;
  102.             }
  103.  
  104.          /* there is no state on the stack with an error shift ... abort */
  105.  
  106.     abort:
  107.          return(1);
  108.  
  109.       found:   /* we have a state with a shift on "error", resume parsing */
  110.  
  111.          yystate = p[1] & 07777;
  112.          goto stack;
  113.  
  114.       case 3:  /* no shift yet; clobber input char */
  115.  
  116.          if( yydebug ) printf( "error recovery discards char %d\n", yychar );
  117.  
  118.          if( yychar == 0 ) goto abort; /* don't discard EOF, quit */
  119.          yychar = -1;
  120.          goto newstate;   /* try again in the same state */
  121.  
  122.          }
  123.  
  124.       }
  125.    }
  126.