Berkeley YACC  1993-03-03
Berkeley's version of Yet Another Compiler Compiler
 All Data Structures Files Functions Variables Typedefs Macros Groups
mkpar.c File Reference
#include "defs.h"
+ Include dependency graph for mkpar.c:

Go to the source code of this file.

Functions

actionparse_actions ()
 
actionget_shifts ()
 
actionadd_reductions ()
 
actionadd_reduce ()
 
 make_parser ()
 
actionparse_actions (int stateno)
 
actionget_shifts (int stateno)
 
actionadd_reductions (int stateno, action *actions)
 
actionadd_reduce (action *actions, int ruleno, int symbol)
 
 find_final_state ()
 
 unused_rules ()
 
 remove_conflicts ()
 
 total_conflicts ()
 
int sole_reduction (int stateno)
 
 defreds ()
 
 free_action_row (action *p)
 
 free_parser ()
 

Variables

action ** parser
 
int SRtotal
 
int RRtotal
 
short * SRconflicts
 
short * RRconflicts
 
short * defred
 
short * rules_used
 
short nunused
 
short final_state
 
static int SRcount
 
static int RRcount
 

Function Documentation

action* add_reduce ( )

Referenced by add_reductions().

+ Here is the caller graph for this function:

action* add_reduce ( action actions,
int  ruleno,
int  symbol 
)

Definition at line 113 of file mkpar.c.

References action::action_code, action::assoc, NEW, shorts::next, action::next, action::number, action::prec, rassoc, REDUCE, rprec, SHIFT, and action::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 }
short number
Definition: defs.h:171
Definition: defs.h:167
char * rassoc
Definition: main.c:128
char action_code
Definition: defs.h:173
short * rprec
Definition: main.c:127
struct action * next
Definition: defs.h:169
short symbol
Definition: defs.h:170
#define SHIFT
Definition: defs.h:82
char assoc
Definition: defs.h:174
short prec
Definition: defs.h:172
#define REDUCE
Definition: defs.h:83
#define NEW(t)
Definition: defs.h:104
action* add_reductions ( )

Referenced by parse_actions().

+ Here is the caller graph for this function:

action* add_reductions ( int  stateno,
action actions 
)

Definition at line 87 of file mkpar.c.

References add_reduce(), BIT, LA, LAruleno, lookaheads, ntokens, tokensetsize, and WORDSIZE.

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 }
#define BIT(r, n)
Definition: defs.h:27
short * LAruleno
Definition: lalr.c:13
unsigned * LA
Definition: lalr.c:14
short * lookaheads
Definition: lalr.c:12
int ntokens
The number of tokens (terminals) in the grammar.
Definition: main.c:64
#define WORDSIZE(n)
Definition: defs.h:26
int tokensetsize
Definition: lalr.c:11
action * add_reduce()

+ Here is the call graph for this function:

defreds ( )

Definition at line 326 of file mkpar.c.

References defred, NEW2, nstates, and sole_reduction().

Referenced by make_parser().

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 }
#define NEW2(n, t)
Definition: defs.h:105
int sole_reduction(int stateno)
Definition: mkpar.c:298
int nstates
Definition: lr0.c:8
short * defred
Definition: mkpar.c:9

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

find_final_state ( )

Definition at line 153 of file mkpar.c.

References accessing_symbol, final_state, goal, shifts::nshifts, ritem, shifts::shift, shift_table, and to_state.

Referenced by make_parser().

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 }
short shift[1]
Definition: defs.h:148
short * accessing_symbol
Definition: lalr.c:15
Definition: defs.h:143
shifts ** shift_table
Definition: lalr.c:17
short final_state
Definition: mkpar.c:12
short * to_state
Definition: lalr.c:21
short nshifts
Definition: defs.h:147
short * ritem
Representation of all productions (and items)
Definition: main.c:112
bucket * goal
Definition: reader.c:20

+ Here is the caller graph for this function:

free_action_row ( action p)

Definition at line 335 of file mkpar.c.

References FREE, and action::next.

Referenced by free_parser().

337 {
338  register action *q;
339 
340  while (p)
341  {
342  q = p->next;
343  FREE(p);
344  p = q;
345  }
346 }
Definition: defs.h:167
struct action * next
Definition: defs.h:169
#define FREE(x)
Definition: defs.h:102

+ Here is the caller graph for this function:

free_parser ( )

Definition at line 348 of file mkpar.c.

References FREE, free_action_row(), and nstates.

Referenced by output().

349 {
350  register int i;
351 
352  for (i = 0; i < nstates; i++)
354 
355  FREE(parser);
356 }
free_action_row(action *p)
Definition: mkpar.c:335
int nstates
Definition: lr0.c:8
action ** parser
Definition: mkpar.c:4
#define FREE(x)
Definition: defs.h:102

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

action* get_shifts ( )

Referenced by parse_actions().

+ Here is the caller graph for this function:

action* get_shifts ( int  stateno)

Definition at line 52 of file mkpar.c.

References accessing_symbol, action::action_code, action::assoc, ISTOKEN, NEW, action::next, shifts::nshifts, action::number, action::prec, SHIFT, shifts::shift, shift_table, action::symbol, symbol_assoc, symbol_prec, and to_state.

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 }
short number
Definition: defs.h:171
short shift[1]
Definition: defs.h:148
Definition: defs.h:167
short * accessing_symbol
Definition: lalr.c:15
Definition: defs.h:143
shifts ** shift_table
Definition: lalr.c:17
short * to_state
Definition: lalr.c:21
char action_code
Definition: defs.h:173
short * symbol_prec
Definition: main.c:92
short nshifts
Definition: defs.h:147
struct action * next
Definition: defs.h:169
short symbol
Definition: defs.h:170
#define SHIFT
Definition: defs.h:82
#define ISTOKEN(s)
Definition: defs.h:95
char * symbol_assoc
Definition: main.c:93
char assoc
Definition: defs.h:174
short prec
Definition: defs.h:172
#define NEW(t)
Definition: defs.h:104
make_parser ( )

Definition at line 23 of file mkpar.c.

References defreds(), find_final_state(), NEW2, nstates, parse_actions(), remove_conflicts(), RRtotal, SRtotal, total_conflicts(), and unused_rules().

Referenced by main().

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 }
find_final_state()
Definition: mkpar.c:153
Definition: defs.h:167
#define NEW2(n, t)
Definition: defs.h:105
total_conflicts()
Definition: mkpar.c:277
int SRtotal
Definition: mkpar.c:5
int nstates
Definition: lr0.c:8
action * parse_actions()
action ** parser
Definition: mkpar.c:4
defreds()
Definition: mkpar.c:326
int RRtotal
Definition: mkpar.c:6
unused_rules()
Definition: mkpar.c:170
remove_conflicts()
Definition: mkpar.c:202

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

action* parse_actions ( )

Referenced by make_parser().

+ Here is the caller graph for this function:

action* parse_actions ( int  stateno)

Definition at line 40 of file mkpar.c.

References add_reductions(), and get_shifts().

42 {
43  register action *actions;
44 
45  actions = get_shifts(stateno);
46  actions = add_reductions(stateno, actions);
47  return (actions);
48 }
Definition: defs.h:167
action * get_shifts()
action * add_reductions()

+ Here is the call graph for this function:

remove_conflicts ( )

Definition at line 202 of file mkpar.c.

References action::action_code, action::assoc, final_state, LEFT, NEW2, action::next, nstates, action::prec, RIGHT, RRconflicts, RRcount, RRtotal, SHIFT, SRconflicts, SRcount, SRtotal, action::suppressed, and action::symbol.

Referenced by make_parser().

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 }
static int SRcount
Definition: mkpar.c:14
Definition: defs.h:167
short * RRconflicts
Definition: mkpar.c:8
#define NEW2(n, t)
Definition: defs.h:105
int SRtotal
Definition: mkpar.c:5
int nstates
Definition: lr0.c:8
short final_state
Definition: mkpar.c:12
char action_code
Definition: defs.h:173
action ** parser
Definition: mkpar.c:4
short * SRconflicts
Definition: mkpar.c:7
struct action * next
Definition: defs.h:169
short symbol
Definition: defs.h:170
#define LEFT
Definition: defs.h:57
int RRtotal
Definition: mkpar.c:6
#define SHIFT
Definition: defs.h:82
#define RIGHT
Definition: defs.h:58
static int RRcount
Definition: mkpar.c:15
char assoc
Definition: defs.h:174
short prec
Definition: defs.h:172
char suppressed
Definition: defs.h:175

+ Here is the caller graph for this function:

int sole_reduction ( int  stateno)

Definition at line 298 of file mkpar.c.

References action::action_code, action::next, action::number, REDUCE, SHIFT, action::suppressed, and action::symbol.

Referenced by defreds().

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 }
short number
Definition: defs.h:171
Definition: defs.h:167
char action_code
Definition: defs.h:173
action ** parser
Definition: mkpar.c:4
struct action * next
Definition: defs.h:169
short symbol
Definition: defs.h:170
#define SHIFT
Definition: defs.h:82
#define REDUCE
Definition: defs.h:83
char suppressed
Definition: defs.h:175

+ Here is the caller graph for this function:

total_conflicts ( )

Definition at line 277 of file mkpar.c.

References myname, RRtotal, and SRtotal.

Referenced by make_parser().

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 }
int SRtotal
Definition: mkpar.c:5
int RRtotal
Definition: mkpar.c:6
char * myname
Definition: main.c:12

+ Here is the caller graph for this function:

unused_rules ( )

Definition at line 170 of file mkpar.c.

References action::action_code, MALLOC, myname, action::next, no_space(), nrules, nstates, action::number, nunused, REDUCE, rules_used, and action::suppressed.

Referenced by make_parser().

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 }
short number
Definition: defs.h:171
#define MALLOC(n)
Definition: defs.h:103
Definition: defs.h:167
int nstates
Definition: lr0.c:8
short nunused
Definition: mkpar.c:11
char action_code
Definition: defs.h:173
action ** parser
Definition: mkpar.c:4
short * rules_used
Definition: mkpar.c:10
int nrules
The number of rules in the grammar.
Definition: main.c:45
no_space()
Definition: error.c:27
struct action * next
Definition: defs.h:169
char * myname
Definition: main.c:12
#define REDUCE
Definition: defs.h:83
char suppressed
Definition: defs.h:175

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Variable Documentation

short* defred

Definition at line 9 of file mkpar.c.

Referenced by defreds(), output_yydefred(), print_actions(), and token_actions().

short final_state
short nunused

Definition at line 11 of file mkpar.c.

Referenced by unused_rules(), and verbose().

action** parser

Definition at line 4 of file mkpar.c.

Referenced by print_actions(), print_conflicts(), print_nulls(), and token_actions().

short* RRconflicts

Definition at line 8 of file mkpar.c.

Referenced by log_conflicts(), print_state(), and remove_conflicts().

int RRcount
static

Definition at line 15 of file mkpar.c.

Referenced by remove_conflicts().

int RRtotal

Definition at line 6 of file mkpar.c.

Referenced by make_parser(), remove_conflicts(), total_conflicts(), and verbose().

short* rules_used

Definition at line 10 of file mkpar.c.

Referenced by log_unused(), and unused_rules().

short* SRconflicts

Definition at line 7 of file mkpar.c.

Referenced by log_conflicts(), print_state(), and remove_conflicts().

int SRcount
static

Definition at line 14 of file mkpar.c.

Referenced by remove_conflicts().

int SRtotal

Definition at line 5 of file mkpar.c.

Referenced by make_parser(), remove_conflicts(), total_conflicts(), and verbose().