bison-patches
[Top][All Lists]
Advanced

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

[PATCH 1/3] grammar: preserve token declaration order


From: Valentin Tolmer
Subject: [PATCH 1/3] grammar: preserve token declaration order
Date: Fri, 25 Jan 2013 11:12:47 +0100

In a declaration %token A B, the token A is declared before B, but in %left
A B (or with %precedence or %nonassoc or %right), the token B was declared
before A (tokens were declared in reverse order).

* src/symlist.h, src/symlist.c (symbol_list_append): New.
* src/parse-gram.y: Use it instead of symbol_list_prepend.
* tests/input.at: Adjust expectations.
---
 NEWS             |    6 ++++++
 src/parse-gram.y |    6 +++---
 src/symlist.c    |   17 +++++++++++++++++
 src/symlist.h    |    3 +++
 tests/input.at   |    8 ++++----
 5 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/NEWS b/NEWS
index a44e1be..5b77c55 100644
--- a/NEWS
+++ b/NEWS
@@ -246,6 +246,12 @@ GNU Bison NEWS
   It used to be an error only if used in non GLR mode, _and_ if there are
   reduce/reduce conflicts.
 
+** Token numbering has changed to preserve the user-defined order
+  
+  When declaring %token A B, the numbering for A is inferior to B. Up to now,
+  when declaring associativity at the same time, with %left (or %right,
+  %precedence, %nonassoc), B was inferior to A.
+
 * Noteworthy changes in release 2.7 (2012-12-12) [stable]
 
 ** Bug fixes
diff --git a/src/parse-gram.y b/src/parse-gram.y
index 6e58835..cde15b9 100644
--- a/src/parse-gram.y
+++ b/src/parse-gram.y
@@ -503,7 +503,7 @@ symbols.prec:
   symbol.prec
     { $$ = symbol_list_sym_new ($1, @1); }
 | symbols.prec symbol.prec
-    { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
+    { $$ = symbol_list_append ($1, symbol_list_sym_new ($2, @2)); }
 ;
 
 symbol.prec:
@@ -516,12 +516,12 @@ symbols.1:
   symbol
     { $$ = symbol_list_sym_new ($1, @1); }
 | symbols.1 symbol
-    { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
+    { $$ = symbol_list_append ($1, symbol_list_sym_new ($2, @2)); }
 ;
 
 generic_symlist:
   generic_symlist_item { $$ = $1; }
-| generic_symlist generic_symlist_item { $$ = symbol_list_prepend ($1, $2); }
+| generic_symlist generic_symlist_item { $$ = symbol_list_append ($1, $2); }
 ;
 
 generic_symlist_item:
diff --git a/src/symlist.c b/src/symlist.c
index 300eeda..7ebf1d7 100644
--- a/src/symlist.c
+++ b/src/symlist.c
@@ -107,6 +107,23 @@ symbol_list_prepend (symbol_list *list, symbol_list *node)
 }
 
 
+/*-------------------------.
+| Append NODE to the LIST. |
+`-------------------------*/
+
+symbol_list *
+symbol_list_append (symbol_list *list, symbol_list *node)
+{
+  if (!list)
+    return node;
+  symbol_list *next = list;
+  while (next->next)
+    next = next->next;
+  next->next = node;
+  return list;
+}
+
+
 /*-----------------------------------------------.
 | Free the LIST, but not the items it contains.  |
 `-----------------------------------------------*/
diff --git a/src/symlist.h b/src/symlist.h
index 03ffebe..0154927 100644
--- a/src/symlist.h
+++ b/src/symlist.h
@@ -97,6 +97,9 @@ void symbol_list_syms_print (const symbol_list *l, FILE *f);
 /** Prepend \c node to \c list.  */
 symbol_list *symbol_list_prepend (symbol_list *list, symbol_list *node);
 
+/** Append \c node to \c list.  */
+symbol_list *symbol_list_append (symbol_list *list, symbol_list *node);
+
 /** Free \c list, but not the items it contains.  */
 void symbol_list_free (symbol_list *list);
 
diff --git a/tests/input.at b/tests/input.at
index 44a2477..7e5676a 100644
--- a/tests/input.at
+++ b/tests/input.at
@@ -383,14 +383,14 @@ input.y:5.10-24: error: %printer redeclaration for 
<field2>
 input.y:2.10-24:     previous declaration
 input.y:5.10-24: error: %printer redeclaration for <field2>
 input.y:5.10-24:     previous declaration
-input.y:11.13-29: error: %destructor redeclaration for <field1>
-input.y:4.13-29:      previous declaration
 input.y:11.13-29: error: %destructor redeclaration for <field2>
 input.y:1.13-29:      previous declaration
-input.y:12.10-24: error: %printer redeclaration for <field1>
-input.y:2.10-24:      previous declaration
+input.y:11.13-29: error: %destructor redeclaration for <field1>
+input.y:4.13-29:      previous declaration
 input.y:12.10-24: error: %printer redeclaration for <field2>
 input.y:5.10-24:      previous declaration
+input.y:12.10-24: error: %printer redeclaration for <field1>
+input.y:2.10-24:      previous declaration
 ]])
 
 AT_CLEANUP
-- 
1.7.9.5




reply via email to

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