]>
git.saurik.com Git - bison.git/blob - src/reader.c
e74ff3237d0dab3efdf1f89967197cfb42e947e5
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 FOUT and OOUT if non null. MATCH is |
158 | the delimiter of the string (either ' or "). |
159 `-----------------------------------------------------------------*/
162 copy_string (FILE *fin
, FILE *fout
, struct obstack
*oout
, int match
)
169 obstack_1grow (oout
, match
);
176 fatal (_("unterminated string at end of file"));
179 complain (_("unterminated string"));
181 c
= match
; /* invent terminator */
188 obstack_1grow (oout
, c
);
194 fatal (_("unterminated string at end of file"));
198 obstack_1grow (oout
, c
);
210 obstack_1grow (oout
, c
);
214 /*----------------------------------------------------------------.
215 | Dump the wannabee comment from IN to OUT1 and OUT2. In fact we |
216 | just saw a `/', which might or might not be a comment. In any |
217 | case, copy what we saw. |
219 | OUT2 might be NULL. |
220 `----------------------------------------------------------------*/
223 copy_comment2 (FILE *fin
, FILE *out1
,
224 struct obstack
*oout2
, struct obstack
*oout
)
230 /* We read a `/', output it. */
234 obstack_1grow (oout
, '/');
236 obstack_1grow (oout2
, '/');
238 switch ((c
= getc (fin
)))
254 obstack_1grow (oout
, c
);
256 obstack_1grow (oout2
, c
);
262 if (!cplus_comment
&& c
== '*')
269 obstack_1grow (oout
, c
);
271 obstack_1grow (oout2
, c
);
280 obstack_1grow (oout
, c
);
282 obstack_1grow (oout2
, c
);
292 obstack_1grow (oout
, c
);
294 obstack_1grow (oout2
, c
);
301 fatal (_("unterminated comment"));
307 obstack_1grow (oout
, c
);
309 obstack_1grow (oout2
, c
);
316 /*-------------------------------------------------------------------.
317 | Dump the comment (actually the current string starting with a `/') |
318 | from FIN to FOUT. |
319 `-------------------------------------------------------------------*/
322 copy_comment (FILE *fin
, FILE *fout
, struct obstack
*oout
)
324 copy_comment2 (fin
, fout
, NULL
, oout
);
328 /*-----------------------------------------------------------------.
329 | FIN is pointing to a location (i.e., a `@'). Output to FOUT a |
330 | reference to this location. STACK_OFFSET is the number of values |
331 | in the current rule so far, which says where to find `$0' with |
332 | respect to the top of the stack. |
333 `-----------------------------------------------------------------*/
336 copy_at (FILE *fin
, FILE *fout
, struct obstack
*oout
, int stack_offset
)
344 fprintf (fout
, "yyloc");
346 obstack_grow_literal_string (oout
, "yyloc");
349 else if (isdigit (c
) || c
== '-')
355 n
= read_signed_integer (fin
);
357 sprintf (buf
, "yylsp[%d]", n
- stack_offset
);
361 obstack_grow (oout
, buf
, strlen (buf
));
368 complain (_("%s is invalid"), quote (buf
));
373 /*-------------------------------------------------------------------.
374 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
376 | Possible inputs: $[<TYPENAME>]($|integer) |
378 | Output to FOUT a reference to this semantic value. STACK_OFFSET is |
379 | the number of values in the current rule so far, which says where |
380 | to find `$0' with respect to the top of the stack. |
381 `-------------------------------------------------------------------*/
384 copy_dollar (FILE *fin
, FILE *fout
, struct obstack
*oout
,
385 symbol_list
*rule
, int stack_offset
)
388 char *type_name
= NULL
;
390 /* Get the type name if explicit. */
393 read_type_name (fin
);
394 type_name
= token_buffer
;
395 value_components_used
= 1;
402 fputs ("yyval", fout
);
404 obstack_grow_literal_string (oout
, "yyval");
407 type_name
= get_type_name (0, rule
);
411 fprintf (fout
, ".%s", type_name
);
413 obstack_fgrow1 (oout
, ".%s", type_name
);
415 if (!type_name
&& typed
)
416 complain (_("$$ of `%s' has no declared type"),
419 else if (isdigit (c
) || c
== '-')
423 n
= read_signed_integer (fin
);
425 if (!type_name
&& n
> 0)
426 type_name
= get_type_name (n
, rule
);
429 fprintf (fout
, "yyvsp[%d]", n
- stack_offset
);
431 obstack_fgrow1 (oout
, "yyvsp[%d]", n
- stack_offset
);
436 fprintf (fout
, ".%s", type_name
);
438 obstack_fgrow1 (oout
, ".%s", type_name
);
440 if (!type_name
&& typed
)
441 complain (_("$%d of `%s' has no declared type"),
448 complain (_("%s is invalid"), quote (buf
));
452 /*-------------------------------------------------------------------.
453 | Copy the contents of a `%{ ... %}' into the definitions file. The |
454 | `%{' has already been read. Return after reading the `%}'. |
455 `-------------------------------------------------------------------*/
458 copy_definition (void)
461 /* -1 while reading a character if prev char was %. */
465 obstack_fgrow2 (&attrs_obstack
, "#line %d %s\n",
466 lineno
, quotearg_style (c_quoting_style
, infile
));
477 obstack_1grow (&attrs_obstack
, c
);
487 copy_string (finput
, 0, &attrs_obstack
, c
);
491 copy_comment (finput
, 0, &attrs_obstack
);
495 fatal ("%s", _("unterminated `%{' definition"));
498 obstack_1grow (&attrs_obstack
, c
);
507 obstack_1grow (&attrs_obstack
, '%');
514 /*-------------------------------------------------------------------.
515 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
516 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
518 `-------------------------------------------------------------------*/
521 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
526 /* The symbol being defined. */
527 struct bucket
*symbol
= NULL
;
529 /* After `%token' and `%nterm', any number of symbols maybe be
533 int tmp_char
= ungetc (skip_white_space (), finput
);
535 /* `%' (for instance from `%token', or from `%%' etc.) is the
536 only valid means to end this declaration. */
540 fatal (_("Premature EOF after %s"), token_buffer
);
548 if (token
== TYPENAME
)
550 typename
= xstrdup (token_buffer
);
551 value_components_used
= 1;
554 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"' && symbol
)
557 warn (_("symbol `%s' used more than once as a literal string"),
559 else if (symbol
->alias
)
560 warn (_("symbol `%s' given more than one literal string"),
564 symval
->class = token_sym
;
565 symval
->type_name
= typename
;
566 symval
->user_token_number
= symbol
->user_token_number
;
567 symbol
->user_token_number
= SALIAS
;
568 symval
->alias
= symbol
;
569 symbol
->alias
= symval
;
570 /* symbol and symval combined are only one symbol */
576 else if (token
== IDENTIFIER
)
578 int oldclass
= symval
->class;
581 if (symbol
->class == what_is_not
)
582 complain (_("symbol %s redefined"), symbol
->tag
);
583 symbol
->class = what_is
;
584 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
585 symbol
->value
= nvars
++;
589 if (symbol
->type_name
== NULL
)
590 symbol
->type_name
= typename
;
591 else if (strcmp (typename
, symbol
->type_name
) != 0)
592 complain (_("type redeclaration for %s"), symbol
->tag
);
595 else if (symbol
&& token
== NUMBER
)
597 symbol
->user_token_number
= numval
;
602 complain (_("`%s' is invalid in %s"),
603 token_buffer
, (what_is
== token_sym
) ? "%token" : "%nterm");
611 /*------------------------------.
612 | Parse what comes after %start |
613 `------------------------------*/
616 parse_start_decl (void)
619 complain (_("multiple %s declarations"), "%start");
620 if (lex () != IDENTIFIER
)
621 complain (_("invalid %s declaration"), "%start");
629 /*-----------------------------------------------------------.
630 | read in a %type declaration and record its information for |
631 | get_type_name to access |
632 `-----------------------------------------------------------*/
635 parse_type_decl (void)
639 if (lex () != TYPENAME
)
641 complain ("%s", _("%type declaration has no <typename>"));
646 name
= xstrdup (token_buffer
);
651 int tmp_char
= ungetc (skip_white_space (), finput
);
656 fatal (_("Premature EOF after %s"), token_buffer
);
668 if (symval
->type_name
== NULL
)
669 symval
->type_name
= name
;
670 else if (strcmp (name
, symval
->type_name
) != 0)
671 complain (_("type redeclaration for %s"), symval
->tag
);
676 complain (_("invalid %%type declaration due to item: %s"),
685 /*----------------------------------------------------------------.
686 | Read in a %left, %right or %nonassoc declaration and record its |
688 `----------------------------------------------------------------*/
691 parse_assoc_decl (associativity assoc
)
696 lastprec
++; /* Assign a new precedence level, never 0. */
701 int tmp_char
= ungetc (skip_white_space (), finput
);
706 fatal (_("Premature EOF after %s"), token_buffer
);
713 name
= xstrdup (token_buffer
);
720 if (symval
->prec
!= 0)
721 complain (_("redefining precedence of %s"), symval
->tag
);
722 symval
->prec
= lastprec
;
723 symval
->assoc
= assoc
;
724 if (symval
->class == nterm_sym
)
725 complain (_("symbol %s redefined"), symval
->tag
);
726 symval
->class = token_sym
;
728 { /* record the type, if one is specified */
729 if (symval
->type_name
== NULL
)
730 symval
->type_name
= name
;
731 else if (strcmp (name
, symval
->type_name
) != 0)
732 complain (_("type redeclaration for %s"), symval
->tag
);
737 if (prev
== IDENTIFIER
)
739 symval
->user_token_number
= numval
;
745 ("invalid text (%s) - number should be after identifier"),
755 complain (_("unexpected item: %s"), token_buffer
);
766 /*--------------------------------------------------------------.
767 | Copy the union declaration into ATTRS_OBSTACK (and fdefines), |
768 | where it is made into the definition of YYSTYPE, the type of |
769 | elements of the parser value stack. |
770 `--------------------------------------------------------------*/
773 parse_union_decl (void)
779 complain (_("multiple %s declarations"), "%union");
784 obstack_fgrow2 (&attrs_obstack
, "\n#line %d %s\n",
785 lineno
, quotearg_style (c_quoting_style
, infile
));
787 obstack_1grow (&attrs_obstack
, '\n');
789 obstack_grow_literal_string (&attrs_obstack
, "typedef union");
791 obstack_grow_literal_string (&defines_obstack
, "typedef union");
797 obstack_1grow (&attrs_obstack
, c
);
799 obstack_1grow (&defines_obstack
, c
);
808 copy_comment2 (finput
, 0, &defines_obstack
, &attrs_obstack
);
817 complain (_("unmatched %s"), "`}'");
821 obstack_grow_literal_string (&attrs_obstack
, " YYSTYPE;\n");
823 obstack_grow_literal_string (&defines_obstack
, " YYSTYPE;\n");
824 /* JF don't choke on trailing semi */
825 c
= skip_white_space ();
837 /*-------------------------------------------------------.
838 | Parse the declaration %expect N which says to expect N |
839 | shift-reduce conflicts. |
840 `-------------------------------------------------------*/
843 parse_expect_decl (void)
845 int c
= skip_white_space ();
849 complain (_("argument of %%expect is not an integer"));
851 expected_conflicts
= read_signed_integer (finput
);
855 /*-------------------------------------------------------------------.
856 | Parse what comes after %thong. the full syntax is |
858 | %thong <type> token number literal |
860 | the <type> or number may be omitted. The number specifies the |
861 | user_token_number. |
863 | Two symbols are entered in the table, one for the token symbol and |
864 | one for the literal. Both are given the <type>, if any, from the |
865 | declaration. The ->user_token_number of the first is SALIAS and |
866 | the ->user_token_number of the second is set to the number, if |
867 | any, from the declaration. The two symbols are linked via |
868 | pointers in their ->alias fields. |
870 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
871 | only the literal string is retained it is the literal string that |
872 | is output to yytname |
873 `-------------------------------------------------------------------*/
876 parse_thong_decl (void)
879 struct bucket
*symbol
;
884 token
= lex (); /* fetch typename or first token */
885 if (token
== TYPENAME
)
887 typename
= xstrdup (token_buffer
);
888 value_components_used
= 1;
889 token
= lex (); /* fetch first token */
892 /* process first token */
894 if (token
!= IDENTIFIER
)
896 complain (_("unrecognized item %s, expected an identifier"),
901 symval
->class = token_sym
;
902 symval
->type_name
= typename
;
903 symval
->user_token_number
= SALIAS
;
906 token
= lex (); /* get number or literal string */
911 token
= lex (); /* okay, did number, now get literal */
916 /* process literal string token */
918 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
920 complain (_("expected string constant instead of %s"), token_buffer
);
924 symval
->class = token_sym
;
925 symval
->type_name
= typename
;
926 symval
->user_token_number
= usrtoknum
;
928 symval
->alias
= symbol
;
929 symbol
->alias
= symval
;
931 /* symbol and symval combined are only one symbol. */
936 /*----------------------------------------------------------------.
937 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
938 | any `%' declarations, and copy the contents of any `%{ ... %}' |
939 | groups to ATTRS_OBSTACK. |
940 `----------------------------------------------------------------*/
943 read_declarations (void)
950 c
= skip_white_space ();
954 tok
= parse_percent_token ();
961 case PERCENT_LEFT_CURLY
:
966 parse_token_decl (token_sym
, nterm_sym
);
970 parse_token_decl (nterm_sym
, token_sym
);
986 parse_expect_decl ();
993 parse_assoc_decl (left_assoc
);
997 parse_assoc_decl (right_assoc
);
1001 parse_assoc_decl (non_assoc
);
1004 case SEMANTIC_PARSER
:
1005 if (semantic_parser
== 0)
1007 semantic_parser
= 1;
1008 open_extra_files ();
1020 complain (_("unrecognized: %s"), token_buffer
);
1025 fatal (_("no input grammar"));
1030 complain (_("unknown character: %s"), quote (buf
));
1036 /*-------------------------------------------------------------------.
1037 | Assuming that a `{' has just been seen, copy everything up to the |
1038 | matching `}' into the actions file. STACK_OFFSET is the number of |
1039 | values in the current rule so far, which says where to find `$0' |
1040 | with respect to the top of the stack. |
1041 `-------------------------------------------------------------------*/
1044 copy_action (symbol_list
*rule
, int stack_offset
)
1050 /* offset is always 0 if parser has already popped the stack pointer */
1051 if (semantic_parser
)
1054 sprintf (buf
, "\ncase %d:\n", nrules
);
1055 obstack_grow (&action_obstack
, buf
, strlen (buf
));
1059 sprintf (buf
, "#line %d %s\n",
1060 lineno
, quotearg_style (c_quoting_style
, infile
));
1061 obstack_grow (&action_obstack
, buf
, strlen (buf
));
1063 obstack_1grow (&action_obstack
, '{');
1075 obstack_1grow (&action_obstack
, c
);
1080 obstack_1grow (&action_obstack
, c
);
1086 copy_string (finput
, 0, &action_obstack
, c
);
1090 copy_comment (finput
, 0, &action_obstack
);
1094 copy_dollar (finput
, 0, &action_obstack
,
1095 rule
, stack_offset
);
1099 copy_at (finput
, 0, &action_obstack
,
1104 fatal (_("unmatched %s"), "`{'");
1107 obstack_1grow (&action_obstack
, c
);
1113 /* above loop exits when c is '}' */
1117 obstack_1grow (&action_obstack
, c
);
1122 obstack_grow_literal_string (&action_obstack
,
1126 /*-------------------------------------------------------------------.
1127 | After `%guard' is seen in the input file, copy the actual guard |
1128 | into the guards file. If the guard is followed by an action, copy |
1129 | that into the actions file. STACK_OFFSET is the number of values |
1130 | in the current rule so far, which says where to find `$0' with |
1131 | respect to the top of the stack, for the simple parser in which |
1132 | the stack is not popped until after the guard is run. |
1133 `-------------------------------------------------------------------*/
1136 copy_guard (symbol_list
*rule
, int stack_offset
)
1142 /* offset is always 0 if parser has already popped the stack pointer */
1143 if (semantic_parser
)
1146 fprintf (fguard
, "\ncase %d:\n", nrules
);
1148 fprintf (fguard
, "#line %d %s\n",
1149 lineno
, quotearg_style (c_quoting_style
, infile
));
1155 while (brace_flag
? (count
> 0) : (c
!= ';'))
1176 complain (_("unmatched %s"), "`}'");
1177 c
= getc (finput
); /* skip it */
1183 copy_string (finput
, fguard
, 0, c
);
1187 copy_comment (finput
, fguard
, 0);
1191 copy_dollar (finput
, fguard
, 0, rule
, stack_offset
);
1195 copy_at (finput
, fguard
, 0, stack_offset
);
1199 fatal ("%s", _("unterminated %guard clause"));
1205 if (c
!= '}' || count
!= 0)
1209 c
= skip_white_space ();
1211 fprintf (fguard
, ";\n break;}");
1213 copy_action (rule
, stack_offset
);
1216 c
= getc (finput
); /* why not skip_white_space -wjh */
1218 copy_action (rule
, stack_offset
);
1226 record_rule_line (void)
1228 /* Record each rule's source line number in rline table. */
1230 if (nrules
>= rline_allocated
)
1232 rline_allocated
= nrules
* 2;
1233 rline
= XREALLOC (rline
, short, rline_allocated
);
1235 rline
[nrules
] = lineno
;
1239 /*-------------------------------------------------------------------.
1240 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1241 | with the user's names. |
1242 `-------------------------------------------------------------------*/
1249 sprintf (token_buffer
, "@%d", ++gensym_count
);
1250 sym
= getsym (token_buffer
);
1251 sym
->class = nterm_sym
;
1252 sym
->value
= nvars
++;
1257 /*------------------------------------------------------------------.
1258 | read in a %type declaration and record its information for |
1259 | get_type_name to access. This is unused. It is only called from |
1260 | the #if 0 part of readgram |
1261 `------------------------------------------------------------------*/
1274 complain (_("invalid %s declaration"), "%type");
1278 name
= xstrdup (token_buffer
);
1293 if (symval
->type_name
== NULL
)
1294 symval
->type_name
= name
;
1295 else if (strcmp (name
, symval
->type_name
) != 0)
1296 complain (_("type redeclaration for %s"), symval
->tag
);
1308 /*------------------------------------------------------------------.
1309 | Parse the input grammar into a one symbol_list structure. Each |
1310 | rule is represented by a sequence of symbols: the left hand side |
1311 | followed by the contents of the right hand side, followed by a |
1312 | null pointer instead of a symbol to terminate the rule. The next |
1313 | symbol is the lhs of the following rule. |
1315 | All guards and actions are copied out to the appropriate files, |
1316 | labelled by the rule number they apply to. |
1317 `------------------------------------------------------------------*/
1328 /* Points to first symbol_list of current rule. its symbol is the
1331 /* Points to the symbol_list preceding crule. */
1332 symbol_list
*crule1
;
1338 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1340 if (t
== IDENTIFIER
|| t
== BAR
)
1342 int action_flag
= 0;
1343 /* Number of symbols in rhs of this rule so far */
1345 int xactions
= 0; /* JF for error checking */
1346 bucket
*first_rhs
= 0;
1348 if (t
== IDENTIFIER
)
1361 complain (_("ill-formed rule: initial symbol not followed by colon"));
1366 if (nrules
== 0 && t
== BAR
)
1368 complain (_("grammar starts with vertical bar"));
1369 lhs
= symval
; /* BOGUS: use a random symval */
1371 /* start a new rule and record its lhs. */
1376 record_rule_line ();
1378 p
= XCALLOC (symbol_list
, 1);
1390 /* mark the rule's lhs as a nonterminal if not already so. */
1392 if (lhs
->class == unknown_sym
)
1394 lhs
->class = nterm_sym
;
1398 else if (lhs
->class == token_sym
)
1399 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1401 /* read the rhs of the rule. */
1409 crule
->ruleprec
= symval
;
1413 if (!(t
== IDENTIFIER
|| t
== LEFT_CURLY
))
1416 /* If next token is an identifier, see if a colon follows it.
1417 If one does, exit this rule now. */
1418 if (t
== IDENTIFIER
)
1430 if (!first_rhs
) /* JF */
1432 /* Not followed by colon =>
1433 process as part of this rule's rhs. */
1436 /* If we just passed an action, that action was in the middle
1437 of a rule, so make a dummy rule to reduce it to a
1443 /* Since the action was written out with this rule's
1444 number, we must give the new rule this number by
1445 inserting the new rule before it. */
1447 /* Make a dummy nonterminal, a gensym. */
1450 /* Make a new rule, whose body is empty,
1451 before the current one, so that the action
1452 just read can belong to it. */
1455 record_rule_line ();
1456 p
= XCALLOC (symbol_list
, 1);
1462 crule1
= XCALLOC (symbol_list
, 1);
1464 crule1
->next
= crule
;
1466 /* Insert the dummy generated by that rule into this
1469 p
= XCALLOC (symbol_list
, 1);
1477 if (t
== IDENTIFIER
)
1480 p
= XCALLOC (symbol_list
, 1);
1485 else /* handle an action. */
1487 copy_action (crule
, rulelength
);
1489 xactions
++; /* JF */
1492 } /* end of read rhs of rule */
1494 /* Put an empty link in the list to mark the end of this rule */
1495 p
= XCALLOC (symbol_list
, 1);
1501 complain (_("two @prec's in a row"));
1503 crule
->ruleprec
= symval
;
1508 if (!semantic_parser
)
1509 complain (_("%%guard present but %%semantic_parser not specified"));
1511 copy_guard (crule
, rulelength
);
1514 else if (t
== LEFT_CURLY
)
1516 /* This case never occurs -wjh */
1518 complain (_("two actions at end of one rule"));
1519 copy_action (crule
, rulelength
);
1521 xactions
++; /* -wjh */
1524 /* If $$ is being set in default way, report if any type
1527 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1529 if (lhs
->type_name
== 0
1530 || first_rhs
->type_name
== 0
1531 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1532 complain (_("type clash (`%s' `%s') on default action"),
1533 lhs
->type_name
? lhs
->type_name
: "",
1534 first_rhs
->type_name
? first_rhs
->type_name
: "");
1536 /* Warn if there is no default for $$ but we need one. */
1537 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1538 complain (_("empty rule for typed nonterminal, and no action"));
1543 /* these things can appear as alternatives to rules. */
1545 a) none of the documentation allows them
1546 b) most of them scan forward until finding a next %
1547 thus they may swallow lots of intervening rules
1549 else if (t
== TOKEN
)
1551 parse_token_decl (token_sym
, nterm_sym
);
1554 else if (t
== NTERM
)
1556 parse_token_decl (nterm_sym
, token_sym
);
1563 else if (t
== UNION
)
1565 parse_union_decl ();
1568 else if (t
== EXPECT
)
1570 parse_expect_decl ();
1573 else if (t
== START
)
1575 parse_start_decl ();
1582 complain (_("invalid input: %s"), token_buffer
);
1587 /* grammar has been read. Do some checking */
1589 if (nsyms
> MAXSHORT
)
1590 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1593 fatal (_("no rules in the input grammar"));
1595 /* JF put out same default YYSTYPE as YACC does */
1597 && !value_components_used
)
1599 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1600 but it seems better to be consistent.
1601 Most programs should declare their own type anyway. */
1602 obstack_grow_literal_string (&attrs_obstack
,
1603 "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1605 obstack_grow_literal_string (&defines_obstack
, "\
1607 # define YYSTYPE int\n\
1611 /* Report any undefined symbols and consider them nonterminals. */
1613 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1614 if (bp
->class == unknown_sym
)
1617 ("symbol %s is used, but is not defined as a token and has no rules"),
1619 bp
->class = nterm_sym
;
1620 bp
->value
= nvars
++;
1623 ntokens
= nsyms
- nvars
;
1626 /*--------------------------------------------------------------.
1627 | For named tokens, but not literal ones, define the name. The |
1628 | value is the user token number. |
1629 `--------------------------------------------------------------*/
1632 output_token_defines (struct obstack
*oout
)
1638 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1640 symbol
= bp
->tag
; /* get symbol */
1642 if (bp
->value
>= ntokens
)
1644 if (bp
->user_token_number
== SALIAS
)
1646 if ('\'' == *symbol
)
1647 continue; /* skip literal character */
1649 continue; /* skip error token */
1650 if ('\"' == *symbol
)
1652 /* use literal string only if given a symbol with an alias */
1654 symbol
= bp
->alias
->tag
;
1659 /* Don't #define nonliteral tokens whose names contain periods. */
1661 while ((c
= *cp
++) && c
!= '.');
1665 obstack_fgrow2 (oout
, "#define\t%s\t%d\n",
1667 ((translations
&& !raw_flag
)
1668 ? bp
->user_token_number
: bp
->value
));
1669 if (semantic_parser
)
1670 obstack_fgrow2 (oout
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1673 obstack_1grow (oout
, '\n');
1677 /*------------------------------------------------------------------.
1678 | Assign symbol numbers, and write definition of token names into |
1679 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1681 `------------------------------------------------------------------*/
1689 int last_user_token_number
;
1690 static char DOLLAR
[] = "$";
1692 /* int lossage = 0; JF set but not used */
1694 tags
= XCALLOC (char *, nsyms
+ 1);
1696 user_toknums
= XCALLOC (short, nsyms
+ 1);
1697 user_toknums
[0] = 0;
1699 sprec
= XCALLOC (short, nsyms
);
1700 sassoc
= XCALLOC (short, nsyms
);
1702 max_user_token_number
= 256;
1703 last_user_token_number
= 256;
1705 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1707 if (bp
->class == nterm_sym
)
1709 bp
->value
+= ntokens
;
1713 /* this symbol and its alias are a single token defn.
1714 allocate a tokno, and assign to both check agreement of
1715 ->prec and ->assoc fields and make both the same */
1717 bp
->value
= bp
->alias
->value
= tokno
++;
1719 if (bp
->prec
!= bp
->alias
->prec
)
1721 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1722 && bp
->user_token_number
== SALIAS
)
1723 complain (_("conflicting precedences for %s and %s"),
1724 bp
->tag
, bp
->alias
->tag
);
1726 bp
->alias
->prec
= bp
->prec
;
1728 bp
->prec
= bp
->alias
->prec
;
1731 if (bp
->assoc
!= bp
->alias
->assoc
)
1733 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1734 && bp
->user_token_number
== SALIAS
)
1735 complain (_("conflicting assoc values for %s and %s"),
1736 bp
->tag
, bp
->alias
->tag
);
1738 bp
->alias
->assoc
= bp
->assoc
;
1740 bp
->assoc
= bp
->alias
->assoc
;
1743 if (bp
->user_token_number
== SALIAS
)
1744 continue; /* do not do processing below for SALIASs */
1747 else /* bp->class == token_sym */
1749 bp
->value
= tokno
++;
1752 if (bp
->class == token_sym
)
1754 if (translations
&& !(bp
->user_token_number
))
1755 bp
->user_token_number
= ++last_user_token_number
;
1756 if (bp
->user_token_number
> max_user_token_number
)
1757 max_user_token_number
= bp
->user_token_number
;
1760 tags
[bp
->value
] = bp
->tag
;
1761 user_toknums
[bp
->value
] = bp
->user_token_number
;
1762 sprec
[bp
->value
] = bp
->prec
;
1763 sassoc
[bp
->value
] = bp
->assoc
;
1771 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1773 /* initialize all entries for literal tokens to 2, the internal
1774 token number for $undefined., which represents all invalid
1776 for (j
= 0; j
<= max_user_token_number
; j
++)
1777 token_translations
[j
] = 2;
1779 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1781 if (bp
->value
>= ntokens
)
1782 continue; /* non-terminal */
1783 if (bp
->user_token_number
== SALIAS
)
1785 if (token_translations
[bp
->user_token_number
] != 2)
1786 complain (_("tokens %s and %s both assigned number %d"),
1787 tags
[token_translations
[bp
->user_token_number
]],
1788 bp
->tag
, bp
->user_token_number
);
1789 token_translations
[bp
->user_token_number
] = bp
->value
;
1793 error_token_number
= errtoken
->value
;
1795 if (!no_parser_flag
)
1796 output_token_defines (&table_obstack
);
1798 if (startval
->class == unknown_sym
)
1799 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1800 else if (startval
->class == token_sym
)
1801 fatal (_("the start symbol %s is a token"), startval
->tag
);
1803 start_symbol
= startval
->value
;
1807 output_token_defines (&defines_obstack
);
1811 if (spec_name_prefix
)
1812 obstack_fgrow1 (&defines_obstack
, "\nextern YYSTYPE %slval;\n",
1815 obstack_grow_literal_string (&defines_obstack
,
1816 "\nextern YYSTYPE yylval;\n");
1819 if (semantic_parser
)
1820 for (i
= ntokens
; i
< nsyms
; i
++)
1822 /* don't make these for dummy nonterminals made by gensym. */
1823 if (*tags
[i
] != '@')
1824 obstack_fgrow2 (&defines_obstack
,
1825 "#define\tNT%s\t%d\n", tags
[i
], i
);
1828 /* `fdefines' is now a temporary file, so we need to copy its
1829 contents in `done', so we can't close it here. */
1837 /*---------------------------------------------------------------.
1838 | Convert the rules into the representation using RRHS, RLHS and |
1840 `---------------------------------------------------------------*/
1851 ritem
= XCALLOC (short, nitems
+ 1);
1852 rlhs
= XCALLOC (short, nrules
) - 1;
1853 rrhs
= XCALLOC (short, nrules
) - 1;
1854 rprec
= XCALLOC (short, nrules
) - 1;
1855 rprecsym
= XCALLOC (short, nrules
) - 1;
1856 rassoc
= XCALLOC (short, nrules
) - 1;
1864 rlhs
[ruleno
] = p
->sym
->value
;
1865 rrhs
[ruleno
] = itemno
;
1866 ruleprec
= p
->ruleprec
;
1871 ritem
[itemno
++] = p
->sym
->value
;
1872 /* A rule gets by default the precedence and associativity
1873 of the last token in it. */
1874 if (p
->sym
->class == token_sym
)
1876 rprec
[ruleno
] = p
->sym
->prec
;
1877 rassoc
[ruleno
] = p
->sym
->assoc
;
1883 /* If this rule has a %prec,
1884 the specified symbol's precedence replaces the default. */
1887 rprec
[ruleno
] = ruleprec
->prec
;
1888 rassoc
[ruleno
] = ruleprec
->assoc
;
1889 rprecsym
[ruleno
] = ruleprec
->value
;
1892 ritem
[itemno
++] = -ruleno
;
1902 /*-------------------------------------------------------------------.
1903 | Read in the grammar specification and record it in the format |
1904 | described in gram.h. All guards are copied into the FGUARD file |
1905 | and all actions into ACTION_OBSTACK, in each case forming the body |
1906 | of a C function (YYGUARD or YYACTION) which contains a switch |
1907 | statement to decide which guard or action to execute. |
1908 `-------------------------------------------------------------------*/
1914 startval
= NULL
; /* start symbol not specified yet. */
1917 /* initially assume token number translation not needed. */
1920 /* Nowadays translations is always set to 1, since we give `error' a
1921 user-token-number to satisfy the Posix demand for YYERRCODE==256.
1929 rline_allocated
= 10;
1930 rline
= XCALLOC (short, rline_allocated
);
1937 semantic_parser
= 0;
1945 /* Initialize the symbol table. */
1947 /* Construct the error token */
1948 errtoken
= getsym ("error");
1949 errtoken
->class = token_sym
;
1950 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1951 /* Construct a token that represents all undefined literal tokens.
1952 It is always token number 2. */
1953 undeftoken
= getsym ("$undefined.");
1954 undeftoken
->class = token_sym
;
1955 undeftoken
->user_token_number
= 2;
1957 /* Read the declaration section. Copy %{ ... %} groups to
1958 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1959 etc. found there. */
1960 obstack_1grow (&table_obstack
, '\n');
1961 obstack_fgrow3 (&table_obstack
, "\
1962 /* %s, made from %s\n\
1963 by GNU bison %s. */\n\
1965 no_parser_flag
? "Bison-generated parse tables" : "A Bison parser",
1968 obstack_grow_literal_string (&table_obstack
,
1969 "#define YYBISON 1 /* Identify Bison output. */\n\n");
1970 read_declarations ();
1971 /* Start writing the guard and action files, if they are needed. */
1973 /* Read in the grammar, build grammar in list form. Write out
1974 guards and actions. */
1976 /* Now we know whether we need the line-number stack. If we do,
1977 write its type into the .tab.h file. */
1979 reader_output_yylsp (&defines_obstack
);
1980 /* Write closing delimiters for actions and guards. */
1983 obstack_grow_literal_string (&table_obstack
, "#define YYLSP_NEEDED 1\n\n");
1984 /* Assign the symbols their symbol numbers. Write #defines for the
1985 token symbols into FDEFINES if requested. */
1987 /* Convert the grammar into the format described in gram.h. */
1989 /* Free the symbol table data structure since symbols are now all
1990 referred to by symbol number. */
1995 /*------------------------------------------------------------------.
1996 | Define YYLTYPE. Cannot be in the skeleton since we might have to |
1997 | output it in the headers if --defines is used. |
1998 `------------------------------------------------------------------*/
2001 reader_output_yylsp (struct obstack
*oout
)
2004 obstack_grow_literal_string (oout
, "\
2007 typedef struct yyltype\n\
2010 int first_column;\n\
2016 # define YYLTYPE yyltype\n\