bison-patches
[Top][All Lists]
Advanced

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

Re: Bison patches


From: Paul Eggert
Subject: Re: Bison patches
Date: 30 Sep 2003 13:14:59 -0700
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3

Frank Heckenbach <address@hidden> writes:

> The default-precedence patch
> <http://mail.gnu.org/archive/html/bison-patches/2003-05/msg00017.html>
> still doesn't seem to be included in the current CVS.

Thanks for pinging us.  I revamped it for the current Bison CVS and
installed the following.  The main changes are to the documentation,
and that a semicolon is required after the directive now.

2003-09-30  Frank Heckenbach  <address@hidden>
        and Paul Eggert  <address@hidden>

        * doc/bison.texinfo (Decl Summary, Contextual Precedence,
        Table of Symbols): Document %default-prec.
        * src/parse-gram.y (PERCENT_DEFAULT_PREC): New token.
        (grammar_declaration): Set default_prec on %default-prec.
        * src/scan-gram.l (%default-prec): New token.
        * src/reader.h (default_prec): New flag.
        * src/reader.c: Likewise.
        (packgram): Handle it.
        * tests/conflicts.at (%default-prec without %prec,
        %default-prec with %prec, %default-prec 1): New tests.
        
Index: doc/bison.texinfo
===================================================================
RCS file: /cvsroot/bison/bison/doc/bison.texinfo,v
retrieving revision 1.111
diff -p -u -r1.111 bison.texinfo
--- doc/bison.texinfo   25 Aug 2003 10:35:38 -0000      1.111
+++ doc/bison.texinfo   30 Sep 2003 19:56:50 -0000
@@ -3679,9 +3679,15 @@ Declare a terminal symbol (token type na
 
 @deffn {Directive} %nonassoc
 Declare a terminal symbol (token type name) that is nonassociative
-(using it in a way that would be associative is a syntax error)
address@hidden deffn
 (@pxref{Precedence Decl, ,Operator Precedence}).
+Using it in a way that would be associative is a syntax error.
address@hidden deffn
+
address@hidden {Directive} %default-prec
+Specify whether to assign a precedence to rules lacking an
+explicit @code{%prec} modifier
+(@pxref{Contextual Precedence, ,Context-Dependent Precedence}).
address@hidden deffn
 
 @deffn {Directive} %type
 Declare the type of semantic values for a nonterminal symbol
@@ -4851,6 +4857,26 @@ exp:    @dots{}
 @end group
 @end example
 
+If you forget to append @code{%prec UMINUS} to the rule for unary
+minus, Bison silently assumes that minus has its usual precedence.
+This kind of problem can be tricky to debug, since one typically
+discovers the mistake only by testing the code.
+
+The @code{%default-prec 0;} declaration makes it easier to discover
+this kind of problem systematically.  It causes rules that lack a
address@hidden modifier to have no precedence, even if the last terminal
+symbol mentioned in their components has a declared precedence.
+
+If @code{%default-prec 0;} is in effect, you must specify @code{%prec}
+for all rules that participate in precedence conflict resolution.
+Then you will see any shift/reduce conflict until you tell Bison how
+to resolve it, either by changing your grammar or by adding an
+explicit precedence.  This will probably add declarations to the
+grammar, but it helps to protect against incorrect rule precedences.
+
+The effect of @code{%default-prec 0;} can be reversed by giving
address@hidden 1;}, which is the default.
+
 @node Parser States
 @section Parser States
 @cindex finite-state machine
@@ -6780,6 +6806,12 @@ parsing.  @xref{Parser Function, ,The Pa
 
 @deffn {Directive} %debug
 Equip the parser for debugging.  @xref{Decl Summary}.
address@hidden deffn
+
address@hidden {Directive} %default-prec @var{state};
+Bison declaration to specify whether to assign a precedence to rules
+that lack an explicit @samp{%prec} modifier.  @xref{Contextual
+Precedence, ,Context-Dependent Precedence}.
 @end deffn
 
 @deffn {Directive} %defines
Index: src/parse-gram.y
===================================================================
RCS file: /cvsroot/bison/bison/src/parse-gram.y,v
retrieving revision 1.43
diff -p -u -r1.43 parse-gram.y
--- src/parse-gram.y    25 Aug 2003 15:16:24 -0000      1.43
+++ src/parse-gram.y    30 Sep 2003 19:56:51 -0000
@@ -115,6 +115,7 @@ int current_prec = 0;
 
 %token
   PERCENT_DEBUG           "%debug"
+  PERCENT_DEFAULT_PREC    "%default-prec"
   PERCENT_DEFINE          "%define"
   PERCENT_DEFINES         "%defines"
   PERCENT_ERROR_VERBOSE   "%error-verbose"
@@ -239,6 +240,13 @@ grammar_declaration:
       for (list = $2; list; list = list->next)
        symbol_printer_set (list->sym, $1, list->location);
       symbol_list_free ($2);
+    }
+| "%default-prec" INT
+    {
+      if (0 <= $2 && $2 <= 1)
+       default_prec = $2;
+      else
+        complain_at (@1, _("invalid value for `%default-prec'"));
     }
 ;
 
Index: src/reader.c
===================================================================
RCS file: /cvsroot/bison/bison/src/reader.c,v
retrieving revision 1.233
diff -p -u -r1.233 reader.c
--- src/reader.c        25 Aug 2003 15:16:25 -0000      1.233
+++ src/reader.c        30 Sep 2003 19:56:51 -0000
@@ -41,6 +41,9 @@ merger_list *merge_functions;
 
 /* Has %union been seen?  */
 bool typed = false;
+
+/* Should rules have a default precedence?  */
+bool default_prec = true;
 
 /*-----------------------.
 | Set the start symbol.  |
@@ -409,7 +412,7 @@ packgram (void)
          ritem[itemno++] = symbol_number_as_item_number (p->sym->number);
          /* A rule gets by default the precedence and associativity
             of the last token in it.  */
-         if (p->sym->class == token_sym)
+         if (p->sym->class == token_sym && default_prec)
            rules[ruleno].prec = p->sym;
          if (p)
            p = p->next;
Index: src/reader.h
===================================================================
RCS file: /cvsroot/bison/bison/src/reader.h,v
retrieving revision 1.41
diff -p -u -r1.41 reader.h
--- src/reader.h        25 Aug 2003 15:16:25 -0000      1.41
+++ src/reader.h        30 Sep 2003 19:56:51 -0000
@@ -73,5 +73,6 @@ void free_merger_functions (void);
 extern merger_list *merge_functions;
 
 extern bool typed;
+extern bool default_prec;
 
 #endif /* !READER_H_ */
Index: src/scan-gram.l
===================================================================
RCS file: /cvsroot/bison/bison/src/scan-gram.l,v
retrieving revision 1.63
diff -p -u -r1.63 scan-gram.l
--- src/scan-gram.l     25 Aug 2003 15:16:25 -0000      1.63
+++ src/scan-gram.l     30 Sep 2003 19:56:52 -0000
@@ -182,6 +182,7 @@ splice       (\\[ \f\t\v]*\n)*
 {
   "%binary"               return PERCENT_NONASSOC;
   "%debug"                return PERCENT_DEBUG;
+  "%default"[-_]"prec"    return PERCENT_DEFAULT_PREC;
   "%define"               return PERCENT_DEFINE;
   "%defines"              return PERCENT_DEFINES;
   "%destructor"                  token_type = PERCENT_DESTRUCTOR; BEGIN 
SC_PRE_CODE;
Index: tests/conflicts.at
===================================================================
RCS file: /cvsroot/bison/bison/tests/conflicts.at,v
retrieving revision 1.25
diff -p -u -r1.25 conflicts.at
--- tests/conflicts.at  13 Jan 2003 06:41:28 -0000      1.25
+++ tests/conflicts.at  30 Sep 2003 19:56:52 -0000
@@ -548,3 +548,77 @@ AT_CHECK([bison -o input.c input.y], 0, 
 input.y: warning: expected 0 reduce/reduce conflicts
 ])
 AT_CLEANUP
+
+
+## ---------------------------- ##
+## %default-prec without %prec  ##
+## ---------------------------- ##
+
+AT_SETUP([%default-prec without %prec])
+
+AT_DATA([[input.y]],
+[[%left '+'
+%left '*'
+
+%%
+
+%default-prec 0;
+
+e:   e '+' e
+   | e '*' e
+   | '0'
+   ;
+]])
+
+AT_CHECK([bison -o input.c input.y], 0, [],
+[[input.y: conflicts: 4 shift/reduce
+]])
+AT_CLEANUP
+
+
+## ------------------------- ##
+## %default-prec with %prec  ##
+## ------------------------- ##
+
+AT_SETUP([%default-prec with %prec])
+
+AT_DATA([[input.y]],
+[[%left '+'
+%left '*'
+
+%%
+
+%default-prec 0;
+
+e:   e '+' e %prec '+'
+   | e '*' e %prec '*'
+   | '0'
+   ;
+]])
+
+AT_CHECK([bison -o input.c input.y])
+AT_CLEANUP
+
+
+## ---------------- ##
+## %default-prec 1  ##
+## ---------------- ##
+
+AT_SETUP([%default-prec 1])
+
+AT_DATA([[input.y]],
+[[%left '+'
+%left '*'
+
+%%
+
+%default-prec 1;
+
+e:   e '+' e
+   | e '*' e
+   | '0'
+   ;
+]])
+
+AT_CHECK([bison -o input.c input.y])
+AT_CLEANUP




reply via email to

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