This is Unofficial EPICS BASE Doxygen Site
verbose.c
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2002 The University of Chicago, as Operator of Argonne
3 * National Laboratory.
4 * Copyright (c) 2002 The Regents of the University of California, as
5 * Operator of Los Alamos National Laboratory.
6 * EPICS BASE is distributed subject to a Software License Agreement found
7 * in file LICENSE that is included with this distribution.
8 \*************************************************************************/
9 
10 #include "defs.h"
11 
12 static short *null_rules;
13 
14 static void log_unused(void);
15 static void log_conflicts(void);
16 static void print_state(int state);
17 static void print_conflicts(int state);
18 static void print_core(int state);
19 static void print_nulls(int state);
20 static void print_actions(int state);
21 static void print_shifts(action *p);
22 static void print_reductions(action *p, int defred);
23 static void print_gotos(int stateno);
24 
25 
26 void
27 verbose(void)
28 {
29  int i;
30 
31  if (!vflag) return;
32 
33  null_rules = (short *) MALLOC(nrules*sizeof(short));
34  if (null_rules == 0) no_space();
35  fprintf(verbose_file, "\f\n");
36  for (i = 0; i < nstates; i++)
37  print_state(i);
38  FREE(null_rules);
39 
40  if (nunused)
41  log_unused();
42  if (SRtotal || RRtotal)
43  log_conflicts();
44 
45  fprintf(verbose_file, "\n\n%d terminals, %d nonterminals\n", ntokens, nvars);
46  fprintf(verbose_file, "%d grammar rules, %d states\n", nrules - 2, nstates);
47 }
48 
49 
50 static void
51 log_unused(void)
52 {
53  int i;
54  short *p;
55 
56  fprintf(verbose_file, "\n\nRules never reduced:\n");
57  for (i = 3; i < nrules; ++i)
58  {
59  if (!rules_used[i])
60  {
61  fprintf(verbose_file, "\t%s :", symbol_name[rlhs[i]]);
62  for (p = ritem + rrhs[i]; *p >= 0; ++p)
63  fprintf(verbose_file, " %s", symbol_name[*p]);
64  fprintf(verbose_file, " (%d)\n", i - 2);
65  }
66  }
67 }
68 
69 
70 static void
71 log_conflicts(void)
72 {
73  int i;
74 
75  fprintf(verbose_file, "\n\n");
76  for (i = 0; i < nstates; i++)
77  {
78  if (SRconflicts[i] || RRconflicts[i])
79  {
80  fprintf(verbose_file, "State %d contains ", i);
81  if (SRconflicts[i] == 1)
82  fprintf(verbose_file, "1 shift/reduce conflict");
83  else if (SRconflicts[i] > 1)
84  fprintf(verbose_file, "%d shift/reduce conflicts",
85  SRconflicts[i]);
86  if (SRconflicts[i] && RRconflicts[i])
87  fprintf(verbose_file, ", ");
88  if (RRconflicts[i] == 1)
89  fprintf(verbose_file, "1 reduce/reduce conflict");
90  else if (RRconflicts[i] > 1)
91  fprintf(verbose_file, "%d reduce/reduce conflicts",
92  RRconflicts[i]);
93  fprintf(verbose_file, ".\n");
94  }
95  }
96 }
97 
98 
99 static void
100 print_state(int state)
101 {
102  if (state)
103  fprintf(verbose_file, "\n\n");
104  if (SRconflicts[state] || RRconflicts[state])
105  print_conflicts(state);
106  fprintf(verbose_file, "state %d\n", state);
107  print_core(state);
108  print_nulls(state);
109  print_actions(state);
110 }
111 
112 
113 static void
114 print_conflicts(int state)
115 {
116  int symbol, act = 0, number = 0;
117  action *p;
118 
119  symbol = -1;
120  for (p = parser[state]; p; p = p->next)
121  {
122  if (p->suppressed == 2)
123  continue;
124 
125  if (p->symbol != symbol)
126  {
127  symbol = p->symbol;
128  number = p->number;
129  if (p->action_code == SHIFT)
130  act = SHIFT;
131  else
132  act = REDUCE;
133  }
134  else if (p->suppressed == 1)
135  {
136  if (state == final_state && symbol == 0)
137  {
138  fprintf(verbose_file, "%d: shift/reduce conflict \
139 (accept, reduce %d) on $end\n", state, p->number - 2);
140  }
141  else
142  {
143  if (act == SHIFT)
144  {
145  fprintf(verbose_file, "%d: shift/reduce conflict \
146 (shift %d, reduce %d) on %s\n", state, number, p->number - 2,
147  symbol_name[symbol]);
148  }
149  else
150  {
151  fprintf(verbose_file, "%d: reduce/reduce conflict \
152 (reduce %d, reduce %d) on %s\n", state, number - 2, p->number - 2,
153  symbol_name[symbol]);
154  }
155  }
156  }
157  }
158 }
159 
160 
161 static void
162 print_core(int state)
163 {
164  int i;
165  int k;
166  int rule;
167  core *statep;
168  short *sp;
169  short *sp1;
170 
171  statep = state_table[state];
172  k = statep->nitems;
173 
174  for (i = 0; i < k; i++)
175  {
176  sp1 = sp = ritem + statep->items[i];
177 
178  while (*sp >= 0) ++sp;
179  rule = -(*sp);
180  fprintf(verbose_file, "\t%s : ", symbol_name[rlhs[rule]]);
181 
182  for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
183  fprintf(verbose_file, "%s ", symbol_name[*sp]);
184 
185  putc('.', verbose_file);
186 
187  while (*sp >= 0)
188  {
189  fprintf(verbose_file, " %s", symbol_name[*sp]);
190  sp++;
191  }
192  fprintf(verbose_file, " (%d)\n", -2 - *sp);
193  }
194 }
195 
196 
197 static void
198 print_nulls(int state)
199 {
200  action *p;
201  int i, j, k, nnulls;
202 
203  nnulls = 0;
204  for (p = parser[state]; p; p = p->next)
205  {
206  if (p->action_code == REDUCE &&
207  (p->suppressed == 0 || p->suppressed == 1))
208  {
209  i = p->number;
210  if (rrhs[i] + 1 == rrhs[i+1])
211  {
212  for (j = 0; j < nnulls && i > null_rules[j]; ++j)
213  continue;
214 
215  if (j == nnulls)
216  {
217  ++nnulls;
218  null_rules[j] = i;
219  }
220  else if (i != null_rules[j])
221  {
222  ++nnulls;
223  for (k = nnulls - 1; k > j; --k)
224  null_rules[k] = null_rules[k-1];
225  null_rules[j] = i;
226  }
227  }
228  }
229  }
230 
231  for (i = 0; i < nnulls; ++i)
232  {
233  j = null_rules[i];
234  fprintf(verbose_file, "\t%s : . (%d)\n", symbol_name[rlhs[j]],
235  j - 2);
236  }
237  fprintf(verbose_file, "\n");
238 }
239 
240 
241 static void
242 print_actions(int stateno)
243 {
244  action *p;
245  shifts *sp;
246  int as;
247 
248  if (stateno == final_state)
249  fprintf(verbose_file, "\t$end accept\n");
250 
251  p = parser[stateno];
252  if (p)
253  {
254  print_shifts(p);
255  print_reductions(p, defred[stateno]);
256  }
257 
258  sp = shift_table[stateno];
259  if (sp && sp->nshifts > 0)
260  {
261  as = accessing_symbol[sp->shift[sp->nshifts - 1]];
262  if (ISVAR(as))
263  print_gotos(stateno);
264  }
265 }
266 
267 
268 static void
269 print_shifts(action *p)
270 {
271  int count;
272  action *q;
273 
274  count = 0;
275  for (q = p; q; q = q->next)
276  {
277  if (q->suppressed < 2 && q->action_code == SHIFT)
278  ++count;
279  }
280 
281  if (count > 0)
282  {
283  for (; p; p = p->next)
284  {
285  if (p->action_code == SHIFT && p->suppressed == 0)
286  fprintf(verbose_file, "\t%s shift %d\n",
287  symbol_name[p->symbol], p->number);
288  }
289  }
290 }
291 
292 
293 static void
294 print_reductions(action *p, int defred)
295 {
296  int k, anyreds;
297  action *q;
298 
299  anyreds = 0;
300  for (q = p; q ; q = q->next)
301  {
302  if (q->action_code == REDUCE && q->suppressed < 2)
303  {
304  anyreds = 1;
305  break;
306  }
307  }
308 
309  if (anyreds == 0)
310  fprintf(verbose_file, "\t. error\n");
311  else
312  {
313  for (; p; p = p->next)
314  {
315  if (p->action_code == REDUCE && p->number != defred)
316  {
317  k = p->number - 2;
318  if (p->suppressed == 0)
319  fprintf(verbose_file, "\t%s reduce %d\n",
320  symbol_name[p->symbol], k);
321  }
322  }
323 
324  if (defred > 0)
325  fprintf(verbose_file, "\t. reduce %d\n", defred - 2);
326  }
327 }
328 
329 
330 static void
331 print_gotos(int stateno)
332 {
333  int i, k;
334  int as;
335  short *to_state;
336  shifts *sp;
337 
338  putc('\n', verbose_file);
339  sp = shift_table[stateno];
340  to_state = sp->shift;
341  for (i = 0; i < sp->nshifts; ++i)
342  {
343  k = to_state[i];
344  as = accessing_symbol[k];
345  if (ISVAR(as))
346  fprintf(verbose_file, "\t%s goto %d\n", symbol_name[as], k);
347  }
348 }
349 
FILE * verbose_file
Definition: antelope.c:53
short nunused
Definition: mkpar.c:19
short number
Definition: defs.h:179
short final_state
Definition: mkpar.c:20
short shift[1]
Definition: defs.h:156
#define MALLOC(n)
Definition: defs.h:111
int i
Definition: scan.c:967
Definition: defs.h:175
short * rlhs
Definition: antelope.c:68
short * accessing_symbol
Definition: lalr.c:23
Definition: defs.h:151
int nstates
Definition: lr0.c:16
short * rules_used
Definition: mkpar.c:18
int nrules
Definition: antelope.c:56
#define ISVAR(s)
Definition: defs.h:104
short * SRconflicts
Definition: mkpar.c:15
shifts ** shift_table
Definition: lalr.c:25
short * to_state
Definition: lalr.c:29
action ** parser
Definition: mkpar.c:12
char action_code
Definition: defs.h:181
int nvars
Definition: antelope.c:59
int RRtotal
Definition: mkpar.c:14
short items[1]
Definition: defs.h:144
short nshifts
Definition: defs.h:155
struct action * next
Definition: defs.h:177
Definition: defs.h:137
short * ritem
Definition: antelope.c:67
short * defred
Definition: mkpar.c:17
short * RRconflicts
Definition: mkpar.c:16
short symbol
Definition: defs.h:178
core ** state_table
Definition: lalr.c:24
#define SHIFT
Definition: defs.h:90
#define FREE(x)
Definition: defs.h:110
short * rrhs
Definition: antelope.c:69
char vflag
Definition: antelope.c:26
short nitems
Definition: defs.h:143
int ntokens
Definition: antelope.c:58
void verbose(void)
Definition: verbose.c:27
char ** symbol_name
Definition: antelope.c:62
void no_space(void) NORETURN
Definition: error.c:23
#define REDUCE
Definition: defs.h:91
int SRtotal
Definition: mkpar.c:13
char suppressed
Definition: defs.h:183