]>
git.saurik.com Git - bison.git/blob - src/reader.c
1 /* Input parser for bison
2 Copyright 1984, 1986, 1989, 1992, 1998, 2000
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. */
34 #include "conflicts.h"
37 /* Number of slots allocated (but not necessarily used yet) in `rline' */
38 static int rline_allocated
;
40 typedef struct symbol_list
42 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 /* Incremented for each generated symbol */
66 static int gensym_count
;
68 static bucket
*errtoken
;
69 static bucket
*undeftoken
;
72 /*===================\
74 \===================*/
77 skip_to_char (int target
)
81 complain (_(" Skipping to next \\n"));
83 complain (_(" Skipping to next %c"), target
);
86 c
= skip_white_space ();
87 while (c
!= target
&& c
!= EOF
);
93 /*---------------------------------------------------------.
94 | Read a signed integer from STREAM and return its value. |
95 `---------------------------------------------------------*/
98 read_signed_integer (FILE *stream
)
100 int c
= getc (stream
);
112 n
= 10 * n
+ (c
- '0');
121 /*--------------------------------------------------------------.
122 | Get the data type (alternative in the union) of the value for |
123 | symbol N in rule RULE. |
124 `--------------------------------------------------------------*/
127 get_type_name (int n
, symbol_list
* rule
)
134 complain (_("invalid $ value"));
144 if (rp
== NULL
|| rp
->sym
== NULL
)
146 complain (_("invalid $ value"));
152 return rp
->sym
->type_name
;
155 /*-----------------------------------------------------------------.
156 | Dump the string from FIN to FOUT and OOUT if non null. MATCH is |
157 | the delimiter of the string (either ' or "). |
158 `-----------------------------------------------------------------*/
161 copy_string (FILE *fin
, FILE *fout
, struct obstack
*oout
, int match
)
168 obstack_1grow (oout
, match
);
175 fatal (_("unterminated string at end of file"));
178 complain (_("unterminated string"));
180 c
= match
; /* invent terminator */
187 obstack_1grow (oout
, c
);
193 fatal (_("unterminated string at end of file"));
197 obstack_1grow (oout
, c
);
209 obstack_1grow (oout
, c
);
213 /*----------------------------------------------------------------.
214 | Dump the wannabee comment from IN to OUT1 and OUT2. In fact we |
215 | just saw a `/', which might or might not be a comment. In any |
216 | case, copy what we saw. |
218 | OUT2 might be NULL. |
219 `----------------------------------------------------------------*/
222 copy_comment2 (FILE *fin
, FILE *out1
, FILE *out2
, struct obstack
*oout
)
228 /* We read a `/', output it. */
234 obstack_1grow (oout
, '/');
236 switch ((c
= getc (fin
)))
254 obstack_1grow (oout
, c
);
260 if (!cplus_comment
&& c
== '*')
269 obstack_1grow (oout
, c
);
280 obstack_1grow (oout
, c
);
292 obstack_1grow (oout
, c
);
299 fatal (_("unterminated comment"));
307 obstack_1grow (oout
, c
);
314 /*-------------------------------------------------------------------.
315 | Dump the comment (actually the current string starting with a `/') |
316 | from FIN to FOUT. |
317 `-------------------------------------------------------------------*/
320 copy_comment (FILE *fin
, FILE *fout
, struct obstack
*oout
)
322 copy_comment2 (fin
, fout
, NULL
, oout
);
326 /*-----------------------------------------------------------------.
327 | FIN is pointing to a location (i.e., a `@'). Output to FOUT a |
328 | reference to this location. STACK_OFFSET is the number of values |
329 | in the current rule so far, which says where to find `$0' with |
330 | respect to the top of the stack. |
331 `-----------------------------------------------------------------*/
334 copy_at (FILE *fin
, FILE *fout
, struct obstack
*oout
, int stack_offset
)
342 fprintf (fout
, "yyloc");
344 obstack_grow_literal_string (oout
, "yyloc");
347 else if (isdigit (c
) || c
== '-')
353 n
= read_signed_integer (fin
);
355 sprintf (buf
, "yylsp[%d]", n
- stack_offset
);
359 obstack_grow (oout
, buf
, strlen (buf
));
366 complain (_("%s is invalid"), quote (buf
));
371 /*-------------------------------------------------------------------.
372 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
374 | Possible inputs: $[<TYPENAME>]($|integer) |
376 | Output to FOUT a reference to this semantic value. STACK_OFFSET is |
377 | the number of values in the current rule so far, which says where |
378 | to find `$0' with respect to the top of the stack. |
379 `-------------------------------------------------------------------*/
382 copy_dollar (FILE *fin
, FILE *fout
, struct obstack
*oout
,
383 symbol_list
*rule
, int stack_offset
)
386 char *type_name
= NULL
;
388 /* Get the type name if explicit. */
391 read_type_name (fin
);
392 type_name
= token_buffer
;
393 value_components_used
= 1;
400 fputs ("yyval", fout
);
402 obstack_grow_literal_string (oout
, "yyval");
405 type_name
= get_type_name (0, rule
);
409 fprintf (fout
, ".%s", type_name
);
411 obstack_fgrow1 (oout
, ".%s", type_name
);
413 if (!type_name
&& typed
)
414 complain (_("$$ of `%s' has no declared type"),
417 else if (isdigit (c
) || c
== '-')
421 n
= read_signed_integer (fin
);
423 if (!type_name
&& n
> 0)
424 type_name
= get_type_name (n
, rule
);
427 fprintf (fout
, "yyvsp[%d]", n
- stack_offset
);
429 obstack_fgrow1 (oout
, "yyvsp[%d]", n
- stack_offset
);
434 fprintf (fout
, ".%s", type_name
);
436 obstack_fgrow1 (oout
, ".%s", type_name
);
438 if (!type_name
&& typed
)
439 complain (_("$%d of `%s' has no declared type"),
446 complain (_("%s is invalid"), quote (buf
));
450 /*-------------------------------------------------------------------.
451 | Copy the contents of a `%{ ... %}' into the definitions file. The |
452 | `%{' has already been read. Return after reading the `%}'. |
453 `-------------------------------------------------------------------*/
456 copy_definition (void)
459 /* -1 while reading a character if prev char was %. */
463 obstack_fgrow2 (&attrs_obstack
, "#line %d \"%s\"\n", lineno
, infile
);
474 obstack_1grow (&attrs_obstack
, c
);
484 copy_string (finput
, 0, &attrs_obstack
, c
);
488 copy_comment (finput
, 0, &attrs_obstack
);
492 fatal ("%s", _("unterminated `%{' definition"));
495 obstack_1grow (&attrs_obstack
, c
);
504 obstack_1grow (&attrs_obstack
, '%');
511 /*-------------------------------------------------------------------.
512 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
513 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
515 `-------------------------------------------------------------------*/
518 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
523 /* The symbol being defined. */
524 struct bucket
*symbol
= NULL
;
526 /* After `%token' and `%nterm', any number of symbols maybe be
530 int tmp_char
= ungetc (skip_white_space (), finput
);
532 /* `%' (for instance from `%token', or from `%%' etc.) is the
533 only valid means to end this declaration. */
537 fatal (_("Premature EOF after %s"), token_buffer
);
545 if (token
== TYPENAME
)
547 typename
= xstrdup (token_buffer
);
548 value_components_used
= 1;
551 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"' && symbol
)
554 warn (_("symbol `%s' used more than once as a literal string"),
556 else if (symbol
->alias
)
557 warn (_("symbol `%s' given more than one literal string"),
561 symval
->class = token_sym
;
562 symval
->type_name
= typename
;
563 symval
->user_token_number
= symbol
->user_token_number
;
564 symbol
->user_token_number
= SALIAS
;
565 symval
->alias
= symbol
;
566 symbol
->alias
= symval
;
567 /* symbol and symval combined are only one symbol */
573 else if (token
== IDENTIFIER
)
575 int oldclass
= symval
->class;
578 if (symbol
->class == what_is_not
)
579 complain (_("symbol %s redefined"), symbol
->tag
);
580 symbol
->class = what_is
;
581 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
582 symbol
->value
= nvars
++;
586 if (symbol
->type_name
== NULL
)
587 symbol
->type_name
= typename
;
588 else if (strcmp (typename
, symbol
->type_name
) != 0)
589 complain (_("type redeclaration for %s"), symbol
->tag
);
592 else if (symbol
&& token
== NUMBER
)
594 symbol
->user_token_number
= numval
;
599 complain (_("`%s' is invalid in %s"),
600 token_buffer
, (what_is
== token_sym
) ? "%token" : "%nterm");
608 /*------------------------------.
609 | Parse what comes after %start |
610 `------------------------------*/
613 parse_start_decl (void)
616 complain (_("multiple %s declarations"), "%start");
617 if (lex () != IDENTIFIER
)
618 complain (_("invalid %s declaration"), "%start");
626 /*-----------------------------------------------------------.
627 | read in a %type declaration and record its information for |
628 | get_type_name to access |
629 `-----------------------------------------------------------*/
632 parse_type_decl (void)
636 if (lex () != TYPENAME
)
638 complain ("%s", _("%type declaration has no <typename>"));
643 name
= xstrdup (token_buffer
);
648 int tmp_char
= ungetc (skip_white_space (), finput
);
653 fatal (_("Premature EOF after %s"), token_buffer
);
665 if (symval
->type_name
== NULL
)
666 symval
->type_name
= name
;
667 else if (strcmp (name
, symval
->type_name
) != 0)
668 complain (_("type redeclaration for %s"), symval
->tag
);
673 complain (_("invalid %%type declaration due to item: %s"),
682 /*----------------------------------------------------------------.
683 | Read in a %left, %right or %nonassoc declaration and record its |
685 `----------------------------------------------------------------*/
688 parse_assoc_decl (associativity assoc
)
693 lastprec
++; /* Assign a new precedence level, never 0. */
698 int tmp_char
= ungetc (skip_white_space (), finput
);
703 fatal (_("Premature EOF after %s"), token_buffer
);
710 name
= xstrdup (token_buffer
);
717 if (symval
->prec
!= 0)
718 complain (_("redefining precedence of %s"), symval
->tag
);
719 symval
->prec
= lastprec
;
720 symval
->assoc
= assoc
;
721 if (symval
->class == nterm_sym
)
722 complain (_("symbol %s redefined"), symval
->tag
);
723 symval
->class = token_sym
;
725 { /* record the type, if one is specified */
726 if (symval
->type_name
== NULL
)
727 symval
->type_name
= name
;
728 else if (strcmp (name
, symval
->type_name
) != 0)
729 complain (_("type redeclaration for %s"), symval
->tag
);
734 if (prev
== IDENTIFIER
)
736 symval
->user_token_number
= numval
;
742 ("invalid text (%s) - number should be after identifier"),
752 complain (_("unexpected item: %s"), token_buffer
);
763 /*--------------------------------------------------------------.
764 | Copy the union declaration into ATTRS_OBSTACK (and fdefines), |
765 | where it is made into the definition of YYSTYPE, the type of |
766 | elements of the parser value stack. |
767 `--------------------------------------------------------------*/
770 parse_union_decl (void)
776 complain (_("multiple %s declarations"), "%union");
781 obstack_fgrow2 (&attrs_obstack
, "\n#line %d \"%s\"\n", lineno
, infile
);
783 obstack_1grow (&attrs_obstack
, '\n');
785 obstack_grow_literal_string (&attrs_obstack
, "typedef union");
787 fprintf (fdefines
, "typedef union");
793 obstack_1grow (&attrs_obstack
, c
);
804 copy_comment2 (finput
, 0, fdefines
, &attrs_obstack
);
813 complain (_("unmatched %s"), "`}'");
817 obstack_grow_literal_string (&attrs_obstack
, " YYSTYPE;\n");
819 fprintf (fdefines
, " YYSTYPE;\n");
820 /* JF don't choke on trailing semi */
821 c
= skip_white_space ();
833 /*-------------------------------------------------------.
834 | Parse the declaration %expect N which says to expect N |
835 | shift-reduce conflicts. |
836 `-------------------------------------------------------*/
839 parse_expect_decl (void)
841 int c
= skip_white_space ();
845 complain (_("argument of %%expect is not an integer"));
847 expected_conflicts
= read_signed_integer (finput
);
851 /*-------------------------------------------------------------------.
852 | Parse what comes after %thong. the full syntax is |
854 | %thong <type> token number literal |
856 | the <type> or number may be omitted. The number specifies the |
857 | user_token_number. |
859 | Two symbols are entered in the table, one for the token symbol and |
860 | one for the literal. Both are given the <type>, if any, from the |
861 | declaration. The ->user_token_number of the first is SALIAS and |
862 | the ->user_token_number of the second is set to the number, if |
863 | any, from the declaration. The two symbols are linked via |
864 | pointers in their ->alias fields. |
866 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
867 | only the literal string is retained it is the literal string that |
868 | is output to yytname |
869 `-------------------------------------------------------------------*/
872 parse_thong_decl (void)
875 struct bucket
*symbol
;
880 token
= lex (); /* fetch typename or first token */
881 if (token
== TYPENAME
)
883 typename
= xstrdup (token_buffer
);
884 value_components_used
= 1;
885 token
= lex (); /* fetch first token */
888 /* process first token */
890 if (token
!= IDENTIFIER
)
892 complain (_("unrecognized item %s, expected an identifier"),
897 symval
->class = token_sym
;
898 symval
->type_name
= typename
;
899 symval
->user_token_number
= SALIAS
;
902 token
= lex (); /* get number or literal string */
907 token
= lex (); /* okay, did number, now get literal */
912 /* process literal string token */
914 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
916 complain (_("expected string constant instead of %s"), token_buffer
);
920 symval
->class = token_sym
;
921 symval
->type_name
= typename
;
922 symval
->user_token_number
= usrtoknum
;
924 symval
->alias
= symbol
;
925 symbol
->alias
= symval
;
927 /* symbol and symval combined are only one symbol. */
932 /*----------------------------------------------------------------.
933 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
934 | any `%' declarations, and copy the contents of any `%{ ... %}' |
935 | groups to ATTRS_OBSTACK. |
936 `----------------------------------------------------------------*/
939 read_declarations (void)
946 c
= skip_white_space ();
950 tok
= parse_percent_token ();
957 case PERCENT_LEFT_CURLY
:
962 parse_token_decl (token_sym
, nterm_sym
);
966 parse_token_decl (nterm_sym
, token_sym
);
982 parse_expect_decl ();
989 parse_assoc_decl (left_assoc
);
993 parse_assoc_decl (right_assoc
);
997 parse_assoc_decl (non_assoc
);
1000 case SEMANTIC_PARSER
:
1001 if (semantic_parser
== 0)
1003 semantic_parser
= 1;
1004 open_extra_files ();
1016 complain (_("unrecognized: %s"), token_buffer
);
1021 fatal (_("no input grammar"));
1026 complain (_("unknown character: %s"), quote (buf
));
1032 /*-------------------------------------------------------------------.
1033 | Assuming that a `{' has just been seen, copy everything up to the |
1034 | matching `}' into the actions file. STACK_OFFSET is the number of |
1035 | values in the current rule so far, which says where to find `$0' |
1036 | with respect to the top of the stack. |
1037 `-------------------------------------------------------------------*/
1040 copy_action (symbol_list
*rule
, int stack_offset
)
1046 /* offset is always 0 if parser has already popped the stack pointer */
1047 if (semantic_parser
)
1050 sprintf (buf
, "\ncase %d:\n", nrules
);
1051 obstack_grow (&action_obstack
, buf
, strlen (buf
));
1055 sprintf (buf
, "#line %d \"%s\"\n", lineno
, infile
);
1056 obstack_grow (&action_obstack
, buf
, strlen (buf
));
1058 obstack_1grow (&action_obstack
, '{');
1070 obstack_1grow (&action_obstack
, c
);
1075 obstack_1grow (&action_obstack
, c
);
1081 copy_string (finput
, 0, &action_obstack
, c
);
1085 copy_comment (finput
, 0, &action_obstack
);
1089 copy_dollar (finput
, 0, &action_obstack
,
1090 rule
, stack_offset
);
1094 copy_at (finput
, 0, &action_obstack
,
1099 fatal (_("unmatched %s"), "`{'");
1102 obstack_1grow (&action_obstack
, c
);
1108 /* above loop exits when c is '}' */
1112 obstack_1grow (&action_obstack
, c
);
1117 obstack_grow_literal_string (&action_obstack
,
1121 /*-------------------------------------------------------------------.
1122 | After `%guard' is seen in the input file, copy the actual guard |
1123 | into the guards file. If the guard is followed by an action, copy |
1124 | that into the actions file. STACK_OFFSET is the number of values |
1125 | in the current rule so far, which says where to find `$0' with |
1126 | respect to the top of the stack, for the simple parser in which |
1127 | the stack is not popped until after the guard is run. |
1128 `-------------------------------------------------------------------*/
1131 copy_guard (symbol_list
*rule
, int stack_offset
)
1137 /* offset is always 0 if parser has already popped the stack pointer */
1138 if (semantic_parser
)
1141 fprintf (fguard
, "\ncase %d:\n", nrules
);
1143 fprintf (fguard
, "#line %d \"%s\"\n", lineno
, infile
);
1149 while (brace_flag
? (count
> 0) : (c
!= ';'))
1170 complain (_("unmatched %s"), "`}'");
1171 c
= getc (finput
); /* skip it */
1177 copy_string (finput
, fguard
, 0, c
);
1181 copy_comment (finput
, fguard
, 0);
1185 copy_dollar (finput
, fguard
, 0, rule
, stack_offset
);
1189 copy_at (finput
, fguard
, 0, stack_offset
);
1193 fatal ("%s", _("unterminated %guard clause"));
1199 if (c
!= '}' || count
!= 0)
1203 c
= skip_white_space ();
1205 fprintf (fguard
, ";\n break;}");
1207 copy_action (rule
, stack_offset
);
1210 c
= getc (finput
); /* why not skip_white_space -wjh */
1212 copy_action (rule
, stack_offset
);
1220 record_rule_line (void)
1222 /* Record each rule's source line number in rline table. */
1224 if (nrules
>= rline_allocated
)
1226 rline_allocated
= nrules
* 2;
1227 rline
= XREALLOC (rline
, short, rline_allocated
);
1229 rline
[nrules
] = lineno
;
1233 /*-------------------------------------------------------------------.
1234 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1235 | with the user's names. |
1236 `-------------------------------------------------------------------*/
1243 sprintf (token_buffer
, "@%d", ++gensym_count
);
1244 sym
= getsym (token_buffer
);
1245 sym
->class = nterm_sym
;
1246 sym
->value
= nvars
++;
1251 /*------------------------------------------------------------------.
1252 | read in a %type declaration and record its information for |
1253 | get_type_name to access. This is unused. It is only called from |
1254 | the #if 0 part of readgram |
1255 `------------------------------------------------------------------*/
1268 complain (_("invalid %s declaration"), "%type");
1272 name
= xstrdup (token_buffer
);
1287 if (symval
->type_name
== NULL
)
1288 symval
->type_name
= name
;
1289 else if (strcmp (name
, symval
->type_name
) != 0)
1290 complain (_("type redeclaration for %s"), symval
->tag
);
1302 /*------------------------------------------------------------------.
1303 | Parse the input grammar into a one symbol_list structure. Each |
1304 | rule is represented by a sequence of symbols: the left hand side |
1305 | followed by the contents of the right hand side, followed by a |
1306 | null pointer instead of a symbol to terminate the rule. The next |
1307 | symbol is the lhs of the following rule. |
1309 | All guards and actions are copied out to the appropriate files, |
1310 | labelled by the rule number they apply to. |
1311 `------------------------------------------------------------------*/
1322 /* Points to first symbol_list of current rule. its symbol is the
1325 /* Points to the symbol_list preceding crule. */
1326 symbol_list
*crule1
;
1332 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1334 if (t
== IDENTIFIER
|| t
== BAR
)
1336 int action_flag
= 0;
1337 /* Number of symbols in rhs of this rule so far */
1339 int xactions
= 0; /* JF for error checking */
1340 bucket
*first_rhs
= 0;
1342 if (t
== IDENTIFIER
)
1355 complain (_("ill-formed rule: initial symbol not followed by colon"));
1360 if (nrules
== 0 && t
== BAR
)
1362 complain (_("grammar starts with vertical bar"));
1363 lhs
= symval
; /* BOGUS: use a random symval */
1365 /* start a new rule and record its lhs. */
1370 record_rule_line ();
1372 p
= XCALLOC (symbol_list
, 1);
1384 /* mark the rule's lhs as a nonterminal if not already so. */
1386 if (lhs
->class == unknown_sym
)
1388 lhs
->class = nterm_sym
;
1392 else if (lhs
->class == token_sym
)
1393 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1395 /* read the rhs of the rule. */
1403 crule
->ruleprec
= symval
;
1407 if (!(t
== IDENTIFIER
|| t
== LEFT_CURLY
))
1410 /* If next token is an identifier, see if a colon follows it.
1411 If one does, exit this rule now. */
1412 if (t
== IDENTIFIER
)
1424 if (!first_rhs
) /* JF */
1426 /* Not followed by colon =>
1427 process as part of this rule's rhs. */
1430 /* If we just passed an action, that action was in the middle
1431 of a rule, so make a dummy rule to reduce it to a
1437 /* Since the action was written out with this rule's
1438 number, we must give the new rule this number by
1439 inserting the new rule before it. */
1441 /* Make a dummy nonterminal, a gensym. */
1444 /* Make a new rule, whose body is empty,
1445 before the current one, so that the action
1446 just read can belong to it. */
1449 record_rule_line ();
1450 p
= XCALLOC (symbol_list
, 1);
1456 crule1
= XCALLOC (symbol_list
, 1);
1458 crule1
->next
= crule
;
1460 /* Insert the dummy generated by that rule into this
1463 p
= XCALLOC (symbol_list
, 1);
1471 if (t
== IDENTIFIER
)
1474 p
= XCALLOC (symbol_list
, 1);
1479 else /* handle an action. */
1481 copy_action (crule
, rulelength
);
1483 xactions
++; /* JF */
1486 } /* end of read rhs of rule */
1488 /* Put an empty link in the list to mark the end of this rule */
1489 p
= XCALLOC (symbol_list
, 1);
1495 complain (_("two @prec's in a row"));
1497 crule
->ruleprec
= symval
;
1502 if (!semantic_parser
)
1503 complain (_("%%guard present but %%semantic_parser not specified"));
1505 copy_guard (crule
, rulelength
);
1508 else if (t
== LEFT_CURLY
)
1510 /* This case never occurs -wjh */
1512 complain (_("two actions at end of one rule"));
1513 copy_action (crule
, rulelength
);
1515 xactions
++; /* -wjh */
1518 /* If $$ is being set in default way, report if any type
1521 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1523 if (lhs
->type_name
== 0
1524 || first_rhs
->type_name
== 0
1525 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1526 complain (_("type clash (`%s' `%s') on default action"),
1527 lhs
->type_name
? lhs
->type_name
: "",
1528 first_rhs
->type_name
? first_rhs
->type_name
: "");
1530 /* Warn if there is no default for $$ but we need one. */
1531 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1532 complain (_("empty rule for typed nonterminal, and no action"));
1537 /* these things can appear as alternatives to rules. */
1539 a) none of the documentation allows them
1540 b) most of them scan forward until finding a next %
1541 thus they may swallow lots of intervening rules
1543 else if (t
== TOKEN
)
1545 parse_token_decl (token_sym
, nterm_sym
);
1548 else if (t
== NTERM
)
1550 parse_token_decl (nterm_sym
, token_sym
);
1557 else if (t
== UNION
)
1559 parse_union_decl ();
1562 else if (t
== EXPECT
)
1564 parse_expect_decl ();
1567 else if (t
== START
)
1569 parse_start_decl ();
1576 complain (_("invalid input: %s"), token_buffer
);
1581 /* grammar has been read. Do some checking */
1583 if (nsyms
> MAXSHORT
)
1584 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1587 fatal (_("no rules in the input grammar"));
1589 /* JF put out same default YYSTYPE as YACC does */
1591 && !value_components_used
)
1593 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1594 but it seems better to be consistent.
1595 Most programs should declare their own type anyway. */
1596 obstack_grow_literal_string (&attrs_obstack
,
1597 "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1599 fprintf (fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1602 /* Report any undefined symbols and consider them nonterminals. */
1604 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1605 if (bp
->class == unknown_sym
)
1608 ("symbol %s is used, but is not defined as a token and has no rules"),
1610 bp
->class = nterm_sym
;
1611 bp
->value
= nvars
++;
1614 ntokens
= nsyms
- nvars
;
1617 /*--------------------------------------------------------------.
1618 | For named tokens, but not literal ones, define the name. The |
1619 | value is the user token number. |
1620 `--------------------------------------------------------------*/
1623 output_token_defines (FILE *file
)
1629 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1631 symbol
= bp
->tag
; /* get symbol */
1633 if (bp
->value
>= ntokens
)
1635 if (bp
->user_token_number
== SALIAS
)
1637 if ('\'' == *symbol
)
1638 continue; /* skip literal character */
1640 continue; /* skip error token */
1641 if ('\"' == *symbol
)
1643 /* use literal string only if given a symbol with an alias */
1645 symbol
= bp
->alias
->tag
;
1650 /* Don't #define nonliteral tokens whose names contain periods. */
1652 while ((c
= *cp
++) && c
!= '.');
1656 fprintf (file
, "#define\t%s\t%d\n", symbol
,
1657 ((translations
&& !raw_flag
)
1658 ? bp
->user_token_number
: bp
->value
));
1659 if (semantic_parser
)
1660 fprintf (file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1667 /*------------------------------------------------------------------.
1668 | Assign symbol numbers, and write definition of token names into |
1669 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1671 `------------------------------------------------------------------*/
1679 int last_user_token_number
;
1680 static char DOLLAR
[] = "$";
1682 /* int lossage = 0; JF set but not used */
1684 tags
= XCALLOC (char *, nsyms
+ 1);
1686 user_toknums
= XCALLOC (short, nsyms
+ 1);
1687 user_toknums
[0] = 0;
1689 sprec
= XCALLOC (short, nsyms
);
1690 sassoc
= XCALLOC (short, nsyms
);
1692 max_user_token_number
= 256;
1693 last_user_token_number
= 256;
1695 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1697 if (bp
->class == nterm_sym
)
1699 bp
->value
+= ntokens
;
1703 /* this symbol and its alias are a single token defn.
1704 allocate a tokno, and assign to both check agreement of
1705 ->prec and ->assoc fields and make both the same */
1707 bp
->value
= bp
->alias
->value
= tokno
++;
1709 if (bp
->prec
!= bp
->alias
->prec
)
1711 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1712 && bp
->user_token_number
== SALIAS
)
1713 complain (_("conflicting precedences for %s and %s"),
1714 bp
->tag
, bp
->alias
->tag
);
1716 bp
->alias
->prec
= bp
->prec
;
1718 bp
->prec
= bp
->alias
->prec
;
1721 if (bp
->assoc
!= bp
->alias
->assoc
)
1723 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1724 && bp
->user_token_number
== SALIAS
)
1725 complain (_("conflicting assoc values for %s and %s"),
1726 bp
->tag
, bp
->alias
->tag
);
1728 bp
->alias
->assoc
= bp
->assoc
;
1730 bp
->assoc
= bp
->alias
->assoc
;
1733 if (bp
->user_token_number
== SALIAS
)
1734 continue; /* do not do processing below for SALIASs */
1737 else /* bp->class == token_sym */
1739 bp
->value
= tokno
++;
1742 if (bp
->class == token_sym
)
1744 if (translations
&& !(bp
->user_token_number
))
1745 bp
->user_token_number
= ++last_user_token_number
;
1746 if (bp
->user_token_number
> max_user_token_number
)
1747 max_user_token_number
= bp
->user_token_number
;
1750 tags
[bp
->value
] = bp
->tag
;
1751 user_toknums
[bp
->value
] = bp
->user_token_number
;
1752 sprec
[bp
->value
] = bp
->prec
;
1753 sassoc
[bp
->value
] = bp
->assoc
;
1761 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1763 /* initialize all entries for literal tokens to 2, the internal
1764 token number for $undefined., which represents all invalid
1766 for (j
= 0; j
<= max_user_token_number
; j
++)
1767 token_translations
[j
] = 2;
1769 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1771 if (bp
->value
>= ntokens
)
1772 continue; /* non-terminal */
1773 if (bp
->user_token_number
== SALIAS
)
1775 if (token_translations
[bp
->user_token_number
] != 2)
1776 complain (_("tokens %s and %s both assigned number %d"),
1777 tags
[token_translations
[bp
->user_token_number
]],
1778 bp
->tag
, bp
->user_token_number
);
1779 token_translations
[bp
->user_token_number
] = bp
->value
;
1783 error_token_number
= errtoken
->value
;
1785 if (!no_parser_flag
)
1786 output_token_defines (ftable
);
1788 if (startval
->class == unknown_sym
)
1789 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1790 else if (startval
->class == token_sym
)
1791 fatal (_("the start symbol %s is a token"), startval
->tag
);
1793 start_symbol
= startval
->value
;
1797 output_token_defines (fdefines
);
1801 if (spec_name_prefix
)
1802 fprintf (fdefines
, "\nextern YYSTYPE %slval;\n",
1805 fprintf (fdefines
, "\nextern YYSTYPE yylval;\n");
1808 if (semantic_parser
)
1809 for (i
= ntokens
; i
< nsyms
; i
++)
1811 /* don't make these for dummy nonterminals made by gensym. */
1812 if (*tags
[i
] != '@')
1813 fprintf (fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1816 /* `fdefines' is now a temporary file, so we need to copy its
1817 contents in `done', so we can't close it here. */
1825 /*---------------------------------------------------------------.
1826 | Convert the rules into the representation using RRHS, RLHS and |
1828 `---------------------------------------------------------------*/
1839 ritem
= XCALLOC (short, nitems
+ 1);
1840 rlhs
= XCALLOC (short, nrules
) - 1;
1841 rrhs
= XCALLOC (short, nrules
) - 1;
1842 rprec
= XCALLOC (short, nrules
) - 1;
1843 rprecsym
= XCALLOC (short, nrules
) - 1;
1844 rassoc
= XCALLOC (short, nrules
) - 1;
1852 rlhs
[ruleno
] = p
->sym
->value
;
1853 rrhs
[ruleno
] = itemno
;
1854 ruleprec
= p
->ruleprec
;
1859 ritem
[itemno
++] = p
->sym
->value
;
1860 /* A rule gets by default the precedence and associativity
1861 of the last token in it. */
1862 if (p
->sym
->class == token_sym
)
1864 rprec
[ruleno
] = p
->sym
->prec
;
1865 rassoc
[ruleno
] = p
->sym
->assoc
;
1871 /* If this rule has a %prec,
1872 the specified symbol's precedence replaces the default. */
1875 rprec
[ruleno
] = ruleprec
->prec
;
1876 rassoc
[ruleno
] = ruleprec
->assoc
;
1877 rprecsym
[ruleno
] = ruleprec
->value
;
1880 ritem
[itemno
++] = -ruleno
;
1890 /*-------------------------------------------------------------------.
1891 | Read in the grammar specification and record it in the format |
1892 | described in gram.h. All guards are copied into the FGUARD file |
1893 | and all actions into ACTION_OBSTACK, in each case forming the body |
1894 | of a C function (YYGUARD or YYACTION) which contains a switch |
1895 | statement to decide which guard or action to execute. |
1896 `-------------------------------------------------------------------*/
1902 startval
= NULL
; /* start symbol not specified yet. */
1905 /* initially assume token number translation not needed. */
1908 /* Nowadays translations is always set to 1, since we give `error' a
1909 user-token-number to satisfy the Posix demand for YYERRCODE==256.
1917 rline_allocated
= 10;
1918 rline
= XCALLOC (short, rline_allocated
);
1925 semantic_parser
= 0;
1933 /* Initialize the symbol table. */
1935 /* Construct the error token */
1936 errtoken
= getsym ("error");
1937 errtoken
->class = token_sym
;
1938 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1939 /* Construct a token that represents all undefined literal tokens.
1940 It is always token number 2. */
1941 undeftoken
= getsym ("$undefined.");
1942 undeftoken
->class = token_sym
;
1943 undeftoken
->user_token_number
= 2;
1945 /* Read the declaration section. Copy %{ ... %} groups to FTABLE
1946 and FDEFINES file. Also notice any %token, %left, etc. found
1948 putc ('\n', ftable
);
1950 /* %s, made from %s\n\
1951 by GNU bison %s. */\n\
1952 \n", no_parser_flag
? "Bison-generated parse tables" : "A Bison parser", infile
, VERSION
);
1954 fputs ("#define YYBISON 1 /* Identify Bison output. */\n\n", ftable
);
1955 read_declarations ();
1956 /* Start writing the guard and action files, if they are needed. */
1958 /* Read in the grammar, build grammar in list form. Write out
1959 guards and actions. */
1961 /* Now we know whether we need the line-number stack. If we do,
1962 write its type into the .tab.h file. */
1964 reader_output_yylsp (fdefines
);
1965 /* Write closing delimiters for actions and guards. */
1968 fputs ("#define YYLSP_NEEDED\n\n", ftable
);
1969 /* Assign the symbols their symbol numbers. Write #defines for the
1970 token symbols into FDEFINES if requested. */
1972 /* Convert the grammar into the format described in gram.h. */
1974 /* Free the symbol table data structure since symbols are now all
1975 referred to by symbol number. */
1981 reader_output_yylsp (FILE *f
)
1987 typedef struct yyltype\n\
1998 # define YYLTYPE yyltype\n\