Berkeley YACC  1993-03-03
Berkeley's version of Yet Another Compiler Compiler
 All Data Structures Files Functions Variables Typedefs Macros Groups
mkpar.c
Go to the documentation of this file.
1 
2 #include "defs.h"
3 
5 int SRtotal;
6 int RRtotal;
7 short *SRconflicts;
8 short *RRconflicts;
9 short *defred;
10 short *rules_used;
11 short nunused;
13 
14 static int SRcount;
15 static int RRcount;
16 
17 extern action *parse_actions();
18 extern action *get_shifts();
19 extern action *add_reductions();
20 extern action *add_reduce();
21 
22 
24 {
25  register int i;
26 
27  parser = NEW2(nstates, action *);
28  for (i = 0; i < nstates; i++)
29  parser[i] = parse_actions(i);
30 
33  unused_rules();
34  if (SRtotal + RRtotal > 0) total_conflicts();
35  defreds();
36 }
37 
38 
39 action *
40 parse_actions(stateno)
41 register int stateno;
42 {
43  register action *actions;
44 
45  actions = get_shifts(stateno);
46  actions = add_reductions(stateno, actions);
47  return (actions);
48 }
49 
50 
51 action *
52 get_shifts(stateno)
53 int stateno;
54 {
55  register action *actions, *temp;
56  register shifts *sp;
57  register short *to_state;
58  register int i, k;
59  register int symbol;
60 
61  actions = 0;
62  sp = shift_table[stateno];
63  if (sp)
64  {
65  to_state = sp->shift;
66  for (i = sp->nshifts - 1; i >= 0; i--)
67  {
68  k = to_state[i];
69  symbol = accessing_symbol[k];
70  if (ISTOKEN(symbol))
71  {
72  temp = NEW(action);
73  temp->next = actions;
74  temp->symbol = symbol;
75  temp->number = k;
76  temp->prec = symbol_prec[symbol];
77  temp->action_code = SHIFT;
78  temp->assoc = symbol_assoc[symbol];
79  actions = temp;
80  }
81  }
82  }
83  return (actions);
84 }
85 
86 action *
87 add_reductions(stateno, actions)
88 int stateno;
89 register action *actions;
90 {
91  register int i, j, m, n;
92  register int ruleno, tokensetsize;
93  register unsigned *rowp;
94 
95  tokensetsize = WORDSIZE(ntokens);
96  m = lookaheads[stateno];
97  n = lookaheads[stateno + 1];
98  for (i = m; i < n; i++)
99  {
100  ruleno = LAruleno[i];
101  rowp = LA + i * tokensetsize;
102  for (j = ntokens - 1; j >= 0; j--)
103  {
104  if (BIT(rowp, j))
105  actions = add_reduce(actions, ruleno, j);
106  }
107  }
108  return (actions);
109 }
110 
111 
112 action *
113 add_reduce(actions, ruleno, symbol)
114 register action *actions;
115 register int ruleno, symbol;
116 {
117  register action *temp, *prev, *next;
118 
119  prev = 0;
120  for (next = actions; next && next->symbol < symbol; next = next->next)
121  prev = next;
122 
123  while (next && next->symbol == symbol && next->action_code == SHIFT)
124  {
125  prev = next;
126  next = next->next;
127  }
128 
129  while (next && next->symbol == symbol &&
130  next->action_code == REDUCE && next->number < ruleno)
131  {
132  prev = next;
133  next = next->next;
134  }
135 
136  temp = NEW(action);
137  temp->next = next;
138  temp->symbol = symbol;
139  temp->number = ruleno;
140  temp->prec = rprec[ruleno];
141  temp->action_code = REDUCE;
142  temp->assoc = rassoc[ruleno];
143 
144  if (prev)
145  prev->next = temp;
146  else
147  actions = temp;
148 
149  return (actions);
150 }
151 
152 
154 {
155  register int goal, i;
156  register short *to_state;
157  register shifts *p;
158 
159  p = shift_table[0];
160  to_state = p->shift;
161  goal = ritem[1];
162  for (i = p->nshifts - 1; i >= 0; --i)
163  {
164  final_state = to_state[i];
165  if (accessing_symbol[final_state] == goal) break;
166  }
167 }
168 
169 
171 {
172  register int i;
173  register action *p;
174 
175  rules_used = (short *) MALLOC(nrules*sizeof(short));
176  if (rules_used == 0) no_space();
177 
178  for (i = 0; i < nrules; ++i)
179  rules_used[i] = 0;
180 
181  for (i = 0; i < nstates; ++i)
182  {
183  for (p = parser[i]; p; p = p->next)
184  {
185  if (p->action_code == REDUCE && p->suppressed == 0)
186  rules_used[p->number] = 1;
187  }
188  }
189 
190  nunused = 0;
191  for (i = 3; i < nrules; ++i)
192  if (!rules_used[i]) ++nunused;
193 
194  if (nunused)
195  if (nunused == 1)
196  fprintf(stderr, "%s: 1 rule never reduced\n", myname);
197  else
198  fprintf(stderr, "%s: %d rules never reduced\n", myname, nunused);
199 }
200 
201 
203 {
204  register int i;
205  register int symbol;
206  register action *p, *pref;
207 
208  SRtotal = 0;
209  RRtotal = 0;
210  SRconflicts = NEW2(nstates, short);
211  RRconflicts = NEW2(nstates, short);
212  for (i = 0; i < nstates; i++)
213  {
214  SRcount = 0;
215  RRcount = 0;
216  symbol = -1;
217  for (p = parser[i]; p; p = p->next)
218  {
219  if (p->symbol != symbol)
220  {
221  pref = p;
222  symbol = p->symbol;
223  }
224  else if (i == final_state && symbol == 0)
225  {
226  SRcount++;
227  p->suppressed = 1;
228  }
229  else if (pref->action_code == SHIFT)
230  {
231  if (pref->prec > 0 && p->prec > 0)
232  {
233  if (pref->prec < p->prec)
234  {
235  pref->suppressed = 2;
236  pref = p;
237  }
238  else if (pref->prec > p->prec)
239  {
240  p->suppressed = 2;
241  }
242  else if (pref->assoc == LEFT)
243  {
244  pref->suppressed = 2;
245  pref = p;
246  }
247  else if (pref->assoc == RIGHT)
248  {
249  p->suppressed = 2;
250  }
251  else
252  {
253  pref->suppressed = 2;
254  p->suppressed = 2;
255  }
256  }
257  else
258  {
259  SRcount++;
260  p->suppressed = 1;
261  }
262  }
263  else
264  {
265  RRcount++;
266  p->suppressed = 1;
267  }
268  }
269  SRtotal += SRcount;
270  RRtotal += RRcount;
271  SRconflicts[i] = SRcount;
272  RRconflicts[i] = RRcount;
273  }
274 }
275 
276 
278 {
279  fprintf(stderr, "%s: ", myname);
280  if (SRtotal == 1)
281  fprintf(stderr, "1 shift/reduce conflict");
282  else if (SRtotal > 1)
283  fprintf(stderr, "%d shift/reduce conflicts", SRtotal);
284 
285  if (SRtotal && RRtotal)
286  fprintf(stderr, ", ");
287 
288  if (RRtotal == 1)
289  fprintf(stderr, "1 reduce/reduce conflict");
290  else if (RRtotal > 1)
291  fprintf(stderr, "%d reduce/reduce conflicts", RRtotal);
292 
293  fprintf(stderr, ".\n");
294 }
295 
296 
297 int
299 int stateno;
300 {
301  register int count, ruleno;
302  register action *p;
303 
304  count = 0;
305  ruleno = 0;
306  for (p = parser[stateno]; p; p = p->next)
307  {
308  if (p->action_code == SHIFT && p->suppressed == 0)
309  return (0);
310  else if (p->action_code == REDUCE && p->suppressed == 0)
311  {
312  if (ruleno > 0 && p->number != ruleno)
313  return (0);
314  if (p->symbol != 1)
315  ++count;
316  ruleno = p->number;
317  }
318  }
319 
320  if (count == 0)
321  return (0);
322  return (ruleno);
323 }
324 
325 
327 {
328  register int i;
329 
330  defred = NEW2(nstates, short);
331  for (i = 0; i < nstates; i++)
332  defred[i] = sole_reduction(i);
333 }
334 
336 register action *p;
337 {
338  register action *q;
339 
340  while (p)
341  {
342  q = p->next;
343  FREE(p);
344  p = q;
345  }
346 }
347 
349 {
350  register int i;
351 
352  for (i = 0; i < nstates; i++)
353  free_action_row(parser[i]);
354 
355  FREE(parser);
356 }
357 
find_final_state()
Definition: mkpar.c:153
#define BIT(r, n)
Definition: defs.h:27
free_action_row(action *p)
Definition: mkpar.c:335
short number
Definition: defs.h:171
short * LAruleno
Definition: lalr.c:13
short shift[1]
Definition: defs.h:148
unsigned * LA
Definition: lalr.c:14
static int SRcount
Definition: mkpar.c:14
#define MALLOC(n)
Definition: defs.h:103
Definition: defs.h:167
short * accessing_symbol
Definition: lalr.c:15
short * RRconflicts
Definition: mkpar.c:8
#define NEW2(n, t)
Definition: defs.h:105
Definition: defs.h:143
short * lookaheads
Definition: lalr.c:12
total_conflicts()
Definition: mkpar.c:277
int ntokens
The number of tokens (terminals) in the grammar.
Definition: main.c:64
int SRtotal
Definition: mkpar.c:5
char * rassoc
Definition: main.c:128
int sole_reduction(int stateno)
Definition: mkpar.c:298
int nstates
Definition: lr0.c:8
short nunused
Definition: mkpar.c:11
shifts ** shift_table
Definition: lalr.c:17
short final_state
Definition: mkpar.c:12
action * parse_actions()
short * to_state
Definition: lalr.c:21
struct shorts * next
Definition: lalr.c:6
char action_code
Definition: defs.h:173
action ** parser
Definition: mkpar.c:4
#define WORDSIZE(n)
Definition: defs.h:26
short * rules_used
Definition: mkpar.c:10
action * get_shifts()
int nrules
The number of rules in the grammar.
Definition: main.c:45
short * rprec
Definition: main.c:127
short * SRconflicts
Definition: mkpar.c:7
no_space()
Definition: error.c:27
defreds()
Definition: mkpar.c:326
short * symbol_prec
Definition: main.c:92
short nshifts
Definition: defs.h:147
struct action * next
Definition: defs.h:169
make_parser()
Definition: mkpar.c:23
short symbol
Definition: defs.h:170
#define LEFT
Definition: defs.h:57
int RRtotal
Definition: mkpar.c:6
#define SHIFT
Definition: defs.h:82
short * defred
Definition: mkpar.c:9
#define ISTOKEN(s)
Definition: defs.h:95
int tokensetsize
Definition: lalr.c:11
#define RIGHT
Definition: defs.h:58
#define FREE(x)
Definition: defs.h:102
char * myname
Definition: main.c:12
short * ritem
Representation of all productions (and items)
Definition: main.c:112
action * add_reduce()
static int RRcount
Definition: mkpar.c:15
unused_rules()
Definition: mkpar.c:170
char * symbol_assoc
Definition: main.c:93
char assoc
Definition: defs.h:174
bucket * goal
Definition: reader.c:20
action * add_reductions()
short prec
Definition: defs.h:172
free_parser()
Definition: mkpar.c:348
remove_conflicts()
Definition: mkpar.c:202
#define REDUCE
Definition: defs.h:83
#define NEW(t)
Definition: defs.h:104
char suppressed
Definition: defs.h:175