bison-patches
[Top][All Lists]
Advanced

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

08-locations.patch


From: Akim Demaille
Subject: 08-locations.patch
Date: Sat, 15 Jun 2002 20:19:17 +0200

Index: ChangeLog
from  Akim Demaille  <address@hidden>
        * src/parse-gram.y (YYLLOC_DEFAULT, current_lhs_location): New.
        (input): Don't try to initialize yylloc here, do it in the
        scanner.
        * src/scan-gram.l (YY_USER_INIT): Initialize yylloc.
        * src/gram.h (rule_t): Change line and action_line into location
        and action_location, of location_t type.
        Adjust all dependencies.
        * src/location.h, src/location.c (empty_location): New.
        * src/reader.h, src/reader.c (grammar_start_symbol_set)
        (grammar_symbol_append, grammar_rule_begin, grammar_rule_end)
        (grammar_current_rule_symbol_append)
        (grammar_current_rule_action_append): Expect a location as argument.
        * src/reader.c (grammar_midrule_action): Adjust to attach an
        action's location as dummy symbol location.
        * src/symtab.h, src/symtab.c (startsymbol_location): New.
        * tests/regression.at (Web2c Report, Rule Line Numbers): Adjust
        the line numbers.
        
        
Index: src/parse-gram.y
--- src/parse-gram.y Fri, 14 Jun 2002 19:36:36 +0200 akim
+++ src/parse-gram.y Fri, 14 Jun 2002 22:49:23 +0200 akim
@@ -40,6 +40,20 @@
 
 /* Produce verbose parse errors.  */
 #define YYERROR_VERBOSE 1
+#define YYLLOC_DEFAULT(Current, Rhs, N)                        \
+do {                                                   \
+  if (N)                                               \
+  {                                                    \
+    Current.first_column  = Rhs[1].first_column;       \
+    Current.first_line    = Rhs[1].first_line;         \
+    Current.last_column   = Rhs[N].last_column;                \
+    Current.last_line     = Rhs[N].last_line;          \
+  }                                                    \
+  else                                                 \
+  {                                                    \
+    Current = Rhs[0];                                  \
+  }                                                    \
+} while (0)
 
 /* Pass the control structure to YYPARSE and YYLEX. */
 #define YYPARSE_PARAM gram_control
@@ -64,6 +78,7 @@
 symbol_class current_class = unknown_sym;
 char *current_type = 0;
 symbol_t *current_lhs;
+location_t current_lhs_location;
 associativity current_assoc;
 int current_prec = 0;
 %}
@@ -128,11 +143,11 @@
 
 %%
 
-input: { LOCATION_RESET (yylloc); }
+input:
   declarations "%%" grammar epilogue.opt
     {
       yycontrol->errcode = 0;
-      epilogue_set ($5, @5);
+      epilogue_set ($4, @4);
     }
 ;
 
@@ -171,7 +186,7 @@ declaration:
 | symbol_declaration
 | "%start" symbol
     {
-      grammar_start_symbol_set ($2);
+      grammar_start_symbol_set ($2, @2);
     }
 | "%union" BRACED_CODE
     {
@@ -298,22 +313,22 @@ grammar:
 ;
 
 rules:
-  ID ":" { current_lhs = $1; } rhses.1 ";"
+  ID ":" { current_lhs = $1; current_lhs_location = @1; } rhses.1 ";"
     {;}
 ;
 
 rhses.1:
-  rhs                { grammar_rule_end (); }
-| rhses.1 "|" rhs    { grammar_rule_end (); }
+  rhs                { grammar_rule_end (@1); }
+| rhses.1 "|" rhs    { grammar_rule_end (@3); }
 ;
 
 rhs:
   /* Nothing.  */
-    { grammar_rule_begin (current_lhs); }
+    { grammar_rule_begin (current_lhs, current_lhs_location); }
 | rhs symbol
-    { grammar_current_rule_symbol_append ($2); }
+    { grammar_current_rule_symbol_append ($2, @2); }
 | rhs action
-    { grammar_current_rule_action_append ($2, @2.first_line); }
+    { grammar_current_rule_action_append ($2, @2); }
 | rhs "%prec" symbol
     { grammar_current_rule_prec_set ($3); }
 ;
Index: src/scan-gram.l
--- src/scan-gram.l Thu, 13 Jun 2002 18:50:44 +0200 akim
+++ src/scan-gram.l Fri, 14 Jun 2002 22:40:55 +0200 akim
@@ -31,6 +31,13 @@
 #include "reader.h"
 
 /* Each time we match a string, move the end cursor to its end. */
+#define YY_USER_INIT                           \
+do {                                           \
+  LOCATION_RESET (*yylloc);                    \
+   /* This is only to avoid GCC warnings. */   \
+  if (yycontrol) {;};                          \
+} while (0)
+
 #define YY_USER_ACTION  LOCATION_COLUMNS (*yylloc, yyleng)
 #define YY_LINES        LOCATION_LINES (*yylloc, yyleng); lineno += yyleng;
 #define YY_STEP         LOCATION_STEP (*yylloc)
@@ -69,9 +76,6 @@
 }
 
 
-/* This is only to avoid GCC warnings. */
-#define YY_USER_INIT    if (yycontrol) {;};
-
 
 static int braces_level = 0;
 static int percent_percent_count = 0;
@@ -96,7 +100,7 @@
      start of the next token.  */
 #define TR_POS 0
 #if TR_POS
-  fprintf (stderr, "FOO1: ");
+  fprintf (stderr, "FOO1: %p: ", yylloc);
   LOCATION_PRINT (stderr, *yylloc);
   fprintf (stderr, "\n");
 #endif
Index: src/Makefile.am
--- src/Makefile.am Fri, 14 Jun 2002 17:55:27 +0200 akim
+++ src/Makefile.am Fri, 14 Jun 2002 20:37:42 +0200 akim
@@ -45,7 +45,7 @@
        getargs.c getargs.h                       \
        gram.c gram.h                             \
        lalr.h lalr.c                             \
-       location.h                                \
+       location.c location.h                     \
        main.c                                    \
        muscle_tab.c muscle_tab.h                 \
        nullable.c nullable.h                     \
Index: src/gram.h
--- src/gram.h Sun, 26 May 2002 20:36:39 +0200 akim
+++ src/gram.h Fri, 14 Jun 2002 20:21:30 +0200 akim
@@ -97,17 +97,18 @@
 
    Associativities are recorded similarly in SYMBOLS[I]->assoc.  */
 
-#include "symtab.h"
+# include "location.h"
+# include "symtab.h"
 
-#define        ISTOKEN(s)      ((s) < ntokens)
-#define        ISVAR(s)        ((s) >= ntokens)
+# define ISTOKEN(s)    ((s) < ntokens)
+# define ISVAR(s)      ((s) >= ntokens)
 
 extern int nrules;
 extern int nsyms;
 extern int ntokens;
 extern int nvars;
 
-#define ITEM_NUMBER_MAX INT_MAX
+# define ITEM_NUMBER_MAX INT_MAX
 typedef int item_number_t;
 extern item_number_t *ritem;
 extern unsigned int nritems;
@@ -118,8 +119,8 @@
 
    Therefore, an symbol_number_t must be a valid item_number_t, and we
    sometimes have to perform the converse transformation.  */
-#define symbol_number_as_item_number(Tok) ((item_number_t) (Tok))
-#define item_number_as_symbol_number(Ite) ((symbol_number_t) (Ite))
+# define symbol_number_as_item_number(Tok) ((item_number_t) (Tok))
+# define item_number_as_symbol_number(Ite) ((symbol_number_t) (Ite))
 
 extern symbol_number_t start_symbol;
 
@@ -143,11 +144,11 @@
   /* This symbol was attached to the rule via %prec. */
   symbol_t *precsym;
 
-  int line;
+  location_t location;
   bool useful;
 
   const char *action;
-  int action_line;
+  location_t action_location;
 } rule_t;
 
 extern struct rule_s *rules;
Index: src/location.h
--- src/location.h Fri, 14 Jun 2002 17:58:25 +0200 akim
+++ src/location.h Fri, 14 Jun 2002 20:36:50 +0200 akim
@@ -62,4 +62,6 @@
   else                                                          \
     fprintf (Out, "%d.%d", (Loc).first_line, (Loc).first_column)
 
+
+extern location_t empty_location;
 #endif /* !LOCATION_H_ */
Index: src/output.c
--- src/output.c Fri, 14 Jun 2002 18:52:33 +0200 akim
+++ src/output.c Fri, 14 Jun 2002 20:22:48 +0200 akim
@@ -307,7 +307,7 @@
       /* Separator in RHS. */
       rhs[i++] = -1;
       /* Line where rule was defined. */
-      rline[r] = rules[r].line;
+      rline[r] = rules[r].location.first_line;
     }
   assert (i == nritems);
 
@@ -544,7 +544,7 @@
 
        if (!no_lines_flag)
          fprintf (out, muscle_find ("linef"),
-                  rules[rule].action_line,
+                  rules[rule].action_location.first_line,
                   quotearg_style (c_quoting_style,
                                   muscle_find ("filename")));
        fprintf (out, "    %s\n    break;\n\n",
Index: src/print.c
--- src/print.c Fri, 14 Jun 2002 18:30:31 +0200 akim
+++ src/print.c Fri, 14 Jun 2002 21:45:55 +0200 akim
@@ -399,8 +399,9 @@
   fprintf (out, "  %s\n", _("Number, Line, Rule"));
   for (j = 1; j < nrules + 1; j++)
     {
-      fprintf (out, _("  %3d %3d %s ->"),
-              j - 1, rules[j].line, escape (rules[j].lhs->tag));
+      fprintf (out, "  %3d %3d %s ->",
+              j - 1, rules[j].location.first_line,
+              escape (rules[j].lhs->tag));
       rule = rules[j].rhs;
       if (*rule >= 0)
        while (*rule >= 0)
Index: src/reader.c
--- src/reader.c Fri, 14 Jun 2002 18:30:31 +0200 akim
+++ src/reader.c Fri, 14 Jun 2002 22:50:45 +0200 akim
@@ -42,14 +42,13 @@
 int typed = 0;
 
 static symbol_list *
-symbol_list_new (symbol_t *sym)
+symbol_list_new (symbol_t *sym, location_t location)
 {
   symbol_list *res = XMALLOC (symbol_list, 1);
   res->next = NULL;
   res->sym = sym;
-  res->line = lineno;
+  res->location = location;
   res->action = NULL;
-  res->action_line = 0;
   res->ruleprec = NULL;
   return res;
 }
@@ -95,7 +94,7 @@
 `-----------------------*/
 
 void
-grammar_start_symbol_set (symbol_t *s)
+grammar_start_symbol_set (symbol_t *s, location_t l)
 {
   if (start_flag)
     complain (_("multiple %s declarations"), "%start");
@@ -103,6 +102,7 @@
     {
       start_flag = 1;
       startsymbol = s;
+      startsymbol_location = l;
     }
 }
 
@@ -197,9 +197,9 @@
 
 /* Append S to the GRAMMAR. */
 void
-grammar_symbol_append (symbol_t *s)
+grammar_symbol_append (symbol_t *symbol, location_t location)
 {
-  symbol_list *p = symbol_list_new (s);
+  symbol_list *p = symbol_list_new (symbol, location);
 
   if (grammar_end)
     grammar_end->next = p;
@@ -209,20 +209,24 @@
   grammar_end = p;
 }
 
-/* The rule currently being defined, and the previous rule.  Point to
-   the first symbol of each list: their lhs.  */
+/* The rule currently being defined, and the previous rule.
+   CURRENT_RULE points to the first LHS of the current rule, while
+   PREVIOUS_RULE_END points to the *end* of the previous rule (NULL).  */
 symbol_list *current_rule = NULL;
-symbol_list *previous_rule = NULL;
+symbol_list *previous_rule_end = NULL;
 
 
-/* Create a new rule for LHS in to the GRAMMAR. */
+/*----------------------------------------------.
+| Create a new rule for LHS in to the GRAMMAR.  |
+`----------------------------------------------*/
 
 void
-grammar_rule_begin (symbol_t *lhs)
+grammar_rule_begin (symbol_t *lhs, location_t location)
 {
   if (!start_flag)
     {
       startsymbol = lhs;
+      startsymbol_location = location;
       start_flag = 1;
     }
 
@@ -230,8 +234,8 @@
   ++nrules;
   ++nritems;
 
-  previous_rule = grammar_end;
-  grammar_symbol_append (lhs);
+  previous_rule_end = grammar_end;
+  grammar_symbol_append (lhs, location);
   current_rule = grammar_end;
 
   /* Mark the rule's lhs as a nonterminal if not already so.  */
@@ -279,21 +283,26 @@
 }
 
 
-/* End the currently being grown rule. */
+/*-------------------------------------.
+| End the currently being grown rule.  |
+`-------------------------------------*/
 
 void
-grammar_rule_end (void)
+grammar_rule_end (location_t location)
 {
   /* Put an empty link in the list to mark the end of this rule  */
-  grammar_symbol_append (NULL);
+  grammar_symbol_append (NULL, grammar_end->location);
+  current_rule->location = location;
   grammar_current_rule_check ();
 }
 
 
-/* The previous action turns out the be a mid-rule action.  Attach it
-   to the current rule, i.e., create a dummy symbol, attach it this
-   mid-rule action, and append this dummy nonterminal to the current
-   rule.  */
+/*-------------------------------------------------------------------.
+| The previous action turns out the be a mid-rule action.  Attach it |
+| to the current rule, i.e., create a dummy symbol, attach it this   |
+| mid-rule action, and append this dummy nonterminal to the current  |
+| rule.                                                              |
+`-------------------------------------------------------------------*/
 
 void
 grammar_midrule_action (void)
@@ -302,35 +311,36 @@
      give the new rule this number by inserting the new rule before
      it.  */
 
-  /* Make a dummy nonterminal, a gensym.  */
-  symbol_t *sdummy = gensym ();
-  symbol_list *midrule_action = symbol_list_new (sdummy);
+  /* Make a DUMMY nonterminal, whose location is that of the midrule
+     action.  Create the MIDRULE.  */
+  symbol_t *dummy = gensym ();
+  location_t dummy_location = current_rule->action_location;
+  symbol_list *midrule = symbol_list_new (dummy, dummy_location);
 
   /* Make a new rule, whose body is empty, before the current one, so
      that the action just read can belong to it.  */
   ++nrules;
   ++nritems;
-  /* Attach its lineno to that of the host rule.  */
-  midrule_action->line = current_rule->line;
-  /* Move the action from the host rule to this one.  */
-  midrule_action->action = current_rule->action;
-  midrule_action->action_line = current_rule->action_line;
+  /* Attach its location and actions to that of the DUMMY.  */
+  midrule->location = dummy_location;
+  midrule->action = current_rule->action;
+  midrule->action_location = dummy_location;
   current_rule->action = NULL;
 
-  if (previous_rule)
-    previous_rule->next = midrule_action;
+  if (previous_rule_end)
+    previous_rule_end->next = midrule;
   else
-    grammar = midrule_action;
+    grammar = midrule;
 
-  /* End of the rule. */
-  previous_rule = symbol_list_new (NULL);
-  previous_rule->next = current_rule;
+  /* End the dummy's rule.  */
+  previous_rule_end = symbol_list_new (NULL, dummy_location);
+  previous_rule_end->next = current_rule;
 
-  midrule_action->next = previous_rule;
+  midrule->next = previous_rule_end;
 
-  /* Insert the dummy generated by that rule into this rule.  */
-  ++nritems;
-  grammar_symbol_append (sdummy);
+  /* Insert the dummy nonterminal replacing the midrule action into
+     the current rule.  */
+  grammar_current_rule_symbol_append (dummy, dummy_location);
 }
 
 /* Set the precedence symbol of the current rule to PRECSYM. */
@@ -347,12 +357,12 @@
    action as a mid-rule action.  */
 
 void
-grammar_current_rule_symbol_append (symbol_t *symbol)
+grammar_current_rule_symbol_append (symbol_t *symbol, location_t location)
 {
   if (current_rule->action)
     grammar_midrule_action ();
   ++nritems;
-  grammar_symbol_append (symbol);
+  grammar_symbol_append (symbol, location);
 }
 
 
@@ -360,12 +370,12 @@
    action as a mid-rule action.  */
 
 void
-grammar_current_rule_action_append (const char *action, int action_line)
+grammar_current_rule_action_append (const char *action, location_t location)
 {
   if (current_rule->action)
     grammar_midrule_action ();
   current_rule->action = action;
-  current_rule->action_line = action_line;
+  current_rule->action_location = location;
 }
 
 
@@ -395,10 +405,10 @@
       rules[ruleno].number = ruleno;
       rules[ruleno].lhs = p->sym;
       rules[ruleno].rhs = ritem + itemno;
-      rules[ruleno].line = p->line;
+      rules[ruleno].location = p->location;
       rules[ruleno].useful = TRUE;
       rules[ruleno].action = p->action;
-      rules[ruleno].action_line = p->action_line;
+      rules[ruleno].action_location = p->action_location;
 
       p = p->next;
       while (p && p->sym)
@@ -500,11 +510,11 @@
 
      axiom: %start EOF.  */
   {
-    symbol_list *p = symbol_list_new (axiom);
-    p->line = grammar->line;
-    p->next = symbol_list_new (startsymbol);
-    p->next->next = symbol_list_new (eoftoken);
-    p->next->next->next = symbol_list_new (NULL);
+    symbol_list *p = symbol_list_new (axiom, empty_location);
+    p->location = grammar->location;
+    p->next = symbol_list_new (startsymbol, empty_location);
+    p->next->next = symbol_list_new (eoftoken, empty_location);
+    p->next->next->next = symbol_list_new (NULL, empty_location);
     p->next->next->next->next = grammar;
     nrules += 1;
     nritems += 3;
Index: src/reader.h
--- src/reader.h Fri, 14 Jun 2002 17:55:27 +0200 akim
+++ src/reader.h Fri, 14 Jun 2002 21:11:34 +0200 akim
@@ -27,11 +27,11 @@
 {
   struct symbol_list *next;
   symbol_t *sym;
-  int line;
+  location_t location;
 
   /* The action is attached to the LHS of a rule. */
   const char *action;
-  int action_line;
+  location_t action_location;
 
   symbol_t *ruleprec;
 } symbol_list;
@@ -66,17 +66,18 @@
 extern int typed;
 
 /* From reader.c. */
-void grammar_start_symbol_set PARAMS ((symbol_t *s));
+void grammar_start_symbol_set PARAMS ((symbol_t *s, location_t l));
 void prologue_augment PARAMS ((const char *prologue, location_t location));
 void epilogue_set PARAMS ((const char *epilogue, location_t location));
-void grammar_symbol_append PARAMS ((symbol_t *s));
-void grammar_rule_begin PARAMS ((symbol_t *lhs));
-void grammar_rule_end PARAMS ((void));
+void grammar_symbol_append PARAMS ((symbol_t *s, location_t l));
+void grammar_rule_begin PARAMS ((symbol_t *lhs, location_t l));
+void grammar_rule_end PARAMS ((location_t l));
 void grammar_midrule_action PARAMS ((void));
 void grammar_current_rule_prec_set PARAMS ((symbol_t *precsym));
-void grammar_current_rule_symbol_append PARAMS ((symbol_t *symbol));
+void grammar_current_rule_symbol_append PARAMS ((symbol_t *symbol,
+                                                location_t l));
 void grammar_current_rule_action_append PARAMS ((const char *action,
-                                                int line));
+                                                location_t l));
 extern symbol_list *current_rule;
 void reader PARAMS ((void));
 
Index: src/symtab.c
--- src/symtab.c Tue, 11 Jun 2002 21:36:46 +0200 akim
+++ src/symtab.c Fri, 14 Jun 2002 21:01:46 +0200 akim
@@ -34,6 +34,7 @@
 symbol_t *eoftoken = NULL;
 symbol_t *axiom = NULL;
 symbol_t *startsymbol = NULL;
+location_t startsymbol_location;
 
 /*---------------------------------.
 | Create a new symbol, named TAG.  |
Index: src/symtab.h
--- src/symtab.h Tue, 11 Jun 2002 19:33:39 +0200 akim
+++ src/symtab.h Fri, 14 Jun 2002 21:02:02 +0200 akim
@@ -22,6 +22,8 @@
 #ifndef SYMTAB_H_
 # define SYMTAB_H_
 
+# include "location.h"
+
 /*----------.
 | Symbols.  |
 `----------*/
@@ -112,6 +114,7 @@
 extern symbol_t *eoftoken;
 extern symbol_t *axiom;
 extern symbol_t *startsymbol;
+extern location_t startsymbol_location;
 
 
 /*---------------.
Index: tests/regression.at
--- tests/regression.at Tue, 11 Jun 2002 21:36:46 +0200 akim
+++ tests/regression.at Fri, 14 Jun 2002 22:58:07 +0200 akim
@@ -149,10 +149,10 @@ expr:
 [[Grammar
 
   Number, Line, Rule
-    0   2 $axiom -> expr $
-    1   2 @1 -> /* empty */
+    0   5 $axiom -> expr $
+    1   5 @1 -> /* empty */
     2   2 expr -> 'a' @1 'b'
-    3  15 @2 -> /* empty */
+    3  18 @2 -> /* empty */
     4  15 expr -> @2 'c'
 
 
@@ -380,7 +380,7 @@ exp: "a";
     1   6 CONST_DEC_PART -> CONST_DEC_LIST
     2  10 CONST_DEC_LIST -> CONST_DEC
     3  12 CONST_DEC_LIST -> CONST_DEC_LIST CONST_DEC
-    4  15 @1 -> /* empty */
+    4  16 @1 -> /* empty */
     5  15 CONST_DEC -> @1 undef_id_tok '=' const_id_tok ';'
 Terminals, with rules where they appear
 $ (0) 0
Index: src/location.c
--- src/location.c Fri, 14 Jun 2002 22:59:08 +0200 akim
+++ src/location.c Fri, 14 Jun 2002 20:37:30 +0200 akim
@@ -0,0 +1,24 @@
+/* Locations for Bison
+   Copyright (C) 2002  Free Software Foundation, Inc.
+
+   This file is part of Bison, the GNU Compiler Compiler.
+
+   Bison is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   Bison is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with Bison; see the file COPYING.  If not, write to
+   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+
+#include "location.h"
+
+location_t empty_location = { 0, 0, 0, 0 };



reply via email to

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