]>
git.saurik.com Git - bison.git/blob - src/reader.c
1 /* Input parser for bison
2 Copyright (C) 1984, 1986, 1989, 1992, 1998, 2000
3 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
7 Bison is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 Bison is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
33 #include "conflicts.h"
35 extern char *printable_version
PARAMS ((int));
37 /* Number of slots allocated (but not necessarily used yet) in `rline' */
38 static int rline_allocated
;
40 typedef struct symbol_list
42 struct symbol_list
*next
;
51 static symbol_list
*grammar
;
52 static int start_flag
;
53 static bucket
*startval
;
55 /* Nonzero if components of semantic values are used, implying
56 they must be unions. */
57 static int value_components_used
;
59 /* Nonzero if %union has been seen. */
62 /* Incremented for each %left, %right or %nonassoc seen */
65 /* Incremented for each generated symbol */
66 static int gensym_count
;
68 static bucket
*errtoken
;
69 static bucket
*undeftoken
;
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 */
357 int tmp_char
= ungetc (skip_white_space (), finput
);
362 fatal (_("Premature EOF after %s"), token_buffer
);
370 if (token
== TYPENAME
)
372 typename
= xstrdup (token_buffer
);
373 value_components_used
= 1;
376 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"' && symbol
)
379 symval
->class = token_sym
;
380 symval
->type_name
= typename
;
381 symval
->user_token_number
= symbol
->user_token_number
;
382 symbol
->user_token_number
= SALIAS
;
384 symval
->alias
= symbol
;
385 symbol
->alias
= symval
;
388 nsyms
--; /* symbol and symval combined are only one symbol */
390 else if (token
== IDENTIFIER
)
392 int oldclass
= symval
->class;
395 if (symbol
->class == what_is_not
)
396 complain (_("symbol %s redefined"), symbol
->tag
);
397 symbol
->class = what_is
;
398 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
399 symbol
->value
= nvars
++;
403 if (symbol
->type_name
== NULL
)
404 symbol
->type_name
= typename
;
405 else if (strcmp (typename
, symbol
->type_name
) != 0)
406 complain (_("type redeclaration for %s"), symbol
->tag
);
409 else if (symbol
&& token
== NUMBER
)
411 symbol
->user_token_number
= numval
;
416 complain (_("`%s' is invalid in %s"),
417 token_buffer
, (what_is
== token_sym
) ? "%token" : "%nterm");
425 /*------------------------------.
426 | Parse what comes after %start |
427 `------------------------------*/
430 parse_start_decl (void)
433 complain (_("multiple %s declarations"), "%start");
434 if (lex () != IDENTIFIER
)
435 complain (_("invalid %s declaration"), "%start");
445 /*--------------------------------------------------------------.
446 | Get the data type (alternative in the union) of the value for |
447 | symbol n in rule rule. |
448 `--------------------------------------------------------------*/
451 get_type_name (int n
, symbol_list
* rule
)
458 complain (_("invalid $ value"));
468 if (rp
== NULL
|| rp
->sym
== NULL
)
470 complain (_("invalid $ value"));
476 return rp
->sym
->type_name
;
480 /*-----------------------------------------------------------.
481 | read in a %type declaration and record its information for |
482 | get_type_name to access |
483 `-----------------------------------------------------------*/
486 parse_type_decl (void)
490 if (lex () != TYPENAME
)
492 complain ("%s", _("%type declaration has no <typename>"));
497 name
= xstrdup (token_buffer
);
502 int tmp_char
= ungetc (skip_white_space (), finput
);
507 fatal (_("Premature EOF after %s"), token_buffer
);
519 if (symval
->type_name
== NULL
)
520 symval
->type_name
= name
;
521 else if (strcmp (name
, symval
->type_name
) != 0)
522 complain (_("type redeclaration for %s"), symval
->tag
);
527 complain (_("invalid %%type declaration due to item: %s"),
536 /*----------------------------------------------------------------.
537 | Read in a %left, %right or %nonassoc declaration and record its |
539 `----------------------------------------------------------------*/
542 parse_assoc_decl (associativity assoc
)
547 lastprec
++; /* Assign a new precedence level, never 0. */
552 int tmp_char
= ungetc (skip_white_space (), finput
);
557 fatal (_("Premature EOF after %s"), token_buffer
);
564 name
= xstrdup (token_buffer
);
571 if (symval
->prec
!= 0)
572 complain (_("redefining precedence of %s"), symval
->tag
);
573 symval
->prec
= lastprec
;
574 symval
->assoc
= assoc
;
575 if (symval
->class == nterm_sym
)
576 complain (_("symbol %s redefined"), symval
->tag
);
577 symval
->class = token_sym
;
579 { /* record the type, if one is specified */
580 if (symval
->type_name
== NULL
)
581 symval
->type_name
= name
;
582 else if (strcmp (name
, symval
->type_name
) != 0)
583 complain (_("type redeclaration for %s"), symval
->tag
);
588 if (prev
== IDENTIFIER
)
590 symval
->user_token_number
= numval
;
596 ("invalid text (%s) - number should be after identifier"),
606 complain (_("unexpected item: %s"), token_buffer
);
617 /*-------------------------------------------------------------------.
618 | Copy the union declaration into fattrs (and fdefines), where it is |
619 | made into the definition of YYSTYPE, the type of elements of the |
620 | parser value stack. |
621 `-------------------------------------------------------------------*/
624 parse_union_decl (void)
630 complain (_("multiple %s declarations"), "%union");
635 fprintf (fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
637 fprintf (fattrs
, "\n");
639 fprintf (fattrs
, "typedef union");
641 fprintf (fdefines
, "typedef union");
659 if (c
!= '*' && c
!= '/')
661 copy_comment2 (finput
, fattrs
, fdefines
, c
);
671 complain (_("unmatched %s"), "`}'");
675 fprintf (fattrs
, " YYSTYPE;\n");
677 fprintf (fdefines
, " YYSTYPE;\n");
678 /* JF don't choke on trailing semi */
679 c
= skip_white_space ();
691 /*-------------------------------------------------------.
692 | Parse the declaration %expect N which says to expect N |
693 | shift-reduce conflicts. |
694 `-------------------------------------------------------*/
697 parse_expect_decl (void)
704 while (c
== ' ' || c
== '\t')
708 while (c
>= '0' && c
<= '9')
718 if (count
<= 0 || count
> 10)
719 complain ("%s", _("argument of %expect is not an integer"));
720 expected_conflicts
= atoi (buffer
);
724 /*-------------------------------------------------------------------.
725 | Parse what comes after %thong. the full syntax is |
727 | %thong <type> token number literal |
729 | the <type> or number may be omitted. The number specifies the |
730 | user_token_number. |
732 | Two symbols are entered in the table, one for the token symbol and |
733 | one for the literal. Both are given the <type>, if any, from the |
734 | declaration. The ->user_token_number of the first is SALIAS and |
735 | the ->user_token_number of the second is set to the number, if |
736 | any, from the declaration. The two symbols are linked via |
737 | pointers in their ->alias fields. |
739 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
740 | only the literal string is retained it is the literal string that |
741 | is output to yytname |
742 `-------------------------------------------------------------------*/
745 parse_thong_decl (void)
748 struct bucket
*symbol
;
753 token
= lex (); /* fetch typename or first token */
754 if (token
== TYPENAME
)
756 typename
= xstrdup (token_buffer
);
757 value_components_used
= 1;
758 token
= lex (); /* fetch first token */
761 /* process first token */
763 if (token
!= IDENTIFIER
)
765 complain (_("unrecognized item %s, expected an identifier"),
770 symval
->class = token_sym
;
771 symval
->type_name
= typename
;
772 symval
->user_token_number
= SALIAS
;
775 token
= lex (); /* get number or literal string */
780 token
= lex (); /* okay, did number, now get literal */
785 /* process literal string token */
787 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
789 complain (_("expected string constant instead of %s"), token_buffer
);
793 symval
->class = token_sym
;
794 symval
->type_name
= typename
;
795 symval
->user_token_number
= usrtoknum
;
797 symval
->alias
= symbol
;
798 symbol
->alias
= symval
;
800 nsyms
--; /* symbol and symval combined are only one symbol */
804 /*----------------------------------------------------------------.
805 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
806 | any `%' declarations, and copy the contents of any `%{ ... %}' |
807 | groups to fattrs. |
808 `----------------------------------------------------------------*/
811 read_declarations (void)
818 c
= skip_white_space ();
822 tok
= parse_percent_token ();
829 case PERCENT_LEFT_CURLY
:
834 parse_token_decl (token_sym
, nterm_sym
);
838 parse_token_decl (nterm_sym
, token_sym
);
854 parse_expect_decl ();
861 parse_assoc_decl (left_assoc
);
865 parse_assoc_decl (right_assoc
);
869 parse_assoc_decl (non_assoc
);
872 case SEMANTIC_PARSER
:
873 if (semantic_parser
== 0)
888 complain (_("unrecognized: %s"), token_buffer
);
893 fatal (_("no input grammar"));
896 complain (_("unknown character: %s"), printable_version (c
));
902 /*-------------------------------------------------------------------.
903 | Assuming that a `{' has just been seen, copy everything up to the |
904 | matching `}' into the actions file. STACK_OFFSET is the number of |
905 | values in the current rule so far, which says where to find `$0' |
906 | with respect to the top of the stack. |
907 `-------------------------------------------------------------------*/
910 copy_action (symbol_list
* rule
, int stack_offset
)
917 /* offset is always 0 if parser has already popped the stack pointer */
921 fprintf (faction
, "\ncase %d:\n", nrules
);
923 fprintf (faction
, "#line %d \"%s\"\n", lineno
, infile
);
947 copy_string (finput
, faction
, c
);
953 if (c
!= '*' && c
!= '/')
955 copy_comment (finput
, faction
, c
);
964 char *cp
= token_buffer
;
966 while ((c
= getc (finput
)) != '>' && c
> 0)
968 if (cp
== token_buffer
+ maxtoken
)
969 cp
= grow_token_buffer (cp
);
974 type_name
= token_buffer
;
975 value_components_used
= 1;
981 fprintf (faction
, "yyval");
983 type_name
= get_type_name (0, rule
);
985 fprintf (faction
, ".%s", type_name
);
986 if (!type_name
&& typed
)
987 complain (_("$$ of `%s' has no declared type"),
990 else if (isdigit (c
) || c
== '-')
993 n
= read_signed_integer (finput
);
996 if (!type_name
&& n
> 0)
997 type_name
= get_type_name (n
, rule
);
999 fprintf (faction
, "yyvsp[%d]", n
- stack_offset
);
1001 fprintf (faction
, ".%s", type_name
);
1002 if (!type_name
&& typed
)
1003 complain (_("$%d of `%s' has no declared type"),
1008 complain (_("$%s is invalid"), printable_version (c
));
1013 copy_at (finput
, faction
, stack_offset
);
1017 fatal (_("unmatched %s"), "`{'");
1026 /* above loop exits when c is '}' */
1035 fprintf (faction
, ";\n break;}");
1038 /*-------------------------------------------------------------------.
1039 | After `%guard' is seen in the input file, copy the actual guard |
1040 | into the guards file. If the guard is followed by an action, copy |
1041 | that into the actions file. STACK_OFFSET is the number of values |
1042 | in the current rule so far, which says where to find `$0' with |
1043 | respect to the top of the stack, for the simple parser in which |
1044 | the stack is not popped until after the guard is run. |
1045 `-------------------------------------------------------------------*/
1048 copy_guard (symbol_list
* rule
, int stack_offset
)
1056 /* offset is always 0 if parser has already popped the stack pointer */
1057 if (semantic_parser
)
1060 fprintf (fguard
, "\ncase %d:\n", nrules
);
1062 fprintf (fguard
, "#line %d \"%s\"\n", lineno
, infile
);
1068 while (brace_flag
? (count
> 0) : (c
!= ';'))
1089 complain (_("unmatched %s"), "`}'");
1090 c
= getc (finput
); /* skip it */
1096 copy_string (finput
, fguard
, c
);
1102 if (c
!= '*' && c
!= '/')
1104 copy_comment (finput
, fguard
, c
);
1113 char *cp
= token_buffer
;
1115 while ((c
= getc (finput
)) != '>' && c
> 0)
1117 if (cp
== token_buffer
+ maxtoken
)
1118 cp
= grow_token_buffer (cp
);
1123 type_name
= token_buffer
;
1130 fprintf (fguard
, "yyval");
1132 type_name
= rule
->sym
->type_name
;
1134 fprintf (fguard
, ".%s", type_name
);
1135 if (!type_name
&& typed
)
1136 complain (_("$$ of `%s' has no declared type"),
1139 else if (isdigit (c
) || c
== '-')
1142 n
= read_signed_integer (finput
);
1145 if (!type_name
&& n
> 0)
1146 type_name
= get_type_name (n
, rule
);
1148 fprintf (fguard
, "yyvsp[%d]", n
- stack_offset
);
1150 fprintf (fguard
, ".%s", type_name
);
1151 if (!type_name
&& typed
)
1152 complain (_("$%d of `%s' has no declared type"),
1157 complain (_("$%s is invalid"), printable_version (c
));
1161 copy_at (finput
, fguard
, stack_offset
);
1165 fatal ("%s", _("unterminated %guard clause"));
1171 if (c
!= '}' || count
!= 0)
1175 c
= skip_white_space ();
1177 fprintf (fguard
, ";\n break;}");
1179 copy_action (rule
, stack_offset
);
1182 c
= getc (finput
); /* why not skip_white_space -wjh */
1184 copy_action (rule
, stack_offset
);
1192 record_rule_line (void)
1194 /* Record each rule's source line number in rline table. */
1196 if (nrules
>= rline_allocated
)
1198 rline_allocated
= nrules
* 2;
1199 rline
= XREALLOC (rline
, short, rline_allocated
);
1201 rline
[nrules
] = lineno
;
1205 /*-------------------------------------------------------------------.
1206 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1207 | with the user's names. |
1208 `-------------------------------------------------------------------*/
1215 sprintf (token_buffer
, "@%d", ++gensym_count
);
1216 sym
= getsym (token_buffer
);
1217 sym
->class = nterm_sym
;
1218 sym
->value
= nvars
++;
1223 /*------------------------------------------------------------------.
1224 | read in a %type declaration and record its information for |
1225 | get_type_name to access. This is unused. It is only called from |
1226 | the #if 0 part of readgram |
1227 `------------------------------------------------------------------*/
1240 complain (_("invalid %s declaration"), "%type");
1244 name
= xstrdup (token_buffer
);
1259 if (symval
->type_name
== NULL
)
1260 symval
->type_name
= name
;
1261 else if (strcmp (name
, symval
->type_name
) != 0)
1262 complain (_("type redeclaration for %s"), symval
->tag
);
1274 /*------------------------------------------------------------------.
1275 | Parse the input grammar into a one symbol_list structure. Each |
1276 | rule is represented by a sequence of symbols: the left hand side |
1277 | followed by the contents of the right hand side, followed by a |
1278 | null pointer instead of a symbol to terminate the rule. The next |
1279 | symbol is the lhs of the following rule. |
1281 | All guards and actions are copied out to the appropriate files, |
1282 | labelled by the rule number they apply to. |
1283 `------------------------------------------------------------------*/
1294 symbol_list
*crule
; /* points to first symbol_list of current rule. */
1295 /* its symbol is the lhs of the rule. */
1296 symbol_list
*crule1
; /* points to the symbol_list preceding crule. */
1302 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1304 if (t
== IDENTIFIER
|| t
== BAR
)
1307 int rulelength
= 0; /* number of symbols in rhs of this rule so far */
1308 int xactions
= 0; /* JF for error checking */
1309 bucket
*first_rhs
= 0;
1311 if (t
== IDENTIFIER
)
1324 complain (_("ill-formed rule: initial symbol not followed by colon"));
1329 if (nrules
== 0 && t
== BAR
)
1331 complain (_("grammar starts with vertical bar"));
1332 lhs
= symval
; /* BOGUS: use a random symval */
1334 /* start a new rule and record its lhs. */
1339 record_rule_line ();
1341 p
= XCALLOC (symbol_list
, 1);
1353 /* mark the rule's lhs as a nonterminal if not already so. */
1355 if (lhs
->class == unknown_sym
)
1357 lhs
->class = nterm_sym
;
1361 else if (lhs
->class == token_sym
)
1362 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1364 /* read the rhs of the rule. */
1372 crule
->ruleprec
= symval
;
1376 if (!(t
== IDENTIFIER
|| t
== LEFT_CURLY
))
1379 /* If next token is an identifier, see if a colon follows it.
1380 If one does, exit this rule now. */
1381 if (t
== IDENTIFIER
)
1393 if (!first_rhs
) /* JF */
1395 /* Not followed by colon =>
1396 process as part of this rule's rhs. */
1399 /* If we just passed an action, that action was in the middle
1400 of a rule, so make a dummy rule to reduce it to a
1406 /* Since the action was written out with this rule's */
1407 /* number, we must give the new rule this number */
1408 /* by inserting the new rule before it. */
1410 /* Make a dummy nonterminal, a gensym. */
1413 /* Make a new rule, whose body is empty,
1414 before the current one, so that the action
1415 just read can belong to it. */
1418 record_rule_line ();
1419 p
= XCALLOC (symbol_list
, 1);
1425 crule1
= XCALLOC (symbol_list
, 1);
1427 crule1
->next
= crule
;
1429 /* insert the dummy generated by that rule into this rule. */
1431 p
= XCALLOC (symbol_list
, 1);
1439 if (t
== IDENTIFIER
)
1442 p
= XCALLOC (symbol_list
, 1);
1447 else /* handle an action. */
1449 copy_action (crule
, rulelength
);
1451 xactions
++; /* JF */
1454 } /* end of read rhs of rule */
1456 /* Put an empty link in the list to mark the end of this rule */
1457 p
= XCALLOC (symbol_list
, 1);
1463 complain (_("two @prec's in a row"));
1465 crule
->ruleprec
= symval
;
1470 if (!semantic_parser
)
1473 ("%guard present but %semantic_parser not specified"));
1475 copy_guard (crule
, rulelength
);
1478 else if (t
== LEFT_CURLY
)
1480 /* This case never occurs -wjh */
1482 complain (_("two actions at end of one rule"));
1483 copy_action (crule
, rulelength
);
1485 xactions
++; /* -wjh */
1488 /* If $$ is being set in default way, report if any type
1491 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1493 if (lhs
->type_name
== 0
1494 || first_rhs
->type_name
== 0
1495 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1496 complain (_("type clash (`%s' `%s') on default action"),
1497 lhs
->type_name
? lhs
->type_name
: "",
1498 first_rhs
->type_name
? first_rhs
->type_name
: "");
1500 /* Warn if there is no default for $$ but we need one. */
1501 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1502 complain (_("empty rule for typed nonterminal, and no action"));
1507 /* these things can appear as alternatives to rules. */
1509 a) none of the documentation allows them
1510 b) most of them scan forward until finding a next %
1511 thus they may swallow lots of intervening rules
1513 else if (t
== TOKEN
)
1515 parse_token_decl (token_sym
, nterm_sym
);
1518 else if (t
== NTERM
)
1520 parse_token_decl (nterm_sym
, token_sym
);
1527 else if (t
== UNION
)
1529 parse_union_decl ();
1532 else if (t
== EXPECT
)
1534 parse_expect_decl ();
1537 else if (t
== START
)
1539 parse_start_decl ();
1546 complain (_("invalid input: %s"), token_buffer
);
1551 /* grammar has been read. Do some checking */
1553 if (nsyms
> MAXSHORT
)
1554 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1557 fatal (_("no rules in the input grammar"));
1559 if (typed
== 0 /* JF put out same default YYSTYPE as YACC does */
1560 && !value_components_used
)
1562 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1563 but it seems better to be consistent.
1564 Most programs should declare their own type anyway. */
1565 fprintf (fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1567 fprintf (fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1570 /* Report any undefined symbols and consider them nonterminals. */
1572 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1573 if (bp
->class == unknown_sym
)
1576 ("symbol %s is used, but is not defined as a token and has no rules"),
1578 bp
->class = nterm_sym
;
1579 bp
->value
= nvars
++;
1582 ntokens
= nsyms
- nvars
;
1585 /*--------------------------------------------------------------.
1586 | For named tokens, but not literal ones, define the name. The |
1587 | value is the user token number. |
1588 `--------------------------------------------------------------*/
1591 output_token_defines (FILE *file
)
1597 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1599 symbol
= bp
->tag
; /* get symbol */
1601 if (bp
->value
>= ntokens
)
1603 if (bp
->user_token_number
== SALIAS
)
1605 if ('\'' == *symbol
)
1606 continue; /* skip literal character */
1608 continue; /* skip error token */
1609 if ('\"' == *symbol
)
1611 /* use literal string only if given a symbol with an alias */
1613 symbol
= bp
->alias
->tag
;
1618 /* Don't #define nonliteral tokens whose names contain periods. */
1620 while ((c
= *cp
++) && c
!= '.');
1624 fprintf (file
, "#define\t%s\t%d\n", symbol
,
1625 ((translations
&& !rawtoknumflag
)
1626 ? bp
->user_token_number
: bp
->value
));
1627 if (semantic_parser
)
1628 fprintf (file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1635 /*------------------------------------------------------------------.
1636 | Assign symbol numbers, and write definition of token names into |
1637 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1639 `------------------------------------------------------------------*/
1647 int last_user_token_number
;
1648 static char DOLLAR
[] = "$";
1650 /* int lossage = 0; JF set but not used */
1652 tags
= XCALLOC (char *, nsyms
+ 1);
1654 user_toknums
= XCALLOC (short, nsyms
+ 1);
1655 user_toknums
[0] = 0;
1657 sprec
= XCALLOC (short, nsyms
);
1658 sassoc
= XCALLOC (short, nsyms
);
1660 max_user_token_number
= 256;
1661 last_user_token_number
= 256;
1663 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1665 if (bp
->class == nterm_sym
)
1667 bp
->value
+= ntokens
;
1671 /* this symbol and its alias are a single token defn.
1672 allocate a tokno, and assign to both check agreement of
1673 ->prec and ->assoc fields and make both the same */
1675 bp
->value
= bp
->alias
->value
= tokno
++;
1677 if (bp
->prec
!= bp
->alias
->prec
)
1679 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1680 && bp
->user_token_number
== SALIAS
)
1681 complain (_("conflicting precedences for %s and %s"),
1682 bp
->tag
, bp
->alias
->tag
);
1684 bp
->alias
->prec
= bp
->prec
;
1686 bp
->prec
= bp
->alias
->prec
;
1689 if (bp
->assoc
!= bp
->alias
->assoc
)
1691 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1692 && bp
->user_token_number
== SALIAS
)
1693 complain (_("conflicting assoc values for %s and %s"),
1694 bp
->tag
, bp
->alias
->tag
);
1696 bp
->alias
->assoc
= bp
->assoc
;
1698 bp
->assoc
= bp
->alias
->assoc
;
1701 if (bp
->user_token_number
== SALIAS
)
1702 continue; /* do not do processing below for SALIASs */
1705 else /* bp->class == token_sym */
1707 bp
->value
= tokno
++;
1710 if (bp
->class == token_sym
)
1712 if (translations
&& !(bp
->user_token_number
))
1713 bp
->user_token_number
= ++last_user_token_number
;
1714 if (bp
->user_token_number
> max_user_token_number
)
1715 max_user_token_number
= bp
->user_token_number
;
1718 tags
[bp
->value
] = bp
->tag
;
1719 user_toknums
[bp
->value
] = bp
->user_token_number
;
1720 sprec
[bp
->value
] = bp
->prec
;
1721 sassoc
[bp
->value
] = bp
->assoc
;
1729 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1731 /* initialize all entries for literal tokens to 2, the internal
1732 token number for $undefined., which represents all invalid
1734 for (j
= 0; j
<= max_user_token_number
; j
++)
1735 token_translations
[j
] = 2;
1737 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1739 if (bp
->value
>= ntokens
)
1740 continue; /* non-terminal */
1741 if (bp
->user_token_number
== SALIAS
)
1743 if (token_translations
[bp
->user_token_number
] != 2)
1744 complain (_("tokens %s and %s both assigned number %d"),
1745 tags
[token_translations
[bp
->user_token_number
]],
1746 bp
->tag
, bp
->user_token_number
);
1747 token_translations
[bp
->user_token_number
] = bp
->value
;
1751 error_token_number
= errtoken
->value
;
1754 output_token_defines (ftable
);
1756 if (startval
->class == unknown_sym
)
1757 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1758 else if (startval
->class == token_sym
)
1759 fatal (_("the start symbol %s is a token"), startval
->tag
);
1761 start_symbol
= startval
->value
;
1765 output_token_defines (fdefines
);
1769 if (spec_name_prefix
)
1770 fprintf (fdefines
, "\nextern YYSTYPE %slval;\n",
1773 fprintf (fdefines
, "\nextern YYSTYPE yylval;\n");
1776 if (semantic_parser
)
1777 for (i
= ntokens
; i
< nsyms
; i
++)
1779 /* don't make these for dummy nonterminals made by gensym. */
1780 if (*tags
[i
] != '@')
1781 fprintf (fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1784 /* `fdefines' is now a temporary file, so we need to copy its
1785 contents in `done', so we can't close it here. */
1793 /*---------------------------------------------------------------.
1794 | Convert the rules into the representation using RRHS, RLHS and |
1796 `---------------------------------------------------------------*/
1807 ritem
= XCALLOC (short, nitems
+ 1);
1808 rlhs
= XCALLOC (short, nrules
) - 1;
1809 rrhs
= XCALLOC (short, nrules
) - 1;
1810 rprec
= XCALLOC (short, nrules
) - 1;
1811 rprecsym
= XCALLOC (short, nrules
) - 1;
1812 rassoc
= XCALLOC (short, nrules
) - 1;
1820 rlhs
[ruleno
] = p
->sym
->value
;
1821 rrhs
[ruleno
] = itemno
;
1822 ruleprec
= p
->ruleprec
;
1827 ritem
[itemno
++] = p
->sym
->value
;
1828 /* A rule gets by default the precedence and associativity
1829 of the last token in it. */
1830 if (p
->sym
->class == token_sym
)
1832 rprec
[ruleno
] = p
->sym
->prec
;
1833 rassoc
[ruleno
] = p
->sym
->assoc
;
1839 /* If this rule has a %prec,
1840 the specified symbol's precedence replaces the default. */
1843 rprec
[ruleno
] = ruleprec
->prec
;
1844 rassoc
[ruleno
] = ruleprec
->assoc
;
1845 rprecsym
[ruleno
] = ruleprec
->value
;
1848 ritem
[itemno
++] = -ruleno
;
1858 /*-------------------------------------------------------------------.
1859 | Read in the grammar specification and record it in the format |
1860 | described in gram.h. All guards are copied into the FGUARD file |
1861 | and all actions into FACTION, in each case forming the body of a C |
1862 | function (YYGUARD or YYACTION) which contains a switch statement |
1863 | to decide which guard or action to execute. |
1864 `-------------------------------------------------------------------*/
1870 startval
= NULL
; /* start symbol not specified yet. */
1873 /* initially assume token number translation not needed. */
1876 /* Nowadays translations is always set to 1, since we give `error' a
1877 user-token-number to satisfy the Posix demand for YYERRCODE==256.
1885 rline_allocated
= 10;
1886 rline
= XCALLOC (short, rline_allocated
);
1893 semantic_parser
= 0;
1902 /* Initialize the symbol table. */
1904 /* Construct the error token */
1905 errtoken
= getsym ("error");
1906 errtoken
->class = token_sym
;
1907 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1908 /* Construct a token that represents all undefined literal tokens.
1909 It is always token number 2. */
1910 undeftoken
= getsym ("$undefined.");
1911 undeftoken
->class = token_sym
;
1912 undeftoken
->user_token_number
= 2;
1914 /* Read the declaration section. Copy %{ ... %} groups to FTABLE
1915 and FDEFINES file. Also notice any %token, %left, etc. found
1917 putc ('\n', ftable
);
1919 /* %s, made from %s\n\
1920 by GNU bison %s. */\n\
1921 \n", noparserflag
? "Bison-generated parse tables" : "A Bison parser", infile
, VERSION
);
1923 fputs ("#define YYBISON 1 /* Identify Bison output. */\n\n", ftable
);
1924 read_declarations ();
1925 /* Start writing the guard and action files, if they are needed. */
1927 /* Read in the grammar, build grammar in list form. Write out
1928 guards and actions. */
1930 /* Now we know whether we need the line-number stack. If we do,
1931 write its type into the .tab.h file. */
1933 reader_output_yylsp (fdefines
);
1934 /* Write closing delimiters for actions and guards. */
1937 fputs ("#define YYLSP_NEEDED\n\n", ftable
);
1938 /* Assign the symbols their symbol numbers. Write #defines for the
1939 token symbols into FDEFINES if requested. */
1941 /* Convert the grammar into the format described in gram.h. */
1943 /* Free the symbol table data structure since symbols are now all
1944 referred to by symbol number. */
1950 reader_output_yylsp (FILE *f
)
1971 #define YYLTYPE yyltype\n\