]>
git.saurik.com Git - bison.git/blob - src/reader.c
1 /* Input parser for bison
2 Copyright 1984, 1986, 1989, 1992, 1998, 2000, 2001
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. */
57 static symbol_list
*grammar
;
58 static int start_flag
;
59 static bucket
*startval
;
61 /* Nonzero if components of semantic values are used, implying
62 they must be unions. */
63 static int value_components_used
;
65 /* Nonzero if %union has been seen. */
68 /* Incremented for each %left, %right or %nonassoc seen */
71 static bucket
*errtoken
;
72 static bucket
*undeftoken
;
76 symbol_list_new (bucket
*sym
)
78 symbol_list
*res
= XMALLOC (symbol_list
, 1);
92 /*===================\
94 \===================*/
97 skip_to_char (int target
)
101 complain (_(" Skipping to next \\n"));
103 complain (_(" Skipping to next %c"), target
);
106 c
= skip_white_space ();
107 while (c
!= target
&& c
!= EOF
);
113 /*---------------------------------------------------------.
114 | Read a signed integer from STREAM and return its value. |
115 `---------------------------------------------------------*/
118 read_signed_integer (FILE *stream
)
120 int c
= getc (stream
);
132 n
= 10 * n
+ (c
- '0');
141 /*--------------------------------------------------------------.
142 | Get the data type (alternative in the union) of the value for |
143 | symbol N in rule RULE. |
144 `--------------------------------------------------------------*/
147 get_type_name (int n
, symbol_list
*rule
)
154 complain (_("invalid $ value"));
164 if (rp
== NULL
|| rp
->sym
== NULL
)
166 complain (_("invalid $ value"));
172 return rp
->sym
->type_name
;
175 /*------------------------------------------------------------.
176 | Dump the string from FIN to OOUT if non null. MATCH is the |
177 | delimiter of the string (either ' or "). |
178 `------------------------------------------------------------*/
181 copy_string2 (FILE *fin
, struct obstack
*oout
, int match
, int store
)
186 obstack_1grow (oout
, match
);
193 fatal (_("unterminated string at end of file"));
196 complain (_("unterminated string"));
198 c
= match
; /* invent terminator */
202 obstack_1grow (oout
, c
);
208 fatal (_("unterminated string at end of file"));
209 obstack_1grow (oout
, c
);
219 obstack_1grow (oout
, c
);
225 copy_string (FILE *fin
, struct obstack
*oout
, int match
)
227 copy_string2 (fin
, oout
, match
, 1);
233 copy_identifier (FILE *fin
, struct obstack
*oout
)
237 while (isalnum (c
= getc (fin
)) || c
== '_')
238 obstack_1grow (oout
, c
);
244 /*------------------------------------------------------------------.
245 | Dump the wannabee comment from IN to OOUT. In fact we just saw a |
246 | `/', which might or might not be a comment. In any case, copy |
248 `------------------------------------------------------------------*/
251 copy_comment (FILE *fin
, struct obstack
*oout
)
257 /* We read a `/', output it. */
258 obstack_1grow (oout
, '/');
260 switch ((c
= getc (fin
)))
273 obstack_1grow (oout
, c
);
279 if (!cplus_comment
&& c
== '*')
283 obstack_1grow (oout
, c
);
289 obstack_1grow (oout
, c
);
296 obstack_1grow (oout
, c
);
303 fatal (_("unterminated comment"));
306 obstack_1grow (oout
, c
);
313 /*-----------------------------------------------------------------.
314 | FIN is pointing to a location (i.e., a `@'). Output to OOUT a |
315 | reference to this location. STACK_OFFSET is the number of values |
316 | in the current rule so far, which says where to find `$0' with |
317 | respect to the top of the stack. |
318 `-----------------------------------------------------------------*/
321 copy_at (FILE *fin
, struct obstack
*oout
, int stack_offset
)
328 obstack_sgrow (oout
, "yyloc");
331 else if (isdigit (c
) || c
== '-')
336 n
= read_signed_integer (fin
);
338 obstack_fgrow1 (oout
, "yylsp[%d]", n
- stack_offset
);
345 complain (_("%s is invalid"), quote (buf
));
350 /*-------------------------------------------------------------------.
351 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
353 | Possible inputs: $[<TYPENAME>]($|integer) |
355 | Output to OOUT a reference to this semantic value. STACK_OFFSET is |
356 | the number of values in the current rule so far, which says where |
357 | to find `$0' with respect to the top of the stack. |
358 `-------------------------------------------------------------------*/
361 copy_dollar (FILE *fin
, struct obstack
*oout
,
362 symbol_list
*rule
, int stack_offset
)
365 const char *type_name
= NULL
;
367 /* Get the type name if explicit. */
370 read_type_name (fin
);
371 type_name
= token_buffer
;
372 value_components_used
= 1;
378 obstack_sgrow (oout
, "yyval");
381 type_name
= get_type_name (0, rule
);
383 obstack_fgrow1 (oout
, ".%s", type_name
);
384 if (!type_name
&& typed
)
385 complain (_("$$ of `%s' has no declared type"),
388 else if (isdigit (c
) || c
== '-')
392 n
= read_signed_integer (fin
);
394 if (!type_name
&& n
> 0)
395 type_name
= get_type_name (n
, rule
);
397 obstack_fgrow1 (oout
, "yyvsp[%d]", n
- stack_offset
);
400 obstack_fgrow1 (oout
, ".%s", type_name
);
401 if (!type_name
&& typed
)
402 complain (_("$%d of `%s' has no declared type"),
409 complain (_("%s is invalid"), quote (buf
));
413 /*-------------------------------------------------------------------.
414 | Copy the contents of a `%{ ... %}' into the definitions file. The |
415 | `%{' has already been read. Return after reading the `%}'. |
416 `-------------------------------------------------------------------*/
419 copy_definition (void)
422 /* -1 while reading a character if prev char was %. */
427 obstack_fgrow2 (&attrs_obstack
, muscle_find ("linef"),
428 lineno
, quotearg_style (c_quoting_style
,
429 muscle_find("filename")));
441 obstack_1grow (&attrs_obstack
, c
);
451 copy_string (finput
, &attrs_obstack
, c
);
455 copy_comment (finput
, &attrs_obstack
);
459 fatal ("%s", _("unterminated `%{' definition"));
462 obstack_1grow (&attrs_obstack
, c
);
471 obstack_1grow (&attrs_obstack
, '%');
478 /*-------------------------------------------------------------------.
479 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
480 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
482 `-------------------------------------------------------------------*/
485 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
487 token_t token
= tok_undef
;
488 char *typename
= NULL
;
490 /* The symbol being defined. */
491 struct bucket
*symbol
= NULL
;
493 /* After `%token' and `%nterm', any number of symbols maybe be
497 int tmp_char
= ungetc (skip_white_space (), finput
);
499 /* `%' (for instance from `%token', or from `%%' etc.) is the
500 only valid means to end this declaration. */
504 fatal (_("Premature EOF after %s"), token_buffer
);
507 if (token
== tok_comma
)
512 if (token
== tok_typename
)
514 typename
= xstrdup (token_buffer
);
515 value_components_used
= 1;
518 else if (token
== tok_identifier
&& *symval
->tag
== '\"' && symbol
)
521 warn (_("symbol `%s' used more than once as a literal string"),
523 else if (symbol
->alias
)
524 warn (_("symbol `%s' given more than one literal string"),
528 symval
->class = token_sym
;
529 symval
->type_name
= typename
;
530 symval
->user_token_number
= symbol
->user_token_number
;
531 symbol
->user_token_number
= SALIAS
;
532 symval
->alias
= symbol
;
533 symbol
->alias
= symval
;
534 /* symbol and symval combined are only one symbol */
539 else if (token
== tok_identifier
)
541 int oldclass
= symval
->class;
544 if (symbol
->class == what_is_not
)
545 complain (_("symbol %s redefined"), symbol
->tag
);
546 symbol
->class = what_is
;
547 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
548 symbol
->value
= nvars
++;
552 if (symbol
->type_name
== NULL
)
553 symbol
->type_name
= typename
;
554 else if (strcmp (typename
, symbol
->type_name
) != 0)
555 complain (_("type redeclaration for %s"), symbol
->tag
);
558 else if (symbol
&& token
== tok_number
)
560 symbol
->user_token_number
= numval
;
564 complain (_("`%s' is invalid in %s"),
566 (what_is
== token_sym
) ? "%token" : "%nterm");
574 /*------------------------------.
575 | Parse what comes after %start |
576 `------------------------------*/
579 parse_start_decl (void)
582 complain (_("multiple %s declarations"), "%start");
583 if (lex () != tok_identifier
)
584 complain (_("invalid %s declaration"), "%start");
592 /*-----------------------------------------------------------.
593 | read in a %type declaration and record its information for |
594 | get_type_name to access |
595 `-----------------------------------------------------------*/
598 parse_type_decl (void)
602 if (lex () != tok_typename
)
604 complain ("%s", _("%type declaration has no <typename>"));
609 name
= xstrdup (token_buffer
);
614 int tmp_char
= ungetc (skip_white_space (), finput
);
619 fatal (_("Premature EOF after %s"), token_buffer
);
631 if (symval
->type_name
== NULL
)
632 symval
->type_name
= name
;
633 else if (strcmp (name
, symval
->type_name
) != 0)
634 complain (_("type redeclaration for %s"), symval
->tag
);
639 complain (_("invalid %%type declaration due to item: %s"),
648 /*----------------------------------------------------------------.
649 | Read in a %left, %right or %nonassoc declaration and record its |
651 `----------------------------------------------------------------*/
654 parse_assoc_decl (associativity assoc
)
659 lastprec
++; /* Assign a new precedence level, never 0. */
664 int tmp_char
= ungetc (skip_white_space (), finput
);
669 fatal (_("Premature EOF after %s"), token_buffer
);
676 name
= xstrdup (token_buffer
);
683 if (symval
->prec
!= 0)
684 complain (_("redefining precedence of %s"), symval
->tag
);
685 symval
->prec
= lastprec
;
686 symval
->assoc
= assoc
;
687 if (symval
->class == nterm_sym
)
688 complain (_("symbol %s redefined"), symval
->tag
);
689 symval
->class = token_sym
;
691 { /* record the type, if one is specified */
692 if (symval
->type_name
== NULL
)
693 symval
->type_name
= name
;
694 else if (strcmp (name
, symval
->type_name
) != 0)
695 complain (_("type redeclaration for %s"), symval
->tag
);
700 if (prev
== tok_identifier
)
702 symval
->user_token_number
= numval
;
707 ("invalid text (%s) - number should be after identifier"),
717 complain (_("unexpected item: %s"), token_buffer
);
727 /*--------------------------------------------------------------.
728 | Copy the union declaration into the stype muscle |
729 | (and fdefines), where it is made into the definition of |
730 | YYSTYPE, the type of elements of the parser value stack. |
731 `--------------------------------------------------------------*/
734 parse_union_decl (void)
739 struct obstack union_obstack
;
741 complain (_("multiple %s declarations"), "%union");
745 obstack_init (&union_obstack
);
746 obstack_sgrow (&union_obstack
, "union");
752 /* If C contains '/', it is output by copy_comment (). */
754 obstack_1grow (&union_obstack
, c
);
763 copy_comment (finput
, &union_obstack
);
771 /* FIXME: Errr. How could this happen???. --akim */
773 complain (_("unmatched %s"), "`}'");
781 /* JF don't choke on trailing semi */
782 c
= skip_white_space ();
785 obstack_1grow (&union_obstack
, 0);
786 muscle_insert ("stype", obstack_finish (&union_obstack
));
790 /*-------------------------------------------------------.
791 | Parse the declaration %expect N which says to expect N |
792 | shift-reduce conflicts. |
793 `-------------------------------------------------------*/
796 parse_expect_decl (void)
798 int c
= skip_white_space ();
802 complain (_("argument of %%expect is not an integer"));
804 expected_conflicts
= read_signed_integer (finput
);
808 /*-------------------------------------------------------------------.
809 | Parse what comes after %thong. the full syntax is |
811 | %thong <type> token number literal |
813 | the <type> or number may be omitted. The number specifies the |
814 | user_token_number. |
816 | Two symbols are entered in the table, one for the token symbol and |
817 | one for the literal. Both are given the <type>, if any, from the |
818 | declaration. The ->user_token_number of the first is SALIAS and |
819 | the ->user_token_number of the second is set to the number, if |
820 | any, from the declaration. The two symbols are linked via |
821 | pointers in their ->alias fields. |
823 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
824 | only the literal string is retained it is the literal string that |
825 | is output to yytname |
826 `-------------------------------------------------------------------*/
829 parse_thong_decl (void)
832 struct bucket
*symbol
;
834 int usrtoknum
= SUNDEF
;
836 token
= lex (); /* fetch typename or first token */
837 if (token
== tok_typename
)
839 typename
= xstrdup (token_buffer
);
840 value_components_used
= 1;
841 token
= lex (); /* fetch first token */
844 /* process first token */
846 if (token
!= tok_identifier
)
848 complain (_("unrecognized item %s, expected an identifier"),
853 symval
->class = token_sym
;
854 symval
->type_name
= typename
;
855 symval
->user_token_number
= SALIAS
;
858 token
= lex (); /* get number or literal string */
860 if (token
== tok_number
)
863 token
= lex (); /* okay, did number, now get literal */
866 /* process literal string token */
868 if (token
!= tok_identifier
|| *symval
->tag
!= '\"')
870 complain (_("expected string constant instead of %s"), token_buffer
);
874 symval
->class = token_sym
;
875 symval
->type_name
= typename
;
876 symval
->user_token_number
= usrtoknum
;
878 symval
->alias
= symbol
;
879 symbol
->alias
= symval
;
881 /* symbol and symval combined are only one symbol. */
886 parse_muscle_decl (void)
888 int ch
= ungetc (skip_white_space (), finput
);
893 if (!isalpha (ch
) && ch
!= '_')
895 complain (_("invalid %s declaration"), "%define");
899 copy_identifier (finput
, &muscle_obstack
);
900 obstack_1grow (&muscle_obstack
, 0);
901 muscle_key
= obstack_finish (&muscle_obstack
);
904 ch
= skip_white_space ();
910 complain (_("invalid %s declaration"), "%define");
915 fatal (_("Premature EOF after %s"), "\"");
917 copy_string2 (finput
, &muscle_obstack
, '"', 0);
918 obstack_1grow (&muscle_obstack
, 0);
919 muscle_value
= obstack_finish (&muscle_obstack
);
921 /* Store the (key, value) pair in the environment. */
922 muscle_insert (muscle_key
, muscle_value
);
927 /*---------------------------------.
928 | Parse a double quoted parameter. |
929 `---------------------------------*/
932 parse_dquoted_param (const char *from
)
934 struct obstack param_obstack
;
935 const char *param
= NULL
;
938 obstack_init (¶m_obstack
);
939 c
= skip_white_space ();
943 complain (_("invalid %s declaration"), from
);
949 while ((c
= literalchar ()) != '"')
950 obstack_1grow (¶m_obstack
, c
);
952 obstack_1grow (¶m_obstack
, '\0');
953 param
= obstack_finish (¶m_obstack
);
955 if (c
!= '"' || strlen (param
) == 0)
957 complain (_("invalid %s declaration"), from
);
967 /*----------------------------------.
968 | Parse what comes after %skeleton. |
969 `----------------------------------*/
972 parse_skel_decl (void)
974 skeleton
= parse_dquoted_param ("%skeleton");
977 /*----------------------------------------------------------------.
978 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
979 | any `%' declarations, and copy the contents of any `%{ ... %}' |
980 | groups to ATTRS_OBSTACK. |
981 `----------------------------------------------------------------*/
984 read_declarations (void)
988 int c
= skip_white_space ();
992 token_t tok
= parse_percent_token ();
996 case tok_two_percents
:
999 case tok_percent_left_curly
:
1004 parse_token_decl (token_sym
, nterm_sym
);
1008 parse_token_decl (nterm_sym
, token_sym
);
1016 parse_start_decl ();
1020 parse_union_decl ();
1024 parse_expect_decl ();
1028 parse_thong_decl ();
1032 parse_assoc_decl (left_assoc
);
1036 parse_assoc_decl (right_assoc
);
1040 parse_assoc_decl (non_assoc
);
1044 parse_muscle_decl ();
1062 complain (_("unrecognized: %s"), token_buffer
);
1067 fatal (_("no input grammar"));
1072 complain (_("unknown character: %s"), quote (buf
));
1078 /*-------------------------------------------------------------------.
1079 | Assuming that a `{' has just been seen, copy everything up to the |
1080 | matching `}' into the actions file. STACK_OFFSET is the number of |
1081 | values in the current rule so far, which says where to find `$0' |
1082 | with respect to the top of the stack. |
1084 | This routine is used both for actions and guards. Only the |
1085 | actions_obstack is used, but this is fine, since we use only |
1086 | pointers to relevant portions inside this obstack. |
1087 `-------------------------------------------------------------------*/
1090 parse_braces (symbol_list
*rule
, int stack_offset
)
1095 /* offset is always 0 if parser has already popped the stack pointer */
1096 if (semantic_parser
)
1102 while ((c
= getc (finput
)) != '}')
1106 obstack_1grow (&action_obstack
, c
);
1111 obstack_1grow (&action_obstack
, c
);
1117 copy_string (finput
, &action_obstack
, c
);
1121 copy_comment (finput
, &action_obstack
);
1125 copy_dollar (finput
, &action_obstack
,
1126 rule
, stack_offset
);
1130 copy_at (finput
, &action_obstack
,
1135 fatal (_("unmatched %s"), "`{'");
1138 obstack_1grow (&action_obstack
, c
);
1141 /* Above loop exits when C is '}'. */
1144 obstack_1grow (&action_obstack
, c
);
1149 obstack_1grow (&action_obstack
, '\0');
1154 parse_action (symbol_list
*rule
, int stack_offset
)
1156 rule
->action_line
= lineno
;
1157 parse_braces (rule
, stack_offset
);
1158 rule
->action
= obstack_finish (&action_obstack
);
1163 parse_guard (symbol_list
*rule
, int stack_offset
)
1166 if (t
!= tok_left_curly
)
1167 complain (_("invalid %s declaration"), "%guard");
1168 rule
->guard_line
= lineno
;
1169 parse_braces (rule
, stack_offset
);
1170 rule
->guard
= obstack_finish (&action_obstack
);
1175 /*-------------------------------------------------------------------.
1176 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1177 | with the user's names. |
1178 `-------------------------------------------------------------------*/
1183 /* Incremented for each generated symbol */
1184 static int gensym_count
= 0;
1185 static char buf
[256];
1189 sprintf (buf
, "@%d", ++gensym_count
);
1191 sym
= getsym (token_buffer
);
1192 sym
->class = nterm_sym
;
1193 sym
->value
= nvars
++;
1197 /*-------------------------------------------------------------------.
1198 | Parse the input grammar into a one symbol_list structure. Each |
1199 | rule is represented by a sequence of symbols: the left hand side |
1200 | followed by the contents of the right hand side, followed by a |
1201 | null pointer instead of a symbol to terminate the rule. The next |
1202 | symbol is the lhs of the following rule. |
1204 | All guards and actions are copied out to the appropriate files, |
1205 | labelled by the rule number they apply to. |
1207 | Bison used to allow some %directives in the rules sections, but |
1208 | this is no longer consider appropriate: (i) the documented grammar |
1209 | doesn't claim it, (ii), it would promote bad style, (iii), error |
1210 | recovery for %directives consists in skipping the junk until a `%' |
1211 | is seen and helrp synchronizing. This scheme is definitely wrong |
1212 | in the rules section. |
1213 `-------------------------------------------------------------------*/
1220 symbol_list
*p
= NULL
;
1221 symbol_list
*p1
= NULL
;
1224 /* Points to first symbol_list of current rule. its symbol is the
1226 symbol_list
*crule
= NULL
;
1227 /* Points to the symbol_list preceding crule. */
1228 symbol_list
*crule1
= NULL
;
1232 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1233 if (t
== tok_identifier
|| t
== tok_bar
)
1235 int action_flag
= 0;
1236 /* Number of symbols in rhs of this rule so far */
1238 int xactions
= 0; /* JF for error checking */
1239 bucket
*first_rhs
= 0;
1241 if (t
== tok_identifier
)
1254 complain (_("ill-formed rule: initial symbol not followed by colon"));
1259 if (nrules
== 0 && t
== tok_bar
)
1261 complain (_("grammar starts with vertical bar"));
1262 lhs
= symval
; /* BOGUS: use a random symval */
1264 /* start a new rule and record its lhs. */
1269 p
= symbol_list_new (lhs
);
1280 /* mark the rule's lhs as a nonterminal if not already so. */
1282 if (lhs
->class == unknown_sym
)
1284 lhs
->class = nterm_sym
;
1288 else if (lhs
->class == token_sym
)
1289 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1291 /* read the rhs of the rule. */
1299 crule
->ruleprec
= symval
;
1303 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1306 /* If next token is an identifier, see if a colon follows it.
1307 If one does, exit this rule now. */
1308 if (t
== tok_identifier
)
1317 if (t1
== tok_colon
)
1320 if (!first_rhs
) /* JF */
1322 /* Not followed by colon =>
1323 process as part of this rule's rhs. */
1326 /* If we just passed an action, that action was in the middle
1327 of a rule, so make a dummy rule to reduce it to a
1331 /* Since the action was written out with this rule's
1332 number, we must give the new rule this number by
1333 inserting the new rule before it. */
1335 /* Make a dummy nonterminal, a gensym. */
1336 bucket
*sdummy
= gensym ();
1338 /* Make a new rule, whose body is empty, before the
1339 current one, so that the action just read can
1343 p
= symbol_list_new (sdummy
);
1344 /* Attach its lineno to that of the host rule. */
1345 p
->line
= crule
->line
;
1350 /* End of the rule. */
1351 crule1
= symbol_list_new (NULL
);
1352 crule1
->next
= crule
;
1356 /* Insert the dummy generated by that rule into this
1359 p
= symbol_list_new (sdummy
);
1366 if (t
== tok_identifier
)
1369 p
= symbol_list_new (symval
);
1373 else /* handle an action. */
1375 parse_action (crule
, rulelength
);
1377 xactions
++; /* JF */
1380 } /* end of read rhs of rule */
1382 /* Put an empty link in the list to mark the end of this rule */
1383 p
= symbol_list_new (NULL
);
1389 complain (_("two @prec's in a row"));
1391 crule
->ruleprec
= symval
;
1397 if (!semantic_parser
)
1398 complain (_("%%guard present but %%semantic_parser not specified"));
1400 parse_guard (crule
, rulelength
);
1404 if (t
== tok_left_curly
)
1406 /* This case never occurs -wjh */
1408 complain (_("two actions at end of one rule"));
1409 parse_action (crule
, rulelength
);
1411 xactions
++; /* -wjh */
1414 /* If $$ is being set in default way, report if any type
1417 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1419 if (lhs
->type_name
== 0
1420 || first_rhs
->type_name
== 0
1421 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1422 complain (_("type clash (`%s' `%s') on default action"),
1423 lhs
->type_name
? lhs
->type_name
: "",
1424 first_rhs
->type_name
? first_rhs
->type_name
: "");
1426 /* Warn if there is no default for $$ but we need one. */
1427 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1428 complain (_("empty rule for typed nonterminal, and no action"));
1429 if (t
== tok_semicolon
)
1434 complain (_("invalid input: %s"), quote (token_buffer
));
1439 /* grammar has been read. Do some checking */
1441 if (nsyms
> MAXSHORT
)
1442 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1445 fatal (_("no rules in the input grammar"));
1447 /* Report any undefined symbols and consider them nonterminals. */
1449 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1450 if (bp
->class == unknown_sym
)
1453 ("symbol %s is used, but is not defined as a token and has no rules"),
1455 bp
->class = nterm_sym
;
1456 bp
->value
= nvars
++;
1459 ntokens
= nsyms
- nvars
;
1462 /* At the end of the grammar file, some C source code must
1463 be stored. It is going to be associated to the epilogue
1466 read_additionnal_code (void)
1469 struct obstack el_obstack
;
1471 obstack_init (&el_obstack
);
1475 obstack_fgrow2 (&el_obstack
, muscle_find ("linef"),
1476 lineno
, quotearg_style (c_quoting_style
,
1477 muscle_find("filename")));
1480 while ((c
= getc (finput
)) != EOF
)
1481 obstack_1grow (&el_obstack
, c
);
1483 obstack_1grow (&el_obstack
, 0);
1484 muscle_insert ("epilogue", obstack_finish (&el_obstack
));
1488 /*------------------------------------------------------------------.
1489 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
1491 `------------------------------------------------------------------*/
1494 token_translations_init (void)
1499 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1501 /* Initialize all entries for literal tokens to 2, the internal
1502 token number for $undefined., which represents all invalid
1504 for (i
= 0; i
<= max_user_token_number
; i
++)
1505 token_translations
[i
] = 2;
1507 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1510 if (bp
->value
>= ntokens
)
1512 /* A token string alias? */
1513 if (bp
->user_token_number
== SALIAS
)
1516 assert (bp
->user_token_number
!= SUNDEF
);
1518 /* A token which translation has already been set? */
1519 if (token_translations
[bp
->user_token_number
] != 2)
1520 complain (_("tokens %s and %s both assigned number %d"),
1521 tags
[token_translations
[bp
->user_token_number
]],
1522 bp
->tag
, bp
->user_token_number
);
1523 token_translations
[bp
->user_token_number
] = bp
->value
;
1528 /*------------------------------------------------------------------.
1529 | Assign symbol numbers, and write definition of token names into |
1530 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1532 `------------------------------------------------------------------*/
1539 int last_user_token_number
;
1540 static char DOLLAR
[] = "$";
1542 tags
= XCALLOC (char *, nsyms
+ 1);
1543 user_toknums
= XCALLOC (short, nsyms
+ 1);
1545 sprec
= XCALLOC (short, nsyms
);
1546 sassoc
= XCALLOC (short, nsyms
);
1548 /* The EOF token. */
1550 user_toknums
[0] = 0;
1552 max_user_token_number
= 256;
1553 last_user_token_number
= 256;
1555 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1557 if (bp
->class == nterm_sym
)
1559 bp
->value
+= ntokens
;
1563 /* this symbol and its alias are a single token defn.
1564 allocate a tokno, and assign to both check agreement of
1565 ->prec and ->assoc fields and make both the same */
1567 bp
->value
= bp
->alias
->value
= tokno
++;
1569 if (bp
->prec
!= bp
->alias
->prec
)
1571 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1572 && bp
->user_token_number
== SALIAS
)
1573 complain (_("conflicting precedences for %s and %s"),
1574 bp
->tag
, bp
->alias
->tag
);
1576 bp
->alias
->prec
= bp
->prec
;
1578 bp
->prec
= bp
->alias
->prec
;
1581 if (bp
->assoc
!= bp
->alias
->assoc
)
1583 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1584 && bp
->user_token_number
== SALIAS
)
1585 complain (_("conflicting assoc values for %s and %s"),
1586 bp
->tag
, bp
->alias
->tag
);
1588 bp
->alias
->assoc
= bp
->assoc
;
1590 bp
->assoc
= bp
->alias
->assoc
;
1593 if (bp
->user_token_number
== SALIAS
)
1594 continue; /* do not do processing below for SALIASs */
1597 else /* bp->class == token_sym */
1599 bp
->value
= tokno
++;
1602 if (bp
->class == token_sym
)
1604 if (bp
->user_token_number
== SUNDEF
)
1605 bp
->user_token_number
= ++last_user_token_number
;
1606 if (bp
->user_token_number
> max_user_token_number
)
1607 max_user_token_number
= bp
->user_token_number
;
1610 tags
[bp
->value
] = bp
->tag
;
1611 user_toknums
[bp
->value
] = bp
->user_token_number
;
1612 sprec
[bp
->value
] = bp
->prec
;
1613 sassoc
[bp
->value
] = bp
->assoc
;
1616 token_translations_init ();
1618 error_token_number
= errtoken
->value
;
1620 if (startval
->class == unknown_sym
)
1621 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1622 else if (startval
->class == token_sym
)
1623 fatal (_("the start symbol %s is a token"), startval
->tag
);
1625 start_symbol
= startval
->value
;
1629 /*---------------------------------------------------------------.
1630 | Save the definition of token names in the `TOKENDEFS' muscle. |
1631 `---------------------------------------------------------------*/
1636 struct obstack tokendefs
;
1640 obstack_init (&tokendefs
);
1642 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1644 symbol
= bp
->tag
; /* get symbol */
1646 if (bp
->value
>= ntokens
)
1648 if (bp
->user_token_number
== SALIAS
)
1650 if ('\'' == *symbol
)
1651 continue; /* skip literal character */
1653 continue; /* skip error token */
1654 if ('\"' == *symbol
)
1656 /* use literal string only if given a symbol with an alias */
1658 symbol
= bp
->alias
->tag
;
1663 /* Don't #define nonliteral tokens whose names contain periods. */
1665 while ((c
= *cp
++) && c
!= '.');
1669 obstack_fgrow2 (&tokendefs
, "# define %s\t%d\n",
1670 symbol
, bp
->user_token_number
);
1671 if (semantic_parser
)
1672 /* FIXME: This is probably wrong, and should be just as
1674 obstack_fgrow2 (&tokendefs
, "# define T%s\t%d\n", symbol
, bp
->value
);
1677 obstack_1grow (&tokendefs
, 0);
1678 muscle_insert ("tokendef", xstrdup (obstack_finish (&tokendefs
)));
1679 obstack_free (&tokendefs
, NULL
);
1683 /*---------------------------------------------------------------.
1684 | Convert the rules into the representation using RRHS, RLHS and |
1686 `---------------------------------------------------------------*/
1695 ritem
= XCALLOC (short, nitems
+ 1);
1696 rule_table
= XCALLOC (rule_t
, nrules
) - 1;
1704 bucket
*ruleprec
= p
->ruleprec
;
1705 rule_table
[ruleno
].lhs
= p
->sym
->value
;
1706 rule_table
[ruleno
].rhs
= itemno
;
1707 rule_table
[ruleno
].line
= p
->line
;
1708 rule_table
[ruleno
].useful
= TRUE
;
1709 rule_table
[ruleno
].action
= p
->action
;
1710 rule_table
[ruleno
].action_line
= p
->action_line
;
1711 rule_table
[ruleno
].guard
= p
->guard
;
1712 rule_table
[ruleno
].guard_line
= p
->guard_line
;
1717 ritem
[itemno
++] = p
->sym
->value
;
1718 /* A rule gets by default the precedence and associativity
1719 of the last token in it. */
1720 if (p
->sym
->class == token_sym
)
1722 rule_table
[ruleno
].prec
= p
->sym
->prec
;
1723 rule_table
[ruleno
].assoc
= p
->sym
->assoc
;
1729 /* If this rule has a %prec,
1730 the specified symbol's precedence replaces the default. */
1733 rule_table
[ruleno
].prec
= ruleprec
->prec
;
1734 rule_table
[ruleno
].assoc
= ruleprec
->assoc
;
1735 rule_table
[ruleno
].precsym
= ruleprec
->value
;
1738 ritem
[itemno
++] = -ruleno
;
1748 ritem_print (stderr
);
1751 /*-------------------------------------------------------------------.
1752 | Read in the grammar specification and record it in the format |
1753 | described in gram.h. All guards are copied into the GUARD_OBSTACK |
1754 | and all actions into ACTION_OBSTACK, in each case forming the body |
1755 | of a C function (YYGUARD or YYACTION) which contains a switch |
1756 | statement to decide which guard or action to execute. |
1757 `-------------------------------------------------------------------*/
1763 startval
= NULL
; /* start symbol not specified yet. */
1773 semantic_parser
= 0;
1781 /* Initialize the muscle obstack. */
1782 obstack_init (&muscle_obstack
);
1784 /* Initialize the symbol table. */
1787 /* Construct the error token */
1788 errtoken
= getsym ("error");
1789 errtoken
->class = token_sym
;
1790 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1792 /* Construct a token that represents all undefined literal tokens.
1793 It is always token number 2. */
1794 undeftoken
= getsym ("$undefined.");
1795 undeftoken
->class = token_sym
;
1796 undeftoken
->user_token_number
= 2;
1798 /* Initialize the obstacks. */
1799 obstack_init (&action_obstack
);
1800 obstack_init (&attrs_obstack
);
1801 obstack_init (&output_obstack
);
1803 finput
= xfopen (infile
, "r");
1805 /* Read the declaration section. Copy %{ ... %} groups to
1806 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1807 etc. found there. */
1808 read_declarations ();
1809 /* Read in the grammar, build grammar in list form. Write out
1810 guards and actions. */
1812 /* Some C code is given at the end of the grammar file. */
1813 read_additionnal_code ();
1818 /* Assign the symbols their symbol numbers. Write #defines for the
1819 token symbols into FDEFINES if requested. */
1824 /* Convert the grammar into the format described in gram.h. */