]> git.saurik.com Git - bison.git/blame - src/gram.h
* configure.in: Use AC_FUNC_STRNLEN.
[bison.git] / src / gram.h
CommitLineData
f7d4d87a 1/* Data definitions for internal representation of bison's input,
b2ed6e58 2 Copyright 1984, 1986, 1989, 1992, 2001 Free Software Foundation, Inc.
f7d4d87a 3
b2ca4022 4 This file is part of Bison, the GNU Compiler Compiler.
f7d4d87a 5
b2ca4022
AD
6 Bison is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
f7d4d87a 10
b2ca4022
AD
11 Bison is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
f7d4d87a 15
b2ca4022
AD
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
f7d4d87a 20
b2ca4022
AD
21#ifndef GRAM_H_
22# define GRAM_H_
f7d4d87a 23
aea13e97 24/* Representation of the grammar rules:
f7d4d87a 25
aea13e97
AD
26 NTOKENS is the number of tokens, and NVARS is the number of
27 variables (nonterminals). NSYMS is the total number, ntokens +
b2ca4022 28 nvars.
f7d4d87a 29
b2ca4022 30 Each symbol (either token or variable) receives a symbol number.
aea13e97
AD
31 Numbers 0 to NTOKENS - 1 are for tokens, and NTOKENS to NSYMS - 1
32 are for variables. Symbol number zero is the end-of-input token.
33 This token is counted in ntokens. The true number of token values
34 assigned is NTOKENS reduced by one for each alias declaration.
35
36 The rules receive rule numbers 1 to NRULES in the order they are
37 written. More precisely Bison augments the grammar with the
38 initial rule, `$axiom: START-SYMBOL EOF', which is numbered 1, all
39 the user rules are 2, 3 etc. Each time a rule number is presented
40 to the user, we subtract 1, so *displayed* rule numbers are 0, 1,
41 2...
42
43 Internally, we cannot use the number 0 for a rule because for
44 instance RITEM stores both symbol (the RHS) and rule numbers: the
45 symbols are shorts >= 0, and rule number are stored negative.
46 Therefore 0 cannot be used, since it would be both the rule number
47 0, and the token EOF).
48
49 Actions and guards are accessed via the rule number.
f7d4d87a 50
b2ed6e58 51 The rules themselves are described by several arrays: amongst which
1a2b5d37 52 RITEM, and RULES.
b2ed6e58 53
1a2b5d37 54 RULES is an array of struct rule_s, which members are:
b2ed6e58 55
aea13e97
AD
56 RULES[R].lhs -- the symbol number of the left hand side of rule R.
57 If -1, the rule has been thrown out by reduce.c and should be
58 ignored.
b2ed6e58 59
aea13e97
AD
60 RULES[R].rhs -- the index in RITEM of the beginning of the portion
61 for rule R.
f7d4d87a 62
1a2b5d37 63 RULES[R].prec -- the precedence level of R.
652a871c 64
aea13e97
AD
65 RULES[R].precsym -- the symbol-number of the symbol in %prec for R
66 (if any).
652a871c 67
1a2b5d37 68 RULES[R].assoc -- the associativity of R.
e41dc700 69
1a2b5d37 70 RULES[R].line -- the line where R was defined.
652a871c 71
1a2b5d37 72 RULES[R].useful -- TRUE iff the rule is used.
68f1e3ed 73
b2ed6e58
AD
74 The right hand side is stored as symbol numbers in a portion of
75 RITEM.
f7d4d87a 76
b2ca4022
AD
77 The length of the portion is one greater than the number of symbols
78 in the rule's right hand side. The last element in the portion
79 contains minus R, which identifies it as the end of a portion and
80 says which rule it is for.
f7d4d87a 81
b2ed6e58 82 The portions of RITEM come in order of increasing rule number and
b2ca4022
AD
83 are followed by an element which is zero to mark the end. nitems
84 is the total length of ritem, not counting the final zero. Each
aea13e97 85 element of RITEM is called an "item" and its index in RITEM is an
b2ca4022 86 item number.
f7d4d87a 87
b2ca4022
AD
88 Item numbers are used in the finite state machine to represent
89 places that parsing can get to.
f7d4d87a 90
aea13e97 91 SYMBOLS[I]->prec records the precedence level of each symbol.
f7d4d87a 92
b2ca4022
AD
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.
f7d4d87a 97
aea13e97 98 Associativities are recorded similarly in SYMBOLS[I]->assoc. */
f7d4d87a
DM
99
100
101#define ISTOKEN(s) ((s) < ntokens)
102#define ISVAR(s) ((s) >= ntokens)
103
f7d4d87a
DM
104extern int nitems;
105extern int nrules;
106extern int nsyms;
107extern int ntokens;
108extern int nvars;
109
110extern short *ritem;
75142d45 111extern int nritems;
b2ed6e58 112
f7d4d87a
DM
113extern int start_symbol;
114
aea13e97 115/* Associativity values for tokens and rules. */
d7020c20
AD
116typedef enum
117{
118 right_assoc,
119 left_assoc,
120 non_assoc
121} associativity;
f7d4d87a 122
f7d4d87a 123
652a871c
AD
124typedef struct rule_s
125{
126 short lhs;
127 short rhs;
128 short prec;
129 short precsym;
aea13e97 130 associativity assoc;
e41dc700 131 short line;
68f1e3ed 132 bool useful;
f499b062 133
3f96f4dc
AD
134 const char *action;
135 short action_line;
f499b062
AD
136
137 const char *guard;
138 short guard_line;
652a871c
AD
139} rule_t;
140
1a2b5d37 141extern struct rule_s *rules;
652a871c 142
0e78e603
AD
143/* Table of the symbols, indexed by the symbol number. */
144extern struct bucket **symbols;
145
b2ca4022
AD
146/* token translation table: indexed by a token number as returned by
147 the user's yylex routine, it yields the internal token number used
342b8b6e 148 by the parser and throughout bison. */
f7d4d87a
DM
149
150extern short *token_translations;
f7d4d87a
DM
151extern int max_user_token_number;
152
b2ca4022
AD
153/* SEMANTIC_PARSER is nonzero if the input file says to use the hairy
154 parser that provides for semantic error recovery. If it is zero,
155 the yacc-compatible simplified parser is used. */
f7d4d87a
DM
156
157extern int semantic_parser;
158
b2ca4022
AD
159/* PURE_PARSER is nonzero if should generate a parser that is all pure
160 and reentrant. */
f7d4d87a
DM
161
162extern int pure_parser;
163
b2ca4022 164/* ERROR_TOKEN_NUMBER is the token number of the error token. */
f7d4d87a
DM
165
166extern int error_token_number;
3067fbef
AD
167
168
169/* Dump RITEM for traces. */
c2713865
AD
170void ritem_print PARAMS ((FILE *out));
171
172/* Return the size of the longest rule RHS. */
173size_t ritem_longest_rhs PARAMS ((void));
174
b2ca4022 175#endif /* !GRAM_H_ */