]>
git.saurik.com Git - bison.git/blob - src/reader.c
1 /* Input parser for bison
2 Copyright (C) 1984, 1986, 1989, 1992, 1998, 2000
3 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
7 Bison is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 Bison is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
33 #include "conflicts.h"
36 /* Number of slots allocated (but not necessarily used yet) in `rline' */
37 static int rline_allocated
;
39 typedef struct symbol_list
41 struct symbol_list
*next
;
50 static symbol_list
*grammar
;
51 static int start_flag
;
52 static bucket
*startval
;
54 /* Nonzero if components of semantic values are used, implying
55 they must be unions. */
56 static int value_components_used
;
58 /* Nonzero if %union has been seen. */
61 /* Incremented for each %left, %right or %nonassoc seen */
64 /* Incremented for each generated symbol */
65 static int gensym_count
;
67 static bucket
*errtoken
;
68 static bucket
*undeftoken
;
71 /*===================\
73 \===================*/
76 skip_to_char (int target
)
80 complain (_(" Skipping to next \\n"));
82 complain (_(" Skipping to next %c"), target
);
85 c
= skip_white_space ();
86 while (c
!= target
&& c
!= EOF
);
92 /*---------------------------------------------------------.
93 | Read a signed integer from STREAM and return its value. |
94 `---------------------------------------------------------*/
97 read_signed_integer (FILE *stream
)
99 int c
= getc (stream
);
111 n
= 10 * n
+ (c
- '0');
120 /*--------------------------------------------------------------.
121 | 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
)
466 /* The symbol being defined. */
467 struct bucket
*symbol
= NULL
;
469 /* After `%token' and `%nterm', any number of symbols maybe be
473 int tmp_char
= ungetc (skip_white_space (), finput
);
475 /* `%' (for instance from `%token', or from `%%' etc.) is the
476 only valid means to end this declaration. */
480 fatal (_("Premature EOF after %s"), token_buffer
);
488 if (token
== TYPENAME
)
490 typename
= xstrdup (token_buffer
);
491 value_components_used
= 1;
494 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"' && symbol
)
497 warn (_("symbol `%s' used more than once as a literal string"),
499 else if (symbol
->alias
)
500 warn (_("symbol `%s' given more than one literal string"),
504 symval
->class = token_sym
;
505 symval
->type_name
= typename
;
506 symval
->user_token_number
= symbol
->user_token_number
;
507 symbol
->user_token_number
= SALIAS
;
508 symval
->alias
= symbol
;
509 symbol
->alias
= symval
;
510 /* symbol and symval combined are only one symbol */
516 else if (token
== IDENTIFIER
)
518 int oldclass
= symval
->class;
521 if (symbol
->class == what_is_not
)
522 complain (_("symbol %s redefined"), symbol
->tag
);
523 symbol
->class = what_is
;
524 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
525 symbol
->value
= nvars
++;
529 if (symbol
->type_name
== NULL
)
530 symbol
->type_name
= typename
;
531 else if (strcmp (typename
, symbol
->type_name
) != 0)
532 complain (_("type redeclaration for %s"), symbol
->tag
);
535 else if (symbol
&& token
== NUMBER
)
537 symbol
->user_token_number
= numval
;
542 complain (_("`%s' is invalid in %s"),
543 token_buffer
, (what_is
== token_sym
) ? "%token" : "%nterm");
551 /*------------------------------.
552 | Parse what comes after %start |
553 `------------------------------*/
556 parse_start_decl (void)
559 complain (_("multiple %s declarations"), "%start");
560 if (lex () != IDENTIFIER
)
561 complain (_("invalid %s declaration"), "%start");
569 /*-----------------------------------------------------------.
570 | read in a %type declaration and record its information for |
571 | get_type_name to access |
572 `-----------------------------------------------------------*/
575 parse_type_decl (void)
579 if (lex () != TYPENAME
)
581 complain ("%s", _("%type declaration has no <typename>"));
586 name
= xstrdup (token_buffer
);
591 int tmp_char
= ungetc (skip_white_space (), finput
);
596 fatal (_("Premature EOF after %s"), token_buffer
);
608 if (symval
->type_name
== NULL
)
609 symval
->type_name
= name
;
610 else if (strcmp (name
, symval
->type_name
) != 0)
611 complain (_("type redeclaration for %s"), symval
->tag
);
616 complain (_("invalid %%type declaration due to item: %s"),
625 /*----------------------------------------------------------------.
626 | Read in a %left, %right or %nonassoc declaration and record its |
628 `----------------------------------------------------------------*/
631 parse_assoc_decl (associativity assoc
)
636 lastprec
++; /* Assign a new precedence level, never 0. */
641 int tmp_char
= ungetc (skip_white_space (), finput
);
646 fatal (_("Premature EOF after %s"), token_buffer
);
653 name
= xstrdup (token_buffer
);
660 if (symval
->prec
!= 0)
661 complain (_("redefining precedence of %s"), symval
->tag
);
662 symval
->prec
= lastprec
;
663 symval
->assoc
= assoc
;
664 if (symval
->class == nterm_sym
)
665 complain (_("symbol %s redefined"), symval
->tag
);
666 symval
->class = token_sym
;
668 { /* record the type, if one is specified */
669 if (symval
->type_name
== NULL
)
670 symval
->type_name
= name
;
671 else if (strcmp (name
, symval
->type_name
) != 0)
672 complain (_("type redeclaration for %s"), symval
->tag
);
677 if (prev
== IDENTIFIER
)
679 symval
->user_token_number
= numval
;
685 ("invalid text (%s) - number should be after identifier"),
695 complain (_("unexpected item: %s"), token_buffer
);
706 /*-------------------------------------------------------------------.
707 | Copy the union declaration into fattrs (and fdefines), where it is |
708 | made into the definition of YYSTYPE, the type of elements of the |
709 | parser value stack. |
710 `-------------------------------------------------------------------*/
713 parse_union_decl (void)
719 complain (_("multiple %s declarations"), "%union");
724 fprintf (fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
726 fprintf (fattrs
, "\n");
728 fprintf (fattrs
, "typedef union");
730 fprintf (fdefines
, "typedef union");
747 copy_comment2 (finput
, fattrs
, fdefines
);
756 complain (_("unmatched %s"), "`}'");
760 fprintf (fattrs
, " YYSTYPE;\n");
762 fprintf (fdefines
, " YYSTYPE;\n");
763 /* JF don't choke on trailing semi */
764 c
= skip_white_space ();
776 /*-------------------------------------------------------.
777 | Parse the declaration %expect N which says to expect N |
778 | shift-reduce conflicts. |
779 `-------------------------------------------------------*/
782 parse_expect_decl (void)
784 int c
= skip_white_space ();
788 complain (_("argument of %%expect is not an integer"));
790 expected_conflicts
= read_signed_integer (finput
);
794 /*-------------------------------------------------------------------.
795 | Parse what comes after %thong. the full syntax is |
797 | %thong <type> token number literal |
799 | the <type> or number may be omitted. The number specifies the |
800 | user_token_number. |
802 | Two symbols are entered in the table, one for the token symbol and |
803 | one for the literal. Both are given the <type>, if any, from the |
804 | declaration. The ->user_token_number of the first is SALIAS and |
805 | the ->user_token_number of the second is set to the number, if |
806 | any, from the declaration. The two symbols are linked via |
807 | pointers in their ->alias fields. |
809 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
810 | only the literal string is retained it is the literal string that |
811 | is output to yytname |
812 `-------------------------------------------------------------------*/
815 parse_thong_decl (void)
818 struct bucket
*symbol
;
823 token
= lex (); /* fetch typename or first token */
824 if (token
== TYPENAME
)
826 typename
= xstrdup (token_buffer
);
827 value_components_used
= 1;
828 token
= lex (); /* fetch first token */
831 /* process first token */
833 if (token
!= IDENTIFIER
)
835 complain (_("unrecognized item %s, expected an identifier"),
840 symval
->class = token_sym
;
841 symval
->type_name
= typename
;
842 symval
->user_token_number
= SALIAS
;
845 token
= lex (); /* get number or literal string */
850 token
= lex (); /* okay, did number, now get literal */
855 /* process literal string token */
857 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
859 complain (_("expected string constant instead of %s"), token_buffer
);
863 symval
->class = token_sym
;
864 symval
->type_name
= typename
;
865 symval
->user_token_number
= usrtoknum
;
867 symval
->alias
= symbol
;
868 symbol
->alias
= symval
;
870 /* symbol and symval combined are only one symbol. */
875 /*----------------------------------------------------------------.
876 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
877 | any `%' declarations, and copy the contents of any `%{ ... %}' |
878 | groups to fattrs. |
879 `----------------------------------------------------------------*/
882 read_declarations (void)
889 c
= skip_white_space ();
893 tok
= parse_percent_token ();
900 case PERCENT_LEFT_CURLY
:
905 parse_token_decl (token_sym
, nterm_sym
);
909 parse_token_decl (nterm_sym
, token_sym
);
925 parse_expect_decl ();
932 parse_assoc_decl (left_assoc
);
936 parse_assoc_decl (right_assoc
);
940 parse_assoc_decl (non_assoc
);
943 case SEMANTIC_PARSER
:
944 if (semantic_parser
== 0)
959 complain (_("unrecognized: %s"), token_buffer
);
964 fatal (_("no input grammar"));
969 complain (_("unknown character: %s"), quote (buf
));
975 /*-------------------------------------------------------------------.
976 | Assuming that a `{' has just been seen, copy everything up to the |
977 | matching `}' into the actions file. STACK_OFFSET is the number of |
978 | values in the current rule so far, which says where to find `$0' |
979 | with respect to the top of the stack. |
980 `-------------------------------------------------------------------*/
983 copy_action (symbol_list
*rule
, int stack_offset
)
988 /* offset is always 0 if parser has already popped the stack pointer */
992 fprintf (faction
, "\ncase %d:\n", nrules
);
994 fprintf (faction
, "#line %d \"%s\"\n", lineno
, infile
);
1018 copy_string (finput
, faction
, c
);
1022 copy_comment (finput
, faction
);
1026 copy_dollar (finput
, faction
, rule
, stack_offset
);
1030 copy_at (finput
, faction
, stack_offset
);
1034 fatal (_("unmatched %s"), "`{'");
1043 /* above loop exits when c is '}' */
1052 fprintf (faction
, ";\n break;}");
1055 /*-------------------------------------------------------------------.
1056 | After `%guard' is seen in the input file, copy the actual guard |
1057 | into the guards file. If the guard is followed by an action, copy |
1058 | that into the actions file. STACK_OFFSET is the number of values |
1059 | in the current rule so far, which says where to find `$0' with |
1060 | respect to the top of the stack, for the simple parser in which |
1061 | the stack is not popped until after the guard is run. |
1062 `-------------------------------------------------------------------*/
1065 copy_guard (symbol_list
*rule
, int stack_offset
)
1071 /* offset is always 0 if parser has already popped the stack pointer */
1072 if (semantic_parser
)
1075 fprintf (fguard
, "\ncase %d:\n", nrules
);
1077 fprintf (fguard
, "#line %d \"%s\"\n", lineno
, infile
);
1083 while (brace_flag
? (count
> 0) : (c
!= ';'))
1104 complain (_("unmatched %s"), "`}'");
1105 c
= getc (finput
); /* skip it */
1111 copy_string (finput
, fguard
, c
);
1115 copy_comment (finput
, fguard
);
1119 copy_dollar (finput
, fguard
, rule
, stack_offset
);
1123 copy_at (finput
, fguard
, stack_offset
);
1127 fatal ("%s", _("unterminated %guard clause"));
1133 if (c
!= '}' || count
!= 0)
1137 c
= skip_white_space ();
1139 fprintf (fguard
, ";\n break;}");
1141 copy_action (rule
, stack_offset
);
1144 c
= getc (finput
); /* why not skip_white_space -wjh */
1146 copy_action (rule
, stack_offset
);
1154 record_rule_line (void)
1156 /* Record each rule's source line number in rline table. */
1158 if (nrules
>= rline_allocated
)
1160 rline_allocated
= nrules
* 2;
1161 rline
= XREALLOC (rline
, short, rline_allocated
);
1163 rline
[nrules
] = lineno
;
1167 /*-------------------------------------------------------------------.
1168 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1169 | with the user's names. |
1170 `-------------------------------------------------------------------*/
1177 sprintf (token_buffer
, "@%d", ++gensym_count
);
1178 sym
= getsym (token_buffer
);
1179 sym
->class = nterm_sym
;
1180 sym
->value
= nvars
++;
1185 /*------------------------------------------------------------------.
1186 | read in a %type declaration and record its information for |
1187 | get_type_name to access. This is unused. It is only called from |
1188 | the #if 0 part of readgram |
1189 `------------------------------------------------------------------*/
1202 complain (_("invalid %s declaration"), "%type");
1206 name
= xstrdup (token_buffer
);
1221 if (symval
->type_name
== NULL
)
1222 symval
->type_name
= name
;
1223 else if (strcmp (name
, symval
->type_name
) != 0)
1224 complain (_("type redeclaration for %s"), symval
->tag
);
1236 /*------------------------------------------------------------------.
1237 | Parse the input grammar into a one symbol_list structure. Each |
1238 | rule is represented by a sequence of symbols: the left hand side |
1239 | followed by the contents of the right hand side, followed by a |
1240 | null pointer instead of a symbol to terminate the rule. The next |
1241 | symbol is the lhs of the following rule. |
1243 | All guards and actions are copied out to the appropriate files, |
1244 | labelled by the rule number they apply to. |
1245 `------------------------------------------------------------------*/
1256 /* Points to first symbol_list of current rule. its symbol is the
1259 /* Points to the symbol_list preceding crule. */
1260 symbol_list
*crule1
;
1266 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1268 if (t
== IDENTIFIER
|| t
== BAR
)
1270 int action_flag
= 0;
1271 /* Number of symbols in rhs of this rule so far */
1273 int xactions
= 0; /* JF for error checking */
1274 bucket
*first_rhs
= 0;
1276 if (t
== IDENTIFIER
)
1289 complain (_("ill-formed rule: initial symbol not followed by colon"));
1294 if (nrules
== 0 && t
== BAR
)
1296 complain (_("grammar starts with vertical bar"));
1297 lhs
= symval
; /* BOGUS: use a random symval */
1299 /* start a new rule and record its lhs. */
1304 record_rule_line ();
1306 p
= XCALLOC (symbol_list
, 1);
1318 /* mark the rule's lhs as a nonterminal if not already so. */
1320 if (lhs
->class == unknown_sym
)
1322 lhs
->class = nterm_sym
;
1326 else if (lhs
->class == token_sym
)
1327 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1329 /* read the rhs of the rule. */
1337 crule
->ruleprec
= symval
;
1341 if (!(t
== IDENTIFIER
|| t
== LEFT_CURLY
))
1344 /* If next token is an identifier, see if a colon follows it.
1345 If one does, exit this rule now. */
1346 if (t
== IDENTIFIER
)
1358 if (!first_rhs
) /* JF */
1360 /* Not followed by colon =>
1361 process as part of this rule's rhs. */
1364 /* If we just passed an action, that action was in the middle
1365 of a rule, so make a dummy rule to reduce it to a
1371 /* Since the action was written out with this rule's
1372 number, we must give the new rule this number by
1373 inserting the new rule before it. */
1375 /* Make a dummy nonterminal, a gensym. */
1378 /* Make a new rule, whose body is empty,
1379 before the current one, so that the action
1380 just read can belong to it. */
1383 record_rule_line ();
1384 p
= XCALLOC (symbol_list
, 1);
1390 crule1
= XCALLOC (symbol_list
, 1);
1392 crule1
->next
= crule
;
1394 /* Insert the dummy generated by that rule into this
1397 p
= XCALLOC (symbol_list
, 1);
1405 if (t
== IDENTIFIER
)
1408 p
= XCALLOC (symbol_list
, 1);
1413 else /* handle an action. */
1415 copy_action (crule
, rulelength
);
1417 xactions
++; /* JF */
1420 } /* end of read rhs of rule */
1422 /* Put an empty link in the list to mark the end of this rule */
1423 p
= XCALLOC (symbol_list
, 1);
1429 complain (_("two @prec's in a row"));
1431 crule
->ruleprec
= symval
;
1436 if (!semantic_parser
)
1437 complain (_("%%guard present but %%semantic_parser not specified"));
1439 copy_guard (crule
, rulelength
);
1442 else if (t
== LEFT_CURLY
)
1444 /* This case never occurs -wjh */
1446 complain (_("two actions at end of one rule"));
1447 copy_action (crule
, rulelength
);
1449 xactions
++; /* -wjh */
1452 /* If $$ is being set in default way, report if any type
1455 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1457 if (lhs
->type_name
== 0
1458 || first_rhs
->type_name
== 0
1459 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1460 complain (_("type clash (`%s' `%s') on default action"),
1461 lhs
->type_name
? lhs
->type_name
: "",
1462 first_rhs
->type_name
? first_rhs
->type_name
: "");
1464 /* Warn if there is no default for $$ but we need one. */
1465 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1466 complain (_("empty rule for typed nonterminal, and no action"));
1471 /* these things can appear as alternatives to rules. */
1473 a) none of the documentation allows them
1474 b) most of them scan forward until finding a next %
1475 thus they may swallow lots of intervening rules
1477 else if (t
== TOKEN
)
1479 parse_token_decl (token_sym
, nterm_sym
);
1482 else if (t
== NTERM
)
1484 parse_token_decl (nterm_sym
, token_sym
);
1491 else if (t
== UNION
)
1493 parse_union_decl ();
1496 else if (t
== EXPECT
)
1498 parse_expect_decl ();
1501 else if (t
== START
)
1503 parse_start_decl ();
1510 complain (_("invalid input: %s"), token_buffer
);
1515 /* grammar has been read. Do some checking */
1517 if (nsyms
> MAXSHORT
)
1518 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1521 fatal (_("no rules in the input grammar"));
1523 /* JF put out same default YYSTYPE as YACC does */
1525 && !value_components_used
)
1527 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1528 but it seems better to be consistent.
1529 Most programs should declare their own type anyway. */
1530 fprintf (fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1532 fprintf (fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1535 /* Report any undefined symbols and consider them nonterminals. */
1537 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1538 if (bp
->class == unknown_sym
)
1541 ("symbol %s is used, but is not defined as a token and has no rules"),
1543 bp
->class = nterm_sym
;
1544 bp
->value
= nvars
++;
1547 ntokens
= nsyms
- nvars
;
1550 /*--------------------------------------------------------------.
1551 | For named tokens, but not literal ones, define the name. The |
1552 | value is the user token number. |
1553 `--------------------------------------------------------------*/
1556 output_token_defines (FILE *file
)
1562 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1564 symbol
= bp
->tag
; /* get symbol */
1566 if (bp
->value
>= ntokens
)
1568 if (bp
->user_token_number
== SALIAS
)
1570 if ('\'' == *symbol
)
1571 continue; /* skip literal character */
1573 continue; /* skip error token */
1574 if ('\"' == *symbol
)
1576 /* use literal string only if given a symbol with an alias */
1578 symbol
= bp
->alias
->tag
;
1583 /* Don't #define nonliteral tokens whose names contain periods. */
1585 while ((c
= *cp
++) && c
!= '.');
1589 fprintf (file
, "#define\t%s\t%d\n", symbol
,
1590 ((translations
&& !raw_flag
)
1591 ? bp
->user_token_number
: bp
->value
));
1592 if (semantic_parser
)
1593 fprintf (file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1600 /*------------------------------------------------------------------.
1601 | Assign symbol numbers, and write definition of token names into |
1602 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1604 `------------------------------------------------------------------*/
1612 int last_user_token_number
;
1613 static char DOLLAR
[] = "$";
1615 /* int lossage = 0; JF set but not used */
1617 tags
= XCALLOC (char *, nsyms
+ 1);
1619 user_toknums
= XCALLOC (short, nsyms
+ 1);
1620 user_toknums
[0] = 0;
1622 sprec
= XCALLOC (short, nsyms
);
1623 sassoc
= XCALLOC (short, nsyms
);
1625 max_user_token_number
= 256;
1626 last_user_token_number
= 256;
1628 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1630 if (bp
->class == nterm_sym
)
1632 bp
->value
+= ntokens
;
1636 /* this symbol and its alias are a single token defn.
1637 allocate a tokno, and assign to both check agreement of
1638 ->prec and ->assoc fields and make both the same */
1640 bp
->value
= bp
->alias
->value
= tokno
++;
1642 if (bp
->prec
!= bp
->alias
->prec
)
1644 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1645 && bp
->user_token_number
== SALIAS
)
1646 complain (_("conflicting precedences for %s and %s"),
1647 bp
->tag
, bp
->alias
->tag
);
1649 bp
->alias
->prec
= bp
->prec
;
1651 bp
->prec
= bp
->alias
->prec
;
1654 if (bp
->assoc
!= bp
->alias
->assoc
)
1656 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1657 && bp
->user_token_number
== SALIAS
)
1658 complain (_("conflicting assoc values for %s and %s"),
1659 bp
->tag
, bp
->alias
->tag
);
1661 bp
->alias
->assoc
= bp
->assoc
;
1663 bp
->assoc
= bp
->alias
->assoc
;
1666 if (bp
->user_token_number
== SALIAS
)
1667 continue; /* do not do processing below for SALIASs */
1670 else /* bp->class == token_sym */
1672 bp
->value
= tokno
++;
1675 if (bp
->class == token_sym
)
1677 if (translations
&& !(bp
->user_token_number
))
1678 bp
->user_token_number
= ++last_user_token_number
;
1679 if (bp
->user_token_number
> max_user_token_number
)
1680 max_user_token_number
= bp
->user_token_number
;
1683 tags
[bp
->value
] = bp
->tag
;
1684 user_toknums
[bp
->value
] = bp
->user_token_number
;
1685 sprec
[bp
->value
] = bp
->prec
;
1686 sassoc
[bp
->value
] = bp
->assoc
;
1694 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1696 /* initialize all entries for literal tokens to 2, the internal
1697 token number for $undefined., which represents all invalid
1699 for (j
= 0; j
<= max_user_token_number
; j
++)
1700 token_translations
[j
] = 2;
1702 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1704 if (bp
->value
>= ntokens
)
1705 continue; /* non-terminal */
1706 if (bp
->user_token_number
== SALIAS
)
1708 if (token_translations
[bp
->user_token_number
] != 2)
1709 complain (_("tokens %s and %s both assigned number %d"),
1710 tags
[token_translations
[bp
->user_token_number
]],
1711 bp
->tag
, bp
->user_token_number
);
1712 token_translations
[bp
->user_token_number
] = bp
->value
;
1716 error_token_number
= errtoken
->value
;
1718 if (!no_parser_flag
)
1719 output_token_defines (ftable
);
1721 if (startval
->class == unknown_sym
)
1722 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1723 else if (startval
->class == token_sym
)
1724 fatal (_("the start symbol %s is a token"), startval
->tag
);
1726 start_symbol
= startval
->value
;
1730 output_token_defines (fdefines
);
1734 if (spec_name_prefix
)
1735 fprintf (fdefines
, "\nextern YYSTYPE %slval;\n",
1738 fprintf (fdefines
, "\nextern YYSTYPE yylval;\n");
1741 if (semantic_parser
)
1742 for (i
= ntokens
; i
< nsyms
; i
++)
1744 /* don't make these for dummy nonterminals made by gensym. */
1745 if (*tags
[i
] != '@')
1746 fprintf (fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1749 /* `fdefines' is now a temporary file, so we need to copy its
1750 contents in `done', so we can't close it here. */
1758 /*---------------------------------------------------------------.
1759 | Convert the rules into the representation using RRHS, RLHS and |
1761 `---------------------------------------------------------------*/
1772 ritem
= XCALLOC (short, nitems
+ 1);
1773 rlhs
= XCALLOC (short, nrules
) - 1;
1774 rrhs
= XCALLOC (short, nrules
) - 1;
1775 rprec
= XCALLOC (short, nrules
) - 1;
1776 rprecsym
= XCALLOC (short, nrules
) - 1;
1777 rassoc
= XCALLOC (short, nrules
) - 1;
1785 rlhs
[ruleno
] = p
->sym
->value
;
1786 rrhs
[ruleno
] = itemno
;
1787 ruleprec
= p
->ruleprec
;
1792 ritem
[itemno
++] = p
->sym
->value
;
1793 /* A rule gets by default the precedence and associativity
1794 of the last token in it. */
1795 if (p
->sym
->class == token_sym
)
1797 rprec
[ruleno
] = p
->sym
->prec
;
1798 rassoc
[ruleno
] = p
->sym
->assoc
;
1804 /* If this rule has a %prec,
1805 the specified symbol's precedence replaces the default. */
1808 rprec
[ruleno
] = ruleprec
->prec
;
1809 rassoc
[ruleno
] = ruleprec
->assoc
;
1810 rprecsym
[ruleno
] = ruleprec
->value
;
1813 ritem
[itemno
++] = -ruleno
;
1823 /*-------------------------------------------------------------------.
1824 | Read in the grammar specification and record it in the format |
1825 | described in gram.h. All guards are copied into the FGUARD file |
1826 | and all actions into FACTION, in each case forming the body of a C |
1827 | function (YYGUARD or YYACTION) which contains a switch statement |
1828 | to decide which guard or action to execute. |
1829 `-------------------------------------------------------------------*/
1835 startval
= NULL
; /* start symbol not specified yet. */
1838 /* initially assume token number translation not needed. */
1841 /* Nowadays translations is always set to 1, since we give `error' a
1842 user-token-number to satisfy the Posix demand for YYERRCODE==256.
1850 rline_allocated
= 10;
1851 rline
= XCALLOC (short, rline_allocated
);
1858 semantic_parser
= 0;
1866 /* Initialize the symbol table. */
1868 /* Construct the error token */
1869 errtoken
= getsym ("error");
1870 errtoken
->class = token_sym
;
1871 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1872 /* Construct a token that represents all undefined literal tokens.
1873 It is always token number 2. */
1874 undeftoken
= getsym ("$undefined.");
1875 undeftoken
->class = token_sym
;
1876 undeftoken
->user_token_number
= 2;
1878 /* Read the declaration section. Copy %{ ... %} groups to FTABLE
1879 and FDEFINES file. Also notice any %token, %left, etc. found
1881 putc ('\n', ftable
);
1883 /* %s, made from %s\n\
1884 by GNU bison %s. */\n\
1885 \n", no_parser_flag
? "Bison-generated parse tables" : "A Bison parser", infile
, VERSION
);
1887 fputs ("#define YYBISON 1 /* Identify Bison output. */\n\n", ftable
);
1888 read_declarations ();
1889 /* Start writing the guard and action files, if they are needed. */
1891 /* Read in the grammar, build grammar in list form. Write out
1892 guards and actions. */
1894 /* Now we know whether we need the line-number stack. If we do,
1895 write its type into the .tab.h file. */
1897 reader_output_yylsp (fdefines
);
1898 /* Write closing delimiters for actions and guards. */
1901 fputs ("#define YYLSP_NEEDED\n\n", ftable
);
1902 /* Assign the symbols their symbol numbers. Write #defines for the
1903 token symbols into FDEFINES if requested. */
1905 /* Convert the grammar into the format described in gram.h. */
1907 /* Free the symbol table data structure since symbols are now all
1908 referred to by symbol number. */
1914 reader_output_yylsp (FILE *f
)
1920 typedef struct yyltype\n\
1931 # define YYLTYPE yyltype\n\