home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OS2-YACC.ZIP / YYPARS.C < prev    next >
Text File  |  1989-09-29  |  5KB  |  182 lines

  1. /*
  2.   HEADER: CUG     nnn.nn;
  3.   TITLE:     YACC - Yet Another Compilier-Compilier
  4.   VERSION:     1.0 for IBM-PC
  5.   DATE:      JAN 28, 1985
  6.   DESCRIPTION:     LALR(1) Parser Generator. From UNIX
  7.   KEYWORDS:     Parser Generator Compilier-Compilier YACC
  8.   SYSTEM:     IBM-PC and Compatiables
  9.   FILENAME:      YYPARS.C
  10.   WARNINGS:     This program is not for the casual user. It will
  11.          be useful primarily to expert developers.
  12.   CRC:         N/A
  13.   SEE-ALSO:     LEX and PREP
  14.   AUTHORS:     Scott Guthery 11100 leafwood lane Austin, TX 78750
  15.   COMPILERS:     DESMET-C
  16.   REFERENCES:     UNIX Systems Manuals
  17. */
  18.  
  19. # define YYFLAG -1000
  20. # define YYERROR goto yyerrlab
  21. # define YYACCEPT return(0)
  22. # define YYABORT return(1)
  23.  
  24. /*      parser for yacc output  */
  25.  
  26. int yydebug = 0; /* 1 for debugging */
  27. YYSTYPE yyv[YYMAXDEPTH]; /* where the values are stored */
  28. int yychar = -1; /* current input token number */
  29. int yynerrs = 0;  /* number of errors */
  30. short yyerrflag = 0;  /* error recovery flag */
  31.  
  32. int yyparse( void )
  33.    {
  34.  
  35.    short yys[YYMAXDEPTH];
  36.    short yyj, yym;
  37.    register YYSTYPE *yypvt;
  38.    register short yystate, *yyps, yyn;
  39.    register YYSTYPE *yypv;
  40.    register short *yyxi;
  41.  
  42.    yystate = 0;
  43.    yychar = -1;
  44.    yynerrs = 0;
  45.    yyerrflag = 0;
  46.    yyps= &yys[-1];
  47.    yypv= &yyv[-1];
  48.  
  49. yystack:    /* put a state and value onto the stack */
  50.  
  51.    if( yydebug  ) printf( "state %d, char 0%o\n", yystate, yychar );
  52.    if( ++yyps> &yys[YYMAXDEPTH] )
  53.       {
  54.       yyerror( "yacc stack overflow" );
  55.       return(1);
  56.       }
  57.    *yyps = yystate;
  58.    ++yypv;
  59. #ifdef UNION
  60.    yyunion(yypv, &yyval);
  61. #else
  62.    *yypv = yyval;
  63. #endif
  64. yynewstate:
  65.  
  66.    yyn = yypact[yystate];
  67.  
  68.    if( yyn<= YYFLAG ) goto yydefault; /* simple state */
  69.  
  70.    if( yychar<0 ) if( (yychar=yylex())<0 ) yychar=0;
  71.    if( (yyn += yychar)<0 || yyn >= YYLAST ) goto yydefault;
  72.  
  73.    if( yychk[ yyn=yyact[ yyn ] ] == yychar )
  74.       {
  75.       /* valid shift */
  76.       yychar = -1;
  77. #ifdef UNION
  78.       yyunion(&yyval, &yylval);
  79. #else
  80.       yyval = yylval;
  81. #endif
  82.       yystate = yyn;
  83.       if( yyerrflag > 0 ) --yyerrflag;
  84.       goto yystack;
  85.       }
  86. yydefault:
  87.    /* default state action */
  88.  
  89.    if( (yyn=yydef[yystate]) == -2 )
  90.       {
  91.       if( yychar<0 ) if( (yychar=yylex())<0 ) yychar = 0;
  92.       /* look through exception table */
  93.  
  94.       for( yyxi=yyexca; (*yyxi!= (-1)) || (yyxi[1]!=yystate) ; yyxi += 2 ) ; /* VOID */
  95.  
  96.       for(yyxi+=2; *yyxi >= 0; yyxi+=2)
  97.          {
  98.          if( *yyxi == yychar ) break;
  99.          }
  100.       if( (yyn = yyxi[1]) < 0 ) return(0);   /* accept */
  101.       }
  102.  
  103.    if( yyn == 0 )
  104.       {
  105.       /* error */
  106.       /* error ... attempt to resume parsing */
  107.  
  108.       switch( yyerrflag )
  109.          {
  110.  
  111.       case 0:   /* brand new error */
  112.  
  113.          yyerror( "syntax error" );
  114. yyerrlab:
  115.          ++yynerrs;
  116.  
  117.       case 1:
  118.       case 2: /* incompletely recovered error ... try again */
  119.  
  120.          yyerrflag = 3;
  121.  
  122.          /* find a state where "error" is a legal shift action */
  123.  
  124.          while ( yyps >= yys )
  125.             {
  126.             yyn = yypact[*yyps] + YYERRCODE;
  127.             if( yyn>= 0 && yyn < YYLAST && yychk[yyact[yyn]] == YYERRCODE )
  128.                {
  129.                yystate = yyact[yyn];  /* simulate a shift of "error" */
  130.                goto yystack;
  131.                }
  132.             yyn = yypact[*yyps];
  133.  
  134.             /* the current yyps has no shift onn "error", pop stack */
  135.  
  136.             if( yydebug ) printf( "error recovery pops state %d, uncovers %d\n", *yyps, yyps[-1] );
  137.             --yyps;
  138.             --yypv;
  139.             }
  140.  
  141.          /* there is no state on the stack with an error shift ... abort */
  142.  
  143. yyabort:
  144.          return(1);
  145.  
  146.  
  147.       case 3:  /* no shift yet; clobber input char */
  148.  
  149.          if( yydebug ) printf( "error recovery discards char %d\n", yychar );
  150.  
  151.          if( yychar == 0 ) goto yyabort; /* don't discard EOF, quit */
  152.          yychar = -1;
  153.          goto yynewstate;   /* try again in the same state */
  154.  
  155.          }
  156.  
  157.       }
  158.  
  159.    /* reduction by production yyn */
  160.  
  161.    if( yydebug ) printf("reduce %d\n",yyn);
  162.    yyps -= yyr2[yyn];
  163.    yypvt = yypv;
  164.    yypv -= yyr2[yyn];
  165. #ifdef UNION
  166.    yyunion(&yyval, &yypv[1]);
  167. #else
  168.    yyval = yypv[1];
  169. #endif
  170.    yym=yyn;
  171.    /* consult goto table to find next state */
  172.    yyn = yyr1[yyn];
  173.    yyj = yypgo[yyn] + *yyps + 1;
  174.    if( yyj>=YYLAST || yychk[ yystate = yyact[yyj] ] != -yyn ) yystate = yyact[yypgo[yyn]];
  175.    switch(yym)
  176.       {
  177.       $A
  178.       }
  179.    goto yystack;  /* stack new state and value */
  180.  
  181.    }
  182.