]>
git.saurik.com Git - bison.git/blob - src/reader.c
bd13e044e70baf9c8bee222c1a64760932edbd1a
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
;
71 /* Nonzero if any action or guard uses the @n construct. */
72 static int yylsp_needed
;
75 /*===================\
77 \===================*/
80 skip_to_char (int target
)
84 complain (_(" Skipping to next \\n"));
86 complain (_(" Skipping to next %c"), target
);
89 c
= skip_white_space ();
90 while (c
!= target
&& c
!= EOF
);
96 /*---------------------------------------------------------.
97 | Read a signed integer from STREAM and return its value. |
98 `---------------------------------------------------------*/
101 read_signed_integer (FILE *stream
)
103 int c
= getc (stream
);
115 n
= 10 * n
+ (c
- '0');
124 /*-------------------------------------------------------------------.
125 | Dump the string from FINPUT to FOUTPUT. MATCH is the delimiter of |
126 | the string (either ' or "). |
127 `-------------------------------------------------------------------*/
130 copy_string (FILE *fin
, FILE *fout
, int match
)
140 fatal (_("unterminated string at end of file"));
143 complain (_("unterminated string"));
145 c
= match
; /* invent terminator */
155 fatal (_("unterminated string at end of file"));
168 /*---------------------------------------------------------------.
169 | Dump the comment from IN to OUT1 and OUT2. C is either `*' or |
170 | `/', depending upon the type of comments used. OUT2 might be |
172 `---------------------------------------------------------------*/
175 copy_comment2 (FILE *in
, FILE *out1
, FILE *out2
, int c
)
180 cplus_comment
= (c
== '/');
189 if (!cplus_comment
&& c
== '*')
219 fatal (_("unterminated comment"));
231 /*------------------------------------------------------------.
232 | Dump the comment from FIN to FOUT. C is either `*' or `/', |
233 | depending upon the type of comments used. |
234 `------------------------------------------------------------*/
237 copy_comment (FILE *fin
, FILE *fout
, int c
)
239 copy_comment2 (fin
, fout
, NULL
, c
);
243 /*-----------------------------------------------------------------.
244 | FIN is pointing to a location (i.e., a `@'). Output to FOUT a |
245 | reference to this location. STACK_OFFSET is the number of values |
246 | in the current rule so far, which says where to find `$0' with |
247 | respect to the top of the stack. |
248 `-----------------------------------------------------------------*/
251 copy_at (FILE *fin
, FILE *fout
, int stack_offset
)
258 fprintf (fout
, "yyloc");
261 else if (isdigit (c
) || c
== '-')
266 n
= read_signed_integer (fin
);
268 fprintf (fout
, "yylsp[%d]", n
- stack_offset
);
272 complain (_("@%s is invalid"), printable_version (c
));
275 /*-------------------------------------------------------------------.
276 | Copy the contents of a `%{ ... %}' into the definitions file. The |
277 | `%{' has already been read. Return after reading the `%}'. |
278 `-------------------------------------------------------------------*/
281 copy_definition (void)
284 /* -1 while reading a character if prev char was %. */
288 fprintf (fattrs
, "#line %d \"%s\"\n", lineno
, infile
);
309 copy_string (finput
, fattrs
, c
);
315 if (c
!= '*' && c
!= '/')
317 copy_comment (finput
, fattrs
, c
);
321 fatal ("%s", _("unterminated `%{' definition"));
342 /*-------------------------------------------------------------------.
343 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
344 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
346 `-------------------------------------------------------------------*/
349 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
353 struct bucket
*symbol
= NULL
; /* pts to symbol being defined */
358 int tmp_char
= ungetc (skip_white_space (), finput
);
363 fatal (_("Premature EOF after %s"), token_buffer
);
371 if (token
== TYPENAME
)
373 k
= strlen (token_buffer
);
374 typename
= XCALLOC (char, k
+ 1);
375 strcpy (typename
, token_buffer
);
376 value_components_used
= 1;
379 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"' && symbol
)
382 symval
->class = token_sym
;
383 symval
->type_name
= typename
;
384 symval
->user_token_number
= symbol
->user_token_number
;
385 symbol
->user_token_number
= SALIAS
;
387 symval
->alias
= symbol
;
388 symbol
->alias
= symval
;
391 nsyms
--; /* symbol and symval combined are only one symbol */
393 else if (token
== IDENTIFIER
)
395 int oldclass
= symval
->class;
398 if (symbol
->class == what_is_not
)
399 complain (_("symbol %s redefined"), symbol
->tag
);
400 symbol
->class = what_is
;
401 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
402 symbol
->value
= nvars
++;
406 if (symbol
->type_name
== NULL
)
407 symbol
->type_name
= typename
;
408 else if (strcmp (typename
, symbol
->type_name
) != 0)
409 complain (_("type redeclaration for %s"), symbol
->tag
);
412 else if (symbol
&& token
== NUMBER
)
414 symbol
->user_token_number
= numval
;
419 complain (_("`%s' is invalid in %s"),
420 token_buffer
, (what_is
== token_sym
) ? "%token" : "%nterm");
428 /*------------------------------.
429 | Parse what comes after %start |
430 `------------------------------*/
433 parse_start_decl (void)
436 complain (_("multiple %s declarations"), "%start");
437 if (lex () != IDENTIFIER
)
438 complain (_("invalid %s declaration"), "%start");
448 /*--------------------------------------------------------------.
449 | Get the data type (alternative in the union) of the value for |
450 | symbol n in rule rule. |
451 `--------------------------------------------------------------*/
454 get_type_name (int n
, symbol_list
* rule
)
461 complain (_("invalid $ value"));
471 if (rp
== NULL
|| rp
->sym
== NULL
)
473 complain (_("invalid $ value"));
479 return rp
->sym
->type_name
;
483 /*-----------------------------------------------------------.
484 | read in a %type declaration and record its information for |
485 | get_type_name to access |
486 `-----------------------------------------------------------*/
489 parse_type_decl (void)
494 if (lex () != TYPENAME
)
496 complain ("%s", _("%type declaration has no <typename>"));
501 k
= strlen (token_buffer
);
502 name
= XCALLOC (char, k
+ 1);
503 strcpy (name
, token_buffer
);
508 int tmp_char
= ungetc (skip_white_space (), finput
);
513 fatal (_("Premature EOF after %s"), token_buffer
);
525 if (symval
->type_name
== NULL
)
526 symval
->type_name
= name
;
527 else if (strcmp (name
, symval
->type_name
) != 0)
528 complain (_("type redeclaration for %s"), symval
->tag
);
533 complain (_("invalid %%type declaration due to item: %s"),
542 /*----------------------------------------------------------------.
543 | Read in a %left, %right or %nonassoc declaration and record its |
545 `----------------------------------------------------------------*/
548 parse_assoc_decl (associativity assoc
)
554 lastprec
++; /* Assign a new precedence level, never 0. */
559 int tmp_char
= ungetc (skip_white_space (), finput
);
564 fatal (_("Premature EOF after %s"), token_buffer
);
572 k
= strlen (token_buffer
);
573 name
= XCALLOC (char, k
+ 1);
574 strcpy (name
, token_buffer
);
581 if (symval
->prec
!= 0)
582 complain (_("redefining precedence of %s"), symval
->tag
);
583 symval
->prec
= lastprec
;
584 symval
->assoc
= assoc
;
585 if (symval
->class == nterm_sym
)
586 complain (_("symbol %s redefined"), symval
->tag
);
587 symval
->class = token_sym
;
589 { /* record the type, if one is specified */
590 if (symval
->type_name
== NULL
)
591 symval
->type_name
= name
;
592 else if (strcmp (name
, symval
->type_name
) != 0)
593 complain (_("type redeclaration for %s"), symval
->tag
);
598 if (prev
== IDENTIFIER
)
600 symval
->user_token_number
= numval
;
606 ("invalid text (%s) - number should be after identifier"),
616 complain (_("unexpected item: %s"), token_buffer
);
627 /*-------------------------------------------------------------------.
628 | Copy the union declaration into fattrs (and fdefines), where it is |
629 | made into the definition of YYSTYPE, the type of elements of the |
630 | parser value stack. |
631 `-------------------------------------------------------------------*/
634 parse_union_decl (void)
640 complain (_("multiple %s declarations"), "%union");
645 fprintf (fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
647 fprintf (fattrs
, "\n");
649 fprintf (fattrs
, "typedef union");
651 fprintf (fdefines
, "typedef union");
669 if (c
!= '*' && c
!= '/')
671 copy_comment2 (finput
, fattrs
, fdefines
, c
);
681 complain (_("unmatched %s"), "`}'");
685 fprintf (fattrs
, " YYSTYPE;\n");
687 fprintf (fdefines
, " YYSTYPE;\n");
688 /* JF don't choke on trailing semi */
689 c
= skip_white_space ();
701 /*-------------------------------------------------------.
702 | Parse the declaration %expect N which says to expect N |
703 | shift-reduce conflicts. |
704 `-------------------------------------------------------*/
707 parse_expect_decl (void)
714 while (c
== ' ' || c
== '\t')
718 while (c
>= '0' && c
<= '9')
728 if (count
<= 0 || count
> 10)
729 complain ("%s", _("argument of %expect is not an integer"));
730 expected_conflicts
= atoi (buffer
);
734 /*-------------------------------------------------------------------.
735 | Parse what comes after %thong. the full syntax is |
737 | %thong <type> token number literal |
739 | the <type> or number may be omitted. The number specifies the |
740 | user_token_number. |
742 | Two symbols are entered in the table, one for the token symbol and |
743 | one for the literal. Both are given the <type>, if any, from the |
744 | declaration. The ->user_token_number of the first is SALIAS and |
745 | the ->user_token_number of the second is set to the number, if |
746 | any, from the declaration. The two symbols are linked via |
747 | pointers in their ->alias fields. |
749 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
750 | only the literal string is retained it is the literal string that |
751 | is output to yytname |
752 `-------------------------------------------------------------------*/
755 parse_thong_decl (void)
758 struct bucket
*symbol
;
763 token
= lex (); /* fetch typename or first token */
764 if (token
== TYPENAME
)
766 k
= strlen (token_buffer
);
767 typename
= XCALLOC (char, k
+ 1);
768 strcpy (typename
, token_buffer
);
769 value_components_used
= 1;
770 token
= lex (); /* fetch first token */
773 /* process first token */
775 if (token
!= IDENTIFIER
)
777 complain (_("unrecognized item %s, expected an identifier"),
782 symval
->class = token_sym
;
783 symval
->type_name
= typename
;
784 symval
->user_token_number
= SALIAS
;
787 token
= lex (); /* get number or literal string */
792 token
= lex (); /* okay, did number, now get literal */
797 /* process literal string token */
799 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
801 complain (_("expected string constant instead of %s"), token_buffer
);
805 symval
->class = token_sym
;
806 symval
->type_name
= typename
;
807 symval
->user_token_number
= usrtoknum
;
809 symval
->alias
= symbol
;
810 symbol
->alias
= symval
;
812 nsyms
--; /* symbol and symval combined are only one symbol */
816 /*----------------------------------------------------------------.
817 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
818 | any `%' declarations, and copy the contents of any `%{ ... %}' |
819 | groups to fattrs. |
820 `----------------------------------------------------------------*/
823 read_declarations (void)
830 c
= skip_white_space ();
834 tok
= parse_percent_token ();
841 case PERCENT_LEFT_CURLY
:
846 parse_token_decl (token_sym
, nterm_sym
);
850 parse_token_decl (nterm_sym
, token_sym
);
866 parse_expect_decl ();
873 parse_assoc_decl (left_assoc
);
877 parse_assoc_decl (right_assoc
);
881 parse_assoc_decl (non_assoc
);
884 case SEMANTIC_PARSER
:
885 if (semantic_parser
== 0)
900 complain (_("unrecognized: %s"), token_buffer
);
905 fatal (_("no input grammar"));
908 complain (_("unknown character: %s"), printable_version (c
));
914 /*-------------------------------------------------------------------.
915 | Assuming that a `{' has just been seen, copy everything up to the |
916 | matching `}' into the actions file. STACK_OFFSET is the number of |
917 | values in the current rule so far, which says where to find `$0' |
918 | with respect to the top of the stack. |
919 `-------------------------------------------------------------------*/
922 copy_action (symbol_list
* rule
, int stack_offset
)
929 /* offset is always 0 if parser has already popped the stack pointer */
933 fprintf (faction
, "\ncase %d:\n", nrules
);
935 fprintf (faction
, "#line %d \"%s\"\n", lineno
, infile
);
959 copy_string (finput
, faction
, c
);
965 if (c
!= '*' && c
!= '/')
967 copy_comment (finput
, faction
, c
);
976 char *cp
= token_buffer
;
978 while ((c
= getc (finput
)) != '>' && c
> 0)
980 if (cp
== token_buffer
+ maxtoken
)
981 cp
= grow_token_buffer (cp
);
986 type_name
= token_buffer
;
987 value_components_used
= 1;
993 fprintf (faction
, "yyval");
995 type_name
= get_type_name (0, rule
);
997 fprintf (faction
, ".%s", type_name
);
998 if (!type_name
&& typed
)
999 complain (_("$$ of `%s' has no declared type"),
1002 else if (isdigit (c
) || c
== '-')
1005 n
= read_signed_integer (finput
);
1008 if (!type_name
&& n
> 0)
1009 type_name
= get_type_name (n
, rule
);
1011 fprintf (faction
, "yyvsp[%d]", n
- stack_offset
);
1013 fprintf (faction
, ".%s", type_name
);
1014 if (!type_name
&& typed
)
1015 complain (_("$%d of `%s' has no declared type"),
1020 complain (_("$%s is invalid"), printable_version (c
));
1025 copy_at (finput
, faction
, stack_offset
);
1029 fatal (_("unmatched %s"), "`{'");
1038 /* above loop exits when c is '}' */
1047 fprintf (faction
, ";\n break;}");
1050 /*-------------------------------------------------------------------.
1051 | After `%guard' is seen in the input file, copy the actual guard |
1052 | into the guards file. If the guard is followed by an action, copy |
1053 | that into the actions file. STACK_OFFSET is the number of values |
1054 | in the current rule so far, which says where to find `$0' with |
1055 | respect to the top of the stack, for the simple parser in which |
1056 | the stack is not popped until after the guard is run. |
1057 `-------------------------------------------------------------------*/
1060 copy_guard (symbol_list
* rule
, int stack_offset
)
1068 /* offset is always 0 if parser has already popped the stack pointer */
1069 if (semantic_parser
)
1072 fprintf (fguard
, "\ncase %d:\n", nrules
);
1074 fprintf (fguard
, "#line %d \"%s\"\n", lineno
, infile
);
1080 while (brace_flag
? (count
> 0) : (c
!= ';'))
1101 complain (_("unmatched %s"), "`}'");
1102 c
= getc (finput
); /* skip it */
1108 copy_string (finput
, fguard
, c
);
1114 if (c
!= '*' && c
!= '/')
1116 copy_comment (finput
, fguard
, c
);
1125 char *cp
= token_buffer
;
1127 while ((c
= getc (finput
)) != '>' && c
> 0)
1129 if (cp
== token_buffer
+ maxtoken
)
1130 cp
= grow_token_buffer (cp
);
1135 type_name
= token_buffer
;
1142 fprintf (fguard
, "yyval");
1144 type_name
= rule
->sym
->type_name
;
1146 fprintf (fguard
, ".%s", type_name
);
1147 if (!type_name
&& typed
)
1148 complain (_("$$ of `%s' has no declared type"),
1151 else if (isdigit (c
) || c
== '-')
1154 n
= read_signed_integer (finput
);
1157 if (!type_name
&& n
> 0)
1158 type_name
= get_type_name (n
, rule
);
1160 fprintf (fguard
, "yyvsp[%d]", n
- stack_offset
);
1162 fprintf (fguard
, ".%s", type_name
);
1163 if (!type_name
&& typed
)
1164 complain (_("$%d of `%s' has no declared type"),
1169 complain (_("$%s is invalid"), printable_version (c
));
1173 copy_at (finput
, fguard
, stack_offset
);
1177 fatal ("%s", _("unterminated %guard clause"));
1183 if (c
!= '}' || count
!= 0)
1187 c
= skip_white_space ();
1189 fprintf (fguard
, ";\n break;}");
1191 copy_action (rule
, stack_offset
);
1194 c
= getc (finput
); /* why not skip_white_space -wjh */
1196 copy_action (rule
, stack_offset
);
1204 record_rule_line (void)
1206 /* Record each rule's source line number in rline table. */
1208 if (nrules
>= rline_allocated
)
1210 rline_allocated
= nrules
* 2;
1211 rline
= XREALLOC (rline
, short, rline_allocated
);
1213 rline
[nrules
] = lineno
;
1217 /*-------------------------------------------------------------------.
1218 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1219 | with the user's names. |
1220 `-------------------------------------------------------------------*/
1227 sprintf (token_buffer
, "@%d", ++gensym_count
);
1228 sym
= getsym (token_buffer
);
1229 sym
->class = nterm_sym
;
1230 sym
->value
= nvars
++;
1235 /*------------------------------------------------------------------.
1236 | read in a %type declaration and record its information for |
1237 | get_type_name to access. This is unused. It is only called from |
1238 | the #if 0 part of readgram |
1239 `------------------------------------------------------------------*/
1252 complain (_("invalid %s declaration"), "%type");
1256 k
= strlen (token_buffer
);
1257 name
= XCALLOC (char, k
+ 1);
1258 strcpy (name
, token_buffer
);
1273 if (symval
->type_name
== NULL
)
1274 symval
->type_name
= name
;
1275 else if (strcmp (name
, symval
->type_name
) != 0)
1276 complain (_("type redeclaration for %s"), symval
->tag
);
1288 /*------------------------------------------------------------------.
1289 | Parse the input grammar into a one symbol_list structure. Each |
1290 | rule is represented by a sequence of symbols: the left hand side |
1291 | followed by the contents of the right hand side, followed by a |
1292 | null pointer instead of a symbol to terminate the rule. The next |
1293 | symbol is the lhs of the following rule. |
1295 | All guards and actions are copied out to the appropriate files, |
1296 | labelled by the rule number they apply to. |
1297 `------------------------------------------------------------------*/
1308 symbol_list
*crule
; /* points to first symbol_list of current rule. */
1309 /* its symbol is the lhs of the rule. */
1310 symbol_list
*crule1
; /* points to the symbol_list preceding crule. */
1316 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1318 if (t
== IDENTIFIER
|| t
== BAR
)
1321 int rulelength
= 0; /* number of symbols in rhs of this rule so far */
1322 int xactions
= 0; /* JF for error checking */
1323 bucket
*first_rhs
= 0;
1325 if (t
== IDENTIFIER
)
1338 complain (_("ill-formed rule: initial symbol not followed by colon"));
1343 if (nrules
== 0 && t
== BAR
)
1345 complain (_("grammar starts with vertical bar"));
1346 lhs
= symval
; /* BOGUS: use a random symval */
1348 /* start a new rule and record its lhs. */
1353 record_rule_line ();
1355 p
= XCALLOC (symbol_list
, 1);
1367 /* mark the rule's lhs as a nonterminal if not already so. */
1369 if (lhs
->class == unknown_sym
)
1371 lhs
->class = nterm_sym
;
1375 else if (lhs
->class == token_sym
)
1376 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1378 /* read the rhs of the rule. */
1386 crule
->ruleprec
= symval
;
1390 if (!(t
== IDENTIFIER
|| t
== LEFT_CURLY
))
1393 /* If next token is an identifier, see if a colon follows it.
1394 If one does, exit this rule now. */
1395 if (t
== IDENTIFIER
)
1407 if (!first_rhs
) /* JF */
1409 /* Not followed by colon =>
1410 process as part of this rule's rhs. */
1413 /* If we just passed an action, that action was in the middle
1414 of a rule, so make a dummy rule to reduce it to a
1420 /* Since the action was written out with this rule's */
1421 /* number, we must give the new rule this number */
1422 /* by inserting the new rule before it. */
1424 /* Make a dummy nonterminal, a gensym. */
1427 /* Make a new rule, whose body is empty,
1428 before the current one, so that the action
1429 just read can belong to it. */
1432 record_rule_line ();
1433 p
= XCALLOC (symbol_list
, 1);
1439 crule1
= XCALLOC (symbol_list
, 1);
1441 crule1
->next
= crule
;
1443 /* insert the dummy generated by that rule into this rule. */
1445 p
= XCALLOC (symbol_list
, 1);
1453 if (t
== IDENTIFIER
)
1456 p
= XCALLOC (symbol_list
, 1);
1461 else /* handle an action. */
1463 copy_action (crule
, rulelength
);
1465 xactions
++; /* JF */
1468 } /* end of read rhs of rule */
1470 /* Put an empty link in the list to mark the end of this rule */
1471 p
= XCALLOC (symbol_list
, 1);
1477 complain (_("two @prec's in a row"));
1479 crule
->ruleprec
= symval
;
1484 if (!semantic_parser
)
1487 ("%guard present but %semantic_parser not specified"));
1489 copy_guard (crule
, rulelength
);
1492 else if (t
== LEFT_CURLY
)
1494 /* This case never occurs -wjh */
1496 complain (_("two actions at end of one rule"));
1497 copy_action (crule
, rulelength
);
1499 xactions
++; /* -wjh */
1502 /* If $$ is being set in default way, report if any type
1505 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1507 if (lhs
->type_name
== 0
1508 || first_rhs
->type_name
== 0
1509 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1510 complain (_("type clash (`%s' `%s') on default action"),
1511 lhs
->type_name
? lhs
->type_name
: "",
1512 first_rhs
->type_name
? first_rhs
->type_name
: "");
1514 /* Warn if there is no default for $$ but we need one. */
1515 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1516 complain (_("empty rule for typed nonterminal, and no action"));
1521 /* these things can appear as alternatives to rules. */
1523 a) none of the documentation allows them
1524 b) most of them scan forward until finding a next %
1525 thus they may swallow lots of intervening rules
1527 else if (t
== TOKEN
)
1529 parse_token_decl (token_sym
, nterm_sym
);
1532 else if (t
== NTERM
)
1534 parse_token_decl (nterm_sym
, token_sym
);
1541 else if (t
== UNION
)
1543 parse_union_decl ();
1546 else if (t
== EXPECT
)
1548 parse_expect_decl ();
1551 else if (t
== START
)
1553 parse_start_decl ();
1560 complain (_("invalid input: %s"), token_buffer
);
1565 /* grammar has been read. Do some checking */
1567 if (nsyms
> MAXSHORT
)
1568 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1571 fatal (_("no rules in the input grammar"));
1573 if (typed
== 0 /* JF put out same default YYSTYPE as YACC does */
1574 && !value_components_used
)
1576 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1577 but it seems better to be consistent.
1578 Most programs should declare their own type anyway. */
1579 fprintf (fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1581 fprintf (fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1584 /* Report any undefined symbols and consider them nonterminals. */
1586 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1587 if (bp
->class == unknown_sym
)
1590 ("symbol %s is used, but is not defined as a token and has no rules"),
1592 bp
->class = nterm_sym
;
1593 bp
->value
= nvars
++;
1596 ntokens
= nsyms
- nvars
;
1599 /*--------------------------------------------------------------.
1600 | For named tokens, but not literal ones, define the name. The |
1601 | value is the user token number. |
1602 `--------------------------------------------------------------*/
1605 output_token_defines (FILE *file
)
1611 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1613 symbol
= bp
->tag
; /* get symbol */
1615 if (bp
->value
>= ntokens
)
1617 if (bp
->user_token_number
== SALIAS
)
1619 if ('\'' == *symbol
)
1620 continue; /* skip literal character */
1622 continue; /* skip error token */
1623 if ('\"' == *symbol
)
1625 /* use literal string only if given a symbol with an alias */
1627 symbol
= bp
->alias
->tag
;
1632 /* Don't #define nonliteral tokens whose names contain periods. */
1634 while ((c
= *cp
++) && c
!= '.');
1638 fprintf (file
, "#define\t%s\t%d\n", symbol
,
1639 ((translations
&& !rawtoknumflag
)
1640 ? bp
->user_token_number
: bp
->value
));
1641 if (semantic_parser
)
1642 fprintf (file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1649 /*------------------------------------------------------------------.
1650 | Assign symbol numbers, and write definition of token names into |
1651 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1653 `------------------------------------------------------------------*/
1661 int last_user_token_number
;
1662 static char DOLLAR
[] = "$";
1664 /* int lossage = 0; JF set but not used */
1666 tags
= XCALLOC (char *, nsyms
+ 1);
1668 user_toknums
= XCALLOC (short, nsyms
+ 1);
1669 user_toknums
[0] = 0;
1671 sprec
= XCALLOC (short, nsyms
);
1672 sassoc
= XCALLOC (short, nsyms
);
1674 max_user_token_number
= 256;
1675 last_user_token_number
= 256;
1677 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1679 if (bp
->class == nterm_sym
)
1681 bp
->value
+= ntokens
;
1685 /* this symbol and its alias are a single token defn.
1686 allocate a tokno, and assign to both check agreement of
1687 ->prec and ->assoc fields and make both the same */
1689 bp
->value
= bp
->alias
->value
= tokno
++;
1691 if (bp
->prec
!= bp
->alias
->prec
)
1693 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1694 && bp
->user_token_number
== SALIAS
)
1695 complain (_("conflicting precedences for %s and %s"),
1696 bp
->tag
, bp
->alias
->tag
);
1698 bp
->alias
->prec
= bp
->prec
;
1700 bp
->prec
= bp
->alias
->prec
;
1703 if (bp
->assoc
!= bp
->alias
->assoc
)
1705 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1706 && bp
->user_token_number
== SALIAS
)
1707 complain (_("conflicting assoc values for %s and %s"),
1708 bp
->tag
, bp
->alias
->tag
);
1710 bp
->alias
->assoc
= bp
->assoc
;
1712 bp
->assoc
= bp
->alias
->assoc
;
1715 if (bp
->user_token_number
== SALIAS
)
1716 continue; /* do not do processing below for SALIASs */
1719 else /* bp->class == token_sym */
1721 bp
->value
= tokno
++;
1724 if (bp
->class == token_sym
)
1726 if (translations
&& !(bp
->user_token_number
))
1727 bp
->user_token_number
= ++last_user_token_number
;
1728 if (bp
->user_token_number
> max_user_token_number
)
1729 max_user_token_number
= bp
->user_token_number
;
1732 tags
[bp
->value
] = bp
->tag
;
1733 user_toknums
[bp
->value
] = bp
->user_token_number
;
1734 sprec
[bp
->value
] = bp
->prec
;
1735 sassoc
[bp
->value
] = bp
->assoc
;
1743 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1745 /* initialize all entries for literal tokens to 2, the internal
1746 token number for $undefined., which represents all invalid
1748 for (j
= 0; j
<= max_user_token_number
; j
++)
1749 token_translations
[j
] = 2;
1751 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1753 if (bp
->value
>= ntokens
)
1754 continue; /* non-terminal */
1755 if (bp
->user_token_number
== SALIAS
)
1757 if (token_translations
[bp
->user_token_number
] != 2)
1758 complain (_("tokens %s and %s both assigned number %d"),
1759 tags
[token_translations
[bp
->user_token_number
]],
1760 bp
->tag
, bp
->user_token_number
);
1761 token_translations
[bp
->user_token_number
] = bp
->value
;
1765 error_token_number
= errtoken
->value
;
1768 output_token_defines (ftable
);
1770 if (startval
->class == unknown_sym
)
1771 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1772 else if (startval
->class == token_sym
)
1773 fatal (_("the start symbol %s is a token"), startval
->tag
);
1775 start_symbol
= startval
->value
;
1779 output_token_defines (fdefines
);
1783 if (spec_name_prefix
)
1784 fprintf (fdefines
, "\nextern YYSTYPE %slval;\n",
1787 fprintf (fdefines
, "\nextern YYSTYPE yylval;\n");
1790 if (semantic_parser
)
1791 for (i
= ntokens
; i
< nsyms
; i
++)
1793 /* don't make these for dummy nonterminals made by gensym. */
1794 if (*tags
[i
] != '@')
1795 fprintf (fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1798 /* `fdefines' is now a temporary file, so we need to copy its
1799 contents in `done', so we can't close it here. */
1807 /*---------------------------------------------------------------.
1808 | Convert the rules into the representation using RRHS, RLHS and |
1810 `---------------------------------------------------------------*/
1821 ritem
= XCALLOC (short, nitems
+ 1);
1822 rlhs
= XCALLOC (short, nrules
) - 1;
1823 rrhs
= XCALLOC (short, nrules
) - 1;
1824 rprec
= XCALLOC (short, nrules
) - 1;
1825 rprecsym
= XCALLOC (short, nrules
) - 1;
1826 rassoc
= XCALLOC (short, nrules
) - 1;
1834 rlhs
[ruleno
] = p
->sym
->value
;
1835 rrhs
[ruleno
] = itemno
;
1836 ruleprec
= p
->ruleprec
;
1841 ritem
[itemno
++] = p
->sym
->value
;
1842 /* A rule gets by default the precedence and associativity
1843 of the last token in it. */
1844 if (p
->sym
->class == token_sym
)
1846 rprec
[ruleno
] = p
->sym
->prec
;
1847 rassoc
[ruleno
] = p
->sym
->assoc
;
1853 /* If this rule has a %prec,
1854 the specified symbol's precedence replaces the default. */
1857 rprec
[ruleno
] = ruleprec
->prec
;
1858 rassoc
[ruleno
] = ruleprec
->assoc
;
1859 rprecsym
[ruleno
] = ruleprec
->value
;
1862 ritem
[itemno
++] = -ruleno
;
1872 /*-------------------------------------------------------------------.
1873 | Read in the grammar specification and record it in the format |
1874 | described in gram.h. All guards are copied into the FGUARD file |
1875 | and all actions into FACTION, in each case forming the body of a C |
1876 | function (YYGUARD or YYACTION) which contains a switch statement |
1877 | to decide which guard or action to execute. |
1878 `-------------------------------------------------------------------*/
1884 startval
= NULL
; /* start symbol not specified yet. */
1887 /* initially assume token number translation not needed. */
1890 /* Nowadays translations is always set to 1, since we give `error' a
1891 user-token-number to satisfy the Posix demand for YYERRCODE==256.
1899 rline_allocated
= 10;
1900 rline
= XCALLOC (short, rline_allocated
);
1907 semantic_parser
= 0;
1916 /* Initialize the symbol table. */
1918 /* Construct the error token */
1919 errtoken
= getsym ("error");
1920 errtoken
->class = token_sym
;
1921 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1922 /* Construct a token that represents all undefined literal tokens.
1923 It is always token number 2. */
1924 undeftoken
= getsym ("$undefined.");
1925 undeftoken
->class = token_sym
;
1926 undeftoken
->user_token_number
= 2;
1928 /* Read the declaration section. Copy %{ ... %} groups to FTABLE
1929 and FDEFINES file. Also notice any %token, %left, etc. found
1931 putc ('\n', ftable
);
1933 /* %s, made from %s\n\
1934 by GNU bison %s. */\n\
1935 \n", noparserflag
? "Bison-generated parse tables" : "A Bison parser", infile
, VERSION
);
1937 fputs ("#define YYBISON 1 /* Identify Bison output. */\n\n", ftable
);
1938 read_declarations ();
1939 /* Start writing the guard and action files, if they are needed. */
1941 /* Read in the grammar, build grammar in list form. Write out
1942 guards and actions. */
1944 /* Now we know whether we need the line-number stack. If we do,
1945 write its type into the .tab.h file. */
1947 reader_output_yylsp (fdefines
);
1948 /* Write closing delimiters for actions and guards. */
1951 fputs ("#define YYLSP_NEEDED\n\n", ftable
);
1952 /* Assign the symbols their symbol numbers. Write #defines for the
1953 token symbols into FDEFINES if requested. */
1955 /* Convert the grammar into the format described in gram.h. */
1957 /* Free the symbol table data structure since symbols are now all
1958 referred to by symbol number. */
1964 reader_output_yylsp (FILE *f
)
1985 #define YYLTYPE yyltype\n\