]>
git.saurik.com Git - bison.git/blob - src/closure.c
1 /* Subroutines for bison
2 Copyright (C) 1984, 1989, 2000, 2001, 2002 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
32 /* NITEMSET is the size of the array ITEMSET. */
36 static bitset ruleset
;
38 /* internal data. See comments before set_fderives and set_firsts. */
39 static bitsetv fderives
= NULL
;
40 static bitsetv firsts
= NULL
;
42 /* Retrieve the FDERIVES/FIRSTS sets of the nonterminals numbered Var. */
43 #define FDERIVES(Var) fderives[(Var) - ntokens]
44 #define FIRSTS(Var) firsts[(Var) - ntokens]
52 print_closure (const char *title
, short *array
, size_t size
)
55 fprintf (stderr
, "Closure: %s\n", title
);
56 for (i
= 0; i
< size
; ++i
)
59 fprintf (stderr
, " %2d: .", array
[i
]);
60 for (rp
= &ritem
[array
[i
]]; *rp
>= 0; ++rp
)
61 fprintf (stderr
, " %s",
62 quotearg_style (escape_quoting_style
, symbols
[*rp
]->tag
));
63 fprintf (stderr
, " (rule %d)\n", -*rp
- 1);
65 fputs ("\n\n", stderr
);
74 fprintf (stderr
, "FIRSTS\n");
75 for (i
= ntokens
; i
< nsyms
; i
++)
77 fprintf (stderr
, "\t%s firsts\n",
78 quotearg_style (escape_quoting_style
, symbols
[i
]->tag
));
79 for (j
= 0; j
< nvars
; j
++)
80 if (bitset_test (FIRSTS (i
), j
))
81 fprintf (stderr
, "\t\t%d (%s)\n",
83 quotearg_style (escape_quoting_style
,
84 symbols
[j
+ ntokens
]->tag
));
86 fprintf (stderr
, "\n\n");
95 fprintf (stderr
, "FDERIVES\n");
96 for (i
= ntokens
; i
< nsyms
; i
++)
98 fprintf (stderr
, "\t%s derives\n",
99 quotearg_style (escape_quoting_style
, symbols
[i
]->tag
));
100 for (j
= 0; j
< nrules
+ 1; j
++)
101 if (bitset_test (FDERIVES (i
), j
))
104 fprintf (stderr
, "\t\t%d:", j
- 1);
105 for (rhsp
= rules
[j
].rhs
; *rhsp
>= 0; ++rhsp
)
106 fprintf (stderr
, " %s",
107 quotearg_style (escape_quoting_style
,
108 symbols
[*rhsp
]->tag
));
109 fputc ('\n', stderr
);
112 fprintf (stderr
, "\n\n");
115 /*--------------------------------------------------------.
116 | Display the MATRIX array of SIZE bitsets of size SIZE. |
117 `--------------------------------------------------------*/
120 bitmatrix_print (const char *title
, bitsetv matrix
)
123 size_t size
= bitset_size (matrix
[0]);
126 fprintf (stderr
, "%s BEGIN\n", title
);
128 /* Column numbers. */
130 for (i
= 0; i
< size
; ++i
)
131 putc (i
/ 10 ? '0' + i
/ 10 : ' ', stderr
);
134 for (i
= 0; i
< size
; ++i
)
135 fprintf (stderr
, "%d", i
% 10);
139 fputs (" .", stderr
);
140 for (i
= 0; i
< size
; ++i
)
142 fputs (".\n", stderr
);
145 for (i
= 0; i
< size
; ++i
)
147 fprintf (stderr
, "%2d|", i
);
148 for (j
= 0; j
< size
; ++j
)
149 fputs (bitset_test (matrix
[i
], j
) ? "1" : " ", stderr
);
150 fputs ("|\n", stderr
);
154 fputs (" `", stderr
);
155 for (i
= 0; i
< size
; ++i
)
157 fputs ("'\n", stderr
);
160 fprintf (stderr
, "%s END\n\n", title
);
163 /*------------------------------------------------------------------.
164 | Set FIRSTS to be an NVARS array of NVARS bitsets indicating which |
165 | items can represent the beginning of the input corresponding to |
166 | which other items. |
168 | For example, if some rule expands symbol 5 into the sequence of |
169 | symbols 8 3 20, the symbol 8 can be the beginning of the data for |
170 | symbol 5, so the bit [8 - ntokens] in first[5 - ntokens] (= FIRST |
172 `------------------------------------------------------------------*/
179 firsts
= bitsetv_create (nvars
, nvars
, BITSET_FIXED
);
181 for (i
= ntokens
; i
< nsyms
; i
++)
182 for (j
= 0; derives
[i
][j
] >= 0; ++j
)
184 int symbol
= rules
[derives
[i
][j
]].rhs
[0];
186 bitset_set (FIRSTS (i
), symbol
- ntokens
);
190 bitmatrix_print ("RTC: Input", firsts
);
191 bitsetv_reflexive_transitive_closure (firsts
);
193 bitmatrix_print ("RTC: Output", firsts
);
199 /*-------------------------------------------------------------------.
200 | Set FDERIVES to an NVARS by NRULES matrix of bits indicating which |
201 | rules can help derive the beginning of the data for each |
204 | For example, if symbol 5 can be derived as the sequence of symbols |
205 | 8 3 20, and one of the rules for deriving symbol 8 is rule 4, then |
206 | the [5 - NTOKENS, 4] bit in FDERIVES is set. |
207 `-------------------------------------------------------------------*/
214 fderives
= bitsetv_create (nvars
, nrules
+ 1, BITSET_FIXED
);
218 for (i
= ntokens
; i
< nsyms
; ++i
)
219 for (j
= ntokens
; j
< nsyms
; ++j
)
220 if (bitset_test (FIRSTS (i
), j
- ntokens
))
221 for (k
= 0; derives
[j
][k
] > 0; ++k
)
222 bitset_set (FDERIVES (i
), derives
[j
][k
]);
227 bitsetv_free (firsts
);
234 itemset
= XCALLOC (short, n
);
236 ruleset
= bitset_create (nrules
+ 1, BITSET_FIXED
);
244 closure (short *core
, int n
)
246 /* Index over CORE. */
249 /* A bit index over RULESET. */
253 print_closure ("input", core
, n
);
255 bitset_zero (ruleset
);
257 for (c
= 0; c
< n
; ++c
)
258 if (ISVAR (ritem
[core
[c
]]))
259 bitset_or (ruleset
, ruleset
, FDERIVES (ritem
[core
[c
]]));
263 for (ruleno
= 0; ruleno
< nrules
+ 1; ++ruleno
)
264 if (bitset_test (ruleset
, ruleno
))
266 int itemno
= rules
[ruleno
].rhs
- ritem
;
267 while (c
< n
&& core
[c
] < itemno
)
269 itemset
[nritemset
] = core
[c
];
273 itemset
[nritemset
] = itemno
;
279 itemset
[nritemset
] = core
[c
];
285 print_closure ("output", itemset
, nritemset
);
293 bitset_free (ruleset
);
294 bitsetv_free (fderives
);