2 /* Id: mkpar.c,v 1.10 2009/10/27 10:50:13 tom Exp */
7 __RCSID("$NetBSD: closure.c,v 1.8 2006/05/24 18:01:43 christos Exp $");
9 static action
*add_reduce(action
*actions
, int ruleno
, int symbol
);
10 static action
*add_reductions(int stateno
, action
*actions
);
11 static action
*get_shifts(int stateno
);
12 static action
*parse_actions(int stateno
);
13 static int sole_reduction(int stateno
);
14 static void defreds(void);
15 static void find_final_state(void);
16 static void free_action_row(action
*p
);
17 static void remove_conflicts(void);
18 static void total_conflicts(void);
19 static void unused_rules(void);
36 static Value_t SRcount
;
37 static Value_t RRcount
;
44 parser
= NEW2(nstates
, action
*);
45 for (i
= 0; i
< nstates
; i
++)
46 parser
[i
] = parse_actions(i
);
51 if (SRtotal
+ RRtotal
> 0)
57 parse_actions(int stateno
)
61 actions
= get_shifts(stateno
);
62 actions
= add_reductions(stateno
, actions
);
67 get_shifts(int stateno
)
69 action
*actions
, *temp
;
76 sp
= shift_table
[stateno
];
79 to_state2
= sp
->shift
;
80 for (i
= (Value_t
) (sp
->nshifts
- 1); i
>= 0; i
--)
83 symbol
= accessing_symbol
[k
];
88 temp
->symbol
= symbol
;
90 temp
->prec
= symbol_prec
[symbol
];
91 temp
->action_code
= SHIFT
;
92 temp
->assoc
= symbol_assoc
[symbol
];
101 add_reductions(int stateno
, action
*actions
)
104 int ruleno
, tokensetsize
;
107 tokensetsize
= WORDSIZE(ntokens
);
108 m
= lookaheads
[stateno
];
109 n
= lookaheads
[stateno
+ 1];
110 for (i
= m
; i
< n
; i
++)
112 ruleno
= LAruleno
[i
];
113 rowp
= LA
+ i
* tokensetsize
;
114 for (j
= ntokens
- 1; j
>= 0; j
--)
117 actions
= add_reduce(actions
, ruleno
, j
);
124 add_reduce(action
*actions
,
128 action
*temp
, *prev
, *next
;
131 for (next
= actions
; next
&& next
->symbol
< symbol
; next
= next
->next
)
134 while (next
&& next
->symbol
== symbol
&& next
->action_code
== SHIFT
)
140 while (next
&& next
->symbol
== symbol
&&
141 next
->action_code
== REDUCE
&& next
->number
< ruleno
)
149 temp
->symbol
= (Value_t
) symbol
;
150 temp
->number
= (Value_t
) ruleno
;
151 temp
->prec
= rprec
[ruleno
];
152 temp
->action_code
= REDUCE
;
153 temp
->assoc
= rassoc
[ruleno
];
164 find_final_state(void)
171 to_state2
= p
->shift
;
173 for (i
= p
->nshifts
- 1; i
>= 0; --i
)
175 final_state
= to_state2
[i
];
176 if (accessing_symbol
[final_state
] == goal
)
187 rules_used
= (Value_t
*) MALLOC((unsigned)nrules
* sizeof(Value_t
));
191 for (i
= 0; i
< nrules
; ++i
)
194 for (i
= 0; i
< nstates
; ++i
)
196 for (p
= parser
[i
]; p
; p
= p
->next
)
198 if (p
->action_code
== REDUCE
&& p
->suppressed
== 0)
199 rules_used
[p
->number
] = 1;
204 for (i
= 3; i
< nrules
; ++i
)
211 fprintf(stderr
, "%s: 1 rule never reduced\n", myname
);
213 fprintf(stderr
, "%s: %d rules never reduced\n", myname
, nunused
);
218 remove_conflicts(void)
222 action
*p
, *pref
= 0;
226 SRconflicts
= NEW2(nstates
, Value_t
);
227 RRconflicts
= NEW2(nstates
, Value_t
);
228 for (i
= 0; i
< nstates
; i
++)
233 for (p
= parser
[i
]; p
; p
= p
->next
)
235 if (p
->symbol
!= symbol
)
240 else if (i
== final_state
&& symbol
== 0)
245 else if (pref
->action_code
== SHIFT
)
247 if (pref
->prec
> 0 && p
->prec
> 0)
249 if (pref
->prec
< p
->prec
)
251 pref
->suppressed
= 2;
254 else if (pref
->prec
> p
->prec
)
258 else if (pref
->assoc
== LEFT
)
260 pref
->suppressed
= 2;
263 else if (pref
->assoc
== RIGHT
)
269 pref
->suppressed
= 2;
287 SRconflicts
[i
] = SRcount
;
288 RRconflicts
[i
] = RRcount
;
293 total_conflicts(void)
295 fprintf(stderr
, "%s: ", myname
);
297 fprintf(stderr
, "1 shift/reduce conflict");
298 else if (SRtotal
> 1)
299 fprintf(stderr
, "%d shift/reduce conflicts", SRtotal
);
301 if (SRtotal
&& RRtotal
)
302 fprintf(stderr
, ", ");
305 fprintf(stderr
, "1 reduce/reduce conflict");
306 else if (RRtotal
> 1)
307 fprintf(stderr
, "%d reduce/reduce conflicts", RRtotal
);
309 fprintf(stderr
, ".\n");
311 if (SRexpect
>= 0 && SRtotal
!= SRexpect
)
313 fprintf(stderr
, "%s: ", myname
);
314 fprintf(stderr
, "expected %d shift/reduce conflict%s.\n",
315 SRexpect
, PLURAL(SRexpect
));
316 exit_code
= EXIT_FAILURE
;
318 if (RRexpect
>= 0 && RRtotal
!= RRexpect
)
320 fprintf(stderr
, "%s: ", myname
);
321 fprintf(stderr
, "expected %d reduce/reduce conflict%s.\n",
322 RRexpect
, PLURAL(RRexpect
));
323 exit_code
= EXIT_FAILURE
;
328 sole_reduction(int stateno
)
335 for (p
= parser
[stateno
]; p
; p
= p
->next
)
337 if (p
->action_code
== SHIFT
&& p
->suppressed
== 0)
339 else if (p
->action_code
== REDUCE
&& p
->suppressed
== 0)
341 if (ruleno
> 0 && p
->number
!= ruleno
)
359 defred
= NEW2(nstates
, Value_t
);
360 for (i
= 0; i
< nstates
; i
++)
361 defred
[i
] = (Value_t
) sole_reduction(i
);
365 free_action_row(action
*p
)
382 for (i
= 0; i
< nstates
; i
++)
383 free_action_row(parser
[i
]);
394 DO_FREE(SRconflicts
);
395 DO_FREE(RRconflicts
);