1 /* $NetBSD: lr0.c,v 1.6 2011/09/10 21:29:04 christos Exp $ */
2 /* Id: lr0.c,v 1.12 2010/06/09 08:53:17 tom Exp */
7 __RCSID("$NetBSD: lr0.c,v 1.6 2011/09/10 21:29:04 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
];
178 n
= (int)(iend
- isp1
);
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));
240 p
->accessing_symbol
= 0;
241 p
->nitems
= (Value_t
) i
;
243 for (i
= 0; start_derives
[i
] >= 0; ++i
)
244 p
->items
[i
] = rrhs
[start_derives
[i
]];
246 first_state
= last_state
= this_state
= p
;
259 for (i
= 0; i
< nsyms
; i
++)
264 while (isp
< itemsetend
)
270 ksp
= kernel_end
[symbol
];
273 shift_symbol
[shiftcount
++] = symbol
;
274 ksp
= kernel_base
[symbol
];
277 *ksp
++ = (Value_t
) (i
+ 1);
278 kernel_end
[symbol
] = ksp
;
282 nshifts
= shiftcount
;
286 new_state(int symbol
)
295 fprintf(stderr
, "Entering new_state(%d)\n", symbol
);
298 if (nstates
>= MAXSHORT
)
299 fatal("too many states");
301 isp1
= kernel_base
[symbol
];
302 iend
= kernel_end
[symbol
];
303 n
= (unsigned)(iend
- isp1
);
305 p
= (core
*)allocate((sizeof(core
) + (n
- 1) * sizeof(short)));
306 p
->accessing_symbol
= (Value_t
) symbol
;
307 p
->number
= (Value_t
) nstates
;
308 p
->nitems
= (Value_t
) n
;
314 last_state
->next
= p
;
322 /* show_cores is used for debugging */
332 for (p
= first_state
; p
; ++k
, p
= p
->next
)
336 printf("state %d, number = %d, accessing symbol = %s\n",
337 k
, p
->number
, symbol_name
[p
->accessing_symbol
]);
339 for (i
= 0; i
< n
; ++i
)
341 itemno
= p
->items
[i
];
342 printf("%4d ", itemno
);
344 while (ritem
[j
] >= 0)
346 printf("%s :", symbol_name
[rlhs
[-ritem
[j
]]]);
349 printf(" %s", symbol_name
[ritem
[j
++]]);
351 while (ritem
[j
] >= 0)
352 printf(" %s", symbol_name
[ritem
[j
++]]);
359 /* show_ritems is used for debugging */
366 for (i
= 0; i
< nitems
; ++i
)
367 printf("ritem[%d] = %d\n", i
, ritem
[i
]);
370 /* show_rrhs is used for debugging */
376 for (i
= 0; i
< nrules
; ++i
)
377 printf("rrhs[%d] = %d\n", i
, rrhs
[i
]);
380 /* show_shifts is used for debugging */
389 for (p
= first_shift
; p
; ++k
, p
= p
->next
)
393 printf("shift %d, number = %d, nshifts = %d\n", k
, p
->number
,
396 for (i
= 0; i
< j
; ++i
)
397 printf("\t%d\n", p
->shift
[i
]);
409 p
= (shifts
*)allocate((sizeof(shifts
) +
410 (unsigned)(nshifts
- 1) * sizeof(short)));
412 p
->number
= this_state
->number
;
413 p
->nshifts
= (Value_t
) nshifts
;
417 send
= shiftset
+ nshifts
;
424 last_shift
->next
= p
;
435 save_reductions(void)
446 for (isp
= itemset
; isp
< itemsetend
; isp
++)
451 redset
[count
++] = (Value_t
) - item
;
457 p
= (reductions
*)allocate((sizeof(reductions
) +
458 (unsigned)(count
- 1) *
461 p
->number
= this_state
->number
;
473 last_reduction
->next
= p
;
491 derives
= NEW2(nsyms
, short *);
492 rules
= NEW2(nvars
+ nrules
, short);
495 for (lhs
= start_symbol
; lhs
< nsyms
; lhs
++)
497 derives
[lhs
] = rules
+ k
;
498 for (i
= 0; i
< nrules
; i
++)
522 printf("\nDERIVES\n\n");
524 for (i
= start_symbol
; i
< nsyms
; i
++)
526 printf("%s derives ", symbol_name
[i
]);
527 for (sp
= derives
[i
]; *sp
>= 0; sp
++)
545 nullable
= MALLOC(nsyms
);
548 for (i
= 0; i
< nsyms
; ++i
)
555 for (i
= 1; i
< nitems
; i
++)
558 while ((j
= ritem
[i
]) >= 0)
577 for (i
= 0; i
< nsyms
; i
++)
580 printf("%s is nullable\n", symbol_name
[i
]);
582 printf("%s is not nullable\n", symbol_name
[i
]);
599 DO_FREE(derives
[start_symbol
]);