]>
git.saurik.com Git - bison.git/blob - src/reader.c
28d4153132e13c2d2ba18e2e3079e0f086b07427
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
, "yylloc");
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 fprintf (fattrs
, "#line %d \"%s\"\n", lineno
, infile
);
484 copy_string (finput
, fattrs
, 0, c
);
488 copy_comment (finput
, fattrs
, 0);
492 fatal ("%s", _("unterminated `%{' definition"));
513 /*-------------------------------------------------------------------.
514 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
515 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
517 `-------------------------------------------------------------------*/
520 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
525 /* The symbol being defined. */
526 struct bucket
*symbol
= NULL
;
528 /* After `%token' and `%nterm', any number of symbols maybe be
532 int tmp_char
= ungetc (skip_white_space (), finput
);
534 /* `%' (for instance from `%token', or from `%%' etc.) is the
535 only valid means to end this declaration. */
539 fatal (_("Premature EOF after %s"), token_buffer
);
547 if (token
== TYPENAME
)
549 typename
= xstrdup (token_buffer
);
550 value_components_used
= 1;
553 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"' && symbol
)
556 warn (_("symbol `%s' used more than once as a literal string"),
558 else if (symbol
->alias
)
559 warn (_("symbol `%s' given more than one literal string"),
563 symval
->class = token_sym
;
564 symval
->type_name
= typename
;
565 symval
->user_token_number
= symbol
->user_token_number
;
566 symbol
->user_token_number
= SALIAS
;
567 symval
->alias
= symbol
;
568 symbol
->alias
= symval
;
569 /* symbol and symval combined are only one symbol */
575 else if (token
== IDENTIFIER
)
577 int oldclass
= symval
->class;
580 if (symbol
->class == what_is_not
)
581 complain (_("symbol %s redefined"), symbol
->tag
);
582 symbol
->class = what_is
;
583 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
584 symbol
->value
= nvars
++;
588 if (symbol
->type_name
== NULL
)
589 symbol
->type_name
= typename
;
590 else if (strcmp (typename
, symbol
->type_name
) != 0)
591 complain (_("type redeclaration for %s"), symbol
->tag
);
594 else if (symbol
&& token
== NUMBER
)
596 symbol
->user_token_number
= numval
;
601 complain (_("`%s' is invalid in %s"),
602 token_buffer
, (what_is
== token_sym
) ? "%token" : "%nterm");
610 /*------------------------------.
611 | Parse what comes after %start |
612 `------------------------------*/
615 parse_start_decl (void)
618 complain (_("multiple %s declarations"), "%start");
619 if (lex () != IDENTIFIER
)
620 complain (_("invalid %s declaration"), "%start");
628 /*-----------------------------------------------------------.
629 | read in a %type declaration and record its information for |
630 | get_type_name to access |
631 `-----------------------------------------------------------*/
634 parse_type_decl (void)
638 if (lex () != TYPENAME
)
640 complain ("%s", _("%type declaration has no <typename>"));
645 name
= xstrdup (token_buffer
);
650 int tmp_char
= ungetc (skip_white_space (), finput
);
655 fatal (_("Premature EOF after %s"), token_buffer
);
667 if (symval
->type_name
== NULL
)
668 symval
->type_name
= name
;
669 else if (strcmp (name
, symval
->type_name
) != 0)
670 complain (_("type redeclaration for %s"), symval
->tag
);
675 complain (_("invalid %%type declaration due to item: %s"),
684 /*----------------------------------------------------------------.
685 | Read in a %left, %right or %nonassoc declaration and record its |
687 `----------------------------------------------------------------*/
690 parse_assoc_decl (associativity assoc
)
695 lastprec
++; /* Assign a new precedence level, never 0. */
700 int tmp_char
= ungetc (skip_white_space (), finput
);
705 fatal (_("Premature EOF after %s"), token_buffer
);
712 name
= xstrdup (token_buffer
);
719 if (symval
->prec
!= 0)
720 complain (_("redefining precedence of %s"), symval
->tag
);
721 symval
->prec
= lastprec
;
722 symval
->assoc
= assoc
;
723 if (symval
->class == nterm_sym
)
724 complain (_("symbol %s redefined"), symval
->tag
);
725 symval
->class = token_sym
;
727 { /* record the type, if one is specified */
728 if (symval
->type_name
== NULL
)
729 symval
->type_name
= name
;
730 else if (strcmp (name
, symval
->type_name
) != 0)
731 complain (_("type redeclaration for %s"), symval
->tag
);
736 if (prev
== IDENTIFIER
)
738 symval
->user_token_number
= numval
;
744 ("invalid text (%s) - number should be after identifier"),
754 complain (_("unexpected item: %s"), token_buffer
);
765 /*-------------------------------------------------------------------.
766 | Copy the union declaration into fattrs (and fdefines), where it is |
767 | made into the definition of YYSTYPE, the type of elements of the |
768 | parser value stack. |
769 `-------------------------------------------------------------------*/
772 parse_union_decl (void)
778 complain (_("multiple %s declarations"), "%union");
783 fprintf (fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
785 fprintf (fattrs
, "\n");
787 fprintf (fattrs
, "typedef union");
789 fprintf (fdefines
, "typedef union");
806 copy_comment2 (finput
, fattrs
, fdefines
, 0);
815 complain (_("unmatched %s"), "`}'");
819 fprintf (fattrs
, " YYSTYPE;\n");
821 fprintf (fdefines
, " YYSTYPE;\n");
822 /* JF don't choke on trailing semi */
823 c
= skip_white_space ();
835 /*-------------------------------------------------------.
836 | Parse the declaration %expect N which says to expect N |
837 | shift-reduce conflicts. |
838 `-------------------------------------------------------*/
841 parse_expect_decl (void)
843 int c
= skip_white_space ();
847 complain (_("argument of %%expect is not an integer"));
849 expected_conflicts
= read_signed_integer (finput
);
853 /*-------------------------------------------------------------------.
854 | Parse what comes after %thong. the full syntax is |
856 | %thong <type> token number literal |
858 | the <type> or number may be omitted. The number specifies the |
859 | user_token_number. |
861 | Two symbols are entered in the table, one for the token symbol and |
862 | one for the literal. Both are given the <type>, if any, from the |
863 | declaration. The ->user_token_number of the first is SALIAS and |
864 | the ->user_token_number of the second is set to the number, if |
865 | any, from the declaration. The two symbols are linked via |
866 | pointers in their ->alias fields. |
868 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
869 | only the literal string is retained it is the literal string that |
870 | is output to yytname |
871 `-------------------------------------------------------------------*/
874 parse_thong_decl (void)
877 struct bucket
*symbol
;
882 token
= lex (); /* fetch typename or first token */
883 if (token
== TYPENAME
)
885 typename
= xstrdup (token_buffer
);
886 value_components_used
= 1;
887 token
= lex (); /* fetch first token */
890 /* process first token */
892 if (token
!= IDENTIFIER
)
894 complain (_("unrecognized item %s, expected an identifier"),
899 symval
->class = token_sym
;
900 symval
->type_name
= typename
;
901 symval
->user_token_number
= SALIAS
;
904 token
= lex (); /* get number or literal string */
909 token
= lex (); /* okay, did number, now get literal */
914 /* process literal string token */
916 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
918 complain (_("expected string constant instead of %s"), token_buffer
);
922 symval
->class = token_sym
;
923 symval
->type_name
= typename
;
924 symval
->user_token_number
= usrtoknum
;
926 symval
->alias
= symbol
;
927 symbol
->alias
= symval
;
929 /* symbol and symval combined are only one symbol. */
934 /*----------------------------------------------------------------.
935 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
936 | any `%' declarations, and copy the contents of any `%{ ... %}' |
937 | groups to fattrs. |
938 `----------------------------------------------------------------*/
941 read_declarations (void)
948 c
= skip_white_space ();
952 tok
= parse_percent_token ();
959 case PERCENT_LEFT_CURLY
:
964 parse_token_decl (token_sym
, nterm_sym
);
968 parse_token_decl (nterm_sym
, token_sym
);
984 parse_expect_decl ();
991 parse_assoc_decl (left_assoc
);
995 parse_assoc_decl (right_assoc
);
999 parse_assoc_decl (non_assoc
);
1002 case SEMANTIC_PARSER
:
1003 if (semantic_parser
== 0)
1005 semantic_parser
= 1;
1006 open_extra_files ();
1018 complain (_("unrecognized: %s"), token_buffer
);
1023 fatal (_("no input grammar"));
1028 complain (_("unknown character: %s"), quote (buf
));
1034 /*-------------------------------------------------------------------.
1035 | Assuming that a `{' has just been seen, copy everything up to the |
1036 | matching `}' into the actions file. STACK_OFFSET is the number of |
1037 | values in the current rule so far, which says where to find `$0' |
1038 | with respect to the top of the stack. |
1039 `-------------------------------------------------------------------*/
1042 copy_action (symbol_list
*rule
, int stack_offset
)
1048 /* offset is always 0 if parser has already popped the stack pointer */
1049 if (semantic_parser
)
1052 sprintf (buf
, "\ncase %d:\n", nrules
);
1053 obstack_grow (&action_obstack
, buf
, strlen (buf
));
1057 sprintf (buf
, "#line %d \"%s\"\n", lineno
, infile
);
1058 obstack_grow (&action_obstack
, buf
, strlen (buf
));
1060 obstack_1grow (&action_obstack
, '{');
1072 obstack_1grow (&action_obstack
, c
);
1077 obstack_1grow (&action_obstack
, c
);
1083 copy_string (finput
, 0, &action_obstack
, c
);
1087 copy_comment (finput
, 0, &action_obstack
);
1091 copy_dollar (finput
, 0, &action_obstack
,
1092 rule
, stack_offset
);
1096 copy_at (finput
, 0, &action_obstack
,
1101 fatal (_("unmatched %s"), "`{'");
1104 obstack_1grow (&action_obstack
, c
);
1110 /* above loop exits when c is '}' */
1114 obstack_1grow (&action_obstack
, c
);
1119 obstack_grow_literal_string (&action_obstack
,
1123 /*-------------------------------------------------------------------.
1124 | After `%guard' is seen in the input file, copy the actual guard |
1125 | into the guards file. If the guard is followed by an action, copy |
1126 | that into the actions file. STACK_OFFSET is the number of values |
1127 | in the current rule so far, which says where to find `$0' with |
1128 | respect to the top of the stack, for the simple parser in which |
1129 | the stack is not popped until after the guard is run. |
1130 `-------------------------------------------------------------------*/
1133 copy_guard (symbol_list
*rule
, int stack_offset
)
1139 /* offset is always 0 if parser has already popped the stack pointer */
1140 if (semantic_parser
)
1143 fprintf (fguard
, "\ncase %d:\n", nrules
);
1145 fprintf (fguard
, "#line %d \"%s\"\n", lineno
, infile
);
1151 while (brace_flag
? (count
> 0) : (c
!= ';'))
1172 complain (_("unmatched %s"), "`}'");
1173 c
= getc (finput
); /* skip it */
1179 copy_string (finput
, fguard
, 0, c
);
1183 copy_comment (finput
, fguard
, 0);
1187 copy_dollar (finput
, fguard
, 0, rule
, stack_offset
);
1191 copy_at (finput
, fguard
, 0, stack_offset
);
1195 fatal ("%s", _("unterminated %guard clause"));
1201 if (c
!= '}' || count
!= 0)
1205 c
= skip_white_space ();
1207 fprintf (fguard
, ";\n break;}");
1209 copy_action (rule
, stack_offset
);
1212 c
= getc (finput
); /* why not skip_white_space -wjh */
1214 copy_action (rule
, stack_offset
);
1222 record_rule_line (void)
1224 /* Record each rule's source line number in rline table. */
1226 if (nrules
>= rline_allocated
)
1228 rline_allocated
= nrules
* 2;
1229 rline
= XREALLOC (rline
, short, rline_allocated
);
1231 rline
[nrules
] = lineno
;
1235 /*-------------------------------------------------------------------.
1236 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1237 | with the user's names. |
1238 `-------------------------------------------------------------------*/
1245 sprintf (token_buffer
, "@%d", ++gensym_count
);
1246 sym
= getsym (token_buffer
);
1247 sym
->class = nterm_sym
;
1248 sym
->value
= nvars
++;
1253 /*------------------------------------------------------------------.
1254 | read in a %type declaration and record its information for |
1255 | get_type_name to access. This is unused. It is only called from |
1256 | the #if 0 part of readgram |
1257 `------------------------------------------------------------------*/
1270 complain (_("invalid %s declaration"), "%type");
1274 name
= xstrdup (token_buffer
);
1289 if (symval
->type_name
== NULL
)
1290 symval
->type_name
= name
;
1291 else if (strcmp (name
, symval
->type_name
) != 0)
1292 complain (_("type redeclaration for %s"), symval
->tag
);
1304 /*------------------------------------------------------------------.
1305 | Parse the input grammar into a one symbol_list structure. Each |
1306 | rule is represented by a sequence of symbols: the left hand side |
1307 | followed by the contents of the right hand side, followed by a |
1308 | null pointer instead of a symbol to terminate the rule. The next |
1309 | symbol is the lhs of the following rule. |
1311 | All guards and actions are copied out to the appropriate files, |
1312 | labelled by the rule number they apply to. |
1313 `------------------------------------------------------------------*/
1324 /* Points to first symbol_list of current rule. its symbol is the
1327 /* Points to the symbol_list preceding crule. */
1328 symbol_list
*crule1
;
1334 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1336 if (t
== IDENTIFIER
|| t
== BAR
)
1338 int action_flag
= 0;
1339 /* Number of symbols in rhs of this rule so far */
1341 int xactions
= 0; /* JF for error checking */
1342 bucket
*first_rhs
= 0;
1344 if (t
== IDENTIFIER
)
1357 complain (_("ill-formed rule: initial symbol not followed by colon"));
1362 if (nrules
== 0 && t
== BAR
)
1364 complain (_("grammar starts with vertical bar"));
1365 lhs
= symval
; /* BOGUS: use a random symval */
1367 /* start a new rule and record its lhs. */
1372 record_rule_line ();
1374 p
= XCALLOC (symbol_list
, 1);
1386 /* mark the rule's lhs as a nonterminal if not already so. */
1388 if (lhs
->class == unknown_sym
)
1390 lhs
->class = nterm_sym
;
1394 else if (lhs
->class == token_sym
)
1395 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1397 /* read the rhs of the rule. */
1405 crule
->ruleprec
= symval
;
1409 if (!(t
== IDENTIFIER
|| t
== LEFT_CURLY
))
1412 /* If next token is an identifier, see if a colon follows it.
1413 If one does, exit this rule now. */
1414 if (t
== IDENTIFIER
)
1426 if (!first_rhs
) /* JF */
1428 /* Not followed by colon =>
1429 process as part of this rule's rhs. */
1432 /* If we just passed an action, that action was in the middle
1433 of a rule, so make a dummy rule to reduce it to a
1439 /* Since the action was written out with this rule's
1440 number, we must give the new rule this number by
1441 inserting the new rule before it. */
1443 /* Make a dummy nonterminal, a gensym. */
1446 /* Make a new rule, whose body is empty,
1447 before the current one, so that the action
1448 just read can belong to it. */
1451 record_rule_line ();
1452 p
= XCALLOC (symbol_list
, 1);
1458 crule1
= XCALLOC (symbol_list
, 1);
1460 crule1
->next
= crule
;
1462 /* Insert the dummy generated by that rule into this
1465 p
= XCALLOC (symbol_list
, 1);
1473 if (t
== IDENTIFIER
)
1476 p
= XCALLOC (symbol_list
, 1);
1481 else /* handle an action. */
1483 copy_action (crule
, rulelength
);
1485 xactions
++; /* JF */
1488 } /* end of read rhs of rule */
1490 /* Put an empty link in the list to mark the end of this rule */
1491 p
= XCALLOC (symbol_list
, 1);
1497 complain (_("two @prec's in a row"));
1499 crule
->ruleprec
= symval
;
1504 if (!semantic_parser
)
1505 complain (_("%%guard present but %%semantic_parser not specified"));
1507 copy_guard (crule
, rulelength
);
1510 else if (t
== LEFT_CURLY
)
1512 /* This case never occurs -wjh */
1514 complain (_("two actions at end of one rule"));
1515 copy_action (crule
, rulelength
);
1517 xactions
++; /* -wjh */
1520 /* If $$ is being set in default way, report if any type
1523 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1525 if (lhs
->type_name
== 0
1526 || first_rhs
->type_name
== 0
1527 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1528 complain (_("type clash (`%s' `%s') on default action"),
1529 lhs
->type_name
? lhs
->type_name
: "",
1530 first_rhs
->type_name
? first_rhs
->type_name
: "");
1532 /* Warn if there is no default for $$ but we need one. */
1533 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1534 complain (_("empty rule for typed nonterminal, and no action"));
1539 /* these things can appear as alternatives to rules. */
1541 a) none of the documentation allows them
1542 b) most of them scan forward until finding a next %
1543 thus they may swallow lots of intervening rules
1545 else if (t
== TOKEN
)
1547 parse_token_decl (token_sym
, nterm_sym
);
1550 else if (t
== NTERM
)
1552 parse_token_decl (nterm_sym
, token_sym
);
1559 else if (t
== UNION
)
1561 parse_union_decl ();
1564 else if (t
== EXPECT
)
1566 parse_expect_decl ();
1569 else if (t
== START
)
1571 parse_start_decl ();
1578 complain (_("invalid input: %s"), token_buffer
);
1583 /* grammar has been read. Do some checking */
1585 if (nsyms
> MAXSHORT
)
1586 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1589 fatal (_("no rules in the input grammar"));
1591 /* JF put out same default YYSTYPE as YACC does */
1593 && !value_components_used
)
1595 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1596 but it seems better to be consistent.
1597 Most programs should declare their own type anyway. */
1598 fprintf (fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1600 fprintf (fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1603 /* Report any undefined symbols and consider them nonterminals. */
1605 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1606 if (bp
->class == unknown_sym
)
1609 ("symbol %s is used, but is not defined as a token and has no rules"),
1611 bp
->class = nterm_sym
;
1612 bp
->value
= nvars
++;
1615 ntokens
= nsyms
- nvars
;
1618 /*--------------------------------------------------------------.
1619 | For named tokens, but not literal ones, define the name. The |
1620 | value is the user token number. |
1621 `--------------------------------------------------------------*/
1624 output_token_defines (FILE *file
)
1630 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1632 symbol
= bp
->tag
; /* get symbol */
1634 if (bp
->value
>= ntokens
)
1636 if (bp
->user_token_number
== SALIAS
)
1638 if ('\'' == *symbol
)
1639 continue; /* skip literal character */
1641 continue; /* skip error token */
1642 if ('\"' == *symbol
)
1644 /* use literal string only if given a symbol with an alias */
1646 symbol
= bp
->alias
->tag
;
1651 /* Don't #define nonliteral tokens whose names contain periods. */
1653 while ((c
= *cp
++) && c
!= '.');
1657 fprintf (file
, "#define\t%s\t%d\n", symbol
,
1658 ((translations
&& !raw_flag
)
1659 ? bp
->user_token_number
: bp
->value
));
1660 if (semantic_parser
)
1661 fprintf (file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1668 /*------------------------------------------------------------------.
1669 | Assign symbol numbers, and write definition of token names into |
1670 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1672 `------------------------------------------------------------------*/
1680 int last_user_token_number
;
1681 static char DOLLAR
[] = "$";
1683 /* int lossage = 0; JF set but not used */
1685 tags
= XCALLOC (char *, nsyms
+ 1);
1687 user_toknums
= XCALLOC (short, nsyms
+ 1);
1688 user_toknums
[0] = 0;
1690 sprec
= XCALLOC (short, nsyms
);
1691 sassoc
= XCALLOC (short, nsyms
);
1693 max_user_token_number
= 256;
1694 last_user_token_number
= 256;
1696 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1698 if (bp
->class == nterm_sym
)
1700 bp
->value
+= ntokens
;
1704 /* this symbol and its alias are a single token defn.
1705 allocate a tokno, and assign to both check agreement of
1706 ->prec and ->assoc fields and make both the same */
1708 bp
->value
= bp
->alias
->value
= tokno
++;
1710 if (bp
->prec
!= bp
->alias
->prec
)
1712 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1713 && bp
->user_token_number
== SALIAS
)
1714 complain (_("conflicting precedences for %s and %s"),
1715 bp
->tag
, bp
->alias
->tag
);
1717 bp
->alias
->prec
= bp
->prec
;
1719 bp
->prec
= bp
->alias
->prec
;
1722 if (bp
->assoc
!= bp
->alias
->assoc
)
1724 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1725 && bp
->user_token_number
== SALIAS
)
1726 complain (_("conflicting assoc values for %s and %s"),
1727 bp
->tag
, bp
->alias
->tag
);
1729 bp
->alias
->assoc
= bp
->assoc
;
1731 bp
->assoc
= bp
->alias
->assoc
;
1734 if (bp
->user_token_number
== SALIAS
)
1735 continue; /* do not do processing below for SALIASs */
1738 else /* bp->class == token_sym */
1740 bp
->value
= tokno
++;
1743 if (bp
->class == token_sym
)
1745 if (translations
&& !(bp
->user_token_number
))
1746 bp
->user_token_number
= ++last_user_token_number
;
1747 if (bp
->user_token_number
> max_user_token_number
)
1748 max_user_token_number
= bp
->user_token_number
;
1751 tags
[bp
->value
] = bp
->tag
;
1752 user_toknums
[bp
->value
] = bp
->user_token_number
;
1753 sprec
[bp
->value
] = bp
->prec
;
1754 sassoc
[bp
->value
] = bp
->assoc
;
1762 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1764 /* initialize all entries for literal tokens to 2, the internal
1765 token number for $undefined., which represents all invalid
1767 for (j
= 0; j
<= max_user_token_number
; j
++)
1768 token_translations
[j
] = 2;
1770 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1772 if (bp
->value
>= ntokens
)
1773 continue; /* non-terminal */
1774 if (bp
->user_token_number
== SALIAS
)
1776 if (token_translations
[bp
->user_token_number
] != 2)
1777 complain (_("tokens %s and %s both assigned number %d"),
1778 tags
[token_translations
[bp
->user_token_number
]],
1779 bp
->tag
, bp
->user_token_number
);
1780 token_translations
[bp
->user_token_number
] = bp
->value
;
1784 error_token_number
= errtoken
->value
;
1786 if (!no_parser_flag
)
1787 output_token_defines (ftable
);
1789 if (startval
->class == unknown_sym
)
1790 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1791 else if (startval
->class == token_sym
)
1792 fatal (_("the start symbol %s is a token"), startval
->tag
);
1794 start_symbol
= startval
->value
;
1798 output_token_defines (fdefines
);
1802 if (spec_name_prefix
)
1803 fprintf (fdefines
, "\nextern YYSTYPE %slval;\n",
1806 fprintf (fdefines
, "\nextern YYSTYPE yylval;\n");
1809 if (semantic_parser
)
1810 for (i
= ntokens
; i
< nsyms
; i
++)
1812 /* don't make these for dummy nonterminals made by gensym. */
1813 if (*tags
[i
] != '@')
1814 fprintf (fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1817 /* `fdefines' is now a temporary file, so we need to copy its
1818 contents in `done', so we can't close it here. */
1826 /*---------------------------------------------------------------.
1827 | Convert the rules into the representation using RRHS, RLHS and |
1829 `---------------------------------------------------------------*/
1840 ritem
= XCALLOC (short, nitems
+ 1);
1841 rlhs
= XCALLOC (short, nrules
) - 1;
1842 rrhs
= XCALLOC (short, nrules
) - 1;
1843 rprec
= XCALLOC (short, nrules
) - 1;
1844 rprecsym
= XCALLOC (short, nrules
) - 1;
1845 rassoc
= XCALLOC (short, nrules
) - 1;
1853 rlhs
[ruleno
] = p
->sym
->value
;
1854 rrhs
[ruleno
] = itemno
;
1855 ruleprec
= p
->ruleprec
;
1860 ritem
[itemno
++] = p
->sym
->value
;
1861 /* A rule gets by default the precedence and associativity
1862 of the last token in it. */
1863 if (p
->sym
->class == token_sym
)
1865 rprec
[ruleno
] = p
->sym
->prec
;
1866 rassoc
[ruleno
] = p
->sym
->assoc
;
1872 /* If this rule has a %prec,
1873 the specified symbol's precedence replaces the default. */
1876 rprec
[ruleno
] = ruleprec
->prec
;
1877 rassoc
[ruleno
] = ruleprec
->assoc
;
1878 rprecsym
[ruleno
] = ruleprec
->value
;
1881 ritem
[itemno
++] = -ruleno
;
1891 /*-------------------------------------------------------------------.
1892 | Read in the grammar specification and record it in the format |
1893 | described in gram.h. All guards are copied into the FGUARD file |
1894 | and all actions into ACTION_OBSTACK, in each case forming the body |
1895 | of a C function (YYGUARD or YYACTION) which contains a switch |
1896 | statement to decide which guard or action to execute. |
1897 `-------------------------------------------------------------------*/
1903 startval
= NULL
; /* start symbol not specified yet. */
1906 /* initially assume token number translation not needed. */
1909 /* Nowadays translations is always set to 1, since we give `error' a
1910 user-token-number to satisfy the Posix demand for YYERRCODE==256.
1918 rline_allocated
= 10;
1919 rline
= XCALLOC (short, rline_allocated
);
1926 semantic_parser
= 0;
1934 /* Initialize the symbol table. */
1936 /* Construct the error token */
1937 errtoken
= getsym ("error");
1938 errtoken
->class = token_sym
;
1939 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1940 /* Construct a token that represents all undefined literal tokens.
1941 It is always token number 2. */
1942 undeftoken
= getsym ("$undefined.");
1943 undeftoken
->class = token_sym
;
1944 undeftoken
->user_token_number
= 2;
1946 /* Read the declaration section. Copy %{ ... %} groups to FTABLE
1947 and FDEFINES file. Also notice any %token, %left, etc. found
1949 putc ('\n', ftable
);
1951 /* %s, made from %s\n\
1952 by GNU bison %s. */\n\
1953 \n", no_parser_flag
? "Bison-generated parse tables" : "A Bison parser", infile
, VERSION
);
1955 fputs ("#define YYBISON 1 /* Identify Bison output. */\n\n", ftable
);
1956 read_declarations ();
1957 /* Start writing the guard and action files, if they are needed. */
1959 /* Read in the grammar, build grammar in list form. Write out
1960 guards and actions. */
1962 /* Now we know whether we need the line-number stack. If we do,
1963 write its type into the .tab.h file. */
1965 reader_output_yylsp (fdefines
);
1966 /* Write closing delimiters for actions and guards. */
1969 fputs ("#define YYLSP_NEEDED\n\n", ftable
);
1970 /* Assign the symbols their symbol numbers. Write #defines for the
1971 token symbols into FDEFINES if requested. */
1973 /* Convert the grammar into the format described in gram.h. */
1975 /* Free the symbol table data structure since symbols are now all
1976 referred to by symbol number. */
1982 reader_output_yylsp (FILE *f
)
1988 typedef struct yyltype\n\
1999 # define YYLTYPE yyltype\n\