]>
git.saurik.com Git - bison.git/blob - src/reader.c
1 /* Input parser for bison
2 Copyright (C) 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. */
33 #include "conflicts.h"
35 extern char *printable_version
PARAMS ((int));
55 #define YYLTYPE yyltype\n\
59 /* Number of slots allocated (but not necessarily used yet) in `rline' */
60 static int rline_allocated
;
62 typedef struct symbol_list
64 struct symbol_list
*next
;
73 static symbol_list
*grammar
;
74 static int start_flag
;
75 static bucket
*startval
;
77 /* Nonzero if components of semantic values are used, implying
78 they must be unions. */
79 static int value_components_used
;
81 static int typed
; /* nonzero if %union has been seen. */
83 static int lastprec
; /* incremented for each %left, %right or %nonassoc seen */
85 static int gensym_count
; /* incremented for each generated symbol */
87 static bucket
*errtoken
;
88 static bucket
*undeftoken
;
90 /* Nonzero if any action or guard uses the @n construct. */
91 static int yylsp_needed
;
94 /*===================\
96 \===================*/
99 skip_to_char (int target
)
103 complain (_(" Skipping to next \\n"));
105 complain (_(" Skipping to next %c"), target
);
108 c
= skip_white_space ();
109 while (c
!= target
&& c
!= EOF
);
115 /*---------------------------------------------------------.
116 | Read a signed integer from STREAM and return its value. |
117 `---------------------------------------------------------*/
120 read_signed_integer (FILE *stream
)
122 int c
= getc (stream
);
134 n
= 10 * n
+ (c
- '0');
143 /*-------------------------------------------------------------------.
144 | Dump the string from FINPUT to FOUTPUT. MATCH is the delimiter of |
145 | the string (either ' or "). |
146 `-------------------------------------------------------------------*/
149 copy_string (FILE *fin
, FILE *fout
, int match
)
159 fatal (_("unterminated string at end of file"));
162 complain (_("unterminated string"));
164 c
= match
; /* invent terminator */
174 fatal (_("unterminated string at end of file"));
187 /*---------------------------------------------------------------.
188 | Dump the comment from IN to OUT1 and OUT2. C is either `*' or |
189 | `/', depending upon the type of comments used. OUT2 might be |
191 `---------------------------------------------------------------*/
194 copy_comment2 (FILE *in
, FILE *out1
, FILE *out2
, int c
)
199 cplus_comment
= (c
== '/');
208 if (!cplus_comment
&& c
== '*')
238 fatal (_("unterminated comment"));
250 /*------------------------------------------------------------.
251 | Dump the comment from FIN to FOUT. C is either `*' or `/', |
252 | depending upon the type of comments used. |
253 `------------------------------------------------------------*/
256 copy_comment (FILE *fin
, FILE *fout
, int c
)
258 copy_comment2 (fin
, fout
, NULL
, c
);
262 /*-----------------------------------------------------------------.
263 | FIN is pointing to a location (i.e., a `@'). Output to FOUT a |
264 | reference to this location. STACK_OFFSET is the number of values |
265 | in the current rule so far, which says where to find `$0' with |
266 | respect to the top of the stack. |
267 `-----------------------------------------------------------------*/
270 copy_at (FILE *fin
, FILE *fout
, int stack_offset
)
277 fprintf (fout
, "yyloc");
280 else if (isdigit (c
) || c
== '-')
285 n
= read_signed_integer (fin
);
287 fprintf (fout
, "yylsp[%d]", n
- stack_offset
);
291 complain (_("@%s is invalid"), printable_version (c
));
294 /*-------------------------------------------------------------------.
295 | Copy the contents of a `%{ ... %}' into the definitions file. The |
296 | `%{' has already been read. Return after reading the `%}'. |
297 `-------------------------------------------------------------------*/
300 copy_definition (void)
303 /* -1 while reading a character if prev char was %. */
307 fprintf (fattrs
, "#line %d \"%s\"\n", lineno
, infile
);
328 copy_string (finput
, fattrs
, c
);
334 if (c
!= '*' && c
!= '/')
336 copy_comment (finput
, fattrs
, c
);
340 fatal ("%s", _("unterminated `%{' definition"));
361 /*-----------------------------------------------------------------.
362 | Parse what comes after %token or %nterm. For %token, what_is is |
363 | STOKEN and what_is_not is SNTERM. For %nterm, the arguments are |
365 `-----------------------------------------------------------------*/
368 parse_token_decl (int what_is
, int what_is_not
)
372 struct bucket
*symbol
= NULL
; /* pts to symbol being defined */
377 int tmp_char
= ungetc (skip_white_space (), finput
);
382 fatal (_("Premature EOF after %s"), token_buffer
);
390 if (token
== TYPENAME
)
392 k
= strlen (token_buffer
);
393 typename
= NEW2 (k
+ 1, char);
394 strcpy (typename
, token_buffer
);
395 value_components_used
= 1;
398 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"' && symbol
)
401 symval
->class = STOKEN
;
402 symval
->type_name
= typename
;
403 symval
->user_token_number
= symbol
->user_token_number
;
404 symbol
->user_token_number
= SALIAS
;
406 symval
->alias
= symbol
;
407 symbol
->alias
= symval
;
410 nsyms
--; /* symbol and symval combined are only one symbol */
412 else if (token
== IDENTIFIER
)
414 int oldclass
= symval
->class;
417 if (symbol
->class == what_is_not
)
418 complain (_("symbol %s redefined"), symbol
->tag
);
419 symbol
->class = what_is
;
420 if (what_is
== SNTERM
&& oldclass
!= SNTERM
)
421 symbol
->value
= nvars
++;
425 if (symbol
->type_name
== NULL
)
426 symbol
->type_name
= typename
;
427 else if (strcmp (typename
, symbol
->type_name
) != 0)
428 complain (_("type redeclaration for %s"), symbol
->tag
);
431 else if (symbol
&& token
== NUMBER
)
433 symbol
->user_token_number
= numval
;
438 complain (_("`%s' is invalid in %s"),
439 token_buffer
, (what_is
== STOKEN
) ? "%token" : "%nterm");
447 /* Parse what comes after %start */
450 parse_start_decl (void)
453 complain (_("multiple %s declarations"), "%start");
454 if (lex () != IDENTIFIER
)
455 complain (_("invalid %s declaration"), "%start");
465 /*--------------------------------------------------------------.
466 | Get the data type (alternative in the union) of the value for |
467 | symbol n in rule rule. |
468 `--------------------------------------------------------------*/
471 get_type_name (int n
, symbol_list
* rule
)
478 complain (_("invalid $ value"));
488 if (rp
== NULL
|| rp
->sym
== NULL
)
490 complain (_("invalid $ value"));
496 return rp
->sym
->type_name
;
500 /*-----------------------------------------------------------.
501 | read in a %type declaration and record its information for |
502 | get_type_name to access |
503 `-----------------------------------------------------------*/
506 parse_type_decl (void)
511 if (lex () != TYPENAME
)
513 complain ("%s", _("%type declaration has no <typename>"));
518 k
= strlen (token_buffer
);
519 name
= NEW2 (k
+ 1, char);
520 strcpy (name
, token_buffer
);
525 int tmp_char
= ungetc (skip_white_space (), finput
);
530 fatal (_("Premature EOF after %s"), token_buffer
);
542 if (symval
->type_name
== NULL
)
543 symval
->type_name
= name
;
544 else if (strcmp (name
, symval
->type_name
) != 0)
545 complain (_("type redeclaration for %s"), symval
->tag
);
550 complain (_("invalid %%type declaration due to item: %s"),
559 /* read in a %left, %right or %nonassoc declaration and record its information. */
560 /* assoc is either LEFT_ASSOC, RIGHT_ASSOC or NON_ASSOC. */
563 parse_assoc_decl (int assoc
)
569 lastprec
++; /* Assign a new precedence level, never 0. */
574 int tmp_char
= ungetc (skip_white_space (), finput
);
579 fatal (_("Premature EOF after %s"), token_buffer
);
587 k
= strlen (token_buffer
);
588 name
= NEW2 (k
+ 1, char);
589 strcpy (name
, token_buffer
);
596 if (symval
->prec
!= 0)
597 complain (_("redefining precedence of %s"), symval
->tag
);
598 symval
->prec
= lastprec
;
599 symval
->assoc
= assoc
;
600 if (symval
->class == SNTERM
)
601 complain (_("symbol %s redefined"), symval
->tag
);
602 symval
->class = STOKEN
;
604 { /* record the type, if one is specified */
605 if (symval
->type_name
== NULL
)
606 symval
->type_name
= name
;
607 else if (strcmp (name
, symval
->type_name
) != 0)
608 complain (_("type redeclaration for %s"), symval
->tag
);
613 if (prev
== IDENTIFIER
)
615 symval
->user_token_number
= numval
;
621 ("invalid text (%s) - number should be after identifier"),
631 complain (_("unexpected item: %s"), token_buffer
);
642 /* copy the union declaration into fattrs (and fdefines),
643 where it is made into the
644 definition of YYSTYPE, the type of elements of the parser value stack. */
647 parse_union_decl (void)
653 complain (_("multiple %s declarations"), "%union");
658 fprintf (fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
660 fprintf (fattrs
, "\n");
662 fprintf (fattrs
, "typedef union");
664 fprintf (fdefines
, "typedef union");
682 if (c
!= '*' && c
!= '/')
684 copy_comment2 (finput
, fattrs
, fdefines
, c
);
694 complain (_("unmatched %s"), "`}'");
698 fprintf (fattrs
, " YYSTYPE;\n");
700 fprintf (fdefines
, " YYSTYPE;\n");
701 /* JF don't choke on trailing semi */
702 c
= skip_white_space ();
713 /* parse the declaration %expect N which says to expect N
714 shift-reduce conflicts. */
717 parse_expect_decl (void)
724 while (c
== ' ' || c
== '\t')
728 while (c
>= '0' && c
<= '9')
738 if (count
<= 0 || count
> 10)
739 complain ("%s", _("argument of %expect is not an integer"));
740 expected_conflicts
= atoi (buffer
);
744 /*-------------------------------------------------------------------.
745 | Parse what comes after %thong. the full syntax is |
747 | %thong <type> token number literal |
749 | the <type> or number may be omitted. The number specifies the |
750 | user_token_number. |
752 | Two symbols are entered in the table, one for the token symbol and |
753 | one for the literal. Both are given the <type>, if any, from the |
754 | declaration. The ->user_token_number of the first is SALIAS and |
755 | the ->user_token_number of the second is set to the number, if |
756 | any, from the declaration. The two symbols are linked via |
757 | pointers in their ->alias fields. |
759 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
760 | only the literal string is retained it is the literal string that |
761 | is output to yytname |
762 `-------------------------------------------------------------------*/
765 parse_thong_decl (void)
768 struct bucket
*symbol
;
773 token
= lex (); /* fetch typename or first token */
774 if (token
== TYPENAME
)
776 k
= strlen (token_buffer
);
777 typename
= NEW2 (k
+ 1, char);
778 strcpy (typename
, token_buffer
);
779 value_components_used
= 1;
780 token
= lex (); /* fetch first token */
783 /* process first token */
785 if (token
!= IDENTIFIER
)
787 complain (_("unrecognized item %s, expected an identifier"),
792 symval
->class = STOKEN
;
793 symval
->type_name
= typename
;
794 symval
->user_token_number
= SALIAS
;
797 token
= lex (); /* get number or literal string */
802 token
= lex (); /* okay, did number, now get literal */
807 /* process literal string token */
809 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
811 complain (_("expected string constant instead of %s"), token_buffer
);
815 symval
->class = STOKEN
;
816 symval
->type_name
= typename
;
817 symval
->user_token_number
= usrtoknum
;
819 symval
->alias
= symbol
;
820 symbol
->alias
= symval
;
822 nsyms
--; /* symbol and symval combined are only one symbol */
825 /*----------------------------------------------------------------.
826 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
827 | any `%' declarations, and copy the contents of any `%{ ... %}' |
828 | groups to fattrs. |
829 `----------------------------------------------------------------*/
832 read_declarations (void)
839 c
= skip_white_space ();
843 tok
= parse_percent_token ();
850 case PERCENT_LEFT_CURLY
:
855 parse_token_decl (STOKEN
, SNTERM
);
859 parse_token_decl (SNTERM
, STOKEN
);
875 parse_expect_decl ();
881 parse_assoc_decl (LEFT_ASSOC
);
885 parse_assoc_decl (RIGHT_ASSOC
);
889 parse_assoc_decl (NON_ASSOC
);
892 case SEMANTIC_PARSER
:
893 if (semantic_parser
== 0)
908 complain (_("unrecognized: %s"), token_buffer
);
913 fatal (_("no input grammar"));
916 complain (_("unknown character: %s"), printable_version (c
));
922 /*-------------------------------------------------------------------.
923 | Assuming that a `{' has just been seen, copy everything up to the |
924 | matching `}' into the actions file. STACK_OFFSET is the number of |
925 | values in the current rule so far, which says where to find `$0' |
926 | with respect to the top of the stack. |
927 `-------------------------------------------------------------------*/
930 copy_action (symbol_list
* rule
, int stack_offset
)
937 /* offset is always 0 if parser has already popped the stack pointer */
941 fprintf (faction
, "\ncase %d:\n", nrules
);
943 fprintf (faction
, "#line %d \"%s\"\n", lineno
, infile
);
967 copy_string (finput
, faction
, c
);
973 if (c
!= '*' && c
!= '/')
975 copy_comment (finput
, faction
, c
);
984 char *cp
= token_buffer
;
986 while ((c
= getc (finput
)) != '>' && c
> 0)
988 if (cp
== token_buffer
+ maxtoken
)
989 cp
= grow_token_buffer (cp
);
994 type_name
= token_buffer
;
995 value_components_used
= 1;
1001 fprintf (faction
, "yyval");
1003 type_name
= get_type_name (0, rule
);
1005 fprintf (faction
, ".%s", type_name
);
1006 if (!type_name
&& typed
)
1007 complain (_("$$ of `%s' has no declared type"),
1010 else if (isdigit (c
) || c
== '-')
1013 n
= read_signed_integer (finput
);
1016 if (!type_name
&& n
> 0)
1017 type_name
= get_type_name (n
, rule
);
1019 fprintf (faction
, "yyvsp[%d]", n
- stack_offset
);
1021 fprintf (faction
, ".%s", type_name
);
1022 if (!type_name
&& typed
)
1023 complain (_("$%d of `%s' has no declared type"),
1028 complain (_("$%s is invalid"), printable_version (c
));
1033 copy_at (finput
, faction
, stack_offset
);
1037 fatal (_("unmatched %s"), "`{'");
1046 /* above loop exits when c is '}' */
1055 fprintf (faction
, ";\n break;}");
1058 /*-------------------------------------------------------------------.
1059 | After `%guard' is seen in the input file, copy the actual guard |
1060 | into the guards file. If the guard is followed by an action, copy |
1061 | that into the actions file. STACK_OFFSET is the number of values |
1062 | in the current rule so far, which says where to find `$0' with |
1063 | respect to the top of the stack, for the simple parser in which |
1064 | the stack is not popped until after the guard is run. |
1065 `-------------------------------------------------------------------*/
1068 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 fprintf (fguard
, "\ncase %d:\n", nrules
);
1082 fprintf (fguard
, "#line %d \"%s\"\n", lineno
, infile
);
1088 while (brace_flag
? (count
> 0) : (c
!= ';'))
1109 complain (_("unmatched %s"), "`}'");
1110 c
= getc (finput
); /* skip it */
1116 copy_string (finput
, fguard
, c
);
1122 if (c
!= '*' && c
!= '/')
1124 copy_comment (finput
, fguard
, c
);
1133 char *cp
= token_buffer
;
1135 while ((c
= getc (finput
)) != '>' && c
> 0)
1137 if (cp
== token_buffer
+ maxtoken
)
1138 cp
= grow_token_buffer (cp
);
1143 type_name
= token_buffer
;
1150 fprintf (fguard
, "yyval");
1152 type_name
= rule
->sym
->type_name
;
1154 fprintf (fguard
, ".%s", type_name
);
1155 if (!type_name
&& typed
)
1156 complain (_("$$ of `%s' has no declared type"),
1159 else if (isdigit (c
) || c
== '-')
1162 n
= read_signed_integer (finput
);
1165 if (!type_name
&& n
> 0)
1166 type_name
= get_type_name (n
, rule
);
1168 fprintf (fguard
, "yyvsp[%d]", n
- stack_offset
);
1170 fprintf (fguard
, ".%s", type_name
);
1171 if (!type_name
&& typed
)
1172 complain (_("$%d of `%s' has no declared type"),
1177 complain (_("$%s is invalid"), printable_version (c
));
1181 copy_at (finput
, fguard
, stack_offset
);
1185 fatal ("%s", _("unterminated %guard clause"));
1191 if (c
!= '}' || count
!= 0)
1195 c
= skip_white_space ();
1197 fprintf (fguard
, ";\n break;}");
1199 copy_action (rule
, stack_offset
);
1202 c
= getc (finput
); /* why not skip_white_space -wjh */
1204 copy_action (rule
, stack_offset
);
1212 record_rule_line (void)
1214 /* Record each rule's source line number in rline table. */
1216 if (nrules
>= rline_allocated
)
1218 rline_allocated
= nrules
* 2;
1219 rline
= (short *) xrealloc ((char *) rline
,
1220 rline_allocated
* sizeof (short));
1222 rline
[nrules
] = lineno
;
1226 /*-------------------------------------------------------------------.
1227 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1228 | with the user's names. |
1229 `-------------------------------------------------------------------*/
1236 sprintf (token_buffer
, "@%d", ++gensym_count
);
1237 sym
= getsym (token_buffer
);
1238 sym
->class = SNTERM
;
1239 sym
->value
= nvars
++;
1244 /*------------------------------------------------------------------.
1245 | read in a %type declaration and record its information for |
1246 | get_type_name to access. This is unused. It is only called from |
1247 | the #if 0 part of readgram |
1248 `------------------------------------------------------------------*/
1261 complain (_("invalid %s declaration"), "%type");
1265 k
= strlen (token_buffer
);
1266 name
= NEW2 (k
+ 1, char);
1267 strcpy (name
, token_buffer
);
1282 if (symval
->type_name
== NULL
)
1283 symval
->type_name
= name
;
1284 else if (strcmp (name
, symval
->type_name
) != 0)
1285 complain (_("type redeclaration for %s"), symval
->tag
);
1297 /*------------------------------------------------------------------.
1298 | Parse the input grammar into a one symbol_list structure. Each |
1299 | rule is represented by a sequence of symbols: the left hand side |
1300 | followed by the contents of the right hand side, followed by a |
1301 | null pointer instead of a symbol to terminate the rule. The next |
1302 | symbol is the lhs of the following rule. |
1304 | All guards and actions are copied out to the appropriate files, |
1305 | labelled by the rule number they apply to. |
1306 `------------------------------------------------------------------*/
1317 symbol_list
*crule
; /* points to first symbol_list of current rule. */
1318 /* its symbol is the lhs of the rule. */
1319 symbol_list
*crule1
; /* points to the symbol_list preceding crule. */
1325 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1327 if (t
== IDENTIFIER
|| t
== BAR
)
1330 int rulelength
= 0; /* number of symbols in rhs of this rule so far */
1331 int xactions
= 0; /* JF for error checking */
1332 bucket
*first_rhs
= 0;
1334 if (t
== IDENTIFIER
)
1347 complain (_("ill-formed rule: initial symbol not followed by colon"));
1352 if (nrules
== 0 && t
== BAR
)
1354 complain (_("grammar starts with vertical bar"));
1355 lhs
= symval
; /* BOGUS: use a random symval */
1357 /* start a new rule and record its lhs. */
1362 record_rule_line ();
1364 p
= NEW (symbol_list
);
1376 /* mark the rule's lhs as a nonterminal if not already so. */
1378 if (lhs
->class == SUNKNOWN
)
1380 lhs
->class = SNTERM
;
1384 else if (lhs
->class == STOKEN
)
1385 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1387 /* read the rhs of the rule. */
1395 crule
->ruleprec
= symval
;
1399 if (!(t
== IDENTIFIER
|| t
== LEFT_CURLY
))
1402 /* If next token is an identifier, see if a colon follows it.
1403 If one does, exit this rule now. */
1404 if (t
== IDENTIFIER
)
1416 if (!first_rhs
) /* JF */
1418 /* Not followed by colon =>
1419 process as part of this rule's rhs. */
1422 /* If we just passed an action, that action was in the middle
1423 of a rule, so make a dummy rule to reduce it to a
1429 /* Since the action was written out with this rule's */
1430 /* number, we must give the new rule this number */
1431 /* by inserting the new rule before it. */
1433 /* Make a dummy nonterminal, a gensym. */
1436 /* Make a new rule, whose body is empty,
1437 before the current one, so that the action
1438 just read can belong to it. */
1441 record_rule_line ();
1442 p
= NEW (symbol_list
);
1448 crule1
= NEW (symbol_list
);
1450 crule1
->next
= crule
;
1452 /* insert the dummy generated by that rule into this rule. */
1454 p
= NEW (symbol_list
);
1462 if (t
== IDENTIFIER
)
1465 p
= NEW (symbol_list
);
1470 else /* handle an action. */
1472 copy_action (crule
, rulelength
);
1474 xactions
++; /* JF */
1477 } /* end of read rhs of rule */
1479 /* Put an empty link in the list to mark the end of this rule */
1480 p
= NEW (symbol_list
);
1486 complain (_("two @prec's in a row"));
1488 crule
->ruleprec
= symval
;
1493 if (!semantic_parser
)
1496 ("%guard present but %semantic_parser not specified"));
1498 copy_guard (crule
, rulelength
);
1501 else if (t
== LEFT_CURLY
)
1503 /* This case never occurs -wjh */
1505 complain (_("two actions at end of one rule"));
1506 copy_action (crule
, rulelength
);
1508 xactions
++; /* -wjh */
1511 /* If $$ is being set in default way, report if any type
1514 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1516 if (lhs
->type_name
== 0
1517 || first_rhs
->type_name
== 0
1518 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1519 complain (_("type clash (`%s' `%s') on default action"),
1520 lhs
->type_name
? lhs
->type_name
: "",
1521 first_rhs
->type_name
? first_rhs
->type_name
: "");
1523 /* Warn if there is no default for $$ but we need one. */
1524 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1525 complain (_("empty rule for typed nonterminal, and no action"));
1530 /* these things can appear as alternatives to rules. */
1532 a) none of the documentation allows them
1533 b) most of them scan forward until finding a next %
1534 thus they may swallow lots of intervening rules
1536 else if (t
== TOKEN
)
1538 parse_token_decl (STOKEN
, SNTERM
);
1541 else if (t
== NTERM
)
1543 parse_token_decl (SNTERM
, STOKEN
);
1550 else if (t
== UNION
)
1552 parse_union_decl ();
1555 else if (t
== EXPECT
)
1557 parse_expect_decl ();
1560 else if (t
== START
)
1562 parse_start_decl ();
1569 complain (_("invalid input: %s"), token_buffer
);
1574 /* grammar has been read. Do some checking */
1576 if (nsyms
> MAXSHORT
)
1577 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1580 fatal (_("no rules in the input grammar"));
1582 if (typed
== 0 /* JF put out same default YYSTYPE as YACC does */
1583 && !value_components_used
)
1585 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1586 but it seems better to be consistent.
1587 Most programs should declare their own type anyway. */
1588 fprintf (fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1590 fprintf (fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1593 /* Report any undefined symbols and consider them nonterminals. */
1595 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1596 if (bp
->class == SUNKNOWN
)
1599 ("symbol %s is used, but is not defined as a token and has no rules"),
1602 bp
->value
= nvars
++;
1605 ntokens
= nsyms
- nvars
;
1608 /*--------------------------------------------------------------.
1609 | For named tokens, but not literal ones, define the name. The |
1610 | value is the user token number. |
1611 `--------------------------------------------------------------*/
1614 output_token_defines (FILE *file
)
1620 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1622 symbol
= bp
->tag
; /* get symbol */
1624 if (bp
->value
>= ntokens
)
1626 if (bp
->user_token_number
== SALIAS
)
1628 if ('\'' == *symbol
)
1629 continue; /* skip literal character */
1631 continue; /* skip error token */
1632 if ('\"' == *symbol
)
1634 /* use literal string only if given a symbol with an alias */
1636 symbol
= bp
->alias
->tag
;
1641 /* Don't #define nonliteral tokens whose names contain periods. */
1643 while ((c
= *cp
++) && c
!= '.');
1647 fprintf (file
, "#define\t%s\t%d\n", symbol
,
1648 ((translations
&& !rawtoknumflag
)
1649 ? bp
->user_token_number
: bp
->value
));
1650 if (semantic_parser
)
1651 fprintf (file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1658 /*------------------------------------------------------------------.
1659 | Assign symbol numbers, and write definition of token names into |
1660 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1662 `------------------------------------------------------------------*/
1670 int last_user_token_number
;
1671 static char DOLLAR
[] = "$";
1673 /* int lossage = 0; JF set but not used */
1675 tags
= NEW2 (nsyms
+ 1, char *);
1677 user_toknums
= NEW2 (nsyms
+ 1, short);
1678 user_toknums
[0] = 0;
1680 sprec
= NEW2 (nsyms
, short);
1681 sassoc
= NEW2 (nsyms
, short);
1683 max_user_token_number
= 256;
1684 last_user_token_number
= 256;
1686 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1688 if (bp
->class == SNTERM
)
1690 bp
->value
+= ntokens
;
1694 /* this symbol and its alias are a single token defn.
1695 allocate a tokno, and assign to both check agreement of
1696 ->prec and ->assoc fields and make both the same */
1698 bp
->value
= bp
->alias
->value
= tokno
++;
1700 if (bp
->prec
!= bp
->alias
->prec
)
1702 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1703 && bp
->user_token_number
== SALIAS
)
1704 complain (_("conflicting precedences for %s and %s"),
1705 bp
->tag
, bp
->alias
->tag
);
1707 bp
->alias
->prec
= bp
->prec
;
1709 bp
->prec
= bp
->alias
->prec
;
1712 if (bp
->assoc
!= bp
->alias
->assoc
)
1714 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1715 && bp
->user_token_number
== SALIAS
)
1716 complain (_("conflicting assoc values for %s and %s"),
1717 bp
->tag
, bp
->alias
->tag
);
1719 bp
->alias
->assoc
= bp
->assoc
;
1721 bp
->assoc
= bp
->alias
->assoc
;
1724 if (bp
->user_token_number
== SALIAS
)
1725 continue; /* do not do processing below for SALIASs */
1728 else /* bp->class == STOKEN */
1730 bp
->value
= tokno
++;
1733 if (bp
->class == STOKEN
)
1735 if (translations
&& !(bp
->user_token_number
))
1736 bp
->user_token_number
= ++last_user_token_number
;
1737 if (bp
->user_token_number
> max_user_token_number
)
1738 max_user_token_number
= bp
->user_token_number
;
1741 tags
[bp
->value
] = bp
->tag
;
1742 user_toknums
[bp
->value
] = bp
->user_token_number
;
1743 sprec
[bp
->value
] = bp
->prec
;
1744 sassoc
[bp
->value
] = bp
->assoc
;
1752 token_translations
= NEW2 (max_user_token_number
+ 1, short);
1754 /* initialize all entries for literal tokens to 2, the internal
1755 token number for $undefined., which represents all invalid
1757 for (j
= 0; j
<= max_user_token_number
; j
++)
1758 token_translations
[j
] = 2;
1760 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1762 if (bp
->value
>= ntokens
)
1763 continue; /* non-terminal */
1764 if (bp
->user_token_number
== SALIAS
)
1766 if (token_translations
[bp
->user_token_number
] != 2)
1767 complain (_("tokens %s and %s both assigned number %d"),
1768 tags
[token_translations
[bp
->user_token_number
]],
1769 bp
->tag
, bp
->user_token_number
);
1770 token_translations
[bp
->user_token_number
] = bp
->value
;
1774 error_token_number
= errtoken
->value
;
1777 output_token_defines (ftable
);
1779 if (startval
->class == SUNKNOWN
)
1780 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1781 else if (startval
->class == STOKEN
)
1782 fatal (_("the start symbol %s is a token"), startval
->tag
);
1784 start_symbol
= startval
->value
;
1788 output_token_defines (fdefines
);
1792 if (spec_name_prefix
)
1793 fprintf (fdefines
, "\nextern YYSTYPE %slval;\n",
1796 fprintf (fdefines
, "\nextern YYSTYPE yylval;\n");
1799 if (semantic_parser
)
1800 for (i
= ntokens
; i
< nsyms
; i
++)
1802 /* don't make these for dummy nonterminals made by gensym. */
1803 if (*tags
[i
] != '@')
1804 fprintf (fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1807 /* `fdefines' is now a temporary file, so we need to copy its
1808 contents in `done', so we can't close it here. */
1816 /*---------------------------------------------------------------.
1817 | Convert the rules into the representation using RRHS, RLHS and |
1819 `---------------------------------------------------------------*/
1830 ritem
= NEW2 (nitems
+ 1, short);
1831 rlhs
= NEW2 (nrules
, short) - 1;
1832 rrhs
= NEW2 (nrules
, short) - 1;
1833 rprec
= NEW2 (nrules
, short) - 1;
1834 rprecsym
= NEW2 (nrules
, short) - 1;
1835 rassoc
= NEW2 (nrules
, short) - 1;
1843 rlhs
[ruleno
] = p
->sym
->value
;
1844 rrhs
[ruleno
] = itemno
;
1845 ruleprec
= p
->ruleprec
;
1850 ritem
[itemno
++] = p
->sym
->value
;
1851 /* A rule gets by default the precedence and associativity
1852 of the last token in it. */
1853 if (p
->sym
->class == STOKEN
)
1855 rprec
[ruleno
] = p
->sym
->prec
;
1856 rassoc
[ruleno
] = p
->sym
->assoc
;
1862 /* If this rule has a %prec,
1863 the specified symbol's precedence replaces the default. */
1866 rprec
[ruleno
] = ruleprec
->prec
;
1867 rassoc
[ruleno
] = ruleprec
->assoc
;
1868 rprecsym
[ruleno
] = ruleprec
->value
;
1871 ritem
[itemno
++] = -ruleno
;
1881 /*-------------------------------------------------------------------.
1882 | Read in the grammar specification and record it in the format |
1883 | described in gram.h. All guards are copied into the FGUARD file |
1884 | and all actions into FACTION, in each case forming the body of a C |
1885 | function (YYGUARD or YYACTION) which contains a switch statement |
1886 | to decide which guard or action to execute. |
1887 `-------------------------------------------------------------------*/
1893 startval
= NULL
; /* start symbol not specified yet. */
1896 /* initially assume token number translation not needed. */
1899 /* Nowadays translations is always set to 1, since we give `error' a
1900 user-token-number to satisfy the Posix demand for YYERRCODE==256.
1908 rline_allocated
= 10;
1909 rline
= NEW2 (rline_allocated
, short);
1916 semantic_parser
= 0;
1925 /* Initialize the symbol table. */
1927 /* Construct the error token */
1928 errtoken
= getsym ("error");
1929 errtoken
->class = STOKEN
;
1930 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1931 /* Construct a token that represents all undefined literal tokens.
1932 It is always token number 2. */
1933 undeftoken
= getsym ("$undefined.");
1934 undeftoken
->class = STOKEN
;
1935 undeftoken
->user_token_number
= 2;
1937 /* Read the declaration section. Copy %{ ... %} groups to FTABLE
1938 and FDEFINES file. Also notice any %token, %left, etc. found
1940 putc ('\n', ftable
);
1942 /* %s, made from %s\n\
1943 by GNU bison %s. */\n\
1944 \n", noparserflag
? "Bison-generated parse tables" : "A Bison parser", infile
, VERSION
);
1946 fputs ("#define YYBISON 1 /* Identify Bison output. */\n\n", ftable
);
1947 read_declarations ();
1948 /* Start writing the guard and action files, if they are needed. */
1950 /* Read in the grammar, build grammar in list form. Write out
1951 guards and actions. */
1953 /* Now we know whether we need the line-number stack. If we do,
1954 write its type into the .tab.h file. */
1956 reader_output_yylsp (fdefines
);
1957 /* Write closing delimiters for actions and guards. */
1960 fputs ("#define YYLSP_NEEDED\n\n", ftable
);
1961 /* Assign the symbols their symbol numbers. Write #defines for the
1962 token symbols into FDEFINES if requested. */
1964 /* Convert the grammar into the format described in gram.h. */
1966 /* Free the symbol table data structure since symbols are now all
1967 referred to by symbol number. */
1972 reader_output_yylsp (FILE *f
)
1975 fprintf (f
, LTYPESTR
);