]>
git.saurik.com Git - bison.git/blob - src/reader.c
9ae0238e41d4cd5862ac6d932647f0ee985f9e20
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. */
36 #include "conflicts.h"
38 /* Number of slots allocated (but not necessarily used yet) in `rline' */
39 static int rline_allocated
;
41 typedef struct symbol_list
43 struct symbol_list
*next
;
52 static symbol_list
*grammar
;
53 static int start_flag
;
54 static bucket
*startval
;
56 /* Nonzero if components of semantic values are used, implying
57 they must be unions. */
58 static int value_components_used
;
60 /* Nonzero if %union has been seen. */
63 /* Incremented for each %left, %right or %nonassoc seen */
66 /* Incremented for each generated symbol */
67 static int gensym_count
;
69 static bucket
*errtoken
;
70 static bucket
*undeftoken
;
73 /*===================\
75 \===================*/
78 skip_to_char (int target
)
82 complain (_(" Skipping to next \\n"));
84 complain (_(" Skipping to next %c"), target
);
87 c
= skip_white_space ();
88 while (c
!= target
&& c
!= EOF
);
94 /*---------------------------------------------------------.
95 | Read a signed integer from STREAM and return its value. |
96 `---------------------------------------------------------*/
99 read_signed_integer (FILE *stream
)
101 int c
= getc (stream
);
113 n
= 10 * n
+ (c
- '0');
122 /*--------------------------------------------------------------.
123 | Get the data type (alternative in the union) of the value for |
124 | symbol N in rule RULE. |
125 `--------------------------------------------------------------*/
128 get_type_name (int n
, symbol_list
* rule
)
135 complain (_("invalid $ value"));
145 if (rp
== NULL
|| rp
->sym
== NULL
)
147 complain (_("invalid $ value"));
153 return rp
->sym
->type_name
;
156 /*------------------------------------------------------------.
157 | Dump the string from FIN to OOUT if non null. MATCH is the |
158 | delimiter of the string (either ' or "). |
159 `------------------------------------------------------------*/
162 copy_string (FILE *fin
, struct obstack
*oout
, int match
)
166 obstack_1grow (oout
, match
);
173 fatal (_("unterminated string at end of file"));
176 complain (_("unterminated string"));
178 c
= match
; /* invent terminator */
182 obstack_1grow (oout
, c
);
188 fatal (_("unterminated string at end of file"));
189 obstack_1grow (oout
, c
);
198 obstack_1grow (oout
, c
);
202 /*-----------------------------------------------------------------.
203 | Dump the wannabee comment from IN to OUT1 and OUT2 (which can be |
204 | NULL). In fact we just saw a `/', which might or might not be a |
205 | comment. In any case, copy what we saw. |
207 | OUT2 might be NULL. |
208 `-----------------------------------------------------------------*/
211 copy_comment2 (FILE *fin
, struct obstack
*oout1
, struct obstack
*oout2
)
217 /* We read a `/', output it. */
218 obstack_1grow (oout1
, '/');
220 obstack_1grow (oout2
, '/');
222 switch ((c
= getc (fin
)))
235 obstack_1grow (oout1
, c
);
237 obstack_1grow (oout2
, c
);
243 if (!cplus_comment
&& c
== '*')
247 obstack_1grow (oout1
, c
);
249 obstack_1grow (oout2
, c
);
255 obstack_1grow (oout1
, c
);
257 obstack_1grow (oout2
, c
);
264 obstack_1grow (oout1
, c
);
266 obstack_1grow (oout2
, c
);
273 fatal (_("unterminated comment"));
276 obstack_1grow (oout1
, c
);
278 obstack_1grow (oout2
, c
);
285 /*-------------------------------------------------------------------.
286 | Dump the comment (actually the current string starting with a `/') |
287 | from FIN to OOUT. |
288 `-------------------------------------------------------------------*/
291 copy_comment (FILE *fin
, struct obstack
*oout
)
293 copy_comment2 (fin
, oout
, NULL
);
297 /*-----------------------------------------------------------------.
298 | FIN is pointing to a location (i.e., a `@'). Output to OOUT a |
299 | reference to this location. STACK_OFFSET is the number of values |
300 | in the current rule so far, which says where to find `$0' with |
301 | respect to the top of the stack. |
302 `-----------------------------------------------------------------*/
305 copy_at (FILE *fin
, struct obstack
*oout
, int stack_offset
)
312 obstack_sgrow (oout
, "yyloc");
315 else if (isdigit (c
) || c
== '-')
321 n
= read_signed_integer (fin
);
323 obstack_fgrow1 (oout
, "yylsp[%d]", n
- stack_offset
);
330 complain (_("%s is invalid"), quote (buf
));
335 /*-------------------------------------------------------------------.
336 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
338 | Possible inputs: $[<TYPENAME>]($|integer) |
340 | Output to OOUT a reference to this semantic value. STACK_OFFSET is |
341 | the number of values in the current rule so far, which says where |
342 | to find `$0' with respect to the top of the stack. |
343 `-------------------------------------------------------------------*/
346 copy_dollar (FILE *fin
, struct obstack
*oout
,
347 symbol_list
*rule
, int stack_offset
)
350 char *type_name
= NULL
;
352 /* Get the type name if explicit. */
355 read_type_name (fin
);
356 type_name
= token_buffer
;
357 value_components_used
= 1;
363 obstack_sgrow (oout
, "yyval");
366 type_name
= get_type_name (0, rule
);
368 obstack_fgrow1 (oout
, ".%s", type_name
);
369 if (!type_name
&& typed
)
370 complain (_("$$ of `%s' has no declared type"),
373 else if (isdigit (c
) || c
== '-')
377 n
= read_signed_integer (fin
);
379 if (!type_name
&& n
> 0)
380 type_name
= get_type_name (n
, rule
);
382 obstack_fgrow1 (oout
, "yyvsp[%d]", n
- stack_offset
);
385 obstack_fgrow1 (oout
, ".%s", type_name
);
386 if (!type_name
&& typed
)
387 complain (_("$%d of `%s' has no declared type"),
394 complain (_("%s is invalid"), quote (buf
));
398 /*-------------------------------------------------------------------.
399 | Copy the contents of a `%{ ... %}' into the definitions file. The |
400 | `%{' has already been read. Return after reading the `%}'. |
401 `-------------------------------------------------------------------*/
404 copy_definition (void)
407 /* -1 while reading a character if prev char was %. */
411 obstack_fgrow2 (&attrs_obstack
, "#line %d %s\n",
412 lineno
, quotearg_style (c_quoting_style
, infile
));
423 obstack_1grow (&attrs_obstack
, c
);
433 copy_string (finput
, &attrs_obstack
, c
);
437 copy_comment (finput
, &attrs_obstack
);
441 fatal ("%s", _("unterminated `%{' definition"));
444 obstack_1grow (&attrs_obstack
, c
);
453 obstack_1grow (&attrs_obstack
, '%');
460 /*-------------------------------------------------------------------.
461 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
462 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
464 `-------------------------------------------------------------------*/
467 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
472 /* The symbol being defined. */
473 struct bucket
*symbol
= NULL
;
475 /* After `%token' and `%nterm', any number of symbols maybe be
479 int tmp_char
= ungetc (skip_white_space (), finput
);
481 /* `%' (for instance from `%token', or from `%%' etc.) is the
482 only valid means to end this declaration. */
486 fatal (_("Premature EOF after %s"), token_buffer
);
494 if (token
== TYPENAME
)
496 typename
= xstrdup (token_buffer
);
497 value_components_used
= 1;
500 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"' && symbol
)
503 warn (_("symbol `%s' used more than once as a literal string"),
505 else if (symbol
->alias
)
506 warn (_("symbol `%s' given more than one literal string"),
510 symval
->class = token_sym
;
511 symval
->type_name
= typename
;
512 symval
->user_token_number
= symbol
->user_token_number
;
513 symbol
->user_token_number
= SALIAS
;
514 symval
->alias
= symbol
;
515 symbol
->alias
= symval
;
516 /* symbol and symval combined are only one symbol */
522 else if (token
== IDENTIFIER
)
524 int oldclass
= symval
->class;
527 if (symbol
->class == what_is_not
)
528 complain (_("symbol %s redefined"), symbol
->tag
);
529 symbol
->class = what_is
;
530 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
531 symbol
->value
= nvars
++;
535 if (symbol
->type_name
== NULL
)
536 symbol
->type_name
= typename
;
537 else if (strcmp (typename
, symbol
->type_name
) != 0)
538 complain (_("type redeclaration for %s"), symbol
->tag
);
541 else if (symbol
&& token
== NUMBER
)
543 symbol
->user_token_number
= numval
;
548 complain (_("`%s' is invalid in %s"),
549 token_buffer
, (what_is
== token_sym
) ? "%token" : "%nterm");
557 /*------------------------------.
558 | Parse what comes after %start |
559 `------------------------------*/
562 parse_start_decl (void)
565 complain (_("multiple %s declarations"), "%start");
566 if (lex () != IDENTIFIER
)
567 complain (_("invalid %s declaration"), "%start");
575 /*-----------------------------------------------------------.
576 | read in a %type declaration and record its information for |
577 | get_type_name to access |
578 `-----------------------------------------------------------*/
581 parse_type_decl (void)
585 if (lex () != TYPENAME
)
587 complain ("%s", _("%type declaration has no <typename>"));
592 name
= xstrdup (token_buffer
);
597 int tmp_char
= ungetc (skip_white_space (), finput
);
602 fatal (_("Premature EOF after %s"), token_buffer
);
614 if (symval
->type_name
== NULL
)
615 symval
->type_name
= name
;
616 else if (strcmp (name
, symval
->type_name
) != 0)
617 complain (_("type redeclaration for %s"), symval
->tag
);
622 complain (_("invalid %%type declaration due to item: %s"),
631 /*----------------------------------------------------------------.
632 | Read in a %left, %right or %nonassoc declaration and record its |
634 `----------------------------------------------------------------*/
637 parse_assoc_decl (associativity assoc
)
642 lastprec
++; /* Assign a new precedence level, never 0. */
647 int tmp_char
= ungetc (skip_white_space (), finput
);
652 fatal (_("Premature EOF after %s"), token_buffer
);
659 name
= xstrdup (token_buffer
);
666 if (symval
->prec
!= 0)
667 complain (_("redefining precedence of %s"), symval
->tag
);
668 symval
->prec
= lastprec
;
669 symval
->assoc
= assoc
;
670 if (symval
->class == nterm_sym
)
671 complain (_("symbol %s redefined"), symval
->tag
);
672 symval
->class = token_sym
;
674 { /* record the type, if one is specified */
675 if (symval
->type_name
== NULL
)
676 symval
->type_name
= name
;
677 else if (strcmp (name
, symval
->type_name
) != 0)
678 complain (_("type redeclaration for %s"), symval
->tag
);
683 if (prev
== IDENTIFIER
)
685 symval
->user_token_number
= numval
;
691 ("invalid text (%s) - number should be after identifier"),
701 complain (_("unexpected item: %s"), token_buffer
);
712 /*--------------------------------------------------------------.
713 | Copy the union declaration into ATTRS_OBSTACK (and fdefines), |
714 | where it is made into the definition of YYSTYPE, the type of |
715 | elements of the parser value stack. |
716 `--------------------------------------------------------------*/
719 parse_union_decl (void)
725 complain (_("multiple %s declarations"), "%union");
730 obstack_fgrow2 (&attrs_obstack
, "\n#line %d %s\n",
731 lineno
, quotearg_style (c_quoting_style
, infile
));
733 obstack_1grow (&attrs_obstack
, '\n');
735 obstack_sgrow (&attrs_obstack
, "typedef union");
737 obstack_sgrow (&defines_obstack
, "typedef union");
743 obstack_1grow (&attrs_obstack
, c
);
745 obstack_1grow (&defines_obstack
, c
);
754 copy_comment2 (finput
, &defines_obstack
, &attrs_obstack
);
763 complain (_("unmatched %s"), "`}'");
767 obstack_sgrow (&attrs_obstack
, " YYSTYPE;\n");
769 obstack_sgrow (&defines_obstack
, " YYSTYPE;\n");
770 /* JF don't choke on trailing semi */
771 c
= skip_white_space ();
783 /*-------------------------------------------------------.
784 | Parse the declaration %expect N which says to expect N |
785 | shift-reduce conflicts. |
786 `-------------------------------------------------------*/
789 parse_expect_decl (void)
791 int c
= skip_white_space ();
795 complain (_("argument of %%expect is not an integer"));
797 expected_conflicts
= read_signed_integer (finput
);
801 /*-------------------------------------------------------------------.
802 | Parse what comes after %thong. the full syntax is |
804 | %thong <type> token number literal |
806 | the <type> or number may be omitted. The number specifies the |
807 | user_token_number. |
809 | Two symbols are entered in the table, one for the token symbol and |
810 | one for the literal. Both are given the <type>, if any, from the |
811 | declaration. The ->user_token_number of the first is SALIAS and |
812 | the ->user_token_number of the second is set to the number, if |
813 | any, from the declaration. The two symbols are linked via |
814 | pointers in their ->alias fields. |
816 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
817 | only the literal string is retained it is the literal string that |
818 | is output to yytname |
819 `-------------------------------------------------------------------*/
822 parse_thong_decl (void)
825 struct bucket
*symbol
;
830 token
= lex (); /* fetch typename or first token */
831 if (token
== TYPENAME
)
833 typename
= xstrdup (token_buffer
);
834 value_components_used
= 1;
835 token
= lex (); /* fetch first token */
838 /* process first token */
840 if (token
!= IDENTIFIER
)
842 complain (_("unrecognized item %s, expected an identifier"),
847 symval
->class = token_sym
;
848 symval
->type_name
= typename
;
849 symval
->user_token_number
= SALIAS
;
852 token
= lex (); /* get number or literal string */
857 token
= lex (); /* okay, did number, now get literal */
862 /* process literal string token */
864 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
866 complain (_("expected string constant instead of %s"), token_buffer
);
870 symval
->class = token_sym
;
871 symval
->type_name
= typename
;
872 symval
->user_token_number
= usrtoknum
;
874 symval
->alias
= symbol
;
875 symbol
->alias
= symval
;
877 /* symbol and symval combined are only one symbol. */
882 /*----------------------------------------------------------------.
883 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
884 | any `%' declarations, and copy the contents of any `%{ ... %}' |
885 | groups to ATTRS_OBSTACK. |
886 `----------------------------------------------------------------*/
889 read_declarations (void)
896 c
= skip_white_space ();
900 tok
= parse_percent_token ();
907 case PERCENT_LEFT_CURLY
:
912 parse_token_decl (token_sym
, nterm_sym
);
916 parse_token_decl (nterm_sym
, token_sym
);
932 parse_expect_decl ();
940 parse_assoc_decl (left_assoc
);
944 parse_assoc_decl (right_assoc
);
948 parse_assoc_decl (non_assoc
);
955 complain (_("unrecognized: %s"), token_buffer
);
960 fatal (_("no input grammar"));
965 complain (_("unknown character: %s"), quote (buf
));
971 /*-------------------------------------------------------------------.
972 | Assuming that a `{' has just been seen, copy everything up to the |
973 | matching `}' into the actions file. STACK_OFFSET is the number of |
974 | values in the current rule so far, which says where to find `$0' |
975 | with respect to the top of the stack. |
976 `-------------------------------------------------------------------*/
979 copy_action (symbol_list
*rule
, int stack_offset
)
985 /* offset is always 0 if parser has already popped the stack pointer */
989 sprintf (buf
, "\ncase %d:\n", nrules
);
990 obstack_grow (&action_obstack
, buf
, strlen (buf
));
994 sprintf (buf
, "#line %d %s\n",
995 lineno
, quotearg_style (c_quoting_style
, infile
));
996 obstack_grow (&action_obstack
, buf
, strlen (buf
));
998 obstack_1grow (&action_obstack
, '{');
1010 obstack_1grow (&action_obstack
, c
);
1015 obstack_1grow (&action_obstack
, c
);
1021 copy_string (finput
, &action_obstack
, c
);
1025 copy_comment (finput
, &action_obstack
);
1029 copy_dollar (finput
, &action_obstack
,
1030 rule
, stack_offset
);
1034 copy_at (finput
, &action_obstack
,
1039 fatal (_("unmatched %s"), "`{'");
1042 obstack_1grow (&action_obstack
, c
);
1048 /* above loop exits when c is '}' */
1052 obstack_1grow (&action_obstack
, c
);
1057 obstack_sgrow (&action_obstack
, ";\n break;}");
1060 /*-------------------------------------------------------------------.
1061 | After `%guard' is seen in the input file, copy the actual guard |
1062 | into the guards file. If the guard is followed by an action, copy |
1063 | that into the actions file. STACK_OFFSET is the number of values |
1064 | in the current rule so far, which says where to find `$0' with |
1065 | respect to the top of the stack, for the simple parser in which |
1066 | the stack is not popped until after the guard is run. |
1067 `-------------------------------------------------------------------*/
1070 copy_guard (symbol_list
*rule
, int stack_offset
)
1076 /* offset is always 0 if parser has already popped the stack pointer */
1077 if (semantic_parser
)
1080 obstack_fgrow1 (&guard_obstack
, "\ncase %d:\n", nrules
);
1082 obstack_fgrow2 (&guard_obstack
, "#line %d %s\n",
1083 lineno
, quotearg_style (c_quoting_style
, infile
));
1084 obstack_1grow (&guard_obstack
, '{');
1089 while (brace_flag
? (count
> 0) : (c
!= ';'))
1094 obstack_1grow (&guard_obstack
, c
);
1099 obstack_1grow (&guard_obstack
, c
);
1105 obstack_1grow (&guard_obstack
, c
);
1110 complain (_("unmatched %s"), "`}'");
1111 c
= getc (finput
); /* skip it */
1117 copy_string (finput
, &guard_obstack
, c
);
1121 copy_comment (finput
, &guard_obstack
);
1125 copy_dollar (finput
, &guard_obstack
, rule
, stack_offset
);
1129 copy_at (finput
, &guard_obstack
, stack_offset
);
1133 fatal ("%s", _("unterminated %guard clause"));
1136 obstack_1grow (&guard_obstack
, c
);
1139 if (c
!= '}' || count
!= 0)
1143 c
= skip_white_space ();
1145 obstack_sgrow (&guard_obstack
, ";\n break;}");
1147 copy_action (rule
, stack_offset
);
1150 c
= getc (finput
); /* why not skip_white_space -wjh */
1152 copy_action (rule
, stack_offset
);
1160 record_rule_line (void)
1162 /* Record each rule's source line number in rline table. */
1164 if (nrules
>= rline_allocated
)
1166 rline_allocated
= nrules
* 2;
1167 rline
= XREALLOC (rline
, short, rline_allocated
);
1169 rline
[nrules
] = lineno
;
1173 /*-------------------------------------------------------------------.
1174 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1175 | with the user's names. |
1176 `-------------------------------------------------------------------*/
1183 sprintf (token_buffer
, "@%d", ++gensym_count
);
1184 sym
= getsym (token_buffer
);
1185 sym
->class = nterm_sym
;
1186 sym
->value
= nvars
++;
1191 /*------------------------------------------------------------------.
1192 | read in a %type declaration and record its information for |
1193 | get_type_name to access. This is unused. It is only called from |
1194 | the #if 0 part of readgram |
1195 `------------------------------------------------------------------*/
1208 complain (_("invalid %s declaration"), "%type");
1212 name
= xstrdup (token_buffer
);
1227 if (symval
->type_name
== NULL
)
1228 symval
->type_name
= name
;
1229 else if (strcmp (name
, symval
->type_name
) != 0)
1230 complain (_("type redeclaration for %s"), symval
->tag
);
1242 /*------------------------------------------------------------------.
1243 | Parse the input grammar into a one symbol_list structure. Each |
1244 | rule is represented by a sequence of symbols: the left hand side |
1245 | followed by the contents of the right hand side, followed by a |
1246 | null pointer instead of a symbol to terminate the rule. The next |
1247 | symbol is the lhs of the following rule. |
1249 | All guards and actions are copied out to the appropriate files, |
1250 | labelled by the rule number they apply to. |
1251 `------------------------------------------------------------------*/
1262 /* Points to first symbol_list of current rule. its symbol is the
1265 /* Points to the symbol_list preceding crule. */
1266 symbol_list
*crule1
;
1272 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1274 if (t
== IDENTIFIER
|| t
== BAR
)
1276 int action_flag
= 0;
1277 /* Number of symbols in rhs of this rule so far */
1279 int xactions
= 0; /* JF for error checking */
1280 bucket
*first_rhs
= 0;
1282 if (t
== IDENTIFIER
)
1295 complain (_("ill-formed rule: initial symbol not followed by colon"));
1300 if (nrules
== 0 && t
== BAR
)
1302 complain (_("grammar starts with vertical bar"));
1303 lhs
= symval
; /* BOGUS: use a random symval */
1305 /* start a new rule and record its lhs. */
1310 record_rule_line ();
1312 p
= XCALLOC (symbol_list
, 1);
1324 /* mark the rule's lhs as a nonterminal if not already so. */
1326 if (lhs
->class == unknown_sym
)
1328 lhs
->class = nterm_sym
;
1332 else if (lhs
->class == token_sym
)
1333 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1335 /* read the rhs of the rule. */
1343 crule
->ruleprec
= symval
;
1347 if (!(t
== IDENTIFIER
|| t
== LEFT_CURLY
))
1350 /* If next token is an identifier, see if a colon follows it.
1351 If one does, exit this rule now. */
1352 if (t
== IDENTIFIER
)
1364 if (!first_rhs
) /* JF */
1366 /* Not followed by colon =>
1367 process as part of this rule's rhs. */
1370 /* If we just passed an action, that action was in the middle
1371 of a rule, so make a dummy rule to reduce it to a
1377 /* Since the action was written out with this rule's
1378 number, we must give the new rule this number by
1379 inserting the new rule before it. */
1381 /* Make a dummy nonterminal, a gensym. */
1384 /* Make a new rule, whose body is empty,
1385 before the current one, so that the action
1386 just read can belong to it. */
1389 record_rule_line ();
1390 p
= XCALLOC (symbol_list
, 1);
1396 crule1
= XCALLOC (symbol_list
, 1);
1398 crule1
->next
= crule
;
1400 /* Insert the dummy generated by that rule into this
1403 p
= XCALLOC (symbol_list
, 1);
1411 if (t
== IDENTIFIER
)
1414 p
= XCALLOC (symbol_list
, 1);
1419 else /* handle an action. */
1421 copy_action (crule
, rulelength
);
1423 xactions
++; /* JF */
1426 } /* end of read rhs of rule */
1428 /* Put an empty link in the list to mark the end of this rule */
1429 p
= XCALLOC (symbol_list
, 1);
1435 complain (_("two @prec's in a row"));
1437 crule
->ruleprec
= symval
;
1442 if (!semantic_parser
)
1443 complain (_("%%guard present but %%semantic_parser not specified"));
1445 copy_guard (crule
, rulelength
);
1448 else if (t
== LEFT_CURLY
)
1450 /* This case never occurs -wjh */
1452 complain (_("two actions at end of one rule"));
1453 copy_action (crule
, rulelength
);
1455 xactions
++; /* -wjh */
1458 /* If $$ is being set in default way, report if any type
1461 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1463 if (lhs
->type_name
== 0
1464 || first_rhs
->type_name
== 0
1465 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1466 complain (_("type clash (`%s' `%s') on default action"),
1467 lhs
->type_name
? lhs
->type_name
: "",
1468 first_rhs
->type_name
? first_rhs
->type_name
: "");
1470 /* Warn if there is no default for $$ but we need one. */
1471 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1472 complain (_("empty rule for typed nonterminal, and no action"));
1477 /* these things can appear as alternatives to rules. */
1479 a) none of the documentation allows them
1480 b) most of them scan forward until finding a next %
1481 thus they may swallow lots of intervening rules
1483 else if (t
== TOKEN
)
1485 parse_token_decl (token_sym
, nterm_sym
);
1488 else if (t
== NTERM
)
1490 parse_token_decl (nterm_sym
, token_sym
);
1497 else if (t
== UNION
)
1499 parse_union_decl ();
1502 else if (t
== EXPECT
)
1504 parse_expect_decl ();
1507 else if (t
== START
)
1509 parse_start_decl ();
1516 complain (_("invalid input: %s"), token_buffer
);
1521 /* grammar has been read. Do some checking */
1523 if (nsyms
> MAXSHORT
)
1524 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1527 fatal (_("no rules in the input grammar"));
1529 /* JF put out same default YYSTYPE as YACC does */
1531 && !value_components_used
)
1533 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1534 but it seems better to be consistent.
1535 Most programs should declare their own type anyway. */
1536 obstack_sgrow (&attrs_obstack
,
1537 "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1539 obstack_sgrow (&defines_obstack
, "\
1541 # define YYSTYPE int\n\
1545 /* Report any undefined symbols and consider them nonterminals. */
1547 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1548 if (bp
->class == unknown_sym
)
1551 ("symbol %s is used, but is not defined as a token and has no rules"),
1553 bp
->class = nterm_sym
;
1554 bp
->value
= nvars
++;
1557 ntokens
= nsyms
- nvars
;
1560 /*--------------------------------------------------------------.
1561 | For named tokens, but not literal ones, define the name. The |
1562 | value is the user token number. |
1563 `--------------------------------------------------------------*/
1566 output_token_defines (struct obstack
*oout
)
1572 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1574 symbol
= bp
->tag
; /* get symbol */
1576 if (bp
->value
>= ntokens
)
1578 if (bp
->user_token_number
== SALIAS
)
1580 if ('\'' == *symbol
)
1581 continue; /* skip literal character */
1583 continue; /* skip error token */
1584 if ('\"' == *symbol
)
1586 /* use literal string only if given a symbol with an alias */
1588 symbol
= bp
->alias
->tag
;
1593 /* Don't #define nonliteral tokens whose names contain periods. */
1595 while ((c
= *cp
++) && c
!= '.');
1599 obstack_fgrow2 (oout
, "#define\t%s\t%d\n",
1601 ((translations
&& !raw_flag
)
1602 ? bp
->user_token_number
: bp
->value
));
1603 if (semantic_parser
)
1604 obstack_fgrow2 (oout
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1607 obstack_1grow (oout
, '\n');
1611 /*------------------------------------------------------------------.
1612 | Assign symbol numbers, and write definition of token names into |
1613 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1615 `------------------------------------------------------------------*/
1623 int last_user_token_number
;
1624 static char DOLLAR
[] = "$";
1626 /* int lossage = 0; JF set but not used */
1628 tags
= XCALLOC (char *, nsyms
+ 1);
1630 user_toknums
= XCALLOC (short, nsyms
+ 1);
1631 user_toknums
[0] = 0;
1633 sprec
= XCALLOC (short, nsyms
);
1634 sassoc
= XCALLOC (short, nsyms
);
1636 max_user_token_number
= 256;
1637 last_user_token_number
= 256;
1639 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1641 if (bp
->class == nterm_sym
)
1643 bp
->value
+= ntokens
;
1647 /* this symbol and its alias are a single token defn.
1648 allocate a tokno, and assign to both check agreement of
1649 ->prec and ->assoc fields and make both the same */
1651 bp
->value
= bp
->alias
->value
= tokno
++;
1653 if (bp
->prec
!= bp
->alias
->prec
)
1655 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1656 && bp
->user_token_number
== SALIAS
)
1657 complain (_("conflicting precedences for %s and %s"),
1658 bp
->tag
, bp
->alias
->tag
);
1660 bp
->alias
->prec
= bp
->prec
;
1662 bp
->prec
= bp
->alias
->prec
;
1665 if (bp
->assoc
!= bp
->alias
->assoc
)
1667 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1668 && bp
->user_token_number
== SALIAS
)
1669 complain (_("conflicting assoc values for %s and %s"),
1670 bp
->tag
, bp
->alias
->tag
);
1672 bp
->alias
->assoc
= bp
->assoc
;
1674 bp
->assoc
= bp
->alias
->assoc
;
1677 if (bp
->user_token_number
== SALIAS
)
1678 continue; /* do not do processing below for SALIASs */
1681 else /* bp->class == token_sym */
1683 bp
->value
= tokno
++;
1686 if (bp
->class == token_sym
)
1688 if (translations
&& !(bp
->user_token_number
))
1689 bp
->user_token_number
= ++last_user_token_number
;
1690 if (bp
->user_token_number
> max_user_token_number
)
1691 max_user_token_number
= bp
->user_token_number
;
1694 tags
[bp
->value
] = bp
->tag
;
1695 user_toknums
[bp
->value
] = bp
->user_token_number
;
1696 sprec
[bp
->value
] = bp
->prec
;
1697 sassoc
[bp
->value
] = bp
->assoc
;
1705 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1707 /* initialize all entries for literal tokens to 2, the internal
1708 token number for $undefined., which represents all invalid
1710 for (j
= 0; j
<= max_user_token_number
; j
++)
1711 token_translations
[j
] = 2;
1713 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1715 if (bp
->value
>= ntokens
)
1716 continue; /* non-terminal */
1717 if (bp
->user_token_number
== SALIAS
)
1719 if (token_translations
[bp
->user_token_number
] != 2)
1720 complain (_("tokens %s and %s both assigned number %d"),
1721 tags
[token_translations
[bp
->user_token_number
]],
1722 bp
->tag
, bp
->user_token_number
);
1723 token_translations
[bp
->user_token_number
] = bp
->value
;
1727 error_token_number
= errtoken
->value
;
1729 if (!no_parser_flag
)
1730 output_token_defines (&table_obstack
);
1732 if (startval
->class == unknown_sym
)
1733 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1734 else if (startval
->class == token_sym
)
1735 fatal (_("the start symbol %s is a token"), startval
->tag
);
1737 start_symbol
= startval
->value
;
1741 output_token_defines (&defines_obstack
);
1745 if (spec_name_prefix
)
1746 obstack_fgrow1 (&defines_obstack
, "\nextern YYSTYPE %slval;\n",
1749 obstack_sgrow (&defines_obstack
,
1750 "\nextern YYSTYPE yylval;\n");
1753 if (semantic_parser
)
1754 for (i
= ntokens
; i
< nsyms
; i
++)
1756 /* don't make these for dummy nonterminals made by gensym. */
1757 if (*tags
[i
] != '@')
1758 obstack_fgrow2 (&defines_obstack
,
1759 "#define\tNT%s\t%d\n", tags
[i
], i
);
1762 /* `fdefines' is now a temporary file, so we need to copy its
1763 contents in `done', so we can't close it here. */
1771 /*---------------------------------------------------------------.
1772 | Convert the rules into the representation using RRHS, RLHS and |
1774 `---------------------------------------------------------------*/
1785 ritem
= XCALLOC (short, nitems
+ 1);
1786 rlhs
= XCALLOC (short, nrules
) - 1;
1787 rrhs
= XCALLOC (short, nrules
) - 1;
1788 rprec
= XCALLOC (short, nrules
) - 1;
1789 rprecsym
= XCALLOC (short, nrules
) - 1;
1790 rassoc
= XCALLOC (short, nrules
) - 1;
1798 rlhs
[ruleno
] = p
->sym
->value
;
1799 rrhs
[ruleno
] = itemno
;
1800 ruleprec
= p
->ruleprec
;
1805 ritem
[itemno
++] = p
->sym
->value
;
1806 /* A rule gets by default the precedence and associativity
1807 of the last token in it. */
1808 if (p
->sym
->class == token_sym
)
1810 rprec
[ruleno
] = p
->sym
->prec
;
1811 rassoc
[ruleno
] = p
->sym
->assoc
;
1817 /* If this rule has a %prec,
1818 the specified symbol's precedence replaces the default. */
1821 rprec
[ruleno
] = ruleprec
->prec
;
1822 rassoc
[ruleno
] = ruleprec
->assoc
;
1823 rprecsym
[ruleno
] = ruleprec
->value
;
1826 ritem
[itemno
++] = -ruleno
;
1836 /*-------------------------------------------------------------------.
1837 | Read in the grammar specification and record it in the format |
1838 | described in gram.h. All guards are copied into the GUARD_OBSTACK |
1839 | and all actions into ACTION_OBSTACK, in each case forming the body |
1840 | of a C function (YYGUARD or YYACTION) which contains a switch |
1841 | statement to decide which guard or action to execute. |
1842 `-------------------------------------------------------------------*/
1848 startval
= NULL
; /* start symbol not specified yet. */
1851 /* initially assume token number translation not needed. */
1854 /* Nowadays translations is always set to 1, since we give `error' a
1855 user-token-number to satisfy the Posix demand for YYERRCODE==256.
1863 rline_allocated
= 10;
1864 rline
= XCALLOC (short, rline_allocated
);
1871 semantic_parser
= 0;
1879 /* Initialize the symbol table. */
1881 /* Construct the error token */
1882 errtoken
= getsym ("error");
1883 errtoken
->class = token_sym
;
1884 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1885 /* Construct a token that represents all undefined literal tokens.
1886 It is always token number 2. */
1887 undeftoken
= getsym ("$undefined.");
1888 undeftoken
->class = token_sym
;
1889 undeftoken
->user_token_number
= 2;
1891 /* Read the declaration section. Copy %{ ... %} groups to
1892 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1893 etc. found there. */
1894 obstack_1grow (&table_obstack
, '\n');
1895 obstack_fgrow3 (&table_obstack
, "\
1896 /* %s, made from %s\n\
1897 by GNU bison %s. */\n\
1899 no_parser_flag
? "Bison-generated parse tables" : "A Bison parser",
1902 obstack_sgrow (&table_obstack
,
1903 "#define YYBISON 1 /* Identify Bison output. */\n\n");
1904 read_declarations ();
1905 /* Start writing the guard and action files, if they are needed. */
1907 /* Read in the grammar, build grammar in list form. Write out
1908 guards and actions. */
1910 /* Now we know whether we need the line-number stack. If we do,
1911 write its type into the .tab.h file. */
1913 reader_output_yylsp (&defines_obstack
);
1914 /* Write closing delimiters for actions and guards. */
1917 obstack_sgrow (&table_obstack
, "#define YYLSP_NEEDED 1\n\n");
1918 /* Assign the symbols their symbol numbers. Write #defines for the
1919 token symbols into FDEFINES if requested. */
1921 /* Convert the grammar into the format described in gram.h. */
1923 /* Free the symbol table data structure since symbols are now all
1924 referred to by symbol number. */
1929 /*------------------------------------------------------------------.
1930 | Define YYLTYPE. Cannot be in the skeleton since we might have to |
1931 | output it in the headers if --defines is used. |
1932 `------------------------------------------------------------------*/
1935 reader_output_yylsp (struct obstack
*oout
)
1938 obstack_sgrow (oout
, "\
1941 typedef struct yyltype\n\
1944 int first_column;\n\
1950 # define YYLTYPE yyltype\n\