home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / program / language / bison / Old_Bison / Parser < prev    next >
Encoding:
Text File  |  1990-04-02  |  11.6 KB  |  495 lines

  1. /* -*-C-*-  Note some compilers choke on comments on `#line' lines.  */
  2. #line 3 "bison.simple"
  3.  
  4. /* Skeleton output parser for bison,
  5.    Copyright (C) 1984 Bob Corbett and Richard Stallman
  6.  
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 1, or (at your option)
  10.    any later version.
  11.  
  12.    This program is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with this program; if not, write to the Free Software
  19.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. /* This is the parser code that is written into each bison parser
  23.   when the %semantic_parser declaration is not specified in the grammar.
  24.   It was written by Richard Stallman by simplifying the hairy parser
  25.   used when %semantic_parser is specified.  */
  26.  
  27. /* Note: there must be only one dollar sign in this file.
  28.    It is replaced by the list of actions, each action
  29.    as one case of the switch.  */
  30.  
  31. #define yyerrok        (yyerrstatus = 0)
  32. #define yyclearin    (yychar = YYEMPTY)
  33. #define YYEMPTY        -2
  34. #define YYEOF        0
  35. #define YYFAIL        goto yyerrlab;
  36. #define YYACCEPT    return(0)
  37. #define YYABORT     return(1)
  38. #define YYERROR        goto yyerrlab
  39.  
  40. #define YYTERROR    1
  41. #define YYERRCODE    256
  42.  
  43. #ifndef YYIMPURE
  44. #define YYLEX        yylex()
  45. #endif
  46.  
  47. #ifndef YYPURE
  48. #define YYLEX        yylex(&yylval, &yylloc)
  49. #endif
  50.  
  51. /* If nonreentrant, generate the variables here */
  52.  
  53. #ifndef YYIMPURE
  54.  
  55. int    yychar;            /*  the lookahead symbol        */
  56. YYSTYPE    yylval;            /*  the semantic value of the        */
  57.                 /*  lookahead symbol            */
  58.  
  59. YYLTYPE yylloc;            /*  location data for the lookahead    */
  60.                 /*  symbol                */
  61.  
  62. int yynerrs;            /*  number of parse errors so far       */
  63. #endif  /* YYIMPURE */
  64.  
  65. #if YYDEBUG != 0
  66. int yydebug;            /*  nonzero means print parse trace    */
  67. /* Since this is uninitialized, it does not stop multiple parsers
  68.    from coexisting.  */
  69. #endif
  70.  
  71. /*  YYMAXDEPTH indicates the initial size of the parser's stacks    */
  72.  
  73. #ifndef    YYMAXDEPTH
  74. #define YYMAXDEPTH 200
  75. #endif
  76.  
  77. /*  YYMAXLIMIT is the maximum size the stacks can grow to
  78.     (effective only if the built-in stack extension method is used).  */
  79.  
  80. #ifndef YYMAXLIMIT
  81. #define YYMAXLIMIT 10000
  82. #endif
  83.  
  84.  
  85. #line 86 "bison.simple"
  86. int
  87. yyparse()
  88. {
  89.   register int yystate;
  90.   register int yyn;
  91.   register short *yyssp;
  92.   register YYSTYPE *yyvsp;
  93.   int yyerrstatus;    /*  number of tokens to shift before error messages enabled */
  94.   int yychar1;        /*  lookahead token as an internal (translated) token number */
  95.  
  96.   /*  yyss: the state stack            */
  97.   /*  yyvs: the semantic value stack        */
  98.   /*  yyls: the location stack            */
  99.  
  100.   short   *yyss = (short *)   malloc(YYMAXDEPTH * sizeof(short));
  101.   YYSTYPE *yyvs = (YYSTYPE *) malloc(YYMAXDEPTH * sizeof(YYSTYPE));
  102.  
  103. #ifdef YYLSP_NEEDED
  104.   YYLTYPE *yyls = (YYLTYPE *) malloc(YYMAXDEPTH * sizeof(YYLTYPE));
  105.   YYLTYPE *yylsp;
  106. #endif
  107.  
  108.   int yymaxdepth = YYMAXDEPTH;
  109.  
  110. #ifndef YYPURE
  111.   int yychar;
  112.   YYSTYPE yylval;
  113.   YYLTYPE yylloc;
  114.   int yynerrs;
  115. #endif
  116.  
  117.   YYSTYPE yyval;        /*  the variable used to return        */
  118.                 /*  semantic values from the action    */
  119.                 /*  routines                */
  120.  
  121.   int yylen;
  122.  
  123. #if YYDEBUG != 0
  124.   if (yydebug)
  125.     fprintf(stderr, "Starting parse\n");
  126. #endif
  127.  
  128.   yystate = 0;
  129.   yyerrstatus = 0;
  130.   yynerrs = 0;
  131.   yychar = YYEMPTY;        /* Cause a token to be read.  */
  132.  
  133.   /* Initialize stack pointers.
  134.      Waste one element of value and location stack
  135.      so that they stay on the same level as the state stack.  */
  136.  
  137.   yyssp = yyss - 1;
  138.   yyvsp = yyvs;
  139.  
  140. #ifdef YYLSP_NEEDED
  141.   yylsp = yyls;
  142. #endif
  143.  
  144. /* Push a new state, which is found in  yystate.  */
  145. /* In all cases, when you get here, the value and location stacks
  146.    have just been pushed. so pushing a state here evens the stacks.  */
  147. yynewstate:
  148.  
  149.   *++yyssp = yystate;
  150.  
  151.   if (yyssp >= yyss + yymaxdepth - 1)
  152.     {
  153.       /* Get the current used size of the three stacks, in elements.  */
  154.       int size = yyssp - yyss + 1;
  155.  
  156. #ifdef yyoverflow
  157.       /* Give user a chance to reallocate the stack */
  158.       /* Use copies of these so that the &'s don't force the real ones into memory. */
  159.       YYSTYPE *yyvs1 = yyvs;
  160.       short *yyss1 = yyss;
  161.  
  162. #ifdef YYLSP_NEEDED
  163.       YYLTYPE *yyls1 = yyls;
  164.       int yylssz = size * sizeof (*yylsp);
  165. #else
  166.       void *yyls1 = NULL;
  167.       int yylssz = 0;
  168. #endif
  169.  
  170.       /* Each stack pointer address is followed by the size of
  171.      the data in use in that stack, in bytes.  */
  172.       yyoverflow("parser stack overflow",
  173.          &yyss1, size * sizeof (*yyssp),
  174.          &yyvs1, size * sizeof (*yyvsp),
  175.          &yyls1, yylssz,
  176.          &yymaxdepth);
  177.  
  178.       yyss = yyss1; yyvs = yyvs1;
  179.  
  180. #ifdef YYLSP_NEEDED
  181.       yyls = yyls1;
  182. #endif
  183.  
  184. #else /* no yyoverflow */
  185.       /* Extend the stack our own way.  */
  186.       if (yymaxdepth >= YYMAXLIMIT)
  187.     yyerror("parser stack overflow");
  188.       yymaxdepth *= 2;
  189.       if (yymaxdepth > YYMAXLIMIT)
  190.     yymaxdepth = YYMAXLIMIT;
  191.       yyss = (short *)   realloc (yyss, yymaxdepth * sizeof (*yyssp));
  192.       if (yyss == 0)
  193.     yyerror("parser stack overflow");
  194.       yyvs = (YYSTYPE *) realloc (yyvs, yymaxdepth * sizeof (*yyvsp));
  195.       if (yyvs == 0)
  196.     yyerror("parser stack overflow");
  197.  
  198. #ifdef YYLSP_NEEDED
  199.       yyls = (YYLTYPE *) realloc (yyls, yymaxdepth * sizeof (*yylsp));
  200.       if (yyls == 0)
  201.     yyerror("parser stack overflow");
  202. #endif
  203.  
  204. #endif /* no yyoverflow */
  205.  
  206.       yyssp = yyss + size - 1;
  207.       yyvsp = yyvs + size - 1;
  208. #ifdef YYLSP_NEEDED
  209.       yylsp = yyls + size - 1;
  210. #endif
  211.  
  212. #if YYDEBUG != 0
  213.       if (yydebug)
  214.     fprintf(stderr, "Stack size increased to %d\n", yymaxdepth);
  215. #endif
  216.  
  217.       if (yyssp >= yyss + yymaxdepth - 1)
  218.     YYABORT;
  219.     }
  220.  
  221. #if YYDEBUG != 0
  222.   if (yydebug)
  223.     fprintf(stderr, "Entering state %d\n", yystate);
  224. #endif
  225.  
  226. /* Do appropriate processing given the current state.  */
  227. /* Read a lookahead token if we need one and don't already have one.  */
  228.  
  229.   /* First try to decide what to do without reference to lookahead token.  */
  230.  
  231.   yyn = yypact[yystate];
  232.   if (yyn == YYFLAG)
  233.     goto yydefault;
  234.  
  235.   /* Not known => get a lookahead token if don't already have one.  */
  236.  
  237.   /* yychar is either YYEMPTY or YYEOF
  238.      or a valid token in external form.  */
  239.  
  240.   if (yychar == YYEMPTY)
  241.     {
  242. #if YYDEBUG != 0
  243.       if (yydebug)
  244.     fprintf(stderr, "Reading a token: ");
  245. #endif
  246.       yychar = YYLEX;
  247.     }
  248.  
  249.   /* Convert token to internal form (in yychar1) for indexing tables with */
  250.  
  251.   if (yychar <= 0)        /* This means end of input. */
  252.     {
  253.       yychar1 = 0;
  254.       yychar = YYEOF;        /* Don't call YYLEX any more */
  255.  
  256. #if YYDEBUG != 0
  257.       if (yydebug)
  258.     fprintf(stderr, "Now at end of input.\n");
  259. #endif
  260.     }
  261.   else
  262.     {
  263.       yychar1 = YYTRANSLATE(yychar);
  264.  
  265. #if YYDEBUG != 0
  266.       if (yydebug)
  267.     fprintf(stderr, "Next token is %d (%s)\n", yychar, yytname[yychar1]);
  268. #endif
  269.     }
  270.  
  271.   yyn += yychar1;
  272.   if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != yychar1)
  273.     goto yydefault;
  274.  
  275.   yyn = yytable[yyn];
  276.  
  277.   /* yyn is what to do for this token type in this state.
  278.      Negative => reduce, -yyn is rule number.
  279.      Positive => shift, yyn is new state.
  280.        New state is final state => don't bother to shift,
  281.        just return success.
  282.      0, or most negative number => error.  */
  283.  
  284.   if (yyn < 0)
  285.     {
  286.       if (yyn == YYFLAG)
  287.     goto yyerrlab;
  288.       yyn = -yyn;
  289.       goto yyreduce;
  290.     }
  291.   else if (yyn == 0)
  292.     goto yyerrlab;
  293.  
  294.   if (yyn == YYFINAL)
  295.     YYACCEPT;
  296.  
  297.   /* Shift the lookahead token.  */
  298.  
  299. #if YYDEBUG != 0
  300.   if (yydebug)
  301.     fprintf(stderr, "Shifting token %d (%s), ", yychar, yytname[yychar1]);
  302. #endif
  303.  
  304.   /* Discard the token being shifted unless it is eof.  */
  305.   if (yychar != YYEOF)
  306.     yychar = YYEMPTY;
  307.  
  308.   *++yyvsp = yylval;
  309. #ifdef YYLSP_NEEDED
  310.   *++yylsp = yylloc;
  311. #endif
  312.  
  313.   /* count tokens shifted since error; after three, turn off error status.  */
  314.   if (yyerrstatus) yyerrstatus--;
  315.  
  316.   yystate = yyn;
  317.   goto yynewstate;
  318.  
  319. /* Do the default action for the current state.  */
  320. yydefault:
  321.  
  322.   yyn = yydefact[yystate];
  323.   if (yyn == 0)
  324.     goto yyerrlab;
  325.  
  326. /* Do a reduction.  yyn is the number of a rule to reduce with.  */
  327. yyreduce:
  328.   yylen = yyr2[yyn];
  329.   yyval = yyvsp[1-yylen]; /* implement default value of the action */
  330.  
  331. #if YYDEBUG != 0
  332.   if (yydebug)
  333.     {
  334.       if (yylen == 1)
  335.     fprintf (stderr, "Reducing 1 value via line %d, ",
  336.          yyrline[yyn]);
  337.       else
  338.     fprintf (stderr, "Reducing %d values via line %d, ",
  339.          yylen, yyrline[yyn]);
  340.     }
  341. #endif
  342.  
  343. $   /* the action file gets copied in in place of this dollarsign */
  344. #line 322 "bison.simple"
  345.  
  346.   yyvsp -= yylen;
  347.   yyssp -= yylen;
  348. #ifdef YYLSP_NEEDED
  349.   yylsp -= yylen;
  350. #endif
  351.  
  352. #if YYDEBUG != 0
  353.   if (yydebug)
  354.     {
  355.       short *ssp1 = yyss - 1;
  356.       fprintf (stderr, "state stack now");
  357.       while (ssp1 != yyssp)
  358.     fprintf (stderr, " %d", *++ssp1);
  359.       fprintf (stderr, "\n");
  360.     }
  361. #endif
  362.  
  363.   *++yyvsp = yyval;
  364.  
  365. #ifdef YYLSP_NEEDED
  366.   yylsp++;
  367.   if (yylen == 0)
  368.     {
  369.       yylsp->first_line = yylloc.first_line;
  370.       yylsp->first_column = yylloc.first_column;
  371.       yylsp->last_line = (yylsp-1)->last_line;
  372.       yylsp->last_column = (yylsp-1)->last_column;
  373.       yylsp->text = 0;
  374.     }
  375.   else
  376.     {
  377.       yylsp->last_line = (yylsp+yylen-1)->last_line;
  378.       yylsp->last_column = (yylsp+yylen-1)->last_column;
  379.     }
  380. #endif
  381.  
  382.   /* Now "shift" the result of the reduction.
  383.      Determine what state that goes to,
  384.      based on the state we popped back to
  385.      and the rule number reduced by.  */
  386.  
  387.   yyn = yyr1[yyn];
  388.  
  389.   yystate = yypgoto[yyn - YYNTBASE] + *yyssp;
  390.   if (yystate >= 0 && yystate <= YYLAST && yycheck[yystate] == *yyssp)
  391.     yystate = yytable[yystate];
  392.   else
  393.     yystate = yydefgoto[yyn - YYNTBASE];
  394.  
  395.   goto yynewstate;
  396.  
  397. yyerrlab:   /* here on detecting error */
  398.  
  399.   if (! yyerrstatus)
  400.     /* If not already recovering from an error, report this error.  */
  401.     {
  402.       ++yynerrs;
  403.       yyerror("parse error");
  404.     }
  405.  
  406.   if (yyerrstatus == 3)
  407.     {
  408.       /* if just tried and failed to reuse lookahead token after an error, discard it.  */
  409.  
  410.       /* return failure if at end of input */
  411.       if (yychar == YYEOF)
  412.     YYABORT;
  413.  
  414. #if YYDEBUG != 0
  415.       if (yydebug)
  416.     fprintf(stderr, "Discarding token %d (%s).\n", yychar, yytname[yychar1]);
  417. #endif
  418.  
  419.       yychar = YYEMPTY;
  420.     }
  421.  
  422.   /* Else will try to reuse lookahead token
  423.      after shifting the error token.  */
  424.  
  425.   yyerrstatus = 3;        /* Each real token shifted decrements this */
  426.  
  427.   goto yyerrhandle;
  428.  
  429. yyerrdefault:  /* current state does not do anything special for the error token. */
  430.  
  431. #if 0
  432.   /* This is wrong; only states that explicitly want error tokens
  433.      should shift them.  */
  434.   yyn = yydefact[yystate];  /* If its default is to accept any token, ok.  Otherwise pop it.*/
  435.   if (yyn) goto yydefault;
  436. #endif
  437.  
  438. yyerrpop:   /* pop the current state because it cannot handle the error token */
  439.  
  440.   if (yyssp == yyss) YYABORT;
  441.   yyvsp--;
  442.   yystate = *--yyssp;
  443. #ifdef YYLSP_NEEDED
  444.   yylsp--;
  445. #endif
  446.  
  447. #if YYDEBUG != 0
  448.   if (yydebug)
  449.     {
  450.       short *ssp1 = yyss - 1;
  451.       fprintf (stderr, "Error: state stack now");
  452.       while (ssp1 != yyssp)
  453.     fprintf (stderr, " %d", *++ssp1);
  454.       fprintf (stderr, "\n");
  455.     }
  456. #endif
  457.  
  458. yyerrhandle:
  459.  
  460.   yyn = yypact[yystate];
  461.   if (yyn == YYFLAG)
  462.     goto yyerrdefault;
  463.  
  464.   yyn += YYTERROR;
  465.   if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != YYTERROR)
  466.     goto yyerrdefault;
  467.  
  468.   yyn = yytable[yyn];
  469.   if (yyn < 0)
  470.     {
  471.       if (yyn == YYFLAG)
  472.     goto yyerrpop;
  473.       yyn = -yyn;
  474.       goto yyreduce;
  475.     }
  476.   else if (yyn == 0)
  477.     goto yyerrpop;
  478.  
  479.   if (yyn == YYFINAL)
  480.     YYACCEPT;
  481.  
  482. #if YYDEBUG != 0
  483.   if (yydebug)
  484.     fprintf(stderr, "Shifting error token, ");
  485. #endif
  486.  
  487.   *++yyvsp = yylval;
  488. #ifdef YYLSP_NEEDED
  489.   *++yylsp = yylloc;
  490. #endif
  491.  
  492.   yystate = yyn;
  493.   goto yynewstate;
  494. }
  495.