1 /* $Id: closure.c,v 1.9 2010/06/09 08:21:47 tom Exp $ */
9 static unsigned *first_derives
;
22 rowsize
= WORDSIZE(nvars
);
23 EFF
= NEW2(nvars
* rowsize
, unsigned);
26 for (i
= start_symbol
; i
< nsyms
; i
++)
29 for (rule
= *sp
; rule
> 0; rule
= *++sp
)
31 symbol
= ritem
[rrhs
[rule
]];
34 symbol
-= start_symbol
;
41 reflexive_transitive_closure(EFF
, nvars
);
49 set_first_derives(void)
63 rulesetsize
= WORDSIZE(nrules
);
64 varsetsize
= WORDSIZE(nvars
);
65 first_derives
= NEW2(nvars
* rulesetsize
, unsigned) - ntokens
* rulesetsize
;
69 rrow
= first_derives
+ ntokens
* rulesetsize
;
70 for (i
= start_symbol
; i
< nsyms
; i
++)
72 vrow
= EFF
+ ((i
- ntokens
) * varsetsize
);
74 for (j
= start_symbol
; j
< nsyms
; k
++, j
++)
76 if (k
>= BITS_PER_WORD
)
82 if (cword
& (unsigned)(1 << k
))
85 while ((rule
= *rp
++) >= 0)
96 print_first_derives();
103 closure(short *nucleus
, int n
)
118 rulesetsize
= WORDSIZE(nrules
);
119 rsend
= ruleset
+ rulesetsize
;
120 for (rsp
= ruleset
; rsp
< rsend
; rsp
++)
124 for (csp
= nucleus
; csp
< csend
; ++csp
)
126 symbol
= ritem
[*csp
];
129 dsp
= first_derives
+ symbol
* rulesetsize
;
137 itemsetend
= itemset
;
139 for (rsp
= ruleset
; rsp
< rsend
; ++rsp
)
144 for (i
= 0; i
< BITS_PER_WORD
; ++i
)
146 if (word
& (unsigned)(1 << i
))
148 itemno
= rrhs
[ruleno
+ i
];
149 while (csp
< csend
&& *csp
< itemno
)
150 *itemsetend
++ = *csp
++;
151 *itemsetend
++ = itemno
;
152 while (csp
< csend
&& *csp
== itemno
)
157 ruleno
+= BITS_PER_WORD
;
161 *itemsetend
++ = *csp
++;
169 finalize_closure(void)
173 FREE(first_derives
+ ntokens
* WORDSIZE(nrules
));
183 printf("\n\nn = %d\n\n", n
);
184 for (isp
= itemset
; isp
< itemsetend
; isp
++)
185 printf(" %d\n", *isp
);
196 printf("\n\nEpsilon Free Firsts\n");
198 for (i
= start_symbol
; i
< nsyms
; i
++)
200 printf("\n%s", symbol_name
[i
]);
201 rowp
= EFF
+ ((i
- start_symbol
) * WORDSIZE(nvars
));
205 for (j
= 0; j
< nvars
; k
++, j
++)
207 if (k
>= BITS_PER_WORD
)
214 printf(" %s", symbol_name
[start_symbol
+ j
]);
220 print_first_derives(void)
228 printf("\n\n\nFirst Derives\n");
230 for (i
= start_symbol
; i
< nsyms
; i
++)
232 printf("\n%s derives\n", symbol_name
[i
]);
233 rp
= first_derives
+ i
* WORDSIZE(nrules
);
235 for (j
= 0; j
<= nrules
; k
++, j
++)
237 if (k
>= BITS_PER_WORD
)
243 if (cword
& (1 << k
))