1 /* $NetBSD: closure.c,v 1.7 2013/04/06 14:52:24 christos Exp $ */
3 /* Id: closure.c,v 1.9 2010/06/09 08:21:47 tom Exp */
8 __RCSID("$NetBSD: closure.c,v 1.7 2013/04/06 14:52:24 christos Exp $");
14 static unsigned *first_derives
;
27 rowsize
= WORDSIZE(nvars
);
28 EFF
= NEW2(nvars
* rowsize
, unsigned);
31 for (i
= start_symbol
; i
< nsyms
; i
++)
34 for (rule
= *sp
; rule
> 0; rule
= *++sp
)
36 symbol
= ritem
[rrhs
[rule
]];
39 symbol
-= start_symbol
;
46 reflexive_transitive_closure(EFF
, nvars
);
54 set_first_derives(void)
68 rulesetsize
= WORDSIZE(nrules
);
69 varsetsize
= WORDSIZE(nvars
);
70 first_derives
= NEW2(nvars
* rulesetsize
, unsigned) - ntokens
* rulesetsize
;
74 rrow
= first_derives
+ ntokens
* rulesetsize
;
75 for (i
= start_symbol
; i
< nsyms
; i
++)
77 vrow
= EFF
+ ((i
- ntokens
) * varsetsize
);
79 for (j
= start_symbol
; j
< nsyms
; k
++, j
++)
81 if (k
>= BITS_PER_WORD
)
87 if (cword
& (unsigned)(1 << k
))
90 while ((rule
= *rp
++) >= 0)
101 print_first_derives();
108 closure(short *nucleus
, int n
)
123 rulesetsize
= WORDSIZE(nrules
);
124 rsend
= ruleset
+ rulesetsize
;
125 for (rsp
= ruleset
; rsp
< rsend
; rsp
++)
129 for (csp
= nucleus
; csp
< csend
; ++csp
)
131 symbol
= ritem
[*csp
];
134 dsp
= first_derives
+ symbol
* rulesetsize
;
142 itemsetend
= itemset
;
144 for (rsp
= ruleset
; rsp
< rsend
; ++rsp
)
149 for (i
= 0; i
< BITS_PER_WORD
; ++i
)
151 if (word
& (unsigned)(1 << i
))
153 itemno
= rrhs
[ruleno
+ i
];
154 while (csp
< csend
&& *csp
< itemno
)
155 *itemsetend
++ = *csp
++;
156 *itemsetend
++ = itemno
;
157 while (csp
< csend
&& *csp
== itemno
)
162 ruleno
+= BITS_PER_WORD
;
166 *itemsetend
++ = *csp
++;
174 finalize_closure(void)
178 FREE(first_derives
+ ntokens
* WORDSIZE(nrules
));
188 printf("\n\nn = %d\n\n", n
);
189 for (isp
= itemset
; isp
< itemsetend
; isp
++)
190 printf(" %d\n", *isp
);
201 printf("\n\nEpsilon Free Firsts\n");
203 for (i
= start_symbol
; i
< nsyms
; i
++)
205 printf("\n%s", symbol_name
[i
]);
206 rowp
= EFF
+ ((i
- start_symbol
) * WORDSIZE(nvars
));
210 for (j
= 0; j
< nvars
; k
++, j
++)
212 if (k
>= BITS_PER_WORD
)
219 printf(" %s", symbol_name
[start_symbol
+ j
]);
225 print_first_derives(void)
233 printf("\n\n\nFirst Derives\n");
235 for (i
= start_symbol
; i
< nsyms
; i
++)
237 printf("\n%s derives\n", symbol_name
[i
]);
238 rp
= first_derives
+ i
* WORDSIZE(nrules
);
240 for (j
= 0; j
<= nrules
; k
++, j
++)
242 if (k
>= BITS_PER_WORD
)
248 if (cword
& (1 << k
))