]>
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));
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 | Dump the string from FINPUT to FOUTPUT. MATCH is the delimiter of |
123 | the string (either ' or "). |
124 `-------------------------------------------------------------------*/
127 copy_string (FILE *fin
, FILE *fout
, int match
)
137 fatal (_("unterminated string at end of file"));
140 complain (_("unterminated string"));
142 c
= match
; /* invent terminator */
152 fatal (_("unterminated string at end of file"));
165 /*---------------------------------------------------------------.
166 | Dump the comment from IN to OUT1 and OUT2. C is either `*' or |
167 | `/', depending upon the type of comments used. OUT2 might be |
169 `---------------------------------------------------------------*/
172 copy_comment2 (FILE *in
, FILE *out1
, FILE *out2
, int c
)
177 cplus_comment
= (c
== '/');
186 if (!cplus_comment
&& c
== '*')
216 fatal (_("unterminated comment"));
228 /*------------------------------------------------------------.
229 | Dump the comment from FIN to FOUT. C is either `*' or `/', |
230 | depending upon the type of comments used. |
231 `------------------------------------------------------------*/
234 copy_comment (FILE *fin
, FILE *fout
, int c
)
236 copy_comment2 (fin
, fout
, NULL
, c
);
240 /*-----------------------------------------------------------------.
241 | FIN is pointing to a location (i.e., a `@'). Output to FOUT a |
242 | reference to this location. STACK_OFFSET is the number of values |
243 | in the current rule so far, which says where to find `$0' with |
244 | respect to the top of the stack. |
245 `-----------------------------------------------------------------*/
248 copy_at (FILE *fin
, FILE *fout
, int stack_offset
)
255 fprintf (fout
, "yyloc");
258 else if (isdigit (c
) || c
== '-')
263 n
= read_signed_integer (fin
);
265 fprintf (fout
, "yylsp[%d]", n
- stack_offset
);
269 complain (_("@%s is invalid"), printable_version (c
));
272 /*-------------------------------------------------------------------.
273 | Copy the contents of a `%{ ... %}' into the definitions file. The |
274 | `%{' has already been read. Return after reading the `%}'. |
275 `-------------------------------------------------------------------*/
278 copy_definition (void)
281 /* -1 while reading a character if prev char was %. */
285 fprintf (fattrs
, "#line %d \"%s\"\n", lineno
, infile
);
306 copy_string (finput
, fattrs
, c
);
312 if (c
!= '*' && c
!= '/')
314 copy_comment (finput
, fattrs
, c
);
318 fatal ("%s", _("unterminated `%{' definition"));
339 /*-------------------------------------------------------------------.
340 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
341 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
343 `-------------------------------------------------------------------*/
346 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
350 struct bucket
*symbol
= NULL
; /* pts to symbol being defined */
354 int tmp_char
= ungetc (skip_white_space (), finput
);
359 fatal (_("Premature EOF after %s"), token_buffer
);
367 if (token
== TYPENAME
)
369 typename
= xstrdup (token_buffer
);
370 value_components_used
= 1;
373 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"' && symbol
)
376 symval
->class = token_sym
;
377 symval
->type_name
= typename
;
378 symval
->user_token_number
= symbol
->user_token_number
;
379 symbol
->user_token_number
= SALIAS
;
381 symval
->alias
= symbol
;
382 symbol
->alias
= symval
;
385 nsyms
--; /* symbol and symval combined are only one symbol */
387 else if (token
== IDENTIFIER
)
389 int oldclass
= symval
->class;
392 if (symbol
->class == what_is_not
)
393 complain (_("symbol %s redefined"), symbol
->tag
);
394 symbol
->class = what_is
;
395 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
396 symbol
->value
= nvars
++;
400 if (symbol
->type_name
== NULL
)
401 symbol
->type_name
= typename
;
402 else if (strcmp (typename
, symbol
->type_name
) != 0)
403 complain (_("type redeclaration for %s"), symbol
->tag
);
406 else if (symbol
&& token
== NUMBER
)
408 symbol
->user_token_number
= numval
;
413 complain (_("`%s' is invalid in %s"),
414 token_buffer
, (what_is
== token_sym
) ? "%token" : "%nterm");
422 /*------------------------------.
423 | Parse what comes after %start |
424 `------------------------------*/
427 parse_start_decl (void)
430 complain (_("multiple %s declarations"), "%start");
431 if (lex () != IDENTIFIER
)
432 complain (_("invalid %s declaration"), "%start");
442 /*--------------------------------------------------------------.
443 | Get the data type (alternative in the union) of the value for |
444 | symbol n in rule rule. |
445 `--------------------------------------------------------------*/
448 get_type_name (int n
, symbol_list
* rule
)
455 complain (_("invalid $ value"));
465 if (rp
== NULL
|| rp
->sym
== NULL
)
467 complain (_("invalid $ value"));
473 return rp
->sym
->type_name
;
477 /*-----------------------------------------------------------.
478 | read in a %type declaration and record its information for |
479 | get_type_name to access |
480 `-----------------------------------------------------------*/
483 parse_type_decl (void)
487 if (lex () != TYPENAME
)
489 complain ("%s", _("%type declaration has no <typename>"));
494 name
= xstrdup (token_buffer
);
499 int tmp_char
= ungetc (skip_white_space (), finput
);
504 fatal (_("Premature EOF after %s"), token_buffer
);
516 if (symval
->type_name
== NULL
)
517 symval
->type_name
= name
;
518 else if (strcmp (name
, symval
->type_name
) != 0)
519 complain (_("type redeclaration for %s"), symval
->tag
);
524 complain (_("invalid %%type declaration due to item: %s"),
533 /*----------------------------------------------------------------.
534 | Read in a %left, %right or %nonassoc declaration and record its |
536 `----------------------------------------------------------------*/
539 parse_assoc_decl (associativity assoc
)
544 lastprec
++; /* Assign a new precedence level, never 0. */
549 int tmp_char
= ungetc (skip_white_space (), finput
);
554 fatal (_("Premature EOF after %s"), token_buffer
);
561 name
= xstrdup (token_buffer
);
568 if (symval
->prec
!= 0)
569 complain (_("redefining precedence of %s"), symval
->tag
);
570 symval
->prec
= lastprec
;
571 symval
->assoc
= assoc
;
572 if (symval
->class == nterm_sym
)
573 complain (_("symbol %s redefined"), symval
->tag
);
574 symval
->class = token_sym
;
576 { /* record the type, if one is specified */
577 if (symval
->type_name
== NULL
)
578 symval
->type_name
= name
;
579 else if (strcmp (name
, symval
->type_name
) != 0)
580 complain (_("type redeclaration for %s"), symval
->tag
);
585 if (prev
== IDENTIFIER
)
587 symval
->user_token_number
= numval
;
593 ("invalid text (%s) - number should be after identifier"),
603 complain (_("unexpected item: %s"), token_buffer
);
614 /*-------------------------------------------------------------------.
615 | Copy the union declaration into fattrs (and fdefines), where it is |
616 | made into the definition of YYSTYPE, the type of elements of the |
617 | parser value stack. |
618 `-------------------------------------------------------------------*/
621 parse_union_decl (void)
627 complain (_("multiple %s declarations"), "%union");
632 fprintf (fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
634 fprintf (fattrs
, "\n");
636 fprintf (fattrs
, "typedef union");
638 fprintf (fdefines
, "typedef union");
656 if (c
!= '*' && c
!= '/')
658 copy_comment2 (finput
, fattrs
, fdefines
, c
);
668 complain (_("unmatched %s"), "`}'");
672 fprintf (fattrs
, " YYSTYPE;\n");
674 fprintf (fdefines
, " YYSTYPE;\n");
675 /* JF don't choke on trailing semi */
676 c
= skip_white_space ();
688 /*-------------------------------------------------------.
689 | Parse the declaration %expect N which says to expect N |
690 | shift-reduce conflicts. |
691 `-------------------------------------------------------*/
694 parse_expect_decl (void)
701 while (c
== ' ' || c
== '\t')
705 while (c
>= '0' && c
<= '9')
715 if (count
<= 0 || count
> 10)
716 complain ("%s", _("argument of %expect is not an integer"));
717 expected_conflicts
= atoi (buffer
);
721 /*-------------------------------------------------------------------.
722 | Parse what comes after %thong. the full syntax is |
724 | %thong <type> token number literal |
726 | the <type> or number may be omitted. The number specifies the |
727 | user_token_number. |
729 | Two symbols are entered in the table, one for the token symbol and |
730 | one for the literal. Both are given the <type>, if any, from the |
731 | declaration. The ->user_token_number of the first is SALIAS and |
732 | the ->user_token_number of the second is set to the number, if |
733 | any, from the declaration. The two symbols are linked via |
734 | pointers in their ->alias fields. |
736 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
737 | only the literal string is retained it is the literal string that |
738 | is output to yytname |
739 `-------------------------------------------------------------------*/
742 parse_thong_decl (void)
745 struct bucket
*symbol
;
750 token
= lex (); /* fetch typename or first token */
751 if (token
== TYPENAME
)
753 typename
= xstrdup (token_buffer
);
754 value_components_used
= 1;
755 token
= lex (); /* fetch first token */
758 /* process first token */
760 if (token
!= IDENTIFIER
)
762 complain (_("unrecognized item %s, expected an identifier"),
767 symval
->class = token_sym
;
768 symval
->type_name
= typename
;
769 symval
->user_token_number
= SALIAS
;
772 token
= lex (); /* get number or literal string */
777 token
= lex (); /* okay, did number, now get literal */
782 /* process literal string token */
784 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
786 complain (_("expected string constant instead of %s"), token_buffer
);
790 symval
->class = token_sym
;
791 symval
->type_name
= typename
;
792 symval
->user_token_number
= usrtoknum
;
794 symval
->alias
= symbol
;
795 symbol
->alias
= symval
;
797 nsyms
--; /* symbol and symval combined are only one symbol */
801 /*----------------------------------------------------------------.
802 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
803 | any `%' declarations, and copy the contents of any `%{ ... %}' |
804 | groups to fattrs. |
805 `----------------------------------------------------------------*/
808 read_declarations (void)
815 c
= skip_white_space ();
819 tok
= parse_percent_token ();
826 case PERCENT_LEFT_CURLY
:
831 parse_token_decl (token_sym
, nterm_sym
);
835 parse_token_decl (nterm_sym
, token_sym
);
851 parse_expect_decl ();
858 parse_assoc_decl (left_assoc
);
862 parse_assoc_decl (right_assoc
);
866 parse_assoc_decl (non_assoc
);
869 case SEMANTIC_PARSER
:
870 if (semantic_parser
== 0)
885 complain (_("unrecognized: %s"), token_buffer
);
890 fatal (_("no input grammar"));
893 complain (_("unknown character: %s"), printable_version (c
));
899 /*-------------------------------------------------------------------.
900 | Assuming that a `{' has just been seen, copy everything up to the |
901 | matching `}' into the actions file. STACK_OFFSET is the number of |
902 | values in the current rule so far, which says where to find `$0' |
903 | with respect to the top of the stack. |
904 `-------------------------------------------------------------------*/
907 copy_action (symbol_list
* rule
, int stack_offset
)
914 /* offset is always 0 if parser has already popped the stack pointer */
918 fprintf (faction
, "\ncase %d:\n", nrules
);
920 fprintf (faction
, "#line %d \"%s\"\n", lineno
, infile
);
944 copy_string (finput
, faction
, c
);
950 if (c
!= '*' && c
!= '/')
952 copy_comment (finput
, faction
, c
);
961 char *cp
= token_buffer
;
963 while ((c
= getc (finput
)) != '>' && c
> 0)
965 if (cp
== token_buffer
+ maxtoken
)
966 cp
= grow_token_buffer (cp
);
971 type_name
= token_buffer
;
972 value_components_used
= 1;
978 fprintf (faction
, "yyval");
980 type_name
= get_type_name (0, rule
);
982 fprintf (faction
, ".%s", type_name
);
983 if (!type_name
&& typed
)
984 complain (_("$$ of `%s' has no declared type"),
987 else if (isdigit (c
) || c
== '-')
990 n
= read_signed_integer (finput
);
993 if (!type_name
&& n
> 0)
994 type_name
= get_type_name (n
, rule
);
996 fprintf (faction
, "yyvsp[%d]", n
- stack_offset
);
998 fprintf (faction
, ".%s", type_name
);
999 if (!type_name
&& typed
)
1000 complain (_("$%d of `%s' has no declared type"),
1005 complain (_("$%s is invalid"), printable_version (c
));
1010 copy_at (finput
, faction
, stack_offset
);
1014 fatal (_("unmatched %s"), "`{'");
1023 /* above loop exits when c is '}' */
1032 fprintf (faction
, ";\n break;}");
1035 /*-------------------------------------------------------------------.
1036 | After `%guard' is seen in the input file, copy the actual guard |
1037 | into the guards file. If the guard is followed by an action, copy |
1038 | that into the actions file. STACK_OFFSET is the number of values |
1039 | in the current rule so far, which says where to find `$0' with |
1040 | respect to the top of the stack, for the simple parser in which |
1041 | the stack is not popped until after the guard is run. |
1042 `-------------------------------------------------------------------*/
1045 copy_guard (symbol_list
* rule
, int stack_offset
)
1053 /* offset is always 0 if parser has already popped the stack pointer */
1054 if (semantic_parser
)
1057 fprintf (fguard
, "\ncase %d:\n", nrules
);
1059 fprintf (fguard
, "#line %d \"%s\"\n", lineno
, infile
);
1065 while (brace_flag
? (count
> 0) : (c
!= ';'))
1086 complain (_("unmatched %s"), "`}'");
1087 c
= getc (finput
); /* skip it */
1093 copy_string (finput
, fguard
, c
);
1099 if (c
!= '*' && c
!= '/')
1101 copy_comment (finput
, fguard
, c
);
1110 char *cp
= token_buffer
;
1112 while ((c
= getc (finput
)) != '>' && c
> 0)
1114 if (cp
== token_buffer
+ maxtoken
)
1115 cp
= grow_token_buffer (cp
);
1120 type_name
= token_buffer
;
1127 fprintf (fguard
, "yyval");
1129 type_name
= rule
->sym
->type_name
;
1131 fprintf (fguard
, ".%s", type_name
);
1132 if (!type_name
&& typed
)
1133 complain (_("$$ of `%s' has no declared type"),
1136 else if (isdigit (c
) || c
== '-')
1139 n
= read_signed_integer (finput
);
1142 if (!type_name
&& n
> 0)
1143 type_name
= get_type_name (n
, rule
);
1145 fprintf (fguard
, "yyvsp[%d]", n
- stack_offset
);
1147 fprintf (fguard
, ".%s", type_name
);
1148 if (!type_name
&& typed
)
1149 complain (_("$%d of `%s' has no declared type"),
1154 complain (_("$%s is invalid"), printable_version (c
));
1158 copy_at (finput
, fguard
, stack_offset
);
1162 fatal ("%s", _("unterminated %guard clause"));
1168 if (c
!= '}' || count
!= 0)
1172 c
= skip_white_space ();
1174 fprintf (fguard
, ";\n break;}");
1176 copy_action (rule
, stack_offset
);
1179 c
= getc (finput
); /* why not skip_white_space -wjh */
1181 copy_action (rule
, stack_offset
);
1189 record_rule_line (void)
1191 /* Record each rule's source line number in rline table. */
1193 if (nrules
>= rline_allocated
)
1195 rline_allocated
= nrules
* 2;
1196 rline
= XREALLOC (rline
, short, rline_allocated
);
1198 rline
[nrules
] = lineno
;
1202 /*-------------------------------------------------------------------.
1203 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1204 | with the user's names. |
1205 `-------------------------------------------------------------------*/
1212 sprintf (token_buffer
, "@%d", ++gensym_count
);
1213 sym
= getsym (token_buffer
);
1214 sym
->class = nterm_sym
;
1215 sym
->value
= nvars
++;
1220 /*------------------------------------------------------------------.
1221 | read in a %type declaration and record its information for |
1222 | get_type_name to access. This is unused. It is only called from |
1223 | the #if 0 part of readgram |
1224 `------------------------------------------------------------------*/
1237 complain (_("invalid %s declaration"), "%type");
1241 name
= xstrdup (token_buffer
);
1256 if (symval
->type_name
== NULL
)
1257 symval
->type_name
= name
;
1258 else if (strcmp (name
, symval
->type_name
) != 0)
1259 complain (_("type redeclaration for %s"), symval
->tag
);
1271 /*------------------------------------------------------------------.
1272 | Parse the input grammar into a one symbol_list structure. Each |
1273 | rule is represented by a sequence of symbols: the left hand side |
1274 | followed by the contents of the right hand side, followed by a |
1275 | null pointer instead of a symbol to terminate the rule. The next |
1276 | symbol is the lhs of the following rule. |
1278 | All guards and actions are copied out to the appropriate files, |
1279 | labelled by the rule number they apply to. |
1280 `------------------------------------------------------------------*/
1291 symbol_list
*crule
; /* points to first symbol_list of current rule. */
1292 /* its symbol is the lhs of the rule. */
1293 symbol_list
*crule1
; /* points to the symbol_list preceding crule. */
1299 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1301 if (t
== IDENTIFIER
|| t
== BAR
)
1303 int action_flag
= 0;
1304 int rulelength
= 0; /* number of symbols in rhs of this rule so far */
1305 int xactions
= 0; /* JF for error checking */
1306 bucket
*first_rhs
= 0;
1308 if (t
== IDENTIFIER
)
1321 complain (_("ill-formed rule: initial symbol not followed by colon"));
1326 if (nrules
== 0 && t
== BAR
)
1328 complain (_("grammar starts with vertical bar"));
1329 lhs
= symval
; /* BOGUS: use a random symval */
1331 /* start a new rule and record its lhs. */
1336 record_rule_line ();
1338 p
= XCALLOC (symbol_list
, 1);
1350 /* mark the rule's lhs as a nonterminal if not already so. */
1352 if (lhs
->class == unknown_sym
)
1354 lhs
->class = nterm_sym
;
1358 else if (lhs
->class == token_sym
)
1359 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1361 /* read the rhs of the rule. */
1369 crule
->ruleprec
= symval
;
1373 if (!(t
== IDENTIFIER
|| t
== LEFT_CURLY
))
1376 /* If next token is an identifier, see if a colon follows it.
1377 If one does, exit this rule now. */
1378 if (t
== IDENTIFIER
)
1390 if (!first_rhs
) /* JF */
1392 /* Not followed by colon =>
1393 process as part of this rule's rhs. */
1396 /* If we just passed an action, that action was in the middle
1397 of a rule, so make a dummy rule to reduce it to a
1403 /* Since the action was written out with this rule's */
1404 /* number, we must give the new rule this number */
1405 /* by inserting the new rule before it. */
1407 /* Make a dummy nonterminal, a gensym. */
1410 /* Make a new rule, whose body is empty,
1411 before the current one, so that the action
1412 just read can belong to it. */
1415 record_rule_line ();
1416 p
= XCALLOC (symbol_list
, 1);
1422 crule1
= XCALLOC (symbol_list
, 1);
1424 crule1
->next
= crule
;
1426 /* insert the dummy generated by that rule into this rule. */
1428 p
= XCALLOC (symbol_list
, 1);
1436 if (t
== IDENTIFIER
)
1439 p
= XCALLOC (symbol_list
, 1);
1444 else /* handle an action. */
1446 copy_action (crule
, rulelength
);
1448 xactions
++; /* JF */
1451 } /* end of read rhs of rule */
1453 /* Put an empty link in the list to mark the end of this rule */
1454 p
= XCALLOC (symbol_list
, 1);
1460 complain (_("two @prec's in a row"));
1462 crule
->ruleprec
= symval
;
1467 if (!semantic_parser
)
1470 ("%guard present but %semantic_parser not specified"));
1472 copy_guard (crule
, rulelength
);
1475 else if (t
== LEFT_CURLY
)
1477 /* This case never occurs -wjh */
1479 complain (_("two actions at end of one rule"));
1480 copy_action (crule
, rulelength
);
1482 xactions
++; /* -wjh */
1485 /* If $$ is being set in default way, report if any type
1488 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1490 if (lhs
->type_name
== 0
1491 || first_rhs
->type_name
== 0
1492 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1493 complain (_("type clash (`%s' `%s') on default action"),
1494 lhs
->type_name
? lhs
->type_name
: "",
1495 first_rhs
->type_name
? first_rhs
->type_name
: "");
1497 /* Warn if there is no default for $$ but we need one. */
1498 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1499 complain (_("empty rule for typed nonterminal, and no action"));
1504 /* these things can appear as alternatives to rules. */
1506 a) none of the documentation allows them
1507 b) most of them scan forward until finding a next %
1508 thus they may swallow lots of intervening rules
1510 else if (t
== TOKEN
)
1512 parse_token_decl (token_sym
, nterm_sym
);
1515 else if (t
== NTERM
)
1517 parse_token_decl (nterm_sym
, token_sym
);
1524 else if (t
== UNION
)
1526 parse_union_decl ();
1529 else if (t
== EXPECT
)
1531 parse_expect_decl ();
1534 else if (t
== START
)
1536 parse_start_decl ();
1543 complain (_("invalid input: %s"), token_buffer
);
1548 /* grammar has been read. Do some checking */
1550 if (nsyms
> MAXSHORT
)
1551 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1554 fatal (_("no rules in the input grammar"));
1556 if (typed
== 0 /* JF put out same default YYSTYPE as YACC does */
1557 && !value_components_used
)
1559 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1560 but it seems better to be consistent.
1561 Most programs should declare their own type anyway. */
1562 fprintf (fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1564 fprintf (fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1567 /* Report any undefined symbols and consider them nonterminals. */
1569 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1570 if (bp
->class == unknown_sym
)
1573 ("symbol %s is used, but is not defined as a token and has no rules"),
1575 bp
->class = nterm_sym
;
1576 bp
->value
= nvars
++;
1579 ntokens
= nsyms
- nvars
;
1582 /*--------------------------------------------------------------.
1583 | For named tokens, but not literal ones, define the name. The |
1584 | value is the user token number. |
1585 `--------------------------------------------------------------*/
1588 output_token_defines (FILE *file
)
1594 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1596 symbol
= bp
->tag
; /* get symbol */
1598 if (bp
->value
>= ntokens
)
1600 if (bp
->user_token_number
== SALIAS
)
1602 if ('\'' == *symbol
)
1603 continue; /* skip literal character */
1605 continue; /* skip error token */
1606 if ('\"' == *symbol
)
1608 /* use literal string only if given a symbol with an alias */
1610 symbol
= bp
->alias
->tag
;
1615 /* Don't #define nonliteral tokens whose names contain periods. */
1617 while ((c
= *cp
++) && c
!= '.');
1621 fprintf (file
, "#define\t%s\t%d\n", symbol
,
1622 ((translations
&& !raw_flag
)
1623 ? bp
->user_token_number
: bp
->value
));
1624 if (semantic_parser
)
1625 fprintf (file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1632 /*------------------------------------------------------------------.
1633 | Assign symbol numbers, and write definition of token names into |
1634 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1636 `------------------------------------------------------------------*/
1644 int last_user_token_number
;
1645 static char DOLLAR
[] = "$";
1647 /* int lossage = 0; JF set but not used */
1649 tags
= XCALLOC (char *, nsyms
+ 1);
1651 user_toknums
= XCALLOC (short, nsyms
+ 1);
1652 user_toknums
[0] = 0;
1654 sprec
= XCALLOC (short, nsyms
);
1655 sassoc
= XCALLOC (short, nsyms
);
1657 max_user_token_number
= 256;
1658 last_user_token_number
= 256;
1660 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1662 if (bp
->class == nterm_sym
)
1664 bp
->value
+= ntokens
;
1668 /* this symbol and its alias are a single token defn.
1669 allocate a tokno, and assign to both check agreement of
1670 ->prec and ->assoc fields and make both the same */
1672 bp
->value
= bp
->alias
->value
= tokno
++;
1674 if (bp
->prec
!= bp
->alias
->prec
)
1676 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1677 && bp
->user_token_number
== SALIAS
)
1678 complain (_("conflicting precedences for %s and %s"),
1679 bp
->tag
, bp
->alias
->tag
);
1681 bp
->alias
->prec
= bp
->prec
;
1683 bp
->prec
= bp
->alias
->prec
;
1686 if (bp
->assoc
!= bp
->alias
->assoc
)
1688 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1689 && bp
->user_token_number
== SALIAS
)
1690 complain (_("conflicting assoc values for %s and %s"),
1691 bp
->tag
, bp
->alias
->tag
);
1693 bp
->alias
->assoc
= bp
->assoc
;
1695 bp
->assoc
= bp
->alias
->assoc
;
1698 if (bp
->user_token_number
== SALIAS
)
1699 continue; /* do not do processing below for SALIASs */
1702 else /* bp->class == token_sym */
1704 bp
->value
= tokno
++;
1707 if (bp
->class == token_sym
)
1709 if (translations
&& !(bp
->user_token_number
))
1710 bp
->user_token_number
= ++last_user_token_number
;
1711 if (bp
->user_token_number
> max_user_token_number
)
1712 max_user_token_number
= bp
->user_token_number
;
1715 tags
[bp
->value
] = bp
->tag
;
1716 user_toknums
[bp
->value
] = bp
->user_token_number
;
1717 sprec
[bp
->value
] = bp
->prec
;
1718 sassoc
[bp
->value
] = bp
->assoc
;
1726 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1728 /* initialize all entries for literal tokens to 2, the internal
1729 token number for $undefined., which represents all invalid
1731 for (j
= 0; j
<= max_user_token_number
; j
++)
1732 token_translations
[j
] = 2;
1734 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1736 if (bp
->value
>= ntokens
)
1737 continue; /* non-terminal */
1738 if (bp
->user_token_number
== SALIAS
)
1740 if (token_translations
[bp
->user_token_number
] != 2)
1741 complain (_("tokens %s and %s both assigned number %d"),
1742 tags
[token_translations
[bp
->user_token_number
]],
1743 bp
->tag
, bp
->user_token_number
);
1744 token_translations
[bp
->user_token_number
] = bp
->value
;
1748 error_token_number
= errtoken
->value
;
1750 if (!no_parser_flag
)
1751 output_token_defines (ftable
);
1753 if (startval
->class == unknown_sym
)
1754 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1755 else if (startval
->class == token_sym
)
1756 fatal (_("the start symbol %s is a token"), startval
->tag
);
1758 start_symbol
= startval
->value
;
1762 output_token_defines (fdefines
);
1766 if (spec_name_prefix
)
1767 fprintf (fdefines
, "\nextern YYSTYPE %slval;\n",
1770 fprintf (fdefines
, "\nextern YYSTYPE yylval;\n");
1773 if (semantic_parser
)
1774 for (i
= ntokens
; i
< nsyms
; i
++)
1776 /* don't make these for dummy nonterminals made by gensym. */
1777 if (*tags
[i
] != '@')
1778 fprintf (fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1781 /* `fdefines' is now a temporary file, so we need to copy its
1782 contents in `done', so we can't close it here. */
1790 /*---------------------------------------------------------------.
1791 | Convert the rules into the representation using RRHS, RLHS and |
1793 `---------------------------------------------------------------*/
1804 ritem
= XCALLOC (short, nitems
+ 1);
1805 rlhs
= XCALLOC (short, nrules
) - 1;
1806 rrhs
= XCALLOC (short, nrules
) - 1;
1807 rprec
= XCALLOC (short, nrules
) - 1;
1808 rprecsym
= XCALLOC (short, nrules
) - 1;
1809 rassoc
= XCALLOC (short, nrules
) - 1;
1817 rlhs
[ruleno
] = p
->sym
->value
;
1818 rrhs
[ruleno
] = itemno
;
1819 ruleprec
= p
->ruleprec
;
1824 ritem
[itemno
++] = p
->sym
->value
;
1825 /* A rule gets by default the precedence and associativity
1826 of the last token in it. */
1827 if (p
->sym
->class == token_sym
)
1829 rprec
[ruleno
] = p
->sym
->prec
;
1830 rassoc
[ruleno
] = p
->sym
->assoc
;
1836 /* If this rule has a %prec,
1837 the specified symbol's precedence replaces the default. */
1840 rprec
[ruleno
] = ruleprec
->prec
;
1841 rassoc
[ruleno
] = ruleprec
->assoc
;
1842 rprecsym
[ruleno
] = ruleprec
->value
;
1845 ritem
[itemno
++] = -ruleno
;
1855 /*-------------------------------------------------------------------.
1856 | Read in the grammar specification and record it in the format |
1857 | described in gram.h. All guards are copied into the FGUARD file |
1858 | and all actions into FACTION, in each case forming the body of a C |
1859 | function (YYGUARD or YYACTION) which contains a switch statement |
1860 | to decide which guard or action to execute. |
1861 `-------------------------------------------------------------------*/
1867 startval
= NULL
; /* start symbol not specified yet. */
1870 /* initially assume token number translation not needed. */
1873 /* Nowadays translations is always set to 1, since we give `error' a
1874 user-token-number to satisfy the Posix demand for YYERRCODE==256.
1882 rline_allocated
= 10;
1883 rline
= XCALLOC (short, rline_allocated
);
1890 semantic_parser
= 0;
1898 /* Initialize the symbol table. */
1900 /* Construct the error token */
1901 errtoken
= getsym ("error");
1902 errtoken
->class = token_sym
;
1903 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1904 /* Construct a token that represents all undefined literal tokens.
1905 It is always token number 2. */
1906 undeftoken
= getsym ("$undefined.");
1907 undeftoken
->class = token_sym
;
1908 undeftoken
->user_token_number
= 2;
1910 /* Read the declaration section. Copy %{ ... %} groups to FTABLE
1911 and FDEFINES file. Also notice any %token, %left, etc. found
1913 putc ('\n', ftable
);
1915 /* %s, made from %s\n\
1916 by GNU bison %s. */\n\
1917 \n", no_parser_flag
? "Bison-generated parse tables" : "A Bison parser", infile
, VERSION
);
1919 fputs ("#define YYBISON 1 /* Identify Bison output. */\n\n", ftable
);
1920 read_declarations ();
1921 /* Start writing the guard and action files, if they are needed. */
1923 /* Read in the grammar, build grammar in list form. Write out
1924 guards and actions. */
1926 /* Now we know whether we need the line-number stack. If we do,
1927 write its type into the .tab.h file. */
1929 reader_output_yylsp (fdefines
);
1930 /* Write closing delimiters for actions and guards. */
1933 fputs ("#define YYLSP_NEEDED\n\n", ftable
);
1934 /* Assign the symbols their symbol numbers. Write #defines for the
1935 token symbols into FDEFINES if requested. */
1937 /* Convert the grammar into the format described in gram.h. */
1939 /* Free the symbol table data structure since symbols are now all
1940 referred to by symbol number. */
1946 reader_output_yylsp (FILE *f
)
1952 typedef struct yyltype\n\
1963 # define YYLTYPE yyltype\n\