gram.h revision cea198a11f15a2eb071d98491ca9a8bc8cebfbc4
1b3db213eb55acb661e4b9ea40bcc00af4b76fab9Glenn Kasten/* Data definitions for internal representation of Bison's input. 29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project Copyright (C) 1984, 1986, 1989, 1992, 2001, 2002, 2003, 2004, 2005 49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project Free Software Foundation, Inc. 59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project This file is part of Bison, the GNU Compiler Compiler. 79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project Bison is free software; you can redistribute it and/or modify 99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project it under the terms of the GNU General Public License as published by 109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project the Free Software Foundation; either version 2, or (at your option) 119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project any later version. 129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project Bison is distributed in the hope that it will be useful, 149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project but WITHOUT ANY WARRANTY; without even the implied warranty of 159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project GNU General Public License for more details. 179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project You should have received a copy of the GNU General Public License 199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project along with Bison; see the file COPYING. If not, write to 209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project Boston, MA 02110-1301, USA. */ 229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#ifndef GRAM_H_ 24c81d31c3f801ba3d559a22c27b926ace38a7ab49Glenn Kasten# define GRAM_H_ 25c81d31c3f801ba3d559a22c27b926ace38a7ab49Glenn Kasten 26c81d31c3f801ba3d559a22c27b926ace38a7ab49Glenn Kasten/* Representation of the grammar rules: 279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 28c81d31c3f801ba3d559a22c27b926ace38a7ab49Glenn Kasten NTOKENS is the number of tokens, and NVARS is the number of 29c81d31c3f801ba3d559a22c27b926ace38a7ab49Glenn Kasten variables (nonterminals). NSYMS is the total number, ntokens + 30c81d31c3f801ba3d559a22c27b926ace38a7ab49Glenn Kasten nvars. 319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project Each symbol (either token or variable) receives a symbol number. 339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project Numbers 0 to NTOKENS - 1 are for tokens, and NTOKENS to NSYMS - 1 349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project are for variables. Symbol number zero is the end-of-input token. 359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project This token is counted in ntokens. The true number of token values 369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project assigned is NTOKENS reduced by one for each alias declaration. 379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project The rules receive rule numbers 1 to NRULES in the order they are 399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project written. More precisely Bison augments the grammar with the 409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project initial rule, `$accept: START-SYMBOL $end', which is numbered 1, 4196c08a69ea0b95d1d8a8edb67f73bd9548e09f16Eric Laurent all the user rules are 2, 3 etc. Each time a rule number is 420765c448ab7d51355a7b1e82d359acfcf169f481Glenn Kasten presented to the user, we subtract 1, so *displayed* rule numbers 439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project are 0, 1, 2... 449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project Internally, we cannot use the number 0 for a rule because for 469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project instance RITEM stores both symbol (the RHS) and rule numbers: the 4769a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes symbols are shorts >= 0, and rule number are stored negative. 489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project Therefore 0 cannot be used, since it would be both the rule number 499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 0, and the token $end). 509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5123f7ad39ef52c0ac0a94934a71b6802c0a806b7fGlenn Kasten Actions are accessed via the rule number. 529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project The rules themselves are described by several arrays: amongst which 549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project RITEM, and RULES. 550765c448ab7d51355a7b1e82d359acfcf169f481Glenn Kasten 569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project RULES is an array of rules, whose members are: 579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project RULES[R].lhs -- the symbol of the left hand side of rule R. 599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 600765c448ab7d51355a7b1e82d359acfcf169f481Glenn Kasten RULES[R].rhs -- the index in RITEM of the beginning of the portion 619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project for rule R. 6269a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes 639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project RULES[R].prec -- the symbol providing the precedence level of R. 649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project RULES[R].precsym -- the symbol attached (via %prec) to give its 669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project precedence to R. Of course, if set, it is equal to `prec', but we 679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project need to distinguish one from the other when reducing: a symbol used 68505e5c8859f596ed58489be565d6e029314b2ac8Eric Laurent in a %prec is not useless. 69505e5c8859f596ed58489be565d6e029314b2ac8Eric Laurent 70505e5c8859f596ed58489be565d6e029314b2ac8Eric Laurent RULES[R].assoc -- the associativity of R. 71505e5c8859f596ed58489be565d6e029314b2ac8Eric Laurent 72505e5c8859f596ed58489be565d6e029314b2ac8Eric Laurent RULES[R].dprec -- the dynamic precedence level of R (for GLR 73505e5c8859f596ed58489be565d6e029314b2ac8Eric Laurent parsing). 74505e5c8859f596ed58489be565d6e029314b2ac8Eric Laurent 75505e5c8859f596ed58489be565d6e029314b2ac8Eric Laurent RULES[R].merger -- index of merging function for R (for GLR 76505e5c8859f596ed58489be565d6e029314b2ac8Eric Laurent parsing). 77505e5c8859f596ed58489be565d6e029314b2ac8Eric Laurent 789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project RULES[R].line -- the line where R was defined. 799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project RULES[R].useful -- true iff the rule is used (i.e., false if thrown 810765c448ab7d51355a7b1e82d359acfcf169f481Glenn Kasten away by reduce). 829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project The right hand side is stored as symbol numbers in a portion of 849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project RITEM. 851137be1a686fdfc9f02c3aca7c33f28006df4742Glenn Kasten 869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project The length of the portion is one greater than the number of symbols 879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project in the rule's right hand side. The last element in the portion 889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project contains minus R, which identifies it as the end of a portion and 899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project says which rule it is for. 90bc1d77b6cbce23fbe25f7231651037ae195bc90eGlenn Kasten 919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project The portions of RITEM come in order of increasing rule number. 929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project NRITEMS is the total length of RITEM. Each element of RITEM is 939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project called an "item" and its index in RITEM is an item number. 940765c448ab7d51355a7b1e82d359acfcf169f481Glenn Kasten 959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project Item numbers are used in the finite state machine to represent 969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project places that parsing can get to. 970765c448ab7d51355a7b1e82d359acfcf169f481Glenn Kasten 989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project SYMBOLS[I]->prec records the precedence level of each symbol. 999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project Precedence levels are assigned in increasing order starting with 1 1010765c448ab7d51355a7b1e82d359acfcf169f481Glenn Kasten so that numerically higher precedence values mean tighter binding 1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project as they ought to. Zero as a symbol or rule's precedence means none 1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project is assigned. 1040765c448ab7d51355a7b1e82d359acfcf169f481Glenn Kasten 10569a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes Associativities are recorded similarly in SYMBOLS[I]->assoc. */ 1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project# include "location.h" 1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project# include "symtab.h" 1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project# define ISTOKEN(i) ((i) < ntokens) 1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project# define ISVAR(i) ((i) >= ntokens) 1120765c448ab7d51355a7b1e82d359acfcf169f481Glenn Kasten 1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectextern int nsyms; 1149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectextern int ntokens; 1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectextern int nvars; 1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1170765c448ab7d51355a7b1e82d359acfcf169f481Glenn Kastentypedef int item_number; 1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectextern item_number *ritem; 1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectextern unsigned int nritems; 1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/* There is weird relationship between OT1H item_number and OTOH 1220765c448ab7d51355a7b1e82d359acfcf169f481Glenn Kasten symbol_number and rule_number: we store the latter in 1230765c448ab7d51355a7b1e82d359acfcf169f481Glenn Kasten item_number. symbol_number values are stored as-is, while 1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project the negation of (rule_number + 1) is stored. 1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project Therefore, a symbol_number must be a valid item_number, and we 1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project sometimes have to perform the converse transformation. */ 1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic inline item_number 1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectsymbol_number_as_item_number (symbol_number sym) 13196c08a69ea0b95d1d8a8edb67f73bd9548e09f16Eric Laurent{ 1329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return sym; 133505e5c8859f596ed58489be565d6e029314b2ac8Eric Laurent} 1349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic inline symbol_number 1369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectitem_number_as_symbol_number (item_number i) 1379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{ 1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return i; 1399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic inline bool 1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectitem_number_is_symbol_number (item_number i) 1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{ 1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return i >= 0; 1453762c311729fe9f3af085c14c5c1fb471d994c03Steve Block} 1469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/* Rule numbers. */ 1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projecttypedef int rule_number; 1499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectextern rule_number nrules; 1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1513762c311729fe9f3af085c14c5c1fb471d994c03Steve Blockstatic inline item_number 1529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectrule_number_as_item_number (rule_number r) 1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{ 15471f2cf116aab893e224056c38ab146bd1538dd3eSteve Block return -1 - r; 1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 1569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic inline rule_number 1589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectitem_number_as_rule_number (item_number i) 159{ 160 return -1 - i; 161} 162 163static inline bool 164item_number_is_rule_number (item_number i) 165{ 166 return i < 0; 167} 168 169/*--------. 170| Rules. | 171`--------*/ 172 173typedef struct 174{ 175 /* The number of the rule in the source. It is usually the index in 176 RULES too, except if there are useless rules. */ 177 rule_number user_number; 178 179 /* The index in RULES. Usually the rule number in the source, 180 except if some rules are useless. */ 181 rule_number number; 182 183 symbol *lhs; 184 item_number *rhs; 185 186 /* This symbol provides both the associativity, and the precedence. */ 187 symbol *prec; 188 189 int dprec; 190 int merger; 191 192 /* This symbol was attached to the rule via %prec. */ 193 symbol *precsym; 194 195 location location; 196 bool useful; 197 198 const char *action; 199 location action_location; 200} rule; 201 202extern rule *rules; 203 204/* A function that selects a rule. */ 205typedef bool (*rule_filter) (rule *); 206 207/* Return true IFF the rule has a `number' smaller than NRULES. */ 208bool rule_useful_p (rule *r); 209 210/* Return true IFF the rule has a `number' higher than NRULES. */ 211bool rule_useless_p (rule *r); 212 213/* Return true IFF the rule is not flagged as useful *and* is useful. 214 In other words, it was discarded because of conflicts. */ 215bool rule_never_reduced_p (rule *r); 216 217/* Print this rule's number and lhs on OUT. If a PREVIOUS_LHS was 218 already displayed (by a previous call for another rule), avoid 219 useless repetitions. */ 220void rule_lhs_print (rule *r, symbol *previous_lhs, FILE *out); 221 222/* Return the length of the RHS. */ 223int rule_rhs_length (rule *r); 224 225/* Print this rule's RHS on OUT. */ 226void rule_rhs_print (rule *r, FILE *out); 227 228/* Print this rule on OUT. */ 229void rule_print (rule *r, FILE *out); 230 231 232 233 234/* Table of the symbols, indexed by the symbol number. */ 235extern symbol **symbols; 236 237/* TOKEN_TRANSLATION -- a table indexed by a token number as returned 238 by the user's yylex routine, it yields the internal token number 239 used by the parser and throughout bison. */ 240extern symbol_number *token_translations; 241extern int max_user_token_number; 242 243 244 245/* Dump RITEM for traces. */ 246void ritem_print (FILE *out); 247 248/* Return the size of the longest rule RHS. */ 249size_t ritem_longest_rhs (void); 250 251/* Print the grammar's rules numbers from BEGIN (inclusive) to END 252 (exclusive) on OUT under TITLE. */ 253void grammar_rules_partial_print (FILE *out, const char *title, 254 rule_filter filter); 255 256/* Print the grammar's rules on OUT. */ 257void grammar_rules_print (FILE *out); 258 259/* Dump the grammar. */ 260void grammar_dump (FILE *out, const char *title); 261 262/* Report on STDERR the rules that are not flagged USEFUL, using the 263 MESSAGE (which can be `useless rule' when invoked after grammar 264 reduction, or `never reduced' after conflicts were taken into 265 account). */ 266void grammar_rules_never_reduced_report (const char *message); 267 268/* Free the packed grammar. */ 269void grammar_free (void); 270 271#endif /* !GRAM_H_ */ 272