/[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.79 by akim, Sun Jun 30 17:33:08 2002 UTC revision 1.80 by akim, Sun Jun 30 17:33:19 2002 UTC
# Line 48  print_token (int extnum, int token) Line 48  print_token (int extnum, int token)
48  #endif  #endif
49    
50    
51    
52    /*---------------------------------------.
53    | *WIDTH := max (*WIDTH, strlen (STR)).  |
54    `---------------------------------------*/
55    
56    static void
57    max_length (size_t *width, const char *str)
58    {
59      size_t len = strlen (str);
60      if (len > *width)
61        *width = len;
62    }
63    
64  /*--------------------------------.  /*--------------------------------.
65  | Report information on a state.  |  | Report information on a state.  |
66  `--------------------------------*/  `--------------------------------*/
# Line 71  print_core (FILE *out, state_t *state) Line 84  print_core (FILE *out, state_t *state)
84    if (!snritems)    if (!snritems)
85      return;      return;
86    
87      fputc ('\n', out);
88    
89    for (i = 0; i < snritems; i++)    for (i = 0; i < snritems; i++)
90      {      {
91        item_number_t *sp;        item_number_t *sp;
# Line 99  print_core (FILE *out, state_t *state) Line 114  print_core (FILE *out, state_t *state)
114    
115        fputc ('\n', out);        fputc ('\n', out);
116      }      }
   
   fputc ('\n', out);  
117  }  }
118    
119    
120    /*----------------------------------------------------------------.
121    | Report the shifts iff DISPLAY_SHIFTS_P or the gotos of STATE on |
122    | OUT.                                                            |
123    `----------------------------------------------------------------*/
124    
125  static void  static void
126  print_shifts (FILE *out, state_t *state)  print_transitions (state_t *state, FILE *out, bool display_shifts_p)
127  {  {
   int i;  
128    shifts_t *shiftp = state->shifts;    shifts_t *shiftp = state->shifts;
129      size_t width = 0;
130      int i;
131    
132    for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)    /* Compute the width of the lookaheads column.  */
133      if (!SHIFT_IS_DISABLED (shiftp, i))    for (i = 0; i < shiftp->nshifts; i++)
134        if (!SHIFT_IS_DISABLED (shiftp, i)
135            && SHIFT_IS_SHIFT (shiftp, i) == display_shifts_p)
136        {        {
137          state_number_t state1 = shiftp->shifts[i];          symbol_t *symbol = symbols[SHIFT_SYMBOL (shiftp, i)];
138          symbol_number_t symbol = states[state1]->accessing_symbol;          max_length (&width, symbol_tag_get (symbol));
         fprintf (out,  
                  _("    %-4s\tshift, and go to state %d\n"),  
                  symbol_tag_get (symbols[symbol]), state1);  
139        }        }
140    
141    if (i > 0)    /* Nothing to report. */
142      fputc ('\n', out);    if (!width)
143        return;
144    
145      fputc ('\n', out);
146      width += 2;
147    
148      /* Report lookaheads and shifts.  */
149      for (i = 0; i < shiftp->nshifts; i++)
150        if (!SHIFT_IS_DISABLED (shiftp, i)
151            && SHIFT_IS_SHIFT (shiftp, i) == display_shifts_p)
152          {
153            symbol_t *symbol = symbols[SHIFT_SYMBOL (shiftp, i)];
154            const char *tag = symbol_tag_get (symbol);
155            state_number_t state1 = shiftp->shifts[i];
156            int j;
157    
158            fprintf (out, "    %s", tag);
159            for (j = width - strlen (tag); j > 0; --j)
160              fputc (' ', out);
161            if (display_shifts_p)
162              fprintf (out, _("shift, and go to state %d\n"), state1);
163            else
164              fprintf (out, _("go to state %d\n"), state1);
165          }
166  }  }
167    
168    
169    /*------------------------------------------------------------.
170    | Report the explicit errors of STATE raised from %nonassoc.  |
171    `------------------------------------------------------------*/
172    
173  static void  static void
174  print_errs (FILE *out, state_t *state)  print_errs (FILE *out, state_t *state)
175  {  {
176    errs_t *errp = state->errs;    errs_t *errp = state->errs;
177      size_t width = 0;
178    int i;    int i;
179    
180      /* Compute the width of the lookaheads column.  */
181    for (i = 0; i < errp->nerrs; ++i)    for (i = 0; i < errp->nerrs; ++i)
182      if (errp->errs[i])      if (errp->errs[i])
183        fprintf (out, _("    %-4s\terror (nonassociative)\n"),        max_length (&width, symbol_tag_get (symbols[errp->errs[i]]));
                symbol_tag_get (symbols[errp->errs[i]]));  
   
   if (i > 0)  
     fputc ('\n', out);  
 }  
184    
185      /* Nothing to report. */
186      if (!width)
187        return;
188    
189  static void    fputc ('\n', out);
190  print_gotos (FILE *out, state_t *state)    width += 2;
 {  
   int i;  
   shifts_t *shiftp = state->shifts;  
   
   for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)  
     /* Skip token shifts.  */;  
   
   if (i < shiftp->nshifts)  
     {  
       for (; i < shiftp->nshifts; i++)  
         if (!SHIFT_IS_DISABLED (shiftp, i))  
           {  
             state_number_t state1 = shiftp->shifts[i];  
             symbol_number_t symbol = states[state1]->accessing_symbol;  
             fprintf (out, _("    %-4s\tgo to state %d\n"),  
                      symbol_tag_get (symbols[symbol]), state1);  
           }  
191    
192        fputc ('\n', out);    /* Report lookaheads and errors.  */
193      }    for (i = 0; i < errp->nerrs; ++i)
194        if (errp->errs[i])
195          {
196            const char *tag = symbol_tag_get (symbols[errp->errs[i]]);
197            int j;
198            fprintf (out, "    %s", tag);
199            for (j = width - strlen (tag); j > 0; --j)
200              fputc (' ', out);
201            fputs (_("error (nonassociative)\n"), out);
202          }
203  }  }
204    
205    
# Line 172  print_gotos (FILE *out, state_t *state) Line 209  print_gotos (FILE *out, state_t *state)
209  `----------------------------------------------------------*/  `----------------------------------------------------------*/
210    
211  static rule_t *  static rule_t *
212  state_default_rule_compute (state_t *state)  state_default_rule (state_t *state)
213  {  {
214    reductions_t *redp = state->reductions;    reductions_t *redp = state->reductions;
215    rule_t *default_rule = NULL;    rule_t *default_rule = NULL;
# Line 233  state_default_rule_compute (state_t *sta Line 270  state_default_rule_compute (state_t *sta
270  }  }
271    
272    
273    /*--------------------------------------------------------------------.
274    | Report a reduction of RULE on LOOKAHEADS (which can be `default').  |
275    | If not ENABLED, the rule is masked by a shift or a reduce (S/R and  |
276    | R/R conflicts).                                                     |
277    `--------------------------------------------------------------------*/
278    
279    static void
280    print_reduction (FILE *out, size_t width,
281                     const char *lookahead,
282                     rule_t *rule, bool enabled)
283    {
284      int j;
285      fprintf (out, "    %s", lookahead);
286      for (j = width - strlen (lookahead); j > 0; --j)
287        fputc (' ', out);
288      if (!enabled)
289        fputc ('[', out);
290      fprintf (out, _("reduce using rule %d (%s)"),
291               rule->number - 1, symbol_tag_get (rule->lhs));
292      if (!enabled)
293        fputc (']', out);
294      fputc ('\n', out);
295    }
296    
297    
298  /*----------------------------------------------------.  /*----------------------------------------------------.
299  | Report on OUT the reduction actions of this STATE.  |  | Report on OUT the reduction actions of this STATE.  |
300  `----------------------------------------------------*/  `----------------------------------------------------*/
# Line 240  state_default_rule_compute (state_t *sta Line 302  state_default_rule_compute (state_t *sta
302  static void  static void
303  print_reductions (FILE *out, state_t *state)  print_reductions (FILE *out, state_t *state)
304  {  {
   int i;  
305    shifts_t *shiftp = state->shifts;    shifts_t *shiftp = state->shifts;
306    reductions_t *redp = state->reductions;    reductions_t *redp = state->reductions;
307    rule_t *default_rule = NULL;    rule_t *default_rule = NULL;
308      size_t width = 0;
309      int i, j;
310    
311    if (redp->nreds == 0)    if (redp->nreds == 0)
312      return;      return;
313    
314    default_rule = state_default_rule_compute (state);    default_rule = state_default_rule (state);
315    
316    bitset_zero (shiftset);    bitset_zero (shiftset);
317    for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)    for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
318      if (!SHIFT_IS_DISABLED (shiftp, i))      if (!SHIFT_IS_DISABLED (shiftp, i))
319        bitset_set (shiftset, SHIFT_SYMBOL (shiftp, i));        bitset_set (shiftset, SHIFT_SYMBOL (shiftp, i));
320    
321      /* Compute the width of the lookaheads column.  */
322      if (default_rule)
323        width = strlen (_("$default"));
324      for (i = 0; i < ntokens; i++)
325        {
326          int count = bitset_test (shiftset, i);
327    
328          for (j = 0; j < state->nlookaheads; ++j)
329            if (bitset_test (state->lookaheads[j], i))
330              {
331                if (count == 0)
332                  {
333                    if (state->lookaheads_rule[j] != default_rule)
334                      max_length (&width, symbol_tag_get (symbols[i]));
335                    count++;
336                  }
337                else
338                  {
339                    max_length (&width, symbol_tag_get (symbols[i]));
340                  }
341              }
342        }
343    
344      /* Nothing to report. */
345      if (!width)
346        return;
347    
348      fputc ('\n', out);
349      width += 2;
350    
351      /* Report lookaheads (or $default) and reductions.  */
352    for (i = 0; i < ntokens; i++)    for (i = 0; i < ntokens; i++)
353      {      {
       int j;  
354        int defaulted = 0;        int defaulted = 0;
355        int count = bitset_test (shiftset, i);        int count = bitset_test (shiftset, i);
356    
# Line 267  print_reductions (FILE *out, state_t *st Line 360  print_reductions (FILE *out, state_t *st
360              if (count == 0)              if (count == 0)
361                {                {
362                  if (state->lookaheads_rule[j] != default_rule)                  if (state->lookaheads_rule[j] != default_rule)
363                    fprintf (out,                    print_reduction (out, width,
364                             _("    %-4s\treduce using rule %d (%s)\n"),                                     symbol_tag_get (symbols[i]),
365                             symbol_tag_get (symbols[i]),                                     state->lookaheads_rule[j], TRUE);
                            state->lookaheads_rule[j]->number - 1,  
                            symbol_tag_get_n (state->lookaheads_rule[j]->lhs, 1));  
366                  else                  else
367                    defaulted = 1;                    defaulted = 1;
368                  count++;                  count++;
# Line 279  print_reductions (FILE *out, state_t *st Line 370  print_reductions (FILE *out, state_t *st
370              else              else
371                {                {
372                  if (defaulted)                  if (defaulted)
373                    fprintf (out,                    print_reduction (out, width,
374                             _("    %-4s\treduce using rule %d (%s)\n"),                                     symbol_tag_get (symbols[i]),
375                             symbol_tag_get (symbols[i]),                                     default_rule, TRUE);
                            default_rule->number - 1,  
                            symbol_tag_get_n (default_rule->lhs, 1));  
376                  defaulted = 0;                  defaulted = 0;
377                  fprintf (out,                  print_reduction (out, width,
378                           _("    %-4s\t[reduce using rule %d (%s)]\n"),                                   symbol_tag_get (symbols[i]),
379                           symbol_tag_get (symbols[i]),                                   state->lookaheads_rule[j], FALSE);
                          state->lookaheads_rule[j]->number - 1,  
                          symbol_tag_get_n (state->lookaheads_rule[j]->lhs, 1));  
380                }                }
381            }            }
382      }      }
383    
384    if (default_rule)    if (default_rule)
385      fprintf (out, _("    $default\treduce using rule %d (%s)\n"),      print_reduction (out, width,
386               default_rule->number - 1,                       _("$default"), default_rule, TRUE);
              symbol_tag_get (default_rule->lhs));  
   fputc ('\n', out);  
387  }  }
388    
389    
# Line 315  print_actions (FILE *out, state_t *state Line 400  print_actions (FILE *out, state_t *state
400    
401    if (shiftp->nshifts == 0 && redp->nreds == 0)    if (shiftp->nshifts == 0 && redp->nreds == 0)
402      {      {
403          fputc ('\n', out);
404        if (state->number == final_state->number)        if (state->number == final_state->number)
405          fprintf (out, _("    $default\taccept\n"));          fprintf (out, _("    $default\taccept\n"));
406        else        else
# Line 322  print_actions (FILE *out, state_t *state Line 408  print_actions (FILE *out, state_t *state
408        return;        return;
409      }      }
410    
411    print_shifts (out, state);    /* Print shifts.  */
412      print_transitions (state, out, TRUE);
413    print_errs (out, state);    print_errs (out, state);
414    print_reductions (out, state);    print_reductions (out, state);
415    print_gotos (out, state);    /* Print gotos.  */
416      print_transitions (state, out, FALSE);
417  }  }
418    
419    
420    /*--------------------------------------.
421    | Report all the data on STATE on OUT.  |
422    `--------------------------------------*/
423    
424  static void  static void
425  print_state (FILE *out, state_t *state)  print_state (FILE *out, state_t *state)
426  {  {
   fprintf (out, _("state %d"), state->number);  
427    fputs ("\n\n", out);    fputs ("\n\n", out);
428      fprintf (out, _("state %d"), state->number);
429      fputc ('\n', out);
430    print_core (out, state);    print_core (out, state);
431    print_actions (out, state);    print_actions (out, state);
432    if ((report_flag & report_solved_conflicts)    if ((report_flag & report_solved_conflicts)
433        && state->solved_conflicts)        && state->solved_conflicts)
434      fputs (state->solved_conflicts, out);      fputs (state->solved_conflicts, out);
   fputs ("\n\n", out);  
435  }  }
436    
437  /*-----------------------------------------.  /*-----------------------------------------.
# Line 453  print_grammar (FILE *out) Line 545  print_grammar (FILE *out)
545          }          }
546        fprintf (out, "%s\n", buffer);        fprintf (out, "%s\n", buffer);
547      }      }
   fputs ("\n\n", out);  
548  }  }
549    
550  void  void

Legend:
Removed from v.1.79  
changed lines
  Added in v.1.80

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