]>
git.saurik.com Git - bison.git/blob - src/closure.c
3 Copyright (C) 1984, 1989, 2000, 2001, 2002 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
7 Bison is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 Bison is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to the Free
19 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 #include <bitsetv-print.h>
36 /* NITEMSET is the size of the array ITEMSET. */
40 static bitset ruleset
;
42 /* internal data. See comments before set_fderives and set_firsts. */
43 static bitsetv fderives
= NULL
;
44 static bitsetv firsts
= NULL
;
46 /* Retrieve the FDERIVES/FIRSTS sets of the nonterminals numbered Var. */
47 #define FDERIVES(Var) fderives[(Var) - ntokens]
48 #define FIRSTS(Var) firsts[(Var) - ntokens]
56 print_closure (char const *title
, item_number
*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
++)
81 fprintf (stderr
, "\t%s firsts\n", symbols
[i
]->tag
);
82 BITSET_FOR_EACH (iter
, FIRSTS (i
), j
, 0)
84 fprintf (stderr
, "\t\t%s\n",
85 symbols
[j
+ ntokens
]->tag
);
88 fprintf (stderr
, "\n\n");
98 fprintf (stderr
, "FDERIVES\n");
99 for (i
= ntokens
; i
< nsyms
; i
++)
101 bitset_iterator iter
;
102 fprintf (stderr
, "\t%s derives\n", symbols
[i
]->tag
);
103 BITSET_FOR_EACH (iter
, FDERIVES (i
), r
, 0)
105 fprintf (stderr
, "\t\t%3d ", r
);
106 rule_rhs_print (&rules
[r
], stderr
);
109 fprintf (stderr
, "\n\n");
112 /*------------------------------------------------------------------.
113 | Set FIRSTS to be an NVARS array of NVARS bitsets 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] in first[5 - ntokens] (= FIRST |
121 `------------------------------------------------------------------*/
128 firsts
= bitsetv_create (nvars
, nvars
, BITSET_FIXED
);
130 for (i
= ntokens
; i
< nsyms
; i
++)
131 for (j
= 0; derives
[i
- ntokens
][j
]; ++j
)
133 item_number sym
= derives
[i
- ntokens
][j
]->rhs
[0];
135 bitset_set (FIRSTS (i
), sym
- ntokens
);
138 if (trace_flag
& trace_sets
)
139 bitsetv_matrix_dump (stderr
, "RTC: Firsts Input", firsts
);
140 bitsetv_reflexive_transitive_closure (firsts
);
141 if (trace_flag
& trace_sets
)
142 bitsetv_matrix_dump (stderr
, "RTC: Firsts Output", firsts
);
144 if (trace_flag
& trace_sets
)
148 /*-------------------------------------------------------------------.
149 | Set FDERIVES to an NVARS by NRULES matrix of bits indicating which |
150 | rules can help derive the beginning of the data for each |
153 | For example, if symbol 5 can be derived as the sequence of symbols |
154 | 8 3 20, and one of the rules for deriving symbol 8 is rule 4, then |
155 | the [5 - NTOKENS, 4] bit in FDERIVES is set. |
156 `-------------------------------------------------------------------*/
164 fderives
= bitsetv_create (nvars
, nrules
, BITSET_FIXED
);
168 for (i
= ntokens
; i
< nsyms
; ++i
)
169 for (j
= ntokens
; j
< nsyms
; ++j
)
170 if (bitset_test (FIRSTS (i
), j
- ntokens
))
171 for (k
= 0; derives
[j
- ntokens
][k
]; ++k
)
172 bitset_set (FDERIVES (i
), derives
[j
- ntokens
][k
]->number
);
174 if (trace_flag
& trace_sets
)
177 bitsetv_free (firsts
);
187 ruleset
= bitset_create (nrules
, BITSET_FIXED
);
195 closure (item_number
*core
, int n
)
197 /* Index over CORE. */
200 /* A bit index over RULESET. */
203 bitset_iterator iter
;
205 if (trace_flag
& trace_sets
)
206 print_closure ("input", core
, n
);
208 bitset_zero (ruleset
);
210 for (c
= 0; c
< n
; ++c
)
211 if (ISVAR (ritem
[core
[c
]]))
212 bitset_or (ruleset
, ruleset
, FDERIVES (ritem
[core
[c
]]));
216 BITSET_FOR_EACH (iter
, ruleset
, ruleno
, 0)
218 item_number itemno
= rules
[ruleno
].rhs
- ritem
;
219 while (c
< n
&& core
[c
] < itemno
)
221 itemset
[nritemset
] = core
[c
];
225 itemset
[nritemset
] = itemno
;
231 itemset
[nritemset
] = core
[c
];
236 if (trace_flag
& trace_sets
)
237 print_closure ("output", itemset
, nritemset
);
245 bitset_free (ruleset
);
246 bitsetv_free (fderives
);