1 /* $Id: closure.c,v 1.11 2014/09/18 00:40:07 tom Exp $ */
9 static unsigned *first_base
;
10 static unsigned *first_derives
;
14 static void print_closure(int);
15 static void print_EFF(void);
16 static void print_first_derives(void);
29 rowsize
= WORDSIZE(nvars
);
30 EFF
= NEW2(nvars
* rowsize
, unsigned);
33 for (i
= start_symbol
; i
< nsyms
; i
++)
36 for (rule
= *sp
; rule
> 0; rule
= *++sp
)
38 symbol
= ritem
[rrhs
[rule
]];
41 symbol
-= start_symbol
;
48 reflexive_transitive_closure(EFF
, nvars
);
56 set_first_derives(void)
70 rulesetsize
= WORDSIZE(nrules
);
71 varsetsize
= WORDSIZE(nvars
);
72 first_base
= NEW2(nvars
* rulesetsize
, unsigned);
73 first_derives
= first_base
- ntokens
* rulesetsize
;
77 rrow
= first_derives
+ ntokens
* rulesetsize
;
78 for (i
= start_symbol
; i
< nsyms
; i
++)
80 vrow
= EFF
+ ((i
- ntokens
) * varsetsize
);
82 for (j
= start_symbol
; j
< nsyms
; k
++, j
++)
84 if (k
>= BITS_PER_WORD
)
90 if (cword
& (unsigned)(1 << k
))
93 while ((rule
= *rp
++) >= 0)
104 print_first_derives();
111 closure(Value_t
*nucleus
, int n
)
126 rulesetsize
= WORDSIZE(nrules
);
127 rsend
= ruleset
+ rulesetsize
;
128 for (rsp
= ruleset
; rsp
< rsend
; rsp
++)
132 for (csp
= nucleus
; csp
< csend
; ++csp
)
134 symbol
= ritem
[*csp
];
137 dsp
= first_derives
+ symbol
* rulesetsize
;
145 itemsetend
= itemset
;
147 for (rsp
= ruleset
; rsp
< rsend
; ++rsp
)
152 for (i
= 0; i
< BITS_PER_WORD
; ++i
)
154 if (word
& (unsigned)(1 << i
))
156 itemno
= rrhs
[ruleno
+ i
];
157 while (csp
< csend
&& *csp
< itemno
)
158 *itemsetend
++ = *csp
++;
159 *itemsetend
++ = itemno
;
160 while (csp
< csend
&& *csp
== itemno
)
165 ruleno
+= BITS_PER_WORD
;
169 *itemsetend
++ = *csp
++;
177 finalize_closure(void)
191 printf("\n\nn = %d\n\n", n
);
192 for (isp
= itemset
; isp
< itemsetend
; isp
++)
193 printf(" %d\n", *isp
);
204 printf("\n\nEpsilon Free Firsts\n");
206 for (i
= start_symbol
; i
< nsyms
; i
++)
208 printf("\n%s", symbol_name
[i
]);
209 rowp
= EFF
+ ((i
- start_symbol
) * WORDSIZE(nvars
));
213 for (j
= 0; j
< nvars
; k
++, j
++)
215 if (k
>= BITS_PER_WORD
)
222 printf(" %s", symbol_name
[start_symbol
+ j
]);
228 print_first_derives(void)
236 printf("\n\n\nFirst Derives\n");
238 for (i
= start_symbol
; i
< nsyms
; i
++)
240 printf("\n%s derives\n", symbol_name
[i
]);
241 rp
= first_derives
+ i
* WORDSIZE(nrules
);
243 for (j
= 0; j
<= nrules
; k
++, j
++)
245 if (k
>= BITS_PER_WORD
)
251 if (cword
& (1 << k
))