]> git.saurik.com Git - bison.git/blame - src/gram.h
* src/symlist.c (symbol_list_length): Return int, not unsigned
[bison.git] / src / gram.h
CommitLineData
e46cedb9
PE
1/* Data definitions for internal representation of Bison's input.
2
e9071366 3 Copyright (C) 1984, 1986, 1989, 1992, 2001, 2002, 2003, 2004, 2005, 2006
99013900 4 Free Software Foundation, Inc.
f7d4d87a 5
b2ca4022 6 This file is part of Bison, the GNU Compiler Compiler.
f7d4d87a 7
b2ca4022
AD
8 Bison is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
f7d4d87a 12
b2ca4022
AD
13 Bison is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
f7d4d87a 17
b2ca4022
AD
18 You should have received a copy of the GNU General Public License
19 along with Bison; see the file COPYING. If not, write to
0fb669f9
PE
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
f7d4d87a 22
b2ca4022
AD
23#ifndef GRAM_H_
24# define GRAM_H_
f7d4d87a 25
aea13e97 26/* Representation of the grammar rules:
f7d4d87a 27
aea13e97
AD
28 NTOKENS is the number of tokens, and NVARS is the number of
29 variables (nonterminals). NSYMS is the total number, ntokens +
b2ca4022 30 nvars.
f7d4d87a 31
b2ca4022 32 Each symbol (either token or variable) receives a symbol number.
aea13e97
AD
33 Numbers 0 to NTOKENS - 1 are for tokens, and NTOKENS to NSYMS - 1
34 are for variables. Symbol number zero is the end-of-input token.
35 This token is counted in ntokens. The true number of token values
36 assigned is NTOKENS reduced by one for each alias declaration.
37
38 The rules receive rule numbers 1 to NRULES in the order they are
39 written. More precisely Bison augments the grammar with the
88bce5a2
AD
40 initial rule, `$accept: START-SYMBOL $end', which is numbered 1,
41 all the user rules are 2, 3 etc. Each time a rule number is
42 presented to the user, we subtract 1, so *displayed* rule numbers
43 are 0, 1, 2...
aea13e97
AD
44
45 Internally, we cannot use the number 0 for a rule because for
46 instance RITEM stores both symbol (the RHS) and rule numbers: the
47 symbols are shorts >= 0, and rule number are stored negative.
48 Therefore 0 cannot be used, since it would be both the rule number
88bce5a2 49 0, and the token $end).
aea13e97 50
fdbcd8e2 51 Actions are accessed via the rule number.
f7d4d87a 52
b2ed6e58 53 The rules themselves are described by several arrays: amongst which
1a2b5d37 54 RITEM, and RULES.
b2ed6e58 55
7478c462 56 RULES is an array of rules, whose members are:
b2ed6e58 57
03b31c0c 58 RULES[R].lhs -- the symbol of the left hand side of rule R.
b2ed6e58 59
aea13e97
AD
60 RULES[R].rhs -- the index in RITEM of the beginning of the portion
61 for rule R.
f7d4d87a 62
03b31c0c 63 RULES[R].prec -- the symbol providing the precedence level of R.
652a871c 64
03b31c0c
AD
65 RULES[R].precsym -- the symbol attached (via %prec) to give its
66 precedence to R. Of course, if set, it is equal to `prec', but we
67 need to distinguish one from the other when reducing: a symbol used
68 in a %prec is not useless.
652a871c 69
1a2b5d37 70 RULES[R].assoc -- the associativity of R.
e41dc700 71
88bce5a2
AD
72 RULES[R].dprec -- the dynamic precedence level of R (for GLR
73 parsing).
676385e2 74
88bce5a2
AD
75 RULES[R].merger -- index of merging function for R (for GLR
76 parsing).
676385e2 77
1a2b5d37 78 RULES[R].line -- the line where R was defined.
652a871c 79
738c69de 80 RULES[R].useful -- true iff the rule is used (i.e., false if thrown
03b31c0c 81 away by reduce).
68f1e3ed 82
b2ed6e58
AD
83 The right hand side is stored as symbol numbers in a portion of
84 RITEM.
f7d4d87a 85
b2ca4022
AD
86 The length of the portion is one greater than the number of symbols
87 in the rule's right hand side. The last element in the portion
88 contains minus R, which identifies it as the end of a portion and
89 says which rule it is for.
f7d4d87a 90
a900a624
AD
91 The portions of RITEM come in order of increasing rule number.
92 NRITEMS is the total length of RITEM. Each element of RITEM is
93 called an "item" and its index in RITEM is an item number.
f7d4d87a 94
b2ca4022
AD
95 Item numbers are used in the finite state machine to represent
96 places that parsing can get to.
f7d4d87a 97
aea13e97 98 SYMBOLS[I]->prec records the precedence level of each symbol.
f7d4d87a 99
b2ca4022
AD
100 Precedence levels are assigned in increasing order starting with 1
101 so that numerically higher precedence values mean tighter binding
102 as they ought to. Zero as a symbol or rule's precedence means none
103 is assigned.
f7d4d87a 104
aea13e97 105 Associativities are recorded similarly in SYMBOLS[I]->assoc. */
f7d4d87a 106
8efe435c
AD
107# include "location.h"
108# include "symtab.h"
f7d4d87a 109
ba24760f
PE
110# define ISTOKEN(i) ((i) < ntokens)
111# define ISVAR(i) ((i) >= ntokens)
f7d4d87a 112
f7d4d87a
DM
113extern int nsyms;
114extern int ntokens;
115extern int nvars;
116
7478c462 117typedef int item_number;
e9071366 118#define ITEM_NUMBER_MAX INT_MAX
7478c462 119extern item_number *ritem;
0c2d3f4c 120extern unsigned int nritems;
b2ed6e58 121
7478c462
PE
122/* There is weird relationship between OT1H item_number and OTOH
123 symbol_number and rule_number: we store the latter in
124 item_number. symbol_number values are stored as-is, while
125 the negation of (rule_number + 1) is stored.
5fbb0954 126
7478c462 127 Therefore, a symbol_number must be a valid item_number, and we
5fbb0954 128 sometimes have to perform the converse transformation. */
e46cedb9
PE
129
130static inline item_number
ba24760f 131symbol_number_as_item_number (symbol_number sym)
e46cedb9 132{
ba24760f 133 return sym;
e46cedb9
PE
134}
135
136static inline symbol_number
137item_number_as_symbol_number (item_number i)
138{
139 return i;
140}
5fbb0954 141
36b5e963
AD
142static inline bool
143item_number_is_symbol_number (item_number i)
144{
145 return i >= 0;
146}
147
7478c462 148/* Rule numbers. */
f6fbd3da 149typedef int rule_number;
e9071366 150#define RULE_NUMBER_MAX INT_MAX
7478c462 151extern rule_number nrules;
e46cedb9
PE
152
153static inline item_number
154rule_number_as_item_number (rule_number r)
155{
156 return -1 - r;
157}
158
159static inline rule_number
160item_number_as_rule_number (item_number i)
161{
162 return -1 - i;
163}
9222837b 164
36b5e963
AD
165static inline bool
166item_number_is_rule_number (item_number i)
167{
168 return i < 0;
169}
9222837b
AD
170
171/*--------.
172| Rules. |
173`--------*/
62a3e4f0 174
7478c462 175typedef struct
652a871c 176{
c3b407f4
AD
177 /* The number of the rule in the source. It is usually the index in
178 RULES too, except if there are useless rules. */
7478c462 179 rule_number user_number;
d7e1f00c
AD
180
181 /* The index in RULES. Usually the rule number in the source,
182 except if some rules are useless. */
7478c462 183 rule_number number;
c3b407f4 184
7478c462
PE
185 symbol *lhs;
186 item_number *rhs;
03b31c0c
AD
187
188 /* This symbol provides both the associativity, and the precedence. */
7478c462 189 symbol *prec;
03b31c0c 190
f6fbd3da
PE
191 int dprec;
192 int merger;
676385e2 193
03b31c0c 194 /* This symbol was attached to the rule via %prec. */
7478c462 195 symbol *precsym;
03b31c0c 196
7478c462 197 location location;
68f1e3ed 198 bool useful;
f499b062 199
3f96f4dc 200 const char *action;
7478c462
PE
201 location action_location;
202} rule;
652a871c 203
7478c462 204extern rule *rules;
652a871c 205
c8f002c7 206/* A function that selects a rule. */
7478c462 207typedef bool (*rule_filter) (rule *);
c8f002c7
AD
208
209/* Return true IFF the rule has a `number' smaller than NRULES. */
7478c462 210bool rule_useful_p (rule *r);
c8f002c7
AD
211
212/* Return true IFF the rule has a `number' higher than NRULES. */
7478c462 213bool rule_useless_p (rule *r);
c8f002c7
AD
214
215/* Return true IFF the rule is not flagged as useful *and* is useful.
216 In other words, it was discarded because of conflicts. */
7478c462 217bool rule_never_reduced_p (rule *r);
c8f002c7 218
7478c462 219/* Print this rule's number and lhs on OUT. If a PREVIOUS_LHS was
c8f002c7
AD
220 already displayed (by a previous call for another rule), avoid
221 useless repetitions. */
7478c462 222void rule_lhs_print (rule *r, symbol *previous_lhs, FILE *out);
c8f002c7
AD
223
224/* Return the length of the RHS. */
7478c462 225int rule_rhs_length (rule *r);
c8f002c7 226
7478c462
PE
227/* Print this rule's RHS on OUT. */
228void rule_rhs_print (rule *r, FILE *out);
c8f002c7 229
7478c462
PE
230/* Print this rule on OUT. */
231void rule_print (rule *r, FILE *out);
c8f002c7
AD
232
233
234
235
0e78e603 236/* Table of the symbols, indexed by the symbol number. */
7478c462 237extern symbol **symbols;
0e78e603 238
680e8701
AD
239/* TOKEN_TRANSLATION -- a table indexed by a token number as returned
240 by the user's yylex routine, it yields the internal token number
241 used by the parser and throughout bison. */
7478c462 242extern symbol_number *token_translations;
f7d4d87a
DM
243extern int max_user_token_number;
244
f7d4d87a 245
c8f002c7 246
3067fbef 247/* Dump RITEM for traces. */
d33cb3ae 248void ritem_print (FILE *out);
c2713865
AD
249
250/* Return the size of the longest rule RHS. */
d33cb3ae 251size_t ritem_longest_rhs (void);
c2713865 252
9757c359
AD
253/* Print the grammar's rules numbers from BEGIN (inclusive) to END
254 (exclusive) on OUT under TITLE. */
d33cb3ae 255void grammar_rules_partial_print (FILE *out, const char *title,
7478c462 256 rule_filter filter);
9757c359 257
6b98e4b5 258/* Print the grammar's rules on OUT. */
d33cb3ae 259void grammar_rules_print (FILE *out);
6b98e4b5 260
78ab8f67 261/* Dump the grammar. */
d33cb3ae 262void grammar_dump (FILE *out, const char *title);
78ab8f67 263
c8f002c7
AD
264/* Report on STDERR the rules that are not flagged USEFUL, using the
265 MESSAGE (which can be `useless rule' when invoked after grammar
266 reduction, or `never reduced' after conflicts were taken into
267 account). */
d33cb3ae 268void grammar_rules_never_reduced_report (const char *message);
c8f002c7 269
5372019f 270/* Free the packed grammar. */
d33cb3ae 271void grammar_free (void);
5372019f 272
b2ca4022 273#endif /* !GRAM_H_ */