1 /* Input parser for bison
2 Copyright (C) 1984, 1986, 1989, 1992, 1998, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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. */
35 #include "conflicts.h"
36 #include "muscle_tab.h"
38 typedef struct symbol_list
40 struct symbol_list
*next
;
44 /* The action is attached to the LHS of a rule. */
48 /* The guard is attached to the LHS of a rule. */
55 static symbol_list
*grammar
= NULL
;
56 static int start_flag
= 0;
57 static symbol_t
*startval
= NULL
;
59 /* Nonzero if components of semantic values are used, implying
60 they must be unions. */
61 static int value_components_used
;
63 /* Nonzero if %union has been seen. */
66 /* Incremented for each %left, %right or %nonassoc seen */
67 static int lastprec
= 0;
69 symbol_t
*errtoken
= NULL
;
70 symbol_t
*undeftoken
= NULL
;
71 symbol_t
*eoftoken
= NULL
;
72 symbol_t
*axiom
= NULL
;
75 symbol_list_new (symbol_t
*sym
)
77 symbol_list
*res
= XMALLOC (symbol_list
, 1);
89 /*------------------------.
90 | Operations on symbols. |
91 `------------------------*/
94 /*-----------------------------------------------------------.
95 | If THIS is not defined, report an error, and consider it a |
97 `-----------------------------------------------------------*/
100 symbol_check_defined (symbol_t
*this)
102 if (this->class == unknown_sym
)
105 (_("symbol %s is used, but is not defined as a token and has no rules"),
107 this->class = nterm_sym
;
108 this->number
= nvars
++;
115 /*-------------------------------------------------------------------.
116 | Assign a symbol number, and write the definition of the token name |
117 | into FDEFINES. Put in SYMBOLS. |
118 `-------------------------------------------------------------------*/
121 symbol_make_alias (symbol_t
*symbol
, char *typename
)
124 warn (_("symbol `%s' used more than once as a literal string"),
126 else if (symbol
->alias
)
127 warn (_("symbol `%s' given more than one literal string"),
131 symval
->class = token_sym
;
132 symval
->type_name
= typename
;
133 symval
->user_token_number
= symbol
->user_token_number
;
134 symbol
->user_token_number
= SALIAS
;
135 symval
->alias
= symbol
;
136 symbol
->alias
= symval
;
137 /* symbol and symval combined are only one symbol */
140 assert (ntokens
== symbol
->number
|| ntokens
== symval
->number
);
141 symbol
->number
= symval
->number
=
142 (symval
->number
< symbol
->number
) ? symval
->number
: symbol
->number
;
148 /*---------------------------------------------------------.
149 | Check that THIS, and its alias, have same precedence and |
151 `---------------------------------------------------------*/
154 symbol_check_alias_consistence (symbol_t
*this)
156 /* Check only those who _are_ the aliases. */
157 if (this->alias
&& this->user_token_number
== SALIAS
)
159 if (this->prec
!= this->alias
->prec
)
161 if (this->prec
!= 0 && this->alias
->prec
!= 0)
162 complain (_("conflicting precedences for %s and %s"),
163 this->tag
, this->alias
->tag
);
165 this->alias
->prec
= this->prec
;
167 this->prec
= this->alias
->prec
;
170 if (this->assoc
!= this->alias
->assoc
)
172 if (this->assoc
!= 0 && this->alias
->assoc
!= 0)
173 complain (_("conflicting assoc values for %s and %s"),
174 this->tag
, this->alias
->tag
);
175 if (this->assoc
!= 0)
176 this->alias
->assoc
= this->assoc
;
178 this->assoc
= this->alias
->assoc
;
185 /*-------------------------------------------------------------------.
186 | Assign a symbol number, and write the definition of the token name |
187 | into FDEFINES. Put in SYMBOLS. |
188 `-------------------------------------------------------------------*/
191 symbol_pack (symbol_t
*this)
193 if (this->class == nterm_sym
)
195 this->number
+= ntokens
;
197 else if (this->alias
)
199 /* This symbol and its alias are a single token defn.
200 Allocate a tokno, and assign to both check agreement of
201 prec and assoc fields and make both the same */
202 if (this->number
== NUMBER_UNDEFINED
)
204 if (this == eoftoken
|| this->alias
== eoftoken
)
205 this->number
= this->alias
->number
= 0;
208 assert (this->alias
->number
!= NUMBER_UNDEFINED
);
209 this->number
= this->alias
->number
;
212 /* Do not do processing below for SALIASs. */
213 if (this->user_token_number
== SALIAS
)
216 else /* this->class == token_sym */
218 assert (this->number
!= NUMBER_UNDEFINED
);
221 symbols
[this->number
] = this;
228 /*--------------------------------------------------.
229 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
230 `--------------------------------------------------*/
233 symbol_translation (symbol_t
*this)
236 if (this->class == token_sym
237 && this->user_token_number
!= SALIAS
)
239 /* A token which translation has already been set? */
240 if (token_translations
[this->user_token_number
] != undeftoken
->number
)
241 complain (_("tokens %s and %s both assigned number %d"),
242 symbols
[token_translations
[this->user_token_number
]]->tag
,
243 this->tag
, this->user_token_number
);
245 token_translations
[this->user_token_number
] = this->number
;
252 /*===================\
253 | Low level lexing. |
254 \===================*/
257 skip_to_char (int target
)
261 complain (_(" Skipping to next \\n"));
263 complain (_(" Skipping to next %c"), target
);
266 c
= skip_white_space ();
267 while (c
!= target
&& c
!= EOF
);
273 /*---------------------------------------------------------.
274 | Read a signed integer from STREAM and return its value. |
275 `---------------------------------------------------------*/
278 read_signed_integer (FILE *stream
)
280 int c
= getc (stream
);
292 n
= 10 * n
+ (c
- '0');
301 /*--------------------------------------------------------------.
302 | Get the data type (alternative in the union) of the value for |
303 | symbol N in rule RULE. |
304 `--------------------------------------------------------------*/
307 get_type_name (int n
, symbol_list
*rule
)
314 complain (_("invalid $ value"));
324 if (rp
== NULL
|| rp
->sym
== NULL
)
326 complain (_("invalid $ value"));
332 return rp
->sym
->type_name
;
335 /*------------------------------------------------------------------.
336 | Copy the character C to OOUT, and insert quadigraphs when needed. |
337 `------------------------------------------------------------------*/
340 copy_character (struct obstack
*oout
, int c
)
345 obstack_sgrow (oout
, "@<:@");
349 obstack_sgrow (oout
, "@:>@");
353 obstack_1grow (oout
, c
);
357 /*------------------------------------------------------------.
358 | Dump the string from FIN to OOUT if non null. MATCH is the |
359 | delimiter of the string (either ' or "). |
360 `------------------------------------------------------------*/
363 copy_string2 (FILE *fin
, struct obstack
*oout
, int match
, int store
)
368 obstack_1grow (oout
, match
);
375 fatal (_("unterminated string at end of file"));
378 complain (_("unterminated string"));
380 c
= match
; /* invent terminator */
384 copy_character (oout
, c
);
390 fatal (_("unterminated string at end of file"));
391 copy_character (oout
, c
);
401 obstack_1grow (oout
, c
);
407 copy_string (FILE *fin
, struct obstack
*oout
, int match
)
409 copy_string2 (fin
, oout
, match
, 1);
415 copy_identifier (FILE *fin
, struct obstack
*oout
)
419 while (isalnum (c
= getc (fin
)) || c
== '_')
420 obstack_1grow (oout
, c
);
426 /*------------------------------------------------------------------.
427 | Dump the wannabee comment from IN to OOUT. In fact we just saw a |
428 | `/', which might or might not be a comment. In any case, copy |
430 `------------------------------------------------------------------*/
433 copy_comment (FILE *fin
, struct obstack
*oout
)
439 /* We read a `/', output it. */
440 obstack_1grow (oout
, '/');
442 switch ((c
= getc (fin
)))
455 obstack_1grow (oout
, c
);
461 if (!cplus_comment
&& c
== '*')
465 obstack_1grow (oout
, c
);
471 obstack_1grow (oout
, c
);
478 obstack_1grow (oout
, c
);
485 fatal (_("unterminated comment"));
488 copy_character (oout
, c
);
495 /*-------------------------------------------------------------------.
496 | FIN is pointing to a location (i.e., a `@'). Output to OOUT a |
497 | reference to this location. RULE_LENGTH is the number of values in |
498 | the current rule so far, which says where to find `$0' with |
499 | respect to the top of the stack. |
500 `-------------------------------------------------------------------*/
503 copy_at (FILE *fin
, struct obstack
*oout
, int rule_length
)
510 obstack_sgrow (oout
, "]b4_lhs_location[");
512 else if (isdigit (c
) || c
== '-')
517 n
= read_signed_integer (fin
);
519 complain (_("invalid value: %s%d"), "@", n
);
521 obstack_fgrow2 (oout
, "]b4_rhs_location([%d], [%d])[",
528 complain (_("%s is invalid"), quote (buf
));
533 /*------------------------------------------------------------------.
534 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
536 | Possible inputs: $[<TYPENAME>]($|integer) |
538 | Output to OOUT a reference to this semantic value. RULE_LENGTH is |
539 | the number of values in the current rule so far, which says where |
540 | to find `$0' with respect to the top of the stack. |
541 `------------------------------------------------------------------*/
544 copy_dollar (FILE *fin
, struct obstack
*oout
,
545 symbol_list
*rule
, int rule_length
)
548 const char *type_name
= NULL
;
550 /* Get the type name if explicit. */
553 read_type_name (fin
);
554 type_name
= token_buffer
;
555 value_components_used
= 1;
562 type_name
= get_type_name (0, rule
);
563 if (!type_name
&& typed
)
564 complain (_("$$ of `%s' has no declared type"),
568 obstack_fgrow1 (oout
,
569 "]b4_lhs_value([%s])[", type_name
);
571 else if (isdigit (c
) || c
== '-')
575 n
= read_signed_integer (fin
);
578 complain (_("invalid value: %s%d"), "$", n
);
581 if (!type_name
&& n
> 0)
582 type_name
= get_type_name (n
, rule
);
583 if (!type_name
&& typed
)
584 complain (_("$%d of `%s' has no declared type"),
588 obstack_fgrow3 (oout
, "]b4_rhs_value([%d], [%d], [%s])[",
589 rule_length
, n
, type_name
);
596 complain (_("%s is invalid"), quote (buf
));
600 /*-------------------------------------------------------------------.
601 | Copy the contents of a `%{ ... %}' into the definitions file. The |
602 | `%{' has already been read. Return after reading the `%}'. |
603 `-------------------------------------------------------------------*/
606 copy_definition (struct obstack
*oout
)
609 /* -1 while reading a character if prev char was %. */
614 obstack_fgrow2 (oout
, muscle_find ("linef"),
615 lineno
, quotearg_style (c_quoting_style
,
616 muscle_find ("filename")));
628 obstack_1grow (oout
, c
);
638 copy_string (finput
, oout
, c
);
642 copy_comment (finput
, oout
);
646 fatal ("%s", _("unterminated `%{' definition"));
649 copy_character (oout
, c
);
658 obstack_1grow (oout
, '%');
665 /*-------------------------------------------------------------------.
666 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
667 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
669 `-------------------------------------------------------------------*/
672 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
674 token_t token
= tok_undef
;
675 char *typename
= NULL
;
677 /* The symbol being defined. */
678 symbol_t
*symbol
= NULL
;
680 /* After `%token' and `%nterm', any number of symbols maybe be
684 int tmp_char
= ungetc (skip_white_space (), finput
);
686 /* `%' (for instance from `%token', or from `%%' etc.) is the
687 only valid means to end this declaration. */
691 fatal (_("Premature EOF after %s"), token_buffer
);
694 if (token
== tok_comma
)
699 if (token
== tok_typename
)
701 typename
= xstrdup (token_buffer
);
702 value_components_used
= 1;
705 else if (token
== tok_identifier
&& *symval
->tag
== '\"' && symbol
)
707 symbol_make_alias (symbol
, typename
);
710 else if (token
== tok_identifier
)
712 int oldclass
= symval
->class;
715 if (symbol
->class == what_is_not
)
716 complain (_("symbol %s redefined"), symbol
->tag
);
717 symbol
->class = what_is
;
718 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
719 symbol
->number
= nvars
++;
720 if (what_is
== token_sym
&& symbol
->number
== NUMBER_UNDEFINED
)
721 symbol
->number
= ntokens
++;
725 if (symbol
->type_name
== NULL
)
726 symbol
->type_name
= typename
;
727 else if (strcmp (typename
, symbol
->type_name
) != 0)
728 complain (_("type redeclaration for %s"), symbol
->tag
);
731 else if (symbol
&& token
== tok_number
)
733 symbol
->user_token_number
= numval
;
734 /* User defined EOF token? */
738 eoftoken
->number
= 0;
739 /* It is always mapped to 0, so it was already counted in
746 complain (_("`%s' is invalid in %s"),
748 (what_is
== token_sym
) ? "%token" : "%nterm");
756 /*------------------------------.
757 | Parse what comes after %start |
758 `------------------------------*/
761 parse_start_decl (void)
764 complain (_("multiple %s declarations"), "%start");
765 if (lex () != tok_identifier
)
766 complain (_("invalid %s declaration"), "%start");
774 /*-----------------------------------------------------------.
775 | read in a %type declaration and record its information for |
776 | get_type_name to access |
777 `-----------------------------------------------------------*/
780 parse_type_decl (void)
784 if (lex () != tok_typename
)
786 complain ("%s", _("%type declaration has no <typename>"));
791 name
= xstrdup (token_buffer
);
796 int tmp_char
= ungetc (skip_white_space (), finput
);
801 fatal (_("Premature EOF after %s"), token_buffer
);
813 if (symval
->type_name
== NULL
)
814 symval
->type_name
= name
;
815 else if (strcmp (name
, symval
->type_name
) != 0)
816 complain (_("type redeclaration for %s"), symval
->tag
);
821 complain (_("invalid %%type declaration due to item: %s"),
830 /*----------------------------------------------------------------.
831 | Read in a %left, %right or %nonassoc declaration and record its |
833 `----------------------------------------------------------------*/
836 parse_assoc_decl (associativity assoc
)
841 /* Assign a new precedence level, never 0. */
847 int tmp_char
= ungetc (skip_white_space (), finput
);
852 fatal (_("Premature EOF after %s"), token_buffer
);
859 name
= xstrdup (token_buffer
);
866 if (symval
->prec
!= 0)
867 complain (_("redefining precedence of %s"), symval
->tag
);
868 symval
->prec
= lastprec
;
869 symval
->assoc
= assoc
;
870 if (symval
->class == nterm_sym
)
871 complain (_("symbol %s redefined"), symval
->tag
);
872 if (symval
->number
== NUMBER_UNDEFINED
)
874 symval
->number
= ntokens
++;
875 symval
->class = token_sym
;
878 { /* record the type, if one is specified */
879 if (symval
->type_name
== NULL
)
880 symval
->type_name
= name
;
881 else if (strcmp (name
, symval
->type_name
) != 0)
882 complain (_("type redeclaration for %s"), symval
->tag
);
887 if (prev
== tok_identifier
)
889 symval
->user_token_number
= numval
;
894 (_("invalid text (%s) - number should be after identifier"),
904 complain (_("unexpected item: %s"), token_buffer
);
914 /*--------------------------------------------------------------.
915 | Copy the union declaration into the stype muscle |
916 | (and fdefines), where it is made into the definition of |
917 | YYSTYPE, the type of elements of the parser value stack. |
918 `--------------------------------------------------------------*/
921 parse_union_decl (void)
926 struct obstack union_obstack
;
928 complain (_("multiple %s declarations"), "%union");
932 MUSCLE_INSERT_INT ("stype_line", lineno
);
933 obstack_init (&union_obstack
);
934 obstack_sgrow (&union_obstack
, "union");
940 /* If C contains '/', it is output by copy_comment (). */
942 obstack_1grow (&union_obstack
, c
);
951 copy_comment (finput
, &union_obstack
);
959 /* FIXME: Errr. How could this happen???. --akim */
961 complain (_("unmatched %s"), "`}'");
969 /* JF don't choke on trailing semi */
970 c
= skip_white_space ();
973 obstack_1grow (&union_obstack
, 0);
974 muscle_insert ("stype", obstack_finish (&union_obstack
));
978 /*-------------------------------------------------------.
979 | Parse the declaration %expect N which says to expect N |
980 | shift-reduce conflicts. |
981 `-------------------------------------------------------*/
984 parse_expect_decl (void)
986 int c
= skip_white_space ();
990 complain (_("argument of %%expect is not an integer"));
992 expected_conflicts
= read_signed_integer (finput
);
996 /*-------------------------------------------------------------------.
997 | Parse what comes after %thong. the full syntax is |
999 | %thong <type> token number literal |
1001 | the <type> or number may be omitted. The number specifies the |
1002 | user_token_number. |
1004 | Two symbols are entered in the table, one for the token symbol and |
1005 | one for the literal. Both are given the <type>, if any, from the |
1006 | declaration. The ->user_token_number of the first is SALIAS and |
1007 | the ->user_token_number of the second is set to the number, if |
1008 | any, from the declaration. The two symbols are linked via |
1009 | pointers in their ->alias fields. |
1011 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
1012 | only the literal string is retained it is the literal string that |
1013 | is output to yytname |
1014 `-------------------------------------------------------------------*/
1017 parse_thong_decl (void)
1022 int usrtoknum
= SUNDEF
;
1024 token
= lex (); /* fetch typename or first token */
1025 if (token
== tok_typename
)
1027 typename
= xstrdup (token_buffer
);
1028 value_components_used
= 1;
1029 token
= lex (); /* fetch first token */
1032 /* process first token */
1034 if (token
!= tok_identifier
)
1036 complain (_("unrecognized item %s, expected an identifier"),
1041 symval
->class = token_sym
;
1042 symval
->type_name
= typename
;
1043 symval
->user_token_number
= SALIAS
;
1046 token
= lex (); /* get number or literal string */
1048 if (token
== tok_number
)
1051 token
= lex (); /* okay, did number, now get literal */
1054 /* process literal string token */
1056 if (token
!= tok_identifier
|| *symval
->tag
!= '\"')
1058 complain (_("expected string constant instead of %s"), token_buffer
);
1062 symval
->class = token_sym
;
1063 symval
->type_name
= typename
;
1064 symval
->user_token_number
= usrtoknum
;
1066 symval
->alias
= symbol
;
1067 symbol
->alias
= symval
;
1069 /* symbol and symval combined are only one symbol. */
1075 parse_muscle_decl (void)
1077 int ch
= ungetc (skip_white_space (), finput
);
1082 if (!isalpha (ch
) && ch
!= '_')
1084 complain (_("invalid %s declaration"), "%define");
1088 copy_identifier (finput
, &muscle_obstack
);
1089 obstack_1grow (&muscle_obstack
, 0);
1090 muscle_key
= obstack_finish (&muscle_obstack
);
1093 ch
= skip_white_space ();
1096 ungetc (ch
, finput
);
1099 complain (_("invalid %s declaration"), "%define");
1104 fatal (_("Premature EOF after %s"), "\"");
1106 copy_string2 (finput
, &muscle_obstack
, '"', 0);
1107 obstack_1grow (&muscle_obstack
, 0);
1108 muscle_value
= obstack_finish (&muscle_obstack
);
1110 /* Store the (key, value) pair in the environment. */
1111 muscle_insert (muscle_key
, muscle_value
);
1116 /*---------------------------------.
1117 | Parse a double quoted parameter. |
1118 `---------------------------------*/
1121 parse_dquoted_param (const char *from
)
1123 struct obstack param_obstack
;
1124 const char *param
= NULL
;
1127 obstack_init (¶m_obstack
);
1128 c
= skip_white_space ();
1132 complain (_("invalid %s declaration"), from
);
1138 while ((c
= literalchar ()) != '"')
1139 obstack_1grow (¶m_obstack
, c
);
1141 obstack_1grow (¶m_obstack
, '\0');
1142 param
= obstack_finish (¶m_obstack
);
1144 if (c
!= '"' || strlen (param
) == 0)
1146 complain (_("invalid %s declaration"), from
);
1156 /*----------------------------------.
1157 | Parse what comes after %skeleton. |
1158 `----------------------------------*/
1161 parse_skel_decl (void)
1163 skeleton
= parse_dquoted_param ("%skeleton");
1166 /*----------------------------------------------------------------.
1167 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
1168 | any `%' declarations, and copy the contents of any `%{ ... %}' |
1169 | groups to PRE_PROLOGUE_OBSTACK or POST_PROLOGUE_OBSTACK. |
1170 `----------------------------------------------------------------*/
1173 read_declarations (void)
1177 int c
= skip_white_space ();
1181 token_t tok
= parse_percent_token ();
1185 case tok_two_percents
:
1188 case tok_percent_left_curly
:
1190 copy_definition (&pre_prologue_obstack
);
1192 copy_definition (&post_prologue_obstack
);
1196 parse_token_decl (token_sym
, nterm_sym
);
1200 parse_token_decl (nterm_sym
, token_sym
);
1208 parse_start_decl ();
1212 parse_union_decl ();
1216 parse_expect_decl ();
1220 parse_thong_decl ();
1224 parse_assoc_decl (left_assoc
);
1228 parse_assoc_decl (right_assoc
);
1232 parse_assoc_decl (non_assoc
);
1236 parse_muscle_decl ();
1254 complain (_("unrecognized: %s"), token_buffer
);
1259 fatal (_("no input grammar"));
1264 complain (_("unknown character: %s"), quote (buf
));
1270 /*------------------------------------------------------------------.
1271 | Assuming that a `{' has just been seen, copy everything up to the |
1272 | matching `}' into the actions file. RULE_LENGTH is the number of |
1273 | values in the current rule so far, which says where to find `$0' |
1274 | with respect to the top of the stack. |
1276 | This routine is used both for actions and guards. Only |
1277 | ACTION_OBSTACK is used, but this is fine, since we use only |
1278 | pointers to relevant portions inside this obstack. |
1279 `------------------------------------------------------------------*/
1282 parse_braces (symbol_list
*rule
, int rule_length
)
1290 while ((c
= getc (finput
)) != '}')
1294 obstack_1grow (&action_obstack
, c
);
1299 obstack_1grow (&action_obstack
, c
);
1305 copy_string (finput
, &action_obstack
, c
);
1309 copy_comment (finput
, &action_obstack
);
1313 copy_dollar (finput
, &action_obstack
, rule
, rule_length
);
1317 copy_at (finput
, &action_obstack
, rule_length
);
1321 fatal (_("unmatched %s"), "`{'");
1324 obstack_1grow (&action_obstack
, c
);
1327 /* Above loop exits when C is '}'. */
1329 obstack_1grow (&action_obstack
, c
);
1332 obstack_1grow (&action_obstack
, '\0');
1337 parse_action (symbol_list
*rule
, int rule_length
)
1339 rule
->action_line
= lineno
;
1340 parse_braces (rule
, rule_length
);
1341 rule
->action
= obstack_finish (&action_obstack
);
1346 parse_guard (symbol_list
*rule
, int rule_length
)
1349 if (t
!= tok_left_curly
)
1350 complain (_("invalid %s declaration"), "%guard");
1351 rule
->guard_line
= lineno
;
1352 parse_braces (rule
, rule_length
);
1353 rule
->guard
= obstack_finish (&action_obstack
);
1358 /*-------------------------------------------------------------------.
1359 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1360 | with the user's names. |
1361 `-------------------------------------------------------------------*/
1366 /* Incremented for each generated symbol */
1367 static int gensym_count
= 0;
1368 static char buf
[256];
1372 sprintf (buf
, "@%d", ++gensym_count
);
1374 sym
= getsym (token_buffer
);
1375 sym
->class = nterm_sym
;
1376 sym
->number
= nvars
++;
1380 /*-------------------------------------------------------------------.
1381 | Parse the input grammar into a one symbol_list structure. Each |
1382 | rule is represented by a sequence of symbols: the left hand side |
1383 | followed by the contents of the right hand side, followed by a |
1384 | null pointer instead of a symbol to terminate the rule. The next |
1385 | symbol is the lhs of the following rule. |
1387 | All guards and actions are copied out to the appropriate files, |
1388 | labelled by the rule number they apply to. |
1390 | Bison used to allow some %directives in the rules sections, but |
1391 | this is no longer consider appropriate: (i) the documented grammar |
1392 | doesn't claim it, (ii), it would promote bad style, (iii), error |
1393 | recovery for %directives consists in skipping the junk until a `%' |
1394 | is seen and helrp synchronizing. This scheme is definitely wrong |
1395 | in the rules section. |
1396 `-------------------------------------------------------------------*/
1402 symbol_t
*lhs
= NULL
;
1403 symbol_list
*p
= NULL
;
1404 symbol_list
*p1
= NULL
;
1406 /* Points to first symbol_list of current rule. its symbol is the
1408 symbol_list
*crule
= NULL
;
1409 /* Points to the symbol_list preceding crule. */
1410 symbol_list
*crule1
= NULL
;
1414 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1415 if (t
== tok_identifier
|| t
== tok_bar
)
1417 int action_flag
= 0;
1418 /* Number of symbols in rhs of this rule so far */
1420 int xactions
= 0; /* JF for error checking */
1421 symbol_t
*first_rhs
= 0;
1423 if (t
== tok_identifier
)
1436 complain (_("ill-formed rule: initial symbol not followed by colon"));
1441 if (nrules
== 0 && t
== tok_bar
)
1443 complain (_("grammar starts with vertical bar"));
1444 lhs
= symval
; /* BOGUS: use a random symval */
1446 /* start a new rule and record its lhs. */
1451 p
= symbol_list_new (lhs
);
1462 /* mark the rule's lhs as a nonterminal if not already so. */
1464 if (lhs
->class == unknown_sym
)
1466 lhs
->class = nterm_sym
;
1467 lhs
->number
= nvars
;
1470 else if (lhs
->class == token_sym
)
1471 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1473 /* read the rhs of the rule. */
1481 crule
->ruleprec
= symval
;
1485 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1488 /* If next token is an identifier, see if a colon follows it.
1489 If one does, exit this rule now. */
1490 if (t
== tok_identifier
)
1499 if (t1
== tok_colon
)
1501 warn (_("previous rule lacks an ending `;'"));
1505 if (!first_rhs
) /* JF */
1507 /* Not followed by colon =>
1508 process as part of this rule's rhs. */
1511 /* If we just passed an action, that action was in the middle
1512 of a rule, so make a dummy rule to reduce it to a
1516 /* Since the action was written out with this rule's
1517 number, we must give the new rule this number by
1518 inserting the new rule before it. */
1520 /* Make a dummy nonterminal, a gensym. */
1521 symbol_t
*sdummy
= gensym ();
1523 /* Make a new rule, whose body is empty, before the
1524 current one, so that the action just read can
1528 p
= symbol_list_new (sdummy
);
1529 /* Attach its lineno to that of the host rule. */
1530 p
->line
= crule
->line
;
1531 /* Move the action from the host rule to this one. */
1532 p
->action
= crule
->action
;
1533 p
->action_line
= crule
->action_line
;
1534 crule
->action
= NULL
;
1540 /* End of the rule. */
1541 crule1
= symbol_list_new (NULL
);
1542 crule1
->next
= crule
;
1546 /* Insert the dummy generated by that rule into this
1549 p
= symbol_list_new (sdummy
);
1556 if (t
== tok_identifier
)
1559 p
= symbol_list_new (symval
);
1563 else /* handle an action. */
1565 parse_action (crule
, rulelength
);
1567 ++xactions
; /* JF */
1570 } /* end of read rhs of rule */
1572 /* Put an empty link in the list to mark the end of this rule */
1573 p
= symbol_list_new (NULL
);
1579 complain (_("two @prec's in a row"));
1581 crule
->ruleprec
= symval
;
1587 if (!semantic_parser
)
1588 complain (_("%%guard present but %%semantic_parser not specified"));
1590 parse_guard (crule
, rulelength
);
1594 if (t
== tok_left_curly
)
1596 /* This case never occurs -wjh */
1598 complain (_("two actions at end of one rule"));
1599 parse_action (crule
, rulelength
);
1601 ++xactions
; /* -wjh */
1604 /* If $$ is being set in default way, report if any type
1607 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1609 if (lhs
->type_name
== 0
1610 || first_rhs
->type_name
== 0
1611 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1612 complain (_("type clash (`%s' `%s') on default action"),
1613 lhs
->type_name
? lhs
->type_name
: "",
1614 first_rhs
->type_name
? first_rhs
->type_name
: "");
1616 /* Warn if there is no default for $$ but we need one. */
1617 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1618 complain (_("empty rule for typed nonterminal, and no action"));
1619 if (t
== tok_two_percents
|| t
== tok_eof
)
1620 warn (_("previous rule lacks an ending `;'"));
1621 if (t
== tok_semicolon
)
1626 complain (_("invalid input: %s"), quote (token_buffer
));
1630 /* grammar has been read. Do some checking */
1633 fatal (_("no rules in the input grammar"));
1635 /* Report any undefined symbols and consider them nonterminals. */
1636 symbols_do (symbol_check_defined
, NULL
);
1638 /* Insert the initial rule, which line is that of the first rule
1639 (not that of the start symbol):
1641 axiom: %start EOF. */
1642 p
= symbol_list_new (axiom
);
1643 p
->line
= grammar
->line
;
1644 p
->next
= symbol_list_new (startval
);
1645 p
->next
->next
= symbol_list_new (eoftoken
);
1646 p
->next
->next
->next
= symbol_list_new (NULL
);
1647 p
->next
->next
->next
->next
= grammar
;
1653 if (nsyms
> SHRT_MAX
)
1654 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1657 assert (nsyms
== ntokens
+ nvars
);
1660 /* At the end of the grammar file, some C source code must
1661 be stored. It is going to be associated to the epilogue
1664 read_additionnal_code (void)
1667 struct obstack el_obstack
;
1669 obstack_init (&el_obstack
);
1673 obstack_fgrow2 (&el_obstack
, muscle_find ("linef"),
1674 lineno
, quotearg_style (c_quoting_style
,
1675 muscle_find ("filename")));
1678 while ((c
= getc (finput
)) != EOF
)
1679 copy_character (&el_obstack
, c
);
1681 obstack_1grow (&el_obstack
, 0);
1682 muscle_insert ("epilogue", obstack_finish (&el_obstack
));
1686 /*------------------------------------------------------------------.
1687 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
1689 `------------------------------------------------------------------*/
1692 token_translations_init (void)
1694 int num_256_available_p
= TRUE
;
1697 /* Find the highest user token number, and whether 256, the POSIX
1698 preferred user token number for the error token, is used. */
1699 max_user_token_number
= 0;
1700 for (i
= 0; i
< ntokens
; ++i
)
1702 symbol_t
*this = symbols
[i
];
1703 if (this->user_token_number
!= SUNDEF
)
1705 if (this->user_token_number
> max_user_token_number
)
1706 max_user_token_number
= this->user_token_number
;
1707 if (this->user_token_number
== 256)
1708 num_256_available_p
= FALSE
;
1712 /* If 256 is not used, assign it to error, to follow POSIX. */
1713 if (num_256_available_p
&& errtoken
->user_token_number
== SUNDEF
)
1714 errtoken
->user_token_number
= 256;
1716 /* Set the missing user numbers. */
1717 if (max_user_token_number
< 256)
1718 max_user_token_number
= 256;
1720 for (i
= 0; i
< ntokens
; ++i
)
1722 symbol_t
*this = symbols
[i
];
1723 if (this->user_token_number
== SUNDEF
)
1724 this->user_token_number
= ++max_user_token_number
;
1725 if (this->user_token_number
> max_user_token_number
)
1726 max_user_token_number
= this->user_token_number
;
1729 token_translations
= XCALLOC (token_number_t
, max_user_token_number
+ 1);
1731 /* Initialize all entries for literal tokens to 2, the internal
1732 token number for $undefined., which represents all invalid
1734 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
1735 token_translations
[i
] = undeftoken
->number
;
1736 symbols_do (symbol_translation
, NULL
);
1740 /*----------------------------------------------------------------.
1741 | Assign symbol numbers, and write definition of token names into |
1742 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
1743 `----------------------------------------------------------------*/
1748 symbols
= XCALLOC (symbol_t
*, nsyms
);
1750 symbols_do (symbol_check_alias_consistence
, NULL
);
1751 symbols_do (symbol_pack
, NULL
);
1753 token_translations_init ();
1755 if (startval
->class == unknown_sym
)
1756 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1757 else if (startval
->class == token_sym
)
1758 fatal (_("the start symbol %s is a token"), startval
->tag
);
1760 start_symbol
= startval
->number
;
1764 /*---------------------------------------------------------------.
1765 | Convert the rules into the representation using RRHS, RLHS and |
1767 `---------------------------------------------------------------*/
1776 ritem
= XCALLOC (item_number_t
, nritems
+ 1);
1777 rules
= XCALLOC (rule_t
, nrules
) - 1;
1785 symbol_t
*ruleprec
= p
->ruleprec
;
1786 rules
[ruleno
].user_number
= ruleno
;
1787 rules
[ruleno
].number
= ruleno
;
1788 rules
[ruleno
].lhs
= p
->sym
;
1789 rules
[ruleno
].rhs
= ritem
+ itemno
;
1790 rules
[ruleno
].line
= p
->line
;
1791 rules
[ruleno
].useful
= TRUE
;
1792 rules
[ruleno
].action
= p
->action
;
1793 rules
[ruleno
].action_line
= p
->action_line
;
1794 rules
[ruleno
].guard
= p
->guard
;
1795 rules
[ruleno
].guard_line
= p
->guard_line
;
1800 /* item_number_t = token_number_t.
1801 But the former needs to contain more: negative rule numbers. */
1802 ritem
[itemno
++] = token_number_as_item_number (p
->sym
->number
);
1803 /* A rule gets by default the precedence and associativity
1804 of the last token in it. */
1805 if (p
->sym
->class == token_sym
)
1806 rules
[ruleno
].prec
= p
->sym
;
1811 /* If this rule has a %prec,
1812 the specified symbol's precedence replaces the default. */
1815 rules
[ruleno
].precsym
= ruleprec
;
1816 rules
[ruleno
].prec
= ruleprec
;
1818 ritem
[itemno
++] = -ruleno
;
1826 assert (itemno
== nritems
);
1829 ritem_print (stderr
);
1832 /*-------------------------------------------------------------------.
1833 | Read in the grammar specification and record it in the format |
1834 | described in gram.h. All guards are copied into the GUARD_OBSTACK |
1835 | and all actions into ACTION_OBSTACK, in each case forming the body |
1836 | of a C function (YYGUARD or YYACTION) which contains a switch |
1837 | statement to decide which guard or action to execute. |
1838 `-------------------------------------------------------------------*/
1846 /* Initialize the muscle obstack. */
1847 obstack_init (&muscle_obstack
);
1849 /* Initialize the symbol table. */
1852 /* Construct the axiom symbol. */
1853 axiom
= getsym ("$axiom");
1854 axiom
->class = nterm_sym
;
1855 axiom
->number
= nvars
++;
1857 /* Construct the error token */
1858 errtoken
= getsym ("error");
1859 errtoken
->class = token_sym
;
1860 errtoken
->number
= ntokens
++;
1862 /* Construct a token that represents all undefined literal tokens.
1863 It is always token number 2. */
1864 undeftoken
= getsym ("$undefined.");
1865 undeftoken
->class = token_sym
;
1866 undeftoken
->number
= ntokens
++;
1868 /* Initialize the obstacks. */
1869 obstack_init (&action_obstack
);
1870 obstack_init (&output_obstack
);
1871 obstack_init (&pre_prologue_obstack
);
1872 obstack_init (&post_prologue_obstack
);
1874 finput
= xfopen (infile
, "r");
1876 /* Read the declaration section. Copy %{ ... %} groups to
1877 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1878 etc. found there. */
1879 read_declarations ();
1881 /* If the user did not define her EOFTOKEN, do it now. */
1884 eoftoken
= getsym ("$");
1885 eoftoken
->class = token_sym
;
1886 eoftoken
->number
= 0;
1887 /* Value specified by POSIX. */
1888 eoftoken
->user_token_number
= 0;
1891 /* Read in the grammar, build grammar in list form. Write out
1892 guards and actions. */
1894 /* Some C code is given at the end of the grammar file. */
1895 read_additionnal_code ();
1900 /* Assign the symbols their symbol numbers. Write #defines for the
1901 token symbols into FDEFINES if requested. */
1904 /* Convert the grammar into the format described in gram.h. */
1907 /* The grammar as a symbol_list is no longer needed. */
1908 LIST_FREE (symbol_list
, grammar
);
1916 /* Free the symbol table data structure. */