]>
git.saurik.com Git - bison.git/blob - src/reader.c
c8d48067e527f3665b9067f2dd8a0b63341ab277
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 wannabee comment from IN to OUT1 and OUT2. In fact we |
200 | just saw a `/', which might or might not be a comment. In any |
201 | case, copy what we saw. |
203 | OUT2 might be NULL. |
204 `----------------------------------------------------------------*/
207 copy_comment2 (FILE *fin
, FILE *out1
, FILE *out2
)
213 /* We read a `/', output it. */
218 switch ((c
= getc (fin
)))
239 if (!cplus_comment
&& c
== '*')
269 fatal (_("unterminated comment"));
281 /*-------------------------------------------------------------------.
282 | Dump the comment (actually the current string starting with a `/') |
283 | from FIN to FOUT. |
284 `-------------------------------------------------------------------*/
287 copy_comment (FILE *fin
, FILE *fout
)
289 copy_comment2 (fin
, fout
, NULL
);
293 /*-----------------------------------------------------------------.
294 | FIN is pointing to a location (i.e., a `@'). Output to FOUT a |
295 | reference to this location. STACK_OFFSET is the number of values |
296 | in the current rule so far, which says where to find `$0' with |
297 | respect to the top of the stack. |
298 `-----------------------------------------------------------------*/
301 copy_at (FILE *fin
, FILE *fout
, int stack_offset
)
308 fprintf (fout
, "yyloc");
311 else if (isdigit (c
) || c
== '-')
316 n
= read_signed_integer (fin
);
318 fprintf (fout
, "yylsp[%d]", n
- stack_offset
);
325 complain (_("%s is invalid"), quote (buf
));
330 /*-------------------------------------------------------------------.
331 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
333 | Possible inputs: $[<TYPENAME>]($|integer) |
335 | Output to FOUT a reference to this semantic value. STACK_OFFSET is |
336 | the number of values in the current rule so far, which says where |
337 | to find `$0' with respect to the top of the stack. |
338 `-------------------------------------------------------------------*/
341 copy_dollar (FILE *fin
, FILE *fout
,
342 symbol_list
*rule
, int stack_offset
)
345 char *type_name
= NULL
;
347 /* Get the typename if explicit. */
350 char *cp
= token_buffer
;
352 while ((c
= getc (fin
)) != '>' && c
> 0)
354 if (cp
== token_buffer
+ maxtoken
)
355 cp
= grow_token_buffer (cp
);
360 type_name
= token_buffer
;
361 value_components_used
= 1;
368 fprintf (fout
, "yyval");
370 type_name
= get_type_name (0, rule
);
372 fprintf (fout
, ".%s", type_name
);
373 if (!type_name
&& typed
)
374 complain (_("$$ of `%s' has no declared type"),
377 else if (isdigit (c
) || c
== '-')
381 n
= read_signed_integer (fin
);
383 if (!type_name
&& n
> 0)
384 type_name
= get_type_name (n
, rule
);
386 fprintf (fout
, "yyvsp[%d]", n
- stack_offset
);
388 fprintf (fout
, ".%s", type_name
);
389 if (!type_name
&& typed
)
390 complain (_("$%d of `%s' has no declared type"),
397 complain (_("%s is invalid"), quote (buf
));
401 /*-------------------------------------------------------------------.
402 | Copy the contents of a `%{ ... %}' into the definitions file. The |
403 | `%{' has already been read. Return after reading the `%}'. |
404 `-------------------------------------------------------------------*/
407 copy_definition (void)
410 /* -1 while reading a character if prev char was %. */
414 fprintf (fattrs
, "#line %d \"%s\"\n", lineno
, infile
);
435 copy_string (finput
, fattrs
, c
);
439 copy_comment (finput
, fattrs
);
443 fatal ("%s", _("unterminated `%{' definition"));
464 /*-------------------------------------------------------------------.
465 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
466 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
468 `-------------------------------------------------------------------*/
471 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
475 struct bucket
*symbol
= NULL
; /* pts to symbol being defined */
479 int tmp_char
= ungetc (skip_white_space (), finput
);
484 fatal (_("Premature EOF after %s"), token_buffer
);
492 if (token
== TYPENAME
)
494 typename
= xstrdup (token_buffer
);
495 value_components_used
= 1;
498 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"' && symbol
)
501 warn (_("symbol `%s' used more than once as a literal string"),
503 else if (symbol
->alias
)
504 warn (_("symbol `%s' given more than one literal string"),
508 symval
->class = token_sym
;
509 symval
->type_name
= typename
;
510 symval
->user_token_number
= symbol
->user_token_number
;
511 symbol
->user_token_number
= SALIAS
;
512 symval
->alias
= symbol
;
513 symbol
->alias
= symval
;
514 /* symbol and symval combined are only one symbol */
520 else if (token
== IDENTIFIER
)
522 int oldclass
= symval
->class;
525 if (symbol
->class == what_is_not
)
526 complain (_("symbol %s redefined"), symbol
->tag
);
527 symbol
->class = what_is
;
528 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
529 symbol
->value
= nvars
++;
533 if (symbol
->type_name
== NULL
)
534 symbol
->type_name
= typename
;
535 else if (strcmp (typename
, symbol
->type_name
) != 0)
536 complain (_("type redeclaration for %s"), symbol
->tag
);
539 else if (symbol
&& token
== NUMBER
)
541 symbol
->user_token_number
= numval
;
546 complain (_("`%s' is invalid in %s"),
547 token_buffer
, (what_is
== token_sym
) ? "%token" : "%nterm");
555 /*------------------------------.
556 | Parse what comes after %start |
557 `------------------------------*/
560 parse_start_decl (void)
563 complain (_("multiple %s declarations"), "%start");
564 if (lex () != IDENTIFIER
)
565 complain (_("invalid %s declaration"), "%start");
573 /*-----------------------------------------------------------.
574 | read in a %type declaration and record its information for |
575 | get_type_name to access |
576 `-----------------------------------------------------------*/
579 parse_type_decl (void)
583 if (lex () != TYPENAME
)
585 complain ("%s", _("%type declaration has no <typename>"));
590 name
= xstrdup (token_buffer
);
595 int tmp_char
= ungetc (skip_white_space (), finput
);
600 fatal (_("Premature EOF after %s"), token_buffer
);
612 if (symval
->type_name
== NULL
)
613 symval
->type_name
= name
;
614 else if (strcmp (name
, symval
->type_name
) != 0)
615 complain (_("type redeclaration for %s"), symval
->tag
);
620 complain (_("invalid %%type declaration due to item: %s"),
629 /*----------------------------------------------------------------.
630 | Read in a %left, %right or %nonassoc declaration and record its |
632 `----------------------------------------------------------------*/
635 parse_assoc_decl (associativity assoc
)
640 lastprec
++; /* Assign a new precedence level, never 0. */
645 int tmp_char
= ungetc (skip_white_space (), finput
);
650 fatal (_("Premature EOF after %s"), token_buffer
);
657 name
= xstrdup (token_buffer
);
664 if (symval
->prec
!= 0)
665 complain (_("redefining precedence of %s"), symval
->tag
);
666 symval
->prec
= lastprec
;
667 symval
->assoc
= assoc
;
668 if (symval
->class == nterm_sym
)
669 complain (_("symbol %s redefined"), symval
->tag
);
670 symval
->class = token_sym
;
672 { /* record the type, if one is specified */
673 if (symval
->type_name
== NULL
)
674 symval
->type_name
= name
;
675 else if (strcmp (name
, symval
->type_name
) != 0)
676 complain (_("type redeclaration for %s"), symval
->tag
);
681 if (prev
== IDENTIFIER
)
683 symval
->user_token_number
= numval
;
689 ("invalid text (%s) - number should be after identifier"),
699 complain (_("unexpected item: %s"), token_buffer
);
710 /*-------------------------------------------------------------------.
711 | Copy the union declaration into fattrs (and fdefines), where it is |
712 | made into the definition of YYSTYPE, the type of elements of the |
713 | parser value stack. |
714 `-------------------------------------------------------------------*/
717 parse_union_decl (void)
723 complain (_("multiple %s declarations"), "%union");
728 fprintf (fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
730 fprintf (fattrs
, "\n");
732 fprintf (fattrs
, "typedef union");
734 fprintf (fdefines
, "typedef union");
751 copy_comment2 (finput
, fattrs
, fdefines
);
760 complain (_("unmatched %s"), "`}'");
764 fprintf (fattrs
, " YYSTYPE;\n");
766 fprintf (fdefines
, " YYSTYPE;\n");
767 /* JF don't choke on trailing semi */
768 c
= skip_white_space ();
780 /*-------------------------------------------------------.
781 | Parse the declaration %expect N which says to expect N |
782 | shift-reduce conflicts. |
783 `-------------------------------------------------------*/
786 parse_expect_decl (void)
788 int c
= skip_white_space ();
792 complain (_("argument of %%expect is not an integer"));
794 expected_conflicts
= read_signed_integer (finput
);
798 /*-------------------------------------------------------------------.
799 | Parse what comes after %thong. the full syntax is |
801 | %thong <type> token number literal |
803 | the <type> or number may be omitted. The number specifies the |
804 | user_token_number. |
806 | Two symbols are entered in the table, one for the token symbol and |
807 | one for the literal. Both are given the <type>, if any, from the |
808 | declaration. The ->user_token_number of the first is SALIAS and |
809 | the ->user_token_number of the second is set to the number, if |
810 | any, from the declaration. The two symbols are linked via |
811 | pointers in their ->alias fields. |
813 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
814 | only the literal string is retained it is the literal string that |
815 | is output to yytname |
816 `-------------------------------------------------------------------*/
819 parse_thong_decl (void)
822 struct bucket
*symbol
;
827 token
= lex (); /* fetch typename or first token */
828 if (token
== TYPENAME
)
830 typename
= xstrdup (token_buffer
);
831 value_components_used
= 1;
832 token
= lex (); /* fetch first token */
835 /* process first token */
837 if (token
!= IDENTIFIER
)
839 complain (_("unrecognized item %s, expected an identifier"),
844 symval
->class = token_sym
;
845 symval
->type_name
= typename
;
846 symval
->user_token_number
= SALIAS
;
849 token
= lex (); /* get number or literal string */
854 token
= lex (); /* okay, did number, now get literal */
859 /* process literal string token */
861 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
863 complain (_("expected string constant instead of %s"), token_buffer
);
867 symval
->class = token_sym
;
868 symval
->type_name
= typename
;
869 symval
->user_token_number
= usrtoknum
;
871 symval
->alias
= symbol
;
872 symbol
->alias
= symval
;
874 /* symbol and symval combined are only one symbol. */
879 /*----------------------------------------------------------------.
880 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
881 | any `%' declarations, and copy the contents of any `%{ ... %}' |
882 | groups to fattrs. |
883 `----------------------------------------------------------------*/
886 read_declarations (void)
893 c
= skip_white_space ();
897 tok
= parse_percent_token ();
904 case PERCENT_LEFT_CURLY
:
909 parse_token_decl (token_sym
, nterm_sym
);
913 parse_token_decl (nterm_sym
, token_sym
);
929 parse_expect_decl ();
936 parse_assoc_decl (left_assoc
);
940 parse_assoc_decl (right_assoc
);
944 parse_assoc_decl (non_assoc
);
947 case SEMANTIC_PARSER
:
948 if (semantic_parser
== 0)
963 complain (_("unrecognized: %s"), token_buffer
);
968 fatal (_("no input grammar"));
973 complain (_("unknown character: %s"), quote (buf
));
979 /*-------------------------------------------------------------------.
980 | Assuming that a `{' has just been seen, copy everything up to the |
981 | matching `}' into the actions file. STACK_OFFSET is the number of |
982 | values in the current rule so far, which says where to find `$0' |
983 | with respect to the top of the stack. |
984 `-------------------------------------------------------------------*/
987 copy_action (symbol_list
*rule
, int stack_offset
)
992 /* offset is always 0 if parser has already popped the stack pointer */
996 fprintf (faction
, "\ncase %d:\n", nrules
);
998 fprintf (faction
, "#line %d \"%s\"\n", lineno
, infile
);
1022 copy_string (finput
, faction
, c
);
1026 copy_comment (finput
, faction
);
1030 copy_dollar (finput
, faction
, rule
, stack_offset
);
1034 copy_at (finput
, faction
, stack_offset
);
1038 fatal (_("unmatched %s"), "`{'");
1047 /* above loop exits when c is '}' */
1056 fprintf (faction
, ";\n break;}");
1059 /*-------------------------------------------------------------------.
1060 | After `%guard' is seen in the input file, copy the actual guard |
1061 | into the guards file. If the guard is followed by an action, copy |
1062 | that into the actions file. STACK_OFFSET is the number of values |
1063 | in the current rule so far, which says where to find `$0' with |
1064 | respect to the top of the stack, for the simple parser in which |
1065 | the stack is not popped until after the guard is run. |
1066 `-------------------------------------------------------------------*/
1069 copy_guard (symbol_list
*rule
, int stack_offset
)
1075 /* offset is always 0 if parser has already popped the stack pointer */
1076 if (semantic_parser
)
1079 fprintf (fguard
, "\ncase %d:\n", nrules
);
1081 fprintf (fguard
, "#line %d \"%s\"\n", lineno
, infile
);
1087 while (brace_flag
? (count
> 0) : (c
!= ';'))
1108 complain (_("unmatched %s"), "`}'");
1109 c
= getc (finput
); /* skip it */
1115 copy_string (finput
, fguard
, c
);
1119 copy_comment (finput
, fguard
);
1123 copy_dollar (finput
, fguard
, rule
, stack_offset
);
1127 copy_at (finput
, fguard
, stack_offset
);
1131 fatal ("%s", _("unterminated %guard clause"));
1137 if (c
!= '}' || count
!= 0)
1141 c
= skip_white_space ();
1143 fprintf (fguard
, ";\n break;}");
1145 copy_action (rule
, stack_offset
);
1148 c
= getc (finput
); /* why not skip_white_space -wjh */
1150 copy_action (rule
, stack_offset
);
1158 record_rule_line (void)
1160 /* Record each rule's source line number in rline table. */
1162 if (nrules
>= rline_allocated
)
1164 rline_allocated
= nrules
* 2;
1165 rline
= XREALLOC (rline
, short, rline_allocated
);
1167 rline
[nrules
] = lineno
;
1171 /*-------------------------------------------------------------------.
1172 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1173 | with the user's names. |
1174 `-------------------------------------------------------------------*/
1181 sprintf (token_buffer
, "@%d", ++gensym_count
);
1182 sym
= getsym (token_buffer
);
1183 sym
->class = nterm_sym
;
1184 sym
->value
= nvars
++;
1189 /*------------------------------------------------------------------.
1190 | read in a %type declaration and record its information for |
1191 | get_type_name to access. This is unused. It is only called from |
1192 | the #if 0 part of readgram |
1193 `------------------------------------------------------------------*/
1206 complain (_("invalid %s declaration"), "%type");
1210 name
= xstrdup (token_buffer
);
1225 if (symval
->type_name
== NULL
)
1226 symval
->type_name
= name
;
1227 else if (strcmp (name
, symval
->type_name
) != 0)
1228 complain (_("type redeclaration for %s"), symval
->tag
);
1240 /*------------------------------------------------------------------.
1241 | Parse the input grammar into a one symbol_list structure. Each |
1242 | rule is represented by a sequence of symbols: the left hand side |
1243 | followed by the contents of the right hand side, followed by a |
1244 | null pointer instead of a symbol to terminate the rule. The next |
1245 | symbol is the lhs of the following rule. |
1247 | All guards and actions are copied out to the appropriate files, |
1248 | labelled by the rule number they apply to. |
1249 `------------------------------------------------------------------*/
1260 /* Points to first symbol_list of current rule. its symbol is the
1263 /* Points to the symbol_list preceding crule. */
1264 symbol_list
*crule1
;
1270 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1272 if (t
== IDENTIFIER
|| t
== BAR
)
1274 int action_flag
= 0;
1275 /* Number of symbols in rhs of this rule so far */
1277 int xactions
= 0; /* JF for error checking */
1278 bucket
*first_rhs
= 0;
1280 if (t
== IDENTIFIER
)
1293 complain (_("ill-formed rule: initial symbol not followed by colon"));
1298 if (nrules
== 0 && t
== BAR
)
1300 complain (_("grammar starts with vertical bar"));
1301 lhs
= symval
; /* BOGUS: use a random symval */
1303 /* start a new rule and record its lhs. */
1308 record_rule_line ();
1310 p
= XCALLOC (symbol_list
, 1);
1322 /* mark the rule's lhs as a nonterminal if not already so. */
1324 if (lhs
->class == unknown_sym
)
1326 lhs
->class = nterm_sym
;
1330 else if (lhs
->class == token_sym
)
1331 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1333 /* read the rhs of the rule. */
1341 crule
->ruleprec
= symval
;
1345 if (!(t
== IDENTIFIER
|| t
== LEFT_CURLY
))
1348 /* If next token is an identifier, see if a colon follows it.
1349 If one does, exit this rule now. */
1350 if (t
== IDENTIFIER
)
1362 if (!first_rhs
) /* JF */
1364 /* Not followed by colon =>
1365 process as part of this rule's rhs. */
1368 /* If we just passed an action, that action was in the middle
1369 of a rule, so make a dummy rule to reduce it to a
1375 /* Since the action was written out with this rule's */
1376 /* number, we must give the new rule this number */
1377 /* by inserting the new rule before it. */
1379 /* Make a dummy nonterminal, a gensym. */
1382 /* Make a new rule, whose body is empty,
1383 before the current one, so that the action
1384 just read can belong to it. */
1387 record_rule_line ();
1388 p
= XCALLOC (symbol_list
, 1);
1394 crule1
= XCALLOC (symbol_list
, 1);
1396 crule1
->next
= crule
;
1398 /* insert the dummy generated by that rule into this rule. */
1400 p
= XCALLOC (symbol_list
, 1);
1408 if (t
== IDENTIFIER
)
1411 p
= XCALLOC (symbol_list
, 1);
1416 else /* handle an action. */
1418 copy_action (crule
, rulelength
);
1420 xactions
++; /* JF */
1423 } /* end of read rhs of rule */
1425 /* Put an empty link in the list to mark the end of this rule */
1426 p
= XCALLOC (symbol_list
, 1);
1432 complain (_("two @prec's in a row"));
1434 crule
->ruleprec
= symval
;
1439 if (!semantic_parser
)
1440 complain (_("%%guard present but %%semantic_parser not specified"));
1442 copy_guard (crule
, rulelength
);
1445 else if (t
== LEFT_CURLY
)
1447 /* This case never occurs -wjh */
1449 complain (_("two actions at end of one rule"));
1450 copy_action (crule
, rulelength
);
1452 xactions
++; /* -wjh */
1455 /* If $$ is being set in default way, report if any type
1458 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1460 if (lhs
->type_name
== 0
1461 || first_rhs
->type_name
== 0
1462 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1463 complain (_("type clash (`%s' `%s') on default action"),
1464 lhs
->type_name
? lhs
->type_name
: "",
1465 first_rhs
->type_name
? first_rhs
->type_name
: "");
1467 /* Warn if there is no default for $$ but we need one. */
1468 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1469 complain (_("empty rule for typed nonterminal, and no action"));
1474 /* these things can appear as alternatives to rules. */
1476 a) none of the documentation allows them
1477 b) most of them scan forward until finding a next %
1478 thus they may swallow lots of intervening rules
1480 else if (t
== TOKEN
)
1482 parse_token_decl (token_sym
, nterm_sym
);
1485 else if (t
== NTERM
)
1487 parse_token_decl (nterm_sym
, token_sym
);
1494 else if (t
== UNION
)
1496 parse_union_decl ();
1499 else if (t
== EXPECT
)
1501 parse_expect_decl ();
1504 else if (t
== START
)
1506 parse_start_decl ();
1513 complain (_("invalid input: %s"), token_buffer
);
1518 /* grammar has been read. Do some checking */
1520 if (nsyms
> MAXSHORT
)
1521 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1524 fatal (_("no rules in the input grammar"));
1526 /* JF put out same default YYSTYPE as YACC does */
1528 && !value_components_used
)
1530 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1531 but it seems better to be consistent.
1532 Most programs should declare their own type anyway. */
1533 fprintf (fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1535 fprintf (fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1538 /* Report any undefined symbols and consider them nonterminals. */
1540 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1541 if (bp
->class == unknown_sym
)
1544 ("symbol %s is used, but is not defined as a token and has no rules"),
1546 bp
->class = nterm_sym
;
1547 bp
->value
= nvars
++;
1550 ntokens
= nsyms
- nvars
;
1553 /*--------------------------------------------------------------.
1554 | For named tokens, but not literal ones, define the name. The |
1555 | value is the user token number. |
1556 `--------------------------------------------------------------*/
1559 output_token_defines (FILE *file
)
1565 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1567 symbol
= bp
->tag
; /* get symbol */
1569 if (bp
->value
>= ntokens
)
1571 if (bp
->user_token_number
== SALIAS
)
1573 if ('\'' == *symbol
)
1574 continue; /* skip literal character */
1576 continue; /* skip error token */
1577 if ('\"' == *symbol
)
1579 /* use literal string only if given a symbol with an alias */
1581 symbol
= bp
->alias
->tag
;
1586 /* Don't #define nonliteral tokens whose names contain periods. */
1588 while ((c
= *cp
++) && c
!= '.');
1592 fprintf (file
, "#define\t%s\t%d\n", symbol
,
1593 ((translations
&& !raw_flag
)
1594 ? bp
->user_token_number
: bp
->value
));
1595 if (semantic_parser
)
1596 fprintf (file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1603 /*------------------------------------------------------------------.
1604 | Assign symbol numbers, and write definition of token names into |
1605 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1607 `------------------------------------------------------------------*/
1615 int last_user_token_number
;
1616 static char DOLLAR
[] = "$";
1618 /* int lossage = 0; JF set but not used */
1620 tags
= XCALLOC (char *, nsyms
+ 1);
1622 user_toknums
= XCALLOC (short, nsyms
+ 1);
1623 user_toknums
[0] = 0;
1625 sprec
= XCALLOC (short, nsyms
);
1626 sassoc
= XCALLOC (short, nsyms
);
1628 max_user_token_number
= 256;
1629 last_user_token_number
= 256;
1631 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1633 if (bp
->class == nterm_sym
)
1635 bp
->value
+= ntokens
;
1639 /* this symbol and its alias are a single token defn.
1640 allocate a tokno, and assign to both check agreement of
1641 ->prec and ->assoc fields and make both the same */
1643 bp
->value
= bp
->alias
->value
= tokno
++;
1645 if (bp
->prec
!= bp
->alias
->prec
)
1647 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1648 && bp
->user_token_number
== SALIAS
)
1649 complain (_("conflicting precedences for %s and %s"),
1650 bp
->tag
, bp
->alias
->tag
);
1652 bp
->alias
->prec
= bp
->prec
;
1654 bp
->prec
= bp
->alias
->prec
;
1657 if (bp
->assoc
!= bp
->alias
->assoc
)
1659 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1660 && bp
->user_token_number
== SALIAS
)
1661 complain (_("conflicting assoc values for %s and %s"),
1662 bp
->tag
, bp
->alias
->tag
);
1664 bp
->alias
->assoc
= bp
->assoc
;
1666 bp
->assoc
= bp
->alias
->assoc
;
1669 if (bp
->user_token_number
== SALIAS
)
1670 continue; /* do not do processing below for SALIASs */
1673 else /* bp->class == token_sym */
1675 bp
->value
= tokno
++;
1678 if (bp
->class == token_sym
)
1680 if (translations
&& !(bp
->user_token_number
))
1681 bp
->user_token_number
= ++last_user_token_number
;
1682 if (bp
->user_token_number
> max_user_token_number
)
1683 max_user_token_number
= bp
->user_token_number
;
1686 tags
[bp
->value
] = bp
->tag
;
1687 user_toknums
[bp
->value
] = bp
->user_token_number
;
1688 sprec
[bp
->value
] = bp
->prec
;
1689 sassoc
[bp
->value
] = bp
->assoc
;
1697 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1699 /* initialize all entries for literal tokens to 2, the internal
1700 token number for $undefined., which represents all invalid
1702 for (j
= 0; j
<= max_user_token_number
; j
++)
1703 token_translations
[j
] = 2;
1705 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1707 if (bp
->value
>= ntokens
)
1708 continue; /* non-terminal */
1709 if (bp
->user_token_number
== SALIAS
)
1711 if (token_translations
[bp
->user_token_number
] != 2)
1712 complain (_("tokens %s and %s both assigned number %d"),
1713 tags
[token_translations
[bp
->user_token_number
]],
1714 bp
->tag
, bp
->user_token_number
);
1715 token_translations
[bp
->user_token_number
] = bp
->value
;
1719 error_token_number
= errtoken
->value
;
1721 if (!no_parser_flag
)
1722 output_token_defines (ftable
);
1724 if (startval
->class == unknown_sym
)
1725 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1726 else if (startval
->class == token_sym
)
1727 fatal (_("the start symbol %s is a token"), startval
->tag
);
1729 start_symbol
= startval
->value
;
1733 output_token_defines (fdefines
);
1737 if (spec_name_prefix
)
1738 fprintf (fdefines
, "\nextern YYSTYPE %slval;\n",
1741 fprintf (fdefines
, "\nextern YYSTYPE yylval;\n");
1744 if (semantic_parser
)
1745 for (i
= ntokens
; i
< nsyms
; i
++)
1747 /* don't make these for dummy nonterminals made by gensym. */
1748 if (*tags
[i
] != '@')
1749 fprintf (fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1752 /* `fdefines' is now a temporary file, so we need to copy its
1753 contents in `done', so we can't close it here. */
1761 /*---------------------------------------------------------------.
1762 | Convert the rules into the representation using RRHS, RLHS and |
1764 `---------------------------------------------------------------*/
1775 ritem
= XCALLOC (short, nitems
+ 1);
1776 rlhs
= XCALLOC (short, nrules
) - 1;
1777 rrhs
= XCALLOC (short, nrules
) - 1;
1778 rprec
= XCALLOC (short, nrules
) - 1;
1779 rprecsym
= XCALLOC (short, nrules
) - 1;
1780 rassoc
= XCALLOC (short, nrules
) - 1;
1788 rlhs
[ruleno
] = p
->sym
->value
;
1789 rrhs
[ruleno
] = itemno
;
1790 ruleprec
= p
->ruleprec
;
1795 ritem
[itemno
++] = p
->sym
->value
;
1796 /* A rule gets by default the precedence and associativity
1797 of the last token in it. */
1798 if (p
->sym
->class == token_sym
)
1800 rprec
[ruleno
] = p
->sym
->prec
;
1801 rassoc
[ruleno
] = p
->sym
->assoc
;
1807 /* If this rule has a %prec,
1808 the specified symbol's precedence replaces the default. */
1811 rprec
[ruleno
] = ruleprec
->prec
;
1812 rassoc
[ruleno
] = ruleprec
->assoc
;
1813 rprecsym
[ruleno
] = ruleprec
->value
;
1816 ritem
[itemno
++] = -ruleno
;
1826 /*-------------------------------------------------------------------.
1827 | Read in the grammar specification and record it in the format |
1828 | described in gram.h. All guards are copied into the FGUARD file |
1829 | and all actions into FACTION, in each case forming the body of a C |
1830 | function (YYGUARD or YYACTION) which contains a switch statement |
1831 | to decide which guard or action to execute. |
1832 `-------------------------------------------------------------------*/
1838 startval
= NULL
; /* start symbol not specified yet. */
1841 /* initially assume token number translation not needed. */
1844 /* Nowadays translations is always set to 1, since we give `error' a
1845 user-token-number to satisfy the Posix demand for YYERRCODE==256.
1853 rline_allocated
= 10;
1854 rline
= XCALLOC (short, rline_allocated
);
1861 semantic_parser
= 0;
1869 /* Initialize the symbol table. */
1871 /* Construct the error token */
1872 errtoken
= getsym ("error");
1873 errtoken
->class = token_sym
;
1874 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1875 /* Construct a token that represents all undefined literal tokens.
1876 It is always token number 2. */
1877 undeftoken
= getsym ("$undefined.");
1878 undeftoken
->class = token_sym
;
1879 undeftoken
->user_token_number
= 2;
1881 /* Read the declaration section. Copy %{ ... %} groups to FTABLE
1882 and FDEFINES file. Also notice any %token, %left, etc. found
1884 putc ('\n', ftable
);
1886 /* %s, made from %s\n\
1887 by GNU bison %s. */\n\
1888 \n", no_parser_flag
? "Bison-generated parse tables" : "A Bison parser", infile
, VERSION
);
1890 fputs ("#define YYBISON 1 /* Identify Bison output. */\n\n", ftable
);
1891 read_declarations ();
1892 /* Start writing the guard and action files, if they are needed. */
1894 /* Read in the grammar, build grammar in list form. Write out
1895 guards and actions. */
1897 /* Now we know whether we need the line-number stack. If we do,
1898 write its type into the .tab.h file. */
1900 reader_output_yylsp (fdefines
);
1901 /* Write closing delimiters for actions and guards. */
1904 fputs ("#define YYLSP_NEEDED\n\n", ftable
);
1905 /* Assign the symbols their symbol numbers. Write #defines for the
1906 token symbols into FDEFINES if requested. */
1908 /* Convert the grammar into the format described in gram.h. */
1910 /* Free the symbol table data structure since symbols are now all
1911 referred to by symbol number. */
1917 reader_output_yylsp (FILE *f
)
1923 typedef struct yyltype\n\
1934 # define YYLTYPE yyltype\n\