]> git.saurik.com Git - bison.git/blob - src/gram.h
616316405ebc67966eb4e978e06ac3fea6a96b02
[bison.git] / src / gram.h
1 /* Data definitions for internal representation of bison's input,
2 Copyright 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 and guards 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 number of the left hand side of rule R.
58 If -1, the rule has been thrown out by reduce.c and should be
59 ignored.
60
61 RULES[R].rhs -- the index in RITEM of the beginning of the portion
62 for rule R.
63
64 RULES[R].prec -- the precedence level of R.
65
66 RULES[R].precsym -- the symbol-number of the symbol in %prec for R
67 (if any).
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.
74
75 The right hand side is stored as symbol numbers in a portion of
76 RITEM.
77
78 The length of the portion is one greater than the number of symbols
79 in the rule's right hand side. The last element in the portion
80 contains minus R, which identifies it as the end of a portion and
81 says which rule it is for.
82
83 The portions of RITEM come in order of increasing rule number and
84 are followed by an element which is zero to mark the end. nitems
85 is the total length of ritem, not counting the final zero. Each
86 element of RITEM is called an "item" and its index in RITEM is an
87 item number.
88
89 Item numbers are used in the finite state machine to represent
90 places that parsing can get to.
91
92 SYMBOLS[I]->prec records the precedence level of each symbol.
93
94 Precedence levels are assigned in increasing order starting with 1
95 so that numerically higher precedence values mean tighter binding
96 as they ought to. Zero as a symbol or rule's precedence means none
97 is assigned.
98
99 Associativities are recorded similarly in SYMBOLS[I]->assoc. */
100
101
102 #define ISTOKEN(s) ((s) < ntokens)
103 #define ISVAR(s) ((s) >= ntokens)
104
105 extern int nitems;
106 extern int nrules;
107 extern int nsyms;
108 extern int ntokens;
109 extern int nvars;
110
111 extern short *ritem;
112 extern int nritems;
113
114 extern int start_symbol;
115
116 /* Associativity values for tokens and rules. */
117 typedef enum
118 {
119 right_assoc,
120 left_assoc,
121 non_assoc
122 } associativity;
123
124
125 typedef struct rule_s
126 {
127 short lhs;
128 short *rhs;
129 short prec;
130 short precsym;
131 associativity assoc;
132 short line;
133 bool useful;
134
135 const char *action;
136 short action_line;
137
138 const char *guard;
139 short guard_line;
140 } rule_t;
141
142 extern struct rule_s *rules;
143
144 /* Table of the symbols, indexed by the symbol number. */
145 extern struct bucket **symbols;
146
147 /* token translation table: indexed by a token number as returned by
148 the user's yylex routine, it yields the internal token number used
149 by the parser and throughout bison. */
150
151 extern short *token_translations;
152 extern int max_user_token_number;
153
154 /* SEMANTIC_PARSER is nonzero if the input file says to use the hairy
155 parser that provides for semantic error recovery. If it is zero,
156 the yacc-compatible simplified parser is used. */
157
158 extern int semantic_parser;
159
160 /* PURE_PARSER is nonzero if should generate a parser that is all pure
161 and reentrant. */
162
163 extern int pure_parser;
164
165 /* ERROR_TOKEN_NUMBER is the token number of the error token. */
166
167 extern int error_token_number;
168
169
170 /* Dump RITEM for traces. */
171 void ritem_print PARAMS ((FILE *out));
172
173 /* Return the size of the longest rule RHS. */
174 size_t ritem_longest_rhs PARAMS ((void));
175
176 #endif /* !GRAM_H_ */