]>
git.saurik.com Git - bison.git/blob - src/reader.c
cfcab0685fdbe66e3c6910cd6e9f437b9cfe4c4e
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. */
36 #include "conflicts.h"
37 #include "muscle_tab.h"
39 typedef struct symbol_list
41 struct symbol_list
*next
;
51 static symbol_list
*grammar
;
52 static int start_flag
;
53 static bucket
*startval
;
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 */
65 static bucket
*errtoken
;
66 static bucket
*undeftoken
;
70 symbol_list_new (bucket
*sym
)
72 symbol_list
*res
= XMALLOC (symbol_list
, 1);
82 /*===================\
84 \===================*/
87 skip_to_char (int target
)
91 complain (_(" Skipping to next \\n"));
93 complain (_(" Skipping to next %c"), target
);
96 c
= skip_white_space ();
97 while (c
!= target
&& c
!= EOF
);
103 /*---------------------------------------------------------.
104 | Read a signed integer from STREAM and return its value. |
105 `---------------------------------------------------------*/
108 read_signed_integer (FILE *stream
)
110 int c
= getc (stream
);
122 n
= 10 * n
+ (c
- '0');
131 /*--------------------------------------------------------------.
132 | Get the data type (alternative in the union) of the value for |
133 | symbol N in rule RULE. |
134 `--------------------------------------------------------------*/
137 get_type_name (int n
, symbol_list
*rule
)
144 complain (_("invalid $ value"));
154 if (rp
== NULL
|| rp
->sym
== NULL
)
156 complain (_("invalid $ value"));
162 return rp
->sym
->type_name
;
165 /*------------------------------------------------------------.
166 | Dump the string from FIN to OOUT if non null. MATCH is the |
167 | delimiter of the string (either ' or "). |
168 `------------------------------------------------------------*/
171 copy_string2 (FILE *fin
, struct obstack
*oout
, int match
, int store
)
176 obstack_1grow (oout
, match
);
183 fatal (_("unterminated string at end of file"));
186 complain (_("unterminated string"));
188 c
= match
; /* invent terminator */
192 obstack_1grow (oout
, c
);
198 fatal (_("unterminated string at end of file"));
199 obstack_1grow (oout
, c
);
209 obstack_1grow (oout
, c
);
215 copy_string (FILE *fin
, struct obstack
*oout
, int match
)
217 copy_string2 (fin
, oout
, match
, 1);
223 copy_identifier (FILE *fin
, struct obstack
*oout
)
227 while (isalnum (c
= getc (fin
)) || c
== '_')
228 obstack_1grow (oout
, c
);
233 /*-----------------------------------------------------------------.
234 | Dump the wannabee comment from IN to OUT1 and OUT2 (which can be |
235 | NULL). In fact we just saw a `/', which might or might not be a |
236 | comment. In any case, copy what we saw. |
238 | OUT2 might be NULL. |
239 `-----------------------------------------------------------------*/
242 copy_comment2 (FILE *fin
, struct obstack
*oout1
, struct obstack
*oout2
)
248 /* We read a `/', output it. */
249 obstack_1grow (oout1
, '/');
251 obstack_1grow (oout2
, '/');
253 switch ((c
= getc (fin
)))
266 obstack_1grow (oout1
, c
);
268 obstack_1grow (oout2
, c
);
274 if (!cplus_comment
&& c
== '*')
278 obstack_1grow (oout1
, c
);
280 obstack_1grow (oout2
, c
);
286 obstack_1grow (oout1
, c
);
288 obstack_1grow (oout2
, c
);
295 obstack_1grow (oout1
, c
);
297 obstack_1grow (oout2
, c
);
304 fatal (_("unterminated comment"));
307 obstack_1grow (oout1
, c
);
309 obstack_1grow (oout2
, c
);
316 /*-------------------------------------------------------------------.
317 | Dump the comment (actually the current string starting with a `/') |
318 | from FIN to OOUT. |
319 `-------------------------------------------------------------------*/
322 copy_comment (FILE *fin
, struct obstack
*oout
)
324 copy_comment2 (fin
, oout
, NULL
);
328 /*-----------------------------------------------------------------.
329 | FIN is pointing to a location (i.e., a `@'). Output to OOUT a |
330 | reference to this location. STACK_OFFSET is the number of values |
331 | in the current rule so far, which says where to find `$0' with |
332 | respect to the top of the stack. |
333 `-----------------------------------------------------------------*/
336 copy_at (FILE *fin
, struct obstack
*oout
, int stack_offset
)
343 obstack_sgrow (oout
, "yyloc");
346 else if (isdigit (c
) || c
== '-')
351 n
= read_signed_integer (fin
);
353 obstack_fgrow1 (oout
, "yylsp[%d]", n
- stack_offset
);
360 complain (_("%s is invalid"), quote (buf
));
365 /*-------------------------------------------------------------------.
366 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
368 | Possible inputs: $[<TYPENAME>]($|integer) |
370 | Output to OOUT a reference to this semantic value. STACK_OFFSET is |
371 | the number of values in the current rule so far, which says where |
372 | to find `$0' with respect to the top of the stack. |
373 `-------------------------------------------------------------------*/
376 copy_dollar (FILE *fin
, struct obstack
*oout
,
377 symbol_list
*rule
, int stack_offset
)
380 const char *type_name
= NULL
;
382 /* Get the type name if explicit. */
385 read_type_name (fin
);
386 type_name
= token_buffer
;
387 value_components_used
= 1;
393 obstack_sgrow (oout
, "yyval");
396 type_name
= get_type_name (0, rule
);
398 obstack_fgrow1 (oout
, ".%s", type_name
);
399 if (!type_name
&& typed
)
400 complain (_("$$ of `%s' has no declared type"),
403 else if (isdigit (c
) || c
== '-')
407 n
= read_signed_integer (fin
);
409 if (!type_name
&& n
> 0)
410 type_name
= get_type_name (n
, rule
);
412 obstack_fgrow1 (oout
, "yyvsp[%d]", n
- stack_offset
);
415 obstack_fgrow1 (oout
, ".%s", type_name
);
416 if (!type_name
&& typed
)
417 complain (_("$%d of `%s' has no declared type"),
424 complain (_("%s is invalid"), quote (buf
));
428 /*-------------------------------------------------------------------.
429 | Copy the contents of a `%{ ... %}' into the definitions file. The |
430 | `%{' has already been read. Return after reading the `%}'. |
431 `-------------------------------------------------------------------*/
434 copy_definition (void)
437 /* -1 while reading a character if prev char was %. */
442 obstack_fgrow2 (&attrs_obstack
, muscle_find ("linef"),
443 lineno
, quotearg_style (c_quoting_style
,
444 muscle_find("filename")));
456 obstack_1grow (&attrs_obstack
, c
);
466 copy_string (finput
, &attrs_obstack
, c
);
470 copy_comment (finput
, &attrs_obstack
);
474 fatal ("%s", _("unterminated `%{' definition"));
477 obstack_1grow (&attrs_obstack
, c
);
486 obstack_1grow (&attrs_obstack
, '%');
493 /*-------------------------------------------------------------------.
494 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
495 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
497 `-------------------------------------------------------------------*/
500 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
502 token_t token
= tok_undef
;
503 char *typename
= NULL
;
505 /* The symbol being defined. */
506 struct bucket
*symbol
= NULL
;
508 /* After `%token' and `%nterm', any number of symbols maybe be
512 int tmp_char
= ungetc (skip_white_space (), finput
);
514 /* `%' (for instance from `%token', or from `%%' etc.) is the
515 only valid means to end this declaration. */
519 fatal (_("Premature EOF after %s"), token_buffer
);
522 if (token
== tok_comma
)
527 if (token
== tok_typename
)
529 typename
= xstrdup (token_buffer
);
530 value_components_used
= 1;
533 else if (token
== tok_identifier
&& *symval
->tag
== '\"' && symbol
)
536 warn (_("symbol `%s' used more than once as a literal string"),
538 else if (symbol
->alias
)
539 warn (_("symbol `%s' given more than one literal string"),
543 symval
->class = token_sym
;
544 symval
->type_name
= typename
;
545 symval
->user_token_number
= symbol
->user_token_number
;
546 symbol
->user_token_number
= SALIAS
;
547 symval
->alias
= symbol
;
548 symbol
->alias
= symval
;
549 /* symbol and symval combined are only one symbol */
554 else if (token
== tok_identifier
)
556 int oldclass
= symval
->class;
559 if (symbol
->class == what_is_not
)
560 complain (_("symbol %s redefined"), symbol
->tag
);
561 symbol
->class = what_is
;
562 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
563 symbol
->value
= nvars
++;
567 if (symbol
->type_name
== NULL
)
568 symbol
->type_name
= typename
;
569 else if (strcmp (typename
, symbol
->type_name
) != 0)
570 complain (_("type redeclaration for %s"), symbol
->tag
);
573 else if (symbol
&& token
== tok_number
)
575 symbol
->user_token_number
= numval
;
579 complain (_("`%s' is invalid in %s"),
581 (what_is
== token_sym
) ? "%token" : "%nterm");
589 /*------------------------------.
590 | Parse what comes after %start |
591 `------------------------------*/
594 parse_start_decl (void)
597 complain (_("multiple %s declarations"), "%start");
598 if (lex () != tok_identifier
)
599 complain (_("invalid %s declaration"), "%start");
607 /*-----------------------------------------------------------.
608 | read in a %type declaration and record its information for |
609 | get_type_name to access |
610 `-----------------------------------------------------------*/
613 parse_type_decl (void)
617 if (lex () != tok_typename
)
619 complain ("%s", _("%type declaration has no <typename>"));
624 name
= xstrdup (token_buffer
);
629 int tmp_char
= ungetc (skip_white_space (), finput
);
634 fatal (_("Premature EOF after %s"), token_buffer
);
646 if (symval
->type_name
== NULL
)
647 symval
->type_name
= name
;
648 else if (strcmp (name
, symval
->type_name
) != 0)
649 complain (_("type redeclaration for %s"), symval
->tag
);
654 complain (_("invalid %%type declaration due to item: %s"),
663 /*----------------------------------------------------------------.
664 | Read in a %left, %right or %nonassoc declaration and record its |
666 `----------------------------------------------------------------*/
669 parse_assoc_decl (associativity assoc
)
674 lastprec
++; /* Assign a new precedence level, never 0. */
679 int tmp_char
= ungetc (skip_white_space (), finput
);
684 fatal (_("Premature EOF after %s"), token_buffer
);
691 name
= xstrdup (token_buffer
);
698 if (symval
->prec
!= 0)
699 complain (_("redefining precedence of %s"), symval
->tag
);
700 symval
->prec
= lastprec
;
701 symval
->assoc
= assoc
;
702 if (symval
->class == nterm_sym
)
703 complain (_("symbol %s redefined"), symval
->tag
);
704 symval
->class = token_sym
;
706 { /* record the type, if one is specified */
707 if (symval
->type_name
== NULL
)
708 symval
->type_name
= name
;
709 else if (strcmp (name
, symval
->type_name
) != 0)
710 complain (_("type redeclaration for %s"), symval
->tag
);
715 if (prev
== tok_identifier
)
717 symval
->user_token_number
= numval
;
722 ("invalid text (%s) - number should be after identifier"),
732 complain (_("unexpected item: %s"), token_buffer
);
742 /*--------------------------------------------------------------.
743 | Copy the union declaration into the stype muscle |
744 | (and fdefines), where it is made into the definition of |
745 | YYSTYPE, the type of elements of the parser value stack. |
746 `--------------------------------------------------------------*/
749 parse_union_decl (void)
753 struct obstack union_obstack
;
754 const char *prologue
= "\
757 const char *epilogue
= "\
759 # define YYSTYPE yystype\n\
763 complain (_("multiple %s declarations"), "%union");
767 /* FIXME: I'm worried: are you sure attrs_obstack is properly
769 /* I don't see any reasons to keep this line, because we should
770 create a special skeleton for this option. */
772 obstack_1grow (&attrs_obstack
, '\n');
774 obstack_init (&union_obstack
);
775 obstack_sgrow (&union_obstack
, "union");
777 obstack_sgrow (&defines_obstack
, prologue
);
783 /* If C contains '/', it is output by copy_comment (). */
786 obstack_1grow (&union_obstack
, c
);
788 obstack_1grow (&defines_obstack
, c
);
798 copy_comment2 (finput
, &defines_obstack
, &union_obstack
);
807 complain (_("unmatched %s"), "`}'");
812 obstack_sgrow (&defines_obstack
, epilogue
);
813 /* JF don't choke on trailing semi */
814 c
= skip_white_space ();
817 obstack_1grow (&union_obstack
, 0);
818 muscle_insert ("stype", obstack_finish (&union_obstack
));
829 /*-------------------------------------------------------.
830 | Parse the declaration %expect N which says to expect N |
831 | shift-reduce conflicts. |
832 `-------------------------------------------------------*/
835 parse_expect_decl (void)
837 int c
= skip_white_space ();
841 complain (_("argument of %%expect is not an integer"));
843 expected_conflicts
= read_signed_integer (finput
);
847 /*-------------------------------------------------------------------.
848 | Parse what comes after %thong. the full syntax is |
850 | %thong <type> token number literal |
852 | the <type> or number may be omitted. The number specifies the |
853 | user_token_number. |
855 | Two symbols are entered in the table, one for the token symbol and |
856 | one for the literal. Both are given the <type>, if any, from the |
857 | declaration. The ->user_token_number of the first is SALIAS and |
858 | the ->user_token_number of the second is set to the number, if |
859 | any, from the declaration. The two symbols are linked via |
860 | pointers in their ->alias fields. |
862 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
863 | only the literal string is retained it is the literal string that |
864 | is output to yytname |
865 `-------------------------------------------------------------------*/
868 parse_thong_decl (void)
871 struct bucket
*symbol
;
873 int usrtoknum
= SUNDEF
;
875 token
= lex (); /* fetch typename or first token */
876 if (token
== tok_typename
)
878 typename
= xstrdup (token_buffer
);
879 value_components_used
= 1;
880 token
= lex (); /* fetch first token */
883 /* process first token */
885 if (token
!= tok_identifier
)
887 complain (_("unrecognized item %s, expected an identifier"),
892 symval
->class = token_sym
;
893 symval
->type_name
= typename
;
894 symval
->user_token_number
= SALIAS
;
897 token
= lex (); /* get number or literal string */
899 if (token
== tok_number
)
902 token
= lex (); /* okay, did number, now get literal */
905 /* process literal string token */
907 if (token
!= tok_identifier
|| *symval
->tag
!= '\"')
909 complain (_("expected string constant instead of %s"), token_buffer
);
913 symval
->class = token_sym
;
914 symval
->type_name
= typename
;
915 symval
->user_token_number
= usrtoknum
;
917 symval
->alias
= symbol
;
918 symbol
->alias
= symval
;
920 /* symbol and symval combined are only one symbol. */
925 parse_muscle_decl (void)
927 int ch
= ungetc (skip_white_space (), finput
);
932 if (!isalpha (ch
) && ch
!= '_')
934 complain (_("invalid %s declaration"), "%define");
938 copy_identifier (finput
, &muscle_obstack
);
939 obstack_1grow (&muscle_obstack
, 0);
940 muscle_key
= obstack_finish (&muscle_obstack
);
943 ch
= skip_white_space ();
949 complain (_("invalid %s declaration"), "%define");
954 fatal (_("Premature EOF after %s"), "\"");
956 copy_string2 (finput
, &muscle_obstack
, '"', 0);
957 obstack_1grow (&muscle_obstack
, 0);
958 muscle_value
= obstack_finish (&muscle_obstack
);
960 /* Store the (key, value) pair in the environment. */
961 muscle_insert (muscle_key
, muscle_value
);
966 /*---------------------------------.
967 | Parse a double quoted parameter. |
968 `---------------------------------*/
971 parse_dquoted_param (const char *from
)
973 struct obstack param_obstack
;
974 const char *param
= NULL
;
977 obstack_init (¶m_obstack
);
978 c
= skip_white_space ();
982 complain (_("invalid %s declaration"), from
);
988 while ((c
= literalchar ()) != '"')
989 obstack_1grow (¶m_obstack
, c
);
991 obstack_1grow (¶m_obstack
, '\0');
992 param
= obstack_finish (¶m_obstack
);
994 if (c
!= '"' || strlen (param
) == 0)
996 complain (_("invalid %s declaration"), from
);
1006 /*----------------------------------.
1007 | Parse what comes after %skeleton. |
1008 `----------------------------------*/
1011 parse_skel_decl (void)
1013 skeleton
= parse_dquoted_param ("%skeleton");
1016 /*----------------------------------------------------------------.
1017 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
1018 | any `%' declarations, and copy the contents of any `%{ ... %}' |
1019 | groups to ATTRS_OBSTACK. |
1020 `----------------------------------------------------------------*/
1023 read_declarations (void)
1027 int c
= skip_white_space ();
1031 token_t tok
= parse_percent_token ();
1035 case tok_two_percents
:
1038 case tok_percent_left_curly
:
1043 parse_token_decl (token_sym
, nterm_sym
);
1047 parse_token_decl (nterm_sym
, token_sym
);
1055 parse_start_decl ();
1059 parse_union_decl ();
1063 parse_expect_decl ();
1067 parse_thong_decl ();
1071 parse_assoc_decl (left_assoc
);
1075 parse_assoc_decl (right_assoc
);
1079 parse_assoc_decl (non_assoc
);
1083 parse_muscle_decl ();
1101 complain (_("unrecognized: %s"), token_buffer
);
1106 fatal (_("no input grammar"));
1111 complain (_("unknown character: %s"), quote (buf
));
1117 /*-------------------------------------------------------------------.
1118 | Assuming that a `{' has just been seen, copy everything up to the |
1119 | matching `}' into the actions file. STACK_OFFSET is the number of |
1120 | values in the current rule so far, which says where to find `$0' |
1121 | with respect to the top of the stack. |
1122 `-------------------------------------------------------------------*/
1125 copy_action (symbol_list
*rule
, int stack_offset
)
1130 /* offset is always 0 if parser has already popped the stack pointer */
1131 if (semantic_parser
)
1134 obstack_fgrow1 (&action_obstack
, "\ncase %d:\n", nrules
);
1138 obstack_fgrow2 (&action_obstack
, muscle_find ("linef"),
1139 lineno
, quotearg_style (c_quoting_style
,
1140 muscle_find ("filename")));
1142 obstack_1grow (&action_obstack
, '{');
1154 obstack_1grow (&action_obstack
, c
);
1159 obstack_1grow (&action_obstack
, c
);
1165 copy_string (finput
, &action_obstack
, c
);
1169 copy_comment (finput
, &action_obstack
);
1173 copy_dollar (finput
, &action_obstack
,
1174 rule
, stack_offset
);
1178 copy_at (finput
, &action_obstack
,
1183 fatal (_("unmatched %s"), "`{'");
1186 obstack_1grow (&action_obstack
, c
);
1192 /* above loop exits when c is '}' */
1196 obstack_1grow (&action_obstack
, c
);
1201 /* As a Bison extension, add the ending semicolon. Since some Yacc
1202 don't do that, help people using bison as a Yacc finding their
1203 missing semicolons. */
1205 obstack_sgrow (&action_obstack
, "}\n break;");
1207 obstack_sgrow (&action_obstack
, ";\n break;}");
1210 /*-------------------------------------------------------------------.
1211 | After `%guard' is seen in the input file, copy the actual guard |
1212 | into the guards file. If the guard is followed by an action, copy |
1213 | that into the actions file. STACK_OFFSET is the number of values |
1214 | in the current rule so far, which says where to find `$0' with |
1215 | respect to the top of the stack, for the simple parser in which |
1216 | the stack is not popped until after the guard is run. |
1217 `-------------------------------------------------------------------*/
1220 copy_guard (symbol_list
*rule
, int stack_offset
)
1226 /* offset is always 0 if parser has already popped the stack pointer */
1227 if (semantic_parser
)
1230 obstack_fgrow1 (&guard_obstack
, "\ncase %d:\n", nrules
);
1232 obstack_fgrow2 (&guard_obstack
, muscle_find ("linef"),
1233 lineno
, quotearg_style (c_quoting_style
,
1234 muscle_find ("filename")));
1235 obstack_1grow (&guard_obstack
, '{');
1240 while (brace_flag
? (count
> 0) : (c
!= ';'))
1245 obstack_1grow (&guard_obstack
, c
);
1250 obstack_1grow (&guard_obstack
, c
);
1256 obstack_1grow (&guard_obstack
, c
);
1261 complain (_("unmatched %s"), "`}'");
1262 c
= getc (finput
); /* skip it */
1268 copy_string (finput
, &guard_obstack
, c
);
1272 copy_comment (finput
, &guard_obstack
);
1276 copy_dollar (finput
, &guard_obstack
, rule
, stack_offset
);
1280 copy_at (finput
, &guard_obstack
, stack_offset
);
1284 fatal ("%s", _("unterminated %guard clause"));
1287 obstack_1grow (&guard_obstack
, c
);
1290 if (c
!= '}' || count
!= 0)
1294 c
= skip_white_space ();
1296 obstack_sgrow (&guard_obstack
, ";\n break;}");
1298 copy_action (rule
, stack_offset
);
1301 c
= getc (finput
); /* why not skip_white_space -wjh */
1303 copy_action (rule
, stack_offset
);
1310 /*-------------------------------------------------------------------.
1311 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1312 | with the user's names. |
1313 `-------------------------------------------------------------------*/
1318 /* Incremented for each generated symbol */
1319 static int gensym_count
= 0;
1320 static char buf
[256];
1324 sprintf (buf
, "@%d", ++gensym_count
);
1326 sym
= getsym (token_buffer
);
1327 sym
->class = nterm_sym
;
1328 sym
->value
= nvars
++;
1332 /*-------------------------------------------------------------------.
1333 | Parse the input grammar into a one symbol_list structure. Each |
1334 | rule is represented by a sequence of symbols: the left hand side |
1335 | followed by the contents of the right hand side, followed by a |
1336 | null pointer instead of a symbol to terminate the rule. The next |
1337 | symbol is the lhs of the following rule. |
1339 | All guards and actions are copied out to the appropriate files, |
1340 | labelled by the rule number they apply to. |
1342 | Bison used to allow some %directives in the rules sections, but |
1343 | this is no longer consider appropriate: (i) the documented grammar |
1344 | doesn't claim it, (ii), it would promote bad style, (iii), error |
1345 | recovery for %directives consists in skipping the junk until a `%' |
1346 | is seen and helrp synchronizing. This scheme is definitely wrong |
1347 | in the rules section. |
1348 `-------------------------------------------------------------------*/
1355 symbol_list
*p
= NULL
;
1356 symbol_list
*p1
= NULL
;
1359 /* Points to first symbol_list of current rule. its symbol is the
1361 symbol_list
*crule
= NULL
;
1362 /* Points to the symbol_list preceding crule. */
1363 symbol_list
*crule1
= NULL
;
1367 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1368 if (t
== tok_identifier
|| t
== tok_bar
)
1370 int action_flag
= 0;
1371 /* Number of symbols in rhs of this rule so far */
1373 int xactions
= 0; /* JF for error checking */
1374 bucket
*first_rhs
= 0;
1376 if (t
== tok_identifier
)
1389 complain (_("ill-formed rule: initial symbol not followed by colon"));
1394 if (nrules
== 0 && t
== tok_bar
)
1396 complain (_("grammar starts with vertical bar"));
1397 lhs
= symval
; /* BOGUS: use a random symval */
1399 /* start a new rule and record its lhs. */
1404 p
= symbol_list_new (lhs
);
1415 /* mark the rule's lhs as a nonterminal if not already so. */
1417 if (lhs
->class == unknown_sym
)
1419 lhs
->class = nterm_sym
;
1423 else if (lhs
->class == token_sym
)
1424 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1426 /* read the rhs of the rule. */
1434 crule
->ruleprec
= symval
;
1438 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1441 /* If next token is an identifier, see if a colon follows it.
1442 If one does, exit this rule now. */
1443 if (t
== tok_identifier
)
1452 if (t1
== tok_colon
)
1455 if (!first_rhs
) /* JF */
1457 /* Not followed by colon =>
1458 process as part of this rule's rhs. */
1461 /* If we just passed an action, that action was in the middle
1462 of a rule, so make a dummy rule to reduce it to a
1466 /* Since the action was written out with this rule's
1467 number, we must give the new rule this number by
1468 inserting the new rule before it. */
1470 /* Make a dummy nonterminal, a gensym. */
1471 bucket
*sdummy
= gensym ();
1473 /* Make a new rule, whose body is empty, before the
1474 current one, so that the action just read can
1478 p
= symbol_list_new (sdummy
);
1479 /* Attach its lineno to that of the host rule. */
1480 p
->line
= crule
->line
;
1485 /* End of the rule. */
1486 crule1
= symbol_list_new (NULL
);
1487 crule1
->next
= crule
;
1491 /* Insert the dummy generated by that rule into this
1494 p
= symbol_list_new (sdummy
);
1501 if (t
== tok_identifier
)
1504 p
= symbol_list_new (symval
);
1508 else /* handle an action. */
1510 copy_action (crule
, rulelength
);
1512 xactions
++; /* JF */
1515 } /* end of read rhs of rule */
1517 /* Put an empty link in the list to mark the end of this rule */
1518 p
= symbol_list_new (NULL
);
1524 complain (_("two @prec's in a row"));
1526 crule
->ruleprec
= symval
;
1531 if (!semantic_parser
)
1532 complain (_("%%guard present but %%semantic_parser not specified"));
1534 copy_guard (crule
, rulelength
);
1537 else if (t
== tok_left_curly
)
1539 /* This case never occurs -wjh */
1541 complain (_("two actions at end of one rule"));
1542 copy_action (crule
, rulelength
);
1544 xactions
++; /* -wjh */
1547 /* If $$ is being set in default way, report if any type
1550 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1552 if (lhs
->type_name
== 0
1553 || first_rhs
->type_name
== 0
1554 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1555 complain (_("type clash (`%s' `%s') on default action"),
1556 lhs
->type_name
? lhs
->type_name
: "",
1557 first_rhs
->type_name
? first_rhs
->type_name
: "");
1559 /* Warn if there is no default for $$ but we need one. */
1560 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1561 complain (_("empty rule for typed nonterminal, and no action"));
1562 if (t
== tok_semicolon
)
1567 complain (_("invalid input: %s"), quote (token_buffer
));
1572 /* grammar has been read. Do some checking */
1574 if (nsyms
> MAXSHORT
)
1575 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1578 fatal (_("no rules in the input grammar"));
1580 /* Report any undefined symbols and consider them nonterminals. */
1582 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1583 if (bp
->class == unknown_sym
)
1586 ("symbol %s is used, but is not defined as a token and has no rules"),
1588 bp
->class = nterm_sym
;
1589 bp
->value
= nvars
++;
1592 ntokens
= nsyms
- nvars
;
1595 /* At the end of the grammar file, some C source code must
1596 be stored. It is going to be associated to the epilogue
1599 read_additionnal_code (void)
1602 struct obstack el_obstack
;
1604 obstack_init (&el_obstack
);
1608 obstack_fgrow2 (&el_obstack
, muscle_find ("linef"),
1609 lineno
, quotearg_style (c_quoting_style
,
1610 muscle_find("filename")));
1613 while ((c
= getc (finput
)) != EOF
)
1614 obstack_1grow (&el_obstack
, c
);
1616 obstack_1grow (&el_obstack
, 0);
1617 muscle_insert ("epilogue", obstack_finish (&el_obstack
));
1621 /*--------------------------------------------------------------.
1622 | For named tokens, but not literal ones, define the name. The |
1623 | value is the user token number. |
1624 `--------------------------------------------------------------*/
1627 output_token_defines (struct obstack
*oout
)
1633 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1635 symbol
= bp
->tag
; /* get symbol */
1637 if (bp
->value
>= ntokens
)
1639 if (bp
->user_token_number
== SALIAS
)
1641 if ('\'' == *symbol
)
1642 continue; /* skip literal character */
1644 continue; /* skip error token */
1645 if ('\"' == *symbol
)
1647 /* use literal string only if given a symbol with an alias */
1649 symbol
= bp
->alias
->tag
;
1654 /* Don't #define nonliteral tokens whose names contain periods. */
1656 while ((c
= *cp
++) && c
!= '.');
1660 obstack_fgrow2 (oout
, "# define\t%s\t%d\n",
1661 symbol
, bp
->user_token_number
);
1662 if (semantic_parser
)
1663 /* FIXME: This is certainly dead wrong, and should be just as
1665 obstack_fgrow2 (oout
, "# define\tT%s\t%d\n", symbol
, bp
->value
);
1670 /*------------------------------------------------------------------.
1671 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
1673 `------------------------------------------------------------------*/
1676 token_translations_init (void)
1681 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1683 /* Initialize all entries for literal tokens to 2, the internal
1684 token number for $undefined., which represents all invalid
1686 for (i
= 0; i
<= max_user_token_number
; i
++)
1687 token_translations
[i
] = 2;
1689 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1692 if (bp
->value
>= ntokens
)
1694 /* A token string alias? */
1695 if (bp
->user_token_number
== SALIAS
)
1698 assert (bp
->user_token_number
!= SUNDEF
);
1700 /* A token which translation has already been set? */
1701 if (token_translations
[bp
->user_token_number
] != 2)
1702 complain (_("tokens %s and %s both assigned number %d"),
1703 tags
[token_translations
[bp
->user_token_number
]],
1704 bp
->tag
, bp
->user_token_number
);
1705 token_translations
[bp
->user_token_number
] = bp
->value
;
1710 /*------------------------------------------------------------------.
1711 | Assign symbol numbers, and write definition of token names into |
1712 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1714 `------------------------------------------------------------------*/
1721 int last_user_token_number
;
1722 static char DOLLAR
[] = "$";
1724 tags
= XCALLOC (char *, nsyms
+ 1);
1725 user_toknums
= XCALLOC (short, nsyms
+ 1);
1727 sprec
= XCALLOC (short, nsyms
);
1728 sassoc
= XCALLOC (short, nsyms
);
1730 /* The EOF token. */
1732 user_toknums
[0] = 0;
1734 max_user_token_number
= 256;
1735 last_user_token_number
= 256;
1737 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1739 if (bp
->class == nterm_sym
)
1741 bp
->value
+= ntokens
;
1745 /* this symbol and its alias are a single token defn.
1746 allocate a tokno, and assign to both check agreement of
1747 ->prec and ->assoc fields and make both the same */
1749 bp
->value
= bp
->alias
->value
= tokno
++;
1751 if (bp
->prec
!= bp
->alias
->prec
)
1753 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1754 && bp
->user_token_number
== SALIAS
)
1755 complain (_("conflicting precedences for %s and %s"),
1756 bp
->tag
, bp
->alias
->tag
);
1758 bp
->alias
->prec
= bp
->prec
;
1760 bp
->prec
= bp
->alias
->prec
;
1763 if (bp
->assoc
!= bp
->alias
->assoc
)
1765 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1766 && bp
->user_token_number
== SALIAS
)
1767 complain (_("conflicting assoc values for %s and %s"),
1768 bp
->tag
, bp
->alias
->tag
);
1770 bp
->alias
->assoc
= bp
->assoc
;
1772 bp
->assoc
= bp
->alias
->assoc
;
1775 if (bp
->user_token_number
== SALIAS
)
1776 continue; /* do not do processing below for SALIASs */
1779 else /* bp->class == token_sym */
1781 bp
->value
= tokno
++;
1784 if (bp
->class == token_sym
)
1786 if (bp
->user_token_number
== SUNDEF
)
1787 bp
->user_token_number
= ++last_user_token_number
;
1788 if (bp
->user_token_number
> max_user_token_number
)
1789 max_user_token_number
= bp
->user_token_number
;
1792 tags
[bp
->value
] = bp
->tag
;
1793 user_toknums
[bp
->value
] = bp
->user_token_number
;
1794 sprec
[bp
->value
] = bp
->prec
;
1795 sassoc
[bp
->value
] = bp
->assoc
;
1798 token_translations_init ();
1800 error_token_number
= errtoken
->value
;
1802 if (startval
->class == unknown_sym
)
1803 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1804 else if (startval
->class == token_sym
)
1805 fatal (_("the start symbol %s is a token"), startval
->tag
);
1807 start_symbol
= startval
->value
;
1811 /*-----------------------------------.
1812 | Output definition of token names. |
1813 `-----------------------------------*/
1816 symbols_output (void)
1819 struct obstack tokendefs
;
1820 obstack_init (&tokendefs
);
1821 output_token_defines (&tokendefs
);
1822 obstack_1grow (&tokendefs
, 0);
1823 muscle_insert ("tokendef", xstrdup (obstack_finish (&tokendefs
)));
1824 obstack_free (&tokendefs
, NULL
);
1829 output_token_defines (&defines_obstack
);
1832 obstack_fgrow1 (&defines_obstack
, "\nextern YYSTYPE %slval;\n",
1834 if (semantic_parser
)
1838 for (i
= ntokens
; i
< nsyms
; i
++)
1840 /* don't make these for dummy nonterminals made by gensym. */
1841 if (*tags
[i
] != '@')
1842 obstack_fgrow2 (&defines_obstack
,
1843 "# define\tNT%s\t%d\n", tags
[i
], i
);
1846 /* `fdefines' is now a temporary file, so we need to copy its
1847 contents in `done', so we can't close it here. */
1856 /*---------------------------------------------------------------.
1857 | Convert the rules into the representation using RRHS, RLHS and |
1859 `---------------------------------------------------------------*/
1868 ritem
= XCALLOC (short, nitems
+ 1);
1869 rule_table
= XCALLOC (rule_t
, nrules
) - 1;
1877 bucket
*ruleprec
= p
->ruleprec
;
1878 rule_table
[ruleno
].lhs
= p
->sym
->value
;
1879 rule_table
[ruleno
].rhs
= itemno
;
1880 rule_table
[ruleno
].line
= p
->line
;
1881 rule_table
[ruleno
].useful
= TRUE
;
1886 ritem
[itemno
++] = p
->sym
->value
;
1887 /* A rule gets by default the precedence and associativity
1888 of the last token in it. */
1889 if (p
->sym
->class == token_sym
)
1891 rule_table
[ruleno
].prec
= p
->sym
->prec
;
1892 rule_table
[ruleno
].assoc
= p
->sym
->assoc
;
1898 /* If this rule has a %prec,
1899 the specified symbol's precedence replaces the default. */
1902 rule_table
[ruleno
].prec
= ruleprec
->prec
;
1903 rule_table
[ruleno
].assoc
= ruleprec
->assoc
;
1904 rule_table
[ruleno
].precsym
= ruleprec
->value
;
1907 ritem
[itemno
++] = -ruleno
;
1917 ritem_print (stderr
);
1920 /*-------------------------------------------------------------------.
1921 | Read in the grammar specification and record it in the format |
1922 | described in gram.h. All guards are copied into the GUARD_OBSTACK |
1923 | and all actions into ACTION_OBSTACK, in each case forming the body |
1924 | of a C function (YYGUARD or YYACTION) which contains a switch |
1925 | statement to decide which guard or action to execute. |
1926 `-------------------------------------------------------------------*/
1932 startval
= NULL
; /* start symbol not specified yet. */
1942 semantic_parser
= 0;
1950 /* Initialize the muscle obstack. */
1951 obstack_init (&muscle_obstack
);
1953 /* Initialize the symbol table. */
1956 /* Construct the error token */
1957 errtoken
= getsym ("error");
1958 errtoken
->class = token_sym
;
1959 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1961 /* Construct a token that represents all undefined literal tokens.
1962 It is always token number 2. */
1963 undeftoken
= getsym ("$undefined.");
1964 undeftoken
->class = token_sym
;
1965 undeftoken
->user_token_number
= 2;
1967 /* Read the declaration section. Copy %{ ... %} groups to
1968 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1969 etc. found there. */
1970 read_declarations ();
1971 /* Read in the grammar, build grammar in list form. Write out
1972 guards and actions. */
1974 /* Some C code is given at the end of the grammar file. */
1975 read_additionnal_code ();
1977 /* Now we know whether we need the line-number stack. If we do,
1978 write its type into the .tab.h file.
1979 This is no longer need with header skeleton. */
1981 /* Assign the symbols their symbol numbers. Write #defines for the
1982 token symbols into FDEFINES if requested. */
1984 /* Convert the grammar into the format described in gram.h. */
1986 /* Output the headers. */