]>
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
;
126 /* Nonzero if any action or guard uses the @n construct. */
127 static int yylsp_needed
;
129 extern char *version_string
;
138 warn(" Skipping to next \\n");
140 warni(" Skipping to next %c", target
);
143 c
= skip_white_space();
144 while (c
!= target
&& c
!= EOF
);
154 startval
= NULL
; /* start symbol not specified yet. */
157 translations
= 0; /* initially assume token number translation not needed. */
159 /* Nowadays translations is always set to 1,
160 since we give `error' a user-token-number
161 to satisfy the Posix demand for YYERRCODE==256. */
168 rline_allocated
= 10;
169 rline
= NEW2(rline_allocated
, short);
185 /* initialize the symbol table. */
187 /* construct the error token */
188 errtoken
= getsym("error");
189 errtoken
->class = STOKEN
;
190 errtoken
->user_token_number
= 256; /* Value specified by posix. */
191 /* construct a token that represents all undefined literal tokens. */
192 /* it is always token number 2. */
193 getsym("$undefined.")->class = STOKEN
;
194 /* Read the declaration section. Copy %{ ... %} groups to ftable and fdefines file.
195 Also notice any %token, %left, etc. found there. */
197 fprintf(ftable
, "\n/* Bison-generated parse tables, made from %s\n",
200 fprintf(ftable
, "\n/* A Bison parser, made from %s\n", infile
);
201 fprintf(ftable
, " by %s */\n\n", version_string
);
202 fprintf(ftable
, "#define YYBISON 1 /* Identify Bison output. */\n\n");
204 /* start writing the guard and action files, if they are needed. */
206 /* read in the grammar, build grammar in list form. write out guards and actions. */
208 /* Now we know whether we need the line-number stack.
209 If we do, write its type into the .tab.h file. */
211 reader_output_yylsp(fdefines
);
212 /* write closing delimiters for actions and guards. */
215 fprintf(ftable
, "#define YYLSP_NEEDED\n\n");
216 /* assign the symbols their symbol numbers.
217 Write #defines for the token symbols into fdefines if requested. */
219 /* convert the grammar into the format described in gram.h. */
221 /* free the symbol table data structure
222 since symbols are now all referred to by symbol number. */
227 reader_output_yylsp(f
)
231 fprintf(f
, LTYPESTR
);
234 /* read from finput until %% is seen. Discard the %%.
235 Handle any % declarations,
236 and copy the contents of any %{ ... %} groups to fattrs. */
246 c
= skip_white_space();
250 tok
= parse_percent_token();
257 case PERCENT_LEFT_CURLY
:
262 parse_token_decl (STOKEN
, SNTERM
);
266 parse_token_decl (SNTERM
, STOKEN
);
288 parse_assoc_decl(LEFT_ASSOC
);
292 parse_assoc_decl(RIGHT_ASSOC
);
296 parse_assoc_decl(NON_ASSOC
);
299 case SEMANTIC_PARSER
:
300 if (semantic_parser
== 0)
315 warns("unrecognized: %s", token_buffer
);
320 fatal("no input grammar");
324 sprintf(buff
, "unknown character: %s", printable_version(c
));
332 /* copy the contents of a %{ ... %} into the definitions file.
333 The %{ has already been read. Return after reading the %}. */
341 register int after_percent
; /* -1 while reading a character if prev char was % */
345 fprintf(fattrs
, "#line %d \"%s\"\n", lineno
, infile
);
373 fatal("unterminated string at end of file");
376 warn("unterminated string");
388 fatal("unterminated string at end of file");
403 if (c
!= '*' && c
!= '/')
406 cplus_comment
= (c
== '/');
413 if (!cplus_comment
&& c
== '*')
437 fatal("unterminated comment in `%{' definition");
448 fatal("unterminated `%{' definition");
470 /* parse what comes after %token or %nterm.
471 For %token, what_is is STOKEN and what_is_not is SNTERM.
472 For %nterm, the arguments are reversed. */
475 parse_token_decl (what_is
, what_is_not
)
476 int what_is
, what_is_not
;
478 register int token
= 0;
479 register char *typename
= 0;
480 register struct bucket
*symbol
= NULL
; /* pts to symbol being defined */
485 if(ungetc(skip_white_space(), finput
) == '%')
493 if (token
== TYPENAME
)
495 k
= strlen(token_buffer
);
496 typename
= NEW2(k
+ 1, char);
497 strcpy(typename
, token_buffer
);
498 value_components_used
= 1;
501 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"'
505 symval
->class = STOKEN
;
506 symval
->type_name
= typename
;
507 symval
->user_token_number
= symbol
->user_token_number
;
508 symbol
->user_token_number
= SALIAS
;
510 symval
->alias
= symbol
;
511 symbol
->alias
= symval
;
514 nsyms
--; /* 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 warns("symbol %s redefined", symbol
->tag
);
523 symbol
->class = what_is
;
524 if (what_is
== SNTERM
&& oldclass
!= SNTERM
)
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 warns("type redeclaration for %s", symbol
->tag
);
535 else if (symbol
&& token
== NUMBER
)
537 symbol
->user_token_number
= numval
;
542 warnss("`%s' is invalid in %s",
544 (what_is
== STOKEN
) ? "%token" : "%nterm");
551 /* parse what comes after %thong
553 %thong <type> token number literal
554 the <type> or number may be omitted. The number specifies the
557 Two symbols are entered in the table, one for the token symbol and
558 one for the literal. Both are given the <type>, if any, from the declaration.
559 The ->user_token_number of the first is SALIAS and the ->user_token_number
560 of the second is set to the number, if any, from the declaration.
561 The two symbols are linked via pointers in their ->alias fields.
563 during output_defines_table, the symbol is reported
564 thereafter, only the literal string is retained
565 it is the literal string that is output to yytname
572 register struct bucket
*symbol
;
573 register char *typename
= 0;
577 token
= lex(); /* fetch typename or first token */
578 if (token
== TYPENAME
) {
579 k
= strlen(token_buffer
);
580 typename
= NEW2(k
+ 1, char);
581 strcpy(typename
, token_buffer
);
582 value_components_used
= 1;
583 token
= lex(); /* fetch first token */
586 /* process first token */
588 if (token
!= IDENTIFIER
)
590 warns("unrecognized item %s, expected an identifier",
595 symval
->class = STOKEN
;
596 symval
->type_name
= typename
;
597 symval
->user_token_number
= SALIAS
;
600 token
= lex(); /* get number or literal string */
602 if (token
== NUMBER
) {
604 token
= lex(); /* okay, did number, now get literal */
608 /* process literal string token */
610 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
612 warns("expected string constant instead of %s",
617 symval
->class = STOKEN
;
618 symval
->type_name
= typename
;
619 symval
->user_token_number
= usrtoknum
;
621 symval
->alias
= symbol
;
622 symbol
->alias
= symval
;
624 nsyms
--; /* symbol and symval combined are only one symbol */
628 /* parse what comes after %start */
634 warn("multiple %start declarations");
635 if (lex() != IDENTIFIER
)
636 warn("invalid %start declaration");
646 /* read in a %type declaration and record its information for get_type_name to access */
654 if (lex() != TYPENAME
)
656 warn("%type declaration has no <typename>");
661 k
= strlen(token_buffer
);
662 name
= NEW2(k
+ 1, char);
663 strcpy(name
, token_buffer
);
669 if(ungetc(skip_white_space(), finput
) == '%')
682 if (symval
->type_name
== NULL
)
683 symval
->type_name
= name
;
684 else if (strcmp(name
, symval
->type_name
) != 0)
685 warns("type redeclaration for %s", symval
->tag
);
690 warns("invalid %%type declaration due to item: `%s'", token_buffer
);
698 /* read in a %left, %right or %nonassoc declaration and record its information. */
699 /* assoc is either LEFT_ASSOC, RIGHT_ASSOC or NON_ASSOC. */
702 parse_assoc_decl (assoc
)
706 register char *name
= NULL
;
707 register int prev
= 0;
709 lastprec
++; /* Assign a new precedence level, never 0. */
715 if(ungetc(skip_white_space(), finput
) == '%')
724 k
= strlen(token_buffer
);
725 name
= NEW2(k
+ 1, char);
726 strcpy(name
, token_buffer
);
733 if (symval
->prec
!= 0)
734 warns("redefining precedence of %s", symval
->tag
);
735 symval
->prec
= lastprec
;
736 symval
->assoc
= assoc
;
737 if (symval
->class == SNTERM
)
738 warns("symbol %s redefined", symval
->tag
);
739 symval
->class = STOKEN
;
741 { /* record the type, if one is specified */
742 if (symval
->type_name
== NULL
)
743 symval
->type_name
= name
;
744 else if (strcmp(name
, symval
->type_name
) != 0)
745 warns("type redeclaration for %s", symval
->tag
);
750 if (prev
== IDENTIFIER
)
752 symval
->user_token_number
= numval
;
757 warns("invalid text (%s) - number should be after identifier",
767 warns("unexpected item: %s", token_buffer
);
778 /* copy the union declaration into fattrs (and fdefines),
779 where it is made into the
780 definition of YYSTYPE, the type of elements of the parser value stack. */
787 register int in_comment
;
791 warn("multiple %union declarations");
796 fprintf(fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
798 fprintf(fattrs
, "\n");
800 fprintf(fattrs
, "typedef union");
802 fprintf(fdefines
, "typedef union");
823 if (c
!= '*' && c
!= '/')
830 cplus_comment
= (c
== '/');
849 fatal("unterminated comment at end of file");
851 if (!cplus_comment
&& c
== '*')
875 warn ("unmatched close-brace (`}')");
879 fprintf(fattrs
, " YYSTYPE;\n");
881 fprintf(fdefines
, " YYSTYPE;\n");
882 /* JF don't choke on trailing semi */
883 c
=skip_white_space();
884 if(c
!=';') ungetc(c
,finput
);
893 /* parse the declaration %expect N which says to expect N
894 shift-reduce conflicts. */
904 while (c
== ' ' || c
== '\t')
908 while (c
>= '0' && c
<= '9')
918 if (count
<= 0 || count
> 10)
919 warn("argument of %expect is not an integer");
920 expected_conflicts
= atoi (buffer
);
923 /* that's all of parsing the declaration section */
925 /* Get the data type (alternative in the union) of the value for symbol n in rule rule. */
928 get_type_name(n
, rule
)
932 static char *msg
= "invalid $ value";
935 register symbol_list
*rp
;
949 if (rp
== NULL
|| rp
->sym
== NULL
)
957 return (rp
->sym
->type_name
);
961 /* after %guard is seen in the input file,
962 copy the actual guard into the guards file.
963 If the guard is followed by an action, copy that into the actions file.
964 stack_offset is the number of values in the current rule so far,
965 which says where to find $0 with respect to the top of the stack,
966 for the simple parser in which the stack is not popped until after the guard is run. */
969 copy_guard(rule
, stack_offset
)
978 register char *type_name
;
982 /* offset is always 0 if parser has already popped the stack pointer */
983 if (semantic_parser
) stack_offset
= 0;
985 fprintf(fguard
, "\ncase %d:\n", nrules
);
987 fprintf(fguard
, "#line %d \"%s\"\n", lineno
, infile
);
993 while (brace_flag
? (count
> 0) : (c
!= ';'))
1014 warn("unmatched right brace (`}')");
1015 c
= getc(finput
); /* skip it */
1028 fatal("unterminated string at end of file");
1031 warn("unterminated string");
1033 c
= match
; /* invent terminator */
1043 fatal("unterminated string");
1058 if (c
!= '*' && c
!= '/')
1061 cplus_comment
= (c
== '/');
1068 if (!cplus_comment
&& c
== '*')
1092 fatal("unterminated comment");
1108 register char *cp
= token_buffer
;
1110 while ((c
= getc(finput
)) != '>' && c
> 0)
1113 type_name
= token_buffer
;
1120 fprintf(fguard
, "yyval");
1121 if (!type_name
) type_name
= rule
->sym
->type_name
;
1123 fprintf(fguard
, ".%s", type_name
);
1124 if(!type_name
&& typed
)
1125 warns("$$ of `%s' has no declared type", rule
->sym
->tag
);
1128 else if (isdigit(c
) || c
== '-')
1131 n
= read_signed_integer(finput
);
1134 if (!type_name
&& n
> 0)
1135 type_name
= get_type_name(n
, rule
);
1137 fprintf(fguard
, "yyvsp[%d]", n
- stack_offset
);
1139 fprintf(fguard
, ".%s", type_name
);
1140 if(!type_name
&& typed
)
1141 warnss("$%s of `%s' has no declared type", int_to_string(n
), rule
->sym
->tag
);
1145 warni("$%s is invalid", printable_version(c
));
1151 if (isdigit(c
) || c
== '-')
1154 n
= read_signed_integer(finput
);
1159 warni("@%s is invalid", printable_version(c
));
1163 fprintf(fguard
, "yylsp[%d]", n
- stack_offset
);
1169 fatal("unterminated %%guard clause");
1175 if (c
!= '}' || count
!= 0)
1179 c
= skip_white_space();
1181 fprintf(fguard
, ";\n break;}");
1183 copy_action(rule
, stack_offset
);
1186 c
= getc(finput
); /* why not skip_white_space -wjh */
1188 copy_action(rule
, stack_offset
);
1196 /* Assuming that a { has just been seen, copy everything up to the matching }
1197 into the actions file.
1198 stack_offset is the number of values in the current rule so far,
1199 which says where to find $0 with respect to the top of the stack. */
1202 copy_action(rule
, stack_offset
)
1211 register char *type_name
;
1214 /* offset is always 0 if parser has already popped the stack pointer */
1215 if (semantic_parser
) stack_offset
= 0;
1217 fprintf(faction
, "\ncase %d:\n", nrules
);
1219 fprintf(faction
, "#line %d \"%s\"\n", lineno
, infile
);
1251 warn("unterminated string");
1257 fatal("unterminated string at end of file");
1265 fatal("unterminated string");
1280 if (c
!= '*' && c
!= '/')
1283 cplus_comment
= (c
== '/');
1290 if (!cplus_comment
&& c
== '*')
1314 fatal("unterminated comment");
1330 register char *cp
= token_buffer
;
1332 while ((c
= getc(finput
)) != '>' && c
> 0)
1335 type_name
= token_buffer
;
1336 value_components_used
= 1;
1342 fprintf(faction
, "yyval");
1343 if (!type_name
) type_name
= get_type_name(0, rule
);
1345 fprintf(faction
, ".%s", type_name
);
1346 if(!type_name
&& typed
)
1347 warns("$$ of `%s' has no declared type", rule
->sym
->tag
);
1349 else if (isdigit(c
) || c
== '-')
1352 n
= read_signed_integer(finput
);
1355 if (!type_name
&& n
> 0)
1356 type_name
= get_type_name(n
, rule
);
1358 fprintf(faction
, "yyvsp[%d]", n
- stack_offset
);
1360 fprintf(faction
, ".%s", type_name
);
1361 if(!type_name
&& typed
)
1362 warnss("$%s of `%s' has no declared type",
1363 int_to_string(n
), rule
->sym
->tag
);
1367 warni("$%s is invalid", printable_version(c
));
1373 if (isdigit(c
) || c
== '-')
1376 n
= read_signed_integer(finput
);
1381 warn("invalid @-construct");
1385 fprintf(faction
, "yylsp[%d]", n
- stack_offset
);
1391 fatal("unmatched `{'");
1400 /* above loop exits when c is '}' */
1409 fprintf(faction
, ";\n break;}");
1414 /* generate a dummy symbol, a nonterminal,
1415 whose name cannot conflict with the user's names. */
1420 register bucket
*sym
;
1422 sprintf (token_buffer
, "@%d", ++gensym_count
);
1423 sym
= getsym(token_buffer
);
1424 sym
->class = SNTERM
;
1425 sym
->value
= nvars
++;
1429 /* Parse the input grammar into a one symbol_list structure.
1430 Each rule is represented by a sequence of symbols: the left hand side
1431 followed by the contents of the right hand side, followed by a null pointer
1432 instead of a symbol to terminate the rule.
1433 The next symbol is the lhs of the following rule.
1435 All guards and actions are copied out to the appropriate files,
1436 labelled by the rule number they apply to. */
1442 register bucket
*lhs
;
1443 register symbol_list
*p
;
1444 register symbol_list
*p1
;
1445 register bucket
*bp
;
1447 symbol_list
*crule
; /* points to first symbol_list of current rule. */
1448 /* its symbol is the lhs of the rule. */
1449 symbol_list
*crule1
; /* points to the symbol_list preceding crule. */
1455 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1457 if (t
== IDENTIFIER
|| t
== BAR
)
1459 register int actionflag
= 0;
1460 int rulelength
= 0; /* number of symbols in rhs of this rule so far */
1461 int xactions
= 0; /* JF for error checking */
1462 bucket
*first_rhs
= 0;
1464 if (t
== IDENTIFIER
)
1477 warn("ill-formed rule: initial symbol not followed by colon");
1482 if (nrules
== 0 && t
== BAR
)
1484 warn("grammar starts with vertical bar");
1485 lhs
= symval
; /* BOGUS: use a random symval */
1487 /* start a new rule and record its lhs. */
1492 record_rule_line ();
1494 p
= NEW(symbol_list
);
1506 /* mark the rule's lhs as a nonterminal if not already so. */
1508 if (lhs
->class == SUNKNOWN
)
1510 lhs
->class = SNTERM
;
1514 else if (lhs
->class == STOKEN
)
1515 warns("rule given for %s, which is a token", lhs
->tag
);
1517 /* read the rhs of the rule. */
1525 crule
->ruleprec
= symval
;
1529 if (! (t
== IDENTIFIER
|| t
== LEFT_CURLY
)) break;
1531 /* If next token is an identifier, see if a colon follows it.
1532 If one does, exit this rule now. */
1533 if (t
== IDENTIFIER
)
1535 register bucket
*ssave
;
1542 if (t1
== COLON
) break;
1544 if(!first_rhs
) /* JF */
1546 /* Not followed by colon =>
1547 process as part of this rule's rhs. */
1550 /* If we just passed an action, that action was in the middle
1551 of a rule, so make a dummy rule to reduce it to a
1555 register bucket
*sdummy
;
1557 /* Since the action was written out with this rule's */
1558 /* number, we must give the new rule this number */
1559 /* by inserting the new rule before it. */
1561 /* Make a dummy nonterminal, a gensym. */
1564 /* Make a new rule, whose body is empty,
1565 before the current one, so that the action
1566 just read can belong to it. */
1569 record_rule_line ();
1570 p
= NEW(symbol_list
);
1575 crule1
= NEW(symbol_list
);
1577 crule1
->next
= crule
;
1579 /* insert the dummy generated by that rule into this rule. */
1581 p
= NEW(symbol_list
);
1589 if (t
== IDENTIFIER
)
1592 p
= NEW(symbol_list
);
1597 else /* handle an action. */
1599 copy_action(crule
, rulelength
);
1601 xactions
++; /* JF */
1604 } /* end of read rhs of rule */
1606 /* Put an empty link in the list to mark the end of this rule */
1607 p
= NEW(symbol_list
);
1613 warn("two @prec's in a row");
1615 crule
->ruleprec
= symval
;
1620 if (! semantic_parser
)
1621 warn("%%guard present but %%semantic_parser not specified");
1623 copy_guard(crule
, rulelength
);
1626 else if (t
== LEFT_CURLY
)
1628 /* This case never occurs -wjh */
1629 if (actionflag
) warn("two actions at end of one rule");
1630 copy_action(crule
, rulelength
);
1632 xactions
++; /* -wjh */
1635 /* If $$ is being set in default way,
1636 warn if any type mismatch. */
1637 else if (!xactions
&& first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1639 if (lhs
->type_name
== 0 || first_rhs
->type_name
== 0
1640 || strcmp(lhs
->type_name
,first_rhs
->type_name
))
1641 warnss("type clash (`%s' `%s') on default action",
1642 lhs
->type_name
? lhs
->type_name
: "",
1643 first_rhs
->type_name
? first_rhs
->type_name
: "");
1645 /* Warn if there is no default for $$ but we need one. */
1646 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1647 warn("empty rule for typed nonterminal, and no action");
1652 /* these things can appear as alternatives to rules. */
1654 a) none of the documentation allows them
1655 b) most of them scan forward until finding a next %
1656 thus they may swallow lots of intervening rules
1658 else if (t
== TOKEN
)
1660 parse_token_decl(STOKEN
, SNTERM
);
1663 else if (t
== NTERM
)
1665 parse_token_decl(SNTERM
, STOKEN
);
1672 else if (t
== UNION
)
1677 else if (t
== EXPECT
)
1679 parse_expect_decl();
1682 else if (t
== START
)
1691 warns("invalid input: %s", token_buffer
);
1696 /* grammar has been read. Do some checking */
1698 if (nsyms
> MAXSHORT
)
1699 fatals("too many symbols (tokens plus nonterminals); maximum %s",
1700 int_to_string(MAXSHORT
));
1702 fatal("no rules in the input grammar");
1704 if (typed
== 0 /* JF put out same default YYSTYPE as YACC does */
1705 && !value_components_used
)
1707 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1708 but it seems better to be consistent.
1709 Most programs should declare their own type anyway. */
1710 fprintf(fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1712 fprintf(fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1715 /* Report any undefined symbols and consider them nonterminals. */
1717 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1718 if (bp
->class == SUNKNOWN
)
1720 warns("symbol %s is used, but is not defined as a token and has no rules",
1723 bp
->value
= nvars
++;
1726 ntokens
= nsyms
- nvars
;
1733 /* Record each rule's source line number in rline table. */
1735 if (nrules
>= rline_allocated
)
1737 rline_allocated
= nrules
* 2;
1738 rline
= (short *) xrealloc (rline
,
1739 rline_allocated
* sizeof (short));
1741 rline
[nrules
] = lineno
;
1745 /* read in a %type declaration and record its information for get_type_name to access */
1746 /* this is unused. it is only called from the #if 0 part of readgram */
1752 register char *name
;
1758 warn("ill-formed %type declaration");
1762 k
= strlen(token_buffer
);
1763 name
= NEW2(k
+ 1, char);
1764 strcpy(name
, token_buffer
);
1779 if (symval
->type_name
== NULL
)
1780 symval
->type_name
= name
;
1781 else if (strcmp(name
, symval
->type_name
) != 0)
1782 warns("type redeclaration for %s", symval
->tag
);
1794 /* assign symbol numbers, and write definition of token names into fdefines.
1795 Set up vectors tags and sprec of names and precedences of symbols. */
1800 register bucket
*bp
;
1801 register int tokno
= 1;
1803 register int last_user_token_number
;
1805 /* int lossage = 0; JF set but not used */
1807 tags
= NEW2(nsyms
+ 1, char *);
1809 user_toknums
= NEW2(nsyms
+ 1, int);
1810 user_toknums
[0] = 0;
1812 sprec
= NEW2(nsyms
, short);
1813 sassoc
= NEW2(nsyms
, short);
1815 max_user_token_number
= 256;
1816 last_user_token_number
= 256;
1818 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1820 if (bp
->class == SNTERM
)
1822 bp
->value
+= ntokens
;
1826 /* this symbol and its alias are a single token defn.
1827 allocate a tokno, and assign to both
1828 check agreement of ->prec and ->assoc fields
1829 and make both the same
1832 bp
->value
= bp
->alias
->value
= tokno
++;
1834 if (bp
->prec
!= bp
->alias
->prec
) {
1835 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1836 && bp
->user_token_number
== SALIAS
)
1837 warnss("conflicting precedences for %s and %s",
1838 bp
->tag
, bp
->alias
->tag
);
1839 if (bp
->prec
!= 0) bp
->alias
->prec
= bp
->prec
;
1840 else bp
->prec
= bp
->alias
->prec
;
1843 if (bp
->assoc
!= bp
->alias
->assoc
) {
1844 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1845 && bp
->user_token_number
== SALIAS
)
1846 warnss("conflicting assoc values for %s and %s",
1847 bp
->tag
, bp
->alias
->tag
);
1848 if (bp
->assoc
!= 0) bp
->alias
->assoc
= bp
->assoc
;
1849 else bp
->assoc
= bp
->alias
->assoc
;
1852 if (bp
->user_token_number
== SALIAS
)
1853 continue; /* do not do processing below for SALIASs */
1856 else /* bp->class == STOKEN */
1858 bp
->value
= tokno
++;
1861 if (bp
->class == STOKEN
)
1863 if (translations
&& !(bp
->user_token_number
))
1864 bp
->user_token_number
= ++last_user_token_number
;
1865 if (bp
->user_token_number
> max_user_token_number
)
1866 max_user_token_number
= bp
->user_token_number
;
1869 tags
[bp
->value
] = bp
->tag
;
1870 user_toknums
[bp
->value
] = bp
->user_token_number
;
1871 sprec
[bp
->value
] = bp
->prec
;
1872 sassoc
[bp
->value
] = bp
->assoc
;
1880 token_translations
= NEW2(max_user_token_number
+1, short);
1882 /* initialize all entries for literal tokens to 2,
1883 the internal token number for $undefined.,
1884 which represents all invalid inputs. */
1885 for (i
= 0; i
<= max_user_token_number
; i
++)
1886 token_translations
[i
] = 2;
1888 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1890 if (bp
->value
>= ntokens
) continue; /* non-terminal */
1891 if (bp
->user_token_number
== SALIAS
) continue;
1892 if (token_translations
[bp
->user_token_number
] != 2)
1893 warnsss("tokens %s and %s both assigned number %s",
1894 tags
[token_translations
[bp
->user_token_number
]],
1896 int_to_string(bp
->user_token_number
));
1897 token_translations
[bp
->user_token_number
] = bp
->value
;
1901 error_token_number
= errtoken
->value
;
1904 output_token_defines(ftable
);
1906 if (startval
->class == SUNKNOWN
)
1907 fatals("the start symbol %s is undefined", startval
->tag
);
1908 else if (startval
->class == STOKEN
)
1909 fatals("the start symbol %s is a token", startval
->tag
);
1911 start_symbol
= startval
->value
;
1915 output_token_defines(fdefines
);
1919 if (spec_name_prefix
)
1920 fprintf(fdefines
, "\nextern YYSTYPE %slval;\n", spec_name_prefix
);
1922 fprintf(fdefines
, "\nextern YYSTYPE yylval;\n");
1925 if (semantic_parser
)
1926 for (i
= ntokens
; i
< nsyms
; i
++)
1928 /* don't make these for dummy nonterminals made by gensym. */
1929 if (*tags
[i
] != '@')
1930 fprintf(fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1933 /* `fdefines' is now a temporary file, so we need to copy its
1934 contents in `done', so we can't close it here. */
1941 /* For named tokens, but not literal ones, define the name.
1942 The value is the user token number.
1945 output_token_defines(file
)
1949 register char *cp
, *symbol
;
1952 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1954 symbol
= bp
->tag
; /* get symbol */
1956 if (bp
->value
>= ntokens
) continue;
1957 if (bp
->user_token_number
== SALIAS
) continue;
1958 if ('\'' == *symbol
) continue; /* skip literal character */
1959 if (bp
== errtoken
) continue; /* skip error token */
1960 if ('\"' == *symbol
)
1962 /* use literal string only if given a symbol with an alias */
1964 symbol
= bp
->alias
->tag
;
1969 /* Don't #define nonliteral tokens whose names contain periods. */
1971 while ((c
= *cp
++) && c
!= '.');
1972 if (c
!= '\0') continue;
1974 fprintf(file
, "#define\t%s\t%d\n", symbol
,
1975 ((translations
&& ! rawtoknumflag
)
1976 ? bp
->user_token_number
1978 if (semantic_parser
)
1979 fprintf(file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1987 /* convert the rules into the representation using rrhs, rlhs and ritems. */
1992 register int itemno
;
1993 register int ruleno
;
1994 register symbol_list
*p
;
1995 /* register bucket *bp; JF unused */
1999 ritem
= NEW2(nitems
+ 1, short);
2000 rlhs
= NEW2(nrules
, short) - 1;
2001 rrhs
= NEW2(nrules
, short) - 1;
2002 rprec
= NEW2(nrules
, short) - 1;
2003 rprecsym
= NEW2(nrules
, short) - 1;
2004 rassoc
= NEW2(nrules
, short) - 1;
2012 rlhs
[ruleno
] = p
->sym
->value
;
2013 rrhs
[ruleno
] = itemno
;
2014 ruleprec
= p
->ruleprec
;
2019 ritem
[itemno
++] = p
->sym
->value
;
2020 /* A rule gets by default the precedence and associativity
2021 of the last token in it. */
2022 if (p
->sym
->class == STOKEN
)
2024 rprec
[ruleno
] = p
->sym
->prec
;
2025 rassoc
[ruleno
] = p
->sym
->assoc
;
2030 /* If this rule has a %prec,
2031 the specified symbol's precedence replaces the default. */
2034 rprec
[ruleno
] = ruleprec
->prec
;
2035 rassoc
[ruleno
] = ruleprec
->assoc
;
2036 rprecsym
[ruleno
] = ruleprec
->value
;
2039 ritem
[itemno
++] = -ruleno
;
2048 /* Read a signed integer from STREAM and return its value. */
2051 read_signed_integer (stream
)
2054 register int c
= getc(stream
);
2055 register int sign
= 1;
2066 n
= 10*n
+ (c
- '0');