From: Akim Demaille <akim@epita.fr>
Date: Sun, 5 May 2002 11:56:49 +0000 (+0000)
Subject: New experimental feature: display the lookaheads in the report and
X-Git-Tag: BISON-1_49b~238
X-Git-Url: https://git.saurik.com/bison.git/commitdiff_plain/d4e7d3a1919c20a6dbf5eb4cfa62cb6228a0e48d

New experimental feature: display the lookaheads in the report and
graph.
* src/print (print_core): When --trace-flag, display the rules
lookaheads.
* src/print_graph.c (print_core): Likewise.
Swap the arguments.
Adjust caller.
---

diff --git a/ChangeLog b/ChangeLog
index d499bd3d..d49ff431 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2002-05-05  Akim Demaille  <akim@epita.fr>
+
+	New experimental feature: display the lookaheads in the report and
+	graph.
+
+	* src/print (print_core): When --trace-flag, display the rules
+	lookaheads.
+	* src/print_graph.c (print_core): Likewise.
+	Swap the arguments.
+	Adjust caller.
+
 2002-05-05  Akim Demaille  <akim@epita.fr>
 
 	* tests/torture.at (Many lookaheads): New test.
diff --git a/TODO b/TODO
index bc474d21..4780351f 100644
--- a/TODO
+++ b/TODO
@@ -21,6 +21,110 @@ When implementing multiple-%union support, bare the following in mind:
 	  char *sval;
 	}
 
+* Experimental report features
+Decide whether they should be enabled, or optional.  For instance, on:
+
+	input:
+	  exp
+	| input exp
+	;
+
+	exp:
+	  token1 "1"
+	| token2 "2"
+	| token3 "3"
+	;
+
+	token1: token;
+	token2: token;
+	token3: token;
+
+the traditional Bison reports:
+
+	state 0
+
+	    $axiom  ->  . input $   (rule 0)
+
+	    token       shift, and go to state 1
+
+	    input       go to state 2
+	    exp         go to state 3
+	    token1      go to state 4
+	    token2      go to state 5
+	    token3      go to state 6
+
+	state 1
+
+	    token1  ->  token .   (rule 6)
+	    token2  ->  token .   (rule 7)
+	    token3  ->  token .   (rule 8)
+
+	    "2"         reduce using rule 7 (token2)
+	    "3"         reduce using rule 8 (token3)
+	    $default    reduce using rule 6 (token1)
+
+while with --trace, i.e., when enabling both the display of non-core
+item sets and the display of lookaheads, Bison now displays:
+
+	state 0
+
+	    $axiom  ->  . input $   (rule 0)
+	    input  ->  . exp   (rule 1)
+	    input  ->  . input exp   (rule 2)
+	    exp  ->  . token1 "1"   (rule 3)
+	    exp  ->  . token2 "2"   (rule 4)
+	    exp  ->  . token3 "3"   (rule 5)
+	    token1  ->  . token   (rule 6)
+	    token2  ->  . token   (rule 7)
+	    token3  ->  . token   (rule 8)
+
+	    token       shift, and go to state 1
+
+	    input       go to state 2
+	    exp         go to state 3
+	    token1      go to state 4
+	    token2      go to state 5
+	    token3      go to state 6
+
+	state 1
+
+	    token1  ->  token .  ["1"]   (rule 6)
+	    token2  ->  token .  ["2"]   (rule 7)
+	    token3  ->  token .  ["3"]   (rule 8)
+
+	    "2"         reduce using rule 7 (token2)
+	    "3"         reduce using rule 8 (token3)
+	    $default    reduce using rule 6 (token1)
+
+so decide whether this should be an option, or always enabled.  I'm in
+favor of making it the default, but maybe we should tune the output to
+distinguish core item sets from non core:
+
+	state 0
+	    Core:
+	    $axiom  ->  . input $   (rule 0)
+
+	    Derived:
+	    input  ->  . exp   (rule 1)
+	    input  ->  . input exp   (rule 2)
+	    exp  ->  . token1 "1"   (rule 3)
+	    exp  ->  . token2 "2"   (rule 4)
+	    exp  ->  . token3 "3"   (rule 5)
+	    token1  ->  . token   (rule 6)
+	    token2  ->  . token   (rule 7)
+	    token3  ->  . token   (rule 8)
+
+	    token       shift, and go to state 1
+
+	    input       go to state 2
+	    exp         go to state 3
+	    token1      go to state 4
+	    token2      go to state 5
+	    token3      go to state 6
+
+
+Note that the same questions applies to --graph.
+
 * Coding system independence
 Paul notes:
 
diff --git a/src/print.c b/src/print.c
index ec5a12ad..cfc047f1 100644
--- a/src/print.c
+++ b/src/print.c
@@ -105,6 +105,32 @@ print_core (FILE *out, state_t *state)
 	  for (/* Nothing */; *sp >= 0; ++sp)
 	    fprintf (out, " %s", escape (symbols[*sp]->tag));
 
+	  /* Experimental feature: display the lookaheads. */
+	  if (trace_flag && state->nlookaheads)
+	    {
+	      int j, k;
+	      int nlookaheads = 0;
+	      /* Look for lookaheads corresponding to this rule. */
+	      for (j = 0; j < state->nlookaheads; ++j)
+		for (k = 0; k < ntokens; ++k)
+		  if (bitset_test (LA[state->lookaheadsp + j], k)
+		      && LArule[state->lookaheadsp + j]->number == rule)
+		    nlookaheads++;
+	      if (nlookaheads)
+		{
+		  fprintf (out, "  [");
+		  for (j = 0; j < state->nlookaheads; ++j)
+		    for (k = 0; k < ntokens; ++k)
+		      if (bitset_test (LA[state->lookaheadsp + j], k)
+			  && LArule[state->lookaheadsp + j]->number == rule)
+			fprintf (out, "%s%s",
+				 quotearg_style (escape_quoting_style,
+						 symbols[k]->tag),
+				 --nlookaheads ? ", " : "");
+		  fprintf (out, "]");
+		}
+	    }
+
 	  fprintf (out, _("   (rule %d)"), rule - 1);
 	  fputc ('\n', out);
 	}
diff --git a/src/print_graph.c b/src/print_graph.c
index 15861f35..416c4fd2 100644
--- a/src/print_graph.c
+++ b/src/print_graph.c
@@ -47,7 +47,7 @@ escape (const char *s)
 
 /* This part will construct the label of nodes. */
 static void
-print_core (state_t *state, struct obstack *node_obstack)
+print_core (struct obstack *oout, state_t *state)
 {
   int i;
   item_number_t *sitems = state->items;
@@ -61,7 +61,7 @@ print_core (state_t *state, struct obstack *node_obstack)
       snritems = nritemset;
     }
 
-  obstack_fgrow1 (node_obstack, "state %2d\n", state->number);
+  obstack_fgrow1 (oout, "state %2d\n", state->number);
   for (i = 0; i < snritems; i++)
     {
       item_number_t *sp;
@@ -76,17 +76,43 @@ print_core (state_t *state, struct obstack *node_obstack)
       rule = -(*sp);
 
       if (i)
-	obstack_1grow (node_obstack, '\n');
-      obstack_fgrow1 (node_obstack, " %s -> ",
+	obstack_1grow (oout, '\n');
+      obstack_fgrow1 (oout, " %s -> ",
 		      escape (rules[rule].lhs->tag));
 
       for (sp = rules[rule].rhs; sp < sp1; sp++)
-	obstack_fgrow1 (node_obstack, "%s ", escape (symbols[*sp]->tag));
+	obstack_fgrow1 (oout, "%s ", escape (symbols[*sp]->tag));
 
-      obstack_1grow (node_obstack, '.');
+      obstack_1grow (oout, '.');
 
       for (/* Nothing */; *sp >= 0; ++sp)
-	obstack_fgrow1 (node_obstack, " %s", escape (symbols[*sp]->tag));
+	obstack_fgrow1 (oout, " %s", escape (symbols[*sp]->tag));
+
+      /* Experimental feature: display the lookaheads. */
+      if (trace_flag && state->nlookaheads)
+	{
+	  int j, k;
+	  int nlookaheads = 0;
+	  /* Look for lookaheads corresponding to this rule. */
+	  for (j = 0; j < state->nlookaheads; ++j)
+	    for (k = 0; k < ntokens; ++k)
+	      if (bitset_test (LA[state->lookaheadsp + j], k)
+		  && LArule[state->lookaheadsp + j]->number == rule)
+		nlookaheads++;
+	  if (nlookaheads)
+	    {
+	      obstack_sgrow (oout, "  [");
+	      for (j = 0; j < state->nlookaheads; ++j)
+		for (k = 0; k < ntokens; ++k)
+		  if (bitset_test (LA[state->lookaheadsp + j], k)
+		      && LArule[state->lookaheadsp + j]->number == rule)
+		    obstack_fgrow2 (oout, "%s%s",
+				    quotearg_style (escape_quoting_style,
+						    symbols[k]->tag),
+				    --nlookaheads ? ", " : "");
+	      obstack_sgrow (oout, "]");
+	    }
+	}
     }
 }
 
@@ -154,7 +180,7 @@ print_state (state_t *state)
   new_node (&node);
   sprintf (name, "%d", state->number);
   node.title = name;
-  print_core (state, &node_obstack);
+  print_core (&node_obstack, state);
   obstack_1grow (&node_obstack, '\0');
   node.label = obstack_finish (&node_obstack);