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. STACK_OFFSET is the number of values |
498 | in 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 stack_offset
)
510 obstack_sgrow (oout
, "yyloc");
513 else if (isdigit (c
) || c
== '-')
518 n
= read_signed_integer (fin
);
519 if (n
> stack_offset
)
520 complain (_("invalid value: %s%d"), "@", n
);
523 /* Offset is always 0 if parser has already popped the stack
525 obstack_fgrow1 (oout
, "yylsp[%d]",
526 n
- (semantic_parser
? 0 : stack_offset
));
534 complain (_("%s is invalid"), quote (buf
));
539 /*-------------------------------------------------------------------.
540 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
542 | Possible inputs: $[<TYPENAME>]($|integer) |
544 | Output to OOUT a reference to this semantic value. STACK_OFFSET is |
545 | the number of values in the current rule so far, which says where |
546 | to find `$0' with respect to the top of the stack. |
547 `-------------------------------------------------------------------*/
550 copy_dollar (FILE *fin
, struct obstack
*oout
,
551 symbol_list
*rule
, int stack_offset
)
554 const char *type_name
= NULL
;
556 /* Get the type name if explicit. */
559 read_type_name (fin
);
560 type_name
= token_buffer
;
561 value_components_used
= 1;
567 obstack_sgrow (oout
, "yyval");
570 type_name
= get_type_name (0, rule
);
572 obstack_fgrow1 (oout
, ".%s", type_name
);
573 if (!type_name
&& typed
)
574 complain (_("$$ of `%s' has no declared type"),
577 else if (isdigit (c
) || c
== '-')
581 n
= read_signed_integer (fin
);
583 if (n
> stack_offset
)
584 complain (_("invalid value: %s%d"), "$", n
);
587 if (!type_name
&& n
> 0)
588 type_name
= get_type_name (n
, rule
);
590 /* Offset is always 0 if parser has already popped the stack
592 obstack_fgrow1 (oout
, "yyvsp[%d]",
593 n
- (semantic_parser
? 0 : stack_offset
));
596 obstack_fgrow1 (oout
, ".%s", type_name
);
597 if (!type_name
&& typed
)
598 complain (_("$%d of `%s' has no declared type"),
606 complain (_("%s is invalid"), quote (buf
));
610 /*-------------------------------------------------------------------.
611 | Copy the contents of a `%{ ... %}' into the definitions file. The |
612 | `%{' has already been read. Return after reading the `%}'. |
613 `-------------------------------------------------------------------*/
616 copy_definition (struct obstack
*oout
)
619 /* -1 while reading a character if prev char was %. */
624 obstack_fgrow2 (oout
, muscle_find ("linef"),
625 lineno
, quotearg_style (c_quoting_style
,
626 muscle_find ("filename")));
638 obstack_1grow (oout
, c
);
648 copy_string (finput
, oout
, c
);
652 copy_comment (finput
, oout
);
656 fatal ("%s", _("unterminated `%{' definition"));
659 copy_character (oout
, c
);
668 obstack_1grow (oout
, '%');
675 /*-------------------------------------------------------------------.
676 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
677 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
679 `-------------------------------------------------------------------*/
682 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
684 token_t token
= tok_undef
;
685 char *typename
= NULL
;
687 /* The symbol being defined. */
688 symbol_t
*symbol
= NULL
;
690 /* After `%token' and `%nterm', any number of symbols maybe be
694 int tmp_char
= ungetc (skip_white_space (), finput
);
696 /* `%' (for instance from `%token', or from `%%' etc.) is the
697 only valid means to end this declaration. */
701 fatal (_("Premature EOF after %s"), token_buffer
);
704 if (token
== tok_comma
)
709 if (token
== tok_typename
)
711 typename
= xstrdup (token_buffer
);
712 value_components_used
= 1;
715 else if (token
== tok_identifier
&& *symval
->tag
== '\"' && symbol
)
717 symbol_make_alias (symbol
, typename
);
720 else if (token
== tok_identifier
)
722 int oldclass
= symval
->class;
725 if (symbol
->class == what_is_not
)
726 complain (_("symbol %s redefined"), symbol
->tag
);
727 symbol
->class = what_is
;
728 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
729 symbol
->number
= nvars
++;
730 if (what_is
== token_sym
&& symbol
->number
== NUMBER_UNDEFINED
)
731 symbol
->number
= ntokens
++;
735 if (symbol
->type_name
== NULL
)
736 symbol
->type_name
= typename
;
737 else if (strcmp (typename
, symbol
->type_name
) != 0)
738 complain (_("type redeclaration for %s"), symbol
->tag
);
741 else if (symbol
&& token
== tok_number
)
743 symbol
->user_token_number
= numval
;
744 /* User defined EOF token? */
748 eoftoken
->number
= 0;
749 /* It is always mapped to 0, so it was already counted in
756 complain (_("`%s' is invalid in %s"),
758 (what_is
== token_sym
) ? "%token" : "%nterm");
766 /*------------------------------.
767 | Parse what comes after %start |
768 `------------------------------*/
771 parse_start_decl (void)
774 complain (_("multiple %s declarations"), "%start");
775 if (lex () != tok_identifier
)
776 complain (_("invalid %s declaration"), "%start");
784 /*-----------------------------------------------------------.
785 | read in a %type declaration and record its information for |
786 | get_type_name to access |
787 `-----------------------------------------------------------*/
790 parse_type_decl (void)
794 if (lex () != tok_typename
)
796 complain ("%s", _("%type declaration has no <typename>"));
801 name
= xstrdup (token_buffer
);
806 int tmp_char
= ungetc (skip_white_space (), finput
);
811 fatal (_("Premature EOF after %s"), token_buffer
);
823 if (symval
->type_name
== NULL
)
824 symval
->type_name
= name
;
825 else if (strcmp (name
, symval
->type_name
) != 0)
826 complain (_("type redeclaration for %s"), symval
->tag
);
831 complain (_("invalid %%type declaration due to item: %s"),
840 /*----------------------------------------------------------------.
841 | Read in a %left, %right or %nonassoc declaration and record its |
843 `----------------------------------------------------------------*/
846 parse_assoc_decl (associativity assoc
)
851 /* Assign a new precedence level, never 0. */
857 int tmp_char
= ungetc (skip_white_space (), finput
);
862 fatal (_("Premature EOF after %s"), token_buffer
);
869 name
= xstrdup (token_buffer
);
876 if (symval
->prec
!= 0)
877 complain (_("redefining precedence of %s"), symval
->tag
);
878 symval
->prec
= lastprec
;
879 symval
->assoc
= assoc
;
880 if (symval
->class == nterm_sym
)
881 complain (_("symbol %s redefined"), symval
->tag
);
882 if (symval
->number
== NUMBER_UNDEFINED
)
884 symval
->number
= ntokens
++;
885 symval
->class = token_sym
;
888 { /* record the type, if one is specified */
889 if (symval
->type_name
== NULL
)
890 symval
->type_name
= name
;
891 else if (strcmp (name
, symval
->type_name
) != 0)
892 complain (_("type redeclaration for %s"), symval
->tag
);
897 if (prev
== tok_identifier
)
899 symval
->user_token_number
= numval
;
904 (_("invalid text (%s) - number should be after identifier"),
914 complain (_("unexpected item: %s"), token_buffer
);
924 /*--------------------------------------------------------------.
925 | Copy the union declaration into the stype muscle |
926 | (and fdefines), where it is made into the definition of |
927 | YYSTYPE, the type of elements of the parser value stack. |
928 `--------------------------------------------------------------*/
931 parse_union_decl (void)
936 struct obstack union_obstack
;
938 complain (_("multiple %s declarations"), "%union");
942 MUSCLE_INSERT_INT ("stype_line", lineno
);
943 obstack_init (&union_obstack
);
944 obstack_sgrow (&union_obstack
, "union");
950 /* If C contains '/', it is output by copy_comment (). */
952 obstack_1grow (&union_obstack
, c
);
961 copy_comment (finput
, &union_obstack
);
969 /* FIXME: Errr. How could this happen???. --akim */
971 complain (_("unmatched %s"), "`}'");
979 /* JF don't choke on trailing semi */
980 c
= skip_white_space ();
983 obstack_1grow (&union_obstack
, 0);
984 muscle_insert ("stype", obstack_finish (&union_obstack
));
988 /*-------------------------------------------------------.
989 | Parse the declaration %expect N which says to expect N |
990 | shift-reduce conflicts. |
991 `-------------------------------------------------------*/
994 parse_expect_decl (void)
996 int c
= skip_white_space ();
1000 complain (_("argument of %%expect is not an integer"));
1002 expected_conflicts
= read_signed_integer (finput
);
1006 /*-------------------------------------------------------------------.
1007 | Parse what comes after %thong. the full syntax is |
1009 | %thong <type> token number literal |
1011 | the <type> or number may be omitted. The number specifies the |
1012 | user_token_number. |
1014 | Two symbols are entered in the table, one for the token symbol and |
1015 | one for the literal. Both are given the <type>, if any, from the |
1016 | declaration. The ->user_token_number of the first is SALIAS and |
1017 | the ->user_token_number of the second is set to the number, if |
1018 | any, from the declaration. The two symbols are linked via |
1019 | pointers in their ->alias fields. |
1021 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
1022 | only the literal string is retained it is the literal string that |
1023 | is output to yytname |
1024 `-------------------------------------------------------------------*/
1027 parse_thong_decl (void)
1032 int usrtoknum
= SUNDEF
;
1034 token
= lex (); /* fetch typename or first token */
1035 if (token
== tok_typename
)
1037 typename
= xstrdup (token_buffer
);
1038 value_components_used
= 1;
1039 token
= lex (); /* fetch first token */
1042 /* process first token */
1044 if (token
!= tok_identifier
)
1046 complain (_("unrecognized item %s, expected an identifier"),
1051 symval
->class = token_sym
;
1052 symval
->type_name
= typename
;
1053 symval
->user_token_number
= SALIAS
;
1056 token
= lex (); /* get number or literal string */
1058 if (token
== tok_number
)
1061 token
= lex (); /* okay, did number, now get literal */
1064 /* process literal string token */
1066 if (token
!= tok_identifier
|| *symval
->tag
!= '\"')
1068 complain (_("expected string constant instead of %s"), token_buffer
);
1072 symval
->class = token_sym
;
1073 symval
->type_name
= typename
;
1074 symval
->user_token_number
= usrtoknum
;
1076 symval
->alias
= symbol
;
1077 symbol
->alias
= symval
;
1079 /* symbol and symval combined are only one symbol. */
1085 parse_muscle_decl (void)
1087 int ch
= ungetc (skip_white_space (), finput
);
1092 if (!isalpha (ch
) && ch
!= '_')
1094 complain (_("invalid %s declaration"), "%define");
1098 copy_identifier (finput
, &muscle_obstack
);
1099 obstack_1grow (&muscle_obstack
, 0);
1100 muscle_key
= obstack_finish (&muscle_obstack
);
1103 ch
= skip_white_space ();
1106 ungetc (ch
, finput
);
1109 complain (_("invalid %s declaration"), "%define");
1114 fatal (_("Premature EOF after %s"), "\"");
1116 copy_string2 (finput
, &muscle_obstack
, '"', 0);
1117 obstack_1grow (&muscle_obstack
, 0);
1118 muscle_value
= obstack_finish (&muscle_obstack
);
1120 /* Store the (key, value) pair in the environment. */
1121 muscle_insert (muscle_key
, muscle_value
);
1126 /*---------------------------------.
1127 | Parse a double quoted parameter. |
1128 `---------------------------------*/
1131 parse_dquoted_param (const char *from
)
1133 struct obstack param_obstack
;
1134 const char *param
= NULL
;
1137 obstack_init (¶m_obstack
);
1138 c
= skip_white_space ();
1142 complain (_("invalid %s declaration"), from
);
1148 while ((c
= literalchar ()) != '"')
1149 obstack_1grow (¶m_obstack
, c
);
1151 obstack_1grow (¶m_obstack
, '\0');
1152 param
= obstack_finish (¶m_obstack
);
1154 if (c
!= '"' || strlen (param
) == 0)
1156 complain (_("invalid %s declaration"), from
);
1166 /*----------------------------------.
1167 | Parse what comes after %skeleton. |
1168 `----------------------------------*/
1171 parse_skel_decl (void)
1173 skeleton
= parse_dquoted_param ("%skeleton");
1176 /*----------------------------------------------------------------.
1177 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
1178 | any `%' declarations, and copy the contents of any `%{ ... %}' |
1179 | groups to PRE_PROLOGUE_OBSTACK or POST_PROLOGUE_OBSTACK. |
1180 `----------------------------------------------------------------*/
1183 read_declarations (void)
1187 int c
= skip_white_space ();
1191 token_t tok
= parse_percent_token ();
1195 case tok_two_percents
:
1198 case tok_percent_left_curly
:
1200 copy_definition (&pre_prologue_obstack
);
1202 copy_definition (&post_prologue_obstack
);
1206 parse_token_decl (token_sym
, nterm_sym
);
1210 parse_token_decl (nterm_sym
, token_sym
);
1218 parse_start_decl ();
1222 parse_union_decl ();
1226 parse_expect_decl ();
1230 parse_thong_decl ();
1234 parse_assoc_decl (left_assoc
);
1238 parse_assoc_decl (right_assoc
);
1242 parse_assoc_decl (non_assoc
);
1246 parse_muscle_decl ();
1264 complain (_("unrecognized: %s"), token_buffer
);
1269 fatal (_("no input grammar"));
1274 complain (_("unknown character: %s"), quote (buf
));
1280 /*-------------------------------------------------------------------.
1281 | Assuming that a `{' has just been seen, copy everything up to the |
1282 | matching `}' into the actions file. STACK_OFFSET is the number of |
1283 | values in the current rule so far, which says where to find `$0' |
1284 | with respect to the top of the stack. |
1286 | This routine is used both for actions and guards. Only |
1287 | ACTION_OBSTACK is used, but this is fine, since we use only |
1288 | pointers to relevant portions inside this obstack. |
1289 `-------------------------------------------------------------------*/
1292 parse_braces (symbol_list
*rule
, int stack_offset
)
1300 while ((c
= getc (finput
)) != '}')
1304 obstack_1grow (&action_obstack
, c
);
1309 obstack_1grow (&action_obstack
, c
);
1315 copy_string (finput
, &action_obstack
, c
);
1319 copy_comment (finput
, &action_obstack
);
1323 copy_dollar (finput
, &action_obstack
,
1324 rule
, stack_offset
);
1328 copy_at (finput
, &action_obstack
,
1333 fatal (_("unmatched %s"), "`{'");
1336 obstack_1grow (&action_obstack
, c
);
1339 /* Above loop exits when C is '}'. */
1341 obstack_1grow (&action_obstack
, c
);
1344 obstack_1grow (&action_obstack
, '\0');
1349 parse_action (symbol_list
*rule
, int stack_offset
)
1351 rule
->action_line
= lineno
;
1352 parse_braces (rule
, stack_offset
);
1353 rule
->action
= obstack_finish (&action_obstack
);
1358 parse_guard (symbol_list
*rule
, int stack_offset
)
1361 if (t
!= tok_left_curly
)
1362 complain (_("invalid %s declaration"), "%guard");
1363 rule
->guard_line
= lineno
;
1364 parse_braces (rule
, stack_offset
);
1365 rule
->guard
= obstack_finish (&action_obstack
);
1370 /*-------------------------------------------------------------------.
1371 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1372 | with the user's names. |
1373 `-------------------------------------------------------------------*/
1378 /* Incremented for each generated symbol */
1379 static int gensym_count
= 0;
1380 static char buf
[256];
1384 sprintf (buf
, "@%d", ++gensym_count
);
1386 sym
= getsym (token_buffer
);
1387 sym
->class = nterm_sym
;
1388 sym
->number
= nvars
++;
1392 /*-------------------------------------------------------------------.
1393 | Parse the input grammar into a one symbol_list structure. Each |
1394 | rule is represented by a sequence of symbols: the left hand side |
1395 | followed by the contents of the right hand side, followed by a |
1396 | null pointer instead of a symbol to terminate the rule. The next |
1397 | symbol is the lhs of the following rule. |
1399 | All guards and actions are copied out to the appropriate files, |
1400 | labelled by the rule number they apply to. |
1402 | Bison used to allow some %directives in the rules sections, but |
1403 | this is no longer consider appropriate: (i) the documented grammar |
1404 | doesn't claim it, (ii), it would promote bad style, (iii), error |
1405 | recovery for %directives consists in skipping the junk until a `%' |
1406 | is seen and helrp synchronizing. This scheme is definitely wrong |
1407 | in the rules section. |
1408 `-------------------------------------------------------------------*/
1414 symbol_t
*lhs
= NULL
;
1415 symbol_list
*p
= NULL
;
1416 symbol_list
*p1
= NULL
;
1418 /* Points to first symbol_list of current rule. its symbol is the
1420 symbol_list
*crule
= NULL
;
1421 /* Points to the symbol_list preceding crule. */
1422 symbol_list
*crule1
= NULL
;
1426 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1427 if (t
== tok_identifier
|| t
== tok_bar
)
1429 int action_flag
= 0;
1430 /* Number of symbols in rhs of this rule so far */
1432 int xactions
= 0; /* JF for error checking */
1433 symbol_t
*first_rhs
= 0;
1435 if (t
== tok_identifier
)
1448 complain (_("ill-formed rule: initial symbol not followed by colon"));
1453 if (nrules
== 0 && t
== tok_bar
)
1455 complain (_("grammar starts with vertical bar"));
1456 lhs
= symval
; /* BOGUS: use a random symval */
1458 /* start a new rule and record its lhs. */
1463 p
= symbol_list_new (lhs
);
1474 /* mark the rule's lhs as a nonterminal if not already so. */
1476 if (lhs
->class == unknown_sym
)
1478 lhs
->class = nterm_sym
;
1479 lhs
->number
= nvars
;
1482 else if (lhs
->class == token_sym
)
1483 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1485 /* read the rhs of the rule. */
1493 crule
->ruleprec
= symval
;
1497 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1500 /* If next token is an identifier, see if a colon follows it.
1501 If one does, exit this rule now. */
1502 if (t
== tok_identifier
)
1511 if (t1
== tok_colon
)
1513 warn (_("previous rule lacks an ending `;'"));
1517 if (!first_rhs
) /* JF */
1519 /* Not followed by colon =>
1520 process as part of this rule's rhs. */
1523 /* If we just passed an action, that action was in the middle
1524 of a rule, so make a dummy rule to reduce it to a
1528 /* Since the action was written out with this rule's
1529 number, we must give the new rule this number by
1530 inserting the new rule before it. */
1532 /* Make a dummy nonterminal, a gensym. */
1533 symbol_t
*sdummy
= gensym ();
1535 /* Make a new rule, whose body is empty, before the
1536 current one, so that the action just read can
1540 p
= symbol_list_new (sdummy
);
1541 /* Attach its lineno to that of the host rule. */
1542 p
->line
= crule
->line
;
1543 /* Move the action from the host rule to this one. */
1544 p
->action
= crule
->action
;
1545 p
->action_line
= crule
->action_line
;
1546 crule
->action
= NULL
;
1552 /* End of the rule. */
1553 crule1
= symbol_list_new (NULL
);
1554 crule1
->next
= crule
;
1558 /* Insert the dummy generated by that rule into this
1561 p
= symbol_list_new (sdummy
);
1568 if (t
== tok_identifier
)
1571 p
= symbol_list_new (symval
);
1575 else /* handle an action. */
1577 parse_action (crule
, rulelength
);
1579 ++xactions
; /* JF */
1582 } /* end of read rhs of rule */
1584 /* Put an empty link in the list to mark the end of this rule */
1585 p
= symbol_list_new (NULL
);
1591 complain (_("two @prec's in a row"));
1593 crule
->ruleprec
= symval
;
1599 if (!semantic_parser
)
1600 complain (_("%%guard present but %%semantic_parser not specified"));
1602 parse_guard (crule
, rulelength
);
1606 if (t
== tok_left_curly
)
1608 /* This case never occurs -wjh */
1610 complain (_("two actions at end of one rule"));
1611 parse_action (crule
, rulelength
);
1613 ++xactions
; /* -wjh */
1616 /* If $$ is being set in default way, report if any type
1619 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1621 if (lhs
->type_name
== 0
1622 || first_rhs
->type_name
== 0
1623 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1624 complain (_("type clash (`%s' `%s') on default action"),
1625 lhs
->type_name
? lhs
->type_name
: "",
1626 first_rhs
->type_name
? first_rhs
->type_name
: "");
1628 /* Warn if there is no default for $$ but we need one. */
1629 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1630 complain (_("empty rule for typed nonterminal, and no action"));
1631 if (t
== tok_two_percents
|| t
== tok_eof
)
1632 warn (_("previous rule lacks an ending `;'"));
1633 if (t
== tok_semicolon
)
1638 complain (_("invalid input: %s"), quote (token_buffer
));
1642 /* grammar has been read. Do some checking */
1645 fatal (_("no rules in the input grammar"));
1647 /* Report any undefined symbols and consider them nonterminals. */
1648 symbols_do (symbol_check_defined
, NULL
);
1650 /* Insert the initial rule, which line is that of the first rule
1651 (not that of the start symbol):
1653 axiom: %start EOF. */
1654 p
= symbol_list_new (axiom
);
1655 p
->line
= grammar
->line
;
1656 p
->next
= symbol_list_new (startval
);
1657 p
->next
->next
= symbol_list_new (eoftoken
);
1658 p
->next
->next
->next
= symbol_list_new (NULL
);
1659 p
->next
->next
->next
->next
= grammar
;
1665 if (nsyms
> SHRT_MAX
)
1666 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1669 assert (nsyms
== ntokens
+ nvars
);
1672 /* At the end of the grammar file, some C source code must
1673 be stored. It is going to be associated to the epilogue
1676 read_additionnal_code (void)
1679 struct obstack el_obstack
;
1681 obstack_init (&el_obstack
);
1685 obstack_fgrow2 (&el_obstack
, muscle_find ("linef"),
1686 lineno
, quotearg_style (c_quoting_style
,
1687 muscle_find ("filename")));
1690 while ((c
= getc (finput
)) != EOF
)
1691 copy_character (&el_obstack
, c
);
1693 obstack_1grow (&el_obstack
, 0);
1694 muscle_insert ("epilogue", obstack_finish (&el_obstack
));
1698 /*------------------------------------------------------------------.
1699 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
1701 `------------------------------------------------------------------*/
1704 token_translations_init (void)
1706 int num_256_available_p
= TRUE
;
1709 /* Find the highest user token number, and whether 256, the POSIX
1710 preferred user token number for the error token, is used. */
1711 max_user_token_number
= 0;
1712 for (i
= 0; i
< ntokens
; ++i
)
1714 symbol_t
*this = symbols
[i
];
1715 if (this->user_token_number
!= SUNDEF
)
1717 if (this->user_token_number
> max_user_token_number
)
1718 max_user_token_number
= this->user_token_number
;
1719 if (this->user_token_number
== 256)
1720 num_256_available_p
= FALSE
;
1724 /* If 256 is not used, assign it to error, to follow POSIX. */
1725 if (num_256_available_p
&& errtoken
->user_token_number
== SUNDEF
)
1726 errtoken
->user_token_number
= 256;
1728 /* Set the missing user numbers. */
1729 if (max_user_token_number
< 256)
1730 max_user_token_number
= 256;
1732 for (i
= 0; i
< ntokens
; ++i
)
1734 symbol_t
*this = symbols
[i
];
1735 if (this->user_token_number
== SUNDEF
)
1736 this->user_token_number
= ++max_user_token_number
;
1737 if (this->user_token_number
> max_user_token_number
)
1738 max_user_token_number
= this->user_token_number
;
1741 token_translations
= XCALLOC (token_number_t
, max_user_token_number
+ 1);
1743 /* Initialize all entries for literal tokens to 2, the internal
1744 token number for $undefined., which represents all invalid
1746 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
1747 token_translations
[i
] = undeftoken
->number
;
1748 symbols_do (symbol_translation
, NULL
);
1752 /*----------------------------------------------------------------.
1753 | Assign symbol numbers, and write definition of token names into |
1754 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
1755 `----------------------------------------------------------------*/
1760 symbols
= XCALLOC (symbol_t
*, nsyms
);
1762 symbols_do (symbol_check_alias_consistence
, NULL
);
1763 symbols_do (symbol_pack
, NULL
);
1765 token_translations_init ();
1767 if (startval
->class == unknown_sym
)
1768 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1769 else if (startval
->class == token_sym
)
1770 fatal (_("the start symbol %s is a token"), startval
->tag
);
1772 start_symbol
= startval
->number
;
1776 /*---------------------------------------------------------------.
1777 | Convert the rules into the representation using RRHS, RLHS and |
1779 `---------------------------------------------------------------*/
1788 ritem
= XCALLOC (item_number_t
, nritems
+ 1);
1789 rules
= XCALLOC (rule_t
, nrules
) - 1;
1797 symbol_t
*ruleprec
= p
->ruleprec
;
1798 rules
[ruleno
].user_number
= ruleno
;
1799 rules
[ruleno
].number
= ruleno
;
1800 rules
[ruleno
].lhs
= p
->sym
;
1801 rules
[ruleno
].rhs
= ritem
+ itemno
;
1802 rules
[ruleno
].line
= p
->line
;
1803 rules
[ruleno
].useful
= TRUE
;
1804 rules
[ruleno
].action
= p
->action
;
1805 rules
[ruleno
].action_line
= p
->action_line
;
1806 rules
[ruleno
].guard
= p
->guard
;
1807 rules
[ruleno
].guard_line
= p
->guard_line
;
1812 /* item_number_t = token_number_t.
1813 But the former needs to contain more: negative rule numbers. */
1814 ritem
[itemno
++] = token_number_as_item_number (p
->sym
->number
);
1815 /* A rule gets by default the precedence and associativity
1816 of the last token in it. */
1817 if (p
->sym
->class == token_sym
)
1818 rules
[ruleno
].prec
= p
->sym
;
1823 /* If this rule has a %prec,
1824 the specified symbol's precedence replaces the default. */
1827 rules
[ruleno
].precsym
= ruleprec
;
1828 rules
[ruleno
].prec
= ruleprec
;
1830 ritem
[itemno
++] = -ruleno
;
1838 assert (itemno
== nritems
);
1841 ritem_print (stderr
);
1844 /*-------------------------------------------------------------------.
1845 | Read in the grammar specification and record it in the format |
1846 | described in gram.h. All guards are copied into the GUARD_OBSTACK |
1847 | and all actions into ACTION_OBSTACK, in each case forming the body |
1848 | of a C function (YYGUARD or YYACTION) which contains a switch |
1849 | statement to decide which guard or action to execute. |
1850 `-------------------------------------------------------------------*/
1858 /* Initialize the muscle obstack. */
1859 obstack_init (&muscle_obstack
);
1861 /* Initialize the symbol table. */
1864 /* Construct the axiom symbol. */
1865 axiom
= getsym ("$axiom");
1866 axiom
->class = nterm_sym
;
1867 axiom
->number
= nvars
++;
1869 /* Construct the error token */
1870 errtoken
= getsym ("error");
1871 errtoken
->class = token_sym
;
1872 errtoken
->number
= ntokens
++;
1874 /* Construct a token that represents all undefined literal tokens.
1875 It is always token number 2. */
1876 undeftoken
= getsym ("$undefined.");
1877 undeftoken
->class = token_sym
;
1878 undeftoken
->number
= ntokens
++;
1880 /* Initialize the obstacks. */
1881 obstack_init (&action_obstack
);
1882 obstack_init (&output_obstack
);
1883 obstack_init (&pre_prologue_obstack
);
1884 obstack_init (&post_prologue_obstack
);
1886 finput
= xfopen (infile
, "r");
1888 /* Read the declaration section. Copy %{ ... %} groups to
1889 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1890 etc. found there. */
1891 read_declarations ();
1893 /* If the user did not define her EOFTOKEN, do it now. */
1896 eoftoken
= getsym ("$");
1897 eoftoken
->class = token_sym
;
1898 eoftoken
->number
= 0;
1899 /* Value specified by POSIX. */
1900 eoftoken
->user_token_number
= 0;
1903 /* Read in the grammar, build grammar in list form. Write out
1904 guards and actions. */
1906 /* Some C code is given at the end of the grammar file. */
1907 read_additionnal_code ();
1912 /* Assign the symbols their symbol numbers. Write #defines for the
1913 token symbols into FDEFINES if requested. */
1916 /* Convert the grammar into the format described in gram.h. */
1919 /* The grammar as a symbol_list is no longer needed. */
1920 LIST_FREE (symbol_list
, grammar
);
1928 /* Free the symbol table data structure. */