]>
git.saurik.com Git - bison.git/blob - src/reader.c
c89313e2baf123c27a04f1928e57c19d792b8a48
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. */
34 extern bucket
*symval
;
36 extern int expected_conflicts
;
37 extern char *token_buffer
;
40 extern void tabinit
PARAMS ((void));
41 extern void free_symtab
PARAMS ((void));
42 extern char *printable_version
PARAMS ((int));
62 #define YYLTYPE yyltype\n\
66 /* Number of slots allocated (but not necessarily used yet) in `rline' */
67 static int rline_allocated
;
69 typedef struct symbol_list
71 struct symbol_list
*next
;
80 static symbol_list
*grammar
;
81 static int start_flag
;
82 static bucket
*startval
;
84 /* Nonzero if components of semantic values are used, implying
85 they must be unions. */
86 static int value_components_used
;
88 static int typed
; /* nonzero if %union has been seen. */
90 static int lastprec
; /* incremented for each %left, %right or %nonassoc seen */
92 static int gensym_count
; /* incremented for each generated symbol */
94 static bucket
*errtoken
;
95 static bucket
*undeftoken
;
97 /* Nonzero if any action or guard uses the @n construct. */
98 static int yylsp_needed
;
101 /*===================\
102 | Low level lexing. |
103 \===================*/
106 skip_to_char (int target
)
110 complain (_(" Skipping to next \\n"));
112 complain (_(" Skipping to next %c"), target
);
115 c
= skip_white_space ();
116 while (c
!= target
&& c
!= EOF
);
122 /*---------------------------------------------------------.
123 | Read a signed integer from STREAM and return its value. |
124 `---------------------------------------------------------*/
127 read_signed_integer (FILE *stream
)
129 int c
= getc (stream
);
141 n
= 10 * n
+ (c
- '0');
150 /*-------------------------------------------------------------------.
151 | Dump the string from FINPUT to FOUTPUT. MATCH is the delimiter of |
152 | the string (either ' or "). |
153 `-------------------------------------------------------------------*/
156 copy_string (FILE *fin
, FILE *fout
, int match
)
166 fatal (_("unterminated string at end of file"));
169 complain (_("unterminated string"));
171 c
= match
; /* invent terminator */
181 fatal (_("unterminated string at end of file"));
194 /*---------------------------------------------------------------.
195 | Dump the comment from IN to OUT1 and OUT2. C is either `*' or |
196 | `/', depending upon the type of comments used. OUT2 might be |
198 `---------------------------------------------------------------*/
201 copy_comment2 (FILE *in
, FILE *out1
, FILE *out2
, int c
)
206 cplus_comment
= (c
== '/');
215 if (!cplus_comment
&& c
== '*')
245 fatal (_("unterminated comment"));
257 /*------------------------------------------------------------.
258 | Dump the comment from FIN to FOUT. C is either `*' or `/', |
259 | depending upon the type of comments used. |
260 `------------------------------------------------------------*/
263 copy_comment (FILE *fin
, FILE *fout
, int c
)
265 copy_comment2 (fin
, fout
, NULL
, c
);
269 /*-----------------------------------------------------------------.
270 | FIN is pointing to a location (i.e., a `@'). Output to FOUT a |
271 | reference to this location. STACK_OFFSET is the number of values |
272 | in the current rule so far, which says where to find `$0' with |
273 | respect to the top of the stack. |
274 `-----------------------------------------------------------------*/
277 copy_at (FILE *fin
, FILE *fout
, int stack_offset
)
284 fprintf (fout
, "yyloc");
287 else if (isdigit (c
) || c
== '-')
292 n
= read_signed_integer (fin
);
294 fprintf (fout
, "yylsp[%d]", n
- stack_offset
);
298 complain (_("@%s is invalid"), printable_version (c
));
301 /*-------------------------------------------------------------------.
302 | Copy the contents of a `%{ ... %}' into the definitions file. The |
303 | `%{' has already been read. Return after reading the `%}'. |
304 `-------------------------------------------------------------------*/
307 copy_definition (void)
310 /* -1 while reading a character if prev char was %. */
314 fprintf (fattrs
, "#line %d \"%s\"\n", lineno
, infile
);
335 copy_string (finput
, fattrs
, c
);
341 if (c
!= '*' && c
!= '/')
343 copy_comment (finput
, fattrs
, c
);
347 fatal ("%s", _("unterminated `%{' definition"));
368 /*-----------------------------------------------------------------.
369 | Parse what comes after %token or %nterm. For %token, what_is is |
370 | STOKEN and what_is_not is SNTERM. For %nterm, the arguments are |
372 `-----------------------------------------------------------------*/
375 parse_token_decl (int what_is
, int what_is_not
)
379 struct bucket
*symbol
= NULL
; /* pts to symbol being defined */
384 int tmp_char
= ungetc (skip_white_space (), finput
);
389 fatal (_("Premature EOF after %s"), token_buffer
);
397 if (token
== TYPENAME
)
399 k
= strlen (token_buffer
);
400 typename
= NEW2 (k
+ 1, char);
401 strcpy (typename
, token_buffer
);
402 value_components_used
= 1;
405 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"' && symbol
)
408 symval
->class = STOKEN
;
409 symval
->type_name
= typename
;
410 symval
->user_token_number
= symbol
->user_token_number
;
411 symbol
->user_token_number
= SALIAS
;
413 symval
->alias
= symbol
;
414 symbol
->alias
= symval
;
417 nsyms
--; /* symbol and symval combined are only one symbol */
419 else if (token
== IDENTIFIER
)
421 int oldclass
= symval
->class;
424 if (symbol
->class == what_is_not
)
425 complain (_("symbol %s redefined"), symbol
->tag
);
426 symbol
->class = what_is
;
427 if (what_is
== SNTERM
&& oldclass
!= SNTERM
)
428 symbol
->value
= nvars
++;
432 if (symbol
->type_name
== NULL
)
433 symbol
->type_name
= typename
;
434 else if (strcmp (typename
, symbol
->type_name
) != 0)
435 complain (_("type redeclaration for %s"), symbol
->tag
);
438 else if (symbol
&& token
== NUMBER
)
440 symbol
->user_token_number
= numval
;
445 complain (_("`%s' is invalid in %s"),
446 token_buffer
, (what_is
== STOKEN
) ? "%token" : "%nterm");
454 /* Parse what comes after %start */
457 parse_start_decl (void)
460 complain (_("multiple %s declarations"), "%start");
461 if (lex () != IDENTIFIER
)
462 complain (_("invalid %s declaration"), "%start");
472 /*--------------------------------------------------------------.
473 | Get the data type (alternative in the union) of the value for |
474 | symbol n in rule rule. |
475 `--------------------------------------------------------------*/
478 get_type_name (int n
, symbol_list
* rule
)
485 complain (_("invalid $ value"));
495 if (rp
== NULL
|| rp
->sym
== NULL
)
497 complain (_("invalid $ value"));
503 return rp
->sym
->type_name
;
507 /*-----------------------------------------------------------.
508 | read in a %type declaration and record its information for |
509 | get_type_name to access |
510 `-----------------------------------------------------------*/
513 parse_type_decl (void)
518 if (lex () != TYPENAME
)
520 complain ("%s", _("%type declaration has no <typename>"));
525 k
= strlen (token_buffer
);
526 name
= NEW2 (k
+ 1, char);
527 strcpy (name
, token_buffer
);
532 int tmp_char
= ungetc (skip_white_space (), finput
);
537 fatal (_("Premature EOF after %s"), token_buffer
);
549 if (symval
->type_name
== NULL
)
550 symval
->type_name
= name
;
551 else if (strcmp (name
, symval
->type_name
) != 0)
552 complain (_("type redeclaration for %s"), symval
->tag
);
557 complain (_("invalid %%type declaration due to item: %s"),
566 /* read in a %left, %right or %nonassoc declaration and record its information. */
567 /* assoc is either LEFT_ASSOC, RIGHT_ASSOC or NON_ASSOC. */
570 parse_assoc_decl (int assoc
)
576 lastprec
++; /* Assign a new precedence level, never 0. */
581 int tmp_char
= ungetc (skip_white_space (), finput
);
586 fatal (_("Premature EOF after %s"), token_buffer
);
594 k
= strlen (token_buffer
);
595 name
= NEW2 (k
+ 1, char);
596 strcpy (name
, token_buffer
);
603 if (symval
->prec
!= 0)
604 complain (_("redefining precedence of %s"), symval
->tag
);
605 symval
->prec
= lastprec
;
606 symval
->assoc
= assoc
;
607 if (symval
->class == SNTERM
)
608 complain (_("symbol %s redefined"), symval
->tag
);
609 symval
->class = STOKEN
;
611 { /* record the type, if one is specified */
612 if (symval
->type_name
== NULL
)
613 symval
->type_name
= name
;
614 else if (strcmp (name
, symval
->type_name
) != 0)
615 complain (_("type redeclaration for %s"), symval
->tag
);
620 if (prev
== IDENTIFIER
)
622 symval
->user_token_number
= numval
;
628 ("invalid text (%s) - number should be after identifier"),
638 complain (_("unexpected item: %s"), token_buffer
);
649 /* copy the union declaration into fattrs (and fdefines),
650 where it is made into the
651 definition of YYSTYPE, the type of elements of the parser value stack. */
654 parse_union_decl (void)
660 complain (_("multiple %s declarations"), "%union");
665 fprintf (fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
667 fprintf (fattrs
, "\n");
669 fprintf (fattrs
, "typedef union");
671 fprintf (fdefines
, "typedef union");
689 if (c
!= '*' && c
!= '/')
691 copy_comment2 (finput
, fattrs
, fdefines
, c
);
701 complain (_("unmatched %s"), "`}'");
705 fprintf (fattrs
, " YYSTYPE;\n");
707 fprintf (fdefines
, " YYSTYPE;\n");
708 /* JF don't choke on trailing semi */
709 c
= skip_white_space ();
720 /* parse the declaration %expect N which says to expect N
721 shift-reduce conflicts. */
724 parse_expect_decl (void)
731 while (c
== ' ' || c
== '\t')
735 while (c
>= '0' && c
<= '9')
745 if (count
<= 0 || count
> 10)
746 complain ("%s", _("argument of %expect is not an integer"));
747 expected_conflicts
= atoi (buffer
);
751 /*-------------------------------------------------------------------.
752 | Parse what comes after %thong. the full syntax is |
754 | %thong <type> token number literal |
756 | the <type> or number may be omitted. The number specifies the |
757 | user_token_number. |
759 | Two symbols are entered in the table, one for the token symbol and |
760 | one for the literal. Both are given the <type>, if any, from the |
761 | declaration. The ->user_token_number of the first is SALIAS and |
762 | the ->user_token_number of the second is set to the number, if |
763 | any, from the declaration. The two symbols are linked via |
764 | pointers in their ->alias fields. |
766 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
767 | only the literal string is retained it is the literal string that |
768 | is output to yytname |
769 `-------------------------------------------------------------------*/
772 parse_thong_decl (void)
775 struct bucket
*symbol
;
780 token
= lex (); /* fetch typename or first token */
781 if (token
== TYPENAME
)
783 k
= strlen (token_buffer
);
784 typename
= NEW2 (k
+ 1, char);
785 strcpy (typename
, token_buffer
);
786 value_components_used
= 1;
787 token
= lex (); /* fetch first token */
790 /* process first token */
792 if (token
!= IDENTIFIER
)
794 complain (_("unrecognized item %s, expected an identifier"),
799 symval
->class = STOKEN
;
800 symval
->type_name
= typename
;
801 symval
->user_token_number
= SALIAS
;
804 token
= lex (); /* get number or literal string */
809 token
= lex (); /* okay, did number, now get literal */
814 /* process literal string token */
816 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
818 complain (_("expected string constant instead of %s"), token_buffer
);
822 symval
->class = STOKEN
;
823 symval
->type_name
= typename
;
824 symval
->user_token_number
= usrtoknum
;
826 symval
->alias
= symbol
;
827 symbol
->alias
= symval
;
829 nsyms
--; /* symbol and symval combined are only one symbol */
832 /*----------------------------------------------------------------.
833 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
834 | any `%' declarations, and copy the contents of any `%{ ... %}' |
835 | groups to fattrs. |
836 `----------------------------------------------------------------*/
839 read_declarations (void)
846 c
= skip_white_space ();
850 tok
= parse_percent_token ();
857 case PERCENT_LEFT_CURLY
:
862 parse_token_decl (STOKEN
, SNTERM
);
866 parse_token_decl (SNTERM
, STOKEN
);
882 parse_expect_decl ();
888 parse_assoc_decl (LEFT_ASSOC
);
892 parse_assoc_decl (RIGHT_ASSOC
);
896 parse_assoc_decl (NON_ASSOC
);
899 case SEMANTIC_PARSER
:
900 if (semantic_parser
== 0)
915 complain (_("unrecognized: %s"), token_buffer
);
920 fatal (_("no input grammar"));
923 complain (_("unknown character: %s"), printable_version (c
));
929 /*-------------------------------------------------------------------.
930 | Assuming that a `{' has just been seen, copy everything up to the |
931 | matching `}' into the actions file. STACK_OFFSET is the number of |
932 | values in the current rule so far, which says where to find `$0' |
933 | with respect to the top of the stack. |
934 `-------------------------------------------------------------------*/
937 copy_action (symbol_list
* rule
, int stack_offset
)
944 /* offset is always 0 if parser has already popped the stack pointer */
948 fprintf (faction
, "\ncase %d:\n", nrules
);
950 fprintf (faction
, "#line %d \"%s\"\n", lineno
, infile
);
974 copy_string (finput
, faction
, c
);
980 if (c
!= '*' && c
!= '/')
982 copy_comment (finput
, faction
, c
);
991 char *cp
= token_buffer
;
993 while ((c
= getc (finput
)) != '>' && c
> 0)
995 if (cp
== token_buffer
+ maxtoken
)
996 cp
= grow_token_buffer (cp
);
1001 type_name
= token_buffer
;
1002 value_components_used
= 1;
1008 fprintf (faction
, "yyval");
1010 type_name
= get_type_name (0, rule
);
1012 fprintf (faction
, ".%s", type_name
);
1013 if (!type_name
&& typed
)
1014 complain (_("$$ of `%s' has no declared type"),
1017 else if (isdigit (c
) || c
== '-')
1020 n
= read_signed_integer (finput
);
1023 if (!type_name
&& n
> 0)
1024 type_name
= get_type_name (n
, rule
);
1026 fprintf (faction
, "yyvsp[%d]", n
- stack_offset
);
1028 fprintf (faction
, ".%s", type_name
);
1029 if (!type_name
&& typed
)
1030 complain (_("$%d of `%s' has no declared type"),
1035 complain (_("$%s is invalid"), printable_version (c
));
1040 copy_at (finput
, faction
, stack_offset
);
1044 fatal (_("unmatched %s"), "`{'");
1053 /* above loop exits when c is '}' */
1062 fprintf (faction
, ";\n break;}");
1065 /*-------------------------------------------------------------------.
1066 | After `%guard' is seen in the input file, copy the actual guard |
1067 | into the guards file. If the guard is followed by an action, copy |
1068 | that into the actions file. STACK_OFFSET is the number of values |
1069 | in the current rule so far, which says where to find `$0' with |
1070 | respect to the top of the stack, for the simple parser in which |
1071 | the stack is not popped until after the guard is run. |
1072 `-------------------------------------------------------------------*/
1075 copy_guard (symbol_list
* rule
, int stack_offset
)
1083 /* offset is always 0 if parser has already popped the stack pointer */
1084 if (semantic_parser
)
1087 fprintf (fguard
, "\ncase %d:\n", nrules
);
1089 fprintf (fguard
, "#line %d \"%s\"\n", lineno
, infile
);
1095 while (brace_flag
? (count
> 0) : (c
!= ';'))
1116 complain (_("unmatched %s"), "`}'");
1117 c
= getc (finput
); /* skip it */
1123 copy_string (finput
, fguard
, c
);
1129 if (c
!= '*' && c
!= '/')
1131 copy_comment (finput
, fguard
, c
);
1140 char *cp
= token_buffer
;
1142 while ((c
= getc (finput
)) != '>' && c
> 0)
1144 if (cp
== token_buffer
+ maxtoken
)
1145 cp
= grow_token_buffer (cp
);
1150 type_name
= token_buffer
;
1157 fprintf (fguard
, "yyval");
1159 type_name
= rule
->sym
->type_name
;
1161 fprintf (fguard
, ".%s", type_name
);
1162 if (!type_name
&& typed
)
1163 complain (_("$$ of `%s' has no declared type"),
1166 else if (isdigit (c
) || c
== '-')
1169 n
= read_signed_integer (finput
);
1172 if (!type_name
&& n
> 0)
1173 type_name
= get_type_name (n
, rule
);
1175 fprintf (fguard
, "yyvsp[%d]", n
- stack_offset
);
1177 fprintf (fguard
, ".%s", type_name
);
1178 if (!type_name
&& typed
)
1179 complain (_("$%d of `%s' has no declared type"),
1184 complain (_("$%s is invalid"), printable_version (c
));
1188 copy_at (finput
, fguard
, stack_offset
);
1192 fatal ("%s", _("unterminated %guard clause"));
1198 if (c
!= '}' || count
!= 0)
1202 c
= skip_white_space ();
1204 fprintf (fguard
, ";\n break;}");
1206 copy_action (rule
, stack_offset
);
1209 c
= getc (finput
); /* why not skip_white_space -wjh */
1211 copy_action (rule
, stack_offset
);
1219 record_rule_line (void)
1221 /* Record each rule's source line number in rline table. */
1223 if (nrules
>= rline_allocated
)
1225 rline_allocated
= nrules
* 2;
1226 rline
= (short *) xrealloc ((char *) rline
,
1227 rline_allocated
* sizeof (short));
1229 rline
[nrules
] = lineno
;
1233 /*-------------------------------------------------------------------.
1234 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1235 | with the user's names. |
1236 `-------------------------------------------------------------------*/
1243 sprintf (token_buffer
, "@%d", ++gensym_count
);
1244 sym
= getsym (token_buffer
);
1245 sym
->class = SNTERM
;
1246 sym
->value
= nvars
++;
1251 /*------------------------------------------------------------------.
1252 | read in a %type declaration and record its information for |
1253 | get_type_name to access. This is unused. It is only called from |
1254 | the #if 0 part of readgram |
1255 `------------------------------------------------------------------*/
1268 complain (_("invalid %s declaration"), "%type");
1272 k
= strlen (token_buffer
);
1273 name
= NEW2 (k
+ 1, char);
1274 strcpy (name
, token_buffer
);
1289 if (symval
->type_name
== NULL
)
1290 symval
->type_name
= name
;
1291 else if (strcmp (name
, symval
->type_name
) != 0)
1292 complain (_("type redeclaration for %s"), symval
->tag
);
1304 /*------------------------------------------------------------------.
1305 | Parse the input grammar into a one symbol_list structure. Each |
1306 | rule is represented by a sequence of symbols: the left hand side |
1307 | followed by the contents of the right hand side, followed by a |
1308 | null pointer instead of a symbol to terminate the rule. The next |
1309 | symbol is the lhs of the following rule. |
1311 | All guards and actions are copied out to the appropriate files, |
1312 | labelled by the rule number they apply to. |
1313 `------------------------------------------------------------------*/
1324 symbol_list
*crule
; /* points to first symbol_list of current rule. */
1325 /* its symbol is the lhs of the rule. */
1326 symbol_list
*crule1
; /* points to the symbol_list preceding crule. */
1332 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1334 if (t
== IDENTIFIER
|| t
== BAR
)
1337 int rulelength
= 0; /* number of symbols in rhs of this rule so far */
1338 int xactions
= 0; /* JF for error checking */
1339 bucket
*first_rhs
= 0;
1341 if (t
== IDENTIFIER
)
1354 complain (_("ill-formed rule: initial symbol not followed by colon"));
1359 if (nrules
== 0 && t
== BAR
)
1361 complain (_("grammar starts with vertical bar"));
1362 lhs
= symval
; /* BOGUS: use a random symval */
1364 /* start a new rule and record its lhs. */
1369 record_rule_line ();
1371 p
= NEW (symbol_list
);
1383 /* mark the rule's lhs as a nonterminal if not already so. */
1385 if (lhs
->class == SUNKNOWN
)
1387 lhs
->class = SNTERM
;
1391 else if (lhs
->class == STOKEN
)
1392 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1394 /* read the rhs of the rule. */
1402 crule
->ruleprec
= symval
;
1406 if (!(t
== IDENTIFIER
|| t
== LEFT_CURLY
))
1409 /* If next token is an identifier, see if a colon follows it.
1410 If one does, exit this rule now. */
1411 if (t
== IDENTIFIER
)
1423 if (!first_rhs
) /* JF */
1425 /* Not followed by colon =>
1426 process as part of this rule's rhs. */
1429 /* If we just passed an action, that action was in the middle
1430 of a rule, so make a dummy rule to reduce it to a
1436 /* Since the action was written out with this rule's */
1437 /* number, we must give the new rule this number */
1438 /* by inserting the new rule before it. */
1440 /* Make a dummy nonterminal, a gensym. */
1443 /* Make a new rule, whose body is empty,
1444 before the current one, so that the action
1445 just read can belong to it. */
1448 record_rule_line ();
1449 p
= NEW (symbol_list
);
1455 crule1
= NEW (symbol_list
);
1457 crule1
->next
= crule
;
1459 /* insert the dummy generated by that rule into this rule. */
1461 p
= NEW (symbol_list
);
1469 if (t
== IDENTIFIER
)
1472 p
= NEW (symbol_list
);
1477 else /* handle an action. */
1479 copy_action (crule
, rulelength
);
1481 xactions
++; /* JF */
1484 } /* end of read rhs of rule */
1486 /* Put an empty link in the list to mark the end of this rule */
1487 p
= NEW (symbol_list
);
1493 complain (_("two @prec's in a row"));
1495 crule
->ruleprec
= symval
;
1500 if (!semantic_parser
)
1503 ("%guard present but %semantic_parser not specified"));
1505 copy_guard (crule
, rulelength
);
1508 else if (t
== LEFT_CURLY
)
1510 /* This case never occurs -wjh */
1512 complain (_("two actions at end of one rule"));
1513 copy_action (crule
, rulelength
);
1515 xactions
++; /* -wjh */
1518 /* If $$ is being set in default way, report if any type
1521 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1523 if (lhs
->type_name
== 0
1524 || first_rhs
->type_name
== 0
1525 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1526 complain (_("type clash (`%s' `%s') on default action"),
1527 lhs
->type_name
? lhs
->type_name
: "",
1528 first_rhs
->type_name
? first_rhs
->type_name
: "");
1530 /* Warn if there is no default for $$ but we need one. */
1531 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1532 complain (_("empty rule for typed nonterminal, and no action"));
1537 /* these things can appear as alternatives to rules. */
1539 a) none of the documentation allows them
1540 b) most of them scan forward until finding a next %
1541 thus they may swallow lots of intervening rules
1543 else if (t
== TOKEN
)
1545 parse_token_decl (STOKEN
, SNTERM
);
1548 else if (t
== NTERM
)
1550 parse_token_decl (SNTERM
, STOKEN
);
1557 else if (t
== UNION
)
1559 parse_union_decl ();
1562 else if (t
== EXPECT
)
1564 parse_expect_decl ();
1567 else if (t
== START
)
1569 parse_start_decl ();
1576 complain (_("invalid input: %s"), token_buffer
);
1581 /* grammar has been read. Do some checking */
1583 if (nsyms
> MAXSHORT
)
1584 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1587 fatal (_("no rules in the input grammar"));
1589 if (typed
== 0 /* JF put out same default YYSTYPE as YACC does */
1590 && !value_components_used
)
1592 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1593 but it seems better to be consistent.
1594 Most programs should declare their own type anyway. */
1595 fprintf (fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1597 fprintf (fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1600 /* Report any undefined symbols and consider them nonterminals. */
1602 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1603 if (bp
->class == SUNKNOWN
)
1606 ("symbol %s is used, but is not defined as a token and has no rules"),
1609 bp
->value
= nvars
++;
1612 ntokens
= nsyms
- nvars
;
1615 /*--------------------------------------------------------------.
1616 | For named tokens, but not literal ones, define the name. The |
1617 | value is the user token number. |
1618 `--------------------------------------------------------------*/
1621 output_token_defines (FILE *file
)
1627 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1629 symbol
= bp
->tag
; /* get symbol */
1631 if (bp
->value
>= ntokens
)
1633 if (bp
->user_token_number
== SALIAS
)
1635 if ('\'' == *symbol
)
1636 continue; /* skip literal character */
1638 continue; /* skip error token */
1639 if ('\"' == *symbol
)
1641 /* use literal string only if given a symbol with an alias */
1643 symbol
= bp
->alias
->tag
;
1648 /* Don't #define nonliteral tokens whose names contain periods. */
1650 while ((c
= *cp
++) && c
!= '.');
1654 fprintf (file
, "#define\t%s\t%d\n", symbol
,
1655 ((translations
&& !rawtoknumflag
)
1656 ? bp
->user_token_number
: bp
->value
));
1657 if (semantic_parser
)
1658 fprintf (file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1665 /*------------------------------------------------------------------.
1666 | Assign symbol numbers, and write definition of token names into |
1667 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1669 `------------------------------------------------------------------*/
1677 int last_user_token_number
;
1678 static char DOLLAR
[] = "$";
1680 /* int lossage = 0; JF set but not used */
1682 tags
= NEW2 (nsyms
+ 1, char *);
1684 user_toknums
= NEW2 (nsyms
+ 1, short);
1685 user_toknums
[0] = 0;
1687 sprec
= NEW2 (nsyms
, short);
1688 sassoc
= NEW2 (nsyms
, short);
1690 max_user_token_number
= 256;
1691 last_user_token_number
= 256;
1693 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1695 if (bp
->class == SNTERM
)
1697 bp
->value
+= ntokens
;
1701 /* this symbol and its alias are a single token defn.
1702 allocate a tokno, and assign to both check agreement of
1703 ->prec and ->assoc fields and make both the same */
1705 bp
->value
= bp
->alias
->value
= tokno
++;
1707 if (bp
->prec
!= bp
->alias
->prec
)
1709 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1710 && bp
->user_token_number
== SALIAS
)
1711 complain (_("conflicting precedences for %s and %s"),
1712 bp
->tag
, bp
->alias
->tag
);
1714 bp
->alias
->prec
= bp
->prec
;
1716 bp
->prec
= bp
->alias
->prec
;
1719 if (bp
->assoc
!= bp
->alias
->assoc
)
1721 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1722 && bp
->user_token_number
== SALIAS
)
1723 complain (_("conflicting assoc values for %s and %s"),
1724 bp
->tag
, bp
->alias
->tag
);
1726 bp
->alias
->assoc
= bp
->assoc
;
1728 bp
->assoc
= bp
->alias
->assoc
;
1731 if (bp
->user_token_number
== SALIAS
)
1732 continue; /* do not do processing below for SALIASs */
1735 else /* bp->class == STOKEN */
1737 bp
->value
= tokno
++;
1740 if (bp
->class == STOKEN
)
1742 if (translations
&& !(bp
->user_token_number
))
1743 bp
->user_token_number
= ++last_user_token_number
;
1744 if (bp
->user_token_number
> max_user_token_number
)
1745 max_user_token_number
= bp
->user_token_number
;
1748 tags
[bp
->value
] = bp
->tag
;
1749 user_toknums
[bp
->value
] = bp
->user_token_number
;
1750 sprec
[bp
->value
] = bp
->prec
;
1751 sassoc
[bp
->value
] = bp
->assoc
;
1759 token_translations
= NEW2 (max_user_token_number
+ 1, short);
1761 /* initialize all entries for literal tokens to 2, the internal
1762 token number for $undefined., which represents all invalid
1764 for (j
= 0; j
<= max_user_token_number
; j
++)
1765 token_translations
[j
] = 2;
1767 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1769 if (bp
->value
>= ntokens
)
1770 continue; /* non-terminal */
1771 if (bp
->user_token_number
== SALIAS
)
1773 if (token_translations
[bp
->user_token_number
] != 2)
1774 complain (_("tokens %s and %s both assigned number %d"),
1775 tags
[token_translations
[bp
->user_token_number
]],
1776 bp
->tag
, bp
->user_token_number
);
1777 token_translations
[bp
->user_token_number
] = bp
->value
;
1781 error_token_number
= errtoken
->value
;
1784 output_token_defines (ftable
);
1786 if (startval
->class == SUNKNOWN
)
1787 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1788 else if (startval
->class == STOKEN
)
1789 fatal (_("the start symbol %s is a token"), startval
->tag
);
1791 start_symbol
= startval
->value
;
1795 output_token_defines (fdefines
);
1799 if (spec_name_prefix
)
1800 fprintf (fdefines
, "\nextern YYSTYPE %slval;\n",
1803 fprintf (fdefines
, "\nextern YYSTYPE yylval;\n");
1806 if (semantic_parser
)
1807 for (i
= ntokens
; i
< nsyms
; i
++)
1809 /* don't make these for dummy nonterminals made by gensym. */
1810 if (*tags
[i
] != '@')
1811 fprintf (fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1814 /* `fdefines' is now a temporary file, so we need to copy its
1815 contents in `done', so we can't close it here. */
1823 /*---------------------------------------------------------------.
1824 | Convert the rules into the representation using RRHS, RLHS and |
1826 `---------------------------------------------------------------*/
1837 ritem
= NEW2 (nitems
+ 1, short);
1838 rlhs
= NEW2 (nrules
, short) - 1;
1839 rrhs
= NEW2 (nrules
, short) - 1;
1840 rprec
= NEW2 (nrules
, short) - 1;
1841 rprecsym
= NEW2 (nrules
, short) - 1;
1842 rassoc
= NEW2 (nrules
, short) - 1;
1850 rlhs
[ruleno
] = p
->sym
->value
;
1851 rrhs
[ruleno
] = itemno
;
1852 ruleprec
= p
->ruleprec
;
1857 ritem
[itemno
++] = p
->sym
->value
;
1858 /* A rule gets by default the precedence and associativity
1859 of the last token in it. */
1860 if (p
->sym
->class == STOKEN
)
1862 rprec
[ruleno
] = p
->sym
->prec
;
1863 rassoc
[ruleno
] = p
->sym
->assoc
;
1869 /* If this rule has a %prec,
1870 the specified symbol's precedence replaces the default. */
1873 rprec
[ruleno
] = ruleprec
->prec
;
1874 rassoc
[ruleno
] = ruleprec
->assoc
;
1875 rprecsym
[ruleno
] = ruleprec
->value
;
1878 ritem
[itemno
++] = -ruleno
;
1888 /*-------------------------------------------------------------------.
1889 | Read in the grammar specification and record it in the format |
1890 | described in gram.h. All guards are copied into the FGUARD file |
1891 | and all actions into FACTION, in each case forming the body of a C |
1892 | function (YYGUARD or YYACTION) which contains a switch statement |
1893 | to decide which guard or action to execute. |
1894 `-------------------------------------------------------------------*/
1900 startval
= NULL
; /* start symbol not specified yet. */
1903 /* initially assume token number translation not needed. */
1906 /* Nowadays translations is always set to 1, since we give `error' a
1907 user-token-number to satisfy the Posix demand for YYERRCODE==256.
1915 rline_allocated
= 10;
1916 rline
= NEW2 (rline_allocated
, short);
1923 semantic_parser
= 0;
1932 /* Initialize the symbol table. */
1934 /* Construct the error token */
1935 errtoken
= getsym ("error");
1936 errtoken
->class = STOKEN
;
1937 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1938 /* Construct a token that represents all undefined literal tokens.
1939 It is always token number 2. */
1940 undeftoken
= getsym ("$undefined.");
1941 undeftoken
->class = STOKEN
;
1942 undeftoken
->user_token_number
= 2;
1944 /* Read the declaration section. Copy %{ ... %} groups to FTABLE
1945 and FDEFINES file. Also notice any %token, %left, etc. found
1947 putc ('\n', ftable
);
1949 /* %s, made from %s\n\
1950 by GNU bison %s. */\n\
1951 \n", noparserflag
? "Bison-generated parse tables" : "A Bison parser", infile
, VERSION
);
1953 fputs ("#define YYBISON 1 /* Identify Bison output. */\n\n", ftable
);
1954 read_declarations ();
1955 /* Start writing the guard and action files, if they are needed. */
1957 /* Read in the grammar, build grammar in list form. Write out
1958 guards and actions. */
1960 /* Now we know whether we need the line-number stack. If we do,
1961 write its type into the .tab.h file. */
1963 reader_output_yylsp (fdefines
);
1964 /* Write closing delimiters for actions and guards. */
1967 fputs ("#define YYLSP_NEEDED\n\n", ftable
);
1968 /* Assign the symbols their symbol numbers. Write #defines for the
1969 token symbols into FDEFINES if requested. */
1971 /* Convert the grammar into the format described in gram.h. */
1973 /* Free the symbol table data structure since symbols are now all
1974 referred to by symbol number. */
1979 reader_output_yylsp (FILE *f
)
1982 fprintf (f
, LTYPESTR
);