]>
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"
36 /* Number of slots allocated (but not necessarily used yet) in `rline' */
37 static int rline_allocated
;
39 typedef struct symbol_list
41 struct symbol_list
*next
;
50 static symbol_list
*grammar
;
51 static int start_flag
;
52 static bucket
*startval
;
54 /* Nonzero if components of semantic values are used, implying
55 they must be unions. */
56 static int value_components_used
;
58 /* Nonzero if %union has been seen. */
61 /* Incremented for each %left, %right or %nonassoc seen */
64 /* Incremented for each generated symbol */
65 static int gensym_count
;
67 static bucket
*errtoken
;
68 static bucket
*undeftoken
;
71 /*===================\
73 \===================*/
76 skip_to_char (int target
)
80 complain (_(" Skipping to next \\n"));
82 complain (_(" Skipping to next %c"), target
);
85 c
= skip_white_space ();
86 while (c
!= target
&& c
!= EOF
);
92 /*---------------------------------------------------------.
93 | Read a signed integer from STREAM and return its value. |
94 `---------------------------------------------------------*/
97 read_signed_integer (FILE *stream
)
99 int c
= getc (stream
);
111 n
= 10 * n
+ (c
- '0');
120 /*-------------------------------------------------------------------.
121 | Dump the string from FINPUT to FOUTPUT. MATCH is the delimiter of |
122 | the string (either ' or "). |
123 `-------------------------------------------------------------------*/
126 copy_string (FILE *fin
, FILE *fout
, int match
)
136 fatal (_("unterminated string at end of file"));
139 complain (_("unterminated string"));
141 c
= match
; /* invent terminator */
151 fatal (_("unterminated string at end of file"));
164 /*---------------------------------------------------------------.
165 | Dump the comment from IN to OUT1 and OUT2. C is either `*' or |
166 | `/', depending upon the type of comments used. OUT2 might be |
168 `---------------------------------------------------------------*/
171 copy_comment2 (FILE *in
, FILE *out1
, FILE *out2
, int c
)
176 cplus_comment
= (c
== '/');
185 if (!cplus_comment
&& c
== '*')
215 fatal (_("unterminated comment"));
227 /*------------------------------------------------------------.
228 | Dump the comment from FIN to FOUT. C is either `*' or `/', |
229 | depending upon the type of comments used. |
230 `------------------------------------------------------------*/
233 copy_comment (FILE *fin
, FILE *fout
, int c
)
235 copy_comment2 (fin
, fout
, NULL
, c
);
239 /*-----------------------------------------------------------------.
240 | FIN is pointing to a location (i.e., a `@'). Output to FOUT a |
241 | reference to this location. STACK_OFFSET is the number of values |
242 | in the current rule so far, which says where to find `$0' with |
243 | respect to the top of the stack. |
244 `-----------------------------------------------------------------*/
247 copy_at (FILE *fin
, FILE *fout
, int stack_offset
)
254 fprintf (fout
, "yyloc");
257 else if (isdigit (c
) || c
== '-')
262 n
= read_signed_integer (fin
);
264 fprintf (fout
, "yylsp[%d]", n
- stack_offset
);
271 complain (_("%s is invalid"), quote (buf
));
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 warn (_("symbol `%s' used more than once as a literal string"),
381 else if (symbol
->alias
)
382 warn (_("symbol `%s' given more than one literal string"),
386 symval
->class = token_sym
;
387 symval
->type_name
= typename
;
388 symval
->user_token_number
= symbol
->user_token_number
;
389 symbol
->user_token_number
= SALIAS
;
390 symval
->alias
= symbol
;
391 symbol
->alias
= symval
;
392 /* symbol and symval combined are only one symbol */
398 else if (token
== IDENTIFIER
)
400 int oldclass
= symval
->class;
403 if (symbol
->class == what_is_not
)
404 complain (_("symbol %s redefined"), symbol
->tag
);
405 symbol
->class = what_is
;
406 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
407 symbol
->value
= nvars
++;
411 if (symbol
->type_name
== NULL
)
412 symbol
->type_name
= typename
;
413 else if (strcmp (typename
, symbol
->type_name
) != 0)
414 complain (_("type redeclaration for %s"), symbol
->tag
);
417 else if (symbol
&& token
== NUMBER
)
419 symbol
->user_token_number
= numval
;
424 complain (_("`%s' is invalid in %s"),
425 token_buffer
, (what_is
== token_sym
) ? "%token" : "%nterm");
433 /*------------------------------.
434 | Parse what comes after %start |
435 `------------------------------*/
438 parse_start_decl (void)
441 complain (_("multiple %s declarations"), "%start");
442 if (lex () != IDENTIFIER
)
443 complain (_("invalid %s declaration"), "%start");
453 /*--------------------------------------------------------------.
454 | Get the data type (alternative in the union) of the value for |
455 | symbol n in rule rule. |
456 `--------------------------------------------------------------*/
459 get_type_name (int n
, symbol_list
* rule
)
466 complain (_("invalid $ value"));
476 if (rp
== NULL
|| rp
->sym
== NULL
)
478 complain (_("invalid $ value"));
484 return rp
->sym
->type_name
;
488 /*-----------------------------------------------------------.
489 | read in a %type declaration and record its information for |
490 | get_type_name to access |
491 `-----------------------------------------------------------*/
494 parse_type_decl (void)
498 if (lex () != TYPENAME
)
500 complain ("%s", _("%type declaration has no <typename>"));
505 name
= xstrdup (token_buffer
);
510 int tmp_char
= ungetc (skip_white_space (), finput
);
515 fatal (_("Premature EOF after %s"), token_buffer
);
527 if (symval
->type_name
== NULL
)
528 symval
->type_name
= name
;
529 else if (strcmp (name
, symval
->type_name
) != 0)
530 complain (_("type redeclaration for %s"), symval
->tag
);
535 complain (_("invalid %%type declaration due to item: %s"),
544 /*----------------------------------------------------------------.
545 | Read in a %left, %right or %nonassoc declaration and record its |
547 `----------------------------------------------------------------*/
550 parse_assoc_decl (associativity assoc
)
555 lastprec
++; /* Assign a new precedence level, never 0. */
560 int tmp_char
= ungetc (skip_white_space (), finput
);
565 fatal (_("Premature EOF after %s"), token_buffer
);
572 name
= xstrdup (token_buffer
);
579 if (symval
->prec
!= 0)
580 complain (_("redefining precedence of %s"), symval
->tag
);
581 symval
->prec
= lastprec
;
582 symval
->assoc
= assoc
;
583 if (symval
->class == nterm_sym
)
584 complain (_("symbol %s redefined"), symval
->tag
);
585 symval
->class = token_sym
;
587 { /* record the type, if one is specified */
588 if (symval
->type_name
== NULL
)
589 symval
->type_name
= name
;
590 else if (strcmp (name
, symval
->type_name
) != 0)
591 complain (_("type redeclaration for %s"), symval
->tag
);
596 if (prev
== IDENTIFIER
)
598 symval
->user_token_number
= numval
;
604 ("invalid text (%s) - number should be after identifier"),
614 complain (_("unexpected item: %s"), token_buffer
);
625 /*-------------------------------------------------------------------.
626 | Copy the union declaration into fattrs (and fdefines), where it is |
627 | made into the definition of YYSTYPE, the type of elements of the |
628 | parser value stack. |
629 `-------------------------------------------------------------------*/
632 parse_union_decl (void)
638 complain (_("multiple %s declarations"), "%union");
643 fprintf (fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
645 fprintf (fattrs
, "\n");
647 fprintf (fattrs
, "typedef union");
649 fprintf (fdefines
, "typedef union");
667 if (c
!= '*' && c
!= '/')
669 copy_comment2 (finput
, fattrs
, fdefines
, c
);
679 complain (_("unmatched %s"), "`}'");
683 fprintf (fattrs
, " YYSTYPE;\n");
685 fprintf (fdefines
, " YYSTYPE;\n");
686 /* JF don't choke on trailing semi */
687 c
= skip_white_space ();
699 /*-------------------------------------------------------.
700 | Parse the declaration %expect N which says to expect N |
701 | shift-reduce conflicts. |
702 `-------------------------------------------------------*/
705 parse_expect_decl (void)
712 while (c
== ' ' || c
== '\t')
716 while (c
>= '0' && c
<= '9')
718 if (count
< sizeof(buffer
) - 1)
726 if (count
<= 0 || count
> 10)
727 complain ("%s", _("argument of %expect is not an integer"));
728 expected_conflicts
= atoi (buffer
);
732 /*-------------------------------------------------------------------.
733 | Parse what comes after %thong. the full syntax is |
735 | %thong <type> token number literal |
737 | the <type> or number may be omitted. The number specifies the |
738 | user_token_number. |
740 | Two symbols are entered in the table, one for the token symbol and |
741 | one for the literal. Both are given the <type>, if any, from the |
742 | declaration. The ->user_token_number of the first is SALIAS and |
743 | the ->user_token_number of the second is set to the number, if |
744 | any, from the declaration. The two symbols are linked via |
745 | pointers in their ->alias fields. |
747 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
748 | only the literal string is retained it is the literal string that |
749 | is output to yytname |
750 `-------------------------------------------------------------------*/
753 parse_thong_decl (void)
756 struct bucket
*symbol
;
761 token
= lex (); /* fetch typename or first token */
762 if (token
== TYPENAME
)
764 typename
= xstrdup (token_buffer
);
765 value_components_used
= 1;
766 token
= lex (); /* fetch first token */
769 /* process first token */
771 if (token
!= IDENTIFIER
)
773 complain (_("unrecognized item %s, expected an identifier"),
778 symval
->class = token_sym
;
779 symval
->type_name
= typename
;
780 symval
->user_token_number
= SALIAS
;
783 token
= lex (); /* get number or literal string */
788 token
= lex (); /* okay, did number, now get literal */
793 /* process literal string token */
795 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
797 complain (_("expected string constant instead of %s"), token_buffer
);
801 symval
->class = token_sym
;
802 symval
->type_name
= typename
;
803 symval
->user_token_number
= usrtoknum
;
805 symval
->alias
= symbol
;
806 symbol
->alias
= symval
;
808 nsyms
--; /* symbol and symval combined are only one symbol */
812 /*----------------------------------------------------------------.
813 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
814 | any `%' declarations, and copy the contents of any `%{ ... %}' |
815 | groups to fattrs. |
816 `----------------------------------------------------------------*/
819 read_declarations (void)
826 c
= skip_white_space ();
830 tok
= parse_percent_token ();
837 case PERCENT_LEFT_CURLY
:
842 parse_token_decl (token_sym
, nterm_sym
);
846 parse_token_decl (nterm_sym
, token_sym
);
862 parse_expect_decl ();
869 parse_assoc_decl (left_assoc
);
873 parse_assoc_decl (right_assoc
);
877 parse_assoc_decl (non_assoc
);
880 case SEMANTIC_PARSER
:
881 if (semantic_parser
== 0)
896 complain (_("unrecognized: %s"), token_buffer
);
901 fatal (_("no input grammar"));
906 complain (_("unknown character: %s"), quote (buf
));
912 /*-------------------------------------------------------------------.
913 | Assuming that a `{' has just been seen, copy everything up to the |
914 | matching `}' into the actions file. STACK_OFFSET is the number of |
915 | values in the current rule so far, which says where to find `$0' |
916 | with respect to the top of the stack. |
917 `-------------------------------------------------------------------*/
920 copy_action (symbol_list
* rule
, int stack_offset
)
927 /* offset is always 0 if parser has already popped the stack pointer */
931 fprintf (faction
, "\ncase %d:\n", nrules
);
933 fprintf (faction
, "#line %d \"%s\"\n", lineno
, infile
);
957 copy_string (finput
, faction
, c
);
963 if (c
!= '*' && c
!= '/')
965 copy_comment (finput
, faction
, c
);
974 char *cp
= token_buffer
;
976 while ((c
= getc (finput
)) != '>' && c
> 0)
978 if (cp
== token_buffer
+ maxtoken
)
979 cp
= grow_token_buffer (cp
);
984 type_name
= token_buffer
;
985 value_components_used
= 1;
991 fprintf (faction
, "yyval");
993 type_name
= get_type_name (0, rule
);
995 fprintf (faction
, ".%s", type_name
);
996 if (!type_name
&& typed
)
997 complain (_("$$ of `%s' has no declared type"),
1000 else if (isdigit (c
) || c
== '-')
1003 n
= read_signed_integer (finput
);
1006 if (!type_name
&& n
> 0)
1007 type_name
= get_type_name (n
, rule
);
1009 fprintf (faction
, "yyvsp[%d]", n
- stack_offset
);
1011 fprintf (faction
, ".%s", type_name
);
1012 if (!type_name
&& typed
)
1013 complain (_("$%d of `%s' has no declared type"),
1021 complain (_("%s is invalid"), quote (buf
));
1027 copy_at (finput
, faction
, stack_offset
);
1031 fatal (_("unmatched %s"), "`{'");
1040 /* above loop exits when c is '}' */
1049 fprintf (faction
, ";\n break;}");
1052 /*-------------------------------------------------------------------.
1053 | After `%guard' is seen in the input file, copy the actual guard |
1054 | into the guards file. If the guard is followed by an action, copy |
1055 | that into the actions file. STACK_OFFSET is the number of values |
1056 | in the current rule so far, which says where to find `$0' with |
1057 | respect to the top of the stack, for the simple parser in which |
1058 | the stack is not popped until after the guard is run. |
1059 `-------------------------------------------------------------------*/
1062 copy_guard (symbol_list
* rule
, int stack_offset
)
1070 /* offset is always 0 if parser has already popped the stack pointer */
1071 if (semantic_parser
)
1074 fprintf (fguard
, "\ncase %d:\n", nrules
);
1076 fprintf (fguard
, "#line %d \"%s\"\n", lineno
, infile
);
1082 while (brace_flag
? (count
> 0) : (c
!= ';'))
1103 complain (_("unmatched %s"), "`}'");
1104 c
= getc (finput
); /* skip it */
1110 copy_string (finput
, fguard
, c
);
1116 if (c
!= '*' && c
!= '/')
1118 copy_comment (finput
, fguard
, c
);
1127 char *cp
= token_buffer
;
1129 while ((c
= getc (finput
)) != '>' && c
> 0)
1131 if (cp
== token_buffer
+ maxtoken
)
1132 cp
= grow_token_buffer (cp
);
1137 type_name
= token_buffer
;
1144 fprintf (fguard
, "yyval");
1146 type_name
= rule
->sym
->type_name
;
1148 fprintf (fguard
, ".%s", type_name
);
1149 if (!type_name
&& typed
)
1150 complain (_("$$ of `%s' has no declared type"),
1153 else if (isdigit (c
) || c
== '-')
1156 n
= read_signed_integer (finput
);
1159 if (!type_name
&& n
> 0)
1160 type_name
= get_type_name (n
, rule
);
1162 fprintf (fguard
, "yyvsp[%d]", n
- stack_offset
);
1164 fprintf (fguard
, ".%s", type_name
);
1165 if (!type_name
&& typed
)
1166 complain (_("$%d of `%s' has no declared type"),
1174 complain (_("%s is invalid"), quote (buf
));
1179 copy_at (finput
, fguard
, stack_offset
);
1183 fatal ("%s", _("unterminated %guard clause"));
1189 if (c
!= '}' || count
!= 0)
1193 c
= skip_white_space ();
1195 fprintf (fguard
, ";\n break;}");
1197 copy_action (rule
, stack_offset
);
1200 c
= getc (finput
); /* why not skip_white_space -wjh */
1202 copy_action (rule
, stack_offset
);
1210 record_rule_line (void)
1212 /* Record each rule's source line number in rline table. */
1214 if (nrules
>= rline_allocated
)
1216 rline_allocated
= nrules
* 2;
1217 rline
= XREALLOC (rline
, short, rline_allocated
);
1219 rline
[nrules
] = lineno
;
1223 /*-------------------------------------------------------------------.
1224 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1225 | with the user's names. |
1226 `-------------------------------------------------------------------*/
1233 sprintf (token_buffer
, "@%d", ++gensym_count
);
1234 sym
= getsym (token_buffer
);
1235 sym
->class = nterm_sym
;
1236 sym
->value
= nvars
++;
1241 /*------------------------------------------------------------------.
1242 | read in a %type declaration and record its information for |
1243 | get_type_name to access. This is unused. It is only called from |
1244 | the #if 0 part of readgram |
1245 `------------------------------------------------------------------*/
1258 complain (_("invalid %s declaration"), "%type");
1262 name
= xstrdup (token_buffer
);
1277 if (symval
->type_name
== NULL
)
1278 symval
->type_name
= name
;
1279 else if (strcmp (name
, symval
->type_name
) != 0)
1280 complain (_("type redeclaration for %s"), symval
->tag
);
1292 /*------------------------------------------------------------------.
1293 | Parse the input grammar into a one symbol_list structure. Each |
1294 | rule is represented by a sequence of symbols: the left hand side |
1295 | followed by the contents of the right hand side, followed by a |
1296 | null pointer instead of a symbol to terminate the rule. The next |
1297 | symbol is the lhs of the following rule. |
1299 | All guards and actions are copied out to the appropriate files, |
1300 | labelled by the rule number they apply to. |
1301 `------------------------------------------------------------------*/
1312 /* Points to first symbol_list of current rule. its symbol is the
1315 /* Points to the symbol_list preceding crule. */
1316 symbol_list
*crule1
;
1322 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1324 if (t
== IDENTIFIER
|| t
== BAR
)
1326 int action_flag
= 0;
1327 /* Number of symbols in rhs of this rule so far */
1329 int xactions
= 0; /* JF for error checking */
1330 bucket
*first_rhs
= 0;
1332 if (t
== IDENTIFIER
)
1345 complain (_("ill-formed rule: initial symbol not followed by colon"));
1350 if (nrules
== 0 && t
== BAR
)
1352 complain (_("grammar starts with vertical bar"));
1353 lhs
= symval
; /* BOGUS: use a random symval */
1355 /* start a new rule and record its lhs. */
1360 record_rule_line ();
1362 p
= XCALLOC (symbol_list
, 1);
1374 /* mark the rule's lhs as a nonterminal if not already so. */
1376 if (lhs
->class == unknown_sym
)
1378 lhs
->class = nterm_sym
;
1382 else if (lhs
->class == token_sym
)
1383 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1385 /* read the rhs of the rule. */
1393 crule
->ruleprec
= symval
;
1397 if (!(t
== IDENTIFIER
|| t
== LEFT_CURLY
))
1400 /* If next token is an identifier, see if a colon follows it.
1401 If one does, exit this rule now. */
1402 if (t
== IDENTIFIER
)
1414 if (!first_rhs
) /* JF */
1416 /* Not followed by colon =>
1417 process as part of this rule's rhs. */
1420 /* If we just passed an action, that action was in the middle
1421 of a rule, so make a dummy rule to reduce it to a
1427 /* Since the action was written out with this rule's */
1428 /* number, we must give the new rule this number */
1429 /* by inserting the new rule before it. */
1431 /* Make a dummy nonterminal, a gensym. */
1434 /* Make a new rule, whose body is empty,
1435 before the current one, so that the action
1436 just read can belong to it. */
1439 record_rule_line ();
1440 p
= XCALLOC (symbol_list
, 1);
1446 crule1
= XCALLOC (symbol_list
, 1);
1448 crule1
->next
= crule
;
1450 /* insert the dummy generated by that rule into this rule. */
1452 p
= XCALLOC (symbol_list
, 1);
1460 if (t
== IDENTIFIER
)
1463 p
= XCALLOC (symbol_list
, 1);
1468 else /* handle an action. */
1470 copy_action (crule
, rulelength
);
1472 xactions
++; /* JF */
1475 } /* end of read rhs of rule */
1477 /* Put an empty link in the list to mark the end of this rule */
1478 p
= XCALLOC (symbol_list
, 1);
1484 complain (_("two @prec's in a row"));
1486 crule
->ruleprec
= symval
;
1491 if (!semantic_parser
)
1492 complain (_("%%guard present but %%semantic_parser not specified"));
1494 copy_guard (crule
, rulelength
);
1497 else if (t
== LEFT_CURLY
)
1499 /* This case never occurs -wjh */
1501 complain (_("two actions at end of one rule"));
1502 copy_action (crule
, rulelength
);
1504 xactions
++; /* -wjh */
1507 /* If $$ is being set in default way, report if any type
1510 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1512 if (lhs
->type_name
== 0
1513 || first_rhs
->type_name
== 0
1514 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1515 complain (_("type clash (`%s' `%s') on default action"),
1516 lhs
->type_name
? lhs
->type_name
: "",
1517 first_rhs
->type_name
? first_rhs
->type_name
: "");
1519 /* Warn if there is no default for $$ but we need one. */
1520 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1521 complain (_("empty rule for typed nonterminal, and no action"));
1526 /* these things can appear as alternatives to rules. */
1528 a) none of the documentation allows them
1529 b) most of them scan forward until finding a next %
1530 thus they may swallow lots of intervening rules
1532 else if (t
== TOKEN
)
1534 parse_token_decl (token_sym
, nterm_sym
);
1537 else if (t
== NTERM
)
1539 parse_token_decl (nterm_sym
, token_sym
);
1546 else if (t
== UNION
)
1548 parse_union_decl ();
1551 else if (t
== EXPECT
)
1553 parse_expect_decl ();
1556 else if (t
== START
)
1558 parse_start_decl ();
1565 complain (_("invalid input: %s"), token_buffer
);
1570 /* grammar has been read. Do some checking */
1572 if (nsyms
> MAXSHORT
)
1573 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1576 fatal (_("no rules in the input grammar"));
1578 /* JF put out same default YYSTYPE as YACC does */
1580 && !value_components_used
)
1582 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1583 but it seems better to be consistent.
1584 Most programs should declare their own type anyway. */
1585 fprintf (fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1587 fprintf (fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1590 /* Report any undefined symbols and consider them nonterminals. */
1592 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1593 if (bp
->class == unknown_sym
)
1596 ("symbol %s is used, but is not defined as a token and has no rules"),
1598 bp
->class = nterm_sym
;
1599 bp
->value
= nvars
++;
1602 ntokens
= nsyms
- nvars
;
1605 /*--------------------------------------------------------------.
1606 | For named tokens, but not literal ones, define the name. The |
1607 | value is the user token number. |
1608 `--------------------------------------------------------------*/
1611 output_token_defines (FILE *file
)
1617 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1619 symbol
= bp
->tag
; /* get symbol */
1621 if (bp
->value
>= ntokens
)
1623 if (bp
->user_token_number
== SALIAS
)
1625 if ('\'' == *symbol
)
1626 continue; /* skip literal character */
1628 continue; /* skip error token */
1629 if ('\"' == *symbol
)
1631 /* use literal string only if given a symbol with an alias */
1633 symbol
= bp
->alias
->tag
;
1638 /* Don't #define nonliteral tokens whose names contain periods. */
1640 while ((c
= *cp
++) && c
!= '.');
1644 fprintf (file
, "#define\t%s\t%d\n", symbol
,
1645 ((translations
&& !raw_flag
)
1646 ? bp
->user_token_number
: bp
->value
));
1647 if (semantic_parser
)
1648 fprintf (file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1655 /*------------------------------------------------------------------.
1656 | Assign symbol numbers, and write definition of token names into |
1657 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1659 `------------------------------------------------------------------*/
1667 int last_user_token_number
;
1668 static char DOLLAR
[] = "$";
1670 /* int lossage = 0; JF set but not used */
1672 tags
= XCALLOC (char *, nsyms
+ 1);
1674 user_toknums
= XCALLOC (short, nsyms
+ 1);
1675 user_toknums
[0] = 0;
1677 sprec
= XCALLOC (short, nsyms
);
1678 sassoc
= XCALLOC (short, nsyms
);
1680 max_user_token_number
= 256;
1681 last_user_token_number
= 256;
1683 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1685 if (bp
->class == nterm_sym
)
1687 bp
->value
+= ntokens
;
1691 /* this symbol and its alias are a single token defn.
1692 allocate a tokno, and assign to both check agreement of
1693 ->prec and ->assoc fields and make both the same */
1695 bp
->value
= bp
->alias
->value
= tokno
++;
1697 if (bp
->prec
!= bp
->alias
->prec
)
1699 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1700 && bp
->user_token_number
== SALIAS
)
1701 complain (_("conflicting precedences for %s and %s"),
1702 bp
->tag
, bp
->alias
->tag
);
1704 bp
->alias
->prec
= bp
->prec
;
1706 bp
->prec
= bp
->alias
->prec
;
1709 if (bp
->assoc
!= bp
->alias
->assoc
)
1711 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1712 && bp
->user_token_number
== SALIAS
)
1713 complain (_("conflicting assoc values for %s and %s"),
1714 bp
->tag
, bp
->alias
->tag
);
1716 bp
->alias
->assoc
= bp
->assoc
;
1718 bp
->assoc
= bp
->alias
->assoc
;
1721 if (bp
->user_token_number
== SALIAS
)
1722 continue; /* do not do processing below for SALIASs */
1725 else /* bp->class == token_sym */
1727 bp
->value
= tokno
++;
1730 if (bp
->class == token_sym
)
1732 if (translations
&& !(bp
->user_token_number
))
1733 bp
->user_token_number
= ++last_user_token_number
;
1734 if (bp
->user_token_number
> max_user_token_number
)
1735 max_user_token_number
= bp
->user_token_number
;
1738 tags
[bp
->value
] = bp
->tag
;
1739 user_toknums
[bp
->value
] = bp
->user_token_number
;
1740 sprec
[bp
->value
] = bp
->prec
;
1741 sassoc
[bp
->value
] = bp
->assoc
;
1749 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1751 /* initialize all entries for literal tokens to 2, the internal
1752 token number for $undefined., which represents all invalid
1754 for (j
= 0; j
<= max_user_token_number
; j
++)
1755 token_translations
[j
] = 2;
1757 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1759 if (bp
->value
>= ntokens
)
1760 continue; /* non-terminal */
1761 if (bp
->user_token_number
== SALIAS
)
1763 if (token_translations
[bp
->user_token_number
] != 2)
1764 complain (_("tokens %s and %s both assigned number %d"),
1765 tags
[token_translations
[bp
->user_token_number
]],
1766 bp
->tag
, bp
->user_token_number
);
1767 token_translations
[bp
->user_token_number
] = bp
->value
;
1771 error_token_number
= errtoken
->value
;
1773 if (!no_parser_flag
)
1774 output_token_defines (ftable
);
1776 if (startval
->class == unknown_sym
)
1777 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1778 else if (startval
->class == token_sym
)
1779 fatal (_("the start symbol %s is a token"), startval
->tag
);
1781 start_symbol
= startval
->value
;
1785 output_token_defines (fdefines
);
1789 if (spec_name_prefix
)
1790 fprintf (fdefines
, "\nextern YYSTYPE %slval;\n",
1793 fprintf (fdefines
, "\nextern YYSTYPE yylval;\n");
1796 if (semantic_parser
)
1797 for (i
= ntokens
; i
< nsyms
; i
++)
1799 /* don't make these for dummy nonterminals made by gensym. */
1800 if (*tags
[i
] != '@')
1801 fprintf (fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1804 /* `fdefines' is now a temporary file, so we need to copy its
1805 contents in `done', so we can't close it here. */
1813 /*---------------------------------------------------------------.
1814 | Convert the rules into the representation using RRHS, RLHS and |
1816 `---------------------------------------------------------------*/
1827 ritem
= XCALLOC (short, nitems
+ 1);
1828 rlhs
= XCALLOC (short, nrules
) - 1;
1829 rrhs
= XCALLOC (short, nrules
) - 1;
1830 rprec
= XCALLOC (short, nrules
) - 1;
1831 rprecsym
= XCALLOC (short, nrules
) - 1;
1832 rassoc
= XCALLOC (short, nrules
) - 1;
1840 rlhs
[ruleno
] = p
->sym
->value
;
1841 rrhs
[ruleno
] = itemno
;
1842 ruleprec
= p
->ruleprec
;
1847 ritem
[itemno
++] = p
->sym
->value
;
1848 /* A rule gets by default the precedence and associativity
1849 of the last token in it. */
1850 if (p
->sym
->class == token_sym
)
1852 rprec
[ruleno
] = p
->sym
->prec
;
1853 rassoc
[ruleno
] = p
->sym
->assoc
;
1859 /* If this rule has a %prec,
1860 the specified symbol's precedence replaces the default. */
1863 rprec
[ruleno
] = ruleprec
->prec
;
1864 rassoc
[ruleno
] = ruleprec
->assoc
;
1865 rprecsym
[ruleno
] = ruleprec
->value
;
1868 ritem
[itemno
++] = -ruleno
;
1878 /*-------------------------------------------------------------------.
1879 | Read in the grammar specification and record it in the format |
1880 | described in gram.h. All guards are copied into the FGUARD file |
1881 | and all actions into FACTION, in each case forming the body of a C |
1882 | function (YYGUARD or YYACTION) which contains a switch statement |
1883 | to decide which guard or action to execute. |
1884 `-------------------------------------------------------------------*/
1890 startval
= NULL
; /* start symbol not specified yet. */
1893 /* initially assume token number translation not needed. */
1896 /* Nowadays translations is always set to 1, since we give `error' a
1897 user-token-number to satisfy the Posix demand for YYERRCODE==256.
1905 rline_allocated
= 10;
1906 rline
= XCALLOC (short, rline_allocated
);
1913 semantic_parser
= 0;
1921 /* Initialize the symbol table. */
1923 /* Construct the error token */
1924 errtoken
= getsym ("error");
1925 errtoken
->class = token_sym
;
1926 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1927 /* Construct a token that represents all undefined literal tokens.
1928 It is always token number 2. */
1929 undeftoken
= getsym ("$undefined.");
1930 undeftoken
->class = token_sym
;
1931 undeftoken
->user_token_number
= 2;
1933 /* Read the declaration section. Copy %{ ... %} groups to FTABLE
1934 and FDEFINES file. Also notice any %token, %left, etc. found
1936 putc ('\n', ftable
);
1938 /* %s, made from %s\n\
1939 by GNU bison %s. */\n\
1940 \n", no_parser_flag
? "Bison-generated parse tables" : "A Bison parser", infile
, VERSION
);
1942 fputs ("#define YYBISON 1 /* Identify Bison output. */\n\n", ftable
);
1943 read_declarations ();
1944 /* Start writing the guard and action files, if they are needed. */
1946 /* Read in the grammar, build grammar in list form. Write out
1947 guards and actions. */
1949 /* Now we know whether we need the line-number stack. If we do,
1950 write its type into the .tab.h file. */
1952 reader_output_yylsp (fdefines
);
1953 /* Write closing delimiters for actions and guards. */
1956 fputs ("#define YYLSP_NEEDED\n\n", ftable
);
1957 /* Assign the symbols their symbol numbers. Write #defines for the
1958 token symbols into FDEFINES if requested. */
1960 /* Convert the grammar into the format described in gram.h. */
1962 /* Free the symbol table data structure since symbols are now all
1963 referred to by symbol number. */
1969 reader_output_yylsp (FILE *f
)
1975 typedef struct yyltype\n\
1986 # define YYLTYPE yyltype\n\