]> git.saurik.com Git - bison.git/blob - src/gram.h
* tests/calc.at (AT_CHECK_CALC): Adjust: there are now additional
[bison.git] / src / gram.h
1 /* Data definitions for internal representation of bison's input,
2 Copyright (C) 1984, 1986, 1989, 1992, 2001, 2002
3 Free Software Foundation, Inc.
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
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.
11
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.
16
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. */
21
22 #ifndef GRAM_H_
23 # define GRAM_H_
24
25 /* Representation of the grammar rules:
26
27 NTOKENS is the number of tokens, and NVARS is the number of
28 variables (nonterminals). NSYMS is the total number, ntokens +
29 nvars.
30
31 Each symbol (either token or variable) receives a symbol number.
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 are accessed via the rule number.
51
52 The rules themselves are described by several arrays: amongst which
53 RITEM, and RULES.
54
55 RULES is an array of struct rule_s, which members are:
56
57 RULES[R].lhs -- the symbol of the left hand side of rule R.
58
59 RULES[R].rhs -- the index in RITEM of the beginning of the portion
60 for rule R.
61
62 RULES[R].prec -- the symbol providing the precedence level of R.
63
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.
68
69 RULES[R].assoc -- the associativity of R.
70
71 RULES[R].line -- the line where R was defined.
72
73 RULES[R].useful -- TRUE iff the rule is used (i.e., FALSE if thrown
74 away by reduce).
75
76 The right hand side is stored as symbol numbers in a portion of
77 RITEM.
78
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.
83
84 The portions of RITEM come in order of increasing rule number.
85 NRITEMS is the total length of RITEM. Each element of RITEM is
86 called an "item" and its index in RITEM is an item number.
87
88 Item numbers are used in the finite state machine to represent
89 places that parsing can get to.
90
91 SYMBOLS[I]->prec records the precedence level of each symbol.
92
93 Precedence levels are assigned in increasing order starting with 1
94 so that numerically higher precedence values mean tighter binding
95 as they ought to. Zero as a symbol or rule's precedence means none
96 is assigned.
97
98 Associativities are recorded similarly in SYMBOLS[I]->assoc. */
99
100 #include "symtab.h"
101
102 #define ISTOKEN(s) ((s) < ntokens)
103 #define ISVAR(s) ((s) >= ntokens)
104
105 extern int nrules;
106 extern int nsyms;
107 extern int ntokens;
108 extern int nvars;
109
110 #define ITEM_NUMBER_MAX INT_MAX
111 typedef int item_number_t;
112 extern item_number_t *ritem;
113 extern unsigned int nritems;
114
115 /* There is weird relationship between item_number_t and
116 token_number_t: we store token_number_t in item_number_t, but in
117 the latter we also store, as negative numbers, the rule numbers.
118
119 Therefore, an token_number_t must be a valid item_number_t, and we
120 sometimes have to perform the converse transformation. */
121 #define token_number_as_item_number(Tok) ((item_number_t) (Tok))
122 #define item_number_as_token_number(Ite) ((token_number_t) (Ite))
123
124 extern token_number_t start_symbol;
125
126
127 typedef struct rule_s
128 {
129 /* The number of the rule in the source. It is usually the index in
130 RULES too, except if there are useless rules. */
131 short user_number;
132
133 /* The index in RULES. Usually the rule number in the source,
134 except if some rules are useless. */
135 short number;
136
137 symbol_t *lhs;
138 item_number_t *rhs;
139
140 /* This symbol provides both the associativity, and the precedence. */
141 symbol_t *prec;
142
143 /* This symbol was attached to the rule via %prec. */
144 symbol_t *precsym;
145
146 int line;
147 bool useful;
148
149 const char *action;
150 int action_line;
151 } rule_t;
152
153 extern struct rule_s *rules;
154
155 /* Table of the symbols, indexed by the symbol number. */
156 extern symbol_t **symbols;
157
158 /* TOKEN_TRANSLATION -- a table indexed by a token number as returned
159 by the user's yylex routine, it yields the internal token number
160 used by the parser and throughout bison. */
161 extern token_number_t *token_translations;
162 extern int max_user_token_number;
163
164
165 /* PURE_PARSER is nonzero if should generate a parser that is all pure
166 and reentrant. */
167
168 extern int pure_parser;
169
170 /* Report the length of the RHS. */
171 int rule_rhs_length PARAMS ((rule_t *rule));
172
173 /* Dump RITEM for traces. */
174 void ritem_print PARAMS ((FILE *out));
175
176 /* Return the size of the longest rule RHS. */
177 size_t ritem_longest_rhs PARAMS ((void));
178
179 /* Dump the grammar. */
180 void grammar_dump PARAMS ((FILE *out, const char *title));
181
182 /* Free the packed grammar. */
183 void grammar_free PARAMS ((void));
184
185 #endif /* !GRAM_H_ */