1 /* $Id: mkpar.c,v 1.11 2010/06/09 08:53:17 tom Exp $ */
5 static action
*add_reduce(action
*actions
, int ruleno
, int symbol
);
6 static action
*add_reductions(int stateno
, action
*actions
);
7 static action
*get_shifts(int stateno
);
8 static action
*parse_actions(int stateno
);
9 static int sole_reduction(int stateno
);
10 static void defreds(void);
11 static void find_final_state(void);
12 static void free_action_row(action
*p
);
13 static void remove_conflicts(void);
14 static void total_conflicts(void);
15 static void unused_rules(void);
32 static Value_t SRcount
;
33 static Value_t RRcount
;
40 parser
= NEW2(nstates
, action
*);
41 for (i
= 0; i
< nstates
; i
++)
42 parser
[i
] = parse_actions(i
);
47 if (SRtotal
+ RRtotal
> 0)
53 parse_actions(int stateno
)
57 actions
= get_shifts(stateno
);
58 actions
= add_reductions(stateno
, actions
);
63 get_shifts(int stateno
)
65 action
*actions
, *temp
;
72 sp
= shift_table
[stateno
];
75 to_state2
= sp
->shift
;
76 for (i
= (Value_t
) (sp
->nshifts
- 1); i
>= 0; i
--)
79 symbol
= accessing_symbol
[k
];
84 temp
->symbol
= symbol
;
86 temp
->prec
= symbol_prec
[symbol
];
87 temp
->action_code
= SHIFT
;
88 temp
->assoc
= symbol_assoc
[symbol
];
97 add_reductions(int stateno
, action
*actions
)
100 int ruleno
, tokensetsize
;
103 tokensetsize
= WORDSIZE(ntokens
);
104 m
= lookaheads
[stateno
];
105 n
= lookaheads
[stateno
+ 1];
106 for (i
= m
; i
< n
; i
++)
108 ruleno
= LAruleno
[i
];
109 rowp
= LA
+ i
* tokensetsize
;
110 for (j
= ntokens
- 1; j
>= 0; j
--)
113 actions
= add_reduce(actions
, ruleno
, j
);
120 add_reduce(action
*actions
,
124 action
*temp
, *prev
, *next
;
127 for (next
= actions
; next
&& next
->symbol
< symbol
; next
= next
->next
)
130 while (next
&& next
->symbol
== symbol
&& next
->action_code
== SHIFT
)
136 while (next
&& next
->symbol
== symbol
&&
137 next
->action_code
== REDUCE
&& next
->number
< ruleno
)
145 temp
->symbol
= (Value_t
) symbol
;
146 temp
->number
= (Value_t
) ruleno
;
147 temp
->prec
= rprec
[ruleno
];
148 temp
->action_code
= REDUCE
;
149 temp
->assoc
= rassoc
[ruleno
];
160 find_final_state(void)
167 to_state2
= p
->shift
;
169 for (i
= p
->nshifts
- 1; i
>= 0; --i
)
171 final_state
= to_state2
[i
];
172 if (accessing_symbol
[final_state
] == goal
)
183 rules_used
= (Value_t
*) MALLOC((unsigned)nrules
* sizeof(Value_t
));
184 NO_SPACE(rules_used
);
186 for (i
= 0; i
< nrules
; ++i
)
189 for (i
= 0; i
< nstates
; ++i
)
191 for (p
= parser
[i
]; p
; p
= p
->next
)
193 if (p
->action_code
== REDUCE
&& p
->suppressed
== 0)
194 rules_used
[p
->number
] = 1;
199 for (i
= 3; i
< nrules
; ++i
)
206 fprintf(stderr
, "%s: 1 rule never reduced\n", myname
);
208 fprintf(stderr
, "%s: %d rules never reduced\n", myname
, nunused
);
213 remove_conflicts(void)
217 action
*p
, *pref
= 0;
221 SRconflicts
= NEW2(nstates
, Value_t
);
222 RRconflicts
= NEW2(nstates
, Value_t
);
223 for (i
= 0; i
< nstates
; i
++)
228 for (p
= parser
[i
]; p
; p
= p
->next
)
230 if (p
->symbol
!= symbol
)
235 else if (i
== final_state
&& symbol
== 0)
240 else if (pref
!= 0 && pref
->action_code
== SHIFT
)
242 if (pref
->prec
> 0 && p
->prec
> 0)
244 if (pref
->prec
< p
->prec
)
246 pref
->suppressed
= 2;
249 else if (pref
->prec
> p
->prec
)
253 else if (pref
->assoc
== LEFT
)
255 pref
->suppressed
= 2;
258 else if (pref
->assoc
== RIGHT
)
264 pref
->suppressed
= 2;
282 SRconflicts
[i
] = SRcount
;
283 RRconflicts
[i
] = RRcount
;
288 total_conflicts(void)
290 fprintf(stderr
, "%s: ", myname
);
292 fprintf(stderr
, "1 shift/reduce conflict");
293 else if (SRtotal
> 1)
294 fprintf(stderr
, "%d shift/reduce conflicts", SRtotal
);
296 if (SRtotal
&& RRtotal
)
297 fprintf(stderr
, ", ");
300 fprintf(stderr
, "1 reduce/reduce conflict");
301 else if (RRtotal
> 1)
302 fprintf(stderr
, "%d reduce/reduce conflicts", RRtotal
);
304 fprintf(stderr
, ".\n");
306 if (SRexpect
>= 0 && SRtotal
!= SRexpect
)
308 fprintf(stderr
, "%s: ", myname
);
309 fprintf(stderr
, "expected %d shift/reduce conflict%s.\n",
310 SRexpect
, PLURAL(SRexpect
));
311 exit_code
= EXIT_FAILURE
;
313 if (RRexpect
>= 0 && RRtotal
!= RRexpect
)
315 fprintf(stderr
, "%s: ", myname
);
316 fprintf(stderr
, "expected %d reduce/reduce conflict%s.\n",
317 RRexpect
, PLURAL(RRexpect
));
318 exit_code
= EXIT_FAILURE
;
323 sole_reduction(int stateno
)
330 for (p
= parser
[stateno
]; p
; p
= p
->next
)
332 if (p
->action_code
== SHIFT
&& p
->suppressed
== 0)
334 else if (p
->action_code
== REDUCE
&& p
->suppressed
== 0)
336 if (ruleno
> 0 && p
->number
!= ruleno
)
354 defred
= NEW2(nstates
, Value_t
);
355 for (i
= 0; i
< nstates
; i
++)
356 defred
[i
] = (Value_t
) sole_reduction(i
);
360 free_action_row(action
*p
)
377 for (i
= 0; i
< nstates
; i
++)
378 free_action_row(parser
[i
]);
389 DO_FREE(SRconflicts
);
390 DO_FREE(RRconflicts
);