]>
git.saurik.com Git - bison.git/blob - src/print.c
648e3e6cbf9fd291b30a1ad8c1c1d0ad11d01c11
   1 /* Print information on generated parser, for bison, 
   2    Copyright 1984, 1986, 1989, 2000, 2001 Free Software Foundation, Inc. 
   4    This file is part of Bison, the GNU Compiler Compiler. 
   6    Bison is free software; you can redistribute it and/or modify 
   7    it under the terms of the GNU General Public License as published by 
   8    the Free Software Foundation; either version 2, or (at your option) 
  11    Bison is distributed in the hope that it will be useful, 
  12    but WITHOUT ANY WARRANTY; without even the implied warranty of 
  13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  14    GNU General Public License for more details. 
  16    You should have received a copy of the GNU General Public License 
  17    along with Bison; see the file COPYING.  If not, write to 
  18    the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 
  19    Boston, MA 02111-1307, USA.  */ 
  28 #include "conflicts.h" 
  36 static unsigned *shiftset 
= NULL
; 
  37 static unsigned *lookaheadset 
= NULL
; 
  41 print_token (int extnum
, int token
) 
  43   fprintf (out
, _(" type %d is %s\n"), extnum
, tags
[token
]); 
  47 static inline const char * 
  48 escape (const char *s
) 
  50   return quotearg_n_style (1, escape_quoting_style
, s
); 
  53 /* Be cautious not to use twice the same slot in a single expression. */ 
  54 static inline const char * 
  55 escape2 (const char *s
) 
  57   return quotearg_n_style (2, escape_quoting_style
, s
); 
  61 /*--------------------------------. 
  62 | Report information on a state.  | 
  63 `--------------------------------*/ 
  66 print_core (FILE *out
, state_t 
*state
) 
  69   short *sitems 
= state
->items
; 
  70   int snitems   
= state
->nitems
; 
  72   /* New experimental feature: if TRACE_FLAGS output all the items of 
  73      a state, not only its kernel.  */ 
  76       closure (sitems
, snitems
); 
  83       for (i 
= 0; i 
< snitems
; i
++) 
  89           sp1 
= sp 
= ritem 
+ sitems
[i
]; 
  95           fprintf (out
, "    %s  ->  ", escape (tags
[rule_table
[rule
].lhs
])); 
  97           for (sp 
= ritem 
+ rule_table
[rule
].rhs
; sp 
< sp1
; sp
++) 
  98             fprintf (out
, "%s ", escape (tags
[*sp
])); 
 102           for (/* Nothing */; *sp 
> 0; ++sp
) 
 103             fprintf (out
, " %s", escape (tags
[*sp
])); 
 105           fprintf (out
, _("   (rule %d)"), rule
); 
 115 print_shifts (FILE *out
, state_t 
*state
) 
 118   shifts 
*shiftp 
= state
->shifts
; 
 120   for (i 
= 0; i 
< shiftp
->nshifts 
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++) 
 121     if (!SHIFT_IS_DISABLED (shiftp
, i
)) 
 123         int state1 
= shiftp
->shifts
[i
]; 
 124         int symbol 
= state_table
[state1
]->accessing_symbol
; 
 126                  _("    %-4s\tshift, and go to state %d\n"), 
 127                  escape (tags
[symbol
]), state1
); 
 136 print_errs (FILE *out
, state_t 
*state
) 
 138   errs 
*errp 
= state
->errs
; 
 141   for (i 
= 0; i 
< errp
->nerrs
; ++i
) 
 143       fprintf (out
, _("    %-4s\terror (nonassociative)\n"), 
 144                escape (tags
[errp
->errs
[i
]])); 
 152 print_gotos (FILE *out
, state_t 
*state
) 
 155   shifts 
*shiftp 
= state
->shifts
; 
 157   for (i 
= 0; i 
< shiftp
->nshifts 
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++) 
 158     /* Skip token shifts.  */; 
 160   if (i 
< shiftp
->nshifts
) 
 162       for (; i 
< shiftp
->nshifts
; i
++) 
 163         if (!SHIFT_IS_DISABLED (shiftp
, i
)) 
 165             int state1 
= shiftp
->shifts
[i
]; 
 166             int symbol 
= state_table
[state1
]->accessing_symbol
; 
 167             fprintf (out
, _("    %-4s\tgo to state %d\n"), 
 168                      escape (tags
[symbol
]), state1
); 
 176 print_reductions (FILE *out
, state_t 
*state
) 
 179   shifts 
*shiftp 
= state
->shifts
; 
 180   reductions 
*redp 
= state
->reductions
; 
 181   errs 
*errp 
= state
->errs
; 
 184   if (redp
->nreds 
== 0) 
 187   if (state
->consistent
) 
 189       int rule 
= redp
->rules
[0]; 
 190       int symbol 
= rule_table
[rule
].lhs
; 
 191       fprintf (out
, _("    $default\treduce using rule %d (%s)\n\n"), 
 192                rule
, escape (tags
[symbol
])); 
 196   for (i 
= 0; i 
< tokensetsize
; i
++) 
 199   for (i 
= 0; i 
< shiftp
->nshifts 
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++) 
 200     if (!SHIFT_IS_DISABLED (shiftp
, i
)) 
 202         /* if this state has a shift for the error token, don't use a 
 204         if (SHIFT_IS_ERROR (shiftp
, i
)) 
 206         SETBIT (shiftset
, SHIFT_SYMBOL (shiftp
, i
)); 
 209   for (i 
= 0; i 
< errp
->nerrs
; i
++) 
 211       SETBIT (shiftset
, errp
->errs
[i
]); 
 213   if (state
->nlookaheads 
== 1 && !nodefault
) 
 216       int default_rule 
= LAruleno
[state
->lookaheadsp
]; 
 218       for (k 
= 0; k 
< tokensetsize
; ++k
) 
 219         lookaheadset
[k
] = LA (state
->lookaheadsp
)[k
] & shiftset
[k
]; 
 221       for (i 
= 0; i 
< ntokens
; i
++) 
 222         if (BITISSET (lookaheadset
, i
)) 
 223           fprintf (out
, _("    %-4s\t[reduce using rule %d (%s)]\n"), 
 224                    escape (tags
[i
]), default_rule
, 
 225                    escape2 (tags
[rule_table
[default_rule
].lhs
])); 
 227       fprintf (out
, _("    $default\treduce using rule %d (%s)\n\n"), 
 228                default_rule
, escape (tags
[rule_table
[default_rule
].lhs
])); 
 230   else if (state
->nlookaheads 
>= 1) 
 234       int default_rule 
= 0; 
 237         for (i 
= 0; i 
< state
->nlookaheads
; ++i
) 
 242             for (k 
= 0; k 
< tokensetsize
; ++k
) 
 243               lookaheadset
[k
] = LA (state
->lookaheadsp 
+ i
)[k
] & ~shiftset
[k
]; 
 245             for (j 
= 0; j 
< ntokens
; j
++) 
 246               if (BITISSET (lookaheadset
, j
)) 
 252                 default_LA 
= state
->lookaheadsp 
+ i
; 
 253                 default_rule 
= LAruleno
[state
->lookaheadsp 
+ i
]; 
 256             for (k 
= 0; k 
< tokensetsize
; ++k
) 
 257               shiftset
[k
] |= lookaheadset
[k
]; 
 260       for (i 
= 0; i 
< tokensetsize
; i
++) 
 263       for (i 
= 0; i 
< shiftp
->nshifts 
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++) 
 264         if (!SHIFT_IS_DISABLED (shiftp
, i
)) 
 265           SETBIT (shiftset
, SHIFT_SYMBOL (shiftp
, i
)); 
 267       for (i 
= 0; i 
< ntokens
; i
++) 
 271           int count 
= BITISSET (shiftset
, i
); 
 273           for (j 
= 0; j 
< state
->nlookaheads
; ++j
) 
 275               if (BITISSET (LA (state
->lookaheadsp 
+ j
), i
)) 
 279                       if (state
->lookaheadsp 
+ j 
!= default_LA
) 
 281                                  _("    %-4s\treduce using rule %d (%s)\n"), 
 283                                  LAruleno
[state
->lookaheadsp 
+ j
], 
 284                                  escape2 (tags
[rule_table
[LAruleno
[state
->lookaheadsp 
+ j
]].lhs
])); 
 294                                  _("    %-4s\treduce using rule %d (%s)\n"), 
 296                                  LAruleno
[default_LA
], 
 297                                  escape2 (tags
[rule_table
[LAruleno
[default_LA
]].lhs
])); 
 300                                _("    %-4s\t[reduce using rule %d (%s)]\n"), 
 302                                LAruleno
[state
->lookaheadsp 
+ j
], 
 303                                escape2 (tags
[rule_table
[LAruleno
[state
->lookaheadsp 
+ j
]].lhs
])); 
 310         fprintf (out
, _("    $default\treduce using rule %d (%s)\n"), 
 311                  default_rule
, escape (tags
[rule_table
[default_rule
].lhs
])); 
 317 print_actions (FILE *out
, state_t 
*state
) 
 319   reductions 
*redp 
= state
->reductions
; 
 320   shifts 
*shiftp 
= state
->shifts
; 
 322   if (shiftp
->nshifts 
== 0 && redp
->nreds 
== 0) 
 324       if (final_state 
== state
->number
) 
 325         fprintf (out
, _("    $default\taccept\n")); 
 327         fprintf (out
, _("    NO ACTIONS\n")); 
 331   print_shifts (out
, state
); 
 332   print_errs (out
, state
); 
 333   print_reductions (out
, state
); 
 334   print_gotos (out
, state
); 
 338 print_state (FILE *out
, state_t 
*state
) 
 340   fprintf (out
, _("state %d"), state
->number
); 
 342   print_core (out
, state
); 
 343   print_actions (out
, state
); 
 347 /*-----------------------------------------. 
 348 | Print information on the whole grammar.  | 
 349 `-----------------------------------------*/ 
 351 #define END_TEST(End)                           \ 
 353   if (column + strlen(buffer) > (End))          \ 
 355       fprintf (out, "%s\n   ", buffer);         \ 
 363 print_grammar (FILE *out
) 
 370   /* rule # : LHS -> RHS */ 
 371   fprintf (out
, "%s\n\n", _("Grammar")); 
 372   fprintf (out
, "  %s\n", _("Number, Line, Rule")); 
 373   for (i 
= 1; i 
<= nrules
; i
++) 
 374     /* Don't print rules disabled in reduce_grammar_tables.  */ 
 375     if (rule_table
[i
].useful
) 
 377         fprintf (out
, _("  %3d %3d %s ->"), 
 378                  i
, rule_table
[i
].line
, escape (tags
[rule_table
[i
].lhs
])); 
 379         rule 
= &ritem
[rule_table
[i
].rhs
]; 
 382             fprintf (out
, " %s", escape (tags
[*rule
++])); 
 384           fprintf (out
, " /* %s */", _("empty")); 
 390   /* TERMINAL (type #) : rule #s terminal is on RHS */ 
 391   fprintf (out
, "%s\n\n", _("Terminals, with rules where they appear")); 
 392   fprintf (out
, "%s (-1)\n", escape (tags
[0])); 
 394   for (i 
= 0; i 
<= max_user_token_number
; i
++) 
 395     if (token_translations
[i
] != 2) 
 398         column 
= strlen (escape (tags
[token_translations
[i
]])); 
 399         fputs (escape (tags
[token_translations
[i
]]), out
); 
 401         sprintf (buffer
, " (%d)", i
); 
 403         for (j 
= 1; j 
<= nrules
; j
++) 
 404           for (rule 
= &ritem
[rule_table
[j
].rhs
]; *rule 
> 0; rule
++) 
 405             if (*rule 
== token_translations
[i
]) 
 408                 sprintf (buffer 
+ strlen (buffer
), " %d", j
); 
 411         fprintf (out
, "%s\n", buffer
); 
 416   fprintf (out
, "%s\n\n", _("Nonterminals, with rules where they appear")); 
 417   for (i 
= ntokens
; i 
<= nsyms 
- 1; i
++) 
 419       int left_count 
= 0, right_count 
= 0; 
 421       for (j 
= 1; j 
<= nrules
; j
++) 
 423           if (rule_table
[j
].lhs 
== i
) 
 425           for (rule 
= &ritem
[rule_table
[j
].rhs
]; *rule 
> 0; rule
++) 
 434       fputs (escape (tags
[i
]), out
); 
 435       column 
= strlen (escape (tags
[i
])); 
 436       sprintf (buffer
, " (%d)", i
); 
 442           sprintf (buffer 
+ strlen (buffer
), _(" on left:")); 
 444           for (j 
= 1; j 
<= nrules
; j
++) 
 447               if (rule_table
[j
].lhs 
== i
) 
 448                 sprintf (buffer 
+ strlen (buffer
), " %d", j
); 
 455             sprintf (buffer 
+ strlen (buffer
), ","); 
 457           sprintf (buffer 
+ strlen (buffer
), _(" on right:")); 
 458           for (j 
= 1; j 
<= nrules
; j
++) 
 460               for (rule 
= &ritem
[rule_table
[j
].rhs
]; *rule 
> 0; rule
++) 
 464                     sprintf (buffer 
+ strlen (buffer
), " %d", j
); 
 469       fprintf (out
, "%s\n", buffer
); 
 479   /* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but 
 480      that conflicts with Posix.  */ 
 481   FILE *out 
= xfopen (spec_verbose_file
, "w"); 
 483   size_t size 
= obstack_object_size (&output_obstack
); 
 484   fwrite (obstack_finish (&output_obstack
), 1, size
, out
); 
 485   obstack_free (&output_obstack
, NULL
); 
 491   conflicts_output (out
); 
 495   /* New experimental feature: output all the items of a state, not 
 496      only its kernel.  Requires to run closure, which need memory 
 497      allocation/deallocation.  */ 
 499     new_closure (nitems
); 
 500   /* Storage for print_reductions.  */ 
 501   shiftset 
= XCALLOC (unsigned, tokensetsize
); 
 502   lookaheadset 
= XCALLOC (unsigned, tokensetsize
); 
 503   for (i 
= 0; i 
< nstates
; i
++) 
 504     print_state (out
, state_table
[i
]);