2 /* Id: lr0.c,v 1.9 2009/10/27 09:20:39 tom Exp */
7 __RCSID("$NetBSD: closure.c,v 1.8 2006/05/24 18:01:43 christos Exp $");
9 static core
*new_state(int symbol
);
10 static Value_t
get_state(int symbol
);
11 static void allocate_itemsets(void);
12 static void allocate_storage(void);
13 static void append_states(void);
14 static void free_storage(void);
15 static void generate_states(void);
16 static void initialize_states(void);
17 static void new_itemsets(void);
18 static void save_reductions(void);
19 static void save_shifts(void);
20 static void set_derives(void);
21 static void set_nullable(void);
26 reductions
*first_reduction
;
28 static core
**state_set
;
29 static core
*this_state
;
30 static core
*last_state
;
31 static shifts
*last_shift
;
32 static reductions
*last_reduction
;
35 static short *shift_symbol
;
37 static Value_t
*redset
;
38 static Value_t
*shiftset
;
40 static Value_t
**kernel_base
;
41 static Value_t
**kernel_end
;
42 static Value_t
*kernel_items
;
45 allocate_itemsets(void)
56 symbol_count
= NEW2(nsyms
, short);
58 item_end
= ritem
+ nitems
;
59 for (itemp
= ritem
; itemp
< item_end
; itemp
++)
65 symbol_count
[symbol
]++;
69 kernel_base
= NEW2(nsyms
, short *);
70 kernel_items
= NEW2(count
, short);
74 for (i
= 0; i
< nsyms
; i
++)
76 kernel_base
[i
] = kernel_items
+ count
;
77 count
+= symbol_count
[i
];
78 if (max
< symbol_count
[i
])
79 max
= symbol_count
[i
];
82 shift_symbol
= symbol_count
;
83 kernel_end
= NEW2(nsyms
, short *);
87 allocate_storage(void)
90 shiftset
= NEW2(nsyms
, short);
91 redset
= NEW2(nrules
+ 1, short);
92 state_set
= NEW2(nitems
, core
*);
103 fprintf(stderr
, "Entering append_states()\n");
105 for (i
= 1; i
< nshifts
; i
++)
107 symbol
= shift_symbol
[i
];
109 while (j
> 0 && shift_symbol
[j
- 1] > symbol
)
111 shift_symbol
[j
] = shift_symbol
[j
- 1];
114 shift_symbol
[j
] = symbol
;
117 for (i
= 0; i
< nshifts
; i
++)
119 symbol
= shift_symbol
[i
];
120 shiftset
[i
] = get_state(symbol
);
137 generate_states(void)
140 itemset
= NEW2(nitems
, short);
141 ruleset
= NEW2(WORDSIZE(nrules
), unsigned);
147 closure(this_state
->items
, this_state
->nitems
);
155 this_state
= this_state
->next
;
162 get_state(int symbol
)
173 fprintf(stderr
, "Entering get_state(%d)\n", symbol
);
176 isp1
= kernel_base
[symbol
];
177 iend
= kernel_end
[symbol
];
181 assert(0 <= key
&& key
< nitems
);
191 isp1
= kernel_base
[symbol
];
194 while (found
&& isp1
< iend
)
196 if (*isp1
++ != *isp2
++)
209 sp
= sp
->link
= new_state(symbol
);
217 state_set
[key
] = sp
= new_state(symbol
);
224 initialize_states(void)
227 short *start_derives
;
230 start_derives
= derives
[start_symbol
];
231 for (i
= 0; start_derives
[i
] >= 0; ++i
)
234 p
= (core
*)MALLOC(sizeof(core
) + i
* sizeof(short));
241 p
->accessing_symbol
= 0;
242 p
->nitems
= (Value_t
) i
;
244 for (i
= 0; start_derives
[i
] >= 0; ++i
)
245 p
->items
[i
] = rrhs
[start_derives
[i
]];
247 first_state
= last_state
= this_state
= p
;
260 for (i
= 0; i
< nsyms
; i
++)
265 while (isp
< itemsetend
)
271 ksp
= kernel_end
[symbol
];
274 shift_symbol
[shiftcount
++] = symbol
;
275 ksp
= kernel_base
[symbol
];
278 *ksp
++ = (Value_t
) (i
+ 1);
279 kernel_end
[symbol
] = ksp
;
283 nshifts
= shiftcount
;
287 new_state(int symbol
)
296 fprintf(stderr
, "Entering new_state(%d)\n", symbol
);
299 if (nstates
>= MAXSHORT
)
300 fatal("too many states");
302 isp1
= kernel_base
[symbol
];
303 iend
= kernel_end
[symbol
];
304 n
= (unsigned)(iend
- isp1
);
306 p
= (core
*)allocate((unsigned)(sizeof(core
) + (n
- 1) * sizeof(short)));
307 p
->accessing_symbol
= (Value_t
) symbol
;
308 p
->number
= (Value_t
) nstates
;
309 p
->nitems
= (Value_t
) n
;
315 last_state
->next
= p
;
323 /* show_cores is used for debugging */
333 for (p
= first_state
; p
; ++k
, p
= p
->next
)
337 printf("state %d, number = %d, accessing symbol = %s\n",
338 k
, p
->number
, symbol_name
[p
->accessing_symbol
]);
340 for (i
= 0; i
< n
; ++i
)
342 itemno
= p
->items
[i
];
343 printf("%4d ", itemno
);
345 while (ritem
[j
] >= 0)
347 printf("%s :", symbol_name
[rlhs
[-ritem
[j
]]]);
350 printf(" %s", symbol_name
[ritem
[j
++]]);
352 while (ritem
[j
] >= 0)
353 printf(" %s", symbol_name
[ritem
[j
++]]);
360 /* show_ritems is used for debugging */
367 for (i
= 0; i
< nitems
; ++i
)
368 printf("ritem[%d] = %d\n", i
, ritem
[i
]);
371 /* show_rrhs is used for debugging */
377 for (i
= 0; i
< nrules
; ++i
)
378 printf("rrhs[%d] = %d\n", i
, rrhs
[i
]);
381 /* show_shifts is used for debugging */
390 for (p
= first_shift
; p
; ++k
, p
= p
->next
)
394 printf("shift %d, number = %d, nshifts = %d\n", k
, p
->number
,
397 for (i
= 0; i
< j
; ++i
)
398 printf("\t%d\n", p
->shift
[i
]);
410 p
= (shifts
*)allocate((unsigned)(sizeof(shifts
) +
411 (unsigned)(nshifts
- 1) * sizeof(short)));
413 p
->number
= this_state
->number
;
414 p
->nshifts
= (Value_t
) nshifts
;
418 send
= shiftset
+ nshifts
;
425 last_shift
->next
= p
;
436 save_reductions(void)
447 for (isp
= itemset
; isp
< itemsetend
; isp
++)
452 redset
[count
++] = (Value_t
) - item
;
458 p
= (reductions
*)allocate((unsigned)(sizeof(reductions
) +
459 (unsigned)(count
- 1) *
462 p
->number
= this_state
->number
;
474 last_reduction
->next
= p
;
492 derives
= NEW2(nsyms
, short *);
493 rules
= NEW2(nvars
+ nrules
, short);
496 for (lhs
= start_symbol
; lhs
< nsyms
; lhs
++)
498 derives
[lhs
] = rules
+ k
;
499 for (i
= 0; i
< nrules
; i
++)
523 printf("\nDERIVES\n\n");
525 for (i
= start_symbol
; i
< nsyms
; i
++)
527 printf("%s derives ", symbol_name
[i
]);
528 for (sp
= derives
[i
]; *sp
>= 0; sp
++)
546 nullable
= MALLOC(nsyms
);
550 for (i
= 0; i
< nsyms
; ++i
)
557 for (i
= 1; i
< nitems
; i
++)
560 while ((j
= ritem
[i
]) >= 0)
579 for (i
= 0; i
< nsyms
; i
++)
582 printf("%s is nullable\n", symbol_name
[i
]);
584 printf("%s is not nullable\n", symbol_name
[i
]);
601 DO_FREE(derives
[start_symbol
]);