]>
git.saurik.com Git - bison.git/blob - src/reader.c
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;
55 /* Nonzero if components of semantic values are used, implying
56 they must be unions. */
57 static int value_components_used
;
59 /* Nonzero if %union has been seen. */
62 /* Incremented for each %left, %right or %nonassoc seen */
63 static int lastprec
= 0;
66 symbol_list_new (symbol_t
*sym
)
68 symbol_list
*res
= XMALLOC (symbol_list
, 1);
78 /*===================\
80 \===================*/
83 skip_to_char (int target
)
87 complain (_(" Skipping to next \\n"));
89 complain (_(" Skipping to next %c"), target
);
92 c
= skip_white_space ();
93 while (c
!= target
&& c
!= EOF
);
99 /*---------------------------------------------------------.
100 | Read a signed integer from STREAM and return its value. |
101 `---------------------------------------------------------*/
104 read_signed_integer (FILE *stream
)
106 int c
= getc (stream
);
118 n
= 10 * n
+ (c
- '0');
127 /*--------------------------------------------------------------.
128 | Get the data type (alternative in the union) of the value for |
129 | symbol N in rule RULE. |
130 `--------------------------------------------------------------*/
133 get_type_name (int n
, symbol_list
*rule
)
140 complain (_("invalid $ value"));
150 if (rp
== NULL
|| rp
->sym
== NULL
)
152 complain (_("invalid $ value"));
158 return rp
->sym
->type_name
;
161 /*------------------------------------------------------------------.
162 | Copy the character C to OOUT, and insert quadigraphs when needed. |
163 `------------------------------------------------------------------*/
166 copy_character (struct obstack
*oout
, int c
)
171 obstack_sgrow (oout
, "@<:@");
175 obstack_sgrow (oout
, "@:>@");
179 obstack_1grow (oout
, c
);
183 /*------------------------------------------------------------.
184 | Dump the string from FIN to OOUT if non null. MATCH is the |
185 | delimiter of the string (either ' or "). |
186 `------------------------------------------------------------*/
189 copy_string2 (FILE *fin
, struct obstack
*oout
, int match
, int store
)
194 obstack_1grow (oout
, match
);
201 fatal (_("unterminated string at end of file"));
204 complain (_("unterminated string"));
206 c
= match
; /* invent terminator */
210 copy_character (oout
, c
);
216 fatal (_("unterminated string at end of file"));
217 copy_character (oout
, c
);
227 obstack_1grow (oout
, c
);
233 copy_string (FILE *fin
, struct obstack
*oout
, int match
)
235 copy_string2 (fin
, oout
, match
, 1);
241 copy_identifier (FILE *fin
, struct obstack
*oout
)
245 while (isalnum (c
= getc (fin
)) || c
== '_')
246 obstack_1grow (oout
, c
);
252 /*------------------------------------------------------------------.
253 | Dump the wannabee comment from IN to OOUT. In fact we just saw a |
254 | `/', which might or might not be a comment. In any case, copy |
256 `------------------------------------------------------------------*/
259 copy_comment (FILE *fin
, struct obstack
*oout
)
265 /* We read a `/', output it. */
266 obstack_1grow (oout
, '/');
268 switch ((c
= getc (fin
)))
281 obstack_1grow (oout
, c
);
287 if (!cplus_comment
&& c
== '*')
291 obstack_1grow (oout
, c
);
297 obstack_1grow (oout
, c
);
304 obstack_1grow (oout
, c
);
311 fatal (_("unterminated comment"));
314 copy_character (oout
, c
);
321 /*-------------------------------------------------------------------.
322 | FIN is pointing to a location (i.e., a `@'). Output to OOUT a |
323 | reference to this location. RULE_LENGTH is the number of values in |
324 | the current rule so far, which says where to find `$0' with |
325 | respect to the top of the stack. |
326 `-------------------------------------------------------------------*/
329 copy_at (FILE *fin
, struct obstack
*oout
, int rule_length
)
336 obstack_sgrow (oout
, "]b4_lhs_location[");
338 else if (isdigit (c
) || c
== '-')
343 n
= read_signed_integer (fin
);
345 complain (_("invalid value: %s%d"), "@", n
);
347 obstack_fgrow2 (oout
, "]b4_rhs_location([%d], [%d])[",
354 complain (_("%s is invalid"), quote (buf
));
359 /*------------------------------------------------------------------.
360 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
362 | Possible inputs: $[<TYPENAME>]($|integer) |
364 | Output to OOUT a reference to this semantic value. RULE_LENGTH is |
365 | the number of values in the current rule so far, which says where |
366 | to find `$0' with respect to the top of the stack. |
367 `------------------------------------------------------------------*/
370 copy_dollar (FILE *fin
, struct obstack
*oout
,
371 symbol_list
*rule
, int rule_length
)
374 const char *type_name
= NULL
;
376 /* Get the type name if explicit. */
379 read_type_name (fin
);
380 type_name
= token_buffer
;
381 value_components_used
= 1;
388 type_name
= get_type_name (0, rule
);
389 if (!type_name
&& typed
)
390 complain (_("$$ of `%s' has no declared type"),
394 obstack_fgrow1 (oout
,
395 "]b4_lhs_value([%s])[", type_name
);
397 else if (isdigit (c
) || c
== '-')
401 n
= read_signed_integer (fin
);
404 complain (_("invalid value: %s%d"), "$", n
);
407 if (!type_name
&& n
> 0)
408 type_name
= get_type_name (n
, rule
);
409 if (!type_name
&& typed
)
410 complain (_("$%d of `%s' has no declared type"),
414 obstack_fgrow3 (oout
, "]b4_rhs_value([%d], [%d], [%s])[",
415 rule_length
, n
, type_name
);
422 complain (_("%s is invalid"), quote (buf
));
426 /*-------------------------------------------------------------------.
427 | Copy the contents of a `%{ ... %}' into the definitions file. The |
428 | `%{' has already been read. Return after reading the `%}'. |
429 `-------------------------------------------------------------------*/
432 copy_definition (struct obstack
*oout
)
435 /* -1 while reading a character if prev char was %. */
440 obstack_fgrow2 (oout
, muscle_find ("linef"),
441 lineno
, quotearg_style (c_quoting_style
,
442 muscle_find ("filename")));
454 obstack_1grow (oout
, c
);
464 copy_string (finput
, oout
, c
);
468 copy_comment (finput
, oout
);
472 fatal ("%s", _("unterminated `%{' definition"));
475 copy_character (oout
, c
);
484 obstack_1grow (oout
, '%');
491 /*-------------------------------------------------------------------.
492 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
493 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
495 `-------------------------------------------------------------------*/
498 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
500 token_t token
= tok_undef
;
501 char *typename
= NULL
;
503 /* The symbol being defined. */
504 symbol_t
*symbol
= NULL
;
506 /* After `%token' and `%nterm', any number of symbols maybe be
510 int tmp_char
= ungetc (skip_white_space (), finput
);
512 /* `%' (for instance from `%token', or from `%%' etc.) is the
513 only valid means to end this declaration. */
517 fatal (_("Premature EOF after %s"), token_buffer
);
520 if (token
== tok_comma
)
525 if (token
== tok_typename
)
527 typename
= xstrdup (token_buffer
);
528 value_components_used
= 1;
531 else if (token
== tok_identifier
&& *symval
->tag
== '\"' && symbol
)
533 symbol_make_alias (symbol
, symval
, typename
);
536 else if (token
== tok_identifier
)
538 int oldclass
= symval
->class;
541 if (symbol
->class == what_is_not
)
542 complain (_("symbol %s redefined"), symbol
->tag
);
543 symbol
->class = what_is
;
544 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
545 symbol
->number
= nvars
++;
546 if (what_is
== token_sym
&& symbol
->number
== NUMBER_UNDEFINED
)
547 symbol
->number
= ntokens
++;
551 if (symbol
->type_name
== NULL
)
552 symbol
->type_name
= typename
;
553 else if (strcmp (typename
, symbol
->type_name
) != 0)
554 complain (_("type redeclaration for %s"), symbol
->tag
);
557 else if (symbol
&& token
== tok_number
)
559 symbol
->user_token_number
= numval
;
560 /* User defined EOF token? */
564 eoftoken
->number
= 0;
565 /* It is always mapped to 0, so it was already counted in
572 complain (_("`%s' is invalid in %s"),
574 (what_is
== token_sym
) ? "%token" : "%nterm");
582 /*------------------------------.
583 | Parse what comes after %start |
584 `------------------------------*/
587 parse_start_decl (void)
590 complain (_("multiple %s declarations"), "%start");
591 if (lex () != tok_identifier
)
592 complain (_("invalid %s declaration"), "%start");
596 startsymbol
= symval
;
600 /*-----------------------------------------------------------.
601 | read in a %type declaration and record its information for |
602 | get_type_name to access |
603 `-----------------------------------------------------------*/
606 parse_type_decl (void)
610 if (lex () != tok_typename
)
612 complain ("%s", _("%type declaration has no <typename>"));
617 name
= xstrdup (token_buffer
);
622 int tmp_char
= ungetc (skip_white_space (), finput
);
627 fatal (_("Premature EOF after %s"), token_buffer
);
639 if (symval
->type_name
== NULL
)
640 symval
->type_name
= name
;
641 else if (strcmp (name
, symval
->type_name
) != 0)
642 complain (_("type redeclaration for %s"), symval
->tag
);
647 complain (_("invalid %%type declaration due to item: %s"),
656 /*----------------------------------------------------------------.
657 | Read in a %left, %right or %nonassoc declaration and record its |
659 `----------------------------------------------------------------*/
662 parse_assoc_decl (associativity assoc
)
667 /* Assign a new precedence level, never 0. */
673 int tmp_char
= ungetc (skip_white_space (), finput
);
678 fatal (_("Premature EOF after %s"), token_buffer
);
685 name
= xstrdup (token_buffer
);
692 if (symval
->prec
!= 0)
693 complain (_("redefining precedence of %s"), symval
->tag
);
694 symval
->prec
= lastprec
;
695 symval
->assoc
= assoc
;
696 if (symval
->class == nterm_sym
)
697 complain (_("symbol %s redefined"), symval
->tag
);
698 if (symval
->number
== NUMBER_UNDEFINED
)
700 symval
->number
= ntokens
++;
701 symval
->class = token_sym
;
704 { /* record the type, if one is specified */
705 if (symval
->type_name
== NULL
)
706 symval
->type_name
= name
;
707 else if (strcmp (name
, symval
->type_name
) != 0)
708 complain (_("type redeclaration for %s"), symval
->tag
);
713 if (prev
== tok_identifier
)
715 symval
->user_token_number
= numval
;
720 (_("invalid text (%s) - number should be after identifier"),
730 complain (_("unexpected item: %s"), token_buffer
);
740 /*--------------------------------------------------------------.
741 | Copy the union declaration into the stype muscle |
742 | (and fdefines), where it is made into the definition of |
743 | YYSTYPE, the type of elements of the parser value stack. |
744 `--------------------------------------------------------------*/
747 parse_union_decl (void)
752 struct obstack union_obstack
;
754 complain (_("multiple %s declarations"), "%union");
758 MUSCLE_INSERT_INT ("stype_line", lineno
);
759 obstack_init (&union_obstack
);
760 obstack_sgrow (&union_obstack
, "union");
766 /* If C contains '/', it is output by copy_comment (). */
768 obstack_1grow (&union_obstack
, c
);
777 copy_comment (finput
, &union_obstack
);
785 /* FIXME: Errr. How could this happen???. --akim */
787 complain (_("unmatched %s"), "`}'");
795 /* JF don't choke on trailing semi */
796 c
= skip_white_space ();
799 obstack_1grow (&union_obstack
, 0);
800 muscle_insert ("stype", obstack_finish (&union_obstack
));
804 /*-------------------------------------------------------.
805 | Parse the declaration %expect N which says to expect N |
806 | shift-reduce conflicts. |
807 `-------------------------------------------------------*/
810 parse_expect_decl (void)
812 int c
= skip_white_space ();
816 complain (_("argument of %%expect is not an integer"));
818 expected_conflicts
= read_signed_integer (finput
);
822 /*-------------------------------------------------------------------.
823 | Parse what comes after %thong. the full syntax is |
825 | %thong <type> token number literal |
827 | the <type> or number may be omitted. The number specifies the |
828 | user_token_number. |
830 | Two symbols are entered in the table, one for the token symbol and |
831 | one for the literal. Both are given the <type>, if any, from the |
832 | declaration. The ->user_token_number of the first is |
833 | USER_NUMBER_ALIAS and the ->user_token_number of the second is set |
834 | to the number, if any, from the declaration. The two symbols are |
835 | linked via pointers in their ->alias fields. |
837 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
838 | only the literal string is retained it is the literal string that |
839 | is output to yytname |
840 `-------------------------------------------------------------------*/
843 parse_thong_decl (void)
848 int usrtoknum
= USER_NUMBER_UNDEFINED
;
850 token
= lex (); /* fetch typename or first token */
851 if (token
== tok_typename
)
853 typename
= xstrdup (token_buffer
);
854 value_components_used
= 1;
855 token
= lex (); /* fetch first token */
858 /* process first token */
860 if (token
!= tok_identifier
)
862 complain (_("unrecognized item %s, expected an identifier"),
867 symval
->class = token_sym
;
868 symval
->type_name
= typename
;
869 symval
->user_token_number
= USER_NUMBER_ALIAS
;
872 token
= lex (); /* get number or literal string */
874 if (token
== tok_number
)
877 token
= lex (); /* okay, did number, now get literal */
880 /* process literal string token */
882 if (token
!= tok_identifier
|| *symval
->tag
!= '\"')
884 complain (_("expected string constant instead of %s"), token_buffer
);
888 symval
->class = token_sym
;
889 symval
->type_name
= typename
;
890 symval
->user_token_number
= usrtoknum
;
892 symval
->alias
= symbol
;
893 symbol
->alias
= symval
;
895 /* symbol and symval combined are only one symbol. */
901 parse_muscle_decl (void)
903 int ch
= ungetc (skip_white_space (), finput
);
908 if (!isalpha (ch
) && ch
!= '_')
910 complain (_("invalid %s declaration"), "%define");
914 copy_identifier (finput
, &muscle_obstack
);
915 obstack_1grow (&muscle_obstack
, 0);
916 muscle_key
= obstack_finish (&muscle_obstack
);
919 ch
= skip_white_space ();
925 complain (_("invalid %s declaration"), "%define");
930 fatal (_("Premature EOF after %s"), "\"");
932 copy_string2 (finput
, &muscle_obstack
, '"', 0);
933 obstack_1grow (&muscle_obstack
, 0);
934 muscle_value
= obstack_finish (&muscle_obstack
);
936 /* Store the (key, value) pair in the environment. */
937 muscle_insert (muscle_key
, muscle_value
);
942 /*---------------------------------.
943 | Parse a double quoted parameter. |
944 `---------------------------------*/
947 parse_dquoted_param (const char *from
)
949 struct obstack param_obstack
;
950 const char *param
= NULL
;
953 obstack_init (¶m_obstack
);
954 c
= skip_white_space ();
958 complain (_("invalid %s declaration"), from
);
964 while ((c
= literalchar ()) != '"')
965 obstack_1grow (¶m_obstack
, c
);
967 obstack_1grow (¶m_obstack
, '\0');
968 param
= obstack_finish (¶m_obstack
);
970 if (c
!= '"' || strlen (param
) == 0)
972 complain (_("invalid %s declaration"), from
);
982 /*----------------------------------.
983 | Parse what comes after %skeleton. |
984 `----------------------------------*/
987 parse_skel_decl (void)
989 skeleton
= parse_dquoted_param ("%skeleton");
992 /*----------------------------------------------------------------.
993 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
994 | any `%' declarations, and copy the contents of any `%{ ... %}' |
995 | groups to PRE_PROLOGUE_OBSTACK or POST_PROLOGUE_OBSTACK. |
996 `----------------------------------------------------------------*/
999 read_declarations (void)
1003 int c
= skip_white_space ();
1007 token_t tok
= parse_percent_token ();
1011 case tok_two_percents
:
1014 case tok_percent_left_curly
:
1016 copy_definition (&pre_prologue_obstack
);
1018 copy_definition (&post_prologue_obstack
);
1022 parse_token_decl (token_sym
, nterm_sym
);
1026 parse_token_decl (nterm_sym
, token_sym
);
1034 parse_start_decl ();
1038 parse_union_decl ();
1042 parse_expect_decl ();
1046 parse_thong_decl ();
1050 parse_assoc_decl (left_assoc
);
1054 parse_assoc_decl (right_assoc
);
1058 parse_assoc_decl (non_assoc
);
1062 parse_muscle_decl ();
1080 complain (_("unrecognized: %s"), token_buffer
);
1085 fatal (_("no input grammar"));
1090 complain (_("unknown character: %s"), quote (buf
));
1096 /*------------------------------------------------------------------.
1097 | Assuming that a `{' has just been seen, copy everything up to the |
1098 | matching `}' into ACTION_OBSTACK. |
1100 | RULE_LENGTH is the number of values in the current rule so far, |
1101 | which says where to find `$0' with respect to the top of the |
1102 | stack. It is not the same as the rule->length in the case of mid |
1105 | This routine is used for actions. |
1106 `------------------------------------------------------------------*/
1109 parse_action (symbol_list
*rule
, int rule_length
)
1112 rule
->action_line
= lineno
;
1116 while ((c
= getc (finput
)) != '}')
1120 copy_character (&action_obstack
, c
);
1125 copy_character (&action_obstack
, c
);
1131 copy_string (finput
, &action_obstack
, c
);
1135 copy_comment (finput
, &action_obstack
);
1139 copy_dollar (finput
, &action_obstack
, rule
, rule_length
);
1143 copy_at (finput
, &action_obstack
, rule_length
);
1147 fatal (_("unmatched %s"), "`{'");
1150 copy_character (&action_obstack
, c
);
1153 /* Above loop exits when C is '}'. */
1155 copy_character (&action_obstack
, c
);
1158 obstack_1grow (&action_obstack
, '\0');
1159 rule
->action
= obstack_finish (&action_obstack
);
1164 /*-------------------------------------------------------------------.
1165 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1166 | with the user's names. |
1167 `-------------------------------------------------------------------*/
1172 /* Incremented for each generated symbol */
1173 static int gensym_count
= 0;
1174 static char buf
[256];
1178 sprintf (buf
, "@%d", ++gensym_count
);
1180 sym
= getsym (token_buffer
);
1181 sym
->class = nterm_sym
;
1182 sym
->number
= nvars
++;
1186 /*-------------------------------------------------------------------.
1187 | Parse the input grammar into a one symbol_list structure. Each |
1188 | rule is represented by a sequence of symbols: the left hand side |
1189 | followed by the contents of the right hand side, followed by a |
1190 | null pointer instead of a symbol to terminate the rule. The next |
1191 | symbol is the lhs of the following rule. |
1193 | All actions are copied out, labelled by the rule number they apply |
1196 | Bison used to allow some %directives in the rules sections, but |
1197 | this is no longer consider appropriate: (i) the documented grammar |
1198 | doesn't claim it, (ii), it would promote bad style, (iii), error |
1199 | recovery for %directives consists in skipping the junk until a `%' |
1200 | is seen and helrp synchronizing. This scheme is definitely wrong |
1201 | in the rules section. |
1202 `-------------------------------------------------------------------*/
1208 symbol_t
*lhs
= NULL
;
1209 symbol_list
*p
= NULL
;
1210 symbol_list
*p1
= NULL
;
1212 /* Points to first symbol_list of current rule. its symbol is the
1214 symbol_list
*crule
= NULL
;
1215 /* Points to the symbol_list preceding crule. */
1216 symbol_list
*crule1
= NULL
;
1220 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1221 if (t
== tok_identifier
|| t
== tok_bar
)
1223 int action_flag
= 0;
1224 /* Number of symbols in rhs of this rule so far */
1226 int xactions
= 0; /* JF for error checking */
1227 symbol_t
*first_rhs
= 0;
1229 if (t
== tok_identifier
)
1242 complain (_("ill-formed rule: initial symbol not followed by colon"));
1247 if (nrules
== 0 && t
== tok_bar
)
1249 complain (_("grammar starts with vertical bar"));
1250 lhs
= symval
; /* BOGUS: use a random symval */
1252 /* start a new rule and record its lhs. */
1257 p
= symbol_list_new (lhs
);
1268 /* mark the rule's lhs as a nonterminal if not already so. */
1270 if (lhs
->class == unknown_sym
)
1272 lhs
->class = nterm_sym
;
1273 lhs
->number
= nvars
;
1276 else if (lhs
->class == token_sym
)
1277 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1279 /* read the rhs of the rule. */
1287 crule
->ruleprec
= symval
;
1291 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1294 /* If next token is an identifier, see if a colon follows it.
1295 If one does, exit this rule now. */
1296 if (t
== tok_identifier
)
1305 if (t1
== tok_colon
)
1307 warn (_("previous rule lacks an ending `;'"));
1311 if (!first_rhs
) /* JF */
1313 /* Not followed by colon =>
1314 process as part of this rule's rhs. */
1317 /* If we just passed an action, that action was in the middle
1318 of a rule, so make a dummy rule to reduce it to a
1322 /* Since the action was written out with this rule's
1323 number, we must give the new rule this number by
1324 inserting the new rule before it. */
1326 /* Make a dummy nonterminal, a gensym. */
1327 symbol_t
*sdummy
= gensym ();
1329 /* Make a new rule, whose body is empty, before the
1330 current one, so that the action just read can
1334 p
= symbol_list_new (sdummy
);
1335 /* Attach its lineno to that of the host rule. */
1336 p
->line
= crule
->line
;
1337 /* Move the action from the host rule to this one. */
1338 p
->action
= crule
->action
;
1339 p
->action_line
= crule
->action_line
;
1340 crule
->action
= NULL
;
1346 /* End of the rule. */
1347 crule1
= symbol_list_new (NULL
);
1348 crule1
->next
= crule
;
1352 /* Insert the dummy generated by that rule into this
1355 p
= symbol_list_new (sdummy
);
1362 if (t
== tok_identifier
)
1365 p
= symbol_list_new (symval
);
1369 else /* handle an action. */
1371 parse_action (crule
, rulelength
);
1373 ++xactions
; /* JF */
1376 } /* end of read rhs of rule */
1378 /* Put an empty link in the list to mark the end of this rule */
1379 p
= symbol_list_new (NULL
);
1385 complain (_("two @prec's in a row"));
1387 crule
->ruleprec
= symval
;
1391 if (t
== tok_left_curly
)
1393 /* This case never occurs -wjh */
1395 complain (_("two actions at end of one rule"));
1396 parse_action (crule
, rulelength
);
1398 ++xactions
; /* -wjh */
1401 /* If $$ is being set in default way, report if any type
1404 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1406 if (lhs
->type_name
== 0
1407 || first_rhs
->type_name
== 0
1408 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1409 complain (_("type clash (`%s' `%s') on default action"),
1410 lhs
->type_name
? lhs
->type_name
: "",
1411 first_rhs
->type_name
? first_rhs
->type_name
: "");
1413 /* Warn if there is no default for $$ but we need one. */
1414 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1415 complain (_("empty rule for typed nonterminal, and no action"));
1416 if (t
== tok_two_percents
|| t
== tok_eof
)
1417 warn (_("previous rule lacks an ending `;'"));
1418 if (t
== tok_semicolon
)
1423 complain (_("invalid input: %s"), quote (token_buffer
));
1427 /* grammar has been read. Do some checking */
1430 fatal (_("no rules in the input grammar"));
1432 /* Report any undefined symbols and consider them nonterminals. */
1433 symbols_check_defined ();
1435 /* Insert the initial rule, which line is that of the first rule
1436 (not that of the start symbol):
1438 axiom: %start EOF. */
1439 p
= symbol_list_new (axiom
);
1440 p
->line
= grammar
->line
;
1441 p
->next
= symbol_list_new (startsymbol
);
1442 p
->next
->next
= symbol_list_new (eoftoken
);
1443 p
->next
->next
->next
= symbol_list_new (NULL
);
1444 p
->next
->next
->next
->next
= grammar
;
1449 if (nsyms
> SHRT_MAX
)
1450 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1453 assert (nsyms
== ntokens
+ nvars
);
1456 /* At the end of the grammar file, some C source code must
1457 be stored. It is going to be associated to the epilogue
1460 read_additionnal_code (void)
1463 struct obstack el_obstack
;
1465 obstack_init (&el_obstack
);
1469 obstack_fgrow2 (&el_obstack
, muscle_find ("linef"),
1470 lineno
, quotearg_style (c_quoting_style
,
1471 muscle_find ("filename")));
1474 while ((c
= getc (finput
)) != EOF
)
1475 copy_character (&el_obstack
, c
);
1477 obstack_1grow (&el_obstack
, 0);
1478 muscle_insert ("epilogue", obstack_finish (&el_obstack
));
1482 /*---------------------------------------------------------------.
1483 | Convert the rules into the representation using RRHS, RLHS and |
1485 `---------------------------------------------------------------*/
1490 unsigned int itemno
;
1494 ritem
= XCALLOC (item_number_t
, nritems
);
1495 rules
= XCALLOC (rule_t
, nrules
) - 1;
1503 symbol_t
*ruleprec
= p
->ruleprec
;
1504 rules
[ruleno
].user_number
= ruleno
;
1505 rules
[ruleno
].number
= ruleno
;
1506 rules
[ruleno
].lhs
= p
->sym
;
1507 rules
[ruleno
].rhs
= ritem
+ itemno
;
1508 rules
[ruleno
].line
= p
->line
;
1509 rules
[ruleno
].useful
= TRUE
;
1510 rules
[ruleno
].action
= p
->action
;
1511 rules
[ruleno
].action_line
= p
->action_line
;
1516 /* item_number_t = symbol_number_t.
1517 But the former needs to contain more: negative rule numbers. */
1518 ritem
[itemno
++] = symbol_number_as_item_number (p
->sym
->number
);
1519 /* A rule gets by default the precedence and associativity
1520 of the last token in it. */
1521 if (p
->sym
->class == token_sym
)
1522 rules
[ruleno
].prec
= p
->sym
;
1527 /* If this rule has a %prec,
1528 the specified symbol's precedence replaces the default. */
1531 rules
[ruleno
].precsym
= ruleprec
;
1532 rules
[ruleno
].prec
= ruleprec
;
1534 ritem
[itemno
++] = -ruleno
;
1541 assert (itemno
== nritems
);
1544 ritem_print (stderr
);
1547 /*------------------------------------------------------------------.
1548 | Read in the grammar specification and record it in the format |
1549 | described in gram.h. All actions are copied into ACTION_OBSTACK, |
1550 | in each case forming the body of a C function (YYACTION) which |
1551 | contains a switch statement to decide which action to execute. |
1552 `------------------------------------------------------------------*/
1560 /* Initialize the muscle obstack. */
1561 obstack_init (&muscle_obstack
);
1563 /* Initialize the symbol table. */
1566 /* Construct the axiom symbol. */
1567 axiom
= getsym ("$axiom");
1568 axiom
->class = nterm_sym
;
1569 axiom
->number
= nvars
++;
1571 /* Construct the error token */
1572 errtoken
= getsym ("error");
1573 errtoken
->class = token_sym
;
1574 errtoken
->number
= ntokens
++;
1576 /* Construct a token that represents all undefined literal tokens.
1577 It is always token number 2. */
1578 undeftoken
= getsym ("$undefined.");
1579 undeftoken
->class = token_sym
;
1580 undeftoken
->number
= ntokens
++;
1582 /* Initialize the obstacks. */
1583 obstack_init (&action_obstack
);
1584 obstack_init (&output_obstack
);
1585 obstack_init (&pre_prologue_obstack
);
1586 obstack_init (&post_prologue_obstack
);
1588 finput
= xfopen (infile
, "r");
1590 /* Read the declaration section. Copy %{ ... %} groups to
1591 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1592 etc. found there. */
1593 read_declarations ();
1595 /* If the user did not define her EOFTOKEN, do it now. */
1598 eoftoken
= getsym ("$");
1599 eoftoken
->class = token_sym
;
1600 eoftoken
->number
= 0;
1601 /* Value specified by POSIX. */
1602 eoftoken
->user_token_number
= 0;
1605 /* Read in the grammar, build grammar in list form. Write out
1608 /* Some C code is given at the end of the grammar file. */
1609 read_additionnal_code ();
1614 /* Assign the symbols their symbol numbers. Write #defines for the
1615 token symbols into FDEFINES if requested. */
1618 /* Convert the grammar into the format described in gram.h. */
1621 /* The grammar as a symbol_list is no longer needed. */
1622 LIST_FREE (symbol_list
, grammar
);