]>
git.saurik.com Git - bison.git/blob - src/closure.c
b20e98c0ac54620a62d2f0d55df1cf155d293584
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
31 /* NITEMSET is the size of the array ITEMSET. */
35 static bitset ruleset
;
37 /* internal data. See comments before set_fderives and set_firsts. */
38 static bitset
*fderives
= NULL
;
39 static bitset
*firsts
= NULL
;
41 /* Retrieve the FDERIVES/FIRSTS sets of the nonterminals numbered Var. */
42 #define FDERIVES(Var) fderives[(Var) - ntokens]
43 #define FIRSTS(Var) firsts[(Var) - ntokens]
51 print_closure (const char *title
, short *array
, size_t size
)
54 fprintf (stderr
, "Closure: %s\n", title
);
55 for (i
= 0; i
< size
; ++i
)
58 fprintf (stderr
, " %2d: .", array
[i
]);
59 for (rp
= &ritem
[array
[i
]]; *rp
>= 0; ++rp
)
60 fprintf (stderr
, " %s", symbols
[*rp
]->tag
);
61 fprintf (stderr
, " (rule %d)\n", -*rp
- 1);
63 fputs ("\n\n", stderr
);
72 fprintf (stderr
, "FIRSTS\n");
73 for (i
= ntokens
; i
< nsyms
; i
++)
75 fprintf (stderr
, "\t%s firsts\n", symbols
[i
]->tag
);
76 for (j
= 0; j
< nvars
; j
++)
77 if (bitset_test (FIRSTS (i
), j
))
78 fprintf (stderr
, "\t\t%d (%s)\n",
79 j
+ ntokens
, symbols
[j
+ ntokens
]->tag
);
81 fprintf (stderr
, "\n\n");
90 fprintf (stderr
, "FDERIVES\n");
91 for (i
= ntokens
; i
< nsyms
; i
++)
93 fprintf (stderr
, "\t%s derives\n", symbols
[i
]->tag
);
94 for (j
= 0; j
<= nrules
; j
++)
95 if (bitset_test (FDERIVES (i
), j
))
98 fprintf (stderr
, "\t\t%d:", j
- 1);
99 for (rhsp
= &ritem
[rules
[j
].rhs
]; *rhsp
>= 0; ++rhsp
)
100 fprintf (stderr
, " %s", symbols
[*rhsp
]->tag
);
101 fputc ('\n', stderr
);
104 fprintf (stderr
, "\n\n");
107 /*------------------------------------------------------------------.
108 | Set FIRSTS to be an NVARS array of NVARS bitsets indicating which |
109 | items can represent the beginning of the input corresponding to |
110 | which other items. |
112 | For example, if some rule expands symbol 5 into the sequence of |
113 | symbols 8 3 20, the symbol 8 can be the beginning of the data for |
114 | symbol 5, so the bit [8 - ntokens] in first[5 - ntokens] (= FIRST |
116 `------------------------------------------------------------------*/
123 firsts
= XCALLOC (bitset
, nvars
);
124 for (i
= ntokens
; i
< nsyms
; i
++)
126 FIRSTS (i
) = bitset_create (nvars
, BITSET_FIXED
);
127 for (j
= 0; derives
[i
][j
] >= 0; ++j
)
129 int symbol
= ritem
[rules
[derives
[i
][j
]].rhs
];
131 bitset_set (FIRSTS (i
), symbol
- ntokens
);
141 /*-------------------------------------------------------------------.
142 | Set FDERIVES to an NVARS by NRULES matrix of bits indicating which |
143 | rules can help derive the beginning of the data for each |
146 | For example, if symbol 5 can be derived as the sequence of symbols |
147 | 8 3 20, and one of the rules for deriving symbol 8 is rule 4, then |
148 | the [5 - NTOKENS, 4] bit in FDERIVES is set. |
149 `-------------------------------------------------------------------*/
156 fderives
= XCALLOC (bitset
, nvars
);
157 bitset_stats_init ();
158 for (i
= 0 ; i
< nvars
; ++i
)
159 fderives
[i
] = bitset_create (nrules
+ 1, BITSET_FIXED
);
163 for (i
= ntokens
; i
< nsyms
; ++i
)
164 for (j
= ntokens
; j
< nsyms
; ++j
)
165 if (bitset_test (FIRSTS (i
), j
- ntokens
))
166 for (k
= 0; derives
[j
][k
] > 0; ++k
)
167 bitset_set (FDERIVES (i
), derives
[j
][k
]);
172 for (i
= ntokens
; i
< nsyms
; ++i
)
173 bitset_free (FIRSTS (i
));
181 itemset
= XCALLOC (short, n
);
183 ruleset
= bitset_create (nrules
+ 1, BITSET_FIXED
);
191 closure (short *core
, int n
)
193 /* Index over CORE. */
196 /* An index over RULESET. */
199 /* A bit index over RULESET. */
203 print_closure ("input", core
, n
);
207 bitset_copy (ruleset
, FDERIVES (start_symbol
));
211 bitset_zero (ruleset
);
213 for (c
= 0; c
< n
; ++c
)
214 if (ISVAR (ritem
[core
[c
]]))
215 bitset_or (ruleset
, ruleset
, FDERIVES (ritem
[core
[c
]]));
220 for (ruleno
= 0; ruleno
< nrules
+ 1; ++ruleno
)
221 if (bitset_test (ruleset
, ruleno
))
223 int itemno
= rules
[ruleno
].rhs
;
224 while (c
< n
&& core
[c
] < itemno
)
226 itemset
[nitemset
] = core
[c
];
230 itemset
[nitemset
] = itemno
;
236 itemset
[nitemset
] = core
[c
];
242 print_closure ("output", itemset
, nitemset
);
252 bitset_free (ruleset
);
254 for (i
= 0 ; i
< nvars
; ++i
)
255 bitset_free (fderives
[i
]);