]>
git.saurik.com Git - bison.git/blob - src/reader.c
4309cf879999a6382b8ccb265176954543d5ac98
1 /* Input parser for bison
2 Copyright (C) 1984, 1986, 1989, 1992, 1998, 2000
3 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
7 Bison is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 Bison is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
33 #include "conflicts.h"
35 extern char *printable_version
PARAMS ((int));
37 /* Number of slots allocated (but not necessarily used yet) in `rline' */
38 static int rline_allocated
;
40 typedef struct symbol_list
42 struct symbol_list
*next
;
51 static symbol_list
*grammar
;
52 static int start_flag
;
53 static bucket
*startval
;
55 /* Nonzero if components of semantic values are used, implying
56 they must be unions. */
57 static int value_components_used
;
59 /* Nonzero if %union has been seen. */
62 /* Incremented for each %left, %right or %nonassoc seen */
65 /* Incremented for each generated symbol */
66 static int gensym_count
;
68 static bucket
*errtoken
;
69 static bucket
*undeftoken
;
72 /*===================\
74 \===================*/
77 skip_to_char (int target
)
81 complain (_(" Skipping to next \\n"));
83 complain (_(" Skipping to next %c"), target
);
86 c
= skip_white_space ();
87 while (c
!= target
&& c
!= EOF
);
93 /*---------------------------------------------------------.
94 | Read a signed integer from STREAM and return its value. |
95 `---------------------------------------------------------*/
98 read_signed_integer (FILE *stream
)
100 int c
= getc (stream
);
112 n
= 10 * n
+ (c
- '0');
121 /*-------------------------------------------------------------------.
122 | Dump the string from FINPUT to FOUTPUT. MATCH is the delimiter of |
123 | the string (either ' or "). |
124 `-------------------------------------------------------------------*/
127 copy_string (FILE *fin
, FILE *fout
, int match
)
137 fatal (_("unterminated string at end of file"));
140 complain (_("unterminated string"));
142 c
= match
; /* invent terminator */
152 fatal (_("unterminated string at end of file"));
165 /*---------------------------------------------------------------.
166 | Dump the comment from IN to OUT1 and OUT2. C is either `*' or |
167 | `/', depending upon the type of comments used. OUT2 might be |
169 `---------------------------------------------------------------*/
172 copy_comment2 (FILE *in
, FILE *out1
, FILE *out2
, int c
)
177 cplus_comment
= (c
== '/');
186 if (!cplus_comment
&& c
== '*')
216 fatal (_("unterminated comment"));
228 /*------------------------------------------------------------.
229 | Dump the comment from FIN to FOUT. C is either `*' or `/', |
230 | depending upon the type of comments used. |
231 `------------------------------------------------------------*/
234 copy_comment (FILE *fin
, FILE *fout
, int c
)
236 copy_comment2 (fin
, fout
, NULL
, c
);
240 /*-----------------------------------------------------------------.
241 | FIN is pointing to a location (i.e., a `@'). Output to FOUT a |
242 | reference to this location. STACK_OFFSET is the number of values |
243 | in the current rule so far, which says where to find `$0' with |
244 | respect to the top of the stack. |
245 `-----------------------------------------------------------------*/
248 copy_at (FILE *fin
, FILE *fout
, int stack_offset
)
255 fprintf (fout
, "yyloc");
258 else if (isdigit (c
) || c
== '-')
263 n
= read_signed_integer (fin
);
265 fprintf (fout
, "yylsp[%d]", n
- stack_offset
);
269 complain (_("@%s is invalid"), printable_version (c
));
272 /*-------------------------------------------------------------------.
273 | Copy the contents of a `%{ ... %}' into the definitions file. The |
274 | `%{' has already been read. Return after reading the `%}'. |
275 `-------------------------------------------------------------------*/
278 copy_definition (void)
281 /* -1 while reading a character if prev char was %. */
285 fprintf (fattrs
, "#line %d \"%s\"\n", lineno
, infile
);
306 copy_string (finput
, fattrs
, c
);
312 if (c
!= '*' && c
!= '/')
314 copy_comment (finput
, fattrs
, c
);
318 fatal ("%s", _("unterminated `%{' definition"));
339 /*-------------------------------------------------------------------.
340 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
341 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
343 `-------------------------------------------------------------------*/
346 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
350 struct bucket
*symbol
= NULL
; /* pts to symbol being defined */
354 int tmp_char
= ungetc (skip_white_space (), finput
);
359 fatal (_("Premature EOF after %s"), token_buffer
);
367 if (token
== TYPENAME
)
369 typename
= xstrdup (token_buffer
);
370 value_components_used
= 1;
373 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"' && symbol
)
376 warn (_("symbol `%s' used more than once as a literal string"),
378 else if (symbol
->alias
)
379 warn (_("symbol `%s' given more than one literal string"),
383 symval
->class = token_sym
;
384 symval
->type_name
= typename
;
385 symval
->user_token_number
= symbol
->user_token_number
;
386 symbol
->user_token_number
= SALIAS
;
387 symval
->alias
= symbol
;
388 symbol
->alias
= symval
;
389 /* symbol and symval combined are only one symbol */
395 else if (token
== IDENTIFIER
)
397 int oldclass
= symval
->class;
400 if (symbol
->class == what_is_not
)
401 complain (_("symbol %s redefined"), symbol
->tag
);
402 symbol
->class = what_is
;
403 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
404 symbol
->value
= nvars
++;
408 if (symbol
->type_name
== NULL
)
409 symbol
->type_name
= typename
;
410 else if (strcmp (typename
, symbol
->type_name
) != 0)
411 complain (_("type redeclaration for %s"), symbol
->tag
);
414 else if (symbol
&& token
== NUMBER
)
416 symbol
->user_token_number
= numval
;
421 complain (_("`%s' is invalid in %s"),
422 token_buffer
, (what_is
== token_sym
) ? "%token" : "%nterm");
430 /*------------------------------.
431 | Parse what comes after %start |
432 `------------------------------*/
435 parse_start_decl (void)
438 complain (_("multiple %s declarations"), "%start");
439 if (lex () != IDENTIFIER
)
440 complain (_("invalid %s declaration"), "%start");
450 /*--------------------------------------------------------------.
451 | Get the data type (alternative in the union) of the value for |
452 | symbol n in rule rule. |
453 `--------------------------------------------------------------*/
456 get_type_name (int n
, symbol_list
* rule
)
463 complain (_("invalid $ value"));
473 if (rp
== NULL
|| rp
->sym
== NULL
)
475 complain (_("invalid $ value"));
481 return rp
->sym
->type_name
;
485 /*-----------------------------------------------------------.
486 | read in a %type declaration and record its information for |
487 | get_type_name to access |
488 `-----------------------------------------------------------*/
491 parse_type_decl (void)
495 if (lex () != TYPENAME
)
497 complain ("%s", _("%type declaration has no <typename>"));
502 name
= xstrdup (token_buffer
);
507 int tmp_char
= ungetc (skip_white_space (), finput
);
512 fatal (_("Premature EOF after %s"), token_buffer
);
524 if (symval
->type_name
== NULL
)
525 symval
->type_name
= name
;
526 else if (strcmp (name
, symval
->type_name
) != 0)
527 complain (_("type redeclaration for %s"), symval
->tag
);
532 complain (_("invalid %%type declaration due to item: %s"),
541 /*----------------------------------------------------------------.
542 | Read in a %left, %right or %nonassoc declaration and record its |
544 `----------------------------------------------------------------*/
547 parse_assoc_decl (associativity assoc
)
552 lastprec
++; /* Assign a new precedence level, never 0. */
557 int tmp_char
= ungetc (skip_white_space (), finput
);
562 fatal (_("Premature EOF after %s"), token_buffer
);
569 name
= xstrdup (token_buffer
);
576 if (symval
->prec
!= 0)
577 complain (_("redefining precedence of %s"), symval
->tag
);
578 symval
->prec
= lastprec
;
579 symval
->assoc
= assoc
;
580 if (symval
->class == nterm_sym
)
581 complain (_("symbol %s redefined"), symval
->tag
);
582 symval
->class = token_sym
;
584 { /* record the type, if one is specified */
585 if (symval
->type_name
== NULL
)
586 symval
->type_name
= name
;
587 else if (strcmp (name
, symval
->type_name
) != 0)
588 complain (_("type redeclaration for %s"), symval
->tag
);
593 if (prev
== IDENTIFIER
)
595 symval
->user_token_number
= numval
;
601 ("invalid text (%s) - number should be after identifier"),
611 complain (_("unexpected item: %s"), token_buffer
);
622 /*-------------------------------------------------------------------.
623 | Copy the union declaration into fattrs (and fdefines), where it is |
624 | made into the definition of YYSTYPE, the type of elements of the |
625 | parser value stack. |
626 `-------------------------------------------------------------------*/
629 parse_union_decl (void)
635 complain (_("multiple %s declarations"), "%union");
640 fprintf (fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
642 fprintf (fattrs
, "\n");
644 fprintf (fattrs
, "typedef union");
646 fprintf (fdefines
, "typedef union");
664 if (c
!= '*' && c
!= '/')
666 copy_comment2 (finput
, fattrs
, fdefines
, c
);
676 complain (_("unmatched %s"), "`}'");
680 fprintf (fattrs
, " YYSTYPE;\n");
682 fprintf (fdefines
, " YYSTYPE;\n");
683 /* JF don't choke on trailing semi */
684 c
= skip_white_space ();
696 /*-------------------------------------------------------.
697 | Parse the declaration %expect N which says to expect N |
698 | shift-reduce conflicts. |
699 `-------------------------------------------------------*/
702 parse_expect_decl (void)
709 while (c
== ' ' || c
== '\t')
713 while (c
>= '0' && c
<= '9')
715 if (count
< sizeof(buffer
) - 1)
723 if (count
<= 0 || count
> 10)
724 complain ("%s", _("argument of %expect is not an integer"));
725 expected_conflicts
= atoi (buffer
);
729 /*-------------------------------------------------------------------.
730 | Parse what comes after %thong. the full syntax is |
732 | %thong <type> token number literal |
734 | the <type> or number may be omitted. The number specifies the |
735 | user_token_number. |
737 | Two symbols are entered in the table, one for the token symbol and |
738 | one for the literal. Both are given the <type>, if any, from the |
739 | declaration. The ->user_token_number of the first is SALIAS and |
740 | the ->user_token_number of the second is set to the number, if |
741 | any, from the declaration. The two symbols are linked via |
742 | pointers in their ->alias fields. |
744 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
745 | only the literal string is retained it is the literal string that |
746 | is output to yytname |
747 `-------------------------------------------------------------------*/
750 parse_thong_decl (void)
753 struct bucket
*symbol
;
758 token
= lex (); /* fetch typename or first token */
759 if (token
== TYPENAME
)
761 typename
= xstrdup (token_buffer
);
762 value_components_used
= 1;
763 token
= lex (); /* fetch first token */
766 /* process first token */
768 if (token
!= IDENTIFIER
)
770 complain (_("unrecognized item %s, expected an identifier"),
775 symval
->class = token_sym
;
776 symval
->type_name
= typename
;
777 symval
->user_token_number
= SALIAS
;
780 token
= lex (); /* get number or literal string */
785 token
= lex (); /* okay, did number, now get literal */
790 /* process literal string token */
792 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
794 complain (_("expected string constant instead of %s"), token_buffer
);
798 symval
->class = token_sym
;
799 symval
->type_name
= typename
;
800 symval
->user_token_number
= usrtoknum
;
802 symval
->alias
= symbol
;
803 symbol
->alias
= symval
;
805 nsyms
--; /* symbol and symval combined are only one symbol */
809 /*----------------------------------------------------------------.
810 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
811 | any `%' declarations, and copy the contents of any `%{ ... %}' |
812 | groups to fattrs. |
813 `----------------------------------------------------------------*/
816 read_declarations (void)
823 c
= skip_white_space ();
827 tok
= parse_percent_token ();
834 case PERCENT_LEFT_CURLY
:
839 parse_token_decl (token_sym
, nterm_sym
);
843 parse_token_decl (nterm_sym
, token_sym
);
859 parse_expect_decl ();
866 parse_assoc_decl (left_assoc
);
870 parse_assoc_decl (right_assoc
);
874 parse_assoc_decl (non_assoc
);
877 case SEMANTIC_PARSER
:
878 if (semantic_parser
== 0)
893 complain (_("unrecognized: %s"), token_buffer
);
898 fatal (_("no input grammar"));
901 complain (_("unknown character: %s"), printable_version (c
));
907 /*-------------------------------------------------------------------.
908 | Assuming that a `{' has just been seen, copy everything up to the |
909 | matching `}' into the actions file. STACK_OFFSET is the number of |
910 | values in the current rule so far, which says where to find `$0' |
911 | with respect to the top of the stack. |
912 `-------------------------------------------------------------------*/
915 copy_action (symbol_list
* rule
, int stack_offset
)
922 /* offset is always 0 if parser has already popped the stack pointer */
926 fprintf (faction
, "\ncase %d:\n", nrules
);
928 fprintf (faction
, "#line %d \"%s\"\n", lineno
, infile
);
952 copy_string (finput
, faction
, c
);
958 if (c
!= '*' && c
!= '/')
960 copy_comment (finput
, faction
, c
);
969 char *cp
= token_buffer
;
971 while ((c
= getc (finput
)) != '>' && c
> 0)
973 if (cp
== token_buffer
+ maxtoken
)
974 cp
= grow_token_buffer (cp
);
979 type_name
= token_buffer
;
980 value_components_used
= 1;
986 fprintf (faction
, "yyval");
988 type_name
= get_type_name (0, rule
);
990 fprintf (faction
, ".%s", type_name
);
991 if (!type_name
&& typed
)
992 complain (_("$$ of `%s' has no declared type"),
995 else if (isdigit (c
) || c
== '-')
998 n
= read_signed_integer (finput
);
1001 if (!type_name
&& n
> 0)
1002 type_name
= get_type_name (n
, rule
);
1004 fprintf (faction
, "yyvsp[%d]", n
- stack_offset
);
1006 fprintf (faction
, ".%s", type_name
);
1007 if (!type_name
&& typed
)
1008 complain (_("$%d of `%s' has no declared type"),
1013 complain (_("$%s is invalid"), printable_version (c
));
1018 copy_at (finput
, faction
, stack_offset
);
1022 fatal (_("unmatched %s"), "`{'");
1031 /* above loop exits when c is '}' */
1040 fprintf (faction
, ";\n break;}");
1043 /*-------------------------------------------------------------------.
1044 | After `%guard' is seen in the input file, copy the actual guard |
1045 | into the guards file. If the guard is followed by an action, copy |
1046 | that into the actions file. STACK_OFFSET is the number of values |
1047 | in the current rule so far, which says where to find `$0' with |
1048 | respect to the top of the stack, for the simple parser in which |
1049 | the stack is not popped until after the guard is run. |
1050 `-------------------------------------------------------------------*/
1053 copy_guard (symbol_list
* rule
, int stack_offset
)
1061 /* offset is always 0 if parser has already popped the stack pointer */
1062 if (semantic_parser
)
1065 fprintf (fguard
, "\ncase %d:\n", nrules
);
1067 fprintf (fguard
, "#line %d \"%s\"\n", lineno
, infile
);
1073 while (brace_flag
? (count
> 0) : (c
!= ';'))
1094 complain (_("unmatched %s"), "`}'");
1095 c
= getc (finput
); /* skip it */
1101 copy_string (finput
, fguard
, c
);
1107 if (c
!= '*' && c
!= '/')
1109 copy_comment (finput
, fguard
, c
);
1118 char *cp
= token_buffer
;
1120 while ((c
= getc (finput
)) != '>' && c
> 0)
1122 if (cp
== token_buffer
+ maxtoken
)
1123 cp
= grow_token_buffer (cp
);
1128 type_name
= token_buffer
;
1135 fprintf (fguard
, "yyval");
1137 type_name
= rule
->sym
->type_name
;
1139 fprintf (fguard
, ".%s", type_name
);
1140 if (!type_name
&& typed
)
1141 complain (_("$$ of `%s' has no declared type"),
1144 else if (isdigit (c
) || c
== '-')
1147 n
= read_signed_integer (finput
);
1150 if (!type_name
&& n
> 0)
1151 type_name
= get_type_name (n
, rule
);
1153 fprintf (fguard
, "yyvsp[%d]", n
- stack_offset
);
1155 fprintf (fguard
, ".%s", type_name
);
1156 if (!type_name
&& typed
)
1157 complain (_("$%d of `%s' has no declared type"),
1162 complain (_("$%s is invalid"), printable_version (c
));
1166 copy_at (finput
, fguard
, stack_offset
);
1170 fatal ("%s", _("unterminated %guard clause"));
1176 if (c
!= '}' || count
!= 0)
1180 c
= skip_white_space ();
1182 fprintf (fguard
, ";\n break;}");
1184 copy_action (rule
, stack_offset
);
1187 c
= getc (finput
); /* why not skip_white_space -wjh */
1189 copy_action (rule
, stack_offset
);
1197 record_rule_line (void)
1199 /* Record each rule's source line number in rline table. */
1201 if (nrules
>= rline_allocated
)
1203 rline_allocated
= nrules
* 2;
1204 rline
= XREALLOC (rline
, short, rline_allocated
);
1206 rline
[nrules
] = lineno
;
1210 /*-------------------------------------------------------------------.
1211 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1212 | with the user's names. |
1213 `-------------------------------------------------------------------*/
1220 sprintf (token_buffer
, "@%d", ++gensym_count
);
1221 sym
= getsym (token_buffer
);
1222 sym
->class = nterm_sym
;
1223 sym
->value
= nvars
++;
1228 /*------------------------------------------------------------------.
1229 | read in a %type declaration and record its information for |
1230 | get_type_name to access. This is unused. It is only called from |
1231 | the #if 0 part of readgram |
1232 `------------------------------------------------------------------*/
1245 complain (_("invalid %s declaration"), "%type");
1249 name
= xstrdup (token_buffer
);
1264 if (symval
->type_name
== NULL
)
1265 symval
->type_name
= name
;
1266 else if (strcmp (name
, symval
->type_name
) != 0)
1267 complain (_("type redeclaration for %s"), symval
->tag
);
1279 /*------------------------------------------------------------------.
1280 | Parse the input grammar into a one symbol_list structure. Each |
1281 | rule is represented by a sequence of symbols: the left hand side |
1282 | followed by the contents of the right hand side, followed by a |
1283 | null pointer instead of a symbol to terminate the rule. The next |
1284 | symbol is the lhs of the following rule. |
1286 | All guards and actions are copied out to the appropriate files, |
1287 | labelled by the rule number they apply to. |
1288 `------------------------------------------------------------------*/
1299 symbol_list
*crule
; /* points to first symbol_list of current rule. */
1300 /* its symbol is the lhs of the rule. */
1301 symbol_list
*crule1
; /* points to the symbol_list preceding crule. */
1307 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1309 if (t
== IDENTIFIER
|| t
== BAR
)
1311 int action_flag
= 0;
1312 int rulelength
= 0; /* number of symbols in rhs of this rule so far */
1313 int xactions
= 0; /* JF for error checking */
1314 bucket
*first_rhs
= 0;
1316 if (t
== IDENTIFIER
)
1329 complain (_("ill-formed rule: initial symbol not followed by colon"));
1334 if (nrules
== 0 && t
== BAR
)
1336 complain (_("grammar starts with vertical bar"));
1337 lhs
= symval
; /* BOGUS: use a random symval */
1339 /* start a new rule and record its lhs. */
1344 record_rule_line ();
1346 p
= XCALLOC (symbol_list
, 1);
1358 /* mark the rule's lhs as a nonterminal if not already so. */
1360 if (lhs
->class == unknown_sym
)
1362 lhs
->class = nterm_sym
;
1366 else if (lhs
->class == token_sym
)
1367 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1369 /* read the rhs of the rule. */
1377 crule
->ruleprec
= symval
;
1381 if (!(t
== IDENTIFIER
|| t
== LEFT_CURLY
))
1384 /* If next token is an identifier, see if a colon follows it.
1385 If one does, exit this rule now. */
1386 if (t
== IDENTIFIER
)
1398 if (!first_rhs
) /* JF */
1400 /* Not followed by colon =>
1401 process as part of this rule's rhs. */
1404 /* If we just passed an action, that action was in the middle
1405 of a rule, so make a dummy rule to reduce it to a
1411 /* Since the action was written out with this rule's */
1412 /* number, we must give the new rule this number */
1413 /* by inserting the new rule before it. */
1415 /* Make a dummy nonterminal, a gensym. */
1418 /* Make a new rule, whose body is empty,
1419 before the current one, so that the action
1420 just read can belong to it. */
1423 record_rule_line ();
1424 p
= XCALLOC (symbol_list
, 1);
1430 crule1
= XCALLOC (symbol_list
, 1);
1432 crule1
->next
= crule
;
1434 /* insert the dummy generated by that rule into this rule. */
1436 p
= XCALLOC (symbol_list
, 1);
1444 if (t
== IDENTIFIER
)
1447 p
= XCALLOC (symbol_list
, 1);
1452 else /* handle an action. */
1454 copy_action (crule
, rulelength
);
1456 xactions
++; /* JF */
1459 } /* end of read rhs of rule */
1461 /* Put an empty link in the list to mark the end of this rule */
1462 p
= XCALLOC (symbol_list
, 1);
1468 complain (_("two @prec's in a row"));
1470 crule
->ruleprec
= symval
;
1475 if (!semantic_parser
)
1478 ("%guard present but %semantic_parser not specified"));
1480 copy_guard (crule
, rulelength
);
1483 else if (t
== LEFT_CURLY
)
1485 /* This case never occurs -wjh */
1487 complain (_("two actions at end of one rule"));
1488 copy_action (crule
, rulelength
);
1490 xactions
++; /* -wjh */
1493 /* If $$ is being set in default way, report if any type
1496 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1498 if (lhs
->type_name
== 0
1499 || first_rhs
->type_name
== 0
1500 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1501 complain (_("type clash (`%s' `%s') on default action"),
1502 lhs
->type_name
? lhs
->type_name
: "",
1503 first_rhs
->type_name
? first_rhs
->type_name
: "");
1505 /* Warn if there is no default for $$ but we need one. */
1506 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1507 complain (_("empty rule for typed nonterminal, and no action"));
1512 /* these things can appear as alternatives to rules. */
1514 a) none of the documentation allows them
1515 b) most of them scan forward until finding a next %
1516 thus they may swallow lots of intervening rules
1518 else if (t
== TOKEN
)
1520 parse_token_decl (token_sym
, nterm_sym
);
1523 else if (t
== NTERM
)
1525 parse_token_decl (nterm_sym
, token_sym
);
1532 else if (t
== UNION
)
1534 parse_union_decl ();
1537 else if (t
== EXPECT
)
1539 parse_expect_decl ();
1542 else if (t
== START
)
1544 parse_start_decl ();
1551 complain (_("invalid input: %s"), token_buffer
);
1556 /* grammar has been read. Do some checking */
1558 if (nsyms
> MAXSHORT
)
1559 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1562 fatal (_("no rules in the input grammar"));
1564 if (typed
== 0 /* JF put out same default YYSTYPE as YACC does */
1565 && !value_components_used
)
1567 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1568 but it seems better to be consistent.
1569 Most programs should declare their own type anyway. */
1570 fprintf (fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1572 fprintf (fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1575 /* Report any undefined symbols and consider them nonterminals. */
1577 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1578 if (bp
->class == unknown_sym
)
1581 ("symbol %s is used, but is not defined as a token and has no rules"),
1583 bp
->class = nterm_sym
;
1584 bp
->value
= nvars
++;
1587 ntokens
= nsyms
- nvars
;
1590 /*--------------------------------------------------------------.
1591 | For named tokens, but not literal ones, define the name. The |
1592 | value is the user token number. |
1593 `--------------------------------------------------------------*/
1596 output_token_defines (FILE *file
)
1602 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1604 symbol
= bp
->tag
; /* get symbol */
1606 if (bp
->value
>= ntokens
)
1608 if (bp
->user_token_number
== SALIAS
)
1610 if ('\'' == *symbol
)
1611 continue; /* skip literal character */
1613 continue; /* skip error token */
1614 if ('\"' == *symbol
)
1616 /* use literal string only if given a symbol with an alias */
1618 symbol
= bp
->alias
->tag
;
1623 /* Don't #define nonliteral tokens whose names contain periods. */
1625 while ((c
= *cp
++) && c
!= '.');
1629 fprintf (file
, "#define\t%s\t%d\n", symbol
,
1630 ((translations
&& !raw_flag
)
1631 ? bp
->user_token_number
: bp
->value
));
1632 if (semantic_parser
)
1633 fprintf (file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1640 /*------------------------------------------------------------------.
1641 | Assign symbol numbers, and write definition of token names into |
1642 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1644 `------------------------------------------------------------------*/
1652 int last_user_token_number
;
1653 static char DOLLAR
[] = "$";
1655 /* int lossage = 0; JF set but not used */
1657 tags
= XCALLOC (char *, nsyms
+ 1);
1659 user_toknums
= XCALLOC (short, nsyms
+ 1);
1660 user_toknums
[0] = 0;
1662 sprec
= XCALLOC (short, nsyms
);
1663 sassoc
= XCALLOC (short, nsyms
);
1665 max_user_token_number
= 256;
1666 last_user_token_number
= 256;
1668 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1670 if (bp
->class == nterm_sym
)
1672 bp
->value
+= ntokens
;
1676 /* this symbol and its alias are a single token defn.
1677 allocate a tokno, and assign to both check agreement of
1678 ->prec and ->assoc fields and make both the same */
1680 bp
->value
= bp
->alias
->value
= tokno
++;
1682 if (bp
->prec
!= bp
->alias
->prec
)
1684 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1685 && bp
->user_token_number
== SALIAS
)
1686 complain (_("conflicting precedences for %s and %s"),
1687 bp
->tag
, bp
->alias
->tag
);
1689 bp
->alias
->prec
= bp
->prec
;
1691 bp
->prec
= bp
->alias
->prec
;
1694 if (bp
->assoc
!= bp
->alias
->assoc
)
1696 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1697 && bp
->user_token_number
== SALIAS
)
1698 complain (_("conflicting assoc values for %s and %s"),
1699 bp
->tag
, bp
->alias
->tag
);
1701 bp
->alias
->assoc
= bp
->assoc
;
1703 bp
->assoc
= bp
->alias
->assoc
;
1706 if (bp
->user_token_number
== SALIAS
)
1707 continue; /* do not do processing below for SALIASs */
1710 else /* bp->class == token_sym */
1712 bp
->value
= tokno
++;
1715 if (bp
->class == token_sym
)
1717 if (translations
&& !(bp
->user_token_number
))
1718 bp
->user_token_number
= ++last_user_token_number
;
1719 if (bp
->user_token_number
> max_user_token_number
)
1720 max_user_token_number
= bp
->user_token_number
;
1723 tags
[bp
->value
] = bp
->tag
;
1724 user_toknums
[bp
->value
] = bp
->user_token_number
;
1725 sprec
[bp
->value
] = bp
->prec
;
1726 sassoc
[bp
->value
] = bp
->assoc
;
1734 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1736 /* initialize all entries for literal tokens to 2, the internal
1737 token number for $undefined., which represents all invalid
1739 for (j
= 0; j
<= max_user_token_number
; j
++)
1740 token_translations
[j
] = 2;
1742 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1744 if (bp
->value
>= ntokens
)
1745 continue; /* non-terminal */
1746 if (bp
->user_token_number
== SALIAS
)
1748 if (token_translations
[bp
->user_token_number
] != 2)
1749 complain (_("tokens %s and %s both assigned number %d"),
1750 tags
[token_translations
[bp
->user_token_number
]],
1751 bp
->tag
, bp
->user_token_number
);
1752 token_translations
[bp
->user_token_number
] = bp
->value
;
1756 error_token_number
= errtoken
->value
;
1758 if (!no_parser_flag
)
1759 output_token_defines (ftable
);
1761 if (startval
->class == unknown_sym
)
1762 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1763 else if (startval
->class == token_sym
)
1764 fatal (_("the start symbol %s is a token"), startval
->tag
);
1766 start_symbol
= startval
->value
;
1770 output_token_defines (fdefines
);
1774 if (spec_name_prefix
)
1775 fprintf (fdefines
, "\nextern YYSTYPE %slval;\n",
1778 fprintf (fdefines
, "\nextern YYSTYPE yylval;\n");
1781 if (semantic_parser
)
1782 for (i
= ntokens
; i
< nsyms
; i
++)
1784 /* don't make these for dummy nonterminals made by gensym. */
1785 if (*tags
[i
] != '@')
1786 fprintf (fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1789 /* `fdefines' is now a temporary file, so we need to copy its
1790 contents in `done', so we can't close it here. */
1798 /*---------------------------------------------------------------.
1799 | Convert the rules into the representation using RRHS, RLHS and |
1801 `---------------------------------------------------------------*/
1812 ritem
= XCALLOC (short, nitems
+ 1);
1813 rlhs
= XCALLOC (short, nrules
) - 1;
1814 rrhs
= XCALLOC (short, nrules
) - 1;
1815 rprec
= XCALLOC (short, nrules
) - 1;
1816 rprecsym
= XCALLOC (short, nrules
) - 1;
1817 rassoc
= XCALLOC (short, nrules
) - 1;
1825 rlhs
[ruleno
] = p
->sym
->value
;
1826 rrhs
[ruleno
] = itemno
;
1827 ruleprec
= p
->ruleprec
;
1832 ritem
[itemno
++] = p
->sym
->value
;
1833 /* A rule gets by default the precedence and associativity
1834 of the last token in it. */
1835 if (p
->sym
->class == token_sym
)
1837 rprec
[ruleno
] = p
->sym
->prec
;
1838 rassoc
[ruleno
] = p
->sym
->assoc
;
1844 /* If this rule has a %prec,
1845 the specified symbol's precedence replaces the default. */
1848 rprec
[ruleno
] = ruleprec
->prec
;
1849 rassoc
[ruleno
] = ruleprec
->assoc
;
1850 rprecsym
[ruleno
] = ruleprec
->value
;
1853 ritem
[itemno
++] = -ruleno
;
1863 /*-------------------------------------------------------------------.
1864 | Read in the grammar specification and record it in the format |
1865 | described in gram.h. All guards are copied into the FGUARD file |
1866 | and all actions into FACTION, in each case forming the body of a C |
1867 | function (YYGUARD or YYACTION) which contains a switch statement |
1868 | to decide which guard or action to execute. |
1869 `-------------------------------------------------------------------*/
1875 startval
= NULL
; /* start symbol not specified yet. */
1878 /* initially assume token number translation not needed. */
1881 /* Nowadays translations is always set to 1, since we give `error' a
1882 user-token-number to satisfy the Posix demand for YYERRCODE==256.
1890 rline_allocated
= 10;
1891 rline
= XCALLOC (short, rline_allocated
);
1898 semantic_parser
= 0;
1906 /* Initialize the symbol table. */
1908 /* Construct the error token */
1909 errtoken
= getsym ("error");
1910 errtoken
->class = token_sym
;
1911 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1912 /* Construct a token that represents all undefined literal tokens.
1913 It is always token number 2. */
1914 undeftoken
= getsym ("$undefined.");
1915 undeftoken
->class = token_sym
;
1916 undeftoken
->user_token_number
= 2;
1918 /* Read the declaration section. Copy %{ ... %} groups to FTABLE
1919 and FDEFINES file. Also notice any %token, %left, etc. found
1921 putc ('\n', ftable
);
1923 /* %s, made from %s\n\
1924 by GNU bison %s. */\n\
1925 \n", no_parser_flag
? "Bison-generated parse tables" : "A Bison parser", infile
, VERSION
);
1927 fputs ("#define YYBISON 1 /* Identify Bison output. */\n\n", ftable
);
1928 read_declarations ();
1929 /* Start writing the guard and action files, if they are needed. */
1931 /* Read in the grammar, build grammar in list form. Write out
1932 guards and actions. */
1934 /* Now we know whether we need the line-number stack. If we do,
1935 write its type into the .tab.h file. */
1937 reader_output_yylsp (fdefines
);
1938 /* Write closing delimiters for actions and guards. */
1941 fputs ("#define YYLSP_NEEDED\n\n", ftable
);
1942 /* Assign the symbols their symbol numbers. Write #defines for the
1943 token symbols into FDEFINES if requested. */
1945 /* Convert the grammar into the format described in gram.h. */
1947 /* Free the symbol table data structure since symbols are now all
1948 referred to by symbol number. */
1954 reader_output_yylsp (FILE *f
)
1960 typedef struct yyltype\n\
1971 # define YYLTYPE yyltype\n\