]>
git.saurik.com Git - bison.git/blob - src/reader.c
1 /* Input parser for bison
2 Copyright (C) 1984, 1986, 1989, 1992 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
6 Bison is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 Bison is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* read in the grammar specification and record it in the format described in gram.h.
22 All guards are copied into the fguard file and all actions into faction,
23 in each case forming the body of a C function (yyguard or yyaction)
24 which contains a switch statement to decide which guard or action to execute.
26 The entry point is reader(). */
38 #define LTYPESTR "\n#ifndef YYLTYPE\ntypedef\n struct yyltype\n\
39 {\n int timestamp;\n int first_line;\n int first_column;\
40 \n int last_line;\n int last_column;\n char *text;\n }\n\
41 yyltype;\n\n#define YYLTYPE yyltype\n#endif\n\n"
43 /* Number of slots allocated (but not necessarily used yet) in `rline' */
46 extern char *program_name
;
47 extern int definesflag
;
48 extern int nolinesflag
;
49 extern int noparserflag
;
50 extern int rawtoknumflag
;
51 extern bucket
*symval
;
53 extern int expected_conflicts
;
54 extern char *token_buffer
;
56 extern void init_lex();
57 extern void tabinit();
58 extern void output_headers();
59 extern void output_trailers();
60 extern void free_symtab();
61 extern void open_extra_files();
62 extern char *int_to_string();
69 extern void warnsss();
73 extern int skip_white_space();
74 extern int parse_percent_token();
77 void reader_output_yylsp();
78 void read_declarations();
79 void copy_definition();
80 void parse_token_decl();
81 void parse_start_decl();
82 void parse_type_decl();
83 void parse_assoc_decl();
84 void parse_union_decl();
85 void parse_expect_decl();
86 void parse_thong_decl();
89 void record_rule_line();
91 void output_token_defines();
93 int read_signed_integer();
94 static int get_type();
99 struct symbol_list
*next
;
108 symbol_list
*grammar
;
114 /* Nonzero if components of semantic values are used, implying
115 they must be unions. */
116 static int value_components_used
;
118 static int typed
; /* nonzero if %union has been seen. */
120 static int lastprec
; /* incremented for each %left, %right or %nonassoc seen */
122 static int gensym_count
; /* incremented for each generated symbol */
124 static bucket
*errtoken
;
125 static bucket
*undeftoken
;
127 /* Nonzero if any action or guard uses the @n construct. */
128 static int yylsp_needed
;
130 extern char *version_string
;
139 warn(_(" Skipping to next \\n"));
141 warni(_(" Skipping to next %c"), target
);
144 c
= skip_white_space();
145 while (c
!= target
&& c
!= EOF
);
155 startval
= NULL
; /* start symbol not specified yet. */
158 translations
= 0; /* initially assume token number translation not needed. */
160 /* Nowadays translations is always set to 1,
161 since we give `error' a user-token-number
162 to satisfy the Posix demand for YYERRCODE==256. */
169 rline_allocated
= 10;
170 rline
= NEW2(rline_allocated
, short);
186 /* initialize the symbol table. */
188 /* construct the error token */
189 errtoken
= getsym("error");
190 errtoken
->class = STOKEN
;
191 errtoken
->user_token_number
= 256; /* Value specified by posix. */
192 /* construct a token that represents all undefined literal tokens. */
193 /* it is always token number 2. */
194 undeftoken
= getsym("$undefined.");
195 undeftoken
->class = STOKEN
;
196 undeftoken
->user_token_number
= 2;
197 /* Read the declaration section. Copy %{ ... %} groups to ftable and fdefines file.
198 Also notice any %token, %left, etc. found there. */
200 fprintf(ftable
, "\n/* Bison-generated parse tables, made from %s\n",
203 fprintf(ftable
, "\n/* A Bison parser, made from %s\n", infile
);
204 fprintf(ftable
, " by %s */\n\n", version_string
);
205 fprintf(ftable
, "#define YYBISON 1 /* Identify Bison output. */\n\n");
207 /* start writing the guard and action files, if they are needed. */
209 /* read in the grammar, build grammar in list form. write out guards and actions. */
211 /* Now we know whether we need the line-number stack.
212 If we do, write its type into the .tab.h file. */
214 reader_output_yylsp(fdefines
);
215 /* write closing delimiters for actions and guards. */
218 fprintf(ftable
, "#define YYLSP_NEEDED\n\n");
219 /* assign the symbols their symbol numbers.
220 Write #defines for the token symbols into fdefines if requested. */
222 /* convert the grammar into the format described in gram.h. */
224 /* free the symbol table data structure
225 since symbols are now all referred to by symbol number. */
230 reader_output_yylsp(f
)
234 fprintf(f
, LTYPESTR
);
237 /* read from finput until %% is seen. Discard the %%.
238 Handle any % declarations,
239 and copy the contents of any %{ ... %} groups to fattrs. */
249 c
= skip_white_space();
253 tok
= parse_percent_token();
260 case PERCENT_LEFT_CURLY
:
265 parse_token_decl (STOKEN
, SNTERM
);
269 parse_token_decl (SNTERM
, STOKEN
);
291 parse_assoc_decl(LEFT_ASSOC
);
295 parse_assoc_decl(RIGHT_ASSOC
);
299 parse_assoc_decl(NON_ASSOC
);
302 case SEMANTIC_PARSER
:
303 if (semantic_parser
== 0)
318 warns(_("unrecognized: %s"), token_buffer
);
323 fatal(_("no input grammar"));
327 sprintf(buff
, _("unknown character: %s"), printable_version(c
));
335 /* copy the contents of a %{ ... %} into the definitions file.
336 The %{ has already been read. Return after reading the %}. */
344 register int after_percent
; /* -1 while reading a character if prev char was % */
348 fprintf(fattrs
, "#line %d \"%s\"\n", lineno
, infile
);
376 fatal(_("unterminated string at end of file"));
379 warn(_("unterminated string"));
391 fatal(_("unterminated string at end of file"));
406 if (c
!= '*' && c
!= '/')
409 cplus_comment
= (c
== '/');
416 if (!cplus_comment
&& c
== '*')
440 fatal(_("unterminated comment in `%{' definition"));
451 fatal(_("unterminated `%{' definition"));
473 /* parse what comes after %token or %nterm.
474 For %token, what_is is STOKEN and what_is_not is SNTERM.
475 For %nterm, the arguments are reversed. */
478 parse_token_decl (what_is
, what_is_not
)
479 int what_is
, what_is_not
;
481 register int token
= 0;
482 register char *typename
= 0;
483 register struct bucket
*symbol
= NULL
; /* pts to symbol being defined */
488 if(ungetc(skip_white_space(), finput
) == '%')
496 if (token
== TYPENAME
)
498 k
= strlen(token_buffer
);
499 typename
= NEW2(k
+ 1, char);
500 strcpy(typename
, token_buffer
);
501 value_components_used
= 1;
504 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"'
508 symval
->class = STOKEN
;
509 symval
->type_name
= typename
;
510 symval
->user_token_number
= symbol
->user_token_number
;
511 symbol
->user_token_number
= SALIAS
;
513 symval
->alias
= symbol
;
514 symbol
->alias
= symval
;
517 nsyms
--; /* symbol and symval combined are only one symbol */
519 else if (token
== IDENTIFIER
)
521 int oldclass
= symval
->class;
524 if (symbol
->class == what_is_not
)
525 warns(_("symbol %s redefined"), symbol
->tag
);
526 symbol
->class = what_is
;
527 if (what_is
== SNTERM
&& oldclass
!= SNTERM
)
528 symbol
->value
= nvars
++;
532 if (symbol
->type_name
== NULL
)
533 symbol
->type_name
= typename
;
534 else if (strcmp(typename
, symbol
->type_name
) != 0)
535 warns(_("type redeclaration for %s"), symbol
->tag
);
538 else if (symbol
&& token
== NUMBER
)
540 symbol
->user_token_number
= numval
;
545 warnss(_("`%s' is invalid in %s"),
547 (what_is
== STOKEN
) ? "%token" : "%nterm");
554 /* parse what comes after %thong
556 %thong <type> token number literal
557 the <type> or number may be omitted. The number specifies the
560 Two symbols are entered in the table, one for the token symbol and
561 one for the literal. Both are given the <type>, if any, from the declaration.
562 The ->user_token_number of the first is SALIAS and the ->user_token_number
563 of the second is set to the number, if any, from the declaration.
564 The two symbols are linked via pointers in their ->alias fields.
566 during output_defines_table, the symbol is reported
567 thereafter, only the literal string is retained
568 it is the literal string that is output to yytname
575 register struct bucket
*symbol
;
576 register char *typename
= 0;
580 token
= lex(); /* fetch typename or first token */
581 if (token
== TYPENAME
) {
582 k
= strlen(token_buffer
);
583 typename
= NEW2(k
+ 1, char);
584 strcpy(typename
, token_buffer
);
585 value_components_used
= 1;
586 token
= lex(); /* fetch first token */
589 /* process first token */
591 if (token
!= IDENTIFIER
)
593 warns(_("unrecognized item %s, expected an identifier"),
598 symval
->class = STOKEN
;
599 symval
->type_name
= typename
;
600 symval
->user_token_number
= SALIAS
;
603 token
= lex(); /* get number or literal string */
605 if (token
== NUMBER
) {
607 token
= lex(); /* okay, did number, now get literal */
611 /* process literal string token */
613 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
615 warns(_("expected string constant instead of %s"),
620 symval
->class = STOKEN
;
621 symval
->type_name
= typename
;
622 symval
->user_token_number
= usrtoknum
;
624 symval
->alias
= symbol
;
625 symbol
->alias
= symval
;
627 nsyms
--; /* symbol and symval combined are only one symbol */
631 /* parse what comes after %start */
637 warn(_("multiple %start declarations"));
638 if (lex() != IDENTIFIER
)
639 warn(_("invalid %start declaration"));
649 /* read in a %type declaration and record its information for get_type_name to access */
657 if (lex() != TYPENAME
)
659 warn(_("%type declaration has no <typename>"));
664 k
= strlen(token_buffer
);
665 name
= NEW2(k
+ 1, char);
666 strcpy(name
, token_buffer
);
672 if(ungetc(skip_white_space(), finput
) == '%')
685 if (symval
->type_name
== NULL
)
686 symval
->type_name
= name
;
687 else if (strcmp(name
, symval
->type_name
) != 0)
688 warns(_("type redeclaration for %s"), symval
->tag
);
693 warns(_("invalid %%type declaration due to item: `%s'"), token_buffer
);
701 /* read in a %left, %right or %nonassoc declaration and record its information. */
702 /* assoc is either LEFT_ASSOC, RIGHT_ASSOC or NON_ASSOC. */
705 parse_assoc_decl (assoc
)
709 register char *name
= NULL
;
710 register int prev
= 0;
712 lastprec
++; /* Assign a new precedence level, never 0. */
718 if(ungetc(skip_white_space(), finput
) == '%')
727 k
= strlen(token_buffer
);
728 name
= NEW2(k
+ 1, char);
729 strcpy(name
, token_buffer
);
736 if (symval
->prec
!= 0)
737 warns(_("redefining precedence of %s"), symval
->tag
);
738 symval
->prec
= lastprec
;
739 symval
->assoc
= assoc
;
740 if (symval
->class == SNTERM
)
741 warns(_("symbol %s redefined"), symval
->tag
);
742 symval
->class = STOKEN
;
744 { /* record the type, if one is specified */
745 if (symval
->type_name
== NULL
)
746 symval
->type_name
= name
;
747 else if (strcmp(name
, symval
->type_name
) != 0)
748 warns(_("type redeclaration for %s"), symval
->tag
);
753 if (prev
== IDENTIFIER
)
755 symval
->user_token_number
= numval
;
760 warns(_("invalid text (%s) - number should be after identifier"),
770 warns(_("unexpected item: %s"), token_buffer
);
781 /* copy the union declaration into fattrs (and fdefines),
782 where it is made into the
783 definition of YYSTYPE, the type of elements of the parser value stack. */
790 register int in_comment
;
794 warn(_("multiple %union declarations"));
799 fprintf(fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
801 fprintf(fattrs
, "\n");
803 fprintf(fattrs
, "typedef union");
805 fprintf(fdefines
, "typedef union");
826 if (c
!= '*' && c
!= '/')
833 cplus_comment
= (c
== '/');
852 fatal(_("unterminated comment at end of file"));
854 if (!cplus_comment
&& c
== '*')
878 warn (_("unmatched close-brace (`}')"));
882 fprintf(fattrs
, " YYSTYPE;\n");
884 fprintf(fdefines
, " YYSTYPE;\n");
885 /* JF don't choke on trailing semi */
886 c
=skip_white_space();
887 if(c
!=';') ungetc(c
,finput
);
896 /* parse the declaration %expect N which says to expect N
897 shift-reduce conflicts. */
907 while (c
== ' ' || c
== '\t')
911 while (c
>= '0' && c
<= '9')
921 if (count
<= 0 || count
> 10)
922 warn(_("argument of %expect is not an integer"));
923 expected_conflicts
= atoi (buffer
);
926 /* that's all of parsing the declaration section */
928 /* Get the data type (alternative in the union) of the value for symbol n in rule rule. */
931 get_type_name(n
, rule
)
935 static char *msg
= N_("invalid $ value");
938 register symbol_list
*rp
;
952 if (rp
== NULL
|| rp
->sym
== NULL
)
960 return (rp
->sym
->type_name
);
964 /* after %guard is seen in the input file,
965 copy the actual guard into the guards file.
966 If the guard is followed by an action, copy that into the actions file.
967 stack_offset is the number of values in the current rule so far,
968 which says where to find $0 with respect to the top of the stack,
969 for the simple parser in which the stack is not popped until after the guard is run. */
972 copy_guard(rule
, stack_offset
)
981 register char *type_name
;
985 /* offset is always 0 if parser has already popped the stack pointer */
986 if (semantic_parser
) stack_offset
= 0;
988 fprintf(fguard
, "\ncase %d:\n", nrules
);
990 fprintf(fguard
, "#line %d \"%s\"\n", lineno
, infile
);
996 while (brace_flag
? (count
> 0) : (c
!= ';'))
1017 warn(_("unmatched right brace (`}')"));
1018 c
= getc(finput
); /* skip it */
1031 fatal(_("unterminated string at end of file"));
1034 warn(_("unterminated string"));
1036 c
= match
; /* invent terminator */
1046 fatal(_("unterminated string"));
1061 if (c
!= '*' && c
!= '/')
1064 cplus_comment
= (c
== '/');
1071 if (!cplus_comment
&& c
== '*')
1095 fatal(_("unterminated comment"));
1111 register char *cp
= token_buffer
;
1113 while ((c
= getc(finput
)) != '>' && c
> 0)
1116 type_name
= token_buffer
;
1123 fprintf(fguard
, "yyval");
1124 if (!type_name
) type_name
= rule
->sym
->type_name
;
1126 fprintf(fguard
, ".%s", type_name
);
1127 if(!type_name
&& typed
)
1128 warns(_("$$ of `%s' has no declared type"), rule
->sym
->tag
);
1131 else if (isdigit(c
) || c
== '-')
1134 n
= read_signed_integer(finput
);
1137 if (!type_name
&& n
> 0)
1138 type_name
= get_type_name(n
, rule
);
1140 fprintf(fguard
, "yyvsp[%d]", n
- stack_offset
);
1142 fprintf(fguard
, ".%s", type_name
);
1143 if(!type_name
&& typed
)
1144 warnss(_("$%s of `%s' has no declared type"), int_to_string(n
), rule
->sym
->tag
);
1148 warns(_("$%s is invalid"), printable_version(c
));
1154 if (isdigit(c
) || c
== '-')
1157 n
= read_signed_integer(finput
);
1162 warns(_("@%s is invalid"), printable_version(c
));
1166 fprintf(fguard
, "yylsp[%d]", n
- stack_offset
);
1172 fatal(_("unterminated %%guard clause"));
1178 if (c
!= '}' || count
!= 0)
1182 c
= skip_white_space();
1184 fprintf(fguard
, ";\n break;}");
1186 copy_action(rule
, stack_offset
);
1189 c
= getc(finput
); /* why not skip_white_space -wjh */
1191 copy_action(rule
, stack_offset
);
1199 /* Assuming that a { has just been seen, copy everything up to the matching }
1200 into the actions file.
1201 stack_offset is the number of values in the current rule so far,
1202 which says where to find $0 with respect to the top of the stack. */
1205 copy_action(rule
, stack_offset
)
1214 register char *type_name
;
1217 /* offset is always 0 if parser has already popped the stack pointer */
1218 if (semantic_parser
) stack_offset
= 0;
1220 fprintf(faction
, "\ncase %d:\n", nrules
);
1222 fprintf(faction
, "#line %d \"%s\"\n", lineno
, infile
);
1254 warn(_("unterminated string"));
1260 fatal(_("unterminated string at end of file"));
1268 fatal(_("unterminated string"));
1283 if (c
!= '*' && c
!= '/')
1286 cplus_comment
= (c
== '/');
1293 if (!cplus_comment
&& c
== '*')
1317 fatal(_("unterminated comment"));
1333 register char *cp
= token_buffer
;
1335 while ((c
= getc(finput
)) != '>' && c
> 0)
1338 type_name
= token_buffer
;
1339 value_components_used
= 1;
1345 fprintf(faction
, "yyval");
1346 if (!type_name
) type_name
= get_type_name(0, rule
);
1348 fprintf(faction
, ".%s", type_name
);
1349 if(!type_name
&& typed
)
1350 warns(_("$$ of `%s' has no declared type"), rule
->sym
->tag
);
1352 else if (isdigit(c
) || c
== '-')
1355 n
= read_signed_integer(finput
);
1358 if (!type_name
&& n
> 0)
1359 type_name
= get_type_name(n
, rule
);
1361 fprintf(faction
, "yyvsp[%d]", n
- stack_offset
);
1363 fprintf(faction
, ".%s", type_name
);
1364 if(!type_name
&& typed
)
1365 warnss(_("$%s of `%s' has no declared type"),
1366 int_to_string(n
), rule
->sym
->tag
);
1370 warns(_("$%s is invalid"), printable_version(c
));
1376 if (isdigit(c
) || c
== '-')
1379 n
= read_signed_integer(finput
);
1384 warn(_("invalid @-construct"));
1388 fprintf(faction
, "yylsp[%d]", n
- stack_offset
);
1394 fatal(_("unmatched `{'"));
1403 /* above loop exits when c is '}' */
1412 fprintf(faction
, ";\n break;}");
1417 /* generate a dummy symbol, a nonterminal,
1418 whose name cannot conflict with the user's names. */
1423 register bucket
*sym
;
1425 sprintf (token_buffer
, "@%d", ++gensym_count
);
1426 sym
= getsym(token_buffer
);
1427 sym
->class = SNTERM
;
1428 sym
->value
= nvars
++;
1432 /* Parse the input grammar into a one symbol_list structure.
1433 Each rule is represented by a sequence of symbols: the left hand side
1434 followed by the contents of the right hand side, followed by a null pointer
1435 instead of a symbol to terminate the rule.
1436 The next symbol is the lhs of the following rule.
1438 All guards and actions are copied out to the appropriate files,
1439 labelled by the rule number they apply to. */
1445 register bucket
*lhs
;
1446 register symbol_list
*p
;
1447 register symbol_list
*p1
;
1448 register bucket
*bp
;
1450 symbol_list
*crule
; /* points to first symbol_list of current rule. */
1451 /* its symbol is the lhs of the rule. */
1452 symbol_list
*crule1
; /* points to the symbol_list preceding crule. */
1458 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1460 if (t
== IDENTIFIER
|| t
== BAR
)
1462 register int actionflag
= 0;
1463 int rulelength
= 0; /* number of symbols in rhs of this rule so far */
1464 int xactions
= 0; /* JF for error checking */
1465 bucket
*first_rhs
= 0;
1467 if (t
== IDENTIFIER
)
1480 warn(_("ill-formed rule: initial symbol not followed by colon"));
1485 if (nrules
== 0 && t
== BAR
)
1487 warn(_("grammar starts with vertical bar"));
1488 lhs
= symval
; /* BOGUS: use a random symval */
1490 /* start a new rule and record its lhs. */
1495 record_rule_line ();
1497 p
= NEW(symbol_list
);
1509 /* mark the rule's lhs as a nonterminal if not already so. */
1511 if (lhs
->class == SUNKNOWN
)
1513 lhs
->class = SNTERM
;
1517 else if (lhs
->class == STOKEN
)
1518 warns(_("rule given for %s, which is a token"), lhs
->tag
);
1520 /* read the rhs of the rule. */
1528 crule
->ruleprec
= symval
;
1532 if (! (t
== IDENTIFIER
|| t
== LEFT_CURLY
)) break;
1534 /* If next token is an identifier, see if a colon follows it.
1535 If one does, exit this rule now. */
1536 if (t
== IDENTIFIER
)
1538 register bucket
*ssave
;
1545 if (t1
== COLON
) break;
1547 if(!first_rhs
) /* JF */
1549 /* Not followed by colon =>
1550 process as part of this rule's rhs. */
1553 /* If we just passed an action, that action was in the middle
1554 of a rule, so make a dummy rule to reduce it to a
1558 register bucket
*sdummy
;
1560 /* Since the action was written out with this rule's */
1561 /* number, we must give the new rule this number */
1562 /* by inserting the new rule before it. */
1564 /* Make a dummy nonterminal, a gensym. */
1567 /* Make a new rule, whose body is empty,
1568 before the current one, so that the action
1569 just read can belong to it. */
1572 record_rule_line ();
1573 p
= NEW(symbol_list
);
1578 crule1
= NEW(symbol_list
);
1580 crule1
->next
= crule
;
1582 /* insert the dummy generated by that rule into this rule. */
1584 p
= NEW(symbol_list
);
1592 if (t
== IDENTIFIER
)
1595 p
= NEW(symbol_list
);
1600 else /* handle an action. */
1602 copy_action(crule
, rulelength
);
1604 xactions
++; /* JF */
1607 } /* end of read rhs of rule */
1609 /* Put an empty link in the list to mark the end of this rule */
1610 p
= NEW(symbol_list
);
1616 warn(_("two @prec's in a row"));
1618 crule
->ruleprec
= symval
;
1623 if (! semantic_parser
)
1624 warn(_("%%guard present but %%semantic_parser not specified"));
1626 copy_guard(crule
, rulelength
);
1629 else if (t
== LEFT_CURLY
)
1631 /* This case never occurs -wjh */
1632 if (actionflag
) warn(_("two actions at end of one rule"));
1633 copy_action(crule
, rulelength
);
1635 xactions
++; /* -wjh */
1638 /* If $$ is being set in default way,
1639 warn if any type mismatch. */
1640 else if (!xactions
&& first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1642 if (lhs
->type_name
== 0 || first_rhs
->type_name
== 0
1643 || strcmp(lhs
->type_name
,first_rhs
->type_name
))
1644 warnss(_("type clash (`%s' `%s') on default action"),
1645 lhs
->type_name
? lhs
->type_name
: "",
1646 first_rhs
->type_name
? first_rhs
->type_name
: "");
1648 /* Warn if there is no default for $$ but we need one. */
1649 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1650 warn(_("empty rule for typed nonterminal, and no action"));
1655 /* these things can appear as alternatives to rules. */
1657 a) none of the documentation allows them
1658 b) most of them scan forward until finding a next %
1659 thus they may swallow lots of intervening rules
1661 else if (t
== TOKEN
)
1663 parse_token_decl(STOKEN
, SNTERM
);
1666 else if (t
== NTERM
)
1668 parse_token_decl(SNTERM
, STOKEN
);
1675 else if (t
== UNION
)
1680 else if (t
== EXPECT
)
1682 parse_expect_decl();
1685 else if (t
== START
)
1694 warns(_("invalid input: %s"), token_buffer
);
1699 /* grammar has been read. Do some checking */
1701 if (nsyms
> MAXSHORT
)
1702 fatals(_("too many symbols (tokens plus nonterminals); maximum %s"),
1703 int_to_string(MAXSHORT
));
1705 fatal(_("no rules in the input grammar"));
1707 if (typed
== 0 /* JF put out same default YYSTYPE as YACC does */
1708 && !value_components_used
)
1710 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1711 but it seems better to be consistent.
1712 Most programs should declare their own type anyway. */
1713 fprintf(fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1715 fprintf(fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1718 /* Report any undefined symbols and consider them nonterminals. */
1720 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1721 if (bp
->class == SUNKNOWN
)
1723 warns(_("symbol %s is used, but is not defined as a token and has no rules"),
1726 bp
->value
= nvars
++;
1729 ntokens
= nsyms
- nvars
;
1736 /* Record each rule's source line number in rline table. */
1738 if (nrules
>= rline_allocated
)
1740 rline_allocated
= nrules
* 2;
1741 rline
= (short *) xrealloc (rline
,
1742 rline_allocated
* sizeof (short));
1744 rline
[nrules
] = lineno
;
1748 /* read in a %type declaration and record its information for get_type_name to access */
1749 /* this is unused. it is only called from the #if 0 part of readgram */
1755 register char *name
;
1761 warn(_("ill-formed %type declaration"));
1765 k
= strlen(token_buffer
);
1766 name
= NEW2(k
+ 1, char);
1767 strcpy(name
, token_buffer
);
1782 if (symval
->type_name
== NULL
)
1783 symval
->type_name
= name
;
1784 else if (strcmp(name
, symval
->type_name
) != 0)
1785 warns(_("type redeclaration for %s"), symval
->tag
);
1797 /* assign symbol numbers, and write definition of token names into fdefines.
1798 Set up vectors tags and sprec of names and precedences of symbols. */
1803 register bucket
*bp
;
1804 register int tokno
= 1;
1806 register int last_user_token_number
;
1808 /* int lossage = 0; JF set but not used */
1810 tags
= NEW2(nsyms
+ 1, char *);
1812 user_toknums
= NEW2(nsyms
+ 1, int);
1813 user_toknums
[0] = 0;
1815 sprec
= NEW2(nsyms
, short);
1816 sassoc
= NEW2(nsyms
, short);
1818 max_user_token_number
= 256;
1819 last_user_token_number
= 256;
1821 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1823 if (bp
->class == SNTERM
)
1825 bp
->value
+= ntokens
;
1829 /* this symbol and its alias are a single token defn.
1830 allocate a tokno, and assign to both
1831 check agreement of ->prec and ->assoc fields
1832 and make both the same
1835 bp
->value
= bp
->alias
->value
= tokno
++;
1837 if (bp
->prec
!= bp
->alias
->prec
) {
1838 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1839 && bp
->user_token_number
== SALIAS
)
1840 warnss(_("conflicting precedences for %s and %s"),
1841 bp
->tag
, bp
->alias
->tag
);
1842 if (bp
->prec
!= 0) bp
->alias
->prec
= bp
->prec
;
1843 else bp
->prec
= bp
->alias
->prec
;
1846 if (bp
->assoc
!= bp
->alias
->assoc
) {
1847 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1848 && bp
->user_token_number
== SALIAS
)
1849 warnss(_("conflicting assoc values for %s and %s"),
1850 bp
->tag
, bp
->alias
->tag
);
1851 if (bp
->assoc
!= 0) bp
->alias
->assoc
= bp
->assoc
;
1852 else bp
->assoc
= bp
->alias
->assoc
;
1855 if (bp
->user_token_number
== SALIAS
)
1856 continue; /* do not do processing below for SALIASs */
1859 else /* bp->class == STOKEN */
1861 bp
->value
= tokno
++;
1864 if (bp
->class == STOKEN
)
1866 if (translations
&& !(bp
->user_token_number
))
1867 bp
->user_token_number
= ++last_user_token_number
;
1868 if (bp
->user_token_number
> max_user_token_number
)
1869 max_user_token_number
= bp
->user_token_number
;
1872 tags
[bp
->value
] = bp
->tag
;
1873 user_toknums
[bp
->value
] = bp
->user_token_number
;
1874 sprec
[bp
->value
] = bp
->prec
;
1875 sassoc
[bp
->value
] = bp
->assoc
;
1883 token_translations
= NEW2(max_user_token_number
+1, short);
1885 /* initialize all entries for literal tokens to 2,
1886 the internal token number for $undefined.,
1887 which represents all invalid inputs. */
1888 for (i
= 0; i
<= max_user_token_number
; i
++)
1889 token_translations
[i
] = 2;
1891 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1893 if (bp
->value
>= ntokens
) continue; /* non-terminal */
1894 if (bp
->user_token_number
== SALIAS
) continue;
1895 if (token_translations
[bp
->user_token_number
] != 2)
1896 warnsss(_("tokens %s and %s both assigned number %s"),
1897 tags
[token_translations
[bp
->user_token_number
]],
1899 int_to_string(bp
->user_token_number
));
1900 token_translations
[bp
->user_token_number
] = bp
->value
;
1904 error_token_number
= errtoken
->value
;
1907 output_token_defines(ftable
);
1909 if (startval
->class == SUNKNOWN
)
1910 fatals(_("the start symbol %s is undefined"), startval
->tag
);
1911 else if (startval
->class == STOKEN
)
1912 fatals(_("the start symbol %s is a token"), startval
->tag
);
1914 start_symbol
= startval
->value
;
1918 output_token_defines(fdefines
);
1922 if (spec_name_prefix
)
1923 fprintf(fdefines
, "\nextern YYSTYPE %slval;\n", spec_name_prefix
);
1925 fprintf(fdefines
, "\nextern YYSTYPE yylval;\n");
1928 if (semantic_parser
)
1929 for (i
= ntokens
; i
< nsyms
; i
++)
1931 /* don't make these for dummy nonterminals made by gensym. */
1932 if (*tags
[i
] != '@')
1933 fprintf(fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1936 /* `fdefines' is now a temporary file, so we need to copy its
1937 contents in `done', so we can't close it here. */
1944 /* For named tokens, but not literal ones, define the name.
1945 The value is the user token number.
1948 output_token_defines(file
)
1952 register char *cp
, *symbol
;
1955 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1957 symbol
= bp
->tag
; /* get symbol */
1959 if (bp
->value
>= ntokens
) continue;
1960 if (bp
->user_token_number
== SALIAS
) continue;
1961 if ('\'' == *symbol
) continue; /* skip literal character */
1962 if (bp
== errtoken
) continue; /* skip error token */
1963 if ('\"' == *symbol
)
1965 /* use literal string only if given a symbol with an alias */
1967 symbol
= bp
->alias
->tag
;
1972 /* Don't #define nonliteral tokens whose names contain periods. */
1974 while ((c
= *cp
++) && c
!= '.');
1975 if (c
!= '\0') continue;
1977 fprintf(file
, "#define\t%s\t%d\n", symbol
,
1978 ((translations
&& ! rawtoknumflag
)
1979 ? bp
->user_token_number
1981 if (semantic_parser
)
1982 fprintf(file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1990 /* convert the rules into the representation using rrhs, rlhs and ritems. */
1995 register int itemno
;
1996 register int ruleno
;
1997 register symbol_list
*p
;
1998 /* register bucket *bp; JF unused */
2002 ritem
= NEW2(nitems
+ 1, short);
2003 rlhs
= NEW2(nrules
, short) - 1;
2004 rrhs
= NEW2(nrules
, short) - 1;
2005 rprec
= NEW2(nrules
, short) - 1;
2006 rprecsym
= NEW2(nrules
, short) - 1;
2007 rassoc
= NEW2(nrules
, short) - 1;
2015 rlhs
[ruleno
] = p
->sym
->value
;
2016 rrhs
[ruleno
] = itemno
;
2017 ruleprec
= p
->ruleprec
;
2022 ritem
[itemno
++] = p
->sym
->value
;
2023 /* A rule gets by default the precedence and associativity
2024 of the last token in it. */
2025 if (p
->sym
->class == STOKEN
)
2027 rprec
[ruleno
] = p
->sym
->prec
;
2028 rassoc
[ruleno
] = p
->sym
->assoc
;
2033 /* If this rule has a %prec,
2034 the specified symbol's precedence replaces the default. */
2037 rprec
[ruleno
] = ruleprec
->prec
;
2038 rassoc
[ruleno
] = ruleprec
->assoc
;
2039 rprecsym
[ruleno
] = ruleprec
->value
;
2042 ritem
[itemno
++] = -ruleno
;
2051 /* Read a signed integer from STREAM and return its value. */
2054 read_signed_integer (stream
)
2057 register int c
= getc(stream
);
2058 register int sign
= 1;
2069 n
= 10*n
+ (c
- '0');