]>
Commit | Line | Data |
---|---|---|
f7d4d87a | 1 | /* Data definitions for internal representation of bison's input, |
d7e1f00c | 2 | Copyright (C) 1984, 1986, 1989, 1992, 2001, 2002 |
99013900 | 3 | Free Software Foundation, Inc. |
f7d4d87a | 4 | |
b2ca4022 | 5 | This file is part of Bison, the GNU Compiler Compiler. |
f7d4d87a | 6 | |
b2ca4022 AD |
7 | Bison is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
f7d4d87a | 11 | |
b2ca4022 AD |
12 | Bison is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
f7d4d87a | 16 | |
b2ca4022 AD |
17 | You should have received a copy of the GNU General Public License |
18 | along with Bison; see the file COPYING. If not, write to | |
19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
f7d4d87a | 21 | |
b2ca4022 AD |
22 | #ifndef GRAM_H_ |
23 | # define GRAM_H_ | |
f7d4d87a | 24 | |
aea13e97 | 25 | /* Representation of the grammar rules: |
f7d4d87a | 26 | |
aea13e97 AD |
27 | NTOKENS is the number of tokens, and NVARS is the number of |
28 | variables (nonterminals). NSYMS is the total number, ntokens + | |
b2ca4022 | 29 | nvars. |
f7d4d87a | 30 | |
b2ca4022 | 31 | Each symbol (either token or variable) receives a symbol number. |
aea13e97 AD |
32 | Numbers 0 to NTOKENS - 1 are for tokens, and NTOKENS to NSYMS - 1 |
33 | are for variables. Symbol number zero is the end-of-input token. | |
34 | This token is counted in ntokens. The true number of token values | |
35 | assigned is NTOKENS reduced by one for each alias declaration. | |
36 | ||
37 | The rules receive rule numbers 1 to NRULES in the order they are | |
38 | written. More precisely Bison augments the grammar with the | |
39 | initial rule, `$axiom: START-SYMBOL EOF', which is numbered 1, all | |
40 | the user rules are 2, 3 etc. Each time a rule number is presented | |
41 | to the user, we subtract 1, so *displayed* rule numbers are 0, 1, | |
42 | 2... | |
43 | ||
44 | Internally, we cannot use the number 0 for a rule because for | |
45 | instance RITEM stores both symbol (the RHS) and rule numbers: the | |
46 | symbols are shorts >= 0, and rule number are stored negative. | |
47 | Therefore 0 cannot be used, since it would be both the rule number | |
48 | 0, and the token EOF). | |
49 | ||
50 | Actions and guards are accessed via the rule number. | |
f7d4d87a | 51 | |
b2ed6e58 | 52 | The rules themselves are described by several arrays: amongst which |
1a2b5d37 | 53 | RITEM, and RULES. |
b2ed6e58 | 54 | |
1a2b5d37 | 55 | RULES is an array of struct rule_s, which members are: |
b2ed6e58 | 56 | |
03b31c0c | 57 | RULES[R].lhs -- the symbol of the left hand side of rule R. |
b2ed6e58 | 58 | |
aea13e97 AD |
59 | RULES[R].rhs -- the index in RITEM of the beginning of the portion |
60 | for rule R. | |
f7d4d87a | 61 | |
03b31c0c | 62 | RULES[R].prec -- the symbol providing the precedence level of R. |
652a871c | 63 | |
03b31c0c AD |
64 | RULES[R].precsym -- the symbol attached (via %prec) to give its |
65 | precedence to R. Of course, if set, it is equal to `prec', but we | |
66 | need to distinguish one from the other when reducing: a symbol used | |
67 | in a %prec is not useless. | |
652a871c | 68 | |
1a2b5d37 | 69 | RULES[R].assoc -- the associativity of R. |
e41dc700 | 70 | |
1a2b5d37 | 71 | RULES[R].line -- the line where R was defined. |
652a871c | 72 | |
03b31c0c AD |
73 | RULES[R].useful -- TRUE iff the rule is used (i.e., FALSE if thrown |
74 | away by reduce). | |
68f1e3ed | 75 | |
b2ed6e58 AD |
76 | The right hand side is stored as symbol numbers in a portion of |
77 | RITEM. | |
f7d4d87a | 78 | |
b2ca4022 AD |
79 | The length of the portion is one greater than the number of symbols |
80 | in the rule's right hand side. The last element in the portion | |
81 | contains minus R, which identifies it as the end of a portion and | |
82 | says which rule it is for. | |
f7d4d87a | 83 | |
b2ed6e58 | 84 | The portions of RITEM come in order of increasing rule number and |
5123689b | 85 | are followed by an element which is zero to mark the end. nritems |
b2ca4022 | 86 | is the total length of ritem, not counting the final zero. Each |
aea13e97 | 87 | element of RITEM is called an "item" and its index in RITEM is an |
b2ca4022 | 88 | item number. |
f7d4d87a | 89 | |
b2ca4022 AD |
90 | Item numbers are used in the finite state machine to represent |
91 | places that parsing can get to. | |
f7d4d87a | 92 | |
aea13e97 | 93 | SYMBOLS[I]->prec records the precedence level of each symbol. |
f7d4d87a | 94 | |
b2ca4022 AD |
95 | Precedence levels are assigned in increasing order starting with 1 |
96 | so that numerically higher precedence values mean tighter binding | |
97 | as they ought to. Zero as a symbol or rule's precedence means none | |
98 | is assigned. | |
f7d4d87a | 99 | |
aea13e97 | 100 | Associativities are recorded similarly in SYMBOLS[I]->assoc. */ |
f7d4d87a | 101 | |
bba97eb2 | 102 | #include "symtab.h" |
f7d4d87a DM |
103 | |
104 | #define ISTOKEN(s) ((s) < ntokens) | |
105 | #define ISVAR(s) ((s) >= ntokens) | |
106 | ||
f7d4d87a DM |
107 | extern int nrules; |
108 | extern int nsyms; | |
109 | extern int ntokens; | |
110 | extern int nvars; | |
111 | ||
62a3e4f0 AD |
112 | #define ITEM_NUMBER_MAX INT_MAX |
113 | typedef int item_number_t; | |
114 | extern item_number_t *ritem; | |
75142d45 | 115 | extern int nritems; |
b2ed6e58 | 116 | |
5fbb0954 AD |
117 | /* There is weird relationship between item_number_t and |
118 | token_number_t: we store token_number_t in item_number_t, but in | |
119 | the latter we also store, as negative numbers, the rule numbers. | |
120 | ||
121 | Therefore, an token_number_t must be a valid item_number_t, and we | |
122 | sometimes have to perform the converse transformation. */ | |
123 | #define token_number_as_item_number(Tok) ((item_number_t) (Tok)) | |
124 | #define item_number_as_token_number(Ite) ((token_number_t) (Ite)) | |
125 | ||
126 | extern token_number_t start_symbol; | |
f7d4d87a | 127 | |
62a3e4f0 | 128 | |
652a871c AD |
129 | typedef struct rule_s |
130 | { | |
c3b407f4 AD |
131 | /* The number of the rule in the source. It is usually the index in |
132 | RULES too, except if there are useless rules. */ | |
d7e1f00c AD |
133 | short user_number; |
134 | ||
135 | /* The index in RULES. Usually the rule number in the source, | |
136 | except if some rules are useless. */ | |
c3b407f4 AD |
137 | short number; |
138 | ||
db8837cb | 139 | symbol_t *lhs; |
62a3e4f0 | 140 | item_number_t *rhs; |
03b31c0c AD |
141 | |
142 | /* This symbol provides both the associativity, and the precedence. */ | |
db8837cb | 143 | symbol_t *prec; |
03b31c0c AD |
144 | |
145 | /* This symbol was attached to the rule via %prec. */ | |
db8837cb | 146 | symbol_t *precsym; |
03b31c0c | 147 | |
62a3e4f0 | 148 | int line; |
68f1e3ed | 149 | bool useful; |
f499b062 | 150 | |
3f96f4dc | 151 | const char *action; |
62a3e4f0 | 152 | int action_line; |
f499b062 AD |
153 | |
154 | const char *guard; | |
62a3e4f0 | 155 | int guard_line; |
652a871c AD |
156 | } rule_t; |
157 | ||
1a2b5d37 | 158 | extern struct rule_s *rules; |
652a871c | 159 | |
0e78e603 | 160 | /* Table of the symbols, indexed by the symbol number. */ |
db8837cb | 161 | extern symbol_t **symbols; |
0e78e603 | 162 | |
680e8701 AD |
163 | /* TOKEN_TRANSLATION -- a table indexed by a token number as returned |
164 | by the user's yylex routine, it yields the internal token number | |
165 | used by the parser and throughout bison. */ | |
680e8701 | 166 | extern token_number_t *token_translations; |
f7d4d87a DM |
167 | extern int max_user_token_number; |
168 | ||
b2ca4022 AD |
169 | /* SEMANTIC_PARSER is nonzero if the input file says to use the hairy |
170 | parser that provides for semantic error recovery. If it is zero, | |
171 | the yacc-compatible simplified parser is used. */ | |
f7d4d87a DM |
172 | |
173 | extern int semantic_parser; | |
174 | ||
b2ca4022 AD |
175 | /* PURE_PARSER is nonzero if should generate a parser that is all pure |
176 | and reentrant. */ | |
f7d4d87a DM |
177 | |
178 | extern int pure_parser; | |
179 | ||
c3b407f4 AD |
180 | /* Report the length of the RHS. */ |
181 | int rule_rhs_length PARAMS ((rule_t *rule)); | |
3067fbef AD |
182 | |
183 | /* Dump RITEM for traces. */ | |
c2713865 AD |
184 | void ritem_print PARAMS ((FILE *out)); |
185 | ||
186 | /* Return the size of the longest rule RHS. */ | |
187 | size_t ritem_longest_rhs PARAMS ((void)); | |
188 | ||
78ab8f67 AD |
189 | /* Dump the grammar. */ |
190 | void grammar_dump PARAMS ((FILE *out, const char *title)); | |
191 | ||
b2ca4022 | 192 | #endif /* !GRAM_H_ */ |