1 /* $NetBSD: lr0.c,v 1.8 2015/01/03 23:22:52 christos Exp $ */
3 /* Id: lr0.c,v 1.17 2014/11/28 15:46:42 tom Exp */
8 __RCSID("$NetBSD: lr0.c,v 1.8 2015/01/03 23:22:52 christos Exp $");
10 static core
*new_state(int symbol
);
11 static Value_t
get_state(int symbol
);
12 static void allocate_itemsets(void);
13 static void allocate_storage(void);
14 static void append_states(void);
15 static void free_storage(void);
16 static void generate_states(void);
17 static void initialize_states(void);
18 static void new_itemsets(void);
19 static void save_reductions(void);
20 static void save_shifts(void);
21 static void set_derives(void);
22 static void set_nullable(void);
27 reductions
*first_reduction
;
29 static core
**state_set
;
30 static core
*this_state
;
31 static core
*last_state
;
32 static shifts
*last_shift
;
33 static reductions
*last_reduction
;
36 static Value_t
*shift_symbol
;
38 static Value_t
*rules
;
40 static Value_t
*redset
;
41 static Value_t
*shiftset
;
43 static Value_t
**kernel_base
;
44 static Value_t
**kernel_end
;
45 static Value_t
*kernel_items
;
48 allocate_itemsets(void)
56 Value_t
*symbol_count
;
59 symbol_count
= NEW2(nsyms
, Value_t
);
61 item_end
= ritem
+ nitems
;
62 for (itemp
= ritem
; itemp
< item_end
; itemp
++)
68 symbol_count
[symbol
]++;
72 kernel_base
= NEW2(nsyms
, Value_t
*);
73 kernel_items
= NEW2(count
, Value_t
);
77 for (i
= 0; i
< nsyms
; i
++)
79 kernel_base
[i
] = kernel_items
+ count
;
80 count
+= symbol_count
[i
];
81 if (max
< symbol_count
[i
])
82 max
= symbol_count
[i
];
85 shift_symbol
= symbol_count
;
86 kernel_end
= NEW2(nsyms
, Value_t
*);
90 allocate_storage(void)
93 shiftset
= NEW2(nsyms
, Value_t
);
94 redset
= NEW2(nrules
+ 1, Value_t
);
95 state_set
= NEW2(nitems
, core
*);
106 fprintf(stderr
, "Entering append_states()\n");
108 for (i
= 1; i
< nshifts
; i
++)
110 symbol
= shift_symbol
[i
];
112 while (j
> 0 && shift_symbol
[j
- 1] > symbol
)
114 shift_symbol
[j
] = shift_symbol
[j
- 1];
117 shift_symbol
[j
] = symbol
;
120 for (i
= 0; i
< nshifts
; i
++)
122 symbol
= shift_symbol
[i
];
123 shiftset
[i
] = get_state(symbol
);
140 generate_states(void)
143 itemset
= NEW2(nitems
, Value_t
);
144 ruleset
= NEW2(WORDSIZE(nrules
), unsigned);
150 closure(this_state
->items
, this_state
->nitems
);
158 this_state
= this_state
->next
;
165 get_state(int symbol
)
176 fprintf(stderr
, "Entering get_state(%d)\n", symbol
);
179 isp1
= kernel_base
[symbol
];
180 iend
= kernel_end
[symbol
];
181 n
= (int)(iend
- isp1
);
184 assert(0 <= key
&& key
< nitems
);
194 isp1
= kernel_base
[symbol
];
197 while (found
&& isp1
< iend
)
199 if (*isp1
++ != *isp2
++)
212 sp
= sp
->link
= new_state(symbol
);
220 state_set
[key
] = sp
= new_state(symbol
);
227 initialize_states(void)
230 Value_t
*start_derives
;
233 start_derives
= derives
[start_symbol
];
234 for (i
= 0; start_derives
[i
] >= 0; ++i
)
237 p
= (core
*)MALLOC(sizeof(core
) + i
* sizeof(Value_t
));
243 p
->accessing_symbol
= 0;
244 p
->nitems
= (Value_t
) i
;
246 for (i
= 0; start_derives
[i
] >= 0; ++i
)
247 p
->items
[i
] = rrhs
[start_derives
[i
]];
249 first_state
= last_state
= this_state
= p
;
262 for (i
= 0; i
< nsyms
; i
++)
267 while (isp
< itemsetend
)
273 ksp
= kernel_end
[symbol
];
276 shift_symbol
[shiftcount
++] = symbol
;
277 ksp
= kernel_base
[symbol
];
280 *ksp
++ = (Value_t
) (i
+ 1);
281 kernel_end
[symbol
] = ksp
;
285 nshifts
= shiftcount
;
289 new_state(int symbol
)
298 fprintf(stderr
, "Entering new_state(%d)\n", symbol
);
301 if (nstates
>= MAXYYINT
)
302 fatal("too many states");
304 isp1
= kernel_base
[symbol
];
305 iend
= kernel_end
[symbol
];
306 n
= (unsigned)(iend
- isp1
);
308 p
= (core
*)allocate((sizeof(core
) + (n
- 1) * sizeof(Value_t
)));
309 p
->accessing_symbol
= (Value_t
) symbol
;
310 p
->number
= (Value_t
) nstates
;
311 p
->nitems
= (Value_t
) n
;
317 last_state
->next
= p
;
325 /* show_cores is used for debugging */
335 for (p
= first_state
; p
; ++k
, p
= p
->next
)
339 printf("state %d, number = %d, accessing symbol = %s\n",
340 k
, p
->number
, symbol_name
[p
->accessing_symbol
]);
342 for (i
= 0; i
< n
; ++i
)
344 itemno
= p
->items
[i
];
345 printf("%4d ", itemno
);
347 while (ritem
[j
] >= 0)
349 printf("%s :", symbol_name
[rlhs
[-ritem
[j
]]]);
352 printf(" %s", symbol_name
[ritem
[j
++]]);
354 while (ritem
[j
] >= 0)
355 printf(" %s", symbol_name
[ritem
[j
++]]);
362 /* show_ritems is used for debugging */
369 for (i
= 0; i
< nitems
; ++i
)
370 printf("ritem[%d] = %d\n", i
, ritem
[i
]);
373 /* show_rrhs is used for debugging */
379 for (i
= 0; i
< nrules
; ++i
)
380 printf("rrhs[%d] = %d\n", i
, rrhs
[i
]);
383 /* show_shifts is used for debugging */
392 for (p
= first_shift
; p
; ++k
, p
= p
->next
)
396 printf("shift %d, number = %d, nshifts = %d\n", k
, p
->number
,
399 for (i
= 0; i
< j
; ++i
)
400 printf("\t%d\n", p
->shift
[i
]);
413 p
= (shifts
*)allocate((sizeof(shifts
) +
414 (unsigned)(nshifts
- 1) * sizeof(Value_t
)));
416 p
->number
= this_state
->number
;
417 p
->nshifts
= (Value_t
) nshifts
;
421 send
= shiftset
+ nshifts
;
428 last_shift
->next
= p
;
439 save_reductions(void)
450 for (isp
= itemset
; isp
< itemsetend
; isp
++)
455 redset
[count
++] = (Value_t
) - item
;
461 p
= (reductions
*)allocate((sizeof(reductions
) +
462 (unsigned)(count
- 1) *
465 p
->number
= this_state
->number
;
477 last_reduction
->next
= p
;
494 derives
= NEW2(nsyms
, Value_t
*);
495 rules
= NEW2(nvars
+ nrules
, Value_t
);
498 for (lhs
= start_symbol
; lhs
< nsyms
; lhs
++)
500 derives
[lhs
] = rules
+ k
;
501 for (i
= 0; i
< nrules
; i
++)
525 printf("\nDERIVES\n\n");
527 for (i
= start_symbol
; i
< nsyms
; i
++)
529 printf("%s derives ", symbol_name
[i
]);
530 for (sp
= derives
[i
]; *sp
>= 0; sp
++)
548 nullable
= TMALLOC(char, nsyms
);
551 for (i
= 0; i
< nsyms
; ++i
)
558 for (i
= 1; i
< nitems
; i
++)
561 while ((j
= ritem
[i
]) >= 0)
580 for (i
= 0; i
< nsyms
; i
++)
583 printf("%s is nullable\n", symbol_name
[i
]);
585 printf("%s is not nullable\n", symbol_name
[i
]);
604 DO_FREE(derives
[start_symbol
]);