]> git.saurik.com Git - bison.git/blame - src/gram.h
(output_skeleton): Use get_errno instead of errno.
[bison.git] / src / gram.h
CommitLineData
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
88bce5a2
AD
39 initial rule, `$accept: START-SYMBOL $end', which is numbered 1,
40 all the user rules are 2, 3 etc. Each time a rule number is
41 presented to the user, we subtract 1, so *displayed* rule numbers
42 are 0, 1, 2...
aea13e97
AD
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
88bce5a2 48 0, and the token $end).
aea13e97 49
fdbcd8e2 50 Actions 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
7478c462 55 RULES is an array of rules, whose 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
88bce5a2
AD
71 RULES[R].dprec -- the dynamic precedence level of R (for GLR
72 parsing).
676385e2 73
88bce5a2
AD
74 RULES[R].merger -- index of merging function for R (for GLR
75 parsing).
676385e2 76
1a2b5d37 77 RULES[R].line -- the line where R was defined.
652a871c 78
738c69de 79 RULES[R].useful -- true iff the rule is used (i.e., false if thrown
03b31c0c 80 away by reduce).
68f1e3ed 81
b2ed6e58
AD
82 The right hand side is stored as symbol numbers in a portion of
83 RITEM.
f7d4d87a 84
b2ca4022
AD
85 The length of the portion is one greater than the number of symbols
86 in the rule's right hand side. The last element in the portion
87 contains minus R, which identifies it as the end of a portion and
88 says which rule it is for.
f7d4d87a 89
a900a624
AD
90 The portions of RITEM come in order of increasing rule number.
91 NRITEMS is the total length of RITEM. Each element of RITEM is
92 called an "item" and its index in RITEM is an item number.
f7d4d87a 93
b2ca4022
AD
94 Item numbers are used in the finite state machine to represent
95 places that parsing can get to.
f7d4d87a 96
aea13e97 97 SYMBOLS[I]->prec records the precedence level of each symbol.
f7d4d87a 98
b2ca4022
AD
99 Precedence levels are assigned in increasing order starting with 1
100 so that numerically higher precedence values mean tighter binding
101 as they ought to. Zero as a symbol or rule's precedence means none
102 is assigned.
f7d4d87a 103
aea13e97 104 Associativities are recorded similarly in SYMBOLS[I]->assoc. */
f7d4d87a 105
8efe435c
AD
106# include "location.h"
107# include "symtab.h"
f7d4d87a 108
8efe435c
AD
109# define ISTOKEN(s) ((s) < ntokens)
110# define ISVAR(s) ((s) >= ntokens)
f7d4d87a 111
f7d4d87a
DM
112extern int nsyms;
113extern int ntokens;
114extern int nvars;
115
7478c462
PE
116typedef int item_number;
117extern item_number *ritem;
0c2d3f4c 118extern unsigned int nritems;
b2ed6e58 119
7478c462
PE
120/* There is weird relationship between OT1H item_number and OTOH
121 symbol_number and rule_number: we store the latter in
122 item_number. symbol_number values are stored as-is, while
123 the negation of (rule_number + 1) is stored.
5fbb0954 124
7478c462 125 Therefore, a symbol_number must be a valid item_number, and we
5fbb0954 126 sometimes have to perform the converse transformation. */
7478c462
PE
127# define symbol_number_as_item_number(Tok) ((item_number) (Tok))
128# define item_number_as_symbol_number(Ite) ((symbol_number) (Ite))
5fbb0954 129
7478c462 130extern symbol_number start_symbol;
f7d4d87a 131
7478c462
PE
132/* Rule numbers. */
133typedef short rule_number;
134extern rule_number nrules;
9222837b 135# define int_of_rule_number(RNum) ((int) (RNum))
7478c462
PE
136# define rule_number_as_item_number(RNum) ((item_number) (- RNum - 1))
137# define item_number_as_rule_number(INum) ((rule_number) (- INum - 1))
9222837b
AD
138
139
140/*--------.
141| Rules. |
142`--------*/
62a3e4f0 143
7478c462 144typedef struct
652a871c 145{
c3b407f4
AD
146 /* The number of the rule in the source. It is usually the index in
147 RULES too, except if there are useless rules. */
7478c462 148 rule_number user_number;
d7e1f00c
AD
149
150 /* The index in RULES. Usually the rule number in the source,
151 except if some rules are useless. */
7478c462 152 rule_number number;
c3b407f4 153
7478c462
PE
154 symbol *lhs;
155 item_number *rhs;
03b31c0c
AD
156
157 /* This symbol provides both the associativity, and the precedence. */
7478c462 158 symbol *prec;
03b31c0c 159
676385e2
PH
160 short dprec;
161 short merger;
162
03b31c0c 163 /* This symbol was attached to the rule via %prec. */
7478c462 164 symbol *precsym;
03b31c0c 165
7478c462 166 location location;
68f1e3ed 167 bool useful;
f499b062 168
3f96f4dc 169 const char *action;
7478c462
PE
170 location action_location;
171} rule;
652a871c 172
7478c462 173extern rule *rules;
652a871c 174
c8f002c7 175/* A function that selects a rule. */
7478c462 176typedef bool (*rule_filter) (rule *);
c8f002c7
AD
177
178/* Return true IFF the rule has a `number' smaller than NRULES. */
7478c462 179bool rule_useful_p (rule *r);
c8f002c7
AD
180
181/* Return true IFF the rule has a `number' higher than NRULES. */
7478c462 182bool rule_useless_p (rule *r);
c8f002c7
AD
183
184/* Return true IFF the rule is not flagged as useful *and* is useful.
185 In other words, it was discarded because of conflicts. */
7478c462 186bool rule_never_reduced_p (rule *r);
c8f002c7 187
7478c462 188/* Print this rule's number and lhs on OUT. If a PREVIOUS_LHS was
c8f002c7
AD
189 already displayed (by a previous call for another rule), avoid
190 useless repetitions. */
7478c462 191void rule_lhs_print (rule *r, symbol *previous_lhs, FILE *out);
c8f002c7
AD
192
193/* Return the length of the RHS. */
7478c462 194int rule_rhs_length (rule *r);
c8f002c7 195
7478c462
PE
196/* Print this rule's RHS on OUT. */
197void rule_rhs_print (rule *r, FILE *out);
c8f002c7 198
7478c462
PE
199/* Print this rule on OUT. */
200void rule_print (rule *r, FILE *out);
c8f002c7
AD
201
202
203
204
0e78e603 205/* Table of the symbols, indexed by the symbol number. */
7478c462 206extern symbol **symbols;
0e78e603 207
680e8701
AD
208/* TOKEN_TRANSLATION -- a table indexed by a token number as returned
209 by the user's yylex routine, it yields the internal token number
210 used by the parser and throughout bison. */
7478c462 211extern symbol_number *token_translations;
f7d4d87a
DM
212extern int max_user_token_number;
213
f7d4d87a 214
c8f002c7 215
676385e2
PH
216/* GLR_PARSER is nonzero if the input file says to use the GLR
217 (Generalized LR) parser, and to output some additional
218 information used by the GLR algorithm. */
219
220extern int glr_parser;
221
b2ca4022
AD
222/* PURE_PARSER is nonzero if should generate a parser that is all pure
223 and reentrant. */
f7d4d87a
DM
224
225extern int pure_parser;
226
3067fbef 227/* Dump RITEM for traces. */
d33cb3ae 228void ritem_print (FILE *out);
c2713865
AD
229
230/* Return the size of the longest rule RHS. */
d33cb3ae 231size_t ritem_longest_rhs (void);
c2713865 232
9757c359
AD
233/* Print the grammar's rules numbers from BEGIN (inclusive) to END
234 (exclusive) on OUT under TITLE. */
d33cb3ae 235void grammar_rules_partial_print (FILE *out, const char *title,
7478c462 236 rule_filter filter);
9757c359 237
6b98e4b5 238/* Print the grammar's rules on OUT. */
d33cb3ae 239void grammar_rules_print (FILE *out);
6b98e4b5 240
78ab8f67 241/* Dump the grammar. */
d33cb3ae 242void grammar_dump (FILE *out, const char *title);
78ab8f67 243
c8f002c7
AD
244/* Report on STDERR the rules that are not flagged USEFUL, using the
245 MESSAGE (which can be `useless rule' when invoked after grammar
246 reduction, or `never reduced' after conflicts were taken into
247 account). */
d33cb3ae 248void grammar_rules_never_reduced_report (const char *message);
c8f002c7 249
5372019f 250/* Free the packed grammar. */
d33cb3ae 251void grammar_free (void);
5372019f 252
b2ca4022 253#endif /* !GRAM_H_ */