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 | Dump the string from FIN to OOUT if non null. MATCH is the |
337 | delimiter of the string (either ' or "). |
338 `------------------------------------------------------------*/
341 copy_string2 (FILE *fin
, struct obstack
*oout
, int match
, int store
)
346 obstack_1grow (oout
, match
);
353 fatal (_("unterminated string at end of file"));
356 complain (_("unterminated string"));
358 c
= match
; /* invent terminator */
362 obstack_1grow (oout
, c
);
368 fatal (_("unterminated string at end of file"));
369 obstack_1grow (oout
, c
);
379 obstack_1grow (oout
, c
);
385 copy_string (FILE *fin
, struct obstack
*oout
, int match
)
387 copy_string2 (fin
, oout
, match
, 1);
393 copy_identifier (FILE *fin
, struct obstack
*oout
)
397 while (isalnum (c
= getc (fin
)) || c
== '_')
398 obstack_1grow (oout
, c
);
404 /*------------------------------------------------------------------.
405 | Dump the wannabee comment from IN to OOUT. In fact we just saw a |
406 | `/', which might or might not be a comment. In any case, copy |
408 `------------------------------------------------------------------*/
411 copy_comment (FILE *fin
, struct obstack
*oout
)
417 /* We read a `/', output it. */
418 obstack_1grow (oout
, '/');
420 switch ((c
= getc (fin
)))
433 obstack_1grow (oout
, c
);
439 if (!cplus_comment
&& c
== '*')
443 obstack_1grow (oout
, c
);
449 obstack_1grow (oout
, c
);
456 obstack_1grow (oout
, c
);
463 fatal (_("unterminated comment"));
466 obstack_1grow (oout
, c
);
473 /*-----------------------------------------------------------------.
474 | FIN is pointing to a location (i.e., a `@'). Output to OOUT a |
475 | reference to this location. STACK_OFFSET is the number of values |
476 | in the current rule so far, which says where to find `$0' with |
477 | respect to the top of the stack. |
478 `-----------------------------------------------------------------*/
481 copy_at (FILE *fin
, struct obstack
*oout
, int stack_offset
)
488 obstack_sgrow (oout
, "yyloc");
491 else if (isdigit (c
) || c
== '-')
496 n
= read_signed_integer (fin
);
497 if (n
> stack_offset
)
498 complain (_("invalid value: %s%d"), "@", n
);
501 /* Offset is always 0 if parser has already popped the stack
503 obstack_fgrow1 (oout
, "yylsp[%d]",
504 n
- (semantic_parser
? 0 : stack_offset
));
512 complain (_("%s is invalid"), quote (buf
));
517 /*-------------------------------------------------------------------.
518 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
520 | Possible inputs: $[<TYPENAME>]($|integer) |
522 | Output to OOUT a reference to this semantic value. STACK_OFFSET is |
523 | the number of values in the current rule so far, which says where |
524 | to find `$0' with respect to the top of the stack. |
525 `-------------------------------------------------------------------*/
528 copy_dollar (FILE *fin
, struct obstack
*oout
,
529 symbol_list
*rule
, int stack_offset
)
532 const char *type_name
= NULL
;
534 /* Get the type name if explicit. */
537 read_type_name (fin
);
538 type_name
= token_buffer
;
539 value_components_used
= 1;
545 obstack_sgrow (oout
, "yyval");
548 type_name
= get_type_name (0, rule
);
550 obstack_fgrow1 (oout
, ".%s", type_name
);
551 if (!type_name
&& typed
)
552 complain (_("$$ of `%s' has no declared type"),
555 else if (isdigit (c
) || c
== '-')
559 n
= read_signed_integer (fin
);
561 if (n
> stack_offset
)
562 complain (_("invalid value: %s%d"), "$", n
);
565 if (!type_name
&& n
> 0)
566 type_name
= get_type_name (n
, rule
);
568 /* Offset is always 0 if parser has already popped the stack
570 obstack_fgrow1 (oout
, "yyvsp[%d]",
571 n
- (semantic_parser
? 0 : stack_offset
));
574 obstack_fgrow1 (oout
, ".%s", type_name
);
575 if (!type_name
&& typed
)
576 complain (_("$%d of `%s' has no declared type"),
584 complain (_("%s is invalid"), quote (buf
));
588 /*-------------------------------------------------------------------.
589 | Copy the contents of a `%{ ... %}' into the definitions file. The |
590 | `%{' has already been read. Return after reading the `%}'. |
591 `-------------------------------------------------------------------*/
594 copy_definition (struct obstack
*oout
)
597 /* -1 while reading a character if prev char was %. */
602 obstack_fgrow2 (oout
, muscle_find ("linef"),
603 lineno
, quotearg_style (c_quoting_style
,
604 muscle_find ("filename")));
616 obstack_1grow (oout
, c
);
626 copy_string (finput
, oout
, c
);
630 copy_comment (finput
, oout
);
634 fatal ("%s", _("unterminated `%{' definition"));
637 obstack_1grow (oout
, c
);
646 obstack_1grow (oout
, '%');
653 /*-------------------------------------------------------------------.
654 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
655 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
657 `-------------------------------------------------------------------*/
660 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
662 token_t token
= tok_undef
;
663 char *typename
= NULL
;
665 /* The symbol being defined. */
666 symbol_t
*symbol
= NULL
;
668 /* After `%token' and `%nterm', any number of symbols maybe be
672 int tmp_char
= ungetc (skip_white_space (), finput
);
674 /* `%' (for instance from `%token', or from `%%' etc.) is the
675 only valid means to end this declaration. */
679 fatal (_("Premature EOF after %s"), token_buffer
);
682 if (token
== tok_comma
)
687 if (token
== tok_typename
)
689 typename
= xstrdup (token_buffer
);
690 value_components_used
= 1;
693 else if (token
== tok_identifier
&& *symval
->tag
== '\"' && symbol
)
695 symbol_make_alias (symbol
, typename
);
698 else if (token
== tok_identifier
)
700 int oldclass
= symval
->class;
703 if (symbol
->class == what_is_not
)
704 complain (_("symbol %s redefined"), symbol
->tag
);
705 symbol
->class = what_is
;
706 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
707 symbol
->number
= nvars
++;
708 if (what_is
== token_sym
&& symbol
->number
== NUMBER_UNDEFINED
)
709 symbol
->number
= ntokens
++;
713 if (symbol
->type_name
== NULL
)
714 symbol
->type_name
= typename
;
715 else if (strcmp (typename
, symbol
->type_name
) != 0)
716 complain (_("type redeclaration for %s"), symbol
->tag
);
719 else if (symbol
&& token
== tok_number
)
721 symbol
->user_token_number
= numval
;
722 /* User defined EOF token? */
726 eoftoken
->number
= 0;
727 /* It is always mapped to 0, so it was already counted in
734 complain (_("`%s' is invalid in %s"),
736 (what_is
== token_sym
) ? "%token" : "%nterm");
744 /*------------------------------.
745 | Parse what comes after %start |
746 `------------------------------*/
749 parse_start_decl (void)
752 complain (_("multiple %s declarations"), "%start");
753 if (lex () != tok_identifier
)
754 complain (_("invalid %s declaration"), "%start");
762 /*-----------------------------------------------------------.
763 | read in a %type declaration and record its information for |
764 | get_type_name to access |
765 `-----------------------------------------------------------*/
768 parse_type_decl (void)
772 if (lex () != tok_typename
)
774 complain ("%s", _("%type declaration has no <typename>"));
779 name
= xstrdup (token_buffer
);
784 int tmp_char
= ungetc (skip_white_space (), finput
);
789 fatal (_("Premature EOF after %s"), token_buffer
);
801 if (symval
->type_name
== NULL
)
802 symval
->type_name
= name
;
803 else if (strcmp (name
, symval
->type_name
) != 0)
804 complain (_("type redeclaration for %s"), symval
->tag
);
809 complain (_("invalid %%type declaration due to item: %s"),
818 /*----------------------------------------------------------------.
819 | Read in a %left, %right or %nonassoc declaration and record its |
821 `----------------------------------------------------------------*/
824 parse_assoc_decl (associativity assoc
)
829 /* Assign a new precedence level, never 0. */
835 int tmp_char
= ungetc (skip_white_space (), finput
);
840 fatal (_("Premature EOF after %s"), token_buffer
);
847 name
= xstrdup (token_buffer
);
854 if (symval
->prec
!= 0)
855 complain (_("redefining precedence of %s"), symval
->tag
);
856 symval
->prec
= lastprec
;
857 symval
->assoc
= assoc
;
858 if (symval
->class == nterm_sym
)
859 complain (_("symbol %s redefined"), symval
->tag
);
860 if (symval
->number
== NUMBER_UNDEFINED
)
862 symval
->number
= ntokens
++;
863 symval
->class = token_sym
;
866 { /* record the type, if one is specified */
867 if (symval
->type_name
== NULL
)
868 symval
->type_name
= name
;
869 else if (strcmp (name
, symval
->type_name
) != 0)
870 complain (_("type redeclaration for %s"), symval
->tag
);
875 if (prev
== tok_identifier
)
877 symval
->user_token_number
= numval
;
882 (_("invalid text (%s) - number should be after identifier"),
892 complain (_("unexpected item: %s"), token_buffer
);
902 /*--------------------------------------------------------------.
903 | Copy the union declaration into the stype muscle |
904 | (and fdefines), where it is made into the definition of |
905 | YYSTYPE, the type of elements of the parser value stack. |
906 `--------------------------------------------------------------*/
909 parse_union_decl (void)
914 struct obstack union_obstack
;
916 complain (_("multiple %s declarations"), "%union");
920 MUSCLE_INSERT_INT ("stype_line", lineno
);
921 obstack_init (&union_obstack
);
922 obstack_sgrow (&union_obstack
, "union");
928 /* If C contains '/', it is output by copy_comment (). */
930 obstack_1grow (&union_obstack
, c
);
939 copy_comment (finput
, &union_obstack
);
947 /* FIXME: Errr. How could this happen???. --akim */
949 complain (_("unmatched %s"), "`}'");
957 /* JF don't choke on trailing semi */
958 c
= skip_white_space ();
961 obstack_1grow (&union_obstack
, 0);
962 muscle_insert ("stype", obstack_finish (&union_obstack
));
966 /*-------------------------------------------------------.
967 | Parse the declaration %expect N which says to expect N |
968 | shift-reduce conflicts. |
969 `-------------------------------------------------------*/
972 parse_expect_decl (void)
974 int c
= skip_white_space ();
978 complain (_("argument of %%expect is not an integer"));
980 expected_conflicts
= read_signed_integer (finput
);
984 /*-------------------------------------------------------------------.
985 | Parse what comes after %thong. the full syntax is |
987 | %thong <type> token number literal |
989 | the <type> or number may be omitted. The number specifies the |
990 | user_token_number. |
992 | Two symbols are entered in the table, one for the token symbol and |
993 | one for the literal. Both are given the <type>, if any, from the |
994 | declaration. The ->user_token_number of the first is SALIAS and |
995 | the ->user_token_number of the second is set to the number, if |
996 | any, from the declaration. The two symbols are linked via |
997 | pointers in their ->alias fields. |
999 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
1000 | only the literal string is retained it is the literal string that |
1001 | is output to yytname |
1002 `-------------------------------------------------------------------*/
1005 parse_thong_decl (void)
1010 int usrtoknum
= SUNDEF
;
1012 token
= lex (); /* fetch typename or first token */
1013 if (token
== tok_typename
)
1015 typename
= xstrdup (token_buffer
);
1016 value_components_used
= 1;
1017 token
= lex (); /* fetch first token */
1020 /* process first token */
1022 if (token
!= tok_identifier
)
1024 complain (_("unrecognized item %s, expected an identifier"),
1029 symval
->class = token_sym
;
1030 symval
->type_name
= typename
;
1031 symval
->user_token_number
= SALIAS
;
1034 token
= lex (); /* get number or literal string */
1036 if (token
== tok_number
)
1039 token
= lex (); /* okay, did number, now get literal */
1042 /* process literal string token */
1044 if (token
!= tok_identifier
|| *symval
->tag
!= '\"')
1046 complain (_("expected string constant instead of %s"), token_buffer
);
1050 symval
->class = token_sym
;
1051 symval
->type_name
= typename
;
1052 symval
->user_token_number
= usrtoknum
;
1054 symval
->alias
= symbol
;
1055 symbol
->alias
= symval
;
1057 /* symbol and symval combined are only one symbol. */
1063 parse_muscle_decl (void)
1065 int ch
= ungetc (skip_white_space (), finput
);
1070 if (!isalpha (ch
) && ch
!= '_')
1072 complain (_("invalid %s declaration"), "%define");
1076 copy_identifier (finput
, &muscle_obstack
);
1077 obstack_1grow (&muscle_obstack
, 0);
1078 muscle_key
= obstack_finish (&muscle_obstack
);
1081 ch
= skip_white_space ();
1084 ungetc (ch
, finput
);
1087 complain (_("invalid %s declaration"), "%define");
1092 fatal (_("Premature EOF after %s"), "\"");
1094 copy_string2 (finput
, &muscle_obstack
, '"', 0);
1095 obstack_1grow (&muscle_obstack
, 0);
1096 muscle_value
= obstack_finish (&muscle_obstack
);
1098 /* Store the (key, value) pair in the environment. */
1099 muscle_insert (muscle_key
, muscle_value
);
1104 /*---------------------------------.
1105 | Parse a double quoted parameter. |
1106 `---------------------------------*/
1109 parse_dquoted_param (const char *from
)
1111 struct obstack param_obstack
;
1112 const char *param
= NULL
;
1115 obstack_init (¶m_obstack
);
1116 c
= skip_white_space ();
1120 complain (_("invalid %s declaration"), from
);
1126 while ((c
= literalchar ()) != '"')
1127 obstack_1grow (¶m_obstack
, c
);
1129 obstack_1grow (¶m_obstack
, '\0');
1130 param
= obstack_finish (¶m_obstack
);
1132 if (c
!= '"' || strlen (param
) == 0)
1134 complain (_("invalid %s declaration"), from
);
1144 /*----------------------------------.
1145 | Parse what comes after %skeleton. |
1146 `----------------------------------*/
1149 parse_skel_decl (void)
1151 skeleton
= parse_dquoted_param ("%skeleton");
1154 /*----------------------------------------------------------------.
1155 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
1156 | any `%' declarations, and copy the contents of any `%{ ... %}' |
1157 | groups to PRE_PROLOGUE_OBSTACK or POST_PROLOGUE_OBSTACK. |
1158 `----------------------------------------------------------------*/
1161 read_declarations (void)
1165 int c
= skip_white_space ();
1169 token_t tok
= parse_percent_token ();
1173 case tok_two_percents
:
1176 case tok_percent_left_curly
:
1178 copy_definition (&pre_prologue_obstack
);
1180 copy_definition (&post_prologue_obstack
);
1184 parse_token_decl (token_sym
, nterm_sym
);
1188 parse_token_decl (nterm_sym
, token_sym
);
1196 parse_start_decl ();
1200 parse_union_decl ();
1204 parse_expect_decl ();
1208 parse_thong_decl ();
1212 parse_assoc_decl (left_assoc
);
1216 parse_assoc_decl (right_assoc
);
1220 parse_assoc_decl (non_assoc
);
1224 parse_muscle_decl ();
1242 complain (_("unrecognized: %s"), token_buffer
);
1247 fatal (_("no input grammar"));
1252 complain (_("unknown character: %s"), quote (buf
));
1258 /*-------------------------------------------------------------------.
1259 | Assuming that a `{' has just been seen, copy everything up to the |
1260 | matching `}' into the actions file. STACK_OFFSET is the number of |
1261 | values in the current rule so far, which says where to find `$0' |
1262 | with respect to the top of the stack. |
1264 | This routine is used both for actions and guards. Only |
1265 | ACTION_OBSTACK is used, but this is fine, since we use only |
1266 | pointers to relevant portions inside this obstack. |
1267 `-------------------------------------------------------------------*/
1270 parse_braces (symbol_list
*rule
, int stack_offset
)
1278 while ((c
= getc (finput
)) != '}')
1282 obstack_1grow (&action_obstack
, c
);
1287 obstack_1grow (&action_obstack
, c
);
1293 copy_string (finput
, &action_obstack
, c
);
1297 copy_comment (finput
, &action_obstack
);
1301 copy_dollar (finput
, &action_obstack
,
1302 rule
, stack_offset
);
1306 copy_at (finput
, &action_obstack
,
1311 fatal (_("unmatched %s"), "`{'");
1314 obstack_1grow (&action_obstack
, c
);
1317 /* Above loop exits when C is '}'. */
1319 obstack_1grow (&action_obstack
, c
);
1322 obstack_1grow (&action_obstack
, '\0');
1327 parse_action (symbol_list
*rule
, int stack_offset
)
1329 rule
->action_line
= lineno
;
1330 parse_braces (rule
, stack_offset
);
1331 rule
->action
= obstack_finish (&action_obstack
);
1336 parse_guard (symbol_list
*rule
, int stack_offset
)
1339 if (t
!= tok_left_curly
)
1340 complain (_("invalid %s declaration"), "%guard");
1341 rule
->guard_line
= lineno
;
1342 parse_braces (rule
, stack_offset
);
1343 rule
->guard
= obstack_finish (&action_obstack
);
1348 /*-------------------------------------------------------------------.
1349 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1350 | with the user's names. |
1351 `-------------------------------------------------------------------*/
1356 /* Incremented for each generated symbol */
1357 static int gensym_count
= 0;
1358 static char buf
[256];
1362 sprintf (buf
, "@%d", ++gensym_count
);
1364 sym
= getsym (token_buffer
);
1365 sym
->class = nterm_sym
;
1366 sym
->number
= nvars
++;
1370 /*-------------------------------------------------------------------.
1371 | Parse the input grammar into a one symbol_list structure. Each |
1372 | rule is represented by a sequence of symbols: the left hand side |
1373 | followed by the contents of the right hand side, followed by a |
1374 | null pointer instead of a symbol to terminate the rule. The next |
1375 | symbol is the lhs of the following rule. |
1377 | All guards and actions are copied out to the appropriate files, |
1378 | labelled by the rule number they apply to. |
1380 | Bison used to allow some %directives in the rules sections, but |
1381 | this is no longer consider appropriate: (i) the documented grammar |
1382 | doesn't claim it, (ii), it would promote bad style, (iii), error |
1383 | recovery for %directives consists in skipping the junk until a `%' |
1384 | is seen and helrp synchronizing. This scheme is definitely wrong |
1385 | in the rules section. |
1386 `-------------------------------------------------------------------*/
1392 symbol_t
*lhs
= NULL
;
1393 symbol_list
*p
= NULL
;
1394 symbol_list
*p1
= NULL
;
1396 /* Points to first symbol_list of current rule. its symbol is the
1398 symbol_list
*crule
= NULL
;
1399 /* Points to the symbol_list preceding crule. */
1400 symbol_list
*crule1
= NULL
;
1404 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1405 if (t
== tok_identifier
|| t
== tok_bar
)
1407 int action_flag
= 0;
1408 /* Number of symbols in rhs of this rule so far */
1410 int xactions
= 0; /* JF for error checking */
1411 symbol_t
*first_rhs
= 0;
1413 if (t
== tok_identifier
)
1426 complain (_("ill-formed rule: initial symbol not followed by colon"));
1431 if (nrules
== 0 && t
== tok_bar
)
1433 complain (_("grammar starts with vertical bar"));
1434 lhs
= symval
; /* BOGUS: use a random symval */
1436 /* start a new rule and record its lhs. */
1441 p
= symbol_list_new (lhs
);
1452 /* mark the rule's lhs as a nonterminal if not already so. */
1454 if (lhs
->class == unknown_sym
)
1456 lhs
->class = nterm_sym
;
1457 lhs
->number
= nvars
;
1460 else if (lhs
->class == token_sym
)
1461 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1463 /* read the rhs of the rule. */
1471 crule
->ruleprec
= symval
;
1475 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1478 /* If next token is an identifier, see if a colon follows it.
1479 If one does, exit this rule now. */
1480 if (t
== tok_identifier
)
1489 if (t1
== tok_colon
)
1491 warn (_("previous rule lacks an ending `;'"));
1495 if (!first_rhs
) /* JF */
1497 /* Not followed by colon =>
1498 process as part of this rule's rhs. */
1501 /* If we just passed an action, that action was in the middle
1502 of a rule, so make a dummy rule to reduce it to a
1506 /* Since the action was written out with this rule's
1507 number, we must give the new rule this number by
1508 inserting the new rule before it. */
1510 /* Make a dummy nonterminal, a gensym. */
1511 symbol_t
*sdummy
= gensym ();
1513 /* Make a new rule, whose body is empty, before the
1514 current one, so that the action just read can
1518 p
= symbol_list_new (sdummy
);
1519 /* Attach its lineno to that of the host rule. */
1520 p
->line
= crule
->line
;
1521 /* Move the action from the host rule to this one. */
1522 p
->action
= crule
->action
;
1523 p
->action_line
= crule
->action_line
;
1524 crule
->action
= NULL
;
1530 /* End of the rule. */
1531 crule1
= symbol_list_new (NULL
);
1532 crule1
->next
= crule
;
1536 /* Insert the dummy generated by that rule into this
1539 p
= symbol_list_new (sdummy
);
1546 if (t
== tok_identifier
)
1549 p
= symbol_list_new (symval
);
1553 else /* handle an action. */
1555 parse_action (crule
, rulelength
);
1557 ++xactions
; /* JF */
1560 } /* end of read rhs of rule */
1562 /* Put an empty link in the list to mark the end of this rule */
1563 p
= symbol_list_new (NULL
);
1569 complain (_("two @prec's in a row"));
1571 crule
->ruleprec
= symval
;
1577 if (!semantic_parser
)
1578 complain (_("%%guard present but %%semantic_parser not specified"));
1580 parse_guard (crule
, rulelength
);
1584 if (t
== tok_left_curly
)
1586 /* This case never occurs -wjh */
1588 complain (_("two actions at end of one rule"));
1589 parse_action (crule
, rulelength
);
1591 ++xactions
; /* -wjh */
1594 /* If $$ is being set in default way, report if any type
1597 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1599 if (lhs
->type_name
== 0
1600 || first_rhs
->type_name
== 0
1601 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1602 complain (_("type clash (`%s' `%s') on default action"),
1603 lhs
->type_name
? lhs
->type_name
: "",
1604 first_rhs
->type_name
? first_rhs
->type_name
: "");
1606 /* Warn if there is no default for $$ but we need one. */
1607 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1608 complain (_("empty rule for typed nonterminal, and no action"));
1609 if (t
== tok_two_percents
|| t
== tok_eof
)
1610 warn (_("previous rule lacks an ending `;'"));
1611 if (t
== tok_semicolon
)
1616 complain (_("invalid input: %s"), quote (token_buffer
));
1620 /* grammar has been read. Do some checking */
1623 fatal (_("no rules in the input grammar"));
1625 /* Report any undefined symbols and consider them nonterminals. */
1626 symbols_do (symbol_check_defined
, NULL
);
1628 /* Insert the initial rule, which line is that of the first rule
1629 (not that of the start symbol):
1631 axiom: %start EOF. */
1632 p
= symbol_list_new (axiom
);
1633 p
->line
= grammar
->line
;
1634 p
->next
= symbol_list_new (startval
);
1635 p
->next
->next
= symbol_list_new (eoftoken
);
1636 p
->next
->next
->next
= symbol_list_new (NULL
);
1637 p
->next
->next
->next
->next
= grammar
;
1643 if (nsyms
> SHRT_MAX
)
1644 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1647 assert (nsyms
== ntokens
+ nvars
);
1650 /* At the end of the grammar file, some C source code must
1651 be stored. It is going to be associated to the epilogue
1654 read_additionnal_code (void)
1657 struct obstack el_obstack
;
1659 obstack_init (&el_obstack
);
1663 obstack_fgrow2 (&el_obstack
, muscle_find ("linef"),
1664 lineno
, quotearg_style (c_quoting_style
,
1665 muscle_find ("filename")));
1668 while ((c
= getc (finput
)) != EOF
)
1669 obstack_1grow (&el_obstack
, c
);
1671 obstack_1grow (&el_obstack
, 0);
1672 muscle_insert ("epilogue", obstack_finish (&el_obstack
));
1676 /*------------------------------------------------------------------.
1677 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
1679 `------------------------------------------------------------------*/
1682 token_translations_init (void)
1684 int num_256_available_p
= TRUE
;
1687 /* Find the highest user token number, and whether 256, the POSIX
1688 preferred user token number for the error token, is used. */
1689 max_user_token_number
= 0;
1690 for (i
= 0; i
< ntokens
; ++i
)
1692 symbol_t
*this = symbols
[i
];
1693 if (this->user_token_number
!= SUNDEF
)
1695 if (this->user_token_number
> max_user_token_number
)
1696 max_user_token_number
= this->user_token_number
;
1697 if (this->user_token_number
== 256)
1698 num_256_available_p
= FALSE
;
1702 /* If 256 is not used, assign it to error, to follow POSIX. */
1703 if (num_256_available_p
&& errtoken
->user_token_number
== SUNDEF
)
1704 errtoken
->user_token_number
= 256;
1706 /* Set the missing user numbers. */
1707 if (max_user_token_number
< 256)
1708 max_user_token_number
= 256;
1710 for (i
= 0; i
< ntokens
; ++i
)
1712 symbol_t
*this = symbols
[i
];
1713 if (this->user_token_number
== SUNDEF
)
1714 this->user_token_number
= ++max_user_token_number
;
1715 if (this->user_token_number
> max_user_token_number
)
1716 max_user_token_number
= this->user_token_number
;
1719 token_translations
= XCALLOC (token_number_t
, max_user_token_number
+ 1);
1721 /* Initialize all entries for literal tokens to 2, the internal
1722 token number for $undefined., which represents all invalid
1724 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
1725 token_translations
[i
] = undeftoken
->number
;
1726 symbols_do (symbol_translation
, NULL
);
1730 /*----------------------------------------------------------------.
1731 | Assign symbol numbers, and write definition of token names into |
1732 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
1733 `----------------------------------------------------------------*/
1738 symbols
= XCALLOC (symbol_t
*, nsyms
);
1740 symbols_do (symbol_check_alias_consistence
, NULL
);
1741 symbols_do (symbol_pack
, NULL
);
1743 token_translations_init ();
1745 if (startval
->class == unknown_sym
)
1746 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1747 else if (startval
->class == token_sym
)
1748 fatal (_("the start symbol %s is a token"), startval
->tag
);
1750 start_symbol
= startval
->number
;
1754 /*---------------------------------------------------------------.
1755 | Convert the rules into the representation using RRHS, RLHS and |
1757 `---------------------------------------------------------------*/
1766 ritem
= XCALLOC (item_number_t
, nritems
+ 1);
1767 rules
= XCALLOC (rule_t
, nrules
) - 1;
1775 symbol_t
*ruleprec
= p
->ruleprec
;
1776 rules
[ruleno
].user_number
= ruleno
;
1777 rules
[ruleno
].number
= ruleno
;
1778 rules
[ruleno
].lhs
= p
->sym
;
1779 rules
[ruleno
].rhs
= ritem
+ itemno
;
1780 rules
[ruleno
].line
= p
->line
;
1781 rules
[ruleno
].useful
= TRUE
;
1782 rules
[ruleno
].action
= p
->action
;
1783 rules
[ruleno
].action_line
= p
->action_line
;
1784 rules
[ruleno
].guard
= p
->guard
;
1785 rules
[ruleno
].guard_line
= p
->guard_line
;
1790 /* item_number_t = token_number_t.
1791 But the former needs to contain more: negative rule numbers. */
1792 ritem
[itemno
++] = token_number_as_item_number (p
->sym
->number
);
1793 /* A rule gets by default the precedence and associativity
1794 of the last token in it. */
1795 if (p
->sym
->class == token_sym
)
1796 rules
[ruleno
].prec
= p
->sym
;
1801 /* If this rule has a %prec,
1802 the specified symbol's precedence replaces the default. */
1805 rules
[ruleno
].precsym
= ruleprec
;
1806 rules
[ruleno
].prec
= ruleprec
;
1808 ritem
[itemno
++] = -ruleno
;
1816 assert (itemno
== nritems
);
1819 ritem_print (stderr
);
1822 /*-------------------------------------------------------------------.
1823 | Read in the grammar specification and record it in the format |
1824 | described in gram.h. All guards are copied into the GUARD_OBSTACK |
1825 | and all actions into ACTION_OBSTACK, in each case forming the body |
1826 | of a C function (YYGUARD or YYACTION) which contains a switch |
1827 | statement to decide which guard or action to execute. |
1828 `-------------------------------------------------------------------*/
1836 /* Initialize the muscle obstack. */
1837 obstack_init (&muscle_obstack
);
1839 /* Initialize the symbol table. */
1842 /* Construct the axiom symbol. */
1843 axiom
= getsym ("$axiom");
1844 axiom
->class = nterm_sym
;
1845 axiom
->number
= nvars
++;
1847 /* Construct the error token */
1848 errtoken
= getsym ("error");
1849 errtoken
->class = token_sym
;
1850 errtoken
->number
= ntokens
++;
1852 /* Construct a token that represents all undefined literal tokens.
1853 It is always token number 2. */
1854 undeftoken
= getsym ("$undefined.");
1855 undeftoken
->class = token_sym
;
1856 undeftoken
->number
= ntokens
++;
1858 /* Initialize the obstacks. */
1859 obstack_init (&action_obstack
);
1860 obstack_init (&output_obstack
);
1861 obstack_init (&pre_prologue_obstack
);
1862 obstack_init (&post_prologue_obstack
);
1864 finput
= xfopen (infile
, "r");
1866 /* Read the declaration section. Copy %{ ... %} groups to
1867 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1868 etc. found there. */
1869 read_declarations ();
1871 /* If the user did not define her EOFTOKEN, do it now. */
1874 eoftoken
= getsym ("$");
1875 eoftoken
->class = token_sym
;
1876 eoftoken
->number
= 0;
1877 /* Value specified by POSIX. */
1878 eoftoken
->user_token_number
= 0;
1881 /* Read in the grammar, build grammar in list form. Write out
1882 guards and actions. */
1884 /* Some C code is given at the end of the grammar file. */
1885 read_additionnal_code ();
1890 /* Assign the symbols their symbol numbers. Write #defines for the
1891 token symbols into FDEFINES if requested. */
1894 /* Convert the grammar into the format described in gram.h. */
1897 /* The grammar as a symbol_list is no longer needed. */
1898 LIST_FREE (symbol_list
, grammar
);
1906 /* Free the symbol table data structure. */