21 |
#ifndef GRAM_H_ |
#ifndef GRAM_H_ |
22 |
# define GRAM_H_ |
# define GRAM_H_ |
23 |
|
|
24 |
/* representation of the grammar rules: |
/* Representation of the grammar rules: |
25 |
|
|
26 |
ntokens is the number of tokens, and nvars is the number of |
NTOKENS is the number of tokens, and NVARS is the number of |
27 |
variables (nonterminals). nsyms is the total number, ntokens + |
variables (nonterminals). NSYMS is the total number, ntokens + |
28 |
nvars. |
nvars. |
29 |
|
|
|
(the true number of token values assigned is ntokens reduced by one |
|
|
for each alias declaration) |
|
|
|
|
30 |
Each symbol (either token or variable) receives a symbol number. |
Each symbol (either token or variable) receives a symbol number. |
31 |
Numbers 0 to ntokens-1 are for tokens, and ntokens to nsyms-1 are |
Numbers 0 to NTOKENS - 1 are for tokens, and NTOKENS to NSYMS - 1 |
32 |
for variables. Symbol number zero is the end-of-input token. This |
are for variables. Symbol number zero is the end-of-input token. |
33 |
token is counted in ntokens. |
This token is counted in ntokens. The true number of token values |
34 |
|
assigned is NTOKENS reduced by one for each alias declaration. |
35 |
|
|
36 |
|
The rules receive rule numbers 1 to NRULES in the order they are |
37 |
|
written. More precisely Bison augments the grammar with the |
38 |
|
initial rule, `$axiom: START-SYMBOL EOF', which is numbered 1, all |
39 |
|
the user rules are 2, 3 etc. Each time a rule number is presented |
40 |
|
to the user, we subtract 1, so *displayed* rule numbers are 0, 1, |
41 |
|
2... |
42 |
|
|
43 |
|
Internally, we cannot use the number 0 for a rule because for |
44 |
|
instance RITEM stores both symbol (the RHS) and rule numbers: the |
45 |
|
symbols are shorts >= 0, and rule number are stored negative. |
46 |
|
Therefore 0 cannot be used, since it would be both the rule number |
47 |
|
0, and the token EOF). |
48 |
|
|
49 |
The rules receive rule numbers 1 to nrules in the order they are |
Actions and guards are accessed via the rule number. |
|
written. Actions and guards are accessed via the rule number. |
|
50 |
|
|
51 |
The rules themselves are described by several arrays: amongst which |
The rules themselves are described by several arrays: amongst which |
52 |
RITEM, and RULES. |
RITEM, and RULES. |
53 |
|
|
54 |
RULES is an array of struct rule_s, which members are: |
RULES is an array of struct rule_s, which members are: |
55 |
|
|
56 |
RULES[R].lhs -- the symbol number of the left hand side of |
RULES[R].lhs -- the symbol number of the left hand side of rule R. |
57 |
rule R. If -1, the rule has been thrown out by reduce.c and should |
If -1, the rule has been thrown out by reduce.c and should be |
58 |
be ignored. |
ignored. |
59 |
|
|
60 |
RULES[R].rhs -- the index in RITEM of the beginning of the |
RULES[R].rhs -- the index in RITEM of the beginning of the portion |
61 |
portion for rule R. |
for rule R. |
62 |
|
|
63 |
RULES[R].prec -- the precedence level of R. |
RULES[R].prec -- the precedence level of R. |
64 |
|
|
65 |
RULES[R].precsym -- the symbol-number of the symbol in %prec |
RULES[R].precsym -- the symbol-number of the symbol in %prec for R |
66 |
for R (if any). |
(if any). |
67 |
|
|
68 |
RULES[R].assoc -- the associativity of R. |
RULES[R].assoc -- the associativity of R. |
69 |
|
|
82 |
The portions of RITEM come in order of increasing rule number and |
The portions of RITEM come in order of increasing rule number and |
83 |
are followed by an element which is zero to mark the end. nitems |
are followed by an element which is zero to mark the end. nitems |
84 |
is the total length of ritem, not counting the final zero. Each |
is the total length of ritem, not counting the final zero. Each |
85 |
element of ritem is called an "item" and its index in ritem is an |
element of RITEM is called an "item" and its index in RITEM is an |
86 |
item number. |
item number. |
87 |
|
|
88 |
Item numbers are used in the finite state machine to represent |
Item numbers are used in the finite state machine to represent |
89 |
places that parsing can get to. |
places that parsing can get to. |
90 |
|
|
91 |
SYMBOLS[I]->PREC records the precedence level of each symbol. |
SYMBOLS[I]->prec records the precedence level of each symbol. |
92 |
|
|
93 |
Precedence levels are assigned in increasing order starting with 1 |
Precedence levels are assigned in increasing order starting with 1 |
94 |
so that numerically higher precedence values mean tighter binding |
so that numerically higher precedence values mean tighter binding |
95 |
as they ought to. Zero as a symbol or rule's precedence means none |
as they ought to. Zero as a symbol or rule's precedence means none |
96 |
is assigned. |
is assigned. |
97 |
|
|
98 |
Associativities are recorded similarly in rassoc and sassoc. */ |
Associativities are recorded similarly in SYMBOLS[I]->assoc. */ |
99 |
|
|
100 |
|
|
101 |
#define ISTOKEN(s) ((s) < ntokens) |
#define ISTOKEN(s) ((s) < ntokens) |
112 |
|
|
113 |
extern int start_symbol; |
extern int start_symbol; |
114 |
|
|
115 |
/* associativity values in elements of rassoc, sassoc. */ |
/* Associativity values for tokens and rules. */ |
116 |
typedef enum |
typedef enum |
117 |
{ |
{ |
118 |
right_assoc, |
right_assoc, |
127 |
short rhs; |
short rhs; |
128 |
short prec; |
short prec; |
129 |
short precsym; |
short precsym; |
130 |
short assoc; |
associativity assoc; |
131 |
short line; |
short line; |
132 |
bool useful; |
bool useful; |
133 |
|
|