]>
git.saurik.com Git - bison.git/blob - src/closure.c
da9d4db3c71f5277d9b45675a2a6587d37701551
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", symbols
[*rp
]->tag
);
62 fprintf (stderr
, " (rule %d)\n", -*rp
- 1);
64 fputs ("\n\n", stderr
);
73 fprintf (stderr
, "FIRSTS\n");
74 for (i
= ntokens
; i
< nsyms
; i
++)
76 fprintf (stderr
, "\t%s firsts\n", symbols
[i
]->tag
);
77 for (j
= 0; j
< nvars
; j
++)
78 if (bitset_test (FIRSTS (i
), j
))
79 fprintf (stderr
, "\t\t%d (%s)\n",
80 j
+ ntokens
, symbols
[j
+ ntokens
]->tag
);
82 fprintf (stderr
, "\n\n");
91 fprintf (stderr
, "FDERIVES\n");
92 for (i
= ntokens
; i
< nsyms
; i
++)
94 fprintf (stderr
, "\t%s derives\n", symbols
[i
]->tag
);
95 for (j
= 0; j
<= nrules
; j
++)
96 if (bitset_test (FDERIVES (i
), j
))
99 fprintf (stderr
, "\t\t%d:", j
- 1);
100 for (rhsp
= &ritem
[rules
[j
].rhs
]; *rhsp
>= 0; ++rhsp
)
101 fprintf (stderr
, " %s", symbols
[*rhsp
]->tag
);
102 fputc ('\n', stderr
);
105 fprintf (stderr
, "\n\n");
108 /*------------------------------------------------------------------.
109 | Set FIRSTS to be an NVARS array of NVARS bitsets indicating which |
110 | items can represent the beginning of the input corresponding to |
111 | which other items. |
113 | For example, if some rule expands symbol 5 into the sequence of |
114 | symbols 8 3 20, the symbol 8 can be the beginning of the data for |
115 | symbol 5, so the bit [8 - ntokens] in first[5 - ntokens] (= FIRST |
117 `------------------------------------------------------------------*/
124 firsts
= bitsetv_create (nvars
, nvars
, BITSET_FIXED
);
126 for (i
= ntokens
; i
< nsyms
; i
++)
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
);
140 /*-------------------------------------------------------------------.
141 | Set FDERIVES to an NVARS by NRULES matrix of bits indicating which |
142 | rules can help derive the beginning of the data for each |
145 | For example, if symbol 5 can be derived as the sequence of symbols |
146 | 8 3 20, and one of the rules for deriving symbol 8 is rule 4, then |
147 | the [5 - NTOKENS, 4] bit in FDERIVES is set. |
148 `-------------------------------------------------------------------*/
155 fderives
= bitsetv_create (nvars
, nrules
+ 1, BITSET_FIXED
);
159 for (i
= ntokens
; i
< nsyms
; ++i
)
160 for (j
= ntokens
; j
< nsyms
; ++j
)
161 if (bitset_test (FIRSTS (i
), j
- ntokens
))
162 for (k
= 0; derives
[j
][k
] > 0; ++k
)
163 bitset_set (FDERIVES (i
), derives
[j
][k
]);
168 bitsetv_free (firsts
);
175 itemset
= XCALLOC (short, n
);
177 ruleset
= bitset_create (nrules
+ 1, BITSET_FIXED
);
185 closure (short *core
, int n
)
187 /* Index over CORE. */
190 /* An index over RULESET. */
193 /* A bit index over RULESET. */
197 print_closure ("input", core
, n
);
201 bitset_copy (ruleset
, FDERIVES (start_symbol
));
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
]]));
214 for (ruleno
= 0; ruleno
< nrules
+ 1; ++ruleno
)
215 if (bitset_test (ruleset
, ruleno
))
217 int itemno
= rules
[ruleno
].rhs
;
218 while (c
< n
&& core
[c
] < itemno
)
220 itemset
[nitemset
] = core
[c
];
224 itemset
[nitemset
] = itemno
;
230 itemset
[nitemset
] = core
[c
];
236 print_closure ("output", itemset
, nitemset
);
244 bitset_free (ruleset
);
245 bitsetv_free (fderives
);