]>
git.saurik.com Git - bison.git/blob - src/closure.c
1 /* Subroutines for bison
2 Copyright 1984, 1989, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
6 Bison is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 Bison is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
29 /* NITEMSET is the size of the array ITEMSET. */
33 static unsigned *ruleset
;
35 /* internal data. See comments before set_fderives and set_firsts. */
36 static unsigned *fderives
;
37 static unsigned *firsts
;
39 /* Retrieve the FDERIVES/FIRSTS sets of the nonterminals numbered Var. */
40 #define FDERIVES(Var) (fderives + ((Var) - ntokens) * rulesetsize)
41 #define FIRSTS(Var) (firsts + ((Var) - ntokens) * varsetsize)
43 /* number of words required to hold a bit for each rule */
44 static int rulesetsize
;
46 /* number of words required to hold a bit for each variable */
47 static int varsetsize
;
55 print_closure (const char *title
, short *array
, size_t size
)
58 fprintf (stderr
, "Closure: %s\n", title
);
59 for (i
= 0; i
< size
; ++i
)
62 fprintf (stderr
, " %2d: .", array
[i
]);
63 for (rp
= &ritem
[array
[i
]]; *rp
>= 0; ++rp
)
64 fprintf (stderr
, " %s", tags
[*rp
]);
65 fprintf (stderr
, " (rule %d)\n", -*rp
- 1);
67 fputs ("\n\n", stderr
);
76 fprintf (stderr
, "FIRSTS\n");
77 for (i
= ntokens
; i
< nsyms
; i
++)
79 fprintf (stderr
, "\t%s firsts\n", tags
[i
]);
80 for (j
= 0; j
< nvars
; j
++)
81 if (BITISSET (FIRSTS (i
), j
))
82 fprintf (stderr
, "\t\t%d (%s)\n", j
+ ntokens
, tags
[j
+ ntokens
]);
84 fprintf (stderr
, "\n\n");
94 fprintf (stderr
, "FDERIVES\n");
96 for (i
= ntokens
; i
< nsyms
; i
++)
98 fprintf (stderr
, "\t%s derives\n", tags
[i
]);
99 for (j
= 0; j
<= nrules
; j
++)
100 if (BITISSET (FDERIVES (i
), j
))
103 fprintf (stderr
, "\t\t%d:", j
- 1);
104 for (rhsp
= &ritem
[rule_table
[j
].rhs
]; *rhsp
>= 0; ++rhsp
)
105 fprintf (stderr
, " %s", tags
[*rhsp
]);
106 fputc ('\n', stderr
);
109 fprintf (stderr
, "\n\n");
112 /*-------------------------------------------------------------------.
113 | Set FIRSTS to be an NVARS by NVARS bit matrix indicating which |
114 | items can represent the beginning of the input corresponding to |
115 | which other items. |
117 | For example, if some rule expands symbol 5 into the sequence of |
118 | symbols 8 3 20, the symbol 8 can be the beginning of the data for |
119 | symbol 5, so the bit [8 - ntokens, 5 - ntokens] in firsts is set. |
120 `-------------------------------------------------------------------*/
127 varsetsize
= WORDSIZE (nvars
);
129 firsts
= XCALLOC (unsigned, nvars
* varsetsize
);
131 for (i
= ntokens
; i
< nsyms
; i
++)
132 for (j
= 0; derives
[i
][j
] >= 0; ++j
)
134 int symbol
= ritem
[rule_table
[derives
[i
][j
]].rhs
];
136 SETBIT (FIRSTS (i
), symbol
- ntokens
);
145 /*-------------------------------------------------------------------.
146 | Set FDERIVES to an NVARS by NRULES matrix of bits indicating which |
147 | rules can help derive the beginning of the data for each |
150 | For example, if symbol 5 can be derived as the sequence of symbols |
151 | 8 3 20, and one of the rules for deriving symbol 8 is rule 4, then |
152 | the [5 - NTOKENS, 4] bit in FDERIVES is set. |
153 `-------------------------------------------------------------------*/
160 fderives
= XCALLOC (unsigned, nvars
* rulesetsize
);
164 for (i
= ntokens
; i
< nsyms
; ++i
)
165 for (j
= ntokens
; j
< nsyms
; ++j
)
166 if (BITISSET (FIRSTS (i
), j
- ntokens
))
167 for (k
= 0; derives
[j
][k
] > 0; ++k
)
168 SETBIT (FDERIVES (i
), derives
[j
][k
]);
180 itemset
= XCALLOC (short, n
);
182 rulesetsize
= WORDSIZE (nrules
+ 1);
183 ruleset
= XCALLOC (unsigned, rulesetsize
);
191 closure (short *core
, int n
)
193 /* Index over CORE. */
196 /* Index over RULESET. */
199 /* A bit index over RULESET. */
203 print_closure ("input", core
, n
);
207 for (r
= 0; r
< rulesetsize
; ++r
)
208 ruleset
[r
] = FDERIVES (start_symbol
)[r
];
212 for (r
= 0; r
< rulesetsize
; ++r
)
215 for (c
= 0; c
< n
; ++c
)
216 if (ISVAR (ritem
[core
[c
]]))
217 for (r
= 0; r
< rulesetsize
; ++r
)
218 ruleset
[r
] |= FDERIVES (ritem
[core
[c
]])[r
];
223 for (ruleno
= 0; ruleno
< nrules
+ 1; ++ruleno
)
224 if (BITISSET (ruleset
, ruleno
))
226 int itemno
= rule_table
[ruleno
].rhs
;
227 while (c
< n
&& core
[c
] < itemno
)
229 itemset
[nitemset
] = core
[c
];
233 itemset
[nitemset
] = itemno
;
239 itemset
[nitemset
] = core
[c
];
245 print_closure ("output", itemset
, nitemset
);