bison-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] master: lalr1.cc: get rid of yyparse's yystate.


From: Akim Demaille
Subject: [PATCH] master: lalr1.cc: get rid of yyparse's yystate.
Date: Sun, 16 Aug 2009 07:36:39 +0200

yystate and yystack_[0].state are equal, keep only the latter.
The former was also used as a temporary variable to compute the
post-reduction state.  Move this computation into an auxiliary
function.

        * data/glr.c (yyLRgotoState): Fuse variable definition and first
        assignment.
        * data/lalr1.cc (yy_lr_goto_state_): New.
        (yyparse): Use it.
        Replace remaining uses of yystate by yystate_[0].state.
        Remove the former.
---
 ChangeLog     |   15 +++++++++++++++
 data/glr.c    |    3 +--
 data/lalr1.cc |   48 +++++++++++++++++++++++++-----------------------
 3 files changed, 41 insertions(+), 25 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 39575a3..f5da787 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,20 @@
 2009-08-19  Akim Demaille  <address@hidden>
 
+       lalr1.cc: get rid of yyparse's yystate.
+       yystate and yystack_[0].state are equal, keep only the latter.
+       The former was also used as a temporary variable to compute the
+       post-reduction state.  Move this computation into an auxiliary
+       function.
+
+       * data/glr.c (yyLRgotoState): Fuse variable definition and first
+       assignment.
+       * data/lalr1.cc (yy_lr_goto_state_): New.
+       (yyparse): Use it.
+       Replace remaining uses of yystate by yystate_[0].state.
+       Remove the former.
+
+2009-08-19  Akim Demaille  <address@hidden>
+
        lalr1.cc: destroy $$ when YYERROR is called.
        * data/lalr1.cc (yyreduce): Compute the resulting state before
        running the user action so that yylhs is a valid symbol.
diff --git a/data/glr.c b/data/glr.c
index d67ec42..a4b921f 100644
--- a/data/glr.c
+++ b/data/glr.c
@@ -1011,8 +1011,7 @@ yygetLRActions (yyStateNum yystate, int yytoken,
 static inline yyStateNum
 yyLRgotoState (yyStateNum yystate, yySymbol yylhs)
 {
-  int yyr;
-  yyr = yypgoto[yylhs - YYNTOKENS] + yystate;
+  int yyr = yypgoto[yylhs - YYNTOKENS] + yystate;
   if (0 <= yyr && yyr <= YYLAST && yycheck[yyr] == yystate)
     return yytable[yyr];
   else
diff --git a/data/lalr1.cc b/data/lalr1.cc
index 365a384..09fedab 100644
--- a/data/lalr1.cc
+++ b/data/lalr1.cc
@@ -240,6 +240,11 @@ do {                                                       
     \
     /// State numbers.
     typedef int state_type;
 
+    /// Compute post-reduction state.
+    /// \param yystate   the current state
+    /// \param yylhs     the nonterminal to push on the stack
+    state_type yy_lr_goto_state_ (state_type yystate, int yylhs);
+
     /// Internal symbol numbers.
     typedef ]b4_int_type_for([b4_translate])[ token_number_type;
     static const ]b4_int_type(b4_pact_ninf, b4_pact_ninf)[ yypact_ninf_;
@@ -636,6 +641,16 @@ b4_percent_code_get[]dnl
   }
 #endif
 
+  inline ]b4_parser_class_name[::state_type
+  ]b4_parser_class_name[::yy_lr_goto_state_ (state_type yystate, int yylhs)
+  {
+    int yyr = yypgoto_[yylhs - yyntokens_] + yystate;
+    if (0 <= yyr && yyr <= yylast_ && yycheck_[yyr] == yystate)
+      return yytable_[yyr];
+    else
+      return yydefgoto_[yylhs - yyntokens_];
+  }
+
   int
   ]b4_parser_class_name[::parse ()
   {
@@ -645,7 +660,6 @@ b4_percent_code_get[]dnl
     /* State.  */
     int yyn;
     int yylen = 0;
-    int yystate = 0;
 
     /* Error handling.  */
     int yynerrs_ = 0;
@@ -680,14 +694,12 @@ m4_popdef([b4_at_dollar])])dnl
     yystack_ = stack_type (0);
     yypush_ (0, 0, yyla);
 
-    // A new state was pushed on the stack.
-    // Invariant: yystate == yystack_[0].state, i.e.,
-    // yystate was just pushed onto the state stack.
+    // A new symbol was pushed on the stack.
   yynewstate:
-    YYCDEBUG << "Entering state " << yystate << std::endl;
+    YYCDEBUG << "Entering state " << yystack_[0].state << std::endl;
 
     /* Accept?  */
-    if (yystate == yyfinal_)
+    if (yystack_[0].state == yyfinal_)
       goto yyacceptlab;
 
     goto yybackup;
@@ -696,7 +708,7 @@ m4_popdef([b4_at_dollar])])dnl
   yybackup:
 
     /* Try to take a decision without lookahead.  */
-    yyn = yypact_[yystate];
+    yyn = yypact_[yystack_[0].state];
     if (yyn == yypact_ninf_)
       goto yydefault;
 
@@ -740,15 +752,14 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param)));])[
       --yyerrstatus_;
 
     /* Shift the lookahead token.  */
-    yystate = yyn;
-    yypush_ ("Shifting", yystate, yyla);
+    yypush_ ("Shifting", yyn, yyla);
     goto yynewstate;
 
   /*-----------------------------------------------------------.
   | yydefault -- do the default action for the current state.  |
   `-----------------------------------------------------------*/
   yydefault:
-    yyn = yydefact_[yystate];
+    yyn = yydefact_[yystack_[0].state];
     if (yyn == 0)
       goto yyerrlab;
     goto yyreduce;
@@ -758,14 +769,7 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param)));])[
   `-----------------------------*/
   yyreduce:
     yylen = yyr2_[yyn];
-    // Compute post-reduction state.
-    yystate = yypgoto_[yyr1_[yyn] - yyntokens_] + yystack_[yylen].state;
-    if (0 <= yystate && yystate <= yylast_
-       && yycheck_[yystate] == yystack_[yylen].state)
-      yystate = yytable_[yystate];
-    else
-      yystate = yydefgoto_[yyr1_[yyn] - yyntokens_];
-    yylhs.state = yystate;]b4_variant_if([
+    yylhs.state = yy_lr_goto_state_(yystack_[yylen].state, 
yyr1_[yyn]);]b4_variant_if([
     /* Variants are always initialized to an empty instance of the
        correct type. The default $$=$1 action is NOT applied when using
        variants.  */
@@ -829,7 +833,7 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param)));])[
       {
        ++yynerrs_;
        error (]b4_args(b4_locations_if([yyla.location]),
-                        [yysyntax_error_ (yystate, yyla.type)])[);
+                        [[yysyntax_error_ (yystack_[0].state, yyla.type)]])[);
       }
 
 ]b4_locations_if([[
@@ -871,7 +875,6 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param)));])[
        this YYERROR.  */
     yypop_ (yylen);
     yylen = 0;
-    yystate = yystack_[0].state;
     goto yyerrlab1;
 
   /*-------------------------------------------------------------.
@@ -883,7 +886,7 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param)));])[
       stack_symbol_type error_token;
       for (;;)
         {
-          yyn = yypact_[yystate];
+          yyn = yypact_[yystack_[0].state];
           if (yyn != yypact_ninf_)
             {
               yyn += yyterror_;
@@ -902,7 +905,6 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param)));])[
           yyerror_range[0].location = yystack_[0].location;]])[
           yy_destroy_ ("Error: popping", yystack_[0]);
           yypop_ ();
-          yystate = yystack_[0].state;
           YY_STACK_PRINT ();
         }
 ]b4_locations_if([[
@@ -910,7 +912,7 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param)));])[
       YYLLOC_DEFAULT (error_token.location, (yyerror_range - 1), 2);]])[
 
       /* Shift the error token.  */
-      error_token.state = yystate = yyn;
+      error_token.state = yyn;
       yypush_ ("Shifting", error_token);
     }
     goto yynewstate;
-- 
1.6.4





reply via email to

[Prev in Thread] Current Thread [Next in Thread]