]>
git.saurik.com Git - bison.git/blob - src/reader.c
3ef59350cfc8bcf229515d444a50a9085991c25f
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
,
223 struct obstack
*oout2
, struct obstack
*oout
)
229 /* We read a `/', output it. */
233 obstack_1grow (oout
, '/');
235 obstack_1grow (oout2
, '/');
237 switch ((c
= getc (fin
)))
253 obstack_1grow (oout
, c
);
255 obstack_1grow (oout2
, c
);
261 if (!cplus_comment
&& c
== '*')
268 obstack_1grow (oout
, c
);
270 obstack_1grow (oout2
, c
);
279 obstack_1grow (oout
, c
);
281 obstack_1grow (oout2
, c
);
291 obstack_1grow (oout
, c
);
293 obstack_1grow (oout2
, c
);
300 fatal (_("unterminated comment"));
306 obstack_1grow (oout
, c
);
308 obstack_1grow (oout2
, c
);
315 /*-------------------------------------------------------------------.
316 | Dump the comment (actually the current string starting with a `/') |
317 | from FIN to FOUT. |
318 `-------------------------------------------------------------------*/
321 copy_comment (FILE *fin
, FILE *fout
, struct obstack
*oout
)
323 copy_comment2 (fin
, fout
, NULL
, oout
);
327 /*-----------------------------------------------------------------.
328 | FIN is pointing to a location (i.e., a `@'). Output to FOUT a |
329 | reference to this location. STACK_OFFSET is the number of values |
330 | in the current rule so far, which says where to find `$0' with |
331 | respect to the top of the stack. |
332 `-----------------------------------------------------------------*/
335 copy_at (FILE *fin
, FILE *fout
, struct obstack
*oout
, int stack_offset
)
343 fprintf (fout
, "yyloc");
345 obstack_grow_literal_string (oout
, "yyloc");
348 else if (isdigit (c
) || c
== '-')
354 n
= read_signed_integer (fin
);
356 sprintf (buf
, "yylsp[%d]", n
- stack_offset
);
360 obstack_grow (oout
, buf
, strlen (buf
));
367 complain (_("%s is invalid"), quote (buf
));
372 /*-------------------------------------------------------------------.
373 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
375 | Possible inputs: $[<TYPENAME>]($|integer) |
377 | Output to FOUT a reference to this semantic value. STACK_OFFSET is |
378 | the number of values in the current rule so far, which says where |
379 | to find `$0' with respect to the top of the stack. |
380 `-------------------------------------------------------------------*/
383 copy_dollar (FILE *fin
, FILE *fout
, struct obstack
*oout
,
384 symbol_list
*rule
, int stack_offset
)
387 char *type_name
= NULL
;
389 /* Get the type name if explicit. */
392 read_type_name (fin
);
393 type_name
= token_buffer
;
394 value_components_used
= 1;
401 fputs ("yyval", fout
);
403 obstack_grow_literal_string (oout
, "yyval");
406 type_name
= get_type_name (0, rule
);
410 fprintf (fout
, ".%s", type_name
);
412 obstack_fgrow1 (oout
, ".%s", type_name
);
414 if (!type_name
&& typed
)
415 complain (_("$$ of `%s' has no declared type"),
418 else if (isdigit (c
) || c
== '-')
422 n
= read_signed_integer (fin
);
424 if (!type_name
&& n
> 0)
425 type_name
= get_type_name (n
, rule
);
428 fprintf (fout
, "yyvsp[%d]", n
- stack_offset
);
430 obstack_fgrow1 (oout
, "yyvsp[%d]", n
- stack_offset
);
435 fprintf (fout
, ".%s", type_name
);
437 obstack_fgrow1 (oout
, ".%s", type_name
);
439 if (!type_name
&& typed
)
440 complain (_("$%d of `%s' has no declared type"),
447 complain (_("%s is invalid"), quote (buf
));
451 /*-------------------------------------------------------------------.
452 | Copy the contents of a `%{ ... %}' into the definitions file. The |
453 | `%{' has already been read. Return after reading the `%}'. |
454 `-------------------------------------------------------------------*/
457 copy_definition (void)
460 /* -1 while reading a character if prev char was %. */
464 obstack_fgrow2 (&attrs_obstack
, "#line %d \"%s\"\n", lineno
, infile
);
475 obstack_1grow (&attrs_obstack
, c
);
485 copy_string (finput
, 0, &attrs_obstack
, c
);
489 copy_comment (finput
, 0, &attrs_obstack
);
493 fatal ("%s", _("unterminated `%{' definition"));
496 obstack_1grow (&attrs_obstack
, c
);
505 obstack_1grow (&attrs_obstack
, '%');
512 /*-------------------------------------------------------------------.
513 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
514 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
516 `-------------------------------------------------------------------*/
519 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
524 /* The symbol being defined. */
525 struct bucket
*symbol
= NULL
;
527 /* After `%token' and `%nterm', any number of symbols maybe be
531 int tmp_char
= ungetc (skip_white_space (), finput
);
533 /* `%' (for instance from `%token', or from `%%' etc.) is the
534 only valid means to end this declaration. */
538 fatal (_("Premature EOF after %s"), token_buffer
);
546 if (token
== TYPENAME
)
548 typename
= xstrdup (token_buffer
);
549 value_components_used
= 1;
552 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"' && symbol
)
555 warn (_("symbol `%s' used more than once as a literal string"),
557 else if (symbol
->alias
)
558 warn (_("symbol `%s' given more than one literal string"),
562 symval
->class = token_sym
;
563 symval
->type_name
= typename
;
564 symval
->user_token_number
= symbol
->user_token_number
;
565 symbol
->user_token_number
= SALIAS
;
566 symval
->alias
= symbol
;
567 symbol
->alias
= symval
;
568 /* symbol and symval combined are only one symbol */
574 else if (token
== IDENTIFIER
)
576 int oldclass
= symval
->class;
579 if (symbol
->class == what_is_not
)
580 complain (_("symbol %s redefined"), symbol
->tag
);
581 symbol
->class = what_is
;
582 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
583 symbol
->value
= nvars
++;
587 if (symbol
->type_name
== NULL
)
588 symbol
->type_name
= typename
;
589 else if (strcmp (typename
, symbol
->type_name
) != 0)
590 complain (_("type redeclaration for %s"), symbol
->tag
);
593 else if (symbol
&& token
== NUMBER
)
595 symbol
->user_token_number
= numval
;
600 complain (_("`%s' is invalid in %s"),
601 token_buffer
, (what_is
== token_sym
) ? "%token" : "%nterm");
609 /*------------------------------.
610 | Parse what comes after %start |
611 `------------------------------*/
614 parse_start_decl (void)
617 complain (_("multiple %s declarations"), "%start");
618 if (lex () != IDENTIFIER
)
619 complain (_("invalid %s declaration"), "%start");
627 /*-----------------------------------------------------------.
628 | read in a %type declaration and record its information for |
629 | get_type_name to access |
630 `-----------------------------------------------------------*/
633 parse_type_decl (void)
637 if (lex () != TYPENAME
)
639 complain ("%s", _("%type declaration has no <typename>"));
644 name
= xstrdup (token_buffer
);
649 int tmp_char
= ungetc (skip_white_space (), finput
);
654 fatal (_("Premature EOF after %s"), token_buffer
);
666 if (symval
->type_name
== NULL
)
667 symval
->type_name
= name
;
668 else if (strcmp (name
, symval
->type_name
) != 0)
669 complain (_("type redeclaration for %s"), symval
->tag
);
674 complain (_("invalid %%type declaration due to item: %s"),
683 /*----------------------------------------------------------------.
684 | Read in a %left, %right or %nonassoc declaration and record its |
686 `----------------------------------------------------------------*/
689 parse_assoc_decl (associativity assoc
)
694 lastprec
++; /* Assign a new precedence level, never 0. */
699 int tmp_char
= ungetc (skip_white_space (), finput
);
704 fatal (_("Premature EOF after %s"), token_buffer
);
711 name
= xstrdup (token_buffer
);
718 if (symval
->prec
!= 0)
719 complain (_("redefining precedence of %s"), symval
->tag
);
720 symval
->prec
= lastprec
;
721 symval
->assoc
= assoc
;
722 if (symval
->class == nterm_sym
)
723 complain (_("symbol %s redefined"), symval
->tag
);
724 symval
->class = token_sym
;
726 { /* record the type, if one is specified */
727 if (symval
->type_name
== NULL
)
728 symval
->type_name
= name
;
729 else if (strcmp (name
, symval
->type_name
) != 0)
730 complain (_("type redeclaration for %s"), symval
->tag
);
735 if (prev
== IDENTIFIER
)
737 symval
->user_token_number
= numval
;
743 ("invalid text (%s) - number should be after identifier"),
753 complain (_("unexpected item: %s"), token_buffer
);
764 /*--------------------------------------------------------------.
765 | Copy the union declaration into ATTRS_OBSTACK (and fdefines), |
766 | where it is made into the definition of YYSTYPE, the type of |
767 | elements of the parser value stack. |
768 `--------------------------------------------------------------*/
771 parse_union_decl (void)
777 complain (_("multiple %s declarations"), "%union");
782 obstack_fgrow2 (&attrs_obstack
, "\n#line %d \"%s\"\n", lineno
, infile
);
784 obstack_1grow (&attrs_obstack
, '\n');
786 obstack_grow_literal_string (&attrs_obstack
, "typedef union");
788 obstack_grow_literal_string (&defines_obstack
, "typedef union");
794 obstack_1grow (&attrs_obstack
, c
);
796 obstack_grow_literal_string (&defines_obstack
, c
);
805 copy_comment2 (finput
, 0, &defines_obstack
, &attrs_obstack
);
814 complain (_("unmatched %s"), "`}'");
818 obstack_grow_literal_string (&attrs_obstack
, " YYSTYPE;\n");
820 obstack_grow_literal_string (&defines_obstack
, " YYSTYPE;\n");
821 /* JF don't choke on trailing semi */
822 c
= skip_white_space ();
834 /*-------------------------------------------------------.
835 | Parse the declaration %expect N which says to expect N |
836 | shift-reduce conflicts. |
837 `-------------------------------------------------------*/
840 parse_expect_decl (void)
842 int c
= skip_white_space ();
846 complain (_("argument of %%expect is not an integer"));
848 expected_conflicts
= read_signed_integer (finput
);
852 /*-------------------------------------------------------------------.
853 | Parse what comes after %thong. the full syntax is |
855 | %thong <type> token number literal |
857 | the <type> or number may be omitted. The number specifies the |
858 | user_token_number. |
860 | Two symbols are entered in the table, one for the token symbol and |
861 | one for the literal. Both are given the <type>, if any, from the |
862 | declaration. The ->user_token_number of the first is SALIAS and |
863 | the ->user_token_number of the second is set to the number, if |
864 | any, from the declaration. The two symbols are linked via |
865 | pointers in their ->alias fields. |
867 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
868 | only the literal string is retained it is the literal string that |
869 | is output to yytname |
870 `-------------------------------------------------------------------*/
873 parse_thong_decl (void)
876 struct bucket
*symbol
;
881 token
= lex (); /* fetch typename or first token */
882 if (token
== TYPENAME
)
884 typename
= xstrdup (token_buffer
);
885 value_components_used
= 1;
886 token
= lex (); /* fetch first token */
889 /* process first token */
891 if (token
!= IDENTIFIER
)
893 complain (_("unrecognized item %s, expected an identifier"),
898 symval
->class = token_sym
;
899 symval
->type_name
= typename
;
900 symval
->user_token_number
= SALIAS
;
903 token
= lex (); /* get number or literal string */
908 token
= lex (); /* okay, did number, now get literal */
913 /* process literal string token */
915 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
917 complain (_("expected string constant instead of %s"), token_buffer
);
921 symval
->class = token_sym
;
922 symval
->type_name
= typename
;
923 symval
->user_token_number
= usrtoknum
;
925 symval
->alias
= symbol
;
926 symbol
->alias
= symval
;
928 /* symbol and symval combined are only one symbol. */
933 /*----------------------------------------------------------------.
934 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
935 | any `%' declarations, and copy the contents of any `%{ ... %}' |
936 | groups to ATTRS_OBSTACK. |
937 `----------------------------------------------------------------*/
940 read_declarations (void)
947 c
= skip_white_space ();
951 tok
= parse_percent_token ();
958 case PERCENT_LEFT_CURLY
:
963 parse_token_decl (token_sym
, nterm_sym
);
967 parse_token_decl (nterm_sym
, token_sym
);
983 parse_expect_decl ();
990 parse_assoc_decl (left_assoc
);
994 parse_assoc_decl (right_assoc
);
998 parse_assoc_decl (non_assoc
);
1001 case SEMANTIC_PARSER
:
1002 if (semantic_parser
== 0)
1004 semantic_parser
= 1;
1005 open_extra_files ();
1017 complain (_("unrecognized: %s"), token_buffer
);
1022 fatal (_("no input grammar"));
1027 complain (_("unknown character: %s"), quote (buf
));
1033 /*-------------------------------------------------------------------.
1034 | Assuming that a `{' has just been seen, copy everything up to the |
1035 | matching `}' into the actions file. STACK_OFFSET is the number of |
1036 | values in the current rule so far, which says where to find `$0' |
1037 | with respect to the top of the stack. |
1038 `-------------------------------------------------------------------*/
1041 copy_action (symbol_list
*rule
, int stack_offset
)
1047 /* offset is always 0 if parser has already popped the stack pointer */
1048 if (semantic_parser
)
1051 sprintf (buf
, "\ncase %d:\n", nrules
);
1052 obstack_grow (&action_obstack
, buf
, strlen (buf
));
1056 sprintf (buf
, "#line %d \"%s\"\n", lineno
, infile
);
1057 obstack_grow (&action_obstack
, buf
, strlen (buf
));
1059 obstack_1grow (&action_obstack
, '{');
1071 obstack_1grow (&action_obstack
, c
);
1076 obstack_1grow (&action_obstack
, c
);
1082 copy_string (finput
, 0, &action_obstack
, c
);
1086 copy_comment (finput
, 0, &action_obstack
);
1090 copy_dollar (finput
, 0, &action_obstack
,
1091 rule
, stack_offset
);
1095 copy_at (finput
, 0, &action_obstack
,
1100 fatal (_("unmatched %s"), "`{'");
1103 obstack_1grow (&action_obstack
, c
);
1109 /* above loop exits when c is '}' */
1113 obstack_1grow (&action_obstack
, c
);
1118 obstack_grow_literal_string (&action_obstack
,
1122 /*-------------------------------------------------------------------.
1123 | After `%guard' is seen in the input file, copy the actual guard |
1124 | into the guards file. If the guard is followed by an action, copy |
1125 | that into the actions file. STACK_OFFSET is the number of values |
1126 | in the current rule so far, which says where to find `$0' with |
1127 | respect to the top of the stack, for the simple parser in which |
1128 | the stack is not popped until after the guard is run. |
1129 `-------------------------------------------------------------------*/
1132 copy_guard (symbol_list
*rule
, int stack_offset
)
1138 /* offset is always 0 if parser has already popped the stack pointer */
1139 if (semantic_parser
)
1142 fprintf (fguard
, "\ncase %d:\n", nrules
);
1144 fprintf (fguard
, "#line %d \"%s\"\n", lineno
, infile
);
1150 while (brace_flag
? (count
> 0) : (c
!= ';'))
1171 complain (_("unmatched %s"), "`}'");
1172 c
= getc (finput
); /* skip it */
1178 copy_string (finput
, fguard
, 0, c
);
1182 copy_comment (finput
, fguard
, 0);
1186 copy_dollar (finput
, fguard
, 0, rule
, stack_offset
);
1190 copy_at (finput
, fguard
, 0, stack_offset
);
1194 fatal ("%s", _("unterminated %guard clause"));
1200 if (c
!= '}' || count
!= 0)
1204 c
= skip_white_space ();
1206 fprintf (fguard
, ";\n break;}");
1208 copy_action (rule
, stack_offset
);
1211 c
= getc (finput
); /* why not skip_white_space -wjh */
1213 copy_action (rule
, stack_offset
);
1221 record_rule_line (void)
1223 /* Record each rule's source line number in rline table. */
1225 if (nrules
>= rline_allocated
)
1227 rline_allocated
= nrules
* 2;
1228 rline
= XREALLOC (rline
, short, rline_allocated
);
1230 rline
[nrules
] = lineno
;
1234 /*-------------------------------------------------------------------.
1235 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1236 | with the user's names. |
1237 `-------------------------------------------------------------------*/
1244 sprintf (token_buffer
, "@%d", ++gensym_count
);
1245 sym
= getsym (token_buffer
);
1246 sym
->class = nterm_sym
;
1247 sym
->value
= nvars
++;
1252 /*------------------------------------------------------------------.
1253 | read in a %type declaration and record its information for |
1254 | get_type_name to access. This is unused. It is only called from |
1255 | the #if 0 part of readgram |
1256 `------------------------------------------------------------------*/
1269 complain (_("invalid %s declaration"), "%type");
1273 name
= xstrdup (token_buffer
);
1288 if (symval
->type_name
== NULL
)
1289 symval
->type_name
= name
;
1290 else if (strcmp (name
, symval
->type_name
) != 0)
1291 complain (_("type redeclaration for %s"), symval
->tag
);
1303 /*------------------------------------------------------------------.
1304 | Parse the input grammar into a one symbol_list structure. Each |
1305 | rule is represented by a sequence of symbols: the left hand side |
1306 | followed by the contents of the right hand side, followed by a |
1307 | null pointer instead of a symbol to terminate the rule. The next |
1308 | symbol is the lhs of the following rule. |
1310 | All guards and actions are copied out to the appropriate files, |
1311 | labelled by the rule number they apply to. |
1312 `------------------------------------------------------------------*/
1323 /* Points to first symbol_list of current rule. its symbol is the
1326 /* Points to the symbol_list preceding crule. */
1327 symbol_list
*crule1
;
1333 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1335 if (t
== IDENTIFIER
|| t
== BAR
)
1337 int action_flag
= 0;
1338 /* Number of symbols in rhs of this rule so far */
1340 int xactions
= 0; /* JF for error checking */
1341 bucket
*first_rhs
= 0;
1343 if (t
== IDENTIFIER
)
1356 complain (_("ill-formed rule: initial symbol not followed by colon"));
1361 if (nrules
== 0 && t
== BAR
)
1363 complain (_("grammar starts with vertical bar"));
1364 lhs
= symval
; /* BOGUS: use a random symval */
1366 /* start a new rule and record its lhs. */
1371 record_rule_line ();
1373 p
= XCALLOC (symbol_list
, 1);
1385 /* mark the rule's lhs as a nonterminal if not already so. */
1387 if (lhs
->class == unknown_sym
)
1389 lhs
->class = nterm_sym
;
1393 else if (lhs
->class == token_sym
)
1394 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1396 /* read the rhs of the rule. */
1404 crule
->ruleprec
= symval
;
1408 if (!(t
== IDENTIFIER
|| t
== LEFT_CURLY
))
1411 /* If next token is an identifier, see if a colon follows it.
1412 If one does, exit this rule now. */
1413 if (t
== IDENTIFIER
)
1425 if (!first_rhs
) /* JF */
1427 /* Not followed by colon =>
1428 process as part of this rule's rhs. */
1431 /* If we just passed an action, that action was in the middle
1432 of a rule, so make a dummy rule to reduce it to a
1438 /* Since the action was written out with this rule's
1439 number, we must give the new rule this number by
1440 inserting the new rule before it. */
1442 /* Make a dummy nonterminal, a gensym. */
1445 /* Make a new rule, whose body is empty,
1446 before the current one, so that the action
1447 just read can belong to it. */
1450 record_rule_line ();
1451 p
= XCALLOC (symbol_list
, 1);
1457 crule1
= XCALLOC (symbol_list
, 1);
1459 crule1
->next
= crule
;
1461 /* Insert the dummy generated by that rule into this
1464 p
= XCALLOC (symbol_list
, 1);
1472 if (t
== IDENTIFIER
)
1475 p
= XCALLOC (symbol_list
, 1);
1480 else /* handle an action. */
1482 copy_action (crule
, rulelength
);
1484 xactions
++; /* JF */
1487 } /* end of read rhs of rule */
1489 /* Put an empty link in the list to mark the end of this rule */
1490 p
= XCALLOC (symbol_list
, 1);
1496 complain (_("two @prec's in a row"));
1498 crule
->ruleprec
= symval
;
1503 if (!semantic_parser
)
1504 complain (_("%%guard present but %%semantic_parser not specified"));
1506 copy_guard (crule
, rulelength
);
1509 else if (t
== LEFT_CURLY
)
1511 /* This case never occurs -wjh */
1513 complain (_("two actions at end of one rule"));
1514 copy_action (crule
, rulelength
);
1516 xactions
++; /* -wjh */
1519 /* If $$ is being set in default way, report if any type
1522 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1524 if (lhs
->type_name
== 0
1525 || first_rhs
->type_name
== 0
1526 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1527 complain (_("type clash (`%s' `%s') on default action"),
1528 lhs
->type_name
? lhs
->type_name
: "",
1529 first_rhs
->type_name
? first_rhs
->type_name
: "");
1531 /* Warn if there is no default for $$ but we need one. */
1532 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1533 complain (_("empty rule for typed nonterminal, and no action"));
1538 /* these things can appear as alternatives to rules. */
1540 a) none of the documentation allows them
1541 b) most of them scan forward until finding a next %
1542 thus they may swallow lots of intervening rules
1544 else if (t
== TOKEN
)
1546 parse_token_decl (token_sym
, nterm_sym
);
1549 else if (t
== NTERM
)
1551 parse_token_decl (nterm_sym
, token_sym
);
1558 else if (t
== UNION
)
1560 parse_union_decl ();
1563 else if (t
== EXPECT
)
1565 parse_expect_decl ();
1568 else if (t
== START
)
1570 parse_start_decl ();
1577 complain (_("invalid input: %s"), token_buffer
);
1582 /* grammar has been read. Do some checking */
1584 if (nsyms
> MAXSHORT
)
1585 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1588 fatal (_("no rules in the input grammar"));
1590 /* JF put out same default YYSTYPE as YACC does */
1592 && !value_components_used
)
1594 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1595 but it seems better to be consistent.
1596 Most programs should declare their own type anyway. */
1597 obstack_grow_literal_string (&attrs_obstack
,
1598 "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1600 obstack_grow_literal_string (&defines_obstack
, "\
1602 # define YYSTYPE int\n\
1606 /* Report any undefined symbols and consider them nonterminals. */
1608 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1609 if (bp
->class == unknown_sym
)
1612 ("symbol %s is used, but is not defined as a token and has no rules"),
1614 bp
->class = nterm_sym
;
1615 bp
->value
= nvars
++;
1618 ntokens
= nsyms
- nvars
;
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",
1662 ((translations
&& !raw_flag
)
1663 ? bp
->user_token_number
: bp
->value
));
1664 if (semantic_parser
)
1665 obstack_fgrow2 (oout
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1668 obstack_1grow (oout
, '\n');
1672 /*------------------------------------------------------------------.
1673 | Assign symbol numbers, and write definition of token names into |
1674 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1676 `------------------------------------------------------------------*/
1684 int last_user_token_number
;
1685 static char DOLLAR
[] = "$";
1687 /* int lossage = 0; JF set but not used */
1689 tags
= XCALLOC (char *, nsyms
+ 1);
1691 user_toknums
= XCALLOC (short, nsyms
+ 1);
1692 user_toknums
[0] = 0;
1694 sprec
= XCALLOC (short, nsyms
);
1695 sassoc
= XCALLOC (short, nsyms
);
1697 max_user_token_number
= 256;
1698 last_user_token_number
= 256;
1700 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1702 if (bp
->class == nterm_sym
)
1704 bp
->value
+= ntokens
;
1708 /* this symbol and its alias are a single token defn.
1709 allocate a tokno, and assign to both check agreement of
1710 ->prec and ->assoc fields and make both the same */
1712 bp
->value
= bp
->alias
->value
= tokno
++;
1714 if (bp
->prec
!= bp
->alias
->prec
)
1716 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1717 && bp
->user_token_number
== SALIAS
)
1718 complain (_("conflicting precedences for %s and %s"),
1719 bp
->tag
, bp
->alias
->tag
);
1721 bp
->alias
->prec
= bp
->prec
;
1723 bp
->prec
= bp
->alias
->prec
;
1726 if (bp
->assoc
!= bp
->alias
->assoc
)
1728 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1729 && bp
->user_token_number
== SALIAS
)
1730 complain (_("conflicting assoc values for %s and %s"),
1731 bp
->tag
, bp
->alias
->tag
);
1733 bp
->alias
->assoc
= bp
->assoc
;
1735 bp
->assoc
= bp
->alias
->assoc
;
1738 if (bp
->user_token_number
== SALIAS
)
1739 continue; /* do not do processing below for SALIASs */
1742 else /* bp->class == token_sym */
1744 bp
->value
= tokno
++;
1747 if (bp
->class == token_sym
)
1749 if (translations
&& !(bp
->user_token_number
))
1750 bp
->user_token_number
= ++last_user_token_number
;
1751 if (bp
->user_token_number
> max_user_token_number
)
1752 max_user_token_number
= bp
->user_token_number
;
1755 tags
[bp
->value
] = bp
->tag
;
1756 user_toknums
[bp
->value
] = bp
->user_token_number
;
1757 sprec
[bp
->value
] = bp
->prec
;
1758 sassoc
[bp
->value
] = bp
->assoc
;
1766 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1768 /* initialize all entries for literal tokens to 2, the internal
1769 token number for $undefined., which represents all invalid
1771 for (j
= 0; j
<= max_user_token_number
; j
++)
1772 token_translations
[j
] = 2;
1774 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1776 if (bp
->value
>= ntokens
)
1777 continue; /* non-terminal */
1778 if (bp
->user_token_number
== SALIAS
)
1780 if (token_translations
[bp
->user_token_number
] != 2)
1781 complain (_("tokens %s and %s both assigned number %d"),
1782 tags
[token_translations
[bp
->user_token_number
]],
1783 bp
->tag
, bp
->user_token_number
);
1784 token_translations
[bp
->user_token_number
] = bp
->value
;
1788 error_token_number
= errtoken
->value
;
1790 if (!no_parser_flag
)
1791 output_token_defines (&table_obstack
);
1793 if (startval
->class == unknown_sym
)
1794 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1795 else if (startval
->class == token_sym
)
1796 fatal (_("the start symbol %s is a token"), startval
->tag
);
1798 start_symbol
= startval
->value
;
1802 output_token_defines (&defines_obstack
);
1806 if (spec_name_prefix
)
1807 obstack_fgrow1 (&defines_obstack
, "\nextern YYSTYPE %slval;\n",
1810 obstack_grow_literal_string (&defines_obstack
,
1811 "\nextern YYSTYPE yylval;\n");
1814 if (semantic_parser
)
1815 for (i
= ntokens
; i
< nsyms
; i
++)
1817 /* don't make these for dummy nonterminals made by gensym. */
1818 if (*tags
[i
] != '@')
1819 obstack_fgrow2 (&defines_obstack
,
1820 "#define\tNT%s\t%d\n", tags
[i
], i
);
1823 /* `fdefines' is now a temporary file, so we need to copy its
1824 contents in `done', so we can't close it here. */
1832 /*---------------------------------------------------------------.
1833 | Convert the rules into the representation using RRHS, RLHS and |
1835 `---------------------------------------------------------------*/
1846 ritem
= XCALLOC (short, nitems
+ 1);
1847 rlhs
= XCALLOC (short, nrules
) - 1;
1848 rrhs
= XCALLOC (short, nrules
) - 1;
1849 rprec
= XCALLOC (short, nrules
) - 1;
1850 rprecsym
= XCALLOC (short, nrules
) - 1;
1851 rassoc
= XCALLOC (short, nrules
) - 1;
1859 rlhs
[ruleno
] = p
->sym
->value
;
1860 rrhs
[ruleno
] = itemno
;
1861 ruleprec
= p
->ruleprec
;
1866 ritem
[itemno
++] = p
->sym
->value
;
1867 /* A rule gets by default the precedence and associativity
1868 of the last token in it. */
1869 if (p
->sym
->class == token_sym
)
1871 rprec
[ruleno
] = p
->sym
->prec
;
1872 rassoc
[ruleno
] = p
->sym
->assoc
;
1878 /* If this rule has a %prec,
1879 the specified symbol's precedence replaces the default. */
1882 rprec
[ruleno
] = ruleprec
->prec
;
1883 rassoc
[ruleno
] = ruleprec
->assoc
;
1884 rprecsym
[ruleno
] = ruleprec
->value
;
1887 ritem
[itemno
++] = -ruleno
;
1897 /*-------------------------------------------------------------------.
1898 | Read in the grammar specification and record it in the format |
1899 | described in gram.h. All guards are copied into the FGUARD file |
1900 | and all actions into ACTION_OBSTACK, in each case forming the body |
1901 | of a C function (YYGUARD or YYACTION) which contains a switch |
1902 | statement to decide which guard or action to execute. |
1903 `-------------------------------------------------------------------*/
1909 startval
= NULL
; /* start symbol not specified yet. */
1912 /* initially assume token number translation not needed. */
1915 /* Nowadays translations is always set to 1, since we give `error' a
1916 user-token-number to satisfy the Posix demand for YYERRCODE==256.
1924 rline_allocated
= 10;
1925 rline
= XCALLOC (short, rline_allocated
);
1932 semantic_parser
= 0;
1940 /* Initialize the symbol table. */
1942 /* Construct the error token */
1943 errtoken
= getsym ("error");
1944 errtoken
->class = token_sym
;
1945 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1946 /* Construct a token that represents all undefined literal tokens.
1947 It is always token number 2. */
1948 undeftoken
= getsym ("$undefined.");
1949 undeftoken
->class = token_sym
;
1950 undeftoken
->user_token_number
= 2;
1952 /* Read the declaration section. Copy %{ ... %} groups to
1953 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1954 etc. found there. */
1955 obstack_1grow (&table_obstack
, '\n');
1956 obstack_fgrow3 (&table_obstack
, "\
1957 /* %s, made from %s\n\
1958 by GNU bison %s. */\n\
1960 no_parser_flag
? "Bison-generated parse tables" : "A Bison parser",
1963 obstack_grow_literal_string (&table_obstack
,
1964 "#define YYBISON 1 /* Identify Bison output. */\n\n");
1965 read_declarations ();
1966 /* Start writing the guard and action files, if they are needed. */
1968 /* Read in the grammar, build grammar in list form. Write out
1969 guards and actions. */
1971 /* Now we know whether we need the line-number stack. If we do,
1972 write its type into the .tab.h file. */
1974 reader_output_yylsp (&defines_obstack
);
1975 /* Write closing delimiters for actions and guards. */
1978 obstack_grow_literal_string (&table_obstack
, "#define YYLSP_NEEDED\n\n");
1979 /* Assign the symbols their symbol numbers. Write #defines for the
1980 token symbols into FDEFINES if requested. */
1982 /* Convert the grammar into the format described in gram.h. */
1984 /* Free the symbol table data structure since symbols are now all
1985 referred to by symbol number. */
1991 reader_output_yylsp (struct obstack
*oout
)
1994 obstack_grow_literal_string (oout
, "\
1997 typedef struct yyltype\n\
2008 # define YYLTYPE yyltype\n\