Berkeley YACC  1993-03-03
Berkeley's version of Yet Another Compiler Compiler
 All Data Structures Files Functions Variables Typedefs Macros Groups
closure.c File Reference
#include "defs.h"
+ Include dependency graph for closure.c:

Go to the source code of this file.

Functions

 set_EFF ()
 Populates EFF, the matrix of Epsilon-Free Firsts. More...
 
 set_first_derives ()
 Populates first_derives, the matrix of closure productions. More...
 
 closure (short *nucleus, int n)
 
 finalize_closure ()
 

Variables

short * itemset
 
short * itemsetend
 
unsigned * ruleset
 Bitset of closure items for the current state. More...
 
static unsigned * first_derives
 Matrix of closure productions. More...
 
static unsigned * EFF
 Matrix of Epsilon-Free Firsts. More...
 

Function Documentation

closure ( short *  nucleus,
int  n 
)

Definition at line 155 of file closure.c.

References BITS_PER_WORD, first_derives, ISVAR, itemset, itemsetend, nrules, ritem, rrhs, ruleset, and WORDSIZE.

Referenced by generate_states().

158 {
159  register int ruleno;
160  register unsigned word;
161  register unsigned i;
162  register short *csp;
163  register unsigned *dsp;
164  register unsigned *rsp;
165  register int rulesetsize;
166 
167  short *csend;
168  unsigned *rsend;
169  int symbol;
170  int itemno;
171 
172  rulesetsize = WORDSIZE(nrules);
173  rsp = ruleset;
174  rsend = ruleset + rulesetsize;
175  for (rsp = ruleset; rsp < rsend; rsp++)
176  *rsp = 0;
177 
178  csend = nucleus + n;
179  for (csp = nucleus; csp < csend; ++csp)
180  {
181  symbol = ritem[*csp];
182  if (ISVAR(symbol))
183  {
184  dsp = first_derives + symbol * rulesetsize;
185  rsp = ruleset;
186  while (rsp < rsend)
187  *rsp++ |= *dsp++;
188  }
189  }
190 
191  ruleno = 0;
193  csp = nucleus;
194  for (rsp = ruleset; rsp < rsend; ++rsp)
195  {
196  word = *rsp;
197  if (word)
198  {
199  for (i = 0; i < BITS_PER_WORD; ++i)
200  {
201  if (word & (1 << i))
202  {
203  itemno = rrhs[ruleno+i];
204  while (csp < csend && *csp < itemno)
205  *itemsetend++ = *csp++;
206  *itemsetend++ = itemno;
207  while (csp < csend && *csp == itemno)
208  ++csp;
209  }
210  }
211  }
212  ruleno += BITS_PER_WORD;
213  }
214 
215  while (csp < csend)
216  *itemsetend++ = *csp++;
217 
218 #ifdef DEBUG
219  print_closure(n);
220 #endif
221 }
unsigned * ruleset
Bitset of closure items for the current state.
Definition: closure.c:13
short * itemset
Definition: closure.c:3
short * itemsetend
Definition: closure.c:4
#define ISVAR(s)
Definition: defs.h:96
static unsigned * first_derives
Matrix of closure productions.
Definition: closure.c:27
#define BITS_PER_WORD
Definition: defs.h:25
#define WORDSIZE(n)
Definition: defs.h:26
int nrules
The number of rules in the grammar.
Definition: main.c:45
short * rrhs
List of right-hand sides of all rules.
Definition: main.c:126
short * ritem
Representation of all productions (and items)
Definition: main.c:112

+ Here is the caller graph for this function:

finalize_closure ( )

Definition at line 225 of file closure.c.

References first_derives, FREE, itemset, nrules, ntokens, ruleset, and WORDSIZE.

Referenced by generate_states().

226 {
227  FREE(itemset);
228  FREE(ruleset);
230 }
unsigned * ruleset
Bitset of closure items for the current state.
Definition: closure.c:13
short * itemset
Definition: closure.c:3
int ntokens
The number of tokens (terminals) in the grammar.
Definition: main.c:64
static unsigned * first_derives
Matrix of closure productions.
Definition: closure.c:27
#define WORDSIZE(n)
Definition: defs.h:26
int nrules
The number of rules in the grammar.
Definition: main.c:45
#define FREE(x)
Definition: defs.h:102

+ Here is the caller graph for this function:

set_EFF ( )

Populates EFF, the matrix of Epsilon-Free Firsts.

Let \( V \) be the set of all variables (non-terminals) and \( \mathcal{P} \) the set of all productions.

For each non-terminal \( V_i \), scans all of its productions. If they start with a non-terminal \( V_j \), sets \( \mathrm{EFF}_{i,j} \), thus computing the direct epsilon-free firsts of \( V_i \). It then calls a function that computes the reflexive and transitive closure of such relation.

Definition at line 52 of file closure.c.

References derives, EFF, ISVAR, NEW2, nsyms, nvars, reflexive_transitive_closure(), ritem, rrhs, SETBIT, start_symbol, and WORDSIZE.

Referenced by set_first_derives().

53 {
54  register unsigned *row;
55  register int symbol;
56  register short *sp;
57  register int rowsize;
58  register int i;
59  register int rule;
60 
61  rowsize = WORDSIZE(nvars);
62  EFF = NEW2(nvars * rowsize, unsigned);
63 
64  row = EFF;
65  for (i = start_symbol; i < nsyms; i++)
66  {
67  sp = derives[i];
68  for (rule = *sp; rule > 0; rule = *++sp)
69  {
70  symbol = ritem[rrhs[rule]];
71  if (ISVAR(symbol))
72  {
73  symbol -= start_symbol;
74  SETBIT(row, symbol);
75  }
76  }
77  row += rowsize;
78  }
79 
81 
82 #ifdef DEBUG
83  print_EFF();
84 #endif
85 }
#define NEW2(n, t)
Definition: defs.h:105
#define ISVAR(s)
Definition: defs.h:96
#define WORDSIZE(n)
Definition: defs.h:26
int nvars
The number of variables (non-terminals) in the grammar.
Definition: main.c:74
reflexive_transitive_closure(unsigned *R, int n)
Definition: warshall.c:79
int nsyms
The number of symbols (terminals + non-terminals) in the grammar.
Definition: main.c:55
short * rrhs
List of right-hand sides of all rules.
Definition: main.c:126
short * ritem
Representation of all productions (and items)
Definition: main.c:112
short ** derives
List of rules that derive each non-terminal.
Definition: main.c:140
#define SETBIT(r, n)
Definition: defs.h:28
int start_symbol
Index of the starting symbol of the grammar.
Definition: main.c:82
static unsigned * EFF
Matrix of Epsilon-Free Firsts.
Definition: closure.c:40

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

set_first_derives ( )

Populates first_derives, the matrix of closure productions.

Let \( V \) be the set of all variables (non-terminals) and \( \mathcal{P} \) the set of all productions.

Using the information saved in EFF, computes the closure productions of each non-terminal. That is, it sets \( \mathrm{first\_derives}_{i,j} \) if and only if the left-hand side of \( \mathcal{P}_j \) is a \( V_k \) such that \( \mathrm{EFF}_{i,k} \) is set.

In other words, it calculates the same information as set_EFF(), expanding the list of non-terminals to the list of their productions.

Definition at line 100 of file closure.c.

References BITS_PER_WORD, derives, EFF, first_derives, FREE, NEW2, nrules, nsyms, ntokens, nvars, set_EFF(), SETBIT, start_symbol, and WORDSIZE.

Referenced by generate_states().

101 {
102  register unsigned *rrow;
103  register unsigned *vrow;
104  register int j;
105  register unsigned k;
106  register unsigned cword;
107  register short *rp;
108 
109  int rule;
110  int i;
111  int rulesetsize;
112  int varsetsize;
113 
114  rulesetsize = WORDSIZE(nrules);
115  varsetsize = WORDSIZE(nvars);
116  first_derives = NEW2(nvars * rulesetsize, unsigned) - ntokens * rulesetsize;
117 
118  set_EFF();
119 
120  rrow = first_derives + ntokens * rulesetsize;
121  for (i = start_symbol; i < nsyms; i++)
122  {
123  vrow = EFF + ((i - ntokens) * varsetsize);
124  k = BITS_PER_WORD;
125  for (j = start_symbol; j < nsyms; k++, j++)
126  {
127  if (k >= BITS_PER_WORD)
128  {
129  cword = *vrow++;
130  k = 0;
131  }
132 
133  if (cword & (1 << k))
134  {
135  rp = derives[j];
136  while ((rule = *rp++) >= 0)
137  {
138  SETBIT(rrow, rule);
139  }
140  }
141  }
142 
143  vrow += varsetsize;
144  rrow += rulesetsize;
145  }
146 
147 #ifdef DEBUG
148  print_first_derives();
149 #endif
150 
151  FREE(EFF);
152 }
#define NEW2(n, t)
Definition: defs.h:105
int ntokens
The number of tokens (terminals) in the grammar.
Definition: main.c:64
static unsigned * first_derives
Matrix of closure productions.
Definition: closure.c:27
set_EFF()
Populates EFF, the matrix of Epsilon-Free Firsts.
Definition: closure.c:52
#define BITS_PER_WORD
Definition: defs.h:25
#define WORDSIZE(n)
Definition: defs.h:26
int nrules
The number of rules in the grammar.
Definition: main.c:45
int nvars
The number of variables (non-terminals) in the grammar.
Definition: main.c:74
int nsyms
The number of symbols (terminals + non-terminals) in the grammar.
Definition: main.c:55
#define FREE(x)
Definition: defs.h:102
short ** derives
List of rules that derive each non-terminal.
Definition: main.c:140
#define SETBIT(r, n)
Definition: defs.h:28
int start_symbol
Index of the starting symbol of the grammar.
Definition: main.c:82
static unsigned * EFF
Matrix of Epsilon-Free Firsts.
Definition: closure.c:40

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Variable Documentation

unsigned* EFF
static

Matrix of Epsilon-Free Firsts.

Let \( V \) be the set of all variables (non-terminals) and \( \mathcal{P} \) the set of all productions.

This is a matrix of \( \left| V \right| \times \left| V \right| \) bits, where entry \( \mathrm{EFF}_{i,j} \) is set if and only if the closure of an item of kind \( A \rightarrow \alpha . V_i \beta \) should include the productions of \( V_j \) (i.e. the closure items of the form \( V_j \rightarrow . \alpha \)).

It can be seen as a relation \( V \rightarrow 2^V \).

Definition at line 40 of file closure.c.

Referenced by set_EFF(), and set_first_derives().

unsigned* first_derives
static

Matrix of closure productions.

Let \( V \) be the set of all variables (non-terminals) and \( \mathcal{P} \) the set of all productions.

This is a matrix of \( \left| V \right| \times \left| \mathcal{P} \right| \) bits, where entry \( \mathrm{first\_derives}_{i,j} \) is set if and only if the closure of an item of kind \( A \rightarrow \alpha . V_i \beta \) should include the production \( \mathcal{P}_j \) (with the dot on the far left).

It can be seen as a relation \( V \rightarrow 2^{\mathcal{P}} \).

Definition at line 27 of file closure.c.

Referenced by closure(), finalize_closure(), and set_first_derives().

short* itemset

Definition at line 3 of file closure.c.

Referenced by closure(), finalize_closure(), generate_states(), new_itemsets(), and save_reductions().

short* itemsetend

Definition at line 4 of file closure.c.

Referenced by closure(), new_itemsets(), and save_reductions().

unsigned* ruleset

Bitset of closure items for the current state.

The n-th bit is set if and only if the n-th rule is part of the closure of the currently analyzed state. This variable is allocated in generate_states(), overwritten by each iteration of closure() and deleted in finalize_closure().

Definition at line 13 of file closure.c.

Referenced by closure(), finalize_closure(), and generate_states().