]>
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. */
51 #define YYLTYPE yyltype\n\
55 /* Number of slots allocated (but not necessarily used yet) in `rline' */
56 static int rline_allocated
;
58 extern bucket
*symval
;
60 extern int expected_conflicts
;
61 extern char *token_buffer
;
64 extern void tabinit
PARAMS ((void));
65 extern void free_symtab
PARAMS ((void));
66 extern char *printable_version
PARAMS ((int));
68 typedef struct symbol_list
70 struct symbol_list
*next
;
77 extern void reader
PARAMS ((void));
78 extern void reader_output_yylsp
PARAMS ((FILE *));
83 static symbol_list
*grammar
;
84 static int start_flag
;
85 static bucket
*startval
;
87 /* Nonzero if components of semantic values are used, implying
88 they must be unions. */
89 static int value_components_used
;
91 static int typed
; /* nonzero if %union has been seen. */
93 static int lastprec
; /* incremented for each %left, %right or %nonassoc seen */
95 static int gensym_count
; /* incremented for each generated symbol */
97 static bucket
*errtoken
;
98 static bucket
*undeftoken
;
100 /* Nonzero if any action or guard uses the @n construct. */
101 static int yylsp_needed
;
104 /*===================\
105 | Low level lexing. |
106 \===================*/
109 skip_to_char (int target
)
113 complain (_(" Skipping to next \\n"));
115 complain (_(" Skipping to next %c"), target
);
118 c
= skip_white_space ();
119 while (c
!= target
&& c
!= EOF
);
125 /*---------------------------------------------------------.
126 | Read a signed integer from STREAM and return its value. |
127 `---------------------------------------------------------*/
130 read_signed_integer (FILE *stream
)
132 int c
= getc (stream
);
144 n
= 10 * n
+ (c
- '0');
153 /*-------------------------------------------------------------------.
154 | Dump the string from FINPUT to FOUTPUT. MATCH is the delimiter of |
155 | the string (either ' or "). |
156 `-------------------------------------------------------------------*/
159 copy_string (FILE *fin
, FILE *fout
, int match
)
169 fatal (_("unterminated string at end of file"));
172 complain (_("unterminated string"));
174 c
= match
; /* invent terminator */
184 fatal (_("unterminated string at end of file"));
197 /*---------------------------------------------------------------.
198 | Dump the comment from IN to OUT1 and OUT2. C is either `*' or |
199 | `/', depending upon the type of comments used. OUT2 might be |
201 `---------------------------------------------------------------*/
204 copy_comment2 (FILE *in
, FILE *out1
, FILE *out2
, int c
)
209 cplus_comment
= (c
== '/');
218 if (!cplus_comment
&& c
== '*')
248 fatal (_("unterminated comment"));
260 /*------------------------------------------------------------.
261 | Dump the comment from FIN to FOUT. C is either `*' or `/', |
262 | depending upon the type of comments used. |
263 `------------------------------------------------------------*/
266 copy_comment (FILE *fin
, FILE *fout
, int c
)
268 copy_comment2 (fin
, fout
, NULL
, c
);
272 /*-----------------------------------------------------------------.
273 | FIN is pointing to a location (i.e., a `@'). Output to FOUT a |
274 | reference to this location. STACK_OFFSET is the number of values |
275 | in the current rule so far, which says where to find `$0' with |
276 | respect to the top of the stack. |
277 `-----------------------------------------------------------------*/
280 copy_at (FILE *fin
, FILE *fout
, int stack_offset
)
287 fprintf (fout
, "yyloc");
290 else if (isdigit (c
) || c
== '-')
295 n
= read_signed_integer (fin
);
297 fprintf (fout
, "yylsp[%d]", n
- stack_offset
);
301 complain (_("@%s is invalid"), printable_version (c
));
304 /*-------------------------------------------------------------------.
305 | Copy the contents of a `%{ ... %}' into the definitions file. The |
306 | `%{' has already been read. Return after reading the `%}'. |
307 `-------------------------------------------------------------------*/
310 copy_definition (void)
313 /* -1 while reading a character if prev char was %. */
317 fprintf (fattrs
, "#line %d \"%s\"\n", lineno
, infile
);
338 copy_string (finput
, fattrs
, c
);
344 if (c
!= '*' && c
!= '/')
346 copy_comment (finput
, fattrs
, c
);
350 fatal ("%s", _("unterminated `%{' definition"));
371 /*-----------------------------------------------------------------.
372 | Parse what comes after %token or %nterm. For %token, what_is is |
373 | STOKEN and what_is_not is SNTERM. For %nterm, the arguments are |
375 `-----------------------------------------------------------------*/
378 parse_token_decl (int what_is
, int what_is_not
)
382 struct bucket
*symbol
= NULL
; /* pts to symbol being defined */
387 int tmp_char
= ungetc (skip_white_space (), finput
);
392 fatal (_("Premature EOF after %s"), token_buffer
);
400 if (token
== TYPENAME
)
402 k
= strlen (token_buffer
);
403 typename
= NEW2 (k
+ 1, char);
404 strcpy (typename
, token_buffer
);
405 value_components_used
= 1;
408 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"' && symbol
)
411 symval
->class = STOKEN
;
412 symval
->type_name
= typename
;
413 symval
->user_token_number
= symbol
->user_token_number
;
414 symbol
->user_token_number
= SALIAS
;
416 symval
->alias
= symbol
;
417 symbol
->alias
= symval
;
420 nsyms
--; /* symbol and symval combined are only one symbol */
422 else if (token
== IDENTIFIER
)
424 int oldclass
= symval
->class;
427 if (symbol
->class == what_is_not
)
428 complain (_("symbol %s redefined"), symbol
->tag
);
429 symbol
->class = what_is
;
430 if (what_is
== SNTERM
&& oldclass
!= SNTERM
)
431 symbol
->value
= nvars
++;
435 if (symbol
->type_name
== NULL
)
436 symbol
->type_name
= typename
;
437 else if (strcmp (typename
, symbol
->type_name
) != 0)
438 complain (_("type redeclaration for %s"), symbol
->tag
);
441 else if (symbol
&& token
== NUMBER
)
443 symbol
->user_token_number
= numval
;
448 complain (_("`%s' is invalid in %s"),
449 token_buffer
, (what_is
== STOKEN
) ? "%token" : "%nterm");
457 /* Parse what comes after %start */
460 parse_start_decl (void)
463 complain (_("multiple %s declarations"), "%start");
464 if (lex () != IDENTIFIER
)
465 complain (_("invalid %s declaration"), "%start");
475 /*--------------------------------------------------------------.
476 | Get the data type (alternative in the union) of the value for |
477 | symbol n in rule rule. |
478 `--------------------------------------------------------------*/
481 get_type_name (int n
, symbol_list
* rule
)
488 complain (_("invalid $ value"));
498 if (rp
== NULL
|| rp
->sym
== NULL
)
500 complain (_("invalid $ value"));
506 return rp
->sym
->type_name
;
510 /*-----------------------------------------------------------.
511 | read in a %type declaration and record its information for |
512 | get_type_name to access |
513 `-----------------------------------------------------------*/
516 parse_type_decl (void)
521 if (lex () != TYPENAME
)
523 complain ("%s", _("%type declaration has no <typename>"));
528 k
= strlen (token_buffer
);
529 name
= NEW2 (k
+ 1, char);
530 strcpy (name
, token_buffer
);
535 int tmp_char
= ungetc (skip_white_space (), finput
);
540 fatal (_("Premature EOF after %s"), token_buffer
);
552 if (symval
->type_name
== NULL
)
553 symval
->type_name
= name
;
554 else if (strcmp (name
, symval
->type_name
) != 0)
555 complain (_("type redeclaration for %s"), symval
->tag
);
560 complain (_("invalid %%type declaration due to item: %s"),
569 /* read in a %left, %right or %nonassoc declaration and record its information. */
570 /* assoc is either LEFT_ASSOC, RIGHT_ASSOC or NON_ASSOC. */
573 parse_assoc_decl (int assoc
)
579 lastprec
++; /* Assign a new precedence level, never 0. */
584 int tmp_char
= ungetc (skip_white_space (), finput
);
589 fatal (_("Premature EOF after %s"), token_buffer
);
597 k
= strlen (token_buffer
);
598 name
= NEW2 (k
+ 1, char);
599 strcpy (name
, token_buffer
);
606 if (symval
->prec
!= 0)
607 complain (_("redefining precedence of %s"), symval
->tag
);
608 symval
->prec
= lastprec
;
609 symval
->assoc
= assoc
;
610 if (symval
->class == SNTERM
)
611 complain (_("symbol %s redefined"), symval
->tag
);
612 symval
->class = STOKEN
;
614 { /* record the type, if one is specified */
615 if (symval
->type_name
== NULL
)
616 symval
->type_name
= name
;
617 else if (strcmp (name
, symval
->type_name
) != 0)
618 complain (_("type redeclaration for %s"), symval
->tag
);
623 if (prev
== IDENTIFIER
)
625 symval
->user_token_number
= numval
;
631 ("invalid text (%s) - number should be after identifier"),
641 complain (_("unexpected item: %s"), token_buffer
);
652 /* copy the union declaration into fattrs (and fdefines),
653 where it is made into the
654 definition of YYSTYPE, the type of elements of the parser value stack. */
657 parse_union_decl (void)
663 complain (_("multiple %s declarations"), "%union");
668 fprintf (fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
670 fprintf (fattrs
, "\n");
672 fprintf (fattrs
, "typedef union");
674 fprintf (fdefines
, "typedef union");
692 if (c
!= '*' && c
!= '/')
694 copy_comment2 (finput
, fattrs
, fdefines
, c
);
704 complain (_("unmatched %s"), "`}'");
708 fprintf (fattrs
, " YYSTYPE;\n");
710 fprintf (fdefines
, " YYSTYPE;\n");
711 /* JF don't choke on trailing semi */
712 c
= skip_white_space ();
723 /* parse the declaration %expect N which says to expect N
724 shift-reduce conflicts. */
727 parse_expect_decl (void)
734 while (c
== ' ' || c
== '\t')
738 while (c
>= '0' && c
<= '9')
748 if (count
<= 0 || count
> 10)
749 complain ("%s", _("argument of %expect is not an integer"));
750 expected_conflicts
= atoi (buffer
);
754 /*-------------------------------------------------------------------.
755 | Parse what comes after %thong. the full syntax is |
757 | %thong <type> token number literal |
759 | the <type> or number may be omitted. The number specifies the |
760 | user_token_number. |
762 | Two symbols are entered in the table, one for the token symbol and |
763 | one for the literal. Both are given the <type>, if any, from the |
764 | declaration. The ->user_token_number of the first is SALIAS and |
765 | the ->user_token_number of the second is set to the number, if |
766 | any, from the declaration. The two symbols are linked via |
767 | pointers in their ->alias fields. |
769 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
770 | only the literal string is retained it is the literal string that |
771 | is output to yytname |
772 `-------------------------------------------------------------------*/
775 parse_thong_decl (void)
778 struct bucket
*symbol
;
783 token
= lex (); /* fetch typename or first token */
784 if (token
== TYPENAME
)
786 k
= strlen (token_buffer
);
787 typename
= NEW2 (k
+ 1, char);
788 strcpy (typename
, token_buffer
);
789 value_components_used
= 1;
790 token
= lex (); /* fetch first token */
793 /* process first token */
795 if (token
!= IDENTIFIER
)
797 complain (_("unrecognized item %s, expected an identifier"),
802 symval
->class = STOKEN
;
803 symval
->type_name
= typename
;
804 symval
->user_token_number
= SALIAS
;
807 token
= lex (); /* get number or literal string */
812 token
= lex (); /* okay, did number, now get literal */
817 /* process literal string token */
819 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
821 complain (_("expected string constant instead of %s"), token_buffer
);
825 symval
->class = STOKEN
;
826 symval
->type_name
= typename
;
827 symval
->user_token_number
= usrtoknum
;
829 symval
->alias
= symbol
;
830 symbol
->alias
= symval
;
832 nsyms
--; /* symbol and symval combined are only one symbol */
835 /*----------------------------------------------------------------.
836 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
837 | any `%' declarations, and copy the contents of any `%{ ... %}' |
838 | groups to fattrs. |
839 `----------------------------------------------------------------*/
842 read_declarations (void)
849 c
= skip_white_space ();
853 tok
= parse_percent_token ();
860 case PERCENT_LEFT_CURLY
:
865 parse_token_decl (STOKEN
, SNTERM
);
869 parse_token_decl (SNTERM
, STOKEN
);
885 parse_expect_decl ();
891 parse_assoc_decl (LEFT_ASSOC
);
895 parse_assoc_decl (RIGHT_ASSOC
);
899 parse_assoc_decl (NON_ASSOC
);
902 case SEMANTIC_PARSER
:
903 if (semantic_parser
== 0)
918 complain (_("unrecognized: %s"), token_buffer
);
923 fatal (_("no input grammar"));
926 complain (_("unknown character: %s"), printable_version (c
));
932 /*-------------------------------------------------------------------.
933 | Assuming that a `{' has just been seen, copy everything up to the |
934 | matching `}' into the actions file. STACK_OFFSET is the number of |
935 | values in the current rule so far, which says where to find `$0' |
936 | with respect to the top of the stack. |
937 `-------------------------------------------------------------------*/
940 copy_action (symbol_list
* rule
, int stack_offset
)
947 /* offset is always 0 if parser has already popped the stack pointer */
951 fprintf (faction
, "\ncase %d:\n", nrules
);
953 fprintf (faction
, "#line %d \"%s\"\n", lineno
, infile
);
977 copy_string (finput
, faction
, c
);
983 if (c
!= '*' && c
!= '/')
985 copy_comment (finput
, faction
, c
);
994 char *cp
= token_buffer
;
996 while ((c
= getc (finput
)) != '>' && c
> 0)
998 if (cp
== token_buffer
+ maxtoken
)
999 cp
= grow_token_buffer (cp
);
1004 type_name
= token_buffer
;
1005 value_components_used
= 1;
1011 fprintf (faction
, "yyval");
1013 type_name
= get_type_name (0, rule
);
1015 fprintf (faction
, ".%s", type_name
);
1016 if (!type_name
&& typed
)
1017 complain (_("$$ of `%s' has no declared type"),
1020 else if (isdigit (c
) || c
== '-')
1023 n
= read_signed_integer (finput
);
1026 if (!type_name
&& n
> 0)
1027 type_name
= get_type_name (n
, rule
);
1029 fprintf (faction
, "yyvsp[%d]", n
- stack_offset
);
1031 fprintf (faction
, ".%s", type_name
);
1032 if (!type_name
&& typed
)
1033 complain (_("$%d of `%s' has no declared type"),
1038 complain (_("$%s is invalid"), printable_version (c
));
1043 copy_at (finput
, faction
, stack_offset
);
1047 fatal (_("unmatched %s"), "`{'");
1056 /* above loop exits when c is '}' */
1065 fprintf (faction
, ";\n break;}");
1068 /*-------------------------------------------------------------------.
1069 | After `%guard' is seen in the input file, copy the actual guard |
1070 | into the guards file. If the guard is followed by an action, copy |
1071 | that into the actions file. STACK_OFFSET is the number of values |
1072 | in the current rule so far, which says where to find `$0' with |
1073 | respect to the top of the stack, for the simple parser in which |
1074 | the stack is not popped until after the guard is run. |
1075 `-------------------------------------------------------------------*/
1078 copy_guard (symbol_list
* rule
, int stack_offset
)
1086 /* offset is always 0 if parser has already popped the stack pointer */
1087 if (semantic_parser
)
1090 fprintf (fguard
, "\ncase %d:\n", nrules
);
1092 fprintf (fguard
, "#line %d \"%s\"\n", lineno
, infile
);
1098 while (brace_flag
? (count
> 0) : (c
!= ';'))
1119 complain (_("unmatched %s"), "`}'");
1120 c
= getc (finput
); /* skip it */
1126 copy_string (finput
, fguard
, c
);
1132 if (c
!= '*' && c
!= '/')
1134 copy_comment (finput
, fguard
, c
);
1143 char *cp
= token_buffer
;
1145 while ((c
= getc (finput
)) != '>' && c
> 0)
1147 if (cp
== token_buffer
+ maxtoken
)
1148 cp
= grow_token_buffer (cp
);
1153 type_name
= token_buffer
;
1160 fprintf (fguard
, "yyval");
1162 type_name
= rule
->sym
->type_name
;
1164 fprintf (fguard
, ".%s", type_name
);
1165 if (!type_name
&& typed
)
1166 complain (_("$$ of `%s' has no declared type"),
1169 else if (isdigit (c
) || c
== '-')
1172 n
= read_signed_integer (finput
);
1175 if (!type_name
&& n
> 0)
1176 type_name
= get_type_name (n
, rule
);
1178 fprintf (fguard
, "yyvsp[%d]", n
- stack_offset
);
1180 fprintf (fguard
, ".%s", type_name
);
1181 if (!type_name
&& typed
)
1182 complain (_("$%d of `%s' has no declared type"),
1187 complain (_("$%s is invalid"), printable_version (c
));
1191 copy_at (finput
, fguard
, stack_offset
);
1195 fatal ("%s", _("unterminated %guard clause"));
1201 if (c
!= '}' || count
!= 0)
1205 c
= skip_white_space ();
1207 fprintf (fguard
, ";\n break;}");
1209 copy_action (rule
, stack_offset
);
1212 c
= getc (finput
); /* why not skip_white_space -wjh */
1214 copy_action (rule
, stack_offset
);
1222 record_rule_line (void)
1224 /* Record each rule's source line number in rline table. */
1226 if (nrules
>= rline_allocated
)
1228 rline_allocated
= nrules
* 2;
1229 rline
= (short *) xrealloc ((char *) rline
,
1230 rline_allocated
* sizeof (short));
1232 rline
[nrules
] = lineno
;
1236 /*-------------------------------------------------------------------.
1237 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1238 | with the user's names. |
1239 `-------------------------------------------------------------------*/
1246 sprintf (token_buffer
, "@%d", ++gensym_count
);
1247 sym
= getsym (token_buffer
);
1248 sym
->class = SNTERM
;
1249 sym
->value
= nvars
++;
1254 /*------------------------------------------------------------------.
1255 | read in a %type declaration and record its information for |
1256 | get_type_name to access. This is unused. It is only called from |
1257 | the #if 0 part of readgram |
1258 `------------------------------------------------------------------*/
1271 complain (_("invalid %s declaration"), "%type");
1275 k
= strlen (token_buffer
);
1276 name
= NEW2 (k
+ 1, char);
1277 strcpy (name
, token_buffer
);
1292 if (symval
->type_name
== NULL
)
1293 symval
->type_name
= name
;
1294 else if (strcmp (name
, symval
->type_name
) != 0)
1295 complain (_("type redeclaration for %s"), symval
->tag
);
1307 /*------------------------------------------------------------------.
1308 | Parse the input grammar into a one symbol_list structure. Each |
1309 | rule is represented by a sequence of symbols: the left hand side |
1310 | followed by the contents of the right hand side, followed by a |
1311 | null pointer instead of a symbol to terminate the rule. The next |
1312 | symbol is the lhs of the following rule. |
1314 | All guards and actions are copied out to the appropriate files, |
1315 | labelled by the rule number they apply to. |
1316 `------------------------------------------------------------------*/
1327 symbol_list
*crule
; /* points to first symbol_list of current rule. */
1328 /* its symbol is the lhs of the rule. */
1329 symbol_list
*crule1
; /* points to the symbol_list preceding crule. */
1335 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1337 if (t
== IDENTIFIER
|| t
== BAR
)
1340 int rulelength
= 0; /* number of symbols in rhs of this rule so far */
1341 int xactions
= 0; /* JF for error checking */
1342 bucket
*first_rhs
= 0;
1344 if (t
== IDENTIFIER
)
1357 complain (_("ill-formed rule: initial symbol not followed by colon"));
1362 if (nrules
== 0 && t
== BAR
)
1364 complain (_("grammar starts with vertical bar"));
1365 lhs
= symval
; /* BOGUS: use a random symval */
1367 /* start a new rule and record its lhs. */
1372 record_rule_line ();
1374 p
= NEW (symbol_list
);
1386 /* mark the rule's lhs as a nonterminal if not already so. */
1388 if (lhs
->class == SUNKNOWN
)
1390 lhs
->class = SNTERM
;
1394 else if (lhs
->class == STOKEN
)
1395 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1397 /* read the rhs of the rule. */
1405 crule
->ruleprec
= symval
;
1409 if (!(t
== IDENTIFIER
|| t
== LEFT_CURLY
))
1412 /* If next token is an identifier, see if a colon follows it.
1413 If one does, exit this rule now. */
1414 if (t
== IDENTIFIER
)
1426 if (!first_rhs
) /* JF */
1428 /* Not followed by colon =>
1429 process as part of this rule's rhs. */
1432 /* If we just passed an action, that action was in the middle
1433 of a rule, so make a dummy rule to reduce it to a
1439 /* Since the action was written out with this rule's */
1440 /* number, we must give the new rule this number */
1441 /* by inserting the new rule before it. */
1443 /* Make a dummy nonterminal, a gensym. */
1446 /* Make a new rule, whose body is empty,
1447 before the current one, so that the action
1448 just read can belong to it. */
1451 record_rule_line ();
1452 p
= NEW (symbol_list
);
1458 crule1
= NEW (symbol_list
);
1460 crule1
->next
= crule
;
1462 /* insert the dummy generated by that rule into this rule. */
1464 p
= NEW (symbol_list
);
1472 if (t
== IDENTIFIER
)
1475 p
= NEW (symbol_list
);
1480 else /* handle an action. */
1482 copy_action (crule
, rulelength
);
1484 xactions
++; /* JF */
1487 } /* end of read rhs of rule */
1489 /* Put an empty link in the list to mark the end of this rule */
1490 p
= NEW (symbol_list
);
1496 complain (_("two @prec's in a row"));
1498 crule
->ruleprec
= symval
;
1503 if (!semantic_parser
)
1506 ("%guard present but %semantic_parser not specified"));
1508 copy_guard (crule
, rulelength
);
1511 else if (t
== LEFT_CURLY
)
1513 /* This case never occurs -wjh */
1515 complain (_("two actions at end of one rule"));
1516 copy_action (crule
, rulelength
);
1518 xactions
++; /* -wjh */
1521 /* If $$ is being set in default way, report if any type
1524 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1526 if (lhs
->type_name
== 0
1527 || first_rhs
->type_name
== 0
1528 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1529 complain (_("type clash (`%s' `%s') on default action"),
1530 lhs
->type_name
? lhs
->type_name
: "",
1531 first_rhs
->type_name
? first_rhs
->type_name
: "");
1533 /* Warn if there is no default for $$ but we need one. */
1534 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1535 complain (_("empty rule for typed nonterminal, and no action"));
1540 /* these things can appear as alternatives to rules. */
1542 a) none of the documentation allows them
1543 b) most of them scan forward until finding a next %
1544 thus they may swallow lots of intervening rules
1546 else if (t
== TOKEN
)
1548 parse_token_decl (STOKEN
, SNTERM
);
1551 else if (t
== NTERM
)
1553 parse_token_decl (SNTERM
, STOKEN
);
1560 else if (t
== UNION
)
1562 parse_union_decl ();
1565 else if (t
== EXPECT
)
1567 parse_expect_decl ();
1570 else if (t
== START
)
1572 parse_start_decl ();
1579 complain (_("invalid input: %s"), token_buffer
);
1584 /* grammar has been read. Do some checking */
1586 if (nsyms
> MAXSHORT
)
1587 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1590 fatal (_("no rules in the input grammar"));
1592 if (typed
== 0 /* JF put out same default YYSTYPE as YACC does */
1593 && !value_components_used
)
1595 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1596 but it seems better to be consistent.
1597 Most programs should declare their own type anyway. */
1598 fprintf (fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1600 fprintf (fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1603 /* Report any undefined symbols and consider them nonterminals. */
1605 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1606 if (bp
->class == SUNKNOWN
)
1609 ("symbol %s is used, but is not defined as a token and has no rules"),
1612 bp
->value
= nvars
++;
1615 ntokens
= nsyms
- nvars
;
1618 /*--------------------------------------------------------------.
1619 | For named tokens, but not literal ones, define the name. The |
1620 | value is the user token number. |
1621 `--------------------------------------------------------------*/
1624 output_token_defines (FILE *file
)
1630 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1632 symbol
= bp
->tag
; /* get symbol */
1634 if (bp
->value
>= ntokens
)
1636 if (bp
->user_token_number
== SALIAS
)
1638 if ('\'' == *symbol
)
1639 continue; /* skip literal character */
1641 continue; /* skip error token */
1642 if ('\"' == *symbol
)
1644 /* use literal string only if given a symbol with an alias */
1646 symbol
= bp
->alias
->tag
;
1651 /* Don't #define nonliteral tokens whose names contain periods. */
1653 while ((c
= *cp
++) && c
!= '.');
1657 fprintf (file
, "#define\t%s\t%d\n", symbol
,
1658 ((translations
&& !rawtoknumflag
)
1659 ? bp
->user_token_number
: bp
->value
));
1660 if (semantic_parser
)
1661 fprintf (file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1668 /*------------------------------------------------------------------.
1669 | Assign symbol numbers, and write definition of token names into |
1670 | fdefines. Set up vectors tags and sprec of names and precedences |
1672 `------------------------------------------------------------------*/
1680 int last_user_token_number
;
1681 static char DOLLAR
[] = "$";
1683 /* int lossage = 0; JF set but not used */
1685 tags
= NEW2 (nsyms
+ 1, char *);
1687 user_toknums
= NEW2 (nsyms
+ 1, short);
1688 user_toknums
[0] = 0;
1690 sprec
= NEW2 (nsyms
, short);
1691 sassoc
= NEW2 (nsyms
, short);
1693 max_user_token_number
= 256;
1694 last_user_token_number
= 256;
1696 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1698 if (bp
->class == SNTERM
)
1700 bp
->value
+= ntokens
;
1704 /* this symbol and its alias are a single token defn.
1705 allocate a tokno, and assign to both check agreement of
1706 ->prec and ->assoc fields and make both the same */
1708 bp
->value
= bp
->alias
->value
= tokno
++;
1710 if (bp
->prec
!= bp
->alias
->prec
)
1712 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1713 && bp
->user_token_number
== SALIAS
)
1714 complain (_("conflicting precedences for %s and %s"),
1715 bp
->tag
, bp
->alias
->tag
);
1717 bp
->alias
->prec
= bp
->prec
;
1719 bp
->prec
= bp
->alias
->prec
;
1722 if (bp
->assoc
!= bp
->alias
->assoc
)
1724 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1725 && bp
->user_token_number
== SALIAS
)
1726 complain (_("conflicting assoc values for %s and %s"),
1727 bp
->tag
, bp
->alias
->tag
);
1729 bp
->alias
->assoc
= bp
->assoc
;
1731 bp
->assoc
= bp
->alias
->assoc
;
1734 if (bp
->user_token_number
== SALIAS
)
1735 continue; /* do not do processing below for SALIASs */
1738 else /* bp->class == STOKEN */
1740 bp
->value
= tokno
++;
1743 if (bp
->class == STOKEN
)
1745 if (translations
&& !(bp
->user_token_number
))
1746 bp
->user_token_number
= ++last_user_token_number
;
1747 if (bp
->user_token_number
> max_user_token_number
)
1748 max_user_token_number
= bp
->user_token_number
;
1751 tags
[bp
->value
] = bp
->tag
;
1752 user_toknums
[bp
->value
] = bp
->user_token_number
;
1753 sprec
[bp
->value
] = bp
->prec
;
1754 sassoc
[bp
->value
] = bp
->assoc
;
1762 token_translations
= NEW2 (max_user_token_number
+ 1, short);
1764 /* initialize all entries for literal tokens to 2, the internal
1765 token number for $undefined., which represents all invalid
1767 for (j
= 0; j
<= max_user_token_number
; j
++)
1768 token_translations
[j
] = 2;
1770 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1772 if (bp
->value
>= ntokens
)
1773 continue; /* non-terminal */
1774 if (bp
->user_token_number
== SALIAS
)
1776 if (token_translations
[bp
->user_token_number
] != 2)
1777 complain (_("tokens %s and %s both assigned number %d"),
1778 tags
[token_translations
[bp
->user_token_number
]],
1779 bp
->tag
, bp
->user_token_number
);
1780 token_translations
[bp
->user_token_number
] = bp
->value
;
1784 error_token_number
= errtoken
->value
;
1787 output_token_defines (ftable
);
1789 if (startval
->class == SUNKNOWN
)
1790 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1791 else if (startval
->class == STOKEN
)
1792 fatal (_("the start symbol %s is a token"), startval
->tag
);
1794 start_symbol
= startval
->value
;
1798 output_token_defines (fdefines
);
1802 if (spec_name_prefix
)
1803 fprintf (fdefines
, "\nextern YYSTYPE %slval;\n",
1806 fprintf (fdefines
, "\nextern YYSTYPE yylval;\n");
1809 if (semantic_parser
)
1810 for (i
= ntokens
; i
< nsyms
; i
++)
1812 /* don't make these for dummy nonterminals made by gensym. */
1813 if (*tags
[i
] != '@')
1814 fprintf (fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1817 /* `fdefines' is now a temporary file, so we need to copy its
1818 contents in `done', so we can't close it here. */
1826 /*---------------------------------------------------------------.
1827 | Convert the rules into the representation using RRHS, RLHS and |
1829 `---------------------------------------------------------------*/
1840 ritem
= NEW2 (nitems
+ 1, short);
1841 rlhs
= NEW2 (nrules
, short) - 1;
1842 rrhs
= NEW2 (nrules
, short) - 1;
1843 rprec
= NEW2 (nrules
, short) - 1;
1844 rprecsym
= NEW2 (nrules
, short) - 1;
1845 rassoc
= NEW2 (nrules
, short) - 1;
1853 rlhs
[ruleno
] = p
->sym
->value
;
1854 rrhs
[ruleno
] = itemno
;
1855 ruleprec
= p
->ruleprec
;
1860 ritem
[itemno
++] = p
->sym
->value
;
1861 /* A rule gets by default the precedence and associativity
1862 of the last token in it. */
1863 if (p
->sym
->class == STOKEN
)
1865 rprec
[ruleno
] = p
->sym
->prec
;
1866 rassoc
[ruleno
] = p
->sym
->assoc
;
1872 /* If this rule has a %prec,
1873 the specified symbol's precedence replaces the default. */
1876 rprec
[ruleno
] = ruleprec
->prec
;
1877 rassoc
[ruleno
] = ruleprec
->assoc
;
1878 rprecsym
[ruleno
] = ruleprec
->value
;
1881 ritem
[itemno
++] = -ruleno
;
1891 /*-------------------------------------------------------------------.
1892 | Read in the grammar specification and record it in the format |
1893 | described in gram.h. All guards are copied into the FGUARD file |
1894 | and all actions into FACTION, in each case forming the body of a C |
1895 | function (YYGUARD or YYACTION) which contains a switch statement |
1896 | to decide which guard or action to execute. |
1897 `-------------------------------------------------------------------*/
1903 startval
= NULL
; /* start symbol not specified yet. */
1906 /* initially assume token number translation not needed. */
1909 /* Nowadays translations is always set to 1, since we give `error' a
1910 user-token-number to satisfy the Posix demand for YYERRCODE==256.
1918 rline_allocated
= 10;
1919 rline
= NEW2 (rline_allocated
, short);
1926 semantic_parser
= 0;
1935 /* Initialize the symbol table. */
1937 /* Construct the error token */
1938 errtoken
= getsym ("error");
1939 errtoken
->class = STOKEN
;
1940 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1941 /* Construct a token that represents all undefined literal tokens.
1942 It is always token number 2. */
1943 undeftoken
= getsym ("$undefined.");
1944 undeftoken
->class = STOKEN
;
1945 undeftoken
->user_token_number
= 2;
1947 /* Read the declaration section. Copy %{ ... %} groups to FTABLE
1948 and FDEFINES file. Also notice any %token, %left, etc. found
1950 putc ('\n', ftable
);
1952 /* %s, made from %s\n\
1953 by GNU bison %s. */\n\
1954 \n", noparserflag
? "Bison-generated parse tables" : "A Bison parser", infile
, VERSION
);
1956 fputs ("#define YYBISON 1 /* Identify Bison output. */\n\n", ftable
);
1957 read_declarations ();
1958 /* Start writing the guard and action files, if they are needed. */
1960 /* Read in the grammar, build grammar in list form. Write out
1961 guards and actions. */
1963 /* Now we know whether we need the line-number stack. If we do,
1964 write its type into the .tab.h file. */
1966 reader_output_yylsp (fdefines
);
1967 /* Write closing delimiters for actions and guards. */
1970 fputs ("#define YYLSP_NEEDED\n\n", ftable
);
1971 /* Assign the symbols their symbol numbers. Write #defines for the
1972 token symbols into FDEFINES if requested. */
1974 /* Convert the grammar into the format described in gram.h. */
1976 /* Free the symbol table data structure since symbols are now all
1977 referred to by symbol number. */
1982 reader_output_yylsp (FILE *f
)
1985 fprintf (f
, LTYPESTR
);