]>
git.saurik.com Git - bison.git/blob - src/closure.c
33dd9b7a4b7e719ce09c189adb94137fdd2cedc0
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
25 #include "bitsetv-print.h"
33 /* NITEMSET is the size of the array ITEMSET. */
34 item_number_t
*itemset
;
37 static bitset ruleset
;
39 /* internal data. See comments before set_fderives and set_firsts. */
40 static bitsetv fderives
= NULL
;
41 static bitsetv firsts
= NULL
;
43 /* Retrieve the FDERIVES/FIRSTS sets of the nonterminals numbered Var. */
44 #define FDERIVES(Var) fderives[(Var) - ntokens]
45 #define FIRSTS(Var) firsts[(Var) - ntokens]
53 print_closure (const char *title
, item_number_t
*array
, size_t size
)
56 fprintf (stderr
, "Closure: %s\n", title
);
57 for (i
= 0; i
< size
; ++i
)
60 fprintf (stderr
, " %2d: .", array
[i
]);
61 for (rp
= &ritem
[array
[i
]]; *rp
>= 0; ++rp
)
62 fprintf (stderr
, " %s", 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
++)
78 fprintf (stderr
, "\t%s firsts\n", symbols
[i
]->tag
);
79 BITSET_FOR_EACH (iter
, FIRSTS (i
), j
, 0)
81 fprintf (stderr
, "\t\t%s\n",
82 symbols
[j
+ ntokens
]->tag
);
85 fprintf (stderr
, "\n\n");
95 fprintf (stderr
, "FDERIVES\n");
96 for (i
= ntokens
; i
< nsyms
; i
++)
99 fprintf (stderr
, "\t%s derives\n", symbols
[i
]->tag
);
100 BITSET_FOR_EACH (iter
, FDERIVES (i
), r
, 0)
102 fprintf (stderr
, "\t\t%3d ", r
);
103 rule_rhs_print (&rules
[r
], stderr
);
106 fprintf (stderr
, "\n\n");
109 /*------------------------------------------------------------------.
110 | Set FIRSTS to be an NVARS array of NVARS bitsets indicating which |
111 | items can represent the beginning of the input corresponding to |
112 | which other items. |
114 | For example, if some rule expands symbol 5 into the sequence of |
115 | symbols 8 3 20, the symbol 8 can be the beginning of the data for |
116 | symbol 5, so the bit [8 - ntokens] in first[5 - ntokens] (= FIRST |
118 `------------------------------------------------------------------*/
123 symbol_number_t i
, j
;
125 firsts
= bitsetv_create (nvars
, nvars
, BITSET_FIXED
);
127 for (i
= ntokens
; i
< nsyms
; i
++)
128 for (j
= 0; derives
[i
][j
] >= 0; ++j
)
130 int symbol
= rules
[derives
[i
][j
]].rhs
[0];
132 bitset_set (FIRSTS (i
), symbol
- ntokens
);
136 bitsetv_matrix_dump (stderr
, "RTC: Firsts Input", firsts
);
137 bitsetv_reflexive_transitive_closure (firsts
);
139 bitsetv_matrix_dump (stderr
, "RTC: Firsts Output", firsts
);
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 `-------------------------------------------------------------------*/
158 symbol_number_t i
, j
;
161 fderives
= bitsetv_create (nvars
, nrules
+ 1, BITSET_FIXED
);
165 for (i
= ntokens
; i
< nsyms
; ++i
)
166 for (j
= ntokens
; j
< nsyms
; ++j
)
167 if (bitset_test (FIRSTS (i
), j
- ntokens
))
168 for (k
= 0; derives
[j
][k
] > 0; ++k
)
169 bitset_set (FDERIVES (i
), derives
[j
][k
]);
174 bitsetv_free (firsts
);
182 itemset
= XCALLOC (item_number_t
, n
);
184 ruleset
= bitset_create (nrules
+ 1, BITSET_FIXED
);
192 closure (item_number_t
*core
, int n
)
194 /* Index over CORE. */
197 /* A bit index over RULESET. */
198 rule_number_t ruleno
;
200 bitset_iterator iter
;
203 print_closure ("input", core
, n
);
205 bitset_zero (ruleset
);
207 for (c
= 0; c
< n
; ++c
)
208 if (ISVAR (ritem
[core
[c
]]))
209 bitset_or (ruleset
, ruleset
, FDERIVES (ritem
[core
[c
]]));
213 BITSET_FOR_EACH (iter
, ruleset
, ruleno
, 0)
215 item_number_t itemno
= rules
[ruleno
].rhs
- ritem
;
216 while (c
< n
&& core
[c
] < itemno
)
218 itemset
[nritemset
] = core
[c
];
222 itemset
[nritemset
] = itemno
;
228 itemset
[nritemset
] = core
[c
];
234 print_closure ("output", itemset
, nritemset
);
242 bitset_free (ruleset
);
243 bitsetv_free (fderives
);