bison-patches
[Top][All Lists]
Advanced

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

FYI: %initial-action is about the look-ahead


From: Akim Demaille
Subject: FYI: %initial-action is about the look-ahead
Date: Mon, 20 Sep 2004 17:25:20 +0200
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

That was very fuzzy (in my mind too) before, but now I'm convinced
that the initial-action should be about the initial look-ahead value
and location, and these values are those with which the stacks should
be initialized.

Index: ChangeLog
from  Akim Demaille  <address@hidden>

        Let the initial-action act on the look-ahead, and use it for the
        "initial push" (corresponding to an hypothetical beginning-of-file).
        And let lalr1.cc honor %initial-action.

        * doc/bison.texinfo (Initial Action Decl): Clarify, and add an
        example.
        * data/lalr1.cc (Parser::initlocation_): Remove, bad experiment.
        (Parser::Parser): Remove the ctor that used to initialize it.
        (Parser::parse): Like in the other skeletons, issue the "starting
        parse" message before any action.
        Honor %initial-action.
        Initialize the stacks with the lookahead.
        * data/yacc.c: Let $$ and @$ in %initial-action designate the
        look-ahead.
        Push them in the stacks.
        * tests/actions.at, tests/calc.at: Adjust the C++ ctor invocations.

Index: data/lalr1.cc
===================================================================
RCS file: /cvsroot/bison/bison/data/lalr1.cc,v
retrieving revision 1.56
diff -u -u -r1.56 lalr1.cc
--- data/lalr1.cc 20 Sep 2004 09:32:55 -0000 1.56
+++ data/lalr1.cc 20 Sep 2004 15:23:53 -0000
@@ -218,17 +218,9 @@
     typedef Stack< SemanticType > SemanticStack;
     typedef Stack< LocationType > LocationStack;
 
-#if YYLSP_NEEDED
-    ]b4_parser_class_name[ (bool debug,
-           LocationType initlocation][]b4_param[]b4_parse_param_decl[) :
-      ]b4_constructor[][debug_ (debug),
-      cdebug_ (std::cerr),
-      initlocation_ (initlocation)]b4_parse_param_cons[
-#else
     ]b4_parser_class_name[ (bool debug][]b4_param[]b4_parse_param_decl[) :
       ]b4_constructor[][debug_ (debug),
       cdebug_ (std::cerr)]b4_parse_param_cons[
-#endif
     {
     }
 
@@ -325,18 +317,17 @@
     /* Message.  */
     std::string message;
 
-    /* Semantic value and location of look-ahead token.  */
+    /// Semantic value of the look-ahead.
     SemanticType value;
+    /// Location of the look-ahead.
     LocationType location;
     /// The locations where the error started and ended.
     Location error_range_[2];
 
-    /* @@$ and $$.  */
+    /// $$.
     SemanticType yyval;
+    /// @@$.
     LocationType yyloc;
-
-    /* Initial location.  */
-    LocationType initlocation_;
 ]b4_parse_param_vars[
   };
 }
@@ -446,24 +437,34 @@
 int
 yy::]b4_parser_class_name[::parse ()
 {
+  YYCDEBUG << "Starting parse" << std::endl;
+
   nerrs_ = 0;
   errstatus_ = 0;
 
-  /* Initialize the stacks.  The initial state will be pushed in
+  /* Start.  */
+  state_ = 0;
+  looka_ = empty_;
+
+]m4_ifdef([b4_initial_action], [
+m4_pushdef([b4_at_dollar],     [location])dnl
+m4_pushdef([b4_dollar_dollar], [value])dnl
+  /* User initialization code. */
+  b4_initial_action
+m4_popdef([b4_dollar_dollar])dnl
+m4_popdef([b4_at_dollar])dnl
+/* Line __line__ of yacc.c.  */
+b4_syncline(address@hidden@], address@hidden@])])dnl
+
+[  /* Initialize the stacks.  The initial state will be pushed in
      yynewstate, since the latter expects the semantical and the
      location values to have been already stored, initialize these
      stacks with a primary value.  */
   state_stack_ = StateStack (0);
-  semantic_stack_ = SemanticStack (1);
-  location_stack_ = LocationStack (1);
-
-  /* Start.  */
-  state_ = 0;
-  looka_ = empty_;
-#if YYLSP_NEEDED
-  location = initlocation_;
-#endif
-  YYCDEBUG << "Starting parse" << std::endl;
+  semantic_stack_ = SemanticStack (0);
+  location_stack_ = LocationStack (0);
+  semantic_stack_.push (value);
+  location_stack_.push (location);
 
   /* New state.  */
 yynewstate:
Index: data/yacc.c
===================================================================
RCS file: /cvsroot/bison/bison/data/yacc.c,v
retrieving revision 1.70
diff -u -u -r1.70 yacc.c
--- data/yacc.c 20 Sep 2004 12:24:45 -0000 1.70
+++ data/yacc.c 20 Sep 2004 15:23:53 -0000
@@ -821,20 +821,23 @@
 ]b4_location_if([[  yylsp = yyls;
 #if YYLTYPE_IS_TRIVIAL
   /* Initialize the default location before parsing starts.  */
-  yyls[0].first_line   = yyls[0].last_line   = 1;
-  yyls[0].first_column = yyls[0].last_column = 0;
+  yylloc.first_line   = yylloc.last_line   = 1;
+  yylloc.first_column = yylloc.last_column = 0;
 #endif
 ]])
 m4_ifdef([b4_initial_action], [
-m4_pushdef([b4_at_dollar],     [(*yylsp)])dnl
-m4_pushdef([b4_dollar_dollar], [(*yyvsp)])dnl
+m4_pushdef([b4_at_dollar],     [(yylloc)])dnl
+m4_pushdef([b4_dollar_dollar], [(yylval)])dnl
   /* User initialization code. */
   b4_initial_action
 m4_popdef([b4_dollar_dollar])dnl
 m4_popdef([b4_at_dollar])dnl
 /* Line __line__ of yacc.c.  */
 b4_syncline(address@hidden@], address@hidden@])])dnl
-
+[
+  yyvsp[0] = yylval;
+]b4_location_if([[    yylsp[0] = yylloc;
+]])
 [  goto yysetstate;
 
 /*------------------------------------------------------------.
Index: doc/bison.texinfo
===================================================================
RCS file: /cvsroot/bison/bison/doc/bison.texinfo,v
retrieving revision 1.131
diff -u -u -r1.131 bison.texinfo
--- doc/bison.texinfo 20 Sep 2004 14:29:31 -0000 1.131
+++ doc/bison.texinfo 20 Sep 2004 15:23:54 -0000
@@ -3742,10 +3742,21 @@
 @deffn {Directive} %initial-action @{ @var{code} @}
 @findex %initial-action
 Declare that the @var{code} must be invoked before parsing each time
address@hidden is called.  The @var{code} may use @code{@@$} to
-designate the initial location, and the @code{%parse-param}.
address@hidden is called.  The @var{code} may use @code{$$} and
address@hidden@@$} --- initial value and location of the look-ahead --- and the
address@hidden
 @end deffn
 
+For instance, if your locations use a file name, you may use
+
address@hidden
+%parse-param @{ const char *filename @};
+%initial-action
address@hidden
+  @@$.begin.filename = @@$.end.filename = filename;
address@hidden;
address@hidden example
+
 
 @node Destructor Decl
 @subsection Freeing Discarded Symbols
@@ -3822,7 +3833,7 @@
 @item
 incoming terminals during the second phase of error recovery,
 @item
-the current lookahead when the parser aborts (either via an explicit
+the current look-ahead when the parser aborts (either via an explicit
 call to @code{YYABORT}, or as a consequence of a failed error recovery).
 @end itemize
 
@@ -5579,11 +5590,10 @@
 
 @c FIXME: C++ output.
 Because of semantical differences between C and C++, the
address@hidden(1) parsers
-in C produced by Bison by compiled as C++ cannot grow.  In this precise
-case (compiling a C parser as C++) you are suggested to grow
address@hidden  In the near future, a C++ output output will be
-provided which addresses this issue.
address@hidden(1) parsers in C produced by Bison by compiled as C++
+cannot grow.  In this precise case (compiling a C parser as C++) you are
+suggested to grow @code{YYINITDEPTH}.  In the near future, a C++ output
+output will be provided which addresses this issue.
 
 @node Error Recovery
 @chapter Error Recovery
@@ -6887,19 +6897,18 @@
 How can I generate parsers in C++?
 @end display
 
-We are working on a C++ output for Bison, but unfortunately, for lack
-of time, the skeleton is not finished.  It is functional, but in
-numerous respects, it will require additional work which @emph{might}
-break backward compatibility.  Since the skeleton for C++ is not
-documented, we do not consider ourselves bound to this interface,
-nevertheless, as much as possible we will try to keep compatibility.
-
-Another possibility is to use the regular C parsers, and to compile
-them with a C++ compiler.  This works properly, provided that you bear
-some simple C++ rules in mind, such as not including ``real classes''
-(i.e., structure with constructors) in unions.  Therefore, in the
address@hidden, use pointers to classes, or better yet, a single
-pointer type to the root of your lexical/syntactic hierarchy.
+We are working on a C++ output for Bison, but unfortunately, for lack of
+time, the skeleton is not finished.  It is functional, but in numerous
+respects, it will require additional work which @emph{might} break
+backward compatibility.  Since the skeleton for C++ is not documented,
+we do not consider ourselves bound to this interface, nevertheless, as
+much as possible we will try to keep compatibility.
+
+Another possibility is to use the regular C parsers, and to compile them
+with a C++ compiler.  This works properly, provided that you bear some
+simple C++ rules in mind, such as not including ``real classes'' (i.e.,
+structure with constructors) in unions.  Therefore, in the
address@hidden, use pointers to classes.
 
 
 @node Implementing Gotos/Loops
Index: tests/actions.at
===================================================================
RCS file: /cvsroot/bison/bison/tests/actions.at,v
retrieving revision 1.37
diff -u -u -r1.37 actions.at
--- tests/actions.at 20 Sep 2004 09:32:55 -0000 1.37
+++ tests/actions.at 20 Sep 2004 15:23:54 -0000
@@ -327,7 +327,7 @@
 int
 yyparse ()
 {
-  yy::Parser parser (yydebug, yy::Location ());
+  yy::Parser parser (yydebug);
   return parser.parse ();
 }
 ],
Index: tests/calc.at
===================================================================
RCS file: /cvsroot/bison/bison/tests/calc.at,v
retrieving revision 1.65
diff -u -u -r1.65 calc.at
--- tests/calc.at 16 Sep 2004 14:14:27 -0000 1.65
+++ tests/calc.at 20 Sep 2004 15:23:54 -0000
@@ -144,7 +144,7 @@
 int
 yyparse (AT_PARAM_IF([semantic_value *result, int *count]))
 {
-  yy::Parser parser (!!YYDEBUG[]AT_LOCATION_IF([, yy::Location 
()])AT_PARAM_IF([, result, count]));
+  yy::Parser parser (!!YYDEBUG[]AT_PARAM_IF([, result, count]));
   return parser.parse ();
 }
 ],




reply via email to

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