1991-11-23 10:41:43 +00:00
|
|
|
/* Copyright (c) 1991 by the Vrije Universiteit, Amsterdam, the Netherlands.
|
|
|
|
* All rights reserved.
|
1985-10-03 18:31:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* L L G E N
|
|
|
|
*
|
|
|
|
* An Extended LL(1) Parser Generator
|
|
|
|
*
|
|
|
|
* Author : Ceriel J.H. Jacobs
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sets.c
|
1985-11-25 15:47:51 +00:00
|
|
|
* Set manipulation and allocation routines.
|
1985-10-03 18:31:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
# include "types.h"
|
|
|
|
# include "extern.h"
|
|
|
|
# include "sets.h"
|
|
|
|
# include "assert.h"
|
|
|
|
|
|
|
|
# ifndef NORCSID
|
1994-06-24 11:31:16 +00:00
|
|
|
static string rcsid9 = "$Id$";
|
1985-10-03 18:31:47 +00:00
|
|
|
# endif
|
|
|
|
|
|
|
|
/* In this file the following routines are defined: */
|
|
|
|
extern setinit();
|
|
|
|
extern p_set setalloc();
|
1985-11-25 15:47:51 +00:00
|
|
|
extern p_set get_set();
|
1985-10-03 18:31:47 +00:00
|
|
|
extern int setunion();
|
|
|
|
extern int setintersect();
|
|
|
|
extern setminus();
|
|
|
|
extern int setempty();
|
|
|
|
extern int findindex();
|
|
|
|
extern int setcount();
|
|
|
|
|
1985-11-25 15:47:51 +00:00
|
|
|
int nbytes;
|
|
|
|
static int setsize;
|
|
|
|
int tsetsize;
|
|
|
|
p_set *setptr, *maxptr;
|
|
|
|
static t_info set_info;
|
|
|
|
p_mem alloc();
|
1985-10-03 18:31:47 +00:00
|
|
|
|
1991-11-01 10:37:26 +00:00
|
|
|
setinit(nt_needed) {
|
1985-10-03 18:31:47 +00:00
|
|
|
/*
|
|
|
|
* Initialises some variables needed for setcomputations
|
|
|
|
*/
|
|
|
|
register int bitset;
|
|
|
|
|
1985-11-25 15:47:51 +00:00
|
|
|
nbytes = NBYTES(ntokens);
|
|
|
|
bitset = ALIGN(nbytes);
|
|
|
|
tsetsize = NINTS(bitset);
|
1991-11-01 10:37:26 +00:00
|
|
|
if (nt_needed) {
|
1985-10-03 18:31:47 +00:00
|
|
|
/* nonterminals must be included in the sets */
|
|
|
|
bitset += NBYTES(nnonterms);
|
|
|
|
}
|
|
|
|
setsize = NINTS(bitset);
|
1985-11-25 15:47:51 +00:00
|
|
|
set_info.i_esize = sizeof(p_set);
|
|
|
|
set_info.i_incr = 20;
|
1985-10-03 18:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p_set
|
1985-11-25 15:47:51 +00:00
|
|
|
get_set() {
|
1985-10-03 18:31:47 +00:00
|
|
|
/*
|
1985-11-25 15:47:51 +00:00
|
|
|
* Allocate a set that cannot be freed
|
1985-10-03 18:31:47 +00:00
|
|
|
*/
|
1985-11-25 15:47:51 +00:00
|
|
|
register p_set p, q;
|
|
|
|
static p_set sets, maxsets;
|
|
|
|
|
|
|
|
if ((p = sets) >= maxsets) {
|
|
|
|
q = p = (p_set) alloc((unsigned) (50*setsize*sizeof(*sets)));
|
|
|
|
maxsets = p + 50 * setsize;
|
|
|
|
do {
|
|
|
|
*q++ = 0;
|
|
|
|
} while (q < maxsets);
|
1985-10-03 18:31:47 +00:00
|
|
|
}
|
1985-11-25 15:47:51 +00:00
|
|
|
sets = p + setsize;;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_set
|
|
|
|
setalloc() {
|
|
|
|
/*
|
|
|
|
* Allocate a set which can later be freed.
|
|
|
|
*/
|
|
|
|
register p_set p;
|
|
|
|
register int size = setsize;
|
|
|
|
|
|
|
|
p = (p_set) alloc((unsigned) (size * sizeof(*p))) + size;
|
|
|
|
do {
|
|
|
|
*--p = 0;
|
|
|
|
} while (--size);
|
|
|
|
return p;
|
1985-10-03 18:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1985-11-25 15:47:51 +00:00
|
|
|
setunion(a,b) register p_set a,b; {
|
1985-10-03 18:31:47 +00:00
|
|
|
/*
|
|
|
|
* a = a union b.
|
|
|
|
* Return 1 if the set a changed
|
|
|
|
*/
|
1985-11-25 15:47:51 +00:00
|
|
|
register int i;
|
|
|
|
register int j;
|
|
|
|
register int nsub = 0;
|
1985-10-03 18:31:47 +00:00
|
|
|
|
1985-11-25 15:47:51 +00:00
|
|
|
i = setsize;
|
|
|
|
do {
|
1985-10-03 18:31:47 +00:00
|
|
|
*a = (j = *a) | *b++;
|
|
|
|
if (*a++ != j) {
|
|
|
|
nsub = 1;
|
|
|
|
}
|
1985-11-25 15:47:51 +00:00
|
|
|
} while (--i);
|
1985-10-03 18:31:47 +00:00
|
|
|
return nsub;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1985-11-25 15:47:51 +00:00
|
|
|
setintersect(a,b) register p_set a,b; {
|
1985-10-03 18:31:47 +00:00
|
|
|
/*
|
|
|
|
* a = a intersect b.
|
1985-11-25 15:47:51 +00:00
|
|
|
* return 1 if the result is empty
|
1985-10-03 18:31:47 +00:00
|
|
|
*/
|
1985-11-25 15:47:51 +00:00
|
|
|
register int i;
|
|
|
|
register int nempty;
|
1985-10-03 18:31:47 +00:00
|
|
|
|
|
|
|
nempty = 1;
|
1985-11-25 15:47:51 +00:00
|
|
|
i = setsize;
|
|
|
|
do {
|
1985-10-03 18:31:47 +00:00
|
|
|
if (*a++ &= *b++) nempty = 0;
|
1985-11-25 15:47:51 +00:00
|
|
|
} while (--i);
|
1985-10-03 18:31:47 +00:00
|
|
|
return nempty;
|
|
|
|
}
|
|
|
|
|
1985-11-25 15:47:51 +00:00
|
|
|
setminus(a,b) register p_set a,b; {
|
1985-10-03 18:31:47 +00:00
|
|
|
/*
|
|
|
|
* a = a setminus b
|
|
|
|
*/
|
1985-11-25 15:47:51 +00:00
|
|
|
register int i;
|
1985-10-03 18:31:47 +00:00
|
|
|
|
1985-11-25 15:47:51 +00:00
|
|
|
i = setsize;
|
|
|
|
do {
|
1985-10-03 18:31:47 +00:00
|
|
|
*a++ &= ~(*b++);
|
1985-11-25 15:47:51 +00:00
|
|
|
} while (--i);
|
1985-10-03 18:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
setempty(p) register p_set p; {
|
|
|
|
/*
|
|
|
|
* Return 1 if the set p is empty
|
|
|
|
*/
|
1985-11-25 15:47:51 +00:00
|
|
|
register int i;
|
1985-10-03 18:31:47 +00:00
|
|
|
|
1985-11-25 15:47:51 +00:00
|
|
|
i = tsetsize;
|
|
|
|
do {
|
1985-10-03 18:31:47 +00:00
|
|
|
if (*p++) return 0;
|
1985-11-25 15:47:51 +00:00
|
|
|
} while (--i);
|
1985-10-03 18:31:47 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1985-11-25 15:47:51 +00:00
|
|
|
findindex(set) p_set set; {
|
1985-10-03 18:31:47 +00:00
|
|
|
/*
|
|
|
|
* The set "set" will serve as a recovery set.
|
1985-11-25 15:47:51 +00:00
|
|
|
* Search for it in the table. If not present, enter it.
|
|
|
|
* Here is room for improvement. At the moment, the list of
|
|
|
|
* sets is examined with linear search.
|
1985-10-03 18:31:47 +00:00
|
|
|
*/
|
|
|
|
register p_set *t;
|
1985-11-25 15:47:51 +00:00
|
|
|
p_mem new_mem();
|
1985-10-03 18:31:47 +00:00
|
|
|
register p_set a;
|
|
|
|
register p_set b;
|
1985-11-25 15:47:51 +00:00
|
|
|
register int i;
|
1985-10-03 18:31:47 +00:00
|
|
|
int saved;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First search for the set in the table
|
|
|
|
*/
|
|
|
|
for (t = setptr; t < maxptr; t++) {
|
|
|
|
a = *t;
|
1985-11-25 15:47:51 +00:00
|
|
|
b = set;
|
|
|
|
i = tsetsize;
|
|
|
|
do {
|
1985-10-03 18:31:47 +00:00
|
|
|
if (*a++ != *b++) break;
|
1985-11-25 15:47:51 +00:00
|
|
|
} while (--i);
|
1985-10-03 18:31:47 +00:00
|
|
|
if (i) continue;
|
|
|
|
/*
|
|
|
|
* Here, the sets are equal.
|
|
|
|
*/
|
|
|
|
return nbytes * (t - setptr);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Now check if the set consists of only one element.
|
|
|
|
* It would be a waste to use a set for that
|
|
|
|
*/
|
1985-11-25 15:47:51 +00:00
|
|
|
if (setcount(set, &saved) == 1) return -(saved + 1);
|
1985-10-03 18:31:47 +00:00
|
|
|
/*
|
|
|
|
* If it does, return its number as a negative number.
|
|
|
|
*/
|
1985-11-25 15:47:51 +00:00
|
|
|
maxptr = (p_set *) new_mem(&set_info);
|
|
|
|
setptr = (p_set *) set_info.i_ptr;
|
|
|
|
*maxptr = setalloc();
|
|
|
|
setunion(*maxptr, set);
|
1985-10-03 18:31:47 +00:00
|
|
|
return nbytes * (maxptr++ - setptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
setcount(set, saved) register p_set set; int *saved; {
|
|
|
|
register int i, j;
|
|
|
|
|
1985-11-25 15:47:51 +00:00
|
|
|
for (j = 0, i = 0; i < ntokens; i++) {
|
1985-10-03 18:31:47 +00:00
|
|
|
if (IN(set,i)) {
|
|
|
|
j++;
|
|
|
|
*saved = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return j;
|
|
|
|
}
|