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 (getenv ("DEBUG"))
194 fprintf (stderr
, "Packing %s, %s, number = %d\n",
196 this->class == nterm_sym
? "nterm" : "TERM",
198 if (this->class == nterm_sym
)
200 this->number
+= ntokens
;
202 else if (this->alias
)
204 /* This symbol and its alias are a single token defn.
205 Allocate a tokno, and assign to both check agreement of
206 prec and assoc fields and make both the same */
207 if (this->number
== -1)
209 if (this == eoftoken
|| this->alias
== eoftoken
)
210 this->number
= this->alias
->number
= 0;
213 assert (this->alias
->number
!= -1);
214 this->number
= this->alias
->number
;
217 /* Do not do processing below for SALIASs. */
218 if (this->user_token_number
== SALIAS
)
221 else /* this->class == token_sym */
223 assert (this->number
!= -1);
226 if (getenv ("DEBUG"))
227 fprintf (stderr
, "Setting %d = %s\n", this->number
, this->tag
);
228 symbols
[this->number
] = this;
235 /*--------------------------------------------------.
236 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
237 `--------------------------------------------------*/
240 symbol_translation (symbol_t
*this)
242 if (getenv ("DEBUG"))
243 fprintf (stderr
, "Considering Setting UserVal %s = %d (val = %d)\n",
244 this->tag
, this->user_token_number
, this->number
);
247 if (this->class == token_sym
248 && this->user_token_number
!= SALIAS
)
250 /* A token which translation has already been set? */
251 if (token_translations
[this->user_token_number
] != 2)
252 complain (_("tokens %s and %s both assigned number %d"),
253 symbols
[token_translations
[this->user_token_number
]]->tag
,
254 this->tag
, this->user_token_number
);
256 if (getenv ("DEBUG"))
257 fprintf (stderr
, "Setting UserVal %s = %d (val = %d)\n",
258 this->tag
, this->user_token_number
, this->number
);
259 token_translations
[this->user_token_number
] = this->number
;
266 /*===================\
267 | Low level lexing. |
268 \===================*/
271 skip_to_char (int target
)
275 complain (_(" Skipping to next \\n"));
277 complain (_(" Skipping to next %c"), target
);
280 c
= skip_white_space ();
281 while (c
!= target
&& c
!= EOF
);
287 /*---------------------------------------------------------.
288 | Read a signed integer from STREAM and return its value. |
289 `---------------------------------------------------------*/
292 read_signed_integer (FILE *stream
)
294 int c
= getc (stream
);
306 n
= 10 * n
+ (c
- '0');
315 /*--------------------------------------------------------------.
316 | Get the data type (alternative in the union) of the value for |
317 | symbol N in rule RULE. |
318 `--------------------------------------------------------------*/
321 get_type_name (int n
, symbol_list
*rule
)
328 complain (_("invalid $ value"));
338 if (rp
== NULL
|| rp
->sym
== NULL
)
340 complain (_("invalid $ value"));
346 return rp
->sym
->type_name
;
349 /*------------------------------------------------------------.
350 | Dump the string from FIN to OOUT if non null. MATCH is the |
351 | delimiter of the string (either ' or "). |
352 `------------------------------------------------------------*/
355 copy_string2 (FILE *fin
, struct obstack
*oout
, int match
, int store
)
360 obstack_1grow (oout
, match
);
367 fatal (_("unterminated string at end of file"));
370 complain (_("unterminated string"));
372 c
= match
; /* invent terminator */
376 obstack_1grow (oout
, c
);
382 fatal (_("unterminated string at end of file"));
383 obstack_1grow (oout
, c
);
393 obstack_1grow (oout
, c
);
399 copy_string (FILE *fin
, struct obstack
*oout
, int match
)
401 copy_string2 (fin
, oout
, match
, 1);
407 copy_identifier (FILE *fin
, struct obstack
*oout
)
411 while (isalnum (c
= getc (fin
)) || c
== '_')
412 obstack_1grow (oout
, c
);
418 /*------------------------------------------------------------------.
419 | Dump the wannabee comment from IN to OOUT. In fact we just saw a |
420 | `/', which might or might not be a comment. In any case, copy |
422 `------------------------------------------------------------------*/
425 copy_comment (FILE *fin
, struct obstack
*oout
)
431 /* We read a `/', output it. */
432 obstack_1grow (oout
, '/');
434 switch ((c
= getc (fin
)))
447 obstack_1grow (oout
, c
);
453 if (!cplus_comment
&& c
== '*')
457 obstack_1grow (oout
, c
);
463 obstack_1grow (oout
, c
);
470 obstack_1grow (oout
, c
);
477 fatal (_("unterminated comment"));
480 obstack_1grow (oout
, c
);
487 /*-----------------------------------------------------------------.
488 | FIN is pointing to a location (i.e., a `@'). Output to OOUT a |
489 | reference to this location. STACK_OFFSET is the number of values |
490 | in the current rule so far, which says where to find `$0' with |
491 | respect to the top of the stack. |
492 `-----------------------------------------------------------------*/
495 copy_at (FILE *fin
, struct obstack
*oout
, int stack_offset
)
502 obstack_sgrow (oout
, "yyloc");
505 else if (isdigit (c
) || c
== '-')
510 n
= read_signed_integer (fin
);
511 if (n
> stack_offset
)
512 complain (_("invalid value: %s%d"), "@", n
);
515 /* Offset is always 0 if parser has already popped the stack
517 obstack_fgrow1 (oout
, "yylsp[%d]",
518 n
- (semantic_parser
? 0 : stack_offset
));
526 complain (_("%s is invalid"), quote (buf
));
531 /*-------------------------------------------------------------------.
532 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
534 | Possible inputs: $[<TYPENAME>]($|integer) |
536 | Output to OOUT a reference to this semantic value. STACK_OFFSET is |
537 | the number of values in the current rule so far, which says where |
538 | to find `$0' with respect to the top of the stack. |
539 `-------------------------------------------------------------------*/
542 copy_dollar (FILE *fin
, struct obstack
*oout
,
543 symbol_list
*rule
, int stack_offset
)
546 const char *type_name
= NULL
;
548 /* Get the type name if explicit. */
551 read_type_name (fin
);
552 type_name
= token_buffer
;
553 value_components_used
= 1;
559 obstack_sgrow (oout
, "yyval");
562 type_name
= get_type_name (0, rule
);
564 obstack_fgrow1 (oout
, ".%s", type_name
);
565 if (!type_name
&& typed
)
566 complain (_("$$ of `%s' has no declared type"),
569 else if (isdigit (c
) || c
== '-')
573 n
= read_signed_integer (fin
);
575 if (n
> stack_offset
)
576 complain (_("invalid value: %s%d"), "$", n
);
579 if (!type_name
&& n
> 0)
580 type_name
= get_type_name (n
, rule
);
582 /* Offset is always 0 if parser has already popped the stack
584 obstack_fgrow1 (oout
, "yyvsp[%d]",
585 n
- (semantic_parser
? 0 : stack_offset
));
588 obstack_fgrow1 (oout
, ".%s", type_name
);
589 if (!type_name
&& typed
)
590 complain (_("$%d of `%s' has no declared type"),
598 complain (_("%s is invalid"), quote (buf
));
602 /*-------------------------------------------------------------------.
603 | Copy the contents of a `%{ ... %}' into the definitions file. The |
604 | `%{' has already been read. Return after reading the `%}'. |
605 `-------------------------------------------------------------------*/
608 copy_definition (void)
611 /* -1 while reading a character if prev char was %. */
616 obstack_fgrow2 (&attrs_obstack
, muscle_find ("linef"),
617 lineno
, quotearg_style (c_quoting_style
,
618 muscle_find ("filename")));
630 obstack_1grow (&attrs_obstack
, c
);
640 copy_string (finput
, &attrs_obstack
, c
);
644 copy_comment (finput
, &attrs_obstack
);
648 fatal ("%s", _("unterminated `%{' definition"));
651 obstack_1grow (&attrs_obstack
, c
);
660 obstack_1grow (&attrs_obstack
, '%');
667 /*-------------------------------------------------------------------.
668 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
669 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
671 `-------------------------------------------------------------------*/
674 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
676 token_t token
= tok_undef
;
677 char *typename
= NULL
;
679 /* The symbol being defined. */
680 symbol_t
*symbol
= NULL
;
682 /* After `%token' and `%nterm', any number of symbols maybe be
686 int tmp_char
= ungetc (skip_white_space (), finput
);
688 /* `%' (for instance from `%token', or from `%%' etc.) is the
689 only valid means to end this declaration. */
693 fatal (_("Premature EOF after %s"), token_buffer
);
696 if (token
== tok_comma
)
701 if (token
== tok_typename
)
703 typename
= xstrdup (token_buffer
);
704 value_components_used
= 1;
707 else if (token
== tok_identifier
&& *symval
->tag
== '\"' && symbol
)
709 symbol_make_alias (symbol
, typename
);
712 else if (token
== tok_identifier
)
714 int oldclass
= symval
->class;
717 if (symbol
->class == what_is_not
)
718 complain (_("symbol %s redefined"), symbol
->tag
);
719 symbol
->class = what_is
;
720 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
721 symbol
->number
= nvars
++;
722 if (what_is
== token_sym
&& symbol
->number
== -1)
724 symbol
->number
= ntokens
++;
725 if (getenv ("DEBUG"))
726 fprintf (stderr
, "Set %s to %d\n",
727 symbol
->tag
, symbol
->number
);
732 if (symbol
->type_name
== NULL
)
733 symbol
->type_name
= typename
;
734 else if (strcmp (typename
, symbol
->type_name
) != 0)
735 complain (_("type redeclaration for %s"), symbol
->tag
);
738 else if (symbol
&& token
== tok_number
)
740 symbol
->user_token_number
= numval
;
741 /* User defined EOF token? */
745 eoftoken
->number
= 0;
746 /* It is always mapped to 0, so it was already counted in
753 complain (_("`%s' is invalid in %s"),
755 (what_is
== token_sym
) ? "%token" : "%nterm");
763 /*------------------------------.
764 | Parse what comes after %start |
765 `------------------------------*/
768 parse_start_decl (void)
771 complain (_("multiple %s declarations"), "%start");
772 if (lex () != tok_identifier
)
773 complain (_("invalid %s declaration"), "%start");
781 /*-----------------------------------------------------------.
782 | read in a %type declaration and record its information for |
783 | get_type_name to access |
784 `-----------------------------------------------------------*/
787 parse_type_decl (void)
791 if (lex () != tok_typename
)
793 complain ("%s", _("%type declaration has no <typename>"));
798 name
= xstrdup (token_buffer
);
803 int tmp_char
= ungetc (skip_white_space (), finput
);
808 fatal (_("Premature EOF after %s"), token_buffer
);
820 if (symval
->type_name
== NULL
)
821 symval
->type_name
= name
;
822 else if (strcmp (name
, symval
->type_name
) != 0)
823 complain (_("type redeclaration for %s"), symval
->tag
);
828 complain (_("invalid %%type declaration due to item: %s"),
837 /*----------------------------------------------------------------.
838 | Read in a %left, %right or %nonassoc declaration and record its |
840 `----------------------------------------------------------------*/
843 parse_assoc_decl (associativity assoc
)
848 lastprec
++; /* Assign a new precedence level, never 0. */
853 int tmp_char
= ungetc (skip_white_space (), finput
);
858 fatal (_("Premature EOF after %s"), token_buffer
);
865 name
= xstrdup (token_buffer
);
872 if (symval
->prec
!= 0)
873 complain (_("redefining precedence of %s"), symval
->tag
);
874 symval
->prec
= lastprec
;
875 symval
->assoc
= assoc
;
876 if (symval
->class == nterm_sym
)
877 complain (_("symbol %s redefined"), symval
->tag
);
878 if (symval
->number
== -1)
880 symval
->number
= ntokens
++;
881 symval
->class = token_sym
;
884 { /* record the type, if one is specified */
885 if (symval
->type_name
== NULL
)
886 symval
->type_name
= name
;
887 else if (strcmp (name
, symval
->type_name
) != 0)
888 complain (_("type redeclaration for %s"), symval
->tag
);
893 if (prev
== tok_identifier
)
895 symval
->user_token_number
= numval
;
900 (_("invalid text (%s) - number should be after identifier"),
910 complain (_("unexpected item: %s"), token_buffer
);
920 /*--------------------------------------------------------------.
921 | Copy the union declaration into the stype muscle |
922 | (and fdefines), where it is made into the definition of |
923 | YYSTYPE, the type of elements of the parser value stack. |
924 `--------------------------------------------------------------*/
927 parse_union_decl (void)
932 struct obstack union_obstack
;
934 complain (_("multiple %s declarations"), "%union");
938 MUSCLE_INSERT_INT ("stype_line", lineno
);
939 obstack_init (&union_obstack
);
940 obstack_sgrow (&union_obstack
, "union");
946 /* If C contains '/', it is output by copy_comment (). */
948 obstack_1grow (&union_obstack
, c
);
957 copy_comment (finput
, &union_obstack
);
965 /* FIXME: Errr. How could this happen???. --akim */
967 complain (_("unmatched %s"), "`}'");
975 /* JF don't choke on trailing semi */
976 c
= skip_white_space ();
979 obstack_1grow (&union_obstack
, 0);
980 muscle_insert ("stype", obstack_finish (&union_obstack
));
984 /*-------------------------------------------------------.
985 | Parse the declaration %expect N which says to expect N |
986 | shift-reduce conflicts. |
987 `-------------------------------------------------------*/
990 parse_expect_decl (void)
992 int c
= skip_white_space ();
996 complain (_("argument of %%expect is not an integer"));
998 expected_conflicts
= read_signed_integer (finput
);
1002 /*-------------------------------------------------------------------.
1003 | Parse what comes after %thong. the full syntax is |
1005 | %thong <type> token number literal |
1007 | the <type> or number may be omitted. The number specifies the |
1008 | user_token_number. |
1010 | Two symbols are entered in the table, one for the token symbol and |
1011 | one for the literal. Both are given the <type>, if any, from the |
1012 | declaration. The ->user_token_number of the first is SALIAS and |
1013 | the ->user_token_number of the second is set to the number, if |
1014 | any, from the declaration. The two symbols are linked via |
1015 | pointers in their ->alias fields. |
1017 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
1018 | only the literal string is retained it is the literal string that |
1019 | is output to yytname |
1020 `-------------------------------------------------------------------*/
1023 parse_thong_decl (void)
1028 int usrtoknum
= SUNDEF
;
1030 token
= lex (); /* fetch typename or first token */
1031 if (token
== tok_typename
)
1033 typename
= xstrdup (token_buffer
);
1034 value_components_used
= 1;
1035 token
= lex (); /* fetch first token */
1038 /* process first token */
1040 if (token
!= tok_identifier
)
1042 complain (_("unrecognized item %s, expected an identifier"),
1047 symval
->class = token_sym
;
1048 symval
->type_name
= typename
;
1049 symval
->user_token_number
= SALIAS
;
1052 token
= lex (); /* get number or literal string */
1054 if (token
== tok_number
)
1057 token
= lex (); /* okay, did number, now get literal */
1060 /* process literal string token */
1062 if (token
!= tok_identifier
|| *symval
->tag
!= '\"')
1064 complain (_("expected string constant instead of %s"), token_buffer
);
1068 symval
->class = token_sym
;
1069 symval
->type_name
= typename
;
1070 symval
->user_token_number
= usrtoknum
;
1072 symval
->alias
= symbol
;
1073 symbol
->alias
= symval
;
1075 /* symbol and symval combined are only one symbol. */
1081 parse_muscle_decl (void)
1083 int ch
= ungetc (skip_white_space (), finput
);
1088 if (!isalpha (ch
) && ch
!= '_')
1090 complain (_("invalid %s declaration"), "%define");
1094 copy_identifier (finput
, &muscle_obstack
);
1095 obstack_1grow (&muscle_obstack
, 0);
1096 muscle_key
= obstack_finish (&muscle_obstack
);
1099 ch
= skip_white_space ();
1102 ungetc (ch
, finput
);
1105 complain (_("invalid %s declaration"), "%define");
1110 fatal (_("Premature EOF after %s"), "\"");
1112 copy_string2 (finput
, &muscle_obstack
, '"', 0);
1113 obstack_1grow (&muscle_obstack
, 0);
1114 muscle_value
= obstack_finish (&muscle_obstack
);
1116 /* Store the (key, value) pair in the environment. */
1117 muscle_insert (muscle_key
, muscle_value
);
1122 /*---------------------------------.
1123 | Parse a double quoted parameter. |
1124 `---------------------------------*/
1127 parse_dquoted_param (const char *from
)
1129 struct obstack param_obstack
;
1130 const char *param
= NULL
;
1133 obstack_init (¶m_obstack
);
1134 c
= skip_white_space ();
1138 complain (_("invalid %s declaration"), from
);
1144 while ((c
= literalchar ()) != '"')
1145 obstack_1grow (¶m_obstack
, c
);
1147 obstack_1grow (¶m_obstack
, '\0');
1148 param
= obstack_finish (¶m_obstack
);
1150 if (c
!= '"' || strlen (param
) == 0)
1152 complain (_("invalid %s declaration"), from
);
1162 /*----------------------------------.
1163 | Parse what comes after %skeleton. |
1164 `----------------------------------*/
1167 parse_skel_decl (void)
1169 skeleton
= parse_dquoted_param ("%skeleton");
1172 /*----------------------------------------------------------------.
1173 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
1174 | any `%' declarations, and copy the contents of any `%{ ... %}' |
1175 | groups to ATTRS_OBSTACK. |
1176 `----------------------------------------------------------------*/
1179 read_declarations (void)
1183 int c
= skip_white_space ();
1187 token_t tok
= parse_percent_token ();
1191 case tok_two_percents
:
1194 case tok_percent_left_curly
:
1199 parse_token_decl (token_sym
, nterm_sym
);
1203 parse_token_decl (nterm_sym
, token_sym
);
1211 parse_start_decl ();
1215 parse_union_decl ();
1219 parse_expect_decl ();
1223 parse_thong_decl ();
1227 parse_assoc_decl (left_assoc
);
1231 parse_assoc_decl (right_assoc
);
1235 parse_assoc_decl (non_assoc
);
1239 parse_muscle_decl ();
1257 complain (_("unrecognized: %s"), token_buffer
);
1262 fatal (_("no input grammar"));
1267 complain (_("unknown character: %s"), quote (buf
));
1273 /*-------------------------------------------------------------------.
1274 | Assuming that a `{' has just been seen, copy everything up to the |
1275 | matching `}' into the actions file. STACK_OFFSET is the number of |
1276 | values in the current rule so far, which says where to find `$0' |
1277 | with respect to the top of the stack. |
1279 | This routine is used both for actions and guards. Only |
1280 | ACTION_OBSTACK is used, but this is fine, since we use only |
1281 | pointers to relevant portions inside this obstack. |
1282 `-------------------------------------------------------------------*/
1285 parse_braces (symbol_list
*rule
, int stack_offset
)
1293 while ((c
= getc (finput
)) != '}')
1297 obstack_1grow (&action_obstack
, c
);
1302 obstack_1grow (&action_obstack
, c
);
1308 copy_string (finput
, &action_obstack
, c
);
1312 copy_comment (finput
, &action_obstack
);
1316 copy_dollar (finput
, &action_obstack
,
1317 rule
, stack_offset
);
1321 copy_at (finput
, &action_obstack
,
1326 fatal (_("unmatched %s"), "`{'");
1329 obstack_1grow (&action_obstack
, c
);
1332 /* Above loop exits when C is '}'. */
1334 obstack_1grow (&action_obstack
, c
);
1337 obstack_1grow (&action_obstack
, '\0');
1342 parse_action (symbol_list
*rule
, int stack_offset
)
1344 rule
->action_line
= lineno
;
1345 parse_braces (rule
, stack_offset
);
1346 rule
->action
= obstack_finish (&action_obstack
);
1351 parse_guard (symbol_list
*rule
, int stack_offset
)
1354 if (t
!= tok_left_curly
)
1355 complain (_("invalid %s declaration"), "%guard");
1356 rule
->guard_line
= lineno
;
1357 parse_braces (rule
, stack_offset
);
1358 rule
->guard
= obstack_finish (&action_obstack
);
1363 /*-------------------------------------------------------------------.
1364 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1365 | with the user's names. |
1366 `-------------------------------------------------------------------*/
1371 /* Incremented for each generated symbol */
1372 static int gensym_count
= 0;
1373 static char buf
[256];
1377 sprintf (buf
, "@%d", ++gensym_count
);
1379 sym
= getsym (token_buffer
);
1380 sym
->class = nterm_sym
;
1381 sym
->number
= nvars
++;
1385 /*-------------------------------------------------------------------.
1386 | Parse the input grammar into a one symbol_list structure. Each |
1387 | rule is represented by a sequence of symbols: the left hand side |
1388 | followed by the contents of the right hand side, followed by a |
1389 | null pointer instead of a symbol to terminate the rule. The next |
1390 | symbol is the lhs of the following rule. |
1392 | All guards and actions are copied out to the appropriate files, |
1393 | labelled by the rule number they apply to. |
1395 | Bison used to allow some %directives in the rules sections, but |
1396 | this is no longer consider appropriate: (i) the documented grammar |
1397 | doesn't claim it, (ii), it would promote bad style, (iii), error |
1398 | recovery for %directives consists in skipping the junk until a `%' |
1399 | is seen and helrp synchronizing. This scheme is definitely wrong |
1400 | in the rules section. |
1401 `-------------------------------------------------------------------*/
1407 symbol_t
*lhs
= NULL
;
1408 symbol_list
*p
= NULL
;
1409 symbol_list
*p1
= NULL
;
1411 /* Points to first symbol_list of current rule. its symbol is the
1413 symbol_list
*crule
= NULL
;
1414 /* Points to the symbol_list preceding crule. */
1415 symbol_list
*crule1
= NULL
;
1419 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1420 if (t
== tok_identifier
|| t
== tok_bar
)
1422 int action_flag
= 0;
1423 /* Number of symbols in rhs of this rule so far */
1425 int xactions
= 0; /* JF for error checking */
1426 symbol_t
*first_rhs
= 0;
1428 if (t
== tok_identifier
)
1441 complain (_("ill-formed rule: initial symbol not followed by colon"));
1446 if (nrules
== 0 && t
== tok_bar
)
1448 complain (_("grammar starts with vertical bar"));
1449 lhs
= symval
; /* BOGUS: use a random symval */
1451 /* start a new rule and record its lhs. */
1456 p
= symbol_list_new (lhs
);
1467 /* mark the rule's lhs as a nonterminal if not already so. */
1469 if (lhs
->class == unknown_sym
)
1471 lhs
->class = nterm_sym
;
1472 lhs
->number
= nvars
;
1475 else if (lhs
->class == token_sym
)
1476 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1478 /* read the rhs of the rule. */
1486 crule
->ruleprec
= symval
;
1490 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1493 /* If next token is an identifier, see if a colon follows it.
1494 If one does, exit this rule now. */
1495 if (t
== tok_identifier
)
1504 if (t1
== tok_colon
)
1506 warn (_("previous rule lacks an ending `;'"));
1510 if (!first_rhs
) /* JF */
1512 /* Not followed by colon =>
1513 process as part of this rule's rhs. */
1516 /* If we just passed an action, that action was in the middle
1517 of a rule, so make a dummy rule to reduce it to a
1521 /* Since the action was written out with this rule's
1522 number, we must give the new rule this number by
1523 inserting the new rule before it. */
1525 /* Make a dummy nonterminal, a gensym. */
1526 symbol_t
*sdummy
= gensym ();
1528 /* Make a new rule, whose body is empty, before the
1529 current one, so that the action just read can
1533 p
= symbol_list_new (sdummy
);
1534 /* Attach its lineno to that of the host rule. */
1535 p
->line
= crule
->line
;
1536 /* Move the action from the host rule to this one. */
1537 p
->action
= crule
->action
;
1538 p
->action_line
= crule
->action_line
;
1539 crule
->action
= NULL
;
1545 /* End of the rule. */
1546 crule1
= symbol_list_new (NULL
);
1547 crule1
->next
= crule
;
1551 /* Insert the dummy generated by that rule into this
1554 p
= symbol_list_new (sdummy
);
1561 if (t
== tok_identifier
)
1564 p
= symbol_list_new (symval
);
1568 else /* handle an action. */
1570 parse_action (crule
, rulelength
);
1572 xactions
++; /* JF */
1575 } /* end of read rhs of rule */
1577 /* Put an empty link in the list to mark the end of this rule */
1578 p
= symbol_list_new (NULL
);
1584 complain (_("two @prec's in a row"));
1586 crule
->ruleprec
= symval
;
1592 if (!semantic_parser
)
1593 complain (_("%%guard present but %%semantic_parser not specified"));
1595 parse_guard (crule
, rulelength
);
1599 if (t
== tok_left_curly
)
1601 /* This case never occurs -wjh */
1603 complain (_("two actions at end of one rule"));
1604 parse_action (crule
, rulelength
);
1606 xactions
++; /* -wjh */
1609 /* If $$ is being set in default way, report if any type
1612 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1614 if (lhs
->type_name
== 0
1615 || first_rhs
->type_name
== 0
1616 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1617 complain (_("type clash (`%s' `%s') on default action"),
1618 lhs
->type_name
? lhs
->type_name
: "",
1619 first_rhs
->type_name
? first_rhs
->type_name
: "");
1621 /* Warn if there is no default for $$ but we need one. */
1622 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1623 complain (_("empty rule for typed nonterminal, and no action"));
1624 if (t
== tok_two_percents
|| t
== tok_eof
)
1625 warn (_("previous rule lacks an ending `;'"));
1626 if (t
== tok_semicolon
)
1631 complain (_("invalid input: %s"), quote (token_buffer
));
1635 /* grammar has been read. Do some checking */
1638 fatal (_("no rules in the input grammar"));
1640 /* Report any undefined symbols and consider them nonterminals. */
1641 symbols_do (symbol_check_defined
, NULL
);
1643 /* Insert the initial rule, which line is that of the first rule
1644 (not that of the start symbol):
1646 axiom: %start EOF. */
1647 p
= symbol_list_new (axiom
);
1648 p
->line
= grammar
->line
;
1649 p
->next
= symbol_list_new (startval
);
1650 p
->next
->next
= symbol_list_new (eoftoken
);
1651 p
->next
->next
->next
= symbol_list_new (NULL
);
1652 p
->next
->next
->next
->next
= grammar
;
1658 if (nsyms
> MAXSHORT
)
1659 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1662 if (getenv ("DEBUG"))
1663 fprintf (stderr
, "nsyms == ntokens + nvars: %d == %d + %d\n",
1664 nsyms
, ntokens
, nvars
);
1665 assert (nsyms
== ntokens
+ nvars
);
1668 /* At the end of the grammar file, some C source code must
1669 be stored. It is going to be associated to the epilogue
1672 read_additionnal_code (void)
1675 struct obstack el_obstack
;
1677 obstack_init (&el_obstack
);
1681 obstack_fgrow2 (&el_obstack
, muscle_find ("linef"),
1682 lineno
, quotearg_style (c_quoting_style
,
1683 muscle_find ("filename")));
1686 while ((c
= getc (finput
)) != EOF
)
1687 obstack_1grow (&el_obstack
, c
);
1689 obstack_1grow (&el_obstack
, 0);
1690 muscle_insert ("epilogue", obstack_finish (&el_obstack
));
1694 /*------------------------------------------------------------------.
1695 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
1697 `------------------------------------------------------------------*/
1700 token_translations_init (void)
1702 int last_user_token_number
= 256;
1705 /* Set the user numbers. */
1706 for (i
= 0; i
< ntokens
; ++i
)
1708 symbol_t
*this = symbols
[i
];
1709 if (getenv ("DEBUG"))
1710 fprintf (stderr
, "UserVal %s = %d (val = %d)\n",
1711 this->tag
, this->user_token_number
, this->number
);
1712 if (this->user_token_number
== SUNDEF
)
1713 this->user_token_number
= ++last_user_token_number
;
1714 if (this->user_token_number
> max_user_token_number
)
1715 max_user_token_number
= this->user_token_number
;
1716 if (getenv ("DEBUG"))
1717 fprintf (stderr
, "Now: UserVal %s = %d (val = %d)\n",
1718 this->tag
, this->user_token_number
, this->number
);
1721 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1723 /* Initialize all entries for literal tokens to 2, the internal
1724 token number for $undefined., which represents all invalid
1726 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
1727 token_translations
[i
] = 2;
1729 symbols_do (symbol_translation
, NULL
);
1733 /*----------------------------------------------------------------.
1734 | Assign symbol numbers, and write definition of token names into |
1735 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
1736 `----------------------------------------------------------------*/
1741 symbols
= XCALLOC (symbol_t
*, nsyms
);
1743 symbols_do (symbol_check_alias_consistence
, NULL
);
1744 symbols_do (symbol_pack
, NULL
);
1746 token_translations_init ();
1748 error_token_number
= errtoken
->number
;
1750 if (startval
->class == unknown_sym
)
1751 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1752 else if (startval
->class == token_sym
)
1753 fatal (_("the start symbol %s is a token"), startval
->tag
);
1755 start_symbol
= startval
->number
;
1759 /*---------------------------------------------------------------.
1760 | Convert the rules into the representation using RRHS, RLHS and |
1762 `---------------------------------------------------------------*/
1771 /* We use short to index items. */
1772 if (nitems
>= MAXSHORT
)
1773 fatal (_("too many items (max %d)"), MAXSHORT
);
1775 ritem
= XCALLOC (short, nitems
+ 1);
1776 rules
= XCALLOC (rule_t
, nrules
) - 1;
1784 symbol_t
*ruleprec
= p
->ruleprec
;
1785 rules
[ruleno
].user_number
= ruleno
;
1786 rules
[ruleno
].number
= ruleno
;
1787 rules
[ruleno
].lhs
= p
->sym
;
1788 rules
[ruleno
].rhs
= ritem
+ itemno
;
1789 rules
[ruleno
].line
= p
->line
;
1790 rules
[ruleno
].useful
= TRUE
;
1791 rules
[ruleno
].action
= p
->action
;
1792 rules
[ruleno
].action_line
= p
->action_line
;
1793 rules
[ruleno
].guard
= p
->guard
;
1794 rules
[ruleno
].guard_line
= p
->guard_line
;
1799 ritem
[itemno
++] = p
->sym
->number
;
1800 /* A rule gets by default the precedence and associativity
1801 of the last token in it. */
1802 if (p
->sym
->class == token_sym
)
1803 rules
[ruleno
].prec
= p
->sym
;
1808 /* If this rule has a %prec,
1809 the specified symbol's precedence replaces the default. */
1812 rules
[ruleno
].precsym
= ruleprec
;
1813 rules
[ruleno
].prec
= ruleprec
;
1815 ritem
[itemno
++] = -ruleno
;
1824 assert (nritems
== nitems
);
1827 ritem_print (stderr
);
1830 /*-------------------------------------------------------------------.
1831 | Read in the grammar specification and record it in the format |
1832 | described in gram.h. All guards are copied into the GUARD_OBSTACK |
1833 | and all actions into ACTION_OBSTACK, in each case forming the body |
1834 | of a C function (YYGUARD or YYACTION) which contains a switch |
1835 | statement to decide which guard or action to execute. |
1836 `-------------------------------------------------------------------*/
1844 /* Initialize the muscle obstack. */
1845 obstack_init (&muscle_obstack
);
1847 /* Initialize the symbol table. */
1850 /* Construct the axiom symbol. */
1851 axiom
= getsym ("$axiom");
1852 axiom
->class = nterm_sym
;
1853 axiom
->number
= nvars
++;
1855 /* Construct the error token */
1856 errtoken
= getsym ("error");
1857 errtoken
->class = token_sym
;
1858 errtoken
->number
= ntokens
++;
1859 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1861 /* Construct a token that represents all undefined literal tokens.
1862 It is always token number 2. */
1863 undeftoken
= getsym ("$undefined.");
1864 undeftoken
->class = token_sym
;
1865 undeftoken
->number
= ntokens
++;
1866 undeftoken
->user_token_number
= 2;
1868 /* Initialize the obstacks. */
1869 obstack_init (&action_obstack
);
1870 obstack_init (&attrs_obstack
);
1871 obstack_init (&output_obstack
);
1873 finput
= xfopen (infile
, "r");
1875 /* Read the declaration section. Copy %{ ... %} groups to
1876 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1877 etc. found there. */
1878 read_declarations ();
1880 /* If the user did not define her EOFTOKEN, do it now. */
1883 eoftoken
= getsym ("$");
1884 eoftoken
->class = token_sym
;
1885 eoftoken
->number
= 0;
1886 /* Value specified by POSIX. */
1887 eoftoken
->user_token_number
= 0;
1890 /* Read in the grammar, build grammar in list form. Write out
1891 guards and actions. */
1893 /* Some C code is given at the end of the grammar file. */
1894 read_additionnal_code ();
1899 /* Assign the symbols their symbol numbers. Write #defines for the
1900 token symbols into FDEFINES if requested. */
1903 /* Convert the grammar into the format described in gram.h. */
1906 /* The grammar as a symbol_list is no longer needed. */
1907 LIST_FREE (symbol_list
, grammar
);
1915 /* Free the symbol table data structure. */