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. */
52 static symbol_list
*grammar
= NULL
;
53 static int start_flag
= 0;
54 static symbol_t
*startval
= NULL
;
56 /* Nonzero if components of semantic values are used, implying
57 they must be unions. */
58 static int value_components_used
;
60 /* Nonzero if %union has been seen. */
63 /* Incremented for each %left, %right or %nonassoc seen */
64 static int lastprec
= 0;
66 symbol_t
*errtoken
= NULL
;
67 symbol_t
*undeftoken
= NULL
;
68 symbol_t
*eoftoken
= NULL
;
69 symbol_t
*axiom
= NULL
;
72 symbol_list_new (symbol_t
*sym
)
74 symbol_list
*res
= XMALLOC (symbol_list
, 1);
84 /*------------------------.
85 | Operations on symbols. |
86 `------------------------*/
89 /*-----------------------------------------------------------.
90 | If THIS is not defined, report an error, and consider it a |
92 `-----------------------------------------------------------*/
95 symbol_check_defined (symbol_t
*this)
97 if (this->class == unknown_sym
)
100 (_("symbol %s is used, but is not defined as a token and has no rules"),
102 this->class = nterm_sym
;
103 this->number
= nvars
++;
110 /*-------------------------------------------------------------------.
111 | Assign a symbol number, and write the definition of the token name |
112 | into FDEFINES. Put in SYMBOLS. |
113 `-------------------------------------------------------------------*/
116 symbol_make_alias (symbol_t
*symbol
, char *typename
)
119 warn (_("symbol `%s' used more than once as a literal string"),
121 else if (symbol
->alias
)
122 warn (_("symbol `%s' given more than one literal string"),
126 symval
->class = token_sym
;
127 symval
->type_name
= typename
;
128 symval
->user_token_number
= symbol
->user_token_number
;
129 symbol
->user_token_number
= USER_NUMBER_ALIAS
;
130 symval
->alias
= symbol
;
131 symbol
->alias
= symval
;
132 /* symbol and symval combined are only one symbol */
135 assert (ntokens
== symbol
->number
|| ntokens
== symval
->number
);
136 symbol
->number
= symval
->number
=
137 (symval
->number
< symbol
->number
) ? symval
->number
: symbol
->number
;
143 /*---------------------------------------------------------.
144 | Check that THIS, and its alias, have same precedence and |
146 `---------------------------------------------------------*/
149 symbol_check_alias_consistence (symbol_t
*this)
151 /* Check only those who _are_ the aliases. */
152 if (this->alias
&& this->user_token_number
== USER_NUMBER_ALIAS
)
154 if (this->prec
!= this->alias
->prec
)
156 if (this->prec
!= 0 && this->alias
->prec
!= 0)
157 complain (_("conflicting precedences for %s and %s"),
158 this->tag
, this->alias
->tag
);
160 this->alias
->prec
= this->prec
;
162 this->prec
= this->alias
->prec
;
165 if (this->assoc
!= this->alias
->assoc
)
167 if (this->assoc
!= 0 && this->alias
->assoc
!= 0)
168 complain (_("conflicting assoc values for %s and %s"),
169 this->tag
, this->alias
->tag
);
170 if (this->assoc
!= 0)
171 this->alias
->assoc
= this->assoc
;
173 this->assoc
= this->alias
->assoc
;
180 /*-------------------------------------------------------------------.
181 | Assign a symbol number, and write the definition of the token name |
182 | into FDEFINES. Put in SYMBOLS. |
183 `-------------------------------------------------------------------*/
186 symbol_pack (symbol_t
*this)
188 if (this->class == nterm_sym
)
190 this->number
+= ntokens
;
192 else if (this->alias
)
194 /* This symbol and its alias are a single token defn.
195 Allocate a tokno, and assign to both check agreement of
196 prec and assoc fields and make both the same */
197 if (this->number
== NUMBER_UNDEFINED
)
199 if (this == eoftoken
|| this->alias
== eoftoken
)
200 this->number
= this->alias
->number
= 0;
203 assert (this->alias
->number
!= NUMBER_UNDEFINED
);
204 this->number
= this->alias
->number
;
207 /* Do not do processing below for USER_NUMBER_ALIASs. */
208 if (this->user_token_number
== USER_NUMBER_ALIAS
)
211 else /* this->class == token_sym */
213 assert (this->number
!= NUMBER_UNDEFINED
);
216 symbols
[this->number
] = this;
223 /*--------------------------------------------------.
224 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
225 `--------------------------------------------------*/
228 symbol_translation (symbol_t
*this)
231 if (this->class == token_sym
232 && this->user_token_number
!= USER_NUMBER_ALIAS
)
234 /* A token which translation has already been set? */
235 if (token_translations
[this->user_token_number
] != undeftoken
->number
)
236 complain (_("tokens %s and %s both assigned number %d"),
237 symbols
[token_translations
[this->user_token_number
]]->tag
,
238 this->tag
, this->user_token_number
);
240 token_translations
[this->user_token_number
] = this->number
;
247 /*===================\
248 | Low level lexing. |
249 \===================*/
252 skip_to_char (int target
)
256 complain (_(" Skipping to next \\n"));
258 complain (_(" Skipping to next %c"), target
);
261 c
= skip_white_space ();
262 while (c
!= target
&& c
!= EOF
);
268 /*---------------------------------------------------------.
269 | Read a signed integer from STREAM and return its value. |
270 `---------------------------------------------------------*/
273 read_signed_integer (FILE *stream
)
275 int c
= getc (stream
);
287 n
= 10 * n
+ (c
- '0');
296 /*--------------------------------------------------------------.
297 | Get the data type (alternative in the union) of the value for |
298 | symbol N in rule RULE. |
299 `--------------------------------------------------------------*/
302 get_type_name (int n
, symbol_list
*rule
)
309 complain (_("invalid $ value"));
319 if (rp
== NULL
|| rp
->sym
== NULL
)
321 complain (_("invalid $ value"));
327 return rp
->sym
->type_name
;
330 /*------------------------------------------------------------------.
331 | Copy the character C to OOUT, and insert quadigraphs when needed. |
332 `------------------------------------------------------------------*/
335 copy_character (struct obstack
*oout
, int c
)
340 obstack_sgrow (oout
, "@<:@");
344 obstack_sgrow (oout
, "@:>@");
348 obstack_1grow (oout
, c
);
352 /*------------------------------------------------------------.
353 | Dump the string from FIN to OOUT if non null. MATCH is the |
354 | delimiter of the string (either ' or "). |
355 `------------------------------------------------------------*/
358 copy_string2 (FILE *fin
, struct obstack
*oout
, int match
, int store
)
363 obstack_1grow (oout
, match
);
370 fatal (_("unterminated string at end of file"));
373 complain (_("unterminated string"));
375 c
= match
; /* invent terminator */
379 copy_character (oout
, c
);
385 fatal (_("unterminated string at end of file"));
386 copy_character (oout
, c
);
396 obstack_1grow (oout
, c
);
402 copy_string (FILE *fin
, struct obstack
*oout
, int match
)
404 copy_string2 (fin
, oout
, match
, 1);
410 copy_identifier (FILE *fin
, struct obstack
*oout
)
414 while (isalnum (c
= getc (fin
)) || c
== '_')
415 obstack_1grow (oout
, c
);
421 /*------------------------------------------------------------------.
422 | Dump the wannabee comment from IN to OOUT. In fact we just saw a |
423 | `/', which might or might not be a comment. In any case, copy |
425 `------------------------------------------------------------------*/
428 copy_comment (FILE *fin
, struct obstack
*oout
)
434 /* We read a `/', output it. */
435 obstack_1grow (oout
, '/');
437 switch ((c
= getc (fin
)))
450 obstack_1grow (oout
, c
);
456 if (!cplus_comment
&& c
== '*')
460 obstack_1grow (oout
, c
);
466 obstack_1grow (oout
, c
);
473 obstack_1grow (oout
, c
);
480 fatal (_("unterminated comment"));
483 copy_character (oout
, c
);
490 /*-------------------------------------------------------------------.
491 | FIN is pointing to a location (i.e., a `@'). Output to OOUT a |
492 | reference to this location. RULE_LENGTH is the number of values in |
493 | the current rule so far, which says where to find `$0' with |
494 | respect to the top of the stack. |
495 `-------------------------------------------------------------------*/
498 copy_at (FILE *fin
, struct obstack
*oout
, int rule_length
)
505 obstack_sgrow (oout
, "]b4_lhs_location[");
507 else if (isdigit (c
) || c
== '-')
512 n
= read_signed_integer (fin
);
514 complain (_("invalid value: %s%d"), "@", n
);
516 obstack_fgrow2 (oout
, "]b4_rhs_location([%d], [%d])[",
523 complain (_("%s is invalid"), quote (buf
));
528 /*------------------------------------------------------------------.
529 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
531 | Possible inputs: $[<TYPENAME>]($|integer) |
533 | Output to OOUT a reference to this semantic value. RULE_LENGTH is |
534 | the number of values in the current rule so far, which says where |
535 | to find `$0' with respect to the top of the stack. |
536 `------------------------------------------------------------------*/
539 copy_dollar (FILE *fin
, struct obstack
*oout
,
540 symbol_list
*rule
, int rule_length
)
543 const char *type_name
= NULL
;
545 /* Get the type name if explicit. */
548 read_type_name (fin
);
549 type_name
= token_buffer
;
550 value_components_used
= 1;
557 type_name
= get_type_name (0, rule
);
558 if (!type_name
&& typed
)
559 complain (_("$$ of `%s' has no declared type"),
563 obstack_fgrow1 (oout
,
564 "]b4_lhs_value([%s])[", type_name
);
566 else if (isdigit (c
) || c
== '-')
570 n
= read_signed_integer (fin
);
573 complain (_("invalid value: %s%d"), "$", n
);
576 if (!type_name
&& n
> 0)
577 type_name
= get_type_name (n
, rule
);
578 if (!type_name
&& typed
)
579 complain (_("$%d of `%s' has no declared type"),
583 obstack_fgrow3 (oout
, "]b4_rhs_value([%d], [%d], [%s])[",
584 rule_length
, n
, type_name
);
591 complain (_("%s is invalid"), quote (buf
));
595 /*-------------------------------------------------------------------.
596 | Copy the contents of a `%{ ... %}' into the definitions file. The |
597 | `%{' has already been read. Return after reading the `%}'. |
598 `-------------------------------------------------------------------*/
601 copy_definition (struct obstack
*oout
)
604 /* -1 while reading a character if prev char was %. */
609 obstack_fgrow2 (oout
, muscle_find ("linef"),
610 lineno
, quotearg_style (c_quoting_style
,
611 muscle_find ("filename")));
623 obstack_1grow (oout
, c
);
633 copy_string (finput
, oout
, c
);
637 copy_comment (finput
, oout
);
641 fatal ("%s", _("unterminated `%{' definition"));
644 copy_character (oout
, c
);
653 obstack_1grow (oout
, '%');
660 /*-------------------------------------------------------------------.
661 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
662 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
664 `-------------------------------------------------------------------*/
667 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
669 token_t token
= tok_undef
;
670 char *typename
= NULL
;
672 /* The symbol being defined. */
673 symbol_t
*symbol
= NULL
;
675 /* After `%token' and `%nterm', any number of symbols maybe be
679 int tmp_char
= ungetc (skip_white_space (), finput
);
681 /* `%' (for instance from `%token', or from `%%' etc.) is the
682 only valid means to end this declaration. */
686 fatal (_("Premature EOF after %s"), token_buffer
);
689 if (token
== tok_comma
)
694 if (token
== tok_typename
)
696 typename
= xstrdup (token_buffer
);
697 value_components_used
= 1;
700 else if (token
== tok_identifier
&& *symval
->tag
== '\"' && symbol
)
702 symbol_make_alias (symbol
, typename
);
705 else if (token
== tok_identifier
)
707 int oldclass
= symval
->class;
710 if (symbol
->class == what_is_not
)
711 complain (_("symbol %s redefined"), symbol
->tag
);
712 symbol
->class = what_is
;
713 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
714 symbol
->number
= nvars
++;
715 if (what_is
== token_sym
&& symbol
->number
== NUMBER_UNDEFINED
)
716 symbol
->number
= ntokens
++;
720 if (symbol
->type_name
== NULL
)
721 symbol
->type_name
= typename
;
722 else if (strcmp (typename
, symbol
->type_name
) != 0)
723 complain (_("type redeclaration for %s"), symbol
->tag
);
726 else if (symbol
&& token
== tok_number
)
728 symbol
->user_token_number
= numval
;
729 /* User defined EOF token? */
733 eoftoken
->number
= 0;
734 /* It is always mapped to 0, so it was already counted in
741 complain (_("`%s' is invalid in %s"),
743 (what_is
== token_sym
) ? "%token" : "%nterm");
751 /*------------------------------.
752 | Parse what comes after %start |
753 `------------------------------*/
756 parse_start_decl (void)
759 complain (_("multiple %s declarations"), "%start");
760 if (lex () != tok_identifier
)
761 complain (_("invalid %s declaration"), "%start");
769 /*-----------------------------------------------------------.
770 | read in a %type declaration and record its information for |
771 | get_type_name to access |
772 `-----------------------------------------------------------*/
775 parse_type_decl (void)
779 if (lex () != tok_typename
)
781 complain ("%s", _("%type declaration has no <typename>"));
786 name
= xstrdup (token_buffer
);
791 int tmp_char
= ungetc (skip_white_space (), finput
);
796 fatal (_("Premature EOF after %s"), token_buffer
);
808 if (symval
->type_name
== NULL
)
809 symval
->type_name
= name
;
810 else if (strcmp (name
, symval
->type_name
) != 0)
811 complain (_("type redeclaration for %s"), symval
->tag
);
816 complain (_("invalid %%type declaration due to item: %s"),
825 /*----------------------------------------------------------------.
826 | Read in a %left, %right or %nonassoc declaration and record its |
828 `----------------------------------------------------------------*/
831 parse_assoc_decl (associativity assoc
)
836 /* Assign a new precedence level, never 0. */
842 int tmp_char
= ungetc (skip_white_space (), finput
);
847 fatal (_("Premature EOF after %s"), token_buffer
);
854 name
= xstrdup (token_buffer
);
861 if (symval
->prec
!= 0)
862 complain (_("redefining precedence of %s"), symval
->tag
);
863 symval
->prec
= lastprec
;
864 symval
->assoc
= assoc
;
865 if (symval
->class == nterm_sym
)
866 complain (_("symbol %s redefined"), symval
->tag
);
867 if (symval
->number
== NUMBER_UNDEFINED
)
869 symval
->number
= ntokens
++;
870 symval
->class = token_sym
;
873 { /* record the type, if one is specified */
874 if (symval
->type_name
== NULL
)
875 symval
->type_name
= name
;
876 else if (strcmp (name
, symval
->type_name
) != 0)
877 complain (_("type redeclaration for %s"), symval
->tag
);
882 if (prev
== tok_identifier
)
884 symval
->user_token_number
= numval
;
889 (_("invalid text (%s) - number should be after identifier"),
899 complain (_("unexpected item: %s"), token_buffer
);
909 /*--------------------------------------------------------------.
910 | Copy the union declaration into the stype muscle |
911 | (and fdefines), where it is made into the definition of |
912 | YYSTYPE, the type of elements of the parser value stack. |
913 `--------------------------------------------------------------*/
916 parse_union_decl (void)
921 struct obstack union_obstack
;
923 complain (_("multiple %s declarations"), "%union");
927 MUSCLE_INSERT_INT ("stype_line", lineno
);
928 obstack_init (&union_obstack
);
929 obstack_sgrow (&union_obstack
, "union");
935 /* If C contains '/', it is output by copy_comment (). */
937 obstack_1grow (&union_obstack
, c
);
946 copy_comment (finput
, &union_obstack
);
954 /* FIXME: Errr. How could this happen???. --akim */
956 complain (_("unmatched %s"), "`}'");
964 /* JF don't choke on trailing semi */
965 c
= skip_white_space ();
968 obstack_1grow (&union_obstack
, 0);
969 muscle_insert ("stype", obstack_finish (&union_obstack
));
973 /*-------------------------------------------------------.
974 | Parse the declaration %expect N which says to expect N |
975 | shift-reduce conflicts. |
976 `-------------------------------------------------------*/
979 parse_expect_decl (void)
981 int c
= skip_white_space ();
985 complain (_("argument of %%expect is not an integer"));
987 expected_conflicts
= read_signed_integer (finput
);
991 /*-------------------------------------------------------------------.
992 | Parse what comes after %thong. the full syntax is |
994 | %thong <type> token number literal |
996 | the <type> or number may be omitted. The number specifies the |
997 | user_token_number. |
999 | Two symbols are entered in the table, one for the token symbol and |
1000 | one for the literal. Both are given the <type>, if any, from the |
1001 | declaration. The ->user_token_number of the first is |
1002 | USER_NUMBER_ALIAS and the ->user_token_number of the second is set |
1003 | to the number, if any, from the declaration. The two symbols are |
1004 | linked via pointers in their ->alias fields. |
1006 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
1007 | only the literal string is retained it is the literal string that |
1008 | is output to yytname |
1009 `-------------------------------------------------------------------*/
1012 parse_thong_decl (void)
1017 int usrtoknum
= USER_NUMBER_UNDEFINED
;
1019 token
= lex (); /* fetch typename or first token */
1020 if (token
== tok_typename
)
1022 typename
= xstrdup (token_buffer
);
1023 value_components_used
= 1;
1024 token
= lex (); /* fetch first token */
1027 /* process first token */
1029 if (token
!= tok_identifier
)
1031 complain (_("unrecognized item %s, expected an identifier"),
1036 symval
->class = token_sym
;
1037 symval
->type_name
= typename
;
1038 symval
->user_token_number
= USER_NUMBER_ALIAS
;
1041 token
= lex (); /* get number or literal string */
1043 if (token
== tok_number
)
1046 token
= lex (); /* okay, did number, now get literal */
1049 /* process literal string token */
1051 if (token
!= tok_identifier
|| *symval
->tag
!= '\"')
1053 complain (_("expected string constant instead of %s"), token_buffer
);
1057 symval
->class = token_sym
;
1058 symval
->type_name
= typename
;
1059 symval
->user_token_number
= usrtoknum
;
1061 symval
->alias
= symbol
;
1062 symbol
->alias
= symval
;
1064 /* symbol and symval combined are only one symbol. */
1070 parse_muscle_decl (void)
1072 int ch
= ungetc (skip_white_space (), finput
);
1077 if (!isalpha (ch
) && ch
!= '_')
1079 complain (_("invalid %s declaration"), "%define");
1083 copy_identifier (finput
, &muscle_obstack
);
1084 obstack_1grow (&muscle_obstack
, 0);
1085 muscle_key
= obstack_finish (&muscle_obstack
);
1088 ch
= skip_white_space ();
1091 ungetc (ch
, finput
);
1094 complain (_("invalid %s declaration"), "%define");
1099 fatal (_("Premature EOF after %s"), "\"");
1101 copy_string2 (finput
, &muscle_obstack
, '"', 0);
1102 obstack_1grow (&muscle_obstack
, 0);
1103 muscle_value
= obstack_finish (&muscle_obstack
);
1105 /* Store the (key, value) pair in the environment. */
1106 muscle_insert (muscle_key
, muscle_value
);
1111 /*---------------------------------.
1112 | Parse a double quoted parameter. |
1113 `---------------------------------*/
1116 parse_dquoted_param (const char *from
)
1118 struct obstack param_obstack
;
1119 const char *param
= NULL
;
1122 obstack_init (¶m_obstack
);
1123 c
= skip_white_space ();
1127 complain (_("invalid %s declaration"), from
);
1133 while ((c
= literalchar ()) != '"')
1134 obstack_1grow (¶m_obstack
, c
);
1136 obstack_1grow (¶m_obstack
, '\0');
1137 param
= obstack_finish (¶m_obstack
);
1139 if (c
!= '"' || strlen (param
) == 0)
1141 complain (_("invalid %s declaration"), from
);
1151 /*----------------------------------.
1152 | Parse what comes after %skeleton. |
1153 `----------------------------------*/
1156 parse_skel_decl (void)
1158 skeleton
= parse_dquoted_param ("%skeleton");
1161 /*----------------------------------------------------------------.
1162 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
1163 | any `%' declarations, and copy the contents of any `%{ ... %}' |
1164 | groups to PRE_PROLOGUE_OBSTACK or POST_PROLOGUE_OBSTACK. |
1165 `----------------------------------------------------------------*/
1168 read_declarations (void)
1172 int c
= skip_white_space ();
1176 token_t tok
= parse_percent_token ();
1180 case tok_two_percents
:
1183 case tok_percent_left_curly
:
1185 copy_definition (&pre_prologue_obstack
);
1187 copy_definition (&post_prologue_obstack
);
1191 parse_token_decl (token_sym
, nterm_sym
);
1195 parse_token_decl (nterm_sym
, token_sym
);
1203 parse_start_decl ();
1207 parse_union_decl ();
1211 parse_expect_decl ();
1215 parse_thong_decl ();
1219 parse_assoc_decl (left_assoc
);
1223 parse_assoc_decl (right_assoc
);
1227 parse_assoc_decl (non_assoc
);
1231 parse_muscle_decl ();
1249 complain (_("unrecognized: %s"), token_buffer
);
1254 fatal (_("no input grammar"));
1259 complain (_("unknown character: %s"), quote (buf
));
1265 /*------------------------------------------------------------------.
1266 | Assuming that a `{' has just been seen, copy everything up to the |
1267 | matching `}' into ACTION_OBSTACK. |
1269 | RULE_LENGTH is the number of values in the current rule so far, |
1270 | which says where to find `$0' with respect to the top of the |
1271 | stack. It is not the same as the rule->length in the case of mid |
1274 | This routine is used for actions. |
1275 `------------------------------------------------------------------*/
1278 parse_action (symbol_list
*rule
, int rule_length
)
1281 rule
->action_line
= lineno
;
1285 while ((c
= getc (finput
)) != '}')
1289 copy_character (&action_obstack
, c
);
1294 copy_character (&action_obstack
, c
);
1300 copy_string (finput
, &action_obstack
, c
);
1304 copy_comment (finput
, &action_obstack
);
1308 copy_dollar (finput
, &action_obstack
, rule
, rule_length
);
1312 copy_at (finput
, &action_obstack
, rule_length
);
1316 fatal (_("unmatched %s"), "`{'");
1319 copy_character (&action_obstack
, c
);
1322 /* Above loop exits when C is '}'. */
1324 copy_character (&action_obstack
, c
);
1327 obstack_1grow (&action_obstack
, '\0');
1328 rule
->action
= obstack_finish (&action_obstack
);
1333 /*-------------------------------------------------------------------.
1334 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1335 | with the user's names. |
1336 `-------------------------------------------------------------------*/
1341 /* Incremented for each generated symbol */
1342 static int gensym_count
= 0;
1343 static char buf
[256];
1347 sprintf (buf
, "@%d", ++gensym_count
);
1349 sym
= getsym (token_buffer
);
1350 sym
->class = nterm_sym
;
1351 sym
->number
= nvars
++;
1355 /*-------------------------------------------------------------------.
1356 | Parse the input grammar into a one symbol_list structure. Each |
1357 | rule is represented by a sequence of symbols: the left hand side |
1358 | followed by the contents of the right hand side, followed by a |
1359 | null pointer instead of a symbol to terminate the rule. The next |
1360 | symbol is the lhs of the following rule. |
1362 | All actions are copied out, labelled by the rule number they apply |
1365 | Bison used to allow some %directives in the rules sections, but |
1366 | this is no longer consider appropriate: (i) the documented grammar |
1367 | doesn't claim it, (ii), it would promote bad style, (iii), error |
1368 | recovery for %directives consists in skipping the junk until a `%' |
1369 | is seen and helrp synchronizing. This scheme is definitely wrong |
1370 | in the rules section. |
1371 `-------------------------------------------------------------------*/
1377 symbol_t
*lhs
= NULL
;
1378 symbol_list
*p
= NULL
;
1379 symbol_list
*p1
= NULL
;
1381 /* Points to first symbol_list of current rule. its symbol is the
1383 symbol_list
*crule
= NULL
;
1384 /* Points to the symbol_list preceding crule. */
1385 symbol_list
*crule1
= NULL
;
1389 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1390 if (t
== tok_identifier
|| t
== tok_bar
)
1392 int action_flag
= 0;
1393 /* Number of symbols in rhs of this rule so far */
1395 int xactions
= 0; /* JF for error checking */
1396 symbol_t
*first_rhs
= 0;
1398 if (t
== tok_identifier
)
1411 complain (_("ill-formed rule: initial symbol not followed by colon"));
1416 if (nrules
== 0 && t
== tok_bar
)
1418 complain (_("grammar starts with vertical bar"));
1419 lhs
= symval
; /* BOGUS: use a random symval */
1421 /* start a new rule and record its lhs. */
1426 p
= symbol_list_new (lhs
);
1437 /* mark the rule's lhs as a nonterminal if not already so. */
1439 if (lhs
->class == unknown_sym
)
1441 lhs
->class = nterm_sym
;
1442 lhs
->number
= nvars
;
1445 else if (lhs
->class == token_sym
)
1446 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1448 /* read the rhs of the rule. */
1456 crule
->ruleprec
= symval
;
1460 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1463 /* If next token is an identifier, see if a colon follows it.
1464 If one does, exit this rule now. */
1465 if (t
== tok_identifier
)
1474 if (t1
== tok_colon
)
1476 warn (_("previous rule lacks an ending `;'"));
1480 if (!first_rhs
) /* JF */
1482 /* Not followed by colon =>
1483 process as part of this rule's rhs. */
1486 /* If we just passed an action, that action was in the middle
1487 of a rule, so make a dummy rule to reduce it to a
1491 /* Since the action was written out with this rule's
1492 number, we must give the new rule this number by
1493 inserting the new rule before it. */
1495 /* Make a dummy nonterminal, a gensym. */
1496 symbol_t
*sdummy
= gensym ();
1498 /* Make a new rule, whose body is empty, before the
1499 current one, so that the action just read can
1503 p
= symbol_list_new (sdummy
);
1504 /* Attach its lineno to that of the host rule. */
1505 p
->line
= crule
->line
;
1506 /* Move the action from the host rule to this one. */
1507 p
->action
= crule
->action
;
1508 p
->action_line
= crule
->action_line
;
1509 crule
->action
= NULL
;
1515 /* End of the rule. */
1516 crule1
= symbol_list_new (NULL
);
1517 crule1
->next
= crule
;
1521 /* Insert the dummy generated by that rule into this
1524 p
= symbol_list_new (sdummy
);
1531 if (t
== tok_identifier
)
1534 p
= symbol_list_new (symval
);
1538 else /* handle an action. */
1540 parse_action (crule
, rulelength
);
1542 ++xactions
; /* JF */
1545 } /* end of read rhs of rule */
1547 /* Put an empty link in the list to mark the end of this rule */
1548 p
= symbol_list_new (NULL
);
1554 complain (_("two @prec's in a row"));
1556 crule
->ruleprec
= symval
;
1560 if (t
== tok_left_curly
)
1562 /* This case never occurs -wjh */
1564 complain (_("two actions at end of one rule"));
1565 parse_action (crule
, rulelength
);
1567 ++xactions
; /* -wjh */
1570 /* If $$ is being set in default way, report if any type
1573 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1575 if (lhs
->type_name
== 0
1576 || first_rhs
->type_name
== 0
1577 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1578 complain (_("type clash (`%s' `%s') on default action"),
1579 lhs
->type_name
? lhs
->type_name
: "",
1580 first_rhs
->type_name
? first_rhs
->type_name
: "");
1582 /* Warn if there is no default for $$ but we need one. */
1583 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1584 complain (_("empty rule for typed nonterminal, and no action"));
1585 if (t
== tok_two_percents
|| t
== tok_eof
)
1586 warn (_("previous rule lacks an ending `;'"));
1587 if (t
== tok_semicolon
)
1592 complain (_("invalid input: %s"), quote (token_buffer
));
1596 /* grammar has been read. Do some checking */
1599 fatal (_("no rules in the input grammar"));
1601 /* Report any undefined symbols and consider them nonterminals. */
1602 symbols_do (symbol_check_defined
, NULL
);
1604 /* Insert the initial rule, which line is that of the first rule
1605 (not that of the start symbol):
1607 axiom: %start EOF. */
1608 p
= symbol_list_new (axiom
);
1609 p
->line
= grammar
->line
;
1610 p
->next
= symbol_list_new (startval
);
1611 p
->next
->next
= symbol_list_new (eoftoken
);
1612 p
->next
->next
->next
= symbol_list_new (NULL
);
1613 p
->next
->next
->next
->next
= grammar
;
1619 if (nsyms
> SHRT_MAX
)
1620 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1623 assert (nsyms
== ntokens
+ nvars
);
1626 /* At the end of the grammar file, some C source code must
1627 be stored. It is going to be associated to the epilogue
1630 read_additionnal_code (void)
1633 struct obstack el_obstack
;
1635 obstack_init (&el_obstack
);
1639 obstack_fgrow2 (&el_obstack
, muscle_find ("linef"),
1640 lineno
, quotearg_style (c_quoting_style
,
1641 muscle_find ("filename")));
1644 while ((c
= getc (finput
)) != EOF
)
1645 copy_character (&el_obstack
, c
);
1647 obstack_1grow (&el_obstack
, 0);
1648 muscle_insert ("epilogue", obstack_finish (&el_obstack
));
1652 /*------------------------------------------------------------------.
1653 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
1655 `------------------------------------------------------------------*/
1658 token_translations_init (void)
1660 int num_256_available_p
= TRUE
;
1663 /* Find the highest user token number, and whether 256, the POSIX
1664 preferred user token number for the error token, is used. */
1665 max_user_token_number
= 0;
1666 for (i
= 0; i
< ntokens
; ++i
)
1668 symbol_t
*this = symbols
[i
];
1669 if (this->user_token_number
!= USER_NUMBER_UNDEFINED
)
1671 if (this->user_token_number
> max_user_token_number
)
1672 max_user_token_number
= this->user_token_number
;
1673 if (this->user_token_number
== 256)
1674 num_256_available_p
= FALSE
;
1678 /* If 256 is not used, assign it to error, to follow POSIX. */
1679 if (num_256_available_p
1680 && errtoken
->user_token_number
== USER_NUMBER_UNDEFINED
)
1681 errtoken
->user_token_number
= 256;
1683 /* Set the missing user numbers. */
1684 if (max_user_token_number
< 256)
1685 max_user_token_number
= 256;
1687 for (i
= 0; i
< ntokens
; ++i
)
1689 symbol_t
*this = symbols
[i
];
1690 if (this->user_token_number
== USER_NUMBER_UNDEFINED
)
1691 this->user_token_number
= ++max_user_token_number
;
1692 if (this->user_token_number
> max_user_token_number
)
1693 max_user_token_number
= this->user_token_number
;
1696 token_translations
= XCALLOC (symbol_number_t
, max_user_token_number
+ 1);
1698 /* Initialize all entries for literal tokens to 2, the internal
1699 token number for $undefined., which represents all invalid
1701 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
1702 token_translations
[i
] = undeftoken
->number
;
1703 symbols_do (symbol_translation
, NULL
);
1707 /*----------------------------------------------------------------.
1708 | Assign symbol numbers, and write definition of token names into |
1709 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
1710 `----------------------------------------------------------------*/
1715 symbols
= XCALLOC (symbol_t
*, nsyms
);
1717 symbols_do (symbol_check_alias_consistence
, NULL
);
1718 symbols_do (symbol_pack
, NULL
);
1720 token_translations_init ();
1722 if (startval
->class == unknown_sym
)
1723 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1724 else if (startval
->class == token_sym
)
1725 fatal (_("the start symbol %s is a token"), startval
->tag
);
1727 start_symbol
= startval
->number
;
1731 /*---------------------------------------------------------------.
1732 | Convert the rules into the representation using RRHS, RLHS and |
1734 `---------------------------------------------------------------*/
1739 unsigned int itemno
;
1743 ritem
= XCALLOC (item_number_t
, nritems
);
1744 rules
= XCALLOC (rule_t
, nrules
) - 1;
1752 symbol_t
*ruleprec
= p
->ruleprec
;
1753 rules
[ruleno
].user_number
= ruleno
;
1754 rules
[ruleno
].number
= ruleno
;
1755 rules
[ruleno
].lhs
= p
->sym
;
1756 rules
[ruleno
].rhs
= ritem
+ itemno
;
1757 rules
[ruleno
].line
= p
->line
;
1758 rules
[ruleno
].useful
= TRUE
;
1759 rules
[ruleno
].action
= p
->action
;
1760 rules
[ruleno
].action_line
= p
->action_line
;
1765 /* item_number_t = symbol_number_t.
1766 But the former needs to contain more: negative rule numbers. */
1767 ritem
[itemno
++] = symbol_number_as_item_number (p
->sym
->number
);
1768 /* A rule gets by default the precedence and associativity
1769 of the last token in it. */
1770 if (p
->sym
->class == token_sym
)
1771 rules
[ruleno
].prec
= p
->sym
;
1776 /* If this rule has a %prec,
1777 the specified symbol's precedence replaces the default. */
1780 rules
[ruleno
].precsym
= ruleprec
;
1781 rules
[ruleno
].prec
= ruleprec
;
1783 ritem
[itemno
++] = -ruleno
;
1790 assert (itemno
== nritems
);
1793 ritem_print (stderr
);
1796 /*------------------------------------------------------------------.
1797 | Read in the grammar specification and record it in the format |
1798 | described in gram.h. All actions are copied into ACTION_OBSTACK, |
1799 | in each case forming the body of a C function (YYACTION) which |
1800 | contains a switch statement to decide which action to execute. |
1801 `------------------------------------------------------------------*/
1809 /* Initialize the muscle obstack. */
1810 obstack_init (&muscle_obstack
);
1812 /* Initialize the symbol table. */
1815 /* Construct the axiom symbol. */
1816 axiom
= getsym ("$axiom");
1817 axiom
->class = nterm_sym
;
1818 axiom
->number
= nvars
++;
1820 /* Construct the error token */
1821 errtoken
= getsym ("error");
1822 errtoken
->class = token_sym
;
1823 errtoken
->number
= ntokens
++;
1825 /* Construct a token that represents all undefined literal tokens.
1826 It is always token number 2. */
1827 undeftoken
= getsym ("$undefined.");
1828 undeftoken
->class = token_sym
;
1829 undeftoken
->number
= ntokens
++;
1831 /* Initialize the obstacks. */
1832 obstack_init (&action_obstack
);
1833 obstack_init (&output_obstack
);
1834 obstack_init (&pre_prologue_obstack
);
1835 obstack_init (&post_prologue_obstack
);
1837 finput
= xfopen (infile
, "r");
1839 /* Read the declaration section. Copy %{ ... %} groups to
1840 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1841 etc. found there. */
1842 read_declarations ();
1844 /* If the user did not define her EOFTOKEN, do it now. */
1847 eoftoken
= getsym ("$");
1848 eoftoken
->class = token_sym
;
1849 eoftoken
->number
= 0;
1850 /* Value specified by POSIX. */
1851 eoftoken
->user_token_number
= 0;
1854 /* Read in the grammar, build grammar in list form. Write out
1857 /* Some C code is given at the end of the grammar file. */
1858 read_additionnal_code ();
1863 /* Assign the symbols their symbol numbers. Write #defines for the
1864 token symbols into FDEFINES if requested. */
1867 /* Convert the grammar into the format described in gram.h. */
1870 /* The grammar as a symbol_list is no longer needed. */
1871 LIST_FREE (symbol_list
, grammar
);