/[bison]/bison/src/print.c
ViewVC logotype

Diff of /bison/src/print.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.91 by eggert, Tue Oct 22 04:42:25 2002 UTC revision 1.92 by eggert, Wed Dec 11 06:42:22 2002 UTC
# Line 21  Line 21 
21    
22    
23  #include "system.h"  #include "system.h"
24  #include "quotearg.h"  
25  #include "files.h"  #include <bitset.h>
26  #include "symtab.h"  #include <quotearg.h>
27  #include "gram.h"  
28  #include "LR0.h"  #include "LR0.h"
29  #include "lalr.h"  #include "closure.h"
30  #include "conflicts.h"  #include "conflicts.h"
31    #include "files.h"
32  #include "getargs.h"  #include "getargs.h"
33  #include "state.h"  #include "gram.h"
34  #include "reader.h"  #include "lalr.h"
35  #include "print.h"  #include "print.h"
36    #include "reader.h"
37  #include "reduce.h"  #include "reduce.h"
38  #include "closure.h"  #include "state.h"
39  #include "bitset.h"  #include "symtab.h"
40    
41  static bitset shiftset;  static bitset shiftset;
42  static bitset lookaheadset;  static bitset lookaheadset;
# Line 66  max_length (size_t *width, const char *s Line 68  max_length (size_t *width, const char *s
68  `--------------------------------*/  `--------------------------------*/
69    
70  static void  static void
71  print_core (FILE *out, state_t *state)  print_core (FILE *out, state *s)
72  {  {
73    int i;    int i;
74    item_number_t *sitems = state->items;    item_number *sitems = s->items;
75    int snritems   = state->nitems;    int snritems = s->nitems;
76    symbol_t *previous_lhs = NULL;    symbol *previous_lhs = NULL;
77    
78    /* Output all the items of a state, not only its kernel.  */    /* Output all the items of a state, not only its kernel.  */
79    if (report_flag & report_itemsets)    if (report_flag & report_itemsets)
# Line 88  print_core (FILE *out, state_t *state) Line 90  print_core (FILE *out, state_t *state)
90    
91    for (i = 0; i < snritems; i++)    for (i = 0; i < snritems; i++)
92      {      {
93        item_number_t *sp;        item_number *sp;
94        item_number_t *sp1;        item_number *sp1;
95        int rule;        int r;
96    
97        sp1 = sp = ritem + sitems[i];        sp1 = sp = ritem + sitems[i];
98    
99        while (*sp >= 0)        while (*sp >= 0)
100          sp++;          sp++;
101    
102        rule = item_number_as_rule_number (*sp);        r = item_number_as_rule_number (*sp);
103    
104        rule_lhs_print (&rules[rule], previous_lhs, out);        rule_lhs_print (&rules[r], previous_lhs, out);
105        previous_lhs = rules[rule].lhs;        previous_lhs = rules[r].lhs;
106    
107        for (sp = rules[rule].rhs; sp < sp1; sp++)        for (sp = rules[r].rhs; sp < sp1; sp++)
108          fprintf (out, " %s", symbols[*sp]->tag);          fprintf (out, " %s", symbols[*sp]->tag);
109        fputs (" .", out);        fputs (" .", out);
110        for (/* Nothing */; *sp >= 0; ++sp)        for (/* Nothing */; *sp >= 0; ++sp)
# Line 110  print_core (FILE *out, state_t *state) Line 112  print_core (FILE *out, state_t *state)
112    
113        /* Display the lookaheads?  */        /* Display the lookaheads?  */
114        if (report_flag & report_lookaheads)        if (report_flag & report_lookaheads)
115          state_rule_lookaheads_print (state, &rules[rule], out);          state_rule_lookaheads_print (s, &rules[r], out);
116    
117        fputc ('\n', out);        fputc ('\n', out);
118      }      }
119  }  }
120    
121    
122  /*----------------------------------------------------------------.  /*------------------------------------------------------------.
123  | Report the shifts iff DISPLAY_SHIFTS_P or the gotos of STATE on |  | Report the shifts iff DISPLAY_SHIFTS_P or the gotos of S on |
124  | OUT.                                                            |  | OUT.                                                        |
125  `----------------------------------------------------------------*/  `------------------------------------------------------------*/
126    
127  static void  static void
128  print_transitions (state_t *state, FILE *out, bool display_transitions_p)  print_transitions (state *s, FILE *out, bool display_transitions_p)
129  {  {
130    transitions_t *transitions = state->transitions;    transitions *trans = s->transitions;
131    size_t width = 0;    size_t width = 0;
132    int i;    int i;
133    
134    /* Compute the width of the lookaheads column.  */    /* Compute the width of the lookaheads column.  */
135    for (i = 0; i < transitions->num; i++)    for (i = 0; i < trans->num; i++)
136      if (!TRANSITION_IS_DISABLED (transitions, i)      if (!TRANSITION_IS_DISABLED (trans, i)
137          && TRANSITION_IS_SHIFT (transitions, i) == display_transitions_p)          && TRANSITION_IS_SHIFT (trans, i) == display_transitions_p)
138        {        {
139          symbol_t *symbol = symbols[TRANSITION_SYMBOL (transitions, i)];          symbol *sym = symbols[TRANSITION_SYMBOL (trans, i)];
140          max_length (&width, symbol->tag);          max_length (&width, sym->tag);
141        }        }
142    
143    /* Nothing to report. */    /* Nothing to report. */
# Line 146  print_transitions (state_t *state, FILE Line 148  print_transitions (state_t *state, FILE
148    width += 2;    width += 2;
149    
150    /* Report lookaheads and shifts.  */    /* Report lookaheads and shifts.  */
151    for (i = 0; i < transitions->num; i++)    for (i = 0; i < trans->num; i++)
152      if (!TRANSITION_IS_DISABLED (transitions, i)      if (!TRANSITION_IS_DISABLED (trans, i)
153          && TRANSITION_IS_SHIFT (transitions, i) == display_transitions_p)          && TRANSITION_IS_SHIFT (trans, i) == display_transitions_p)
154        {        {
155          symbol_t *symbol = symbols[TRANSITION_SYMBOL (transitions, i)];          symbol *sym = symbols[TRANSITION_SYMBOL (trans, i)];
156          const char *tag = symbol->tag;          const char *tag = sym->tag;
157          state_t *state1 = transitions->states[i];          state *s1 = trans->states[i];
158          int j;          int j;
159    
160          fprintf (out, "    %s", tag);          fprintf (out, "    %s", tag);
161          for (j = width - strlen (tag); j > 0; --j)          for (j = width - strlen (tag); j > 0; --j)
162            fputc (' ', out);            fputc (' ', out);
163          if (display_transitions_p)          if (display_transitions_p)
164            fprintf (out, _("shift, and go to state %d\n"), state1->number);            fprintf (out, _("shift, and go to state %d\n"), s1->number);
165          else          else
166            fprintf (out, _("go to state %d\n"), state1->number);            fprintf (out, _("go to state %d\n"), s1->number);
167        }        }
168  }  }
169    
170    
171  /*------------------------------------------------------------.  /*--------------------------------------------------------.
172  | Report the explicit errors of STATE raised from %nonassoc.  |  | Report the explicit errors of S raised from %nonassoc.  |
173  `------------------------------------------------------------*/  `--------------------------------------------------------*/
174    
175  static void  static void
176  print_errs (FILE *out, state_t *state)  print_errs (FILE *out, state *s)
177  {  {
178    errs_t *errp = state->errs;    errs *errp = s->errs;
179    size_t width = 0;    size_t width = 0;
180    int i;    int i;
181    
# Line 203  print_errs (FILE *out, state_t *state) Line 205  print_errs (FILE *out, state_t *state)
205  }  }
206    
207    
208  /*----------------------------------------------------------.  /*-------------------------------------------------------------.
209  | Return the default rule of this STATE if it has one, NULL |  | Return the default rule of S if it has one, NULL otherwise.  |
210  | otherwise.                                                |  `-------------------------------------------------------------*/
 `----------------------------------------------------------*/  
211    
212  static rule_t *  static rule *
213  state_default_rule (state_t *state)  state_default_rule (state *s)
214  {  {
215    reductions_t *redp = state->reductions;    reductions *reds = s->reductions;
216    rule_t *default_rule = NULL;    rule *default_rule = NULL;
217    int cmax = 0;    int cmax = 0;
218    int i;    int i;
219    
220    /* No need for a lookahead.  */    /* No need for a lookahead.  */
221    if (state->consistent)    if (s->consistent)
222      return redp->rules[0];      return reds->rules[0];
223    
224    /* 1. Each reduction is possibly masked by the lookaheads on which    /* 1. Each reduction is possibly masked by the lookaheads on which
225       we shift (S/R conflicts)...  */       we shift (S/R conflicts)...  */
226    bitset_zero (shiftset);    bitset_zero (shiftset);
227    {    {
228      transitions_t *transitions = state->transitions;      transitions *trans = s->transitions;
229      FOR_EACH_SHIFT (transitions, i)      FOR_EACH_SHIFT (trans, i)
230        {        {
231          /* If this state has a shift for the error token, don't use a          /* If this state has a shift for the error token, don't use a
232               default rule.  */               default rule.  */
233          if (TRANSITION_IS_ERROR (transitions, i))          if (TRANSITION_IS_ERROR (trans, i))
234            return NULL;            return NULL;
235          bitset_set (shiftset, TRANSITION_SYMBOL (transitions, i));          bitset_set (shiftset, TRANSITION_SYMBOL (trans, i));
236        }        }
237    }    }
238    
239    /* 2. Each reduction is possibly masked by the lookaheads on which    /* 2. Each reduction is possibly masked by the lookaheads on which
240       we raise an error (due to %nonassoc).  */       we raise an error (due to %nonassoc).  */
241    {    {
242      errs_t *errp = state->errs;      errs *errp = s->errs;
243      for (i = 0; i < errp->num; i++)      for (i = 0; i < errp->num; i++)
244        if (errp->symbols[i])        if (errp->symbols[i])
245          bitset_set (shiftset, errp->symbols[i]->number);          bitset_set (shiftset, errp->symbols[i]->number);
246    }    }
247    
248    for (i = 0; i < redp->num; ++i)    for (i = 0; i < reds->num; ++i)
249      {      {
250        int count = 0;        int count = 0;
251    
252        /* How many non-masked lookaheads are there for this reduction?        /* How many non-masked lookaheads are there for this reduction?
253           */           */
254        bitset_andn (lookaheadset, redp->lookaheads[i], shiftset);        bitset_andn (lookaheadset, reds->lookaheads[i], shiftset);
255        count = bitset_count (lookaheadset);        count = bitset_count (lookaheadset);
256    
257        if (count > cmax)        if (count > cmax)
258          {          {
259            cmax = count;            cmax = count;
260            default_rule = redp->rules[i];            default_rule = reds->rules[i];
261          }          }
262    
263        /* 3. And finally, each reduction is possibly masked by previous        /* 3. And finally, each reduction is possibly masked by previous
264           reductions (in R/R conflicts, we keep the first reductions).           reductions (in R/R conflicts, we keep the first reductions).
265           */           */
266        bitset_or (shiftset, shiftset, redp->lookaheads[i]);        bitset_or (shiftset, shiftset, reds->lookaheads[i]);
267      }      }
268    
269    return default_rule;    return default_rule;
# Line 278  state_default_rule (state_t *state) Line 279  state_default_rule (state_t *state)
279  static void  static void
280  print_reduction (FILE *out, size_t width,  print_reduction (FILE *out, size_t width,
281                   const char *lookahead,                   const char *lookahead,
282                   rule_t *rule, bool enabled)                   rule *r, bool enabled)
283  {  {
284    int j;    int j;
285    fprintf (out, "    %s", lookahead);    fprintf (out, "    %s", lookahead);
# Line 286  print_reduction (FILE *out, size_t width Line 287  print_reduction (FILE *out, size_t width
287      fputc (' ', out);      fputc (' ', out);
288    if (!enabled)    if (!enabled)
289      fputc ('[', out);      fputc ('[', out);
290    if (rule->number)    if (r->number)
291      fprintf (out, _("reduce using rule %d (%s)"),      fprintf (out, _("reduce using rule %d (%s)"), r->number, r->lhs->tag);
              rule->number, rule->lhs->tag);  
292    else    else
293      fprintf (out, _("accept"));      fprintf (out, _("accept"));
294    if (!enabled)    if (!enabled)
# Line 297  print_reduction (FILE *out, size_t width Line 297  print_reduction (FILE *out, size_t width
297  }  }
298    
299    
300  /*----------------------------------------------------.  /*-------------------------------------------.
301  | Report on OUT the reduction actions of this STATE.  |  | Report on OUT the reduction actions of S.  |
302  `----------------------------------------------------*/  `-------------------------------------------*/
303    
304  static void  static void
305  print_reductions (FILE *out, state_t *state)  print_reductions (FILE *out, state *s)
306  {  {
307    transitions_t *transitions = state->transitions;    transitions *trans = s->transitions;
308    reductions_t *redp = state->reductions;    reductions *reds = s->reductions;
309    rule_t *default_rule = NULL;    rule *default_rule = NULL;
310    size_t width = 0;    size_t width = 0;
311    int i, j;    int i, j;
312    
313    if (redp->num == 0)    if (reds->num == 0)
314      return;      return;
315    
316    default_rule = state_default_rule (state);    default_rule = state_default_rule (s);
317    
318    bitset_zero (shiftset);    bitset_zero (shiftset);
319    FOR_EACH_SHIFT (transitions, i)    FOR_EACH_SHIFT (trans, i)
320      bitset_set (shiftset, TRANSITION_SYMBOL (transitions, i));      bitset_set (shiftset, TRANSITION_SYMBOL (trans, i));
321    
322    /* Compute the width of the lookaheads column.  */    /* Compute the width of the lookaheads column.  */
323    if (default_rule)    if (default_rule)
324      width = strlen (_("$default"));      width = strlen (_("$default"));
325    
326    if (redp->lookaheads)    if (reds->lookaheads)
327      for (i = 0; i < ntokens; i++)      for (i = 0; i < ntokens; i++)
328        {        {
329          int count = bitset_test (shiftset, i);          int count = bitset_test (shiftset, i);
330    
331          for (j = 0; j < redp->num; ++j)          for (j = 0; j < reds->num; ++j)
332            if (bitset_test (redp->lookaheads[j], i))            if (bitset_test (reds->lookaheads[j], i))
333              {              {
334                if (count == 0)                if (count == 0)
335                  {                  {
336                    if (redp->rules[j] != default_rule)                    if (reds->rules[j] != default_rule)
337                      max_length (&width, symbols[i]->tag);                      max_length (&width, symbols[i]->tag);
338                    count++;                    count++;
339                  }                  }
# Line 352  print_reductions (FILE *out, state_t *st Line 352  print_reductions (FILE *out, state_t *st
352    width += 2;    width += 2;
353    
354    /* Report lookaheads (or $default) and reductions.  */    /* Report lookaheads (or $default) and reductions.  */
355    if (redp->lookaheads)    if (reds->lookaheads)
356      for (i = 0; i < ntokens; i++)      for (i = 0; i < ntokens; i++)
357        {        {
358          int defaulted = 0;          int defaulted = 0;
359          int count = bitset_test (shiftset, i);          int count = bitset_test (shiftset, i);
360    
361          for (j = 0; j < redp->num; ++j)          for (j = 0; j < reds->num; ++j)
362            if (bitset_test (redp->lookaheads[j], i))            if (bitset_test (reds->lookaheads[j], i))
363              {              {
364                if (count == 0)                if (count == 0)
365                  {                  {
366                    if (redp->rules[j] != default_rule)                    if (reds->rules[j] != default_rule)
367                      print_reduction (out, width,                      print_reduction (out, width,
368                                       symbols[i]->tag,                                       symbols[i]->tag,
369                                       redp->rules[j], true);                                       reds->rules[j], true);
370                    else                    else
371                      defaulted = 1;                      defaulted = 1;
372                    count++;                    count++;
# Line 380  print_reductions (FILE *out, state_t *st Line 380  print_reductions (FILE *out, state_t *st
380                    defaulted = 0;                    defaulted = 0;
381                    print_reduction (out, width,                    print_reduction (out, width,
382                                     symbols[i]->tag,                                     symbols[i]->tag,
383                                     redp->rules[j], false);                                     reds->rules[j], false);
384                  }                  }
385              }              }
386        }        }
# Line 393  print_reductions (FILE *out, state_t *st Line 393  print_reductions (FILE *out, state_t *st
393    
394  /*--------------------------------------------------------------.  /*--------------------------------------------------------------.
395  | Report on OUT all the actions (shifts, gotos, reductions, and |  | Report on OUT all the actions (shifts, gotos, reductions, and |
396  | explicit erros from %nonassoc) of STATE.                      |  | explicit erros from %nonassoc) of S.                          |
397  `--------------------------------------------------------------*/  `--------------------------------------------------------------*/
398    
399  static void  static void
400  print_actions (FILE *out, state_t *state)  print_actions (FILE *out, state *s)
401  {  {
402    /* Print shifts.  */    /* Print shifts.  */
403    print_transitions (state, out, true);    print_transitions (s, out, true);
404    print_errs (out, state);    print_errs (out, s);
405    print_reductions (out, state);    print_reductions (out, s);
406    /* Print gotos.  */    /* Print gotos.  */
407    print_transitions (state, out, false);    print_transitions (s, out, false);
408  }  }
409    
410    
411  /*--------------------------------------.  /*----------------------------------.
412  | Report all the data on STATE on OUT.  |  | Report all the data on S on OUT.  |
413  `--------------------------------------*/  `----------------------------------*/
414    
415  static void  static void
416  print_state (FILE *out, state_t *state)  print_state (FILE *out, state *s)
417  {  {
418    fputs ("\n\n", out);    fputs ("\n\n", out);
419    fprintf (out, _("state %d"), state->number);    fprintf (out, _("state %d"), s->number);
420    fputc ('\n', out);    fputc ('\n', out);
421    print_core (out, state);    print_core (out, s);
422    print_actions (out, state);    print_actions (out, s);
423    if ((report_flag & report_solved_conflicts)    if ((report_flag & report_solved_conflicts) && s->solved_conflicts)
       && state->solved_conflicts)  
424      {      {
425        fputc ('\n', out);        fputc ('\n', out);
426        fputs (state->solved_conflicts, out);        fputs (s->solved_conflicts, out);
427      }      }
428  }  }
429    
# Line 446  do {                                           \ Line 445  do {                                           \
445  static void  static void
446  print_grammar (FILE *out)  print_grammar (FILE *out)
447  {  {
448    symbol_number_t i;    symbol_number i;
449    char buffer[90];    char buffer[90];
450    int column = 0;    int column = 0;
451    
# Line 458  print_grammar (FILE *out) Line 457  print_grammar (FILE *out)
457      if (token_translations[i] != undeftoken->number)      if (token_translations[i] != undeftoken->number)
458        {        {
459          const char *tag = symbols[token_translations[i]]->tag;          const char *tag = symbols[token_translations[i]]->tag;
460          rule_number_t r;          rule_number r;
461          item_number_t *rhsp;          item_number *rhsp;
462    
463          buffer[0] = 0;          buffer[0] = 0;
464          column = strlen (tag);          column = strlen (tag);
# Line 484  print_grammar (FILE *out) Line 483  print_grammar (FILE *out)
483    for (i = ntokens; i < nsyms; i++)    for (i = ntokens; i < nsyms; i++)
484      {      {
485        int left_count = 0, right_count = 0;        int left_count = 0, right_count = 0;
486        rule_number_t r;        rule_number r;
487        const char *tag = symbols[i]->tag;        const char *tag = symbols[i]->tag;
488    
489        for (r = 0; r < nrules; r++)        for (r = 0; r < nrules; r++)
490          {          {
491            item_number_t *rhsp;            item_number *rhsp;
492            if (rules[r].lhs->number == i)            if (rules[r].lhs->number == i)
493              left_count++;              left_count++;
494            for (rhsp = rules[r].rhs; *rhsp >= 0; rhsp++)            for (rhsp = rules[r].rhs; *rhsp >= 0; rhsp++)
# Line 527  print_grammar (FILE *out) Line 526  print_grammar (FILE *out)
526            sprintf (buffer + strlen (buffer), _(" on right:"));            sprintf (buffer + strlen (buffer), _(" on right:"));
527            for (r = 0; r < nrules; r++)            for (r = 0; r < nrules; r++)
528              {              {
529                item_number_t *rhsp;                item_number *rhsp;
530                for (rhsp = rules[r].rhs; *rhsp >= 0; rhsp++)                for (rhsp = rules[r].rhs; *rhsp >= 0; rhsp++)
531                  if (item_number_as_symbol_number (*rhsp) == i)                  if (item_number_as_symbol_number (*rhsp) == i)
532                    {                    {
# Line 544  print_grammar (FILE *out) Line 543  print_grammar (FILE *out)
543  void  void
544  print_results (void)  print_results (void)
545  {  {
546    state_number_t i;    state_number i;
547    
548    /* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but    /* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but
549       that conflicts with Posix.  */       that conflicts with Posix.  */

Legend:
Removed from v.1.91  
changed lines
  Added in v.1.92

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26