]>
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
30 /* NITEMSET is the size of the array ITEMSET. */
34 static unsigned *ruleset
;
36 /* internal data. See comments before set_fderives and set_firsts. */
37 static unsigned *fderives
;
38 static unsigned *firsts
;
40 /* Retrieve the FDERIVES/FIRSTS sets of the nonterminals numbered Var. */
41 #define FDERIVES(Var) (fderives + ((Var) - ntokens) * rulesetsize)
42 #define FIRSTS(Var) (firsts + ((Var) - ntokens) * varsetsize)
44 /* number of words required to hold a bit for each rule */
45 static int rulesetsize
;
47 /* number of words required to hold a bit for each variable */
48 static int varsetsize
;
56 print_closure (const char *title
, short *array
, size_t size
)
59 fprintf (stderr
, "Closure: %s\n", title
);
60 for (i
= 0; i
< size
; ++i
)
63 fprintf (stderr
, " %2d: .", array
[i
]);
64 for (rp
= &ritem
[array
[i
]]; *rp
>= 0; ++rp
)
65 fprintf (stderr
, " %s", symbols
[*rp
]->tag
);
66 fprintf (stderr
, " (rule %d)\n", -*rp
- 1);
68 fputs ("\n\n", stderr
);
77 fprintf (stderr
, "FIRSTS\n");
78 for (i
= ntokens
; i
< nsyms
; i
++)
80 fprintf (stderr
, "\t%s firsts\n", symbols
[i
]->tag
);
81 for (j
= 0; j
< nvars
; j
++)
82 if (BITISSET (FIRSTS (i
), j
))
83 fprintf (stderr
, "\t\t%d (%s)\n",
84 j
+ ntokens
, symbols
[j
+ ntokens
]->tag
);
86 fprintf (stderr
, "\n\n");
96 fprintf (stderr
, "FDERIVES\n");
98 for (i
= ntokens
; i
< nsyms
; i
++)
100 fprintf (stderr
, "\t%s derives\n", symbols
[i
]->tag
);
101 for (j
= 0; j
<= nrules
; j
++)
102 if (BITISSET (FDERIVES (i
), j
))
105 fprintf (stderr
, "\t\t%d:", j
- 1);
106 for (rhsp
= &ritem
[rules
[j
].rhs
]; *rhsp
>= 0; ++rhsp
)
107 fprintf (stderr
, " %s", symbols
[*rhsp
]->tag
);
108 fputc ('\n', stderr
);
111 fprintf (stderr
, "\n\n");
114 /*-------------------------------------------------------------------.
115 | Set FIRSTS to be an NVARS by NVARS bit matrix indicating which |
116 | items can represent the beginning of the input corresponding to |
117 | which other items. |
119 | For example, if some rule expands symbol 5 into the sequence of |
120 | symbols 8 3 20, the symbol 8 can be the beginning of the data for |
121 | symbol 5, so the bit [8 - ntokens, 5 - ntokens] in firsts is set. |
122 `-------------------------------------------------------------------*/
129 varsetsize
= WORDSIZE (nvars
);
131 firsts
= XCALLOC (unsigned, nvars
* varsetsize
);
133 for (i
= ntokens
; i
< nsyms
; i
++)
134 for (j
= 0; derives
[i
][j
] >= 0; ++j
)
136 int symbol
= ritem
[rules
[derives
[i
][j
]].rhs
];
138 SETBIT (FIRSTS (i
), symbol
- ntokens
);
147 /*-------------------------------------------------------------------.
148 | Set FDERIVES to an NVARS by NRULES matrix of bits indicating which |
149 | rules can help derive the beginning of the data for each |
152 | For example, if symbol 5 can be derived as the sequence of symbols |
153 | 8 3 20, and one of the rules for deriving symbol 8 is rule 4, then |
154 | the [5 - NTOKENS, 4] bit in FDERIVES is set. |
155 `-------------------------------------------------------------------*/
162 fderives
= XCALLOC (unsigned, nvars
* rulesetsize
);
166 for (i
= ntokens
; i
< nsyms
; ++i
)
167 for (j
= ntokens
; j
< nsyms
; ++j
)
168 if (BITISSET (FIRSTS (i
), j
- ntokens
))
169 for (k
= 0; derives
[j
][k
] > 0; ++k
)
170 SETBIT (FDERIVES (i
), derives
[j
][k
]);
182 itemset
= XCALLOC (short, n
);
184 rulesetsize
= WORDSIZE (nrules
+ 1);
185 ruleset
= XCALLOC (unsigned, rulesetsize
);
193 closure (short *core
, int n
)
195 /* Index over CORE. */
198 /* Index over RULESET. */
201 /* A bit index over RULESET. */
205 print_closure ("input", core
, n
);
209 for (r
= 0; r
< rulesetsize
; ++r
)
210 ruleset
[r
] = FDERIVES (start_symbol
)[r
];
214 for (r
= 0; r
< rulesetsize
; ++r
)
217 for (c
= 0; c
< n
; ++c
)
218 if (ISVAR (ritem
[core
[c
]]))
219 for (r
= 0; r
< rulesetsize
; ++r
)
220 ruleset
[r
] |= FDERIVES (ritem
[core
[c
]])[r
];
225 for (ruleno
= 0; ruleno
< nrules
+ 1; ++ruleno
)
226 if (BITISSET (ruleset
, ruleno
))
228 int itemno
= rules
[ruleno
].rhs
;
229 while (c
< n
&& core
[c
] < itemno
)
231 itemset
[nitemset
] = core
[c
];
235 itemset
[nitemset
] = itemno
;
241 itemset
[nitemset
] = core
[c
];
247 print_closure ("output", itemset
, nitemset
);