/[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.17 by akim, Wed Dec 20 17:21:14 2000 UTC revision 1.18 by akim, Thu Oct 4 14:55:20 2001 UTC
# Line 20  Line 20 
20    
21    
22  #include "system.h"  #include "system.h"
 #include "xalloc.h"  
23  #include "files.h"  #include "files.h"
24  #include "gram.h"  #include "gram.h"
25  #include "LR0.h"  #include "LR0.h"
# Line 35  Line 34 
34  static void  static void
35  print_token (int extnum, int token)  print_token (int extnum, int token)
36  {  {
37    obstack_fgrow2 (&output_obstack, _(" type %d is %s\n"), extnum, tags[token]);    fprintf (out, _(" type %d is %s\n"), extnum, tags[token]);
38  }  }
39  #endif  #endif
40    
41    
42  /*================================\  /*--------------------------------.
43  | Report information on a state.  |  | Report information on a state.  |
44  \================================*/  `--------------------------------*/
45    
46  static void  static void
47  print_core (int state)  print_core (FILE *out, int state)
48  {  {
49    int i;    int i;
50    int k;    int k;
# Line 68  print_core (int state) Line 67  print_core (int state)
67          sp++;          sp++;
68    
69        rule = -(*sp);        rule = -(*sp);
70        obstack_fgrow1 (&output_obstack, "    %s  ->  ", tags[rlhs[rule]]);        fprintf (out, "    %s  ->  ", tags[rlhs[rule]]);
71    
72        for (sp = ritem + rrhs[rule]; sp < sp1; sp++)        for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
73          {          {
74            obstack_fgrow1 (&output_obstack, "%s ", tags[*sp]);            fprintf (out, "%s ", tags[*sp]);
75          }          }
76    
77        obstack_1grow (&output_obstack, '.');        fputc ('.', out);
78    
79        while (*sp > 0)        while (*sp > 0)
80          {          {
81            obstack_fgrow1 (&output_obstack, " %s", tags[*sp]);            fprintf (out, " %s", tags[*sp]);
82            sp++;            sp++;
83          }          }
84    
85        obstack_fgrow1 (&output_obstack, _("   (rule %d)"), rule);        fprintf (out, _("   (rule %d)"), rule);
86        obstack_1grow (&output_obstack, '\n');        fputc ('\n', out);
87      }      }
88    
89    obstack_1grow (&output_obstack, '\n');    fputc ('\n', out);
90  }  }
91    
92  static void  static void
93  print_actions (int state)  print_actions (FILE *out, int state)
94  {  {
95    int i;    int i;
96    int k;    int k;
# Line 109  print_actions (int state) Line 108  print_actions (int state)
108    if (!shiftp && !redp)    if (!shiftp && !redp)
109      {      {
110        if (final_state == state)        if (final_state == state)
111          obstack_sgrow (&output_obstack, _("    $default\taccept\n"));          fprintf (out, _("    $default\taccept\n"));
112        else        else
113          obstack_sgrow (&output_obstack, _("    NO ACTIONS\n"));          fprintf (out, _("    NO ACTIONS\n"));
114        return;        return;
115      }      }
116    
# Line 129  print_actions (int state) Line 128  print_actions (int state)
128            if (ISVAR (symbol))            if (ISVAR (symbol))
129              break;              break;
130            if (symbol == 0)      /* I.e. strcmp(tags[symbol],"$")==0 */            if (symbol == 0)      /* I.e. strcmp(tags[symbol],"$")==0 */
131              obstack_fgrow1 (&output_obstack,              fprintf (out,
132                              _("    $   \tgo to state %d\n"), state1);                       _("    $   \tgo to state %d\n"), state1);
133            else            else
134              obstack_fgrow2 (&output_obstack,              fprintf (out,
135                              _("    %-4s\tshift, and go to state %d\n"),                       _("    %-4s\tshift, and go to state %d\n"),
136                              tags[symbol], state1);                       tags[symbol], state1);
137          }          }
138    
139        if (i > 0)        if (i > 0)
140          obstack_1grow (&output_obstack, '\n');          fputc ('\n', out);
141      }      }
142    else    else
143      {      {
# Line 157  print_actions (int state) Line 156  print_actions (int state)
156            if (!errp->errs[j])            if (!errp->errs[j])
157              continue;              continue;
158            symbol = errp->errs[j];            symbol = errp->errs[j];
159            obstack_fgrow1 (&output_obstack, _("    %-4s\terror (nonassociative)\n"),            fprintf (out, _("    %-4s\terror (nonassociative)\n"),
160                     tags[symbol]);                     tags[symbol]);
161          }          }
162    
163        if (j > 0)        if (j > 0)
164          obstack_1grow (&output_obstack, '\n');          fputc ('\n', out);
165      }      }
166    
167    if (consistent[state] && redp)    if (consistent[state] && redp)
168      {      {
169        rule = redp->rules[0];        rule = redp->rules[0];
170        symbol = rlhs[rule];        symbol = rlhs[rule];
171        obstack_fgrow2 (&output_obstack,        fprintf (out, _("    $default\treduce using rule %d (%s)\n\n"),
172                        _("    $default\treduce using rule %d (%s)\n\n"),                 rule, tags[symbol]);
                       rule, tags[symbol]);  
173      }      }
174    else if (redp)    else if (redp)
175      {      {
# Line 186  print_actions (int state) Line 184  print_actions (int state)
184              continue;              continue;
185            state1 = shiftp->shifts[i];            state1 = shiftp->shifts[i];
186            symbol = accessing_symbol[state1];            symbol = accessing_symbol[state1];
187            obstack_fgrow2 (&output_obstack,            fprintf (out, _("    %-4s\tgo to state %d\n"),
188                            _("    %-4s\tgo to state %d\n"),                     tags[symbol], state1);
                           tags[symbol], state1);  
189          }          }
190    
191        obstack_1grow (&output_obstack, '\n');        fputc ('\n', out);
192      }      }
193  }  }
194    
195  static void  static void
196  print_state (int state)  print_state (FILE *out, int state)
197  {  {
198    obstack_sgrow (&output_obstack, "\n\n");    fputs ("\n\n", out);
199    obstack_fgrow1 (&output_obstack, _("state %d"), state);    fprintf (out, _("state %d"), state);
200    obstack_sgrow (&output_obstack, "\n\n");    fputs ("\n\n", out);
201    print_core (state);    print_core (out, state);
202    print_actions (state);    print_actions (out, state);
203  }  }
204    
205  /*-----------------------------------------.  /*-----------------------------------------.
206  | Print information on the whole grammar.  |  | Print information on the whole grammar.  |
207  `-----------------------------------------*/  `-----------------------------------------*/
208    
209  #define END_TEST(End)                                           \  #define END_TEST(End)                           \
210  do {                                                            \  do {                                            \
211    if (column + strlen(buffer) > (End))                          \    if (column + strlen(buffer) > (End))          \
212      {                                                           \      {                                           \
213        obstack_fgrow1 (&output_obstack, "%s\n   ", buffer);      \        fprintf (out, "%s\n   ", buffer);         \
214        column = 3;                                               \        column = 3;                               \
215        buffer[0] = 0;                                            \        buffer[0] = 0;                            \
216      }                                                           \      }                                           \
217  } while (0)  } while (0)
218    
219    
220  static void  static void
221  print_grammar (void)  print_grammar (FILE *out)
222  {  {
223    int i, j;    int i, j;
224    short *rule;    short *rule;
# Line 229  print_grammar (void) Line 226  print_grammar (void)
226    int column = 0;    int column = 0;
227    
228    /* rule # : LHS -> RHS */    /* rule # : LHS -> RHS */
229    obstack_1grow (&output_obstack, '\n');    fprintf (out, "\n%s\n", _("Grammar"));
   obstack_sgrow (&output_obstack, _("Grammar"));  
   obstack_1grow (&output_obstack, '\n');  
230    for (i = 1; i <= nrules; i++)    for (i = 1; i <= nrules; i++)
231      /* Don't print rules disabled in reduce_grammar_tables.  */      /* Don't print rules disabled in reduce_grammar_tables.  */
232      if (rlhs[i] >= 0)      if (rlhs[i] >= 0)
233        {        {
234          obstack_fgrow2 (&output_obstack,          fprintf (out, _("rule %-4d %s ->"), i, tags[rlhs[i]]);
                         _("rule %-4d %s ->"), i, tags[rlhs[i]]);  
235          rule = &ritem[rrhs[i]];          rule = &ritem[rrhs[i]];
236          if (*rule > 0)          if (*rule > 0)
237            while (*rule > 0)            while (*rule > 0)
238              obstack_fgrow1 (&output_obstack, " %s", tags[*rule++]);              fprintf (out, " %s", tags[*rule++]);
239          else          else
240            obstack_sgrow (&output_obstack, _("           /* empty */"));            fprintf (out, "               /* %s */\n", _("empty"));
         obstack_1grow (&output_obstack, '\n');  
241        }        }
242    
243    /* TERMINAL (type #) : rule #s terminal is on RHS */    /* TERMINAL (type #) : rule #s terminal is on RHS */
244    obstack_sgrow (&output_obstack, "\n");    fprintf (out, "\n%s\n\n", _("Terminals, with rules where they appear"));
245    obstack_sgrow (&output_obstack,    fprintf (out, "%s (-1)\n", tags[0]);
                  _("Terminals, with rules where they appear"));  
   obstack_sgrow (&output_obstack, "\n\n");  
   obstack_fgrow1 (&output_obstack, "%s (-1)\n", tags[0]);  
   if (translations)  
     {  
       for (i = 0; i <= max_user_token_number; i++)  
         if (token_translations[i] != 2)  
           {  
             buffer[0] = 0;  
             column = strlen (tags[token_translations[i]]);  
             obstack_sgrow (&output_obstack, tags[token_translations[i]]);  
             END_TEST (50);  
             sprintf (buffer, " (%d)", i);  
246    
247              for (j = 1; j <= nrules; j++)    for (i = 0; i <= max_user_token_number; i++)
248                for (rule = &ritem[rrhs[j]]; *rule > 0; rule++)      if (token_translations[i] != 2)
249                  if (*rule == token_translations[i])        {
250                    {          buffer[0] = 0;
251                      END_TEST (65);          column = strlen (tags[token_translations[i]]);
252                      sprintf (buffer + strlen (buffer), " %d", j);          fputs (tags[token_translations[i]], out);
253                      break;          END_TEST (50);
254                    }          sprintf (buffer, " (%d)", i);
             obstack_fgrow1 (&output_obstack, "%s\n", buffer);  
           }  
     }  
   else  
     {  
       for (i = 1; i < ntokens; i++)  
         {  
           buffer[0] = 0;  
           column = strlen (tags[i]);  
           obstack_sgrow (&output_obstack, tags[i]);  
           END_TEST (50);  
           sprintf (buffer, " (%d)", i);  
255    
256            for (j = 1; j <= nrules; j++)          for (j = 1; j <= nrules; j++)
257              for (rule = &ritem[rrhs[j]]; *rule > 0; rule++)            for (rule = &ritem[rrhs[j]]; *rule > 0; rule++)
258                if (*rule == i)              if (*rule == token_translations[i])
259                  {                {
260                    END_TEST (65);                  END_TEST (65);
261                    sprintf (buffer + strlen (buffer), " %d", j);                  sprintf (buffer + strlen (buffer), " %d", j);
262                    break;                  break;
263                  }                }
264            obstack_fgrow1 (&output_obstack, "%s\n", buffer);          fprintf (out, "%s\n", buffer);
265          }        }
     }  
266    
267    obstack_sgrow (&output_obstack, "\n");    fprintf (out, "\n%s\n\n",
268    obstack_sgrow (&output_obstack,             _("Nonterminals, with rules where they appear"));
                  _("Nonterminals, with rules where they appear"));  
   obstack_sgrow (&output_obstack, "\n\n");  
269    for (i = ntokens; i <= nsyms - 1; i++)    for (i = ntokens; i <= nsyms - 1; i++)
270      {      {
271        int left_count = 0, right_count = 0;        int left_count = 0, right_count = 0;
# Line 318  print_grammar (void) Line 283  print_grammar (void)
283          }          }
284    
285        buffer[0] = 0;        buffer[0] = 0;
286        obstack_sgrow (&output_obstack, tags[i]);        fputs (tags[i], out);
287        column = strlen (tags[i]);        column = strlen (tags[i]);
288        sprintf (buffer, " (%d)", i);        sprintf (buffer, " (%d)", i);
289        END_TEST (0);        END_TEST (0);
# Line 353  print_grammar (void) Line 318  print_grammar (void)
318                    }                    }
319              }              }
320          }          }
321        obstack_fgrow1 (&output_obstack, "%s\n", buffer);        fprintf (out, "%s\n", buffer);
322      }      }
323  }  }
324    
325  void  void
326  print_results (void)  print_results (void)
327  {  {
328    int i;    if (verbose_flag)
329        {
330          int i;
331    
332    if (any_conflicts)        /* We used to use just .out if spec_name_prefix (-p) was used, but
333      print_conflicts ();           that conflicts with Posix.  */
334          FILE *out = xfopen (spec_verbose_file, "w");
335    
336    if (verbose_flag)        size_t size = obstack_object_size (&output_obstack);
337      print_grammar ();        fwrite (obstack_finish (&output_obstack), 1, size, out);
338    
339    if (verbose_flag)        if (any_conflicts)
340      for (i = 0; i < nstates; i++)          print_conflicts (out);
341        print_state (i);  
342          print_grammar (out);
343    
344          for (i = 0; i < nstates; i++)
345            print_state (out, i);
346    
347          xfclose (out);
348        }
349      obstack_free (&output_obstack, NULL);
350  }  }

Legend:
Removed from v.1.17  
changed lines
  Added in v.1.18

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