home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d105 / bison.lha / Bison / calc.c < prev    next >
C/C++ Source or Header  |  1987-10-25  |  14KB  |  526 lines

  1.  
  2. /*  A Bison parser, made from calc.y  */
  3.  
  4. #line 1 "calc.y"
  5.  
  6. #include <ctype.h>
  7.  
  8. int regs[26];
  9. int base;
  10.  
  11. int val;
  12. int printflag;
  13.  
  14.  
  15. #ifndef YYLTYPE
  16. typedef
  17.   struct yyltype
  18. {
  19.       int timestamp;
  20.       int first_line;
  21.       int first_column;
  22. int last_line;
  23.       int last_column;
  24.       char *text;
  25.    }
  26. yyltype;
  27.  
  28. #define YYLTYPE yyltype
  29. #endif
  30.  
  31. #define    YYACCEPT    return(0)
  32. #define    YYERROR    return(1)
  33. #include <stdio.h>
  34.  
  35. typedef int YYSTYPE;
  36. #define    DIGIT    258
  37. #define    LETTER    259
  38. #define    EXIT    260
  39. #define    UMINUS    261
  40.  
  41.  
  42.  
  43. #define    YYFINAL        31
  44. #define    YYFLAG        -32768
  45. #define    YYNTBASE    17
  46.  
  47. #define YYTRANSLATE(x) (yytranslate[x])
  48.  
  49. static char yytranslate[] = {     0,
  50.      2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
  51.      2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
  52.      2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
  53.      2,     2,     2,     2,     2,     2,    12,     7,     2,    15,
  54.     16,    10,     8,     2,     9,     2,    11,     2,     2,     2,
  55.      2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
  56.     14,     2,     2,     2,     2,     2,     2,     2,     2,     2,
  57.      2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
  58.      2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
  59.      2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
  60.      2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
  61.      2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
  62.      2,     2,     2,     6,     2,     2,     2,     2,     2,     2,
  63.      2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
  64.      2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
  65.      2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
  66.      2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
  67.      2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
  68.      2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
  69.      2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
  70.      2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
  71.      2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
  72.      2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
  73.      2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
  74.      2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
  75.      2,     2,     2,     2,     2,     1,     2,     3,     4,     5,
  76.     13
  77. };
  78.  
  79. static short yyr1[] = {     0,
  80.     17,    17,    17,    18,    18,    18,    18,    18,    18,    18,
  81.     18,    18,    18,    18,    19,    19
  82. };
  83.  
  84. static short yyr2[] = {     0,
  85.      1,     3,     1,     3,     3,     3,     3,     3,     3,     3,
  86.      3,     2,     1,     1,     1,     2
  87. };
  88.  
  89. static short yydefact[] = {     0,
  90.     15,    13,     3,     0,     0,     1,    14,     0,    13,    12,
  91.      0,     0,     0,     0,     0,     0,     0,     0,    16,     2,
  92.      4,    10,    11,     5,     6,     7,     8,     9,     0,     0,
  93.      0
  94. };
  95.  
  96. static short yydefgoto[] = {    29,
  97.      6,     7
  98. };
  99.  
  100. static short yypact[] = {    12,
  101. -32768,   -12,-32768,    15,    15,    36,     0,    15,-32768,-32768,
  102.     25,    15,    15,    15,    15,    15,    15,    15,-32768,    36,
  103. -32768,    42,    47,    -5,    -5,-32768,-32768,-32768,    20,    22,
  104. -32768
  105. };
  106.  
  107. static short yypgoto[] = {-32768,
  108.     -4,-32768
  109. };
  110.  
  111.  
  112. #define    YYLAST        59
  113.  
  114.  
  115. static short yytable[] = {    10,
  116.     11,     8,    19,    20,    16,    17,    18,    22,    23,    24,
  117.     25,    26,    27,    28,     1,     2,     3,     1,     9,    30,
  118.      4,    31,     0,     4,     0,     0,     5,     0,     0,     5,
  119.     12,    13,    14,    15,    16,    17,    18,     0,     0,     0,
  120.     21,    12,    13,    14,    15,    16,    17,    18,    13,    14,
  121.     15,    16,    17,    18,    14,    15,    16,    17,    18
  122. };
  123.  
  124. static short yycheck[] = {     4,
  125.      5,    14,     3,     8,    10,    11,    12,    12,    13,    14,
  126.     15,    16,    17,    18,     3,     4,     5,     3,     4,     0,
  127.      9,     0,    -1,     9,    -1,    -1,    15,    -1,    -1,    15,
  128.      6,     7,     8,     9,    10,    11,    12,    -1,    -1,    -1,
  129.     16,     6,     7,     8,     9,    10,    11,    12,     7,     8,
  130.      9,    10,    11,    12,     8,     9,    10,    11,    12
  131. };
  132. #define YYPURE 1
  133.  
  134. #line 2 "simparser.c"
  135.  
  136. /* Skeleton output parser for bison,
  137.    copyright (C) 1984 Bob Corbett and Richard Stallman
  138.  
  139.    Permission is granted to anyone to make or distribute verbatim copies of this program
  140.    provided that the copyright notice and this permission notice are preserved;
  141.    and provided that the recipient is not asked to waive or limit his right to
  142.    redistribute copies as permitted by this permission notice;
  143.    and provided that anyone possessing an executable copy
  144.    is granted access to copy the source code, in machine-readable form,
  145.    in some reasonable manner.
  146.  
  147.    Permission is granted to distribute derived works or enhanced versions of
  148.    this program under the above conditions with the additional condition
  149.    that the entire derivative or enhanced work
  150.    must be covered by a permission notice identical to this one.
  151.  
  152.    Anything distributed as part of a package containing portions derived
  153.    from this program, which cannot in current practice perform its function usefully
  154.    in the absense of what was derived directly from this program,
  155.    is to be considered as forming, together with the latter,
  156.    a single work derived from this program,
  157.    which must be entirely covered by a permission notice identical to this one
  158.    in order for distribution of the package to be permitted.
  159.  
  160.  In other words, you are welcome to use, share and improve this program.
  161.  You are forbidden to forbid anyone else to use, share and improve
  162.  what you give them.   Help stamp out software-hoarding!  */
  163.  
  164. /* This is the parser code that is written into each bison parser
  165.   when the %semantic_parser declaration is not specified in the grammar.
  166.   It was written by Richard Stallman by simplifying the hairy parser
  167.   used when %semantic_parser is specified.  */
  168.  
  169. /* Note: there must be only one dollar sign in this file.
  170.    It is changed into a #include that includes the action file.  */
  171.  
  172. #define yyerrok        (yyerrstatus = 0)
  173. #define yyclearin    (yychar1 = -2)
  174.  
  175. #define YYTERROR    1
  176.  
  177. #ifndef YYIMPURE
  178. #define YYLEX        yylex()
  179. #endif
  180.  
  181. #ifndef YYPURE
  182. #define YYLEX        yylex(&yylval, &yylloc)
  183. #endif
  184.  
  185. /* If nonreentrant, generate the variables here */
  186.  
  187. #ifndef YYIMPURE
  188.  
  189. int    yychar;            /*  the lookahead symbol        */
  190. YYSTYPE    yylval;            /*  the semantic value of the        */
  191.                 /*  lookahead symbol            */
  192.  
  193. YYLTYPE yylloc;            /*  location data for the lookahead    */
  194.                 /*  symbol                */
  195.  
  196. int yydebug = 0;        /*  nonzero means print parse trace    */
  197.  
  198. #endif  /* YYIMPURE */
  199.  
  200.  
  201. /*  YYMAXDEPTH indicates the size of the parser's state and value    */
  202. /*  stacks.                                */
  203.  
  204. #ifndef    YYMAXDEPTH
  205. #define    YYMAXDEPTH    250
  206. #endif
  207.  
  208. int
  209. yyparse()
  210. {
  211.   register int yystate;
  212.   register int yyn;
  213.   register short *yyssp;
  214.   register YYSTYPE *yyvsp;
  215.   YYLTYPE *yylsp;
  216.   int yyerrstatus;    /*  number of tokens to shift before error messages enabled */
  217.   int yychar1;        /*  lookahead token as an internal (translated) token number */
  218.  
  219.   short    yyssa[YYMAXDEPTH];    /*  the state stack            */
  220.   YYSTYPE yyvsa[YYMAXDEPTH];    /*  the semantic value stack        */
  221.   YYLTYPE yylsa[YYMAXDEPTH];    /*  the location stack            */
  222.  
  223.   short *yyss = yyssa;        /*  refer to the stacks thru separate pointers */
  224.   YYSTYPE *yyvs = yyvsa;    /*  to allow yyoverflow to reallocate them elsewhere */
  225.   YYLTYPE *yyls = yylsa;
  226.  
  227.   int yymaxdepth = YYMAXDEPTH;
  228.  
  229. #ifndef YYPURE
  230.  
  231.   int yychar;
  232.   YYSTYPE yylval;
  233.   YYLTYPE yylloc;
  234.  
  235.   extern int yydebug;
  236.  
  237. #endif
  238.  
  239.  
  240.   YYSTYPE yyval;        /*  the variable used to return        */
  241.                 /*  semantic values from the action    */
  242.                 /*  routines                */
  243.  
  244.   int yylen;
  245.  
  246.   yystate = 0;
  247.   yyerrstatus = 0;
  248.   yychar1 = 1;   /* just to cause a proper refill */
  249.  
  250.   yyssp = yyss - 1;
  251.   yyvsp = yyvs - 1;
  252.   yylsp = yyls - 1;
  253.  
  254. yyrefill:
  255. /* at end of file, yychar is <= 0 and yychar1 is 0.
  256.  After shifting error token, if should reuse the lookahead token,
  257.   yychar is that token in external form and yychar1 is -1.
  258.  After shifting error token, if should discard the lookahead token,
  259.   yychar is the token to be discarded and yychar1 is 100000.
  260.  After yyclearin has been done, yychar1 is -2.
  261.  After normal token processing, yychar1 is positive. */
  262.  
  263.   if (yychar1 > 0) yychar = YYLEX;
  264.   if (yychar <= 0)
  265.     yychar1 = 0;
  266.   else yychar1 = YYTRANSLATE(yychar);
  267.  
  268.   if (yydebug)
  269.     fprintf(stderr, "Next token is type %d\n", yychar);
  270.  
  271. yynewstate:
  272.  
  273.   *++yyssp = yystate;
  274.  
  275.   if (yyssp >= yyss + yymaxdepth - 1)
  276.     {
  277. #ifdef yyoverflow
  278.       /* Give user a chance to reallocate the stack */
  279.       int yyssize = yyssp-yyss, yylsize = yylsp-yyls, yyvsize = yyvsp-yyvs;
  280.       /* Use copies of these so that the &'s don't force the real ones into memory. */
  281.       YYSTYPE *yyvs1 = yyvs;
  282.       YYLTYPE *yyls1 = yyls;
  283.       short *yyss1 = yyss;
  284.  
  285.       /* Each stack pointer address is followed by the size of
  286.      the data in use in that stack, in bytes.  */
  287.       yyoverflow("parser stack overflow",
  288.          &yyss1, (yyssize+1)*sizeof(*yyssp), &yyvs1, (yyvsize+1)*sizeof(*yyvsp),
  289.          &yyls1, (yylsize+1)*sizeof(*yylsp), &yymaxdepth);
  290.  
  291.       yyss = yyss1; yyvs = yyvs1; yyls = yyls1;
  292.  
  293.       yyssp = yyss+yyssize;
  294.       yylsp = yyls+yylsize;
  295.       yyvsp = yyvs+yyvsize;
  296.  
  297.       if (yyssp >= yyss + yymaxdepth - 1)
  298.     YYERROR;
  299. #endif
  300.  
  301. #ifndef yyoverflow
  302.       yyerror("parser stack overflow");
  303. #endif
  304.     }
  305.  
  306.   if (yydebug)
  307.     fprintf(stderr, "Entering state %d\n", yystate);
  308.  
  309. yyresume:
  310.  
  311.   yyn = yypact[yystate];
  312.   if (yyn == YYFLAG)
  313.     goto yydefault;
  314.  
  315.   yyn += yychar1;
  316.   if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != yychar1)
  317.     goto yydefault;
  318.  
  319.   yyn = yytable[yyn];
  320.   if (yyn < 0)
  321.     {
  322.       yyn = -yyn;
  323.       goto yyreduce;
  324.     }
  325.   else if (yyn == 0)
  326.     goto yyerrlab;
  327.  
  328.   if (yyn == YYFINAL)
  329.     YYACCEPT;
  330.  
  331.   if (yydebug)
  332.     fprintf(stderr, "Shifting token %d, ", yychar);
  333.  
  334.   *++yyvsp = yylval;
  335.   *++yylsp = yylloc;
  336.  
  337.   /* count tokens shifted since error; after three, turn off error status.  */
  338.   if (yyerrstatus) yyerrstatus--;
  339.  
  340.   yystate = yyn;
  341.   goto yyrefill;
  342.  
  343. yydefault:
  344.  
  345.   yyn = yydefact[yystate];
  346.   if (yyn == 0)
  347.     goto yyerrlab;
  348.  
  349. yyreduce:
  350.   /* yyn is the number of a rule to reduce with.  */
  351.  
  352.   if (yydebug)
  353.     fprintf(stderr, "Reducing via rule %d, ", yyn);
  354.  
  355.   yylen = yyr2[yyn];
  356.   yyval = yyvsp[1-yylen]; /* implement default value of the action */
  357.  
  358. #include "calc.action.c"
  359.    /* this dollarsign gets turned into a #include of the action file */
  360.  
  361.   yyvsp -= yylen;
  362.   yylsp -= yylen;
  363.   yyssp -= yylen;
  364.  
  365.   if (yydebug)
  366.     fprintf(stderr, "popping to level %d\n", yyssp-yyss);
  367.  
  368.   *++yyvsp = yyval;
  369.  
  370.   yylsp++;
  371.   if (yylen == 0)
  372.     {
  373.       yylsp->first_line = yylloc.first_line;
  374.       yylsp->first_column = yylloc.first_column;
  375.       yylsp->last_line = (yylsp-1)->last_line;
  376.       yylsp->last_column = (yylsp-1)->last_column;
  377.       yylsp->text = 0;
  378.     }
  379.   else
  380.     {
  381.       yylsp->last_line = (yylsp+yylen-1)->last_line;
  382.       yylsp->last_column = (yylsp+yylen-1)->last_column;
  383.     }
  384.  
  385.   yyn = yyr1[yyn];
  386.  
  387.   yystate = yypgoto[yyn - YYNTBASE] + *yyssp;
  388.   if (yystate >= 0 && yystate <= YYLAST && yycheck[yystate] == *yyssp)
  389.     yystate = yytable[yystate];
  390.   else
  391.     yystate = yydefgoto[yyn - YYNTBASE];
  392.  
  393.   if (yychar1 == -2)   /* detect use of yyclearin  */
  394.     { yychar1 = 1; goto yyrefill; }
  395.   /* want to reuse or discard token after error => do so. */
  396.   if (yychar1 < 0 || yychar1 == 100000)
  397.     goto yyrefill;
  398.   goto yynewstate;
  399.  
  400. yyerrlab:   /* here on detecting error */
  401.  
  402.   if (! yyerrstatus)
  403.     /* If not already recovering from an error, report this error.  */
  404.     {
  405.       yyerror("parse error");
  406.     }
  407.  
  408.   if (yyerrstatus == 3)
  409.     {
  410.       /* if just tried and failed to reuse lookahead token after an error, discard it.  */
  411.       if (yydebug)
  412.     fprintf(stderr, "Discarding token %d.\n", yychar);
  413.  
  414.       yychar1 = 100000;  /* Make sure it really gets discarded. */
  415.  
  416.       /* return failure if at end of input */
  417.       if (yychar <= 0)
  418.     YYERROR;
  419.     }
  420.   else
  421.     {
  422.       /* Else try to reuse it after shifting the error token.  */
  423.       yychar1 = -1;
  424.     }
  425.  
  426.   yyerrstatus = 3;
  427.  
  428.   goto yyerrhandle;
  429.  
  430. yyerrdefault:  /* current state does not do anything special for the error token. */
  431.  
  432.   yyn = yydefact[yystate];  /* If its default is to accept any token, ok.  Otherwise pop it.*/
  433.   if (yyn) goto yydefault;
  434.  
  435. yyerrpop:   /* pop the current state because it cannot handle the error token */
  436.  
  437.   if (yyssp == yyss) YYERROR;
  438.   yyvsp--;
  439.   yylsp--;
  440.   yystate = *--yyssp;
  441.  
  442.   if (yydebug)
  443.     fprintf(stderr, "Error: Popping to level %d and state %d.\n", yyssp-yyss, yystate);
  444.  
  445. yyerrhandle:
  446.  
  447.   yyn = yypact[yystate];
  448.   if (yyn == YYFLAG)
  449.     goto yyerrdefault;
  450.  
  451.   yyn += YYTERROR;
  452.   if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != YYTERROR)
  453.     goto yyerrdefault;
  454.  
  455.   yyn = yytable[yyn];
  456.   if (yyn < 0)
  457.     {
  458.       yyn = -yyn;
  459.       goto yyreduce;
  460.     }
  461.   else if (yyn == 0)
  462.     goto yyerrpop;
  463.  
  464.   if (yyn == YYFINAL)
  465.     YYACCEPT;
  466.  
  467.   if (yydebug)
  468.     fprintf(stderr, "Shifting error token, ");
  469.  
  470.   *++yyvsp = yylval;
  471.   *++yylsp = yylloc;
  472.  
  473.   yystate = yyn;
  474.   goto yyrefill;
  475.  
  476. }
  477. #line 69 "calc.y"
  478.  
  479.  
  480. static int eol;
  481.  
  482. int
  483. yylex()
  484. {
  485.   int c;
  486.  
  487.   while ( (c=getchar()) == ' ') {}
  488.   if (c == '\n')
  489.     { eol = 1;
  490.       return 0; }
  491.   if (c == 'Q')
  492.     return(EXIT);
  493.   if (islower(c))
  494.     {
  495.       yylval = c - 'a';
  496.       return (LETTER);
  497.     }
  498.   if (isdigit(c))
  499.     {
  500.       yylval = c - '0';
  501.       return (DIGIT);
  502.     }
  503.   return (c);
  504. }
  505.  
  506. yyerror(s)
  507. char *s;
  508. {
  509.   printf("%s\n", s);
  510. }
  511.  
  512. main()
  513. {
  514.   for (;;)
  515.     {
  516.       eol = 0;
  517.       printflag = 0;
  518.  
  519.       if (yyparse()) printflag = 0;
  520.  
  521.       if (printflag) printf("%d\n", val);
  522.  
  523.       while (!eol) yylex();
  524.     }
  525. }
  526.