5 extern short *itemsetend
;
6 extern unsigned *ruleset
;
11 reductions
*first_reduction
;
16 static core
**state_set
;
17 static core
*this_state
;
18 static core
*last_state
;
19 static shifts
*last_shift
;
20 static reductions
*last_reduction
;
23 static short *shift_symbol
;
26 static short *shiftset
;
28 static short **kernel_base
;
29 static short **kernel_end
;
30 static short *kernel_items
;
35 register short *itemp
;
36 register short *item_end
;
41 register short *symbol_count
;
44 symbol_count
= NEW2(nsyms
, short);
46 item_end
= ritem
+ nitems
;
47 for (itemp
= ritem
; itemp
< item_end
; itemp
++)
53 symbol_count
[symbol
]++;
57 kernel_base
= NEW2(nsyms
, short *);
58 kernel_items
= NEW2(count
, short);
62 for (i
= 0; i
< nsyms
; i
++)
64 kernel_base
[i
] = kernel_items
+ count
;
65 count
+= symbol_count
[i
];
66 if (max
< symbol_count
[i
])
67 max
= symbol_count
[i
];
70 shift_symbol
= symbol_count
;
71 kernel_end
= NEW2(nsyms
, short *);
78 shiftset
= NEW2(nsyms
, short);
79 redset
= NEW2(nrules
+ 1, short);
80 state_set
= NEW2(nitems
, core
*);
91 fprintf(stderr
, "Entering append_states()\n");
93 for (i
= 1; i
< nshifts
; i
++)
95 symbol
= shift_symbol
[i
];
97 while (j
> 0 && shift_symbol
[j
- 1] > symbol
)
99 shift_symbol
[j
] = shift_symbol
[j
- 1];
102 shift_symbol
[j
] = symbol
;
105 for (i
= 0; i
< nshifts
; i
++)
107 symbol
= shift_symbol
[i
];
108 shiftset
[i
] = get_state(symbol
);
129 itemset
= NEW2(nitems
, short);
130 ruleset
= NEW2(WORDSIZE(nrules
), unsigned);
136 closure(this_state
->items
, this_state
->nitems
);
144 this_state
= this_state
->next
;
158 register short *isp1
;
159 register short *isp2
;
160 register short *iend
;
166 fprintf(stderr
, "Entering get_state(%d)\n", symbol
);
169 isp1
= kernel_base
[symbol
];
170 iend
= kernel_end
[symbol
];
174 assert(0 <= key
&& key
< nitems
);
184 isp1
= kernel_base
[symbol
];
187 while (found
&& isp1
< iend
)
189 if (*isp1
++ != *isp2
++)
202 sp
= sp
->link
= new_state(symbol
);
210 state_set
[key
] = sp
= new_state(symbol
);
221 register short *start_derives
;
224 start_derives
= derives
[start_symbol
];
225 for (i
= 0; start_derives
[i
] >= 0; ++i
)
228 p
= (core
*) MALLOC(sizeof(core
) + i
*sizeof(short));
229 if (p
== 0) no_space();
234 p
->accessing_symbol
= 0;
237 for (i
= 0; start_derives
[i
] >= 0; ++i
)
238 p
->items
[i
] = rrhs
[start_derives
[i
]];
240 first_state
= last_state
= this_state
= p
;
248 register int shiftcount
;
253 for (i
= 0; i
< nsyms
; i
++)
258 while (isp
< itemsetend
)
264 ksp
= kernel_end
[symbol
];
267 shift_symbol
[shiftcount
++] = symbol
;
268 ksp
= kernel_base
[symbol
];
272 kernel_end
[symbol
] = ksp
;
276 nshifts
= shiftcount
;
287 register short *isp1
;
288 register short *isp2
;
289 register short *iend
;
292 fprintf(stderr
, "Entering new_state(%d)\n", symbol
);
295 if (nstates
>= MAXSHORT
)
296 fatal("too many states");
298 isp1
= kernel_base
[symbol
];
299 iend
= kernel_end
[symbol
];
302 p
= (core
*) allocate((unsigned) (sizeof(core
) + (n
- 1) * sizeof(short)));
303 p
->accessing_symbol
= symbol
;
311 last_state
->next
= p
;
320 /* show_cores is used for debugging */
329 for (p
= first_state
; p
; ++k
, p
= p
->next
)
332 printf("state %d, number = %d, accessing symbol = %s\n",
333 k
, p
->number
, symbol_name
[p
->accessing_symbol
]);
335 for (i
= 0; i
< n
; ++i
)
337 itemno
= p
->items
[i
];
338 printf("%4d ", itemno
);
340 while (ritem
[j
] >= 0) ++j
;
341 printf("%s :", symbol_name
[rlhs
[-ritem
[j
]]]);
344 printf(" %s", symbol_name
[ritem
[j
++]]);
346 while (ritem
[j
] >= 0)
347 printf(" %s", symbol_name
[ritem
[j
++]]);
355 /* show_ritems is used for debugging */
361 for (i
= 0; i
< nitems
; ++i
)
362 printf("ritem[%d] = %d\n", i
, ritem
[i
]);
366 /* show_rrhs is used for debugging */
371 for (i
= 0; i
< nrules
; ++i
)
372 printf("rrhs[%d] = %d\n", i
, rrhs
[i
]);
376 /* show_shifts is used for debugging */
384 for (p
= first_shift
; p
; ++k
, p
= p
->next
)
387 printf("shift %d, number = %d, nshifts = %d\n", k
, p
->number
,
390 for (i
= 0; i
< j
; ++i
)
391 printf("\t%d\n", p
->shift
[i
]);
401 register short *send
;
403 p
= (shifts
*) allocate((unsigned) (sizeof(shifts
) +
404 (nshifts
- 1) * sizeof(short)));
406 p
->number
= this_state
->number
;
407 p
->nshifts
= nshifts
;
411 send
= shiftset
+ nshifts
;
418 last_shift
->next
= p
;
437 register reductions
*p
;
438 register short *rend
;
441 for (isp
= itemset
; isp
< itemsetend
; isp
++)
446 redset
[count
++] = -item
;
452 p
= (reductions
*) allocate((unsigned) (sizeof(reductions
) +
453 (count
- 1) * sizeof(short)));
455 p
->number
= this_state
->number
;
467 last_reduction
->next
= p
;
483 register short *rules
;
485 derives
= NEW2(nsyms
, short *);
486 rules
= NEW2(nvars
+ nrules
, short);
489 for (lhs
= start_symbol
; lhs
< nsyms
; lhs
++)
491 derives
[lhs
] = rules
+ k
;
492 for (i
= 0; i
< nrules
; i
++)
511 FREE(derives
[start_symbol
]);
521 printf("\nDERIVES\n\n");
523 for (i
= start_symbol
; i
< nsyms
; i
++)
525 printf("%s derives ", symbol_name
[i
]);
526 for (sp
= derives
[i
]; *sp
>= 0; sp
++)
544 nullable
= MALLOC(nsyms
);
545 if (nullable
== 0) no_space();
547 for (i
= 0; i
< nsyms
; ++i
)
554 for (i
= 1; i
< nitems
; i
++)
557 while ((j
= ritem
[i
]) >= 0)
576 for (i
= 0; i
< nsyms
; i
++)
579 printf("%s is nullable\n", symbol_name
[i
]);
581 printf("%s is not nullable\n", symbol_name
[i
]);