]>
git.saurik.com Git - bison.git/blob - src/reader.c
0b2f643f5cce8543bb523d806ed13a3d5fc74c76
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 type name if explicit. */
350 read_type_name (fin
);
351 type_name
= token_buffer
;
352 value_components_used
= 1;
358 fprintf (fout
, "yyval");
360 type_name
= get_type_name (0, rule
);
362 fprintf (fout
, ".%s", type_name
);
363 if (!type_name
&& typed
)
364 complain (_("$$ of `%s' has no declared type"),
367 else if (isdigit (c
) || c
== '-')
371 n
= read_signed_integer (fin
);
373 if (!type_name
&& n
> 0)
374 type_name
= get_type_name (n
, rule
);
376 fprintf (fout
, "yyvsp[%d]", n
- stack_offset
);
378 fprintf (fout
, ".%s", type_name
);
379 if (!type_name
&& typed
)
380 complain (_("$%d of `%s' has no declared type"),
387 complain (_("%s is invalid"), quote (buf
));
391 /*-------------------------------------------------------------------.
392 | Copy the contents of a `%{ ... %}' into the definitions file. The |
393 | `%{' has already been read. Return after reading the `%}'. |
394 `-------------------------------------------------------------------*/
397 copy_definition (void)
400 /* -1 while reading a character if prev char was %. */
404 fprintf (fattrs
, "#line %d \"%s\"\n", lineno
, infile
);
425 copy_string (finput
, fattrs
, c
);
429 copy_comment (finput
, fattrs
);
433 fatal ("%s", _("unterminated `%{' definition"));
454 /*-------------------------------------------------------------------.
455 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
456 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
458 `-------------------------------------------------------------------*/
461 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
465 struct bucket
*symbol
= NULL
; /* pts to symbol being defined */
469 int tmp_char
= ungetc (skip_white_space (), finput
);
474 fatal (_("Premature EOF after %s"), token_buffer
);
482 if (token
== TYPENAME
)
484 typename
= xstrdup (token_buffer
);
485 value_components_used
= 1;
488 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"' && symbol
)
491 warn (_("symbol `%s' used more than once as a literal string"),
493 else if (symbol
->alias
)
494 warn (_("symbol `%s' given more than one literal string"),
498 symval
->class = token_sym
;
499 symval
->type_name
= typename
;
500 symval
->user_token_number
= symbol
->user_token_number
;
501 symbol
->user_token_number
= SALIAS
;
502 symval
->alias
= symbol
;
503 symbol
->alias
= symval
;
504 /* symbol and symval combined are only one symbol */
510 else if (token
== IDENTIFIER
)
512 int oldclass
= symval
->class;
515 if (symbol
->class == what_is_not
)
516 complain (_("symbol %s redefined"), symbol
->tag
);
517 symbol
->class = what_is
;
518 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
519 symbol
->value
= nvars
++;
523 if (symbol
->type_name
== NULL
)
524 symbol
->type_name
= typename
;
525 else if (strcmp (typename
, symbol
->type_name
) != 0)
526 complain (_("type redeclaration for %s"), symbol
->tag
);
529 else if (symbol
&& token
== NUMBER
)
531 symbol
->user_token_number
= numval
;
536 complain (_("`%s' is invalid in %s"),
537 token_buffer
, (what_is
== token_sym
) ? "%token" : "%nterm");
545 /*------------------------------.
546 | Parse what comes after %start |
547 `------------------------------*/
550 parse_start_decl (void)
553 complain (_("multiple %s declarations"), "%start");
554 if (lex () != IDENTIFIER
)
555 complain (_("invalid %s declaration"), "%start");
563 /*-----------------------------------------------------------.
564 | read in a %type declaration and record its information for |
565 | get_type_name to access |
566 `-----------------------------------------------------------*/
569 parse_type_decl (void)
573 if (lex () != TYPENAME
)
575 complain ("%s", _("%type declaration has no <typename>"));
580 name
= xstrdup (token_buffer
);
585 int tmp_char
= ungetc (skip_white_space (), finput
);
590 fatal (_("Premature EOF after %s"), token_buffer
);
602 if (symval
->type_name
== NULL
)
603 symval
->type_name
= name
;
604 else if (strcmp (name
, symval
->type_name
) != 0)
605 complain (_("type redeclaration for %s"), symval
->tag
);
610 complain (_("invalid %%type declaration due to item: %s"),
619 /*----------------------------------------------------------------.
620 | Read in a %left, %right or %nonassoc declaration and record its |
622 `----------------------------------------------------------------*/
625 parse_assoc_decl (associativity assoc
)
630 lastprec
++; /* Assign a new precedence level, never 0. */
635 int tmp_char
= ungetc (skip_white_space (), finput
);
640 fatal (_("Premature EOF after %s"), token_buffer
);
647 name
= xstrdup (token_buffer
);
654 if (symval
->prec
!= 0)
655 complain (_("redefining precedence of %s"), symval
->tag
);
656 symval
->prec
= lastprec
;
657 symval
->assoc
= assoc
;
658 if (symval
->class == nterm_sym
)
659 complain (_("symbol %s redefined"), symval
->tag
);
660 symval
->class = token_sym
;
662 { /* record the type, if one is specified */
663 if (symval
->type_name
== NULL
)
664 symval
->type_name
= name
;
665 else if (strcmp (name
, symval
->type_name
) != 0)
666 complain (_("type redeclaration for %s"), symval
->tag
);
671 if (prev
== IDENTIFIER
)
673 symval
->user_token_number
= numval
;
679 ("invalid text (%s) - number should be after identifier"),
689 complain (_("unexpected item: %s"), token_buffer
);
700 /*-------------------------------------------------------------------.
701 | Copy the union declaration into fattrs (and fdefines), where it is |
702 | made into the definition of YYSTYPE, the type of elements of the |
703 | parser value stack. |
704 `-------------------------------------------------------------------*/
707 parse_union_decl (void)
713 complain (_("multiple %s declarations"), "%union");
718 fprintf (fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
720 fprintf (fattrs
, "\n");
722 fprintf (fattrs
, "typedef union");
724 fprintf (fdefines
, "typedef union");
741 copy_comment2 (finput
, fattrs
, fdefines
);
750 complain (_("unmatched %s"), "`}'");
754 fprintf (fattrs
, " YYSTYPE;\n");
756 fprintf (fdefines
, " YYSTYPE;\n");
757 /* JF don't choke on trailing semi */
758 c
= skip_white_space ();
770 /*-------------------------------------------------------.
771 | Parse the declaration %expect N which says to expect N |
772 | shift-reduce conflicts. |
773 `-------------------------------------------------------*/
776 parse_expect_decl (void)
778 int c
= skip_white_space ();
782 complain (_("argument of %%expect is not an integer"));
784 expected_conflicts
= read_signed_integer (finput
);
788 /*-------------------------------------------------------------------.
789 | Parse what comes after %thong. the full syntax is |
791 | %thong <type> token number literal |
793 | the <type> or number may be omitted. The number specifies the |
794 | user_token_number. |
796 | Two symbols are entered in the table, one for the token symbol and |
797 | one for the literal. Both are given the <type>, if any, from the |
798 | declaration. The ->user_token_number of the first is SALIAS and |
799 | the ->user_token_number of the second is set to the number, if |
800 | any, from the declaration. The two symbols are linked via |
801 | pointers in their ->alias fields. |
803 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
804 | only the literal string is retained it is the literal string that |
805 | is output to yytname |
806 `-------------------------------------------------------------------*/
809 parse_thong_decl (void)
812 struct bucket
*symbol
;
817 token
= lex (); /* fetch typename or first token */
818 if (token
== TYPENAME
)
820 typename
= xstrdup (token_buffer
);
821 value_components_used
= 1;
822 token
= lex (); /* fetch first token */
825 /* process first token */
827 if (token
!= IDENTIFIER
)
829 complain (_("unrecognized item %s, expected an identifier"),
834 symval
->class = token_sym
;
835 symval
->type_name
= typename
;
836 symval
->user_token_number
= SALIAS
;
839 token
= lex (); /* get number or literal string */
844 token
= lex (); /* okay, did number, now get literal */
849 /* process literal string token */
851 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
853 complain (_("expected string constant instead of %s"), token_buffer
);
857 symval
->class = token_sym
;
858 symval
->type_name
= typename
;
859 symval
->user_token_number
= usrtoknum
;
861 symval
->alias
= symbol
;
862 symbol
->alias
= symval
;
864 /* symbol and symval combined are only one symbol. */
869 /*----------------------------------------------------------------.
870 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
871 | any `%' declarations, and copy the contents of any `%{ ... %}' |
872 | groups to fattrs. |
873 `----------------------------------------------------------------*/
876 read_declarations (void)
883 c
= skip_white_space ();
887 tok
= parse_percent_token ();
894 case PERCENT_LEFT_CURLY
:
899 parse_token_decl (token_sym
, nterm_sym
);
903 parse_token_decl (nterm_sym
, token_sym
);
919 parse_expect_decl ();
926 parse_assoc_decl (left_assoc
);
930 parse_assoc_decl (right_assoc
);
934 parse_assoc_decl (non_assoc
);
937 case SEMANTIC_PARSER
:
938 if (semantic_parser
== 0)
953 complain (_("unrecognized: %s"), token_buffer
);
958 fatal (_("no input grammar"));
963 complain (_("unknown character: %s"), quote (buf
));
969 /*-------------------------------------------------------------------.
970 | Assuming that a `{' has just been seen, copy everything up to the |
971 | matching `}' into the actions file. STACK_OFFSET is the number of |
972 | values in the current rule so far, which says where to find `$0' |
973 | with respect to the top of the stack. |
974 `-------------------------------------------------------------------*/
977 copy_action (symbol_list
*rule
, int stack_offset
)
982 /* offset is always 0 if parser has already popped the stack pointer */
986 fprintf (faction
, "\ncase %d:\n", nrules
);
988 fprintf (faction
, "#line %d \"%s\"\n", lineno
, infile
);
1012 copy_string (finput
, faction
, c
);
1016 copy_comment (finput
, faction
);
1020 copy_dollar (finput
, faction
, rule
, stack_offset
);
1024 copy_at (finput
, faction
, stack_offset
);
1028 fatal (_("unmatched %s"), "`{'");
1037 /* above loop exits when c is '}' */
1046 fprintf (faction
, ";\n break;}");
1049 /*-------------------------------------------------------------------.
1050 | After `%guard' is seen in the input file, copy the actual guard |
1051 | into the guards file. If the guard is followed by an action, copy |
1052 | that into the actions file. STACK_OFFSET is the number of values |
1053 | in the current rule so far, which says where to find `$0' with |
1054 | respect to the top of the stack, for the simple parser in which |
1055 | the stack is not popped until after the guard is run. |
1056 `-------------------------------------------------------------------*/
1059 copy_guard (symbol_list
*rule
, int stack_offset
)
1065 /* offset is always 0 if parser has already popped the stack pointer */
1066 if (semantic_parser
)
1069 fprintf (fguard
, "\ncase %d:\n", nrules
);
1071 fprintf (fguard
, "#line %d \"%s\"\n", lineno
, infile
);
1077 while (brace_flag
? (count
> 0) : (c
!= ';'))
1098 complain (_("unmatched %s"), "`}'");
1099 c
= getc (finput
); /* skip it */
1105 copy_string (finput
, fguard
, c
);
1109 copy_comment (finput
, fguard
);
1113 copy_dollar (finput
, fguard
, rule
, stack_offset
);
1117 copy_at (finput
, fguard
, stack_offset
);
1121 fatal ("%s", _("unterminated %guard clause"));
1127 if (c
!= '}' || count
!= 0)
1131 c
= skip_white_space ();
1133 fprintf (fguard
, ";\n break;}");
1135 copy_action (rule
, stack_offset
);
1138 c
= getc (finput
); /* why not skip_white_space -wjh */
1140 copy_action (rule
, stack_offset
);
1148 record_rule_line (void)
1150 /* Record each rule's source line number in rline table. */
1152 if (nrules
>= rline_allocated
)
1154 rline_allocated
= nrules
* 2;
1155 rline
= XREALLOC (rline
, short, rline_allocated
);
1157 rline
[nrules
] = lineno
;
1161 /*-------------------------------------------------------------------.
1162 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1163 | with the user's names. |
1164 `-------------------------------------------------------------------*/
1171 sprintf (token_buffer
, "@%d", ++gensym_count
);
1172 sym
= getsym (token_buffer
);
1173 sym
->class = nterm_sym
;
1174 sym
->value
= nvars
++;
1179 /*------------------------------------------------------------------.
1180 | read in a %type declaration and record its information for |
1181 | get_type_name to access. This is unused. It is only called from |
1182 | the #if 0 part of readgram |
1183 `------------------------------------------------------------------*/
1196 complain (_("invalid %s declaration"), "%type");
1200 name
= xstrdup (token_buffer
);
1215 if (symval
->type_name
== NULL
)
1216 symval
->type_name
= name
;
1217 else if (strcmp (name
, symval
->type_name
) != 0)
1218 complain (_("type redeclaration for %s"), symval
->tag
);
1230 /*------------------------------------------------------------------.
1231 | Parse the input grammar into a one symbol_list structure. Each |
1232 | rule is represented by a sequence of symbols: the left hand side |
1233 | followed by the contents of the right hand side, followed by a |
1234 | null pointer instead of a symbol to terminate the rule. The next |
1235 | symbol is the lhs of the following rule. |
1237 | All guards and actions are copied out to the appropriate files, |
1238 | labelled by the rule number they apply to. |
1239 `------------------------------------------------------------------*/
1250 /* Points to first symbol_list of current rule. its symbol is the
1253 /* Points to the symbol_list preceding crule. */
1254 symbol_list
*crule1
;
1260 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1262 if (t
== IDENTIFIER
|| t
== BAR
)
1264 int action_flag
= 0;
1265 /* Number of symbols in rhs of this rule so far */
1267 int xactions
= 0; /* JF for error checking */
1268 bucket
*first_rhs
= 0;
1270 if (t
== IDENTIFIER
)
1283 complain (_("ill-formed rule: initial symbol not followed by colon"));
1288 if (nrules
== 0 && t
== BAR
)
1290 complain (_("grammar starts with vertical bar"));
1291 lhs
= symval
; /* BOGUS: use a random symval */
1293 /* start a new rule and record its lhs. */
1298 record_rule_line ();
1300 p
= XCALLOC (symbol_list
, 1);
1312 /* mark the rule's lhs as a nonterminal if not already so. */
1314 if (lhs
->class == unknown_sym
)
1316 lhs
->class = nterm_sym
;
1320 else if (lhs
->class == token_sym
)
1321 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1323 /* read the rhs of the rule. */
1331 crule
->ruleprec
= symval
;
1335 if (!(t
== IDENTIFIER
|| t
== LEFT_CURLY
))
1338 /* If next token is an identifier, see if a colon follows it.
1339 If one does, exit this rule now. */
1340 if (t
== IDENTIFIER
)
1352 if (!first_rhs
) /* JF */
1354 /* Not followed by colon =>
1355 process as part of this rule's rhs. */
1358 /* If we just passed an action, that action was in the middle
1359 of a rule, so make a dummy rule to reduce it to a
1365 /* Since the action was written out with this rule's
1366 number, we must give the new rule this number by
1367 inserting the new rule before it. */
1369 /* Make a dummy nonterminal, a gensym. */
1372 /* Make a new rule, whose body is empty,
1373 before the current one, so that the action
1374 just read can belong to it. */
1377 record_rule_line ();
1378 p
= XCALLOC (symbol_list
, 1);
1384 crule1
= XCALLOC (symbol_list
, 1);
1386 crule1
->next
= crule
;
1388 /* Insert the dummy generated by that rule into this
1391 p
= XCALLOC (symbol_list
, 1);
1399 if (t
== IDENTIFIER
)
1402 p
= XCALLOC (symbol_list
, 1);
1407 else /* handle an action. */
1409 copy_action (crule
, rulelength
);
1411 xactions
++; /* JF */
1414 } /* end of read rhs of rule */
1416 /* Put an empty link in the list to mark the end of this rule */
1417 p
= XCALLOC (symbol_list
, 1);
1423 complain (_("two @prec's in a row"));
1425 crule
->ruleprec
= symval
;
1430 if (!semantic_parser
)
1431 complain (_("%%guard present but %%semantic_parser not specified"));
1433 copy_guard (crule
, rulelength
);
1436 else if (t
== LEFT_CURLY
)
1438 /* This case never occurs -wjh */
1440 complain (_("two actions at end of one rule"));
1441 copy_action (crule
, rulelength
);
1443 xactions
++; /* -wjh */
1446 /* If $$ is being set in default way, report if any type
1449 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1451 if (lhs
->type_name
== 0
1452 || first_rhs
->type_name
== 0
1453 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1454 complain (_("type clash (`%s' `%s') on default action"),
1455 lhs
->type_name
? lhs
->type_name
: "",
1456 first_rhs
->type_name
? first_rhs
->type_name
: "");
1458 /* Warn if there is no default for $$ but we need one. */
1459 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1460 complain (_("empty rule for typed nonterminal, and no action"));
1465 /* these things can appear as alternatives to rules. */
1467 a) none of the documentation allows them
1468 b) most of them scan forward until finding a next %
1469 thus they may swallow lots of intervening rules
1471 else if (t
== TOKEN
)
1473 parse_token_decl (token_sym
, nterm_sym
);
1476 else if (t
== NTERM
)
1478 parse_token_decl (nterm_sym
, token_sym
);
1485 else if (t
== UNION
)
1487 parse_union_decl ();
1490 else if (t
== EXPECT
)
1492 parse_expect_decl ();
1495 else if (t
== START
)
1497 parse_start_decl ();
1504 complain (_("invalid input: %s"), token_buffer
);
1509 /* grammar has been read. Do some checking */
1511 if (nsyms
> MAXSHORT
)
1512 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1515 fatal (_("no rules in the input grammar"));
1517 /* JF put out same default YYSTYPE as YACC does */
1519 && !value_components_used
)
1521 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1522 but it seems better to be consistent.
1523 Most programs should declare their own type anyway. */
1524 fprintf (fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1526 fprintf (fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1529 /* Report any undefined symbols and consider them nonterminals. */
1531 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1532 if (bp
->class == unknown_sym
)
1535 ("symbol %s is used, but is not defined as a token and has no rules"),
1537 bp
->class = nterm_sym
;
1538 bp
->value
= nvars
++;
1541 ntokens
= nsyms
- nvars
;
1544 /*--------------------------------------------------------------.
1545 | For named tokens, but not literal ones, define the name. The |
1546 | value is the user token number. |
1547 `--------------------------------------------------------------*/
1550 output_token_defines (FILE *file
)
1556 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1558 symbol
= bp
->tag
; /* get symbol */
1560 if (bp
->value
>= ntokens
)
1562 if (bp
->user_token_number
== SALIAS
)
1564 if ('\'' == *symbol
)
1565 continue; /* skip literal character */
1567 continue; /* skip error token */
1568 if ('\"' == *symbol
)
1570 /* use literal string only if given a symbol with an alias */
1572 symbol
= bp
->alias
->tag
;
1577 /* Don't #define nonliteral tokens whose names contain periods. */
1579 while ((c
= *cp
++) && c
!= '.');
1583 fprintf (file
, "#define\t%s\t%d\n", symbol
,
1584 ((translations
&& !raw_flag
)
1585 ? bp
->user_token_number
: bp
->value
));
1586 if (semantic_parser
)
1587 fprintf (file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1594 /*------------------------------------------------------------------.
1595 | Assign symbol numbers, and write definition of token names into |
1596 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1598 `------------------------------------------------------------------*/
1606 int last_user_token_number
;
1607 static char DOLLAR
[] = "$";
1609 /* int lossage = 0; JF set but not used */
1611 tags
= XCALLOC (char *, nsyms
+ 1);
1613 user_toknums
= XCALLOC (short, nsyms
+ 1);
1614 user_toknums
[0] = 0;
1616 sprec
= XCALLOC (short, nsyms
);
1617 sassoc
= XCALLOC (short, nsyms
);
1619 max_user_token_number
= 256;
1620 last_user_token_number
= 256;
1622 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1624 if (bp
->class == nterm_sym
)
1626 bp
->value
+= ntokens
;
1630 /* this symbol and its alias are a single token defn.
1631 allocate a tokno, and assign to both check agreement of
1632 ->prec and ->assoc fields and make both the same */
1634 bp
->value
= bp
->alias
->value
= tokno
++;
1636 if (bp
->prec
!= bp
->alias
->prec
)
1638 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1639 && bp
->user_token_number
== SALIAS
)
1640 complain (_("conflicting precedences for %s and %s"),
1641 bp
->tag
, bp
->alias
->tag
);
1643 bp
->alias
->prec
= bp
->prec
;
1645 bp
->prec
= bp
->alias
->prec
;
1648 if (bp
->assoc
!= bp
->alias
->assoc
)
1650 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1651 && bp
->user_token_number
== SALIAS
)
1652 complain (_("conflicting assoc values for %s and %s"),
1653 bp
->tag
, bp
->alias
->tag
);
1655 bp
->alias
->assoc
= bp
->assoc
;
1657 bp
->assoc
= bp
->alias
->assoc
;
1660 if (bp
->user_token_number
== SALIAS
)
1661 continue; /* do not do processing below for SALIASs */
1664 else /* bp->class == token_sym */
1666 bp
->value
= tokno
++;
1669 if (bp
->class == token_sym
)
1671 if (translations
&& !(bp
->user_token_number
))
1672 bp
->user_token_number
= ++last_user_token_number
;
1673 if (bp
->user_token_number
> max_user_token_number
)
1674 max_user_token_number
= bp
->user_token_number
;
1677 tags
[bp
->value
] = bp
->tag
;
1678 user_toknums
[bp
->value
] = bp
->user_token_number
;
1679 sprec
[bp
->value
] = bp
->prec
;
1680 sassoc
[bp
->value
] = bp
->assoc
;
1688 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1690 /* initialize all entries for literal tokens to 2, the internal
1691 token number for $undefined., which represents all invalid
1693 for (j
= 0; j
<= max_user_token_number
; j
++)
1694 token_translations
[j
] = 2;
1696 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1698 if (bp
->value
>= ntokens
)
1699 continue; /* non-terminal */
1700 if (bp
->user_token_number
== SALIAS
)
1702 if (token_translations
[bp
->user_token_number
] != 2)
1703 complain (_("tokens %s and %s both assigned number %d"),
1704 tags
[token_translations
[bp
->user_token_number
]],
1705 bp
->tag
, bp
->user_token_number
);
1706 token_translations
[bp
->user_token_number
] = bp
->value
;
1710 error_token_number
= errtoken
->value
;
1712 if (!no_parser_flag
)
1713 output_token_defines (ftable
);
1715 if (startval
->class == unknown_sym
)
1716 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1717 else if (startval
->class == token_sym
)
1718 fatal (_("the start symbol %s is a token"), startval
->tag
);
1720 start_symbol
= startval
->value
;
1724 output_token_defines (fdefines
);
1728 if (spec_name_prefix
)
1729 fprintf (fdefines
, "\nextern YYSTYPE %slval;\n",
1732 fprintf (fdefines
, "\nextern YYSTYPE yylval;\n");
1735 if (semantic_parser
)
1736 for (i
= ntokens
; i
< nsyms
; i
++)
1738 /* don't make these for dummy nonterminals made by gensym. */
1739 if (*tags
[i
] != '@')
1740 fprintf (fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1743 /* `fdefines' is now a temporary file, so we need to copy its
1744 contents in `done', so we can't close it here. */
1752 /*---------------------------------------------------------------.
1753 | Convert the rules into the representation using RRHS, RLHS and |
1755 `---------------------------------------------------------------*/
1766 ritem
= XCALLOC (short, nitems
+ 1);
1767 rlhs
= XCALLOC (short, nrules
) - 1;
1768 rrhs
= XCALLOC (short, nrules
) - 1;
1769 rprec
= XCALLOC (short, nrules
) - 1;
1770 rprecsym
= XCALLOC (short, nrules
) - 1;
1771 rassoc
= XCALLOC (short, nrules
) - 1;
1779 rlhs
[ruleno
] = p
->sym
->value
;
1780 rrhs
[ruleno
] = itemno
;
1781 ruleprec
= p
->ruleprec
;
1786 ritem
[itemno
++] = p
->sym
->value
;
1787 /* A rule gets by default the precedence and associativity
1788 of the last token in it. */
1789 if (p
->sym
->class == token_sym
)
1791 rprec
[ruleno
] = p
->sym
->prec
;
1792 rassoc
[ruleno
] = p
->sym
->assoc
;
1798 /* If this rule has a %prec,
1799 the specified symbol's precedence replaces the default. */
1802 rprec
[ruleno
] = ruleprec
->prec
;
1803 rassoc
[ruleno
] = ruleprec
->assoc
;
1804 rprecsym
[ruleno
] = ruleprec
->value
;
1807 ritem
[itemno
++] = -ruleno
;
1817 /*-------------------------------------------------------------------.
1818 | Read in the grammar specification and record it in the format |
1819 | described in gram.h. All guards are copied into the FGUARD file |
1820 | and all actions into FACTION, in each case forming the body of a C |
1821 | function (YYGUARD or YYACTION) which contains a switch statement |
1822 | to decide which guard or action to execute. |
1823 `-------------------------------------------------------------------*/
1829 startval
= NULL
; /* start symbol not specified yet. */
1832 /* initially assume token number translation not needed. */
1835 /* Nowadays translations is always set to 1, since we give `error' a
1836 user-token-number to satisfy the Posix demand for YYERRCODE==256.
1844 rline_allocated
= 10;
1845 rline
= XCALLOC (short, rline_allocated
);
1852 semantic_parser
= 0;
1860 /* Initialize the symbol table. */
1862 /* Construct the error token */
1863 errtoken
= getsym ("error");
1864 errtoken
->class = token_sym
;
1865 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1866 /* Construct a token that represents all undefined literal tokens.
1867 It is always token number 2. */
1868 undeftoken
= getsym ("$undefined.");
1869 undeftoken
->class = token_sym
;
1870 undeftoken
->user_token_number
= 2;
1872 /* Read the declaration section. Copy %{ ... %} groups to FTABLE
1873 and FDEFINES file. Also notice any %token, %left, etc. found
1875 putc ('\n', ftable
);
1877 /* %s, made from %s\n\
1878 by GNU bison %s. */\n\
1879 \n", no_parser_flag
? "Bison-generated parse tables" : "A Bison parser", infile
, VERSION
);
1881 fputs ("#define YYBISON 1 /* Identify Bison output. */\n\n", ftable
);
1882 read_declarations ();
1883 /* Start writing the guard and action files, if they are needed. */
1885 /* Read in the grammar, build grammar in list form. Write out
1886 guards and actions. */
1888 /* Now we know whether we need the line-number stack. If we do,
1889 write its type into the .tab.h file. */
1891 reader_output_yylsp (fdefines
);
1892 /* Write closing delimiters for actions and guards. */
1895 fputs ("#define YYLSP_NEEDED\n\n", ftable
);
1896 /* Assign the symbols their symbol numbers. Write #defines for the
1897 token symbols into FDEFINES if requested. */
1899 /* Convert the grammar into the format described in gram.h. */
1901 /* Free the symbol table data structure since symbols are now all
1902 referred to by symbol number. */
1908 reader_output_yylsp (FILE *f
)
1914 typedef struct yyltype\n\
1925 # define YYLTYPE yyltype\n\