* src/closure.c (firsts): Now, also a bitset.

Adjust all dependencies.
(varsetsize): Remove, now unused.
* src/warshall.h, src/warshall.c: Now work on arrays of bitsets.
This commit is contained in:
Akim Demaille
2002-03-04 12:03:01 +00:00
parent 34ba97434f
commit d8a0245ce7
4 changed files with 60 additions and 55 deletions

View File

@@ -1,3 +1,10 @@
2002-03-04 Akim Demaille <akim@epita.fr>
* src/closure.c (firsts): Now, also a bitset.
Adjust all dependencies.
(varsetsize): Remove, now unused.
* src/warshall.h, src/warshall.c: Now work on arrays of bitsets.
2002-03-04 Akim Demaille <akim@epita.fr> 2002-03-04 Akim Demaille <akim@epita.fr>
* src/print.c: Convert to use bitset.h, not hand coded iterations * src/print.c: Convert to use bitset.h, not hand coded iterations

View File

@@ -25,8 +25,8 @@
#include "reader.h" #include "reader.h"
#include "closure.h" #include "closure.h"
#include "derives.h" #include "derives.h"
#include "warshall.h"
#include "bitset.h" #include "bitset.h"
#include "warshall.h"
/* NITEMSET is the size of the array ITEMSET. */ /* NITEMSET is the size of the array ITEMSET. */
short *itemset; short *itemset;
@@ -35,15 +35,12 @@ int nitemset;
static bitset ruleset; static bitset ruleset;
/* internal data. See comments before set_fderives and set_firsts. */ /* internal data. See comments before set_fderives and set_firsts. */
static bitset *fderives; static bitset *fderives = NULL;
static unsigned int *firsts; static bitset *firsts = NULL;
/* Retrieve the FDERIVES/FIRSTS sets of the nonterminals numbered Var. */ /* Retrieve the FDERIVES/FIRSTS sets of the nonterminals numbered Var. */
#define FDERIVES(Var) fderives[(Var) - ntokens] #define FDERIVES(Var) fderives[(Var) - ntokens]
#define FIRSTS(Var) (firsts + ((Var) - ntokens) * varsetsize) #define FIRSTS(Var) firsts[(Var) - ntokens]
/* number of words required to hold a bit for each variable */
static int varsetsize;
/*-----------------. /*-----------------.
@@ -77,7 +74,7 @@ print_firsts (void)
{ {
fprintf (stderr, "\t%s firsts\n", symbols[i]->tag); fprintf (stderr, "\t%s firsts\n", symbols[i]->tag);
for (j = 0; j < nvars; j++) for (j = 0; j < nvars; j++)
if (BITISSET (FIRSTS (i), j)) if (bitset_test (FIRSTS (i), j))
fprintf (stderr, "\t\t%d (%s)\n", fprintf (stderr, "\t\t%d (%s)\n",
j + ntokens, symbols[j + ntokens]->tag); j + ntokens, symbols[j + ntokens]->tag);
} }
@@ -88,11 +85,9 @@ print_firsts (void)
static void static void
print_fderives (void) print_fderives (void)
{ {
int i; int i, j;
int j;
fprintf (stderr, "FDERIVES\n"); fprintf (stderr, "FDERIVES\n");
for (i = ntokens; i < nsyms; i++) for (i = ntokens; i < nsyms; i++)
{ {
fprintf (stderr, "\t%s derives\n", symbols[i]->tag); fprintf (stderr, "\t%s derives\n", symbols[i]->tag);
@@ -109,32 +104,34 @@ print_fderives (void)
fprintf (stderr, "\n\n"); fprintf (stderr, "\n\n");
} }
/*-------------------------------------------------------------------. /*------------------------------------------------------------------.
| Set FIRSTS to be an NVARS by NVARS bit matrix indicating which | | Set FIRSTS to be an NVARS array of NVARS bitsets indicating which |
| items can represent the beginning of the input corresponding to | | items can represent the beginning of the input corresponding to |
| which other items. | | which other items. |
| | | |
| For example, if some rule expands symbol 5 into the sequence of | | For example, if some rule expands symbol 5 into the sequence of |
| symbols 8 3 20, the symbol 8 can be the beginning of the data for | | symbols 8 3 20, the symbol 8 can be the beginning of the data for |
| symbol 5, so the bit [8 - ntokens, 5 - ntokens] in firsts is set. | | symbol 5, so the bit [8 - ntokens] in first[5 - ntokens] (= FIRST |
`-------------------------------------------------------------------*/ | (5)) is set. |
`------------------------------------------------------------------*/
static void static void
set_firsts (void) set_firsts (void)
{ {
int i, j; int i, j;
varsetsize = WORDSIZE (nvars); firsts = XCALLOC (bitset, nvars);
firsts = XCALLOC (unsigned, nvars * varsetsize);
for (i = ntokens; i < nsyms; i++) for (i = ntokens; i < nsyms; i++)
for (j = 0; derives[i][j] >= 0; ++j) {
{ FIRSTS (i) = bitset_create (nvars, BITSET_FIXED);
int symbol = ritem[rules[derives[i][j]].rhs]; bitset_zero (FIRSTS (i));
if (ISVAR (symbol)) for (j = 0; derives[i][j] >= 0; ++j)
SETBIT (FIRSTS (i), symbol - ntokens); {
} int symbol = ritem[rules[derives[i][j]].rhs];
if (ISVAR (symbol))
bitset_set (FIRSTS (i), symbol - ntokens);
}
}
RTC (firsts, nvars); RTC (firsts, nvars);
@@ -169,13 +166,15 @@ set_fderives (void)
for (i = ntokens; i < nsyms; ++i) for (i = ntokens; i < nsyms; ++i)
for (j = ntokens; j < nsyms; ++j) for (j = ntokens; j < nsyms; ++j)
if (BITISSET (FIRSTS (i), j - ntokens)) if (bitset_test (FIRSTS (i), j - ntokens))
for (k = 0; derives[j][k] > 0; ++k) for (k = 0; derives[j][k] > 0; ++k)
bitset_set (FDERIVES (i), derives[j][k]); bitset_set (FDERIVES (i), derives[j][k]);
if (trace_flag) if (trace_flag)
print_fderives (); print_fderives ();
for (i = ntokens; i < nsyms; ++i)
bitset_free (FIRSTS (i));
XFREE (firsts); XFREE (firsts);
} }

View File

@@ -1,5 +1,5 @@
/* Generate transitive closure of a matrix, /* Generate transitive closure of a matrix,
Copyright 1984, 1989, 2000, 2001 Free Software Foundation, Inc. Copyright 1984, 1989, 2000, 2001, 2002 Free Software Foundation, Inc.
This file is part of Bison, the GNU Compiler Compiler. This file is part of Bison, the GNU Compiler Compiler.
@@ -21,19 +21,17 @@
#include "system.h" #include "system.h"
#include "getargs.h" #include "getargs.h"
#include "bitset.h"
#include "warshall.h" #include "warshall.h"
/*-------------------------------------------------------------. /*--------------------------------------------------------.
| Given n by n matrix of bits R, modify its contents to be the | | Display the MATRIX array of SIZE bitsets of size SIZE. |
| transive closure of what was given. | `--------------------------------------------------------*/
`-------------------------------------------------------------*/
static void static void
bitmatrix_print (const char *title, unsigned *matrix, size_t size) bitmatrix_print (const char *title, bitset *matrix, size_t size)
{ {
size_t i, j; size_t i, j;
size_t rowsize = WORDSIZE (size) * sizeof (unsigned);
#define ROW(Num) ((unsigned *) ((char *) matrix + ((Num) * rowsize)))
/* Title. */ /* Title. */
fprintf (stderr, "%s BEGIN\n", title); fprintf (stderr, "%s BEGIN\n", title);
@@ -59,7 +57,7 @@ bitmatrix_print (const char *title, unsigned *matrix, size_t size)
{ {
fprintf (stderr, "%2d|", i); fprintf (stderr, "%2d|", i);
for (j = 0; j < size; ++j) for (j = 0; j < size; ++j)
fputs (BITISSET (ROW (i), j) ? "1" : " ", stderr); fputs (bitset_test (matrix[i], j) ? "1" : " ", stderr);
fputs ("|\n", stderr); fputs ("|\n", stderr);
} }
@@ -73,44 +71,45 @@ bitmatrix_print (const char *title, unsigned *matrix, size_t size)
fprintf (stderr, "%s END\n\n", title); fprintf (stderr, "%s END\n\n", title);
} }
#define R(Num) (unsigned *) ((char *) R + ((Num) * rowsize)) /*-------------------------------------------------------------------.
| Given the MATRIX array of N bitsets of size N, modify its contents |
| to be the transive closure of what was given. |
`-------------------------------------------------------------------*/
static void static void
TC (unsigned *R, int n) TC (bitset *matrix, int n)
{ {
int rowsize = WORDSIZE (n) * sizeof (unsigned);
int i, j, k; int i, j, k;
if (trace_flag) if (trace_flag)
bitmatrix_print ("TC: Input", R, n); bitmatrix_print ("TC: Input", matrix, n);
/* R (J, I) && R (I, K) => R (J, K). /* R (J, I) && R (I, K) => R (J, K).
I *must* be the outter loop. */ I *must* be the outter loop. */
for (i = 0; i < n; ++i) for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j) for (j = 0; j < n; ++j)
if (BITISSET (R (j), i)) if (bitset_test (matrix[j], i))
for (k = 0; k < n; ++k) for (k = 0; k < n; ++k)
if (BITISSET (R (i), k)) if (bitset_test (matrix[i], k))
SETBIT (R (j), k); bitset_set (matrix[j], k);
if (trace_flag) if (trace_flag)
bitmatrix_print ("TC: Output", R, n); bitmatrix_print ("TC: Output", matrix, n);
} }
/*---------------------------------------------------------------. /*---------------------------------------------------------------.
| Reflexive Transitive Closure. Same as TC and then set all the | | Reflexive Transitive Closure. Same as TC and then set all the |
| bits on the diagonal of R. | | bits on the diagonal of MATRIX. |
`---------------------------------------------------------------*/ `---------------------------------------------------------------*/
void void
RTC (unsigned *R, int n) RTC (bitset *matrix, int n)
{ {
int rowsize = WORDSIZE (n) * sizeof (unsigned);
int i; int i;
TC (R, n); TC (matrix, n);
for (i = 0; i < n; ++i) for (i = 0; i < n; ++i)
SETBIT (R (i), i); bitset_set (matrix[i], i);
} }

View File

@@ -1,5 +1,5 @@
/* Generate transitive closure of a matrix, /* Generate transitive closure of a matrix,
Copyright 1984, 1989, 2000, 2001 Free Software Foundation, Inc. Copyright 1984, 1989, 2000, 2001, 2002 Free Software Foundation, Inc.
This file is part of Bison, the GNU Compiler Compiler. This file is part of Bison, the GNU Compiler Compiler.
@@ -21,5 +21,5 @@
#ifndef WARSHALL_H_ #ifndef WARSHALL_H_
# define WARSHALL_H_ # define WARSHALL_H_
void RTC PARAMS ((unsigned *, int)); void RTC PARAMS ((bitset *, int));
#endif /* !WARSHALL_H_ */ #endif /* !WARSHALL_H_ */