1 /* $NetBSD: closure.c,v 1.8 2015/01/03 23:22:52 christos Exp $ */
3 /* Id: closure.c,v 1.11 2014/09/18 00:40:07 tom Exp */
8 __RCSID("$NetBSD: closure.c,v 1.8 2015/01/03 23:22:52 christos Exp $");
14 static unsigned *first_base
;
15 static unsigned *first_derives
;
19 static void print_closure(int);
20 static void print_EFF(void);
21 static void print_first_derives(void);
34 rowsize
= WORDSIZE(nvars
);
35 EFF
= NEW2(nvars
* rowsize
, unsigned);
38 for (i
= start_symbol
; i
< nsyms
; i
++)
41 for (rule
= *sp
; rule
> 0; rule
= *++sp
)
43 symbol
= ritem
[rrhs
[rule
]];
46 symbol
-= start_symbol
;
53 reflexive_transitive_closure(EFF
, nvars
);
61 set_first_derives(void)
75 rulesetsize
= WORDSIZE(nrules
);
76 varsetsize
= WORDSIZE(nvars
);
77 first_base
= NEW2(nvars
* rulesetsize
, unsigned);
78 first_derives
= first_base
- ntokens
* rulesetsize
;
82 rrow
= first_derives
+ ntokens
* rulesetsize
;
83 for (i
= start_symbol
; i
< nsyms
; i
++)
85 vrow
= EFF
+ ((i
- ntokens
) * varsetsize
);
87 for (j
= start_symbol
; j
< nsyms
; k
++, j
++)
89 if (k
>= BITS_PER_WORD
)
95 if (cword
& (unsigned)(1 << k
))
98 while ((rule
= *rp
++) >= 0)
109 print_first_derives();
116 closure(Value_t
*nucleus
, int n
)
131 rulesetsize
= WORDSIZE(nrules
);
132 rsend
= ruleset
+ rulesetsize
;
133 for (rsp
= ruleset
; rsp
< rsend
; rsp
++)
137 for (csp
= nucleus
; csp
< csend
; ++csp
)
139 symbol
= ritem
[*csp
];
142 dsp
= first_derives
+ symbol
* rulesetsize
;
150 itemsetend
= itemset
;
152 for (rsp
= ruleset
; rsp
< rsend
; ++rsp
)
157 for (i
= 0; i
< BITS_PER_WORD
; ++i
)
159 if (word
& (unsigned)(1 << i
))
161 itemno
= rrhs
[ruleno
+ i
];
162 while (csp
< csend
&& *csp
< itemno
)
163 *itemsetend
++ = *csp
++;
164 *itemsetend
++ = itemno
;
165 while (csp
< csend
&& *csp
== itemno
)
170 ruleno
+= BITS_PER_WORD
;
174 *itemsetend
++ = *csp
++;
182 finalize_closure(void)
196 printf("\n\nn = %d\n\n", n
);
197 for (isp
= itemset
; isp
< itemsetend
; isp
++)
198 printf(" %d\n", *isp
);
209 printf("\n\nEpsilon Free Firsts\n");
211 for (i
= start_symbol
; i
< nsyms
; i
++)
213 printf("\n%s", symbol_name
[i
]);
214 rowp
= EFF
+ ((i
- start_symbol
) * WORDSIZE(nvars
));
218 for (j
= 0; j
< nvars
; k
++, j
++)
220 if (k
>= BITS_PER_WORD
)
227 printf(" %s", symbol_name
[start_symbol
+ j
]);
233 print_first_derives(void)
241 printf("\n\n\nFirst Derives\n");
243 for (i
= start_symbol
; i
< nsyms
; i
++)
245 printf("\n%s derives\n", symbol_name
[i
]);
246 rp
= first_derives
+ i
* WORDSIZE(nrules
);
248 for (j
= 0; j
<= nrules
; k
++, j
++)
250 if (k
>= BITS_PER_WORD
)
256 if (cword
& (1 << k
))