2 /* Id: closure.c,v 1.7 2009/10/27 09:30:14 tom Exp */
7 __RCSID("$NetBSD: closure.c,v 1.8 2006/05/24 18:01:43 christos Exp $");
13 static unsigned *first_derives
;
26 rowsize
= WORDSIZE(nvars
);
27 EFF
= NEW2(nvars
* rowsize
, unsigned);
30 for (i
= start_symbol
; i
< nsyms
; i
++)
33 for (rule
= *sp
; rule
> 0; rule
= *++sp
)
35 symbol
= ritem
[rrhs
[rule
]];
38 symbol
-= start_symbol
;
45 reflexive_transitive_closure(EFF
, nvars
);
53 set_first_derives(void)
67 rulesetsize
= WORDSIZE(nrules
);
68 varsetsize
= WORDSIZE(nvars
);
69 first_derives
= NEW2(nvars
* rulesetsize
, unsigned) - ntokens
* rulesetsize
;
73 rrow
= first_derives
+ ntokens
* rulesetsize
;
74 for (i
= start_symbol
; i
< nsyms
; i
++)
76 vrow
= EFF
+ ((i
- ntokens
) * varsetsize
);
78 for (j
= start_symbol
; j
< nsyms
; k
++, j
++)
80 if (k
>= BITS_PER_WORD
)
89 while ((rule
= *rp
++) >= 0)
101 print_first_derives();
108 closure(short *nucleus
, int n
)
123 rulesetsize
= WORDSIZE(nrules
);
125 rsend
= ruleset
+ rulesetsize
;
126 for (rsp
= ruleset
; rsp
< rsend
; rsp
++)
130 for (csp
= nucleus
; csp
< csend
; ++csp
)
132 symbol
= ritem
[*csp
];
135 dsp
= first_derives
+ symbol
* rulesetsize
;
143 itemsetend
= itemset
;
145 for (rsp
= ruleset
; rsp
< rsend
; ++rsp
)
150 for (i
= 0; i
< BITS_PER_WORD
; ++i
)
154 itemno
= rrhs
[ruleno
+ i
];
155 while (csp
< csend
&& *csp
< itemno
)
156 *itemsetend
++ = *csp
++;
157 *itemsetend
++ = itemno
;
158 while (csp
< csend
&& *csp
== itemno
)
163 ruleno
+= BITS_PER_WORD
;
167 *itemsetend
++ = *csp
++;
175 finalize_closure(void)
179 FREE(first_derives
+ ntokens
* WORDSIZE(nrules
));
189 printf("\n\nn = %d\n\n", n
);
190 for (isp
= itemset
; isp
< itemsetend
; isp
++)
191 printf(" %d\n", *isp
);
202 printf("\n\nEpsilon Free Firsts\n");
204 for (i
= start_symbol
; i
< nsyms
; i
++)
206 printf("\n%s", symbol_name
[i
]);
207 rowp
= EFF
+ ((i
- start_symbol
) * WORDSIZE(nvars
));
211 for (j
= 0; j
< nvars
; k
++, j
++)
213 if (k
>= BITS_PER_WORD
)
220 printf(" %s", symbol_name
[start_symbol
+ j
]);
226 print_first_derives(void)
234 printf("\n\n\nFirst Derives\n");
236 for (i
= start_symbol
; i
< nsyms
; i
++)
238 printf("\n%s derives\n", symbol_name
[i
]);
239 rp
= first_derives
+ i
* WORDSIZE(nrules
);
241 for (j
= 0; j
<= nrules
; k
++, j
++)
243 if (k
>= BITS_PER_WORD
)
249 if (cword
& (1 << k
))