bison-patches
[Top][All Lists]
Advanced

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

Re: symbol declarations after rules


From: Joel E. Denny
Subject: Re: symbol declarations after rules
Date: Fri, 23 Jun 2006 17:40:07 -0400 (EDT)

On Fri, 23 Jun 2006, Joel E. Denny wrote:

> On Thu, 22 Jun 2006, Joel E. Denny wrote:
> 
> > However, I was imagining a user who just wants to add a 
> > %before-definitions and/or %after-definitions to an existing grammar file.  
> > Should he be forced to convert all his (other) prologue blocks to 
> > %before-header and %after-header?
> > 
> > I guess we could go with the restriction and re-evaluate if someone 
> > complains.
> 
> I'll add this restriction in a separate patch.  That'll make it easier to 
> remember what to do if we want to back it out later.

An uncommitted patch for this restriction is below.

It breaks the test suite in a way that looks annoying to fix.  Of course, 
it's possible to fix the test suite, but I'm taking this as evidence that 
the restriction is more trouble than it's worth.

Again, it seems reasonable that a user may wish to add a %*-header 
declaration to an existing grammar file that already uses Yacc prologues. 
While that won't produce the cleanest grammar file, I think Bison's 
behavior in that case is not too hard to understand:

  Every %{...%} before the first %union appends to the pre-prologue.
  Every %{...%} after the first %union appends to the post-prologue.
  %before-header always appends to the pre-prologue.
  %after-header always appends to the post-prologue.
  %start-header and %end-header always insert code into the header.

What do you think?

Joel

Index: src/parse-gram.y
===================================================================
RCS file: /sources/bison/bison/src/parse-gram.y,v
retrieving revision 1.80
diff -p -u -r1.80 parse-gram.y
--- src/parse-gram.y    23 Jun 2006 20:17:28 -0000      1.80
+++ src/parse-gram.y    23 Jun 2006 21:20:54 -0000
@@ -55,6 +55,10 @@ static char const *char_name (char);
 
 static void add_param (char const *, char *, location);
 
+/* Complain if traditional Yacc prologues are combined with Bison's %*-header
+   declarations.  */
+void check_prologue_style (location, bool);
+
 static symbol_class current_class = unknown_sym;
 static uniqstr current_type = 0;
 static symbol *current_lhs;
@@ -217,18 +221,21 @@ declaration:
 | PROLOGUE
     {
       prologue_augment (translate_code ($1, @1), @1, typed);
+      check_prologue_style (@1, true);
     }
 | "%after-header" "{...}"
     {
       /* Remove the '{', and replace the '}' with '\n'.  */
       $2[strlen ($2) - 1] = '\n';
       prologue_augment (translate_code ($2+1, @2), @2, true);
+      check_prologue_style (@2, false);
     }
 | "%before-header" "{...}"
     {
       /* Remove the '{', and replace the '}' with '\n'.  */
       $2[strlen ($2) - 1] = '\n';
       prologue_augment (translate_code ($2+1, @2), @2, false);
+      check_prologue_style (@2, false);
     }
 | "%debug"                                 { debug_flag = true; }
 | "%define" string_content
@@ -243,6 +250,7 @@ declaration:
       /* Remove the '{', and replace the '}' with '\n'.  */
       $2[strlen ($2) - 1] = '\n';
       muscle_code_grow ("end_header", translate_code ($2+1, @2), @2);
+      check_prologue_style (@2, false);
     }
 | "%error-verbose"                         { error_verbose = true; }
 | "%expect" INT                            { expected_sr_conflicts = $2; }
@@ -272,6 +280,7 @@ declaration:
       /* Remove the '{', and replace the '}' with '\n'.  */
       $2[strlen ($2) - 1] = '\n';
       muscle_code_grow ("start_header", translate_code ($2+1, @2), @2);
+      check_prologue_style (@2, false);
     }
 | "%token-table"                           { token_table_flag = true; }
 | "%verbose"                               { report_flag = report_states; }
@@ -656,3 +665,33 @@ char_name (char c)
       return quotearg_style (escape_quoting_style, buf);
     }
 }
+
+void
+check_prologue_style (location loc, bool yacc)
+{
+  static location previous_loc;
+  static enum { NONE, YACC, HEADER } previous_style = NONE;
+  switch (previous_style)
+    {
+      case NONE:
+       previous_loc = loc;
+       previous_style = yacc ? YACC : HEADER;
+       break;
+      case YACC:
+       if (!yacc)
+         {
+           complain_at (loc, _("%%*-header declaration seen here."));
+           complain_at (previous_loc, _("Yacc prologue seen here."));
+           complain (_("These declaration styles cannot be combined."));
+         }
+       break;
+      case HEADER:
+       if (yacc)
+         {
+           complain_at (loc, _("Yacc prologue seen here."));
+           complain_at (previous_loc, _("%%*-header declaration seen here."));
+           complain (_("These declaration styles cannot be combined."));
+         }
+       break;
+    }
+}




reply via email to

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