home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / DataScope 2.0.3 / DataScope2l / DSSource / fparse.y.c < prev    next >
Encoding:
Text File  |  1994-05-04  |  6.5 KB  |  314 lines  |  [TEXT/MPS ]

  1.  
  2. # line 3 "yfor.y"
  3. #include "stdio.h"
  4.  
  5. struct nodet {
  6.     int token;
  7.  
  8.     struct nodet
  9.         *left,
  10.         *right;
  11.  
  12.     float constv;
  13.     char *var;            /* pointer to name of variable involved */
  14. };
  15.  
  16. typedef struct nodet node;
  17.  
  18. float f;
  19. node *mkone(),*nlist;
  20.  
  21.  
  22. # line 25 "yfor.y"
  23. typedef union  {
  24.     node *n;
  25.     float lexc;
  26.     char *lexv;
  27. } YYSTYPE;
  28. # define TPLUS 257
  29. # define TMINUS 258
  30. # define TSTAR 259
  31. # define TSLASH 260
  32. # define TEQUAL 261
  33. # define TLP 262
  34. # define TRP 263
  35. # define TFN 264
  36. # define TFN2 265
  37. # define TCOMMA 266
  38. # define TIDENT 267
  39. # define TFLOAT 268
  40. # define UMINUS 269
  41. #define yyclearin yychar = -1
  42. #define yyerrok yyerrflag = 0
  43. extern int yychar;
  44. extern short yyerrflag;
  45. #ifndef YYMAXDEPTH
  46. #define YYMAXDEPTH 150
  47. #endif
  48. YYSTYPE yylval, yyval;
  49. # define YYERRCODE 256
  50.  
  51. # line 103 "yfor.y"
  52.  
  53.  
  54. #include "fcomp.c"
  55.  
  56. /*
  57. *  include lexical analyzer
  58. */
  59. #include "fparse.l.c"
  60. short yyexca[] ={
  61. -1, 1,
  62.     0, -1,
  63.     -2, 0,
  64.     };
  65. # define YYNPROD 14
  66. # define YYLAST 53
  67. short yyact[]={
  68.  
  69.    7,   2,  17,   6,   8,  11,  12,  14,  13,   9,
  70.   10,  24,  14,  13,  25,  11,  12,  14,  13,   5,
  71.    3,  27,  11,  12,  14,  13,   4,   1,  22,  11,
  72.   12,  14,  13,   0,  15,  16,   0,   0,  18,  19,
  73.   20,  21,   0,   0,  23,   0,   0,   0,   0,   0,
  74.    0,   0,  26 };
  75. short yypact[]={
  76.  
  77. -266,-1000,-241,-258,-228,-1000,-1000,-258,-258,-260,
  78. -1000,-258,-258,-258,-258,-1000,-235,-258,-247,-247,
  79. -1000,-1000,-1000,-252,-1000,-258,-242,-1000 };
  80. short yypgo[]={
  81.  
  82.    0,  27,  26,  19,   3 };
  83. short yyr1[]={
  84.  
  85.    0,   1,   2,   2,   2,   2,   2,   2,   2,   2,
  86.    2,   2,   3,   4 };
  87. short yyr2[]={
  88.  
  89.    0,   3,   1,   1,   3,   3,   3,   3,   2,   3,
  90.    4,   6,   1,   1 };
  91. short yychk[]={
  92.  
  93. -1000,  -1, 267, 261,  -2,  -3,  -4, 258, 262, 267,
  94.  268, 257, 258, 260, 259,  -2,  -2, 262,  -2,  -2,
  95.   -2,  -2, 263,  -2, 263, 266,  -2, 263 };
  96. short yydef[]={
  97.  
  98.    0,  -2,   0,   0,   1,   2,   3,   0,   0,  13,
  99.   12,   0,   0,   0,   0,   8,   0,   0,   4,   5,
  100.    6,   7,   9,   0,  10,   0,   0,  11 };
  101. #ifndef lint
  102. static    char yaccpar_sccsid[] = "@(#)yaccpar 1.2 86/07/18 SMI"; /* from UCB 4.1 83/02/11 */
  103. #endif
  104.  
  105. #
  106. # define YYFLAG -1000
  107. # define YYERROR goto yyerrlab
  108. # define YYACCEPT return(0)
  109. # define YYABORT return(1)
  110.  
  111. /*    parser for yacc output    */
  112.  
  113. #ifdef YYDEBUG
  114. int yydebug = 0; /* 1 for debugging */
  115. #endif
  116. YYSTYPE yyv[YYMAXDEPTH]; /* where the values are stored */
  117. int yychar = -1; /* current input token number */
  118. int yynerrs = 0;  /* number of errors */
  119. short yyerrflag = 0;  /* error recovery flag */
  120.  
  121. yyparse() {
  122.  
  123.     short yys[YYMAXDEPTH];
  124.     short yyj, yym;
  125.     register YYSTYPE *yypvt;
  126.     register short yystate, *yyps, yyn;
  127.     register YYSTYPE *yypv;
  128.     register short *yyxi;
  129.  
  130.     yystate = 0;
  131.     yychar = -1;
  132.     yynerrs = 0;
  133.     yyerrflag = 0;
  134.     yyps= &yys[-1];
  135.     yypv= &yyv[-1];
  136.  
  137.  yystack:    /* put a state and value onto the stack */
  138.  
  139. #ifdef YYDEBUG
  140.     if( yydebug  ) printf( "state %d, char 0%o\n", yystate, yychar );
  141. #endif
  142.         if( ++yyps>= &yys[YYMAXDEPTH] ) { yyerror( "yacc stack overflow" ); return(1); }
  143.         *yyps = yystate;
  144.         ++yypv;
  145.         *yypv = yyval;
  146.  
  147.  yynewstate:
  148.  
  149.     yyn = yypact[yystate];
  150.  
  151.     if( yyn<= YYFLAG ) goto yydefault; /* simple state */
  152.  
  153.     if( yychar<0 ) if( (yychar=yylex())<0 ) yychar=0;
  154.     if( (yyn += yychar)<0 || yyn >= YYLAST ) goto yydefault;
  155.  
  156.     if( yychk[ yyn=yyact[ yyn ] ] == yychar ){ /* valid shift */
  157.         yychar = -1;
  158.         yyval = yylval;
  159.         yystate = yyn;
  160.         if( yyerrflag > 0 ) --yyerrflag;
  161.         goto yystack;
  162.         }
  163.  
  164.  yydefault:
  165.     /* default state action */
  166.  
  167.     if( (yyn=yydef[yystate]) == -2 ) {
  168.         if( yychar<0 ) if( (yychar=yylex())<0 ) yychar = 0;
  169.         /* look through exception table */
  170.  
  171.         for( yyxi=yyexca; (*yyxi!= (-1)) || (yyxi[1]!=yystate) ; yyxi += 2 ) ; /* VOID */
  172.  
  173.         while( *(yyxi+=2) >= 0 ){
  174.             if( *yyxi == yychar ) break;
  175.             }
  176.         if( (yyn = yyxi[1]) < 0 ) return(0);   /* accept */
  177.         }
  178.  
  179.     if( yyn == 0 ){ /* error */
  180.         /* error ... attempt to resume parsing */
  181.  
  182.         switch( yyerrflag ){
  183.  
  184.         case 0:   /* brand new error */
  185.  
  186.             yyerror( "syntax error" );
  187.         yyerrlab:
  188.             ++yynerrs;
  189.  
  190.         case 1:
  191.         case 2: /* incompletely recovered error ... try again */
  192.  
  193.             yyerrflag = 3;
  194.  
  195.             /* find a state where "error" is a legal shift action */
  196.  
  197.             while ( yyps >= yys ) {
  198.                yyn = yypact[*yyps] + YYERRCODE;
  199.                if( yyn>= 0 && yyn < YYLAST && yychk[yyact[yyn]] == YYERRCODE ){
  200.                   yystate = yyact[yyn];  /* simulate a shift of "error" */
  201.                   goto yystack;
  202.                   }
  203.                yyn = yypact[*yyps];
  204.  
  205.                /* the current yyps has no shift onn "error", pop stack */
  206.  
  207. #ifdef YYDEBUG
  208.                if( yydebug ) printf( "error recovery pops state %d, uncovers %d\n", *yyps, yyps[-1] );
  209. #endif
  210.                --yyps;
  211.                --yypv;
  212.                }
  213.  
  214.             /* there is no state on the stack with an error shift ... abort */
  215.  
  216.     yyabort:
  217.             return(1);
  218.  
  219.  
  220.         case 3:  /* no shift yet; clobber input char */
  221.  
  222. #ifdef YYDEBUG
  223.             if( yydebug ) printf( "error recovery discards char %d\n", yychar );
  224. #endif
  225.  
  226.             if( yychar == 0 ) goto yyabort; /* don't discard EOF, quit */
  227.             yychar = -1;
  228.             goto yynewstate;   /* try again in the same state */
  229.  
  230.             }
  231.  
  232.         }
  233.  
  234.     /* reduction by production yyn */
  235.  
  236. #ifdef YYDEBUG
  237.         if( yydebug ) printf("reduce %d\n",yyn);
  238. #endif
  239.         yyps -= yyr2[yyn];
  240.         yypvt = yypv;
  241.         yypv -= yyr2[yyn];
  242.         yyval = yypv[1];
  243.         yym=yyn;
  244.             /* consult goto table to find next state */
  245.         yyn = yyr1[yyn];
  246.         yyj = yypgo[yyn] + *yyps + 1;
  247.         if( yyj>=YYLAST || yychk[ yystate = yyact[yyj] ] != -yyn ) yystate = yyact[yypgo[yyn]];
  248.         switch(yym){
  249.             
  250. case 1:
  251. # line 47 "yfor.y"
  252.             nlist = mkone(TEQUAL,yypvt[-0].n,NULL);
  253.             nlist->var = yypvt[-2].lexv;                /* save name of the variable */
  254.             } break;
  255. case 4:
  256. # line 56 "yfor.y"
  257. {
  258.                 yyval.n = mkone(TPLUS,yypvt[-2].n,yypvt[-0].n);
  259.                 } break;
  260. case 5:
  261. # line 60 "yfor.y"
  262. {
  263.                 yyval.n = mkone(TMINUS,yypvt[-2].n,yypvt[-0].n);
  264.                 } break;
  265. case 6:
  266. # line 64 "yfor.y"
  267. {
  268.                 yyval.n = mkone(TSLASH,yypvt[-2].n,yypvt[-0].n);
  269.                 } break;
  270. case 7:
  271. # line 68 "yfor.y"
  272. {
  273.                 yyval.n = mkone(TSTAR,yypvt[-2].n,yypvt[-0].n);
  274.                 } break;
  275. case 8:
  276. # line 72 "yfor.y"
  277. {
  278.                 yyval.n = mkone(UMINUS,yypvt[-0].n,NULL);
  279.                 } break;
  280. case 9:
  281. # line 76 "yfor.y"
  282. {
  283.                 yyval.n = yypvt[-1].n;
  284.                 } break;
  285. case 10:
  286. # line 80 "yfor.y"
  287. {
  288.                 yyval.n = mkone(TFN,yypvt[-1].n,NULL);
  289.                 yyval.n->var = yypvt[-3].lexv;
  290.                 } break;
  291. case 11:
  292. # line 85 "yfor.y"
  293. {
  294.                 yyval.n = mkone(TFN2,yypvt[-3].n,yypvt[-1].n);
  295.                 yyval.n->var = yypvt[-5].lexv;
  296.                 } break;
  297. case 12:
  298. # line 92 "yfor.y"
  299.                 yyval.n = mkone(TFLOAT,NULL,NULL);
  300.                 yyval.n->constv = yypvt[-0].lexc;
  301.                 } break;
  302. case 13:
  303. # line 97 "yfor.y"
  304.                 yyval.n = mkone(TIDENT,NULL,NULL);
  305.                 yyval.n->var = yypvt[-0].lexv;
  306.                 } break;
  307.         }
  308.         goto yystack;  /* stack new state and value */
  309.  
  310.     }
  311.