]>
git.saurik.com Git - bison.git/blob - src/reader.c
e35e4f38a7e890fab831c0febe1ffd7c583f1903
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 | Get the data type (alternative in the union) of the value for |
122 | symbol N in rule RULE. |
123 `--------------------------------------------------------------*/
126 get_type_name (int n
, symbol_list
* rule
)
133 complain (_("invalid $ value"));
143 if (rp
== NULL
|| rp
->sym
== NULL
)
145 complain (_("invalid $ value"));
151 return rp
->sym
->type_name
;
154 /*-------------------------------------------------------------------.
155 | Dump the string from FINPUT to FOUTPUT. MATCH is the delimiter of |
156 | the string (either ' or "). |
157 `-------------------------------------------------------------------*/
160 copy_string (FILE *fin
, FILE *fout
, int match
)
170 fatal (_("unterminated string at end of file"));
173 complain (_("unterminated string"));
175 c
= match
; /* invent terminator */
185 fatal (_("unterminated string at end of file"));
198 /*---------------------------------------------------------------.
199 | Dump the comment from IN to OUT1 and OUT2. C is either `*' or |
200 | `/', depending upon the type of comments used. OUT2 might be |
202 `---------------------------------------------------------------*/
205 copy_comment2 (FILE *in
, FILE *out1
, FILE *out2
, int c
)
210 cplus_comment
= (c
== '/');
219 if (!cplus_comment
&& c
== '*')
249 fatal (_("unterminated comment"));
261 /*------------------------------------------------------------.
262 | Dump the comment from FIN to FOUT. C is either `*' or `/', |
263 | depending upon the type of comments used. |
264 `------------------------------------------------------------*/
267 copy_comment (FILE *fin
, FILE *fout
, int c
)
269 copy_comment2 (fin
, fout
, NULL
, c
);
273 /*-----------------------------------------------------------------.
274 | FIN is pointing to a location (i.e., a `@'). Output to FOUT a |
275 | reference to this location. STACK_OFFSET is the number of values |
276 | in the current rule so far, which says where to find `$0' with |
277 | respect to the top of the stack. |
278 `-----------------------------------------------------------------*/
281 copy_at (FILE *fin
, FILE *fout
, int stack_offset
)
288 fprintf (fout
, "yyloc");
291 else if (isdigit (c
) || c
== '-')
296 n
= read_signed_integer (fin
);
298 fprintf (fout
, "yylsp[%d]", n
- stack_offset
);
305 complain (_("%s is invalid"), quote (buf
));
310 /*-------------------------------------------------------------------.
311 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
313 | Possible inputs: $[<TYPENAME>]($|integer) |
315 | Output to FOUT a reference to this semantic value. STACK_OFFSET is |
316 | the number of values in the current rule so far, which says where |
317 | to find `$0' with respect to the top of the stack. |
318 `-------------------------------------------------------------------*/
321 copy_dollar (FILE *fin
, FILE *fout
,
322 symbol_list
*rule
, int stack_offset
)
325 char *type_name
= NULL
;
327 /* Get the typename if explicit. */
330 char *cp
= token_buffer
;
332 while ((c
= getc (fin
)) != '>' && c
> 0)
334 if (cp
== token_buffer
+ maxtoken
)
335 cp
= grow_token_buffer (cp
);
340 type_name
= token_buffer
;
341 value_components_used
= 1;
348 fprintf (fout
, "yyval");
350 type_name
= get_type_name (0, rule
);
352 fprintf (fout
, ".%s", type_name
);
353 if (!type_name
&& typed
)
354 complain (_("$$ of `%s' has no declared type"),
357 else if (isdigit (c
) || c
== '-')
361 n
= read_signed_integer (fin
);
363 if (!type_name
&& n
> 0)
364 type_name
= get_type_name (n
, rule
);
366 fprintf (fout
, "yyvsp[%d]", n
- stack_offset
);
368 fprintf (fout
, ".%s", type_name
);
369 if (!type_name
&& typed
)
370 complain (_("$%d of `%s' has no declared type"),
377 complain (_("%s is invalid"), quote (buf
));
381 /*-------------------------------------------------------------------.
382 | Copy the contents of a `%{ ... %}' into the definitions file. The |
383 | `%{' has already been read. Return after reading the `%}'. |
384 `-------------------------------------------------------------------*/
387 copy_definition (void)
390 /* -1 while reading a character if prev char was %. */
394 fprintf (fattrs
, "#line %d \"%s\"\n", lineno
, infile
);
415 copy_string (finput
, fattrs
, c
);
421 if (c
!= '*' && c
!= '/')
423 copy_comment (finput
, fattrs
, c
);
427 fatal ("%s", _("unterminated `%{' definition"));
448 /*-------------------------------------------------------------------.
449 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
450 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
452 `-------------------------------------------------------------------*/
455 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
459 struct bucket
*symbol
= NULL
; /* pts to symbol being defined */
463 int tmp_char
= ungetc (skip_white_space (), finput
);
468 fatal (_("Premature EOF after %s"), token_buffer
);
476 if (token
== TYPENAME
)
478 typename
= xstrdup (token_buffer
);
479 value_components_used
= 1;
482 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"' && symbol
)
485 warn (_("symbol `%s' used more than once as a literal string"),
487 else if (symbol
->alias
)
488 warn (_("symbol `%s' given more than one literal string"),
492 symval
->class = token_sym
;
493 symval
->type_name
= typename
;
494 symval
->user_token_number
= symbol
->user_token_number
;
495 symbol
->user_token_number
= SALIAS
;
496 symval
->alias
= symbol
;
497 symbol
->alias
= symval
;
498 /* symbol and symval combined are only one symbol */
504 else if (token
== IDENTIFIER
)
506 int oldclass
= symval
->class;
509 if (symbol
->class == what_is_not
)
510 complain (_("symbol %s redefined"), symbol
->tag
);
511 symbol
->class = what_is
;
512 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
513 symbol
->value
= nvars
++;
517 if (symbol
->type_name
== NULL
)
518 symbol
->type_name
= typename
;
519 else if (strcmp (typename
, symbol
->type_name
) != 0)
520 complain (_("type redeclaration for %s"), symbol
->tag
);
523 else if (symbol
&& token
== NUMBER
)
525 symbol
->user_token_number
= numval
;
530 complain (_("`%s' is invalid in %s"),
531 token_buffer
, (what_is
== token_sym
) ? "%token" : "%nterm");
539 /*------------------------------.
540 | Parse what comes after %start |
541 `------------------------------*/
544 parse_start_decl (void)
547 complain (_("multiple %s declarations"), "%start");
548 if (lex () != IDENTIFIER
)
549 complain (_("invalid %s declaration"), "%start");
557 /*-----------------------------------------------------------.
558 | read in a %type declaration and record its information for |
559 | get_type_name to access |
560 `-----------------------------------------------------------*/
563 parse_type_decl (void)
567 if (lex () != TYPENAME
)
569 complain ("%s", _("%type declaration has no <typename>"));
574 name
= xstrdup (token_buffer
);
579 int tmp_char
= ungetc (skip_white_space (), finput
);
584 fatal (_("Premature EOF after %s"), token_buffer
);
596 if (symval
->type_name
== NULL
)
597 symval
->type_name
= name
;
598 else if (strcmp (name
, symval
->type_name
) != 0)
599 complain (_("type redeclaration for %s"), symval
->tag
);
604 complain (_("invalid %%type declaration due to item: %s"),
613 /*----------------------------------------------------------------.
614 | Read in a %left, %right or %nonassoc declaration and record its |
616 `----------------------------------------------------------------*/
619 parse_assoc_decl (associativity assoc
)
624 lastprec
++; /* Assign a new precedence level, never 0. */
629 int tmp_char
= ungetc (skip_white_space (), finput
);
634 fatal (_("Premature EOF after %s"), token_buffer
);
641 name
= xstrdup (token_buffer
);
648 if (symval
->prec
!= 0)
649 complain (_("redefining precedence of %s"), symval
->tag
);
650 symval
->prec
= lastprec
;
651 symval
->assoc
= assoc
;
652 if (symval
->class == nterm_sym
)
653 complain (_("symbol %s redefined"), symval
->tag
);
654 symval
->class = token_sym
;
656 { /* record the type, if one is specified */
657 if (symval
->type_name
== NULL
)
658 symval
->type_name
= name
;
659 else if (strcmp (name
, symval
->type_name
) != 0)
660 complain (_("type redeclaration for %s"), symval
->tag
);
665 if (prev
== IDENTIFIER
)
667 symval
->user_token_number
= numval
;
673 ("invalid text (%s) - number should be after identifier"),
683 complain (_("unexpected item: %s"), token_buffer
);
694 /*-------------------------------------------------------------------.
695 | Copy the union declaration into fattrs (and fdefines), where it is |
696 | made into the definition of YYSTYPE, the type of elements of the |
697 | parser value stack. |
698 `-------------------------------------------------------------------*/
701 parse_union_decl (void)
707 complain (_("multiple %s declarations"), "%union");
712 fprintf (fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
714 fprintf (fattrs
, "\n");
716 fprintf (fattrs
, "typedef union");
718 fprintf (fdefines
, "typedef union");
736 if (c
!= '*' && c
!= '/')
738 copy_comment2 (finput
, fattrs
, fdefines
, c
);
748 complain (_("unmatched %s"), "`}'");
752 fprintf (fattrs
, " YYSTYPE;\n");
754 fprintf (fdefines
, " YYSTYPE;\n");
755 /* JF don't choke on trailing semi */
756 c
= skip_white_space ();
768 /*-------------------------------------------------------.
769 | Parse the declaration %expect N which says to expect N |
770 | shift-reduce conflicts. |
771 `-------------------------------------------------------*/
774 parse_expect_decl (void)
781 while (c
== ' ' || c
== '\t')
785 while (c
>= '0' && c
<= '9')
787 if (count
< sizeof(buffer
) - 1)
795 if (count
<= 0 || count
> 10)
796 complain (_("argument of %%expect is not an integer"));
797 expected_conflicts
= atoi (buffer
);
801 /*-------------------------------------------------------------------.
802 | Parse what comes after %thong. the full syntax is |
804 | %thong <type> token number literal |
806 | the <type> or number may be omitted. The number specifies the |
807 | user_token_number. |
809 | Two symbols are entered in the table, one for the token symbol and |
810 | one for the literal. Both are given the <type>, if any, from the |
811 | declaration. The ->user_token_number of the first is SALIAS and |
812 | the ->user_token_number of the second is set to the number, if |
813 | any, from the declaration. The two symbols are linked via |
814 | pointers in their ->alias fields. |
816 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
817 | only the literal string is retained it is the literal string that |
818 | is output to yytname |
819 `-------------------------------------------------------------------*/
822 parse_thong_decl (void)
825 struct bucket
*symbol
;
830 token
= lex (); /* fetch typename or first token */
831 if (token
== TYPENAME
)
833 typename
= xstrdup (token_buffer
);
834 value_components_used
= 1;
835 token
= lex (); /* fetch first token */
838 /* process first token */
840 if (token
!= IDENTIFIER
)
842 complain (_("unrecognized item %s, expected an identifier"),
847 symval
->class = token_sym
;
848 symval
->type_name
= typename
;
849 symval
->user_token_number
= SALIAS
;
852 token
= lex (); /* get number or literal string */
857 token
= lex (); /* okay, did number, now get literal */
862 /* process literal string token */
864 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
866 complain (_("expected string constant instead of %s"), token_buffer
);
870 symval
->class = token_sym
;
871 symval
->type_name
= typename
;
872 symval
->user_token_number
= usrtoknum
;
874 symval
->alias
= symbol
;
875 symbol
->alias
= symval
;
877 /* symbol and symval combined are only one symbol. */
882 /*----------------------------------------------------------------.
883 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
884 | any `%' declarations, and copy the contents of any `%{ ... %}' |
885 | groups to fattrs. |
886 `----------------------------------------------------------------*/
889 read_declarations (void)
896 c
= skip_white_space ();
900 tok
= parse_percent_token ();
907 case PERCENT_LEFT_CURLY
:
912 parse_token_decl (token_sym
, nterm_sym
);
916 parse_token_decl (nterm_sym
, token_sym
);
932 parse_expect_decl ();
939 parse_assoc_decl (left_assoc
);
943 parse_assoc_decl (right_assoc
);
947 parse_assoc_decl (non_assoc
);
950 case SEMANTIC_PARSER
:
951 if (semantic_parser
== 0)
966 complain (_("unrecognized: %s"), token_buffer
);
971 fatal (_("no input grammar"));
976 complain (_("unknown character: %s"), quote (buf
));
982 /*-------------------------------------------------------------------.
983 | Assuming that a `{' has just been seen, copy everything up to the |
984 | matching `}' into the actions file. STACK_OFFSET is the number of |
985 | values in the current rule so far, which says where to find `$0' |
986 | with respect to the top of the stack. |
987 `-------------------------------------------------------------------*/
990 copy_action (symbol_list
*rule
, int stack_offset
)
995 /* offset is always 0 if parser has already popped the stack pointer */
999 fprintf (faction
, "\ncase %d:\n", nrules
);
1001 fprintf (faction
, "#line %d \"%s\"\n", lineno
, infile
);
1002 putc ('{', faction
);
1025 copy_string (finput
, faction
, c
);
1031 if (c
!= '*' && c
!= '/')
1033 copy_comment (finput
, faction
, c
);
1037 copy_dollar (finput
, faction
, rule
, stack_offset
);
1041 copy_at (finput
, faction
, stack_offset
);
1045 fatal (_("unmatched %s"), "`{'");
1054 /* above loop exits when c is '}' */
1063 fprintf (faction
, ";\n break;}");
1066 /*-------------------------------------------------------------------.
1067 | After `%guard' is seen in the input file, copy the actual guard |
1068 | into the guards file. If the guard is followed by an action, copy |
1069 | that into the actions file. STACK_OFFSET is the number of values |
1070 | in the current rule so far, which says where to find `$0' with |
1071 | respect to the top of the stack, for the simple parser in which |
1072 | the stack is not popped until after the guard is run. |
1073 `-------------------------------------------------------------------*/
1076 copy_guard (symbol_list
*rule
, int stack_offset
)
1082 /* offset is always 0 if parser has already popped the stack pointer */
1083 if (semantic_parser
)
1086 fprintf (fguard
, "\ncase %d:\n", nrules
);
1088 fprintf (fguard
, "#line %d \"%s\"\n", lineno
, infile
);
1094 while (brace_flag
? (count
> 0) : (c
!= ';'))
1115 complain (_("unmatched %s"), "`}'");
1116 c
= getc (finput
); /* skip it */
1122 copy_string (finput
, fguard
, c
);
1128 if (c
!= '*' && c
!= '/')
1130 copy_comment (finput
, fguard
, c
);
1134 copy_dollar (finput
, fguard
, rule
, stack_offset
);
1138 copy_at (finput
, fguard
, stack_offset
);
1142 fatal ("%s", _("unterminated %guard clause"));
1148 if (c
!= '}' || count
!= 0)
1152 c
= skip_white_space ();
1154 fprintf (fguard
, ";\n break;}");
1156 copy_action (rule
, stack_offset
);
1159 c
= getc (finput
); /* why not skip_white_space -wjh */
1161 copy_action (rule
, stack_offset
);
1169 record_rule_line (void)
1171 /* Record each rule's source line number in rline table. */
1173 if (nrules
>= rline_allocated
)
1175 rline_allocated
= nrules
* 2;
1176 rline
= XREALLOC (rline
, short, rline_allocated
);
1178 rline
[nrules
] = lineno
;
1182 /*-------------------------------------------------------------------.
1183 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1184 | with the user's names. |
1185 `-------------------------------------------------------------------*/
1192 sprintf (token_buffer
, "@%d", ++gensym_count
);
1193 sym
= getsym (token_buffer
);
1194 sym
->class = nterm_sym
;
1195 sym
->value
= nvars
++;
1200 /*------------------------------------------------------------------.
1201 | read in a %type declaration and record its information for |
1202 | get_type_name to access. This is unused. It is only called from |
1203 | the #if 0 part of readgram |
1204 `------------------------------------------------------------------*/
1217 complain (_("invalid %s declaration"), "%type");
1221 name
= xstrdup (token_buffer
);
1236 if (symval
->type_name
== NULL
)
1237 symval
->type_name
= name
;
1238 else if (strcmp (name
, symval
->type_name
) != 0)
1239 complain (_("type redeclaration for %s"), symval
->tag
);
1251 /*------------------------------------------------------------------.
1252 | Parse the input grammar into a one symbol_list structure. Each |
1253 | rule is represented by a sequence of symbols: the left hand side |
1254 | followed by the contents of the right hand side, followed by a |
1255 | null pointer instead of a symbol to terminate the rule. The next |
1256 | symbol is the lhs of the following rule. |
1258 | All guards and actions are copied out to the appropriate files, |
1259 | labelled by the rule number they apply to. |
1260 `------------------------------------------------------------------*/
1271 /* Points to first symbol_list of current rule. its symbol is the
1274 /* Points to the symbol_list preceding crule. */
1275 symbol_list
*crule1
;
1281 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1283 if (t
== IDENTIFIER
|| t
== BAR
)
1285 int action_flag
= 0;
1286 /* Number of symbols in rhs of this rule so far */
1288 int xactions
= 0; /* JF for error checking */
1289 bucket
*first_rhs
= 0;
1291 if (t
== IDENTIFIER
)
1304 complain (_("ill-formed rule: initial symbol not followed by colon"));
1309 if (nrules
== 0 && t
== BAR
)
1311 complain (_("grammar starts with vertical bar"));
1312 lhs
= symval
; /* BOGUS: use a random symval */
1314 /* start a new rule and record its lhs. */
1319 record_rule_line ();
1321 p
= XCALLOC (symbol_list
, 1);
1333 /* mark the rule's lhs as a nonterminal if not already so. */
1335 if (lhs
->class == unknown_sym
)
1337 lhs
->class = nterm_sym
;
1341 else if (lhs
->class == token_sym
)
1342 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1344 /* read the rhs of the rule. */
1352 crule
->ruleprec
= symval
;
1356 if (!(t
== IDENTIFIER
|| t
== LEFT_CURLY
))
1359 /* If next token is an identifier, see if a colon follows it.
1360 If one does, exit this rule now. */
1361 if (t
== IDENTIFIER
)
1373 if (!first_rhs
) /* JF */
1375 /* Not followed by colon =>
1376 process as part of this rule's rhs. */
1379 /* If we just passed an action, that action was in the middle
1380 of a rule, so make a dummy rule to reduce it to a
1386 /* Since the action was written out with this rule's */
1387 /* number, we must give the new rule this number */
1388 /* by inserting the new rule before it. */
1390 /* Make a dummy nonterminal, a gensym. */
1393 /* Make a new rule, whose body is empty,
1394 before the current one, so that the action
1395 just read can belong to it. */
1398 record_rule_line ();
1399 p
= XCALLOC (symbol_list
, 1);
1405 crule1
= XCALLOC (symbol_list
, 1);
1407 crule1
->next
= crule
;
1409 /* insert the dummy generated by that rule into this rule. */
1411 p
= XCALLOC (symbol_list
, 1);
1419 if (t
== IDENTIFIER
)
1422 p
= XCALLOC (symbol_list
, 1);
1427 else /* handle an action. */
1429 copy_action (crule
, rulelength
);
1431 xactions
++; /* JF */
1434 } /* end of read rhs of rule */
1436 /* Put an empty link in the list to mark the end of this rule */
1437 p
= XCALLOC (symbol_list
, 1);
1443 complain (_("two @prec's in a row"));
1445 crule
->ruleprec
= symval
;
1450 if (!semantic_parser
)
1451 complain (_("%%guard present but %%semantic_parser not specified"));
1453 copy_guard (crule
, rulelength
);
1456 else if (t
== LEFT_CURLY
)
1458 /* This case never occurs -wjh */
1460 complain (_("two actions at end of one rule"));
1461 copy_action (crule
, rulelength
);
1463 xactions
++; /* -wjh */
1466 /* If $$ is being set in default way, report if any type
1469 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1471 if (lhs
->type_name
== 0
1472 || first_rhs
->type_name
== 0
1473 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1474 complain (_("type clash (`%s' `%s') on default action"),
1475 lhs
->type_name
? lhs
->type_name
: "",
1476 first_rhs
->type_name
? first_rhs
->type_name
: "");
1478 /* Warn if there is no default for $$ but we need one. */
1479 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1480 complain (_("empty rule for typed nonterminal, and no action"));
1485 /* these things can appear as alternatives to rules. */
1487 a) none of the documentation allows them
1488 b) most of them scan forward until finding a next %
1489 thus they may swallow lots of intervening rules
1491 else if (t
== TOKEN
)
1493 parse_token_decl (token_sym
, nterm_sym
);
1496 else if (t
== NTERM
)
1498 parse_token_decl (nterm_sym
, token_sym
);
1505 else if (t
== UNION
)
1507 parse_union_decl ();
1510 else if (t
== EXPECT
)
1512 parse_expect_decl ();
1515 else if (t
== START
)
1517 parse_start_decl ();
1524 complain (_("invalid input: %s"), token_buffer
);
1529 /* grammar has been read. Do some checking */
1531 if (nsyms
> MAXSHORT
)
1532 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1535 fatal (_("no rules in the input grammar"));
1537 /* JF put out same default YYSTYPE as YACC does */
1539 && !value_components_used
)
1541 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1542 but it seems better to be consistent.
1543 Most programs should declare their own type anyway. */
1544 fprintf (fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1546 fprintf (fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1549 /* Report any undefined symbols and consider them nonterminals. */
1551 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1552 if (bp
->class == unknown_sym
)
1555 ("symbol %s is used, but is not defined as a token and has no rules"),
1557 bp
->class = nterm_sym
;
1558 bp
->value
= nvars
++;
1561 ntokens
= nsyms
- nvars
;
1564 /*--------------------------------------------------------------.
1565 | For named tokens, but not literal ones, define the name. The |
1566 | value is the user token number. |
1567 `--------------------------------------------------------------*/
1570 output_token_defines (FILE *file
)
1576 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1578 symbol
= bp
->tag
; /* get symbol */
1580 if (bp
->value
>= ntokens
)
1582 if (bp
->user_token_number
== SALIAS
)
1584 if ('\'' == *symbol
)
1585 continue; /* skip literal character */
1587 continue; /* skip error token */
1588 if ('\"' == *symbol
)
1590 /* use literal string only if given a symbol with an alias */
1592 symbol
= bp
->alias
->tag
;
1597 /* Don't #define nonliteral tokens whose names contain periods. */
1599 while ((c
= *cp
++) && c
!= '.');
1603 fprintf (file
, "#define\t%s\t%d\n", symbol
,
1604 ((translations
&& !raw_flag
)
1605 ? bp
->user_token_number
: bp
->value
));
1606 if (semantic_parser
)
1607 fprintf (file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1614 /*------------------------------------------------------------------.
1615 | Assign symbol numbers, and write definition of token names into |
1616 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1618 `------------------------------------------------------------------*/
1626 int last_user_token_number
;
1627 static char DOLLAR
[] = "$";
1629 /* int lossage = 0; JF set but not used */
1631 tags
= XCALLOC (char *, nsyms
+ 1);
1633 user_toknums
= XCALLOC (short, nsyms
+ 1);
1634 user_toknums
[0] = 0;
1636 sprec
= XCALLOC (short, nsyms
);
1637 sassoc
= XCALLOC (short, nsyms
);
1639 max_user_token_number
= 256;
1640 last_user_token_number
= 256;
1642 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1644 if (bp
->class == nterm_sym
)
1646 bp
->value
+= ntokens
;
1650 /* this symbol and its alias are a single token defn.
1651 allocate a tokno, and assign to both check agreement of
1652 ->prec and ->assoc fields and make both the same */
1654 bp
->value
= bp
->alias
->value
= tokno
++;
1656 if (bp
->prec
!= bp
->alias
->prec
)
1658 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1659 && bp
->user_token_number
== SALIAS
)
1660 complain (_("conflicting precedences for %s and %s"),
1661 bp
->tag
, bp
->alias
->tag
);
1663 bp
->alias
->prec
= bp
->prec
;
1665 bp
->prec
= bp
->alias
->prec
;
1668 if (bp
->assoc
!= bp
->alias
->assoc
)
1670 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1671 && bp
->user_token_number
== SALIAS
)
1672 complain (_("conflicting assoc values for %s and %s"),
1673 bp
->tag
, bp
->alias
->tag
);
1675 bp
->alias
->assoc
= bp
->assoc
;
1677 bp
->assoc
= bp
->alias
->assoc
;
1680 if (bp
->user_token_number
== SALIAS
)
1681 continue; /* do not do processing below for SALIASs */
1684 else /* bp->class == token_sym */
1686 bp
->value
= tokno
++;
1689 if (bp
->class == token_sym
)
1691 if (translations
&& !(bp
->user_token_number
))
1692 bp
->user_token_number
= ++last_user_token_number
;
1693 if (bp
->user_token_number
> max_user_token_number
)
1694 max_user_token_number
= bp
->user_token_number
;
1697 tags
[bp
->value
] = bp
->tag
;
1698 user_toknums
[bp
->value
] = bp
->user_token_number
;
1699 sprec
[bp
->value
] = bp
->prec
;
1700 sassoc
[bp
->value
] = bp
->assoc
;
1708 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1710 /* initialize all entries for literal tokens to 2, the internal
1711 token number for $undefined., which represents all invalid
1713 for (j
= 0; j
<= max_user_token_number
; j
++)
1714 token_translations
[j
] = 2;
1716 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1718 if (bp
->value
>= ntokens
)
1719 continue; /* non-terminal */
1720 if (bp
->user_token_number
== SALIAS
)
1722 if (token_translations
[bp
->user_token_number
] != 2)
1723 complain (_("tokens %s and %s both assigned number %d"),
1724 tags
[token_translations
[bp
->user_token_number
]],
1725 bp
->tag
, bp
->user_token_number
);
1726 token_translations
[bp
->user_token_number
] = bp
->value
;
1730 error_token_number
= errtoken
->value
;
1732 if (!no_parser_flag
)
1733 output_token_defines (ftable
);
1735 if (startval
->class == unknown_sym
)
1736 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1737 else if (startval
->class == token_sym
)
1738 fatal (_("the start symbol %s is a token"), startval
->tag
);
1740 start_symbol
= startval
->value
;
1744 output_token_defines (fdefines
);
1748 if (spec_name_prefix
)
1749 fprintf (fdefines
, "\nextern YYSTYPE %slval;\n",
1752 fprintf (fdefines
, "\nextern YYSTYPE yylval;\n");
1755 if (semantic_parser
)
1756 for (i
= ntokens
; i
< nsyms
; i
++)
1758 /* don't make these for dummy nonterminals made by gensym. */
1759 if (*tags
[i
] != '@')
1760 fprintf (fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1763 /* `fdefines' is now a temporary file, so we need to copy its
1764 contents in `done', so we can't close it here. */
1772 /*---------------------------------------------------------------.
1773 | Convert the rules into the representation using RRHS, RLHS and |
1775 `---------------------------------------------------------------*/
1786 ritem
= XCALLOC (short, nitems
+ 1);
1787 rlhs
= XCALLOC (short, nrules
) - 1;
1788 rrhs
= XCALLOC (short, nrules
) - 1;
1789 rprec
= XCALLOC (short, nrules
) - 1;
1790 rprecsym
= XCALLOC (short, nrules
) - 1;
1791 rassoc
= XCALLOC (short, nrules
) - 1;
1799 rlhs
[ruleno
] = p
->sym
->value
;
1800 rrhs
[ruleno
] = itemno
;
1801 ruleprec
= p
->ruleprec
;
1806 ritem
[itemno
++] = p
->sym
->value
;
1807 /* A rule gets by default the precedence and associativity
1808 of the last token in it. */
1809 if (p
->sym
->class == token_sym
)
1811 rprec
[ruleno
] = p
->sym
->prec
;
1812 rassoc
[ruleno
] = p
->sym
->assoc
;
1818 /* If this rule has a %prec,
1819 the specified symbol's precedence replaces the default. */
1822 rprec
[ruleno
] = ruleprec
->prec
;
1823 rassoc
[ruleno
] = ruleprec
->assoc
;
1824 rprecsym
[ruleno
] = ruleprec
->value
;
1827 ritem
[itemno
++] = -ruleno
;
1837 /*-------------------------------------------------------------------.
1838 | Read in the grammar specification and record it in the format |
1839 | described in gram.h. All guards are copied into the FGUARD file |
1840 | and all actions into FACTION, in each case forming the body of a C |
1841 | function (YYGUARD or YYACTION) which contains a switch statement |
1842 | to decide which guard or action to execute. |
1843 `-------------------------------------------------------------------*/
1849 startval
= NULL
; /* start symbol not specified yet. */
1852 /* initially assume token number translation not needed. */
1855 /* Nowadays translations is always set to 1, since we give `error' a
1856 user-token-number to satisfy the Posix demand for YYERRCODE==256.
1864 rline_allocated
= 10;
1865 rline
= XCALLOC (short, rline_allocated
);
1872 semantic_parser
= 0;
1880 /* Initialize the symbol table. */
1882 /* Construct the error token */
1883 errtoken
= getsym ("error");
1884 errtoken
->class = token_sym
;
1885 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1886 /* Construct a token that represents all undefined literal tokens.
1887 It is always token number 2. */
1888 undeftoken
= getsym ("$undefined.");
1889 undeftoken
->class = token_sym
;
1890 undeftoken
->user_token_number
= 2;
1892 /* Read the declaration section. Copy %{ ... %} groups to FTABLE
1893 and FDEFINES file. Also notice any %token, %left, etc. found
1895 putc ('\n', ftable
);
1897 /* %s, made from %s\n\
1898 by GNU bison %s. */\n\
1899 \n", no_parser_flag
? "Bison-generated parse tables" : "A Bison parser", infile
, VERSION
);
1901 fputs ("#define YYBISON 1 /* Identify Bison output. */\n\n", ftable
);
1902 read_declarations ();
1903 /* Start writing the guard and action files, if they are needed. */
1905 /* Read in the grammar, build grammar in list form. Write out
1906 guards and actions. */
1908 /* Now we know whether we need the line-number stack. If we do,
1909 write its type into the .tab.h file. */
1911 reader_output_yylsp (fdefines
);
1912 /* Write closing delimiters for actions and guards. */
1915 fputs ("#define YYLSP_NEEDED\n\n", ftable
);
1916 /* Assign the symbols their symbol numbers. Write #defines for the
1917 token symbols into FDEFINES if requested. */
1919 /* Convert the grammar into the format described in gram.h. */
1921 /* Free the symbol table data structure since symbols are now all
1922 referred to by symbol number. */
1928 reader_output_yylsp (FILE *f
)
1934 typedef struct yyltype\n\
1945 # define YYLTYPE yyltype\n\