]>
git.saurik.com Git - bison.git/blob - src/reader.c
8db4183192ac2ee1ea04f780c6ff234ab51a6570
1 /* Input parser for bison
2 Copyright (C) 1984, 1986, 1989, 1992, 1998 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(). */
37 #define LTYPESTR "\n#ifndef YYLTYPE\ntypedef\n struct yyltype\n\
38 {\n int timestamp;\n int first_line;\n int first_column;\
39 \n int last_line;\n int last_column;\n char *text;\n }\n\
40 yyltype;\n\n#define YYLTYPE yyltype\n#endif\n\n"
42 /* Number of slots allocated (but not necessarily used yet) in `rline' */
45 extern char *program_name
;
46 extern int definesflag
;
47 extern int nolinesflag
;
48 extern int noparserflag
;
49 extern int rawtoknumflag
;
50 extern bucket
*symval
;
52 extern int expected_conflicts
;
53 extern char *token_buffer
;
56 extern void init_lex
PARAMS((void));
57 extern char *grow_token_buffer
PARAMS((char *));
58 extern void tabinit
PARAMS((void));
59 extern void output_headers
PARAMS((void));
60 extern void output_trailers
PARAMS((void));
61 extern void free_symtab
PARAMS((void));
62 extern void open_extra_files
PARAMS((void));
63 extern char *int_to_string
PARAMS((int));
64 extern char *printable_version
PARAMS((int));
65 extern void fatal
PARAMS((char *));
66 extern void fatals
PARAMS((char *, char *));
67 extern void warn
PARAMS((char *));
68 extern void warni
PARAMS((char *, int));
69 extern void warns
PARAMS((char *, char *));
70 extern void warnss
PARAMS((char *, char *, char *));
71 extern void warnsss
PARAMS((char *, char *, char *, char *));
72 extern void unlex
PARAMS((int));
73 extern void done
PARAMS((int));
75 extern int skip_white_space
PARAMS((void));
76 extern int parse_percent_token
PARAMS((void));
77 extern int lex
PARAMS((void));
82 struct symbol_list
*next
;
89 void reader
PARAMS((void));
90 void reader_output_yylsp
PARAMS((FILE *));
91 void read_declarations
PARAMS((void));
92 void copy_definition
PARAMS((void));
93 void parse_token_decl
PARAMS((int, int));
94 void parse_start_decl
PARAMS((void));
95 void parse_type_decl
PARAMS((void));
96 void parse_assoc_decl
PARAMS((int));
97 void parse_union_decl
PARAMS((void));
98 void parse_expect_decl
PARAMS((void));
99 char *get_type_name
PARAMS((int, symbol_list
*));
100 void copy_guard
PARAMS((symbol_list
*, int));
101 void parse_thong_decl
PARAMS((void));
102 void copy_action
PARAMS((symbol_list
*, int));
103 bucket
*gensym
PARAMS((void));
104 void readgram
PARAMS((void));
105 void record_rule_line
PARAMS((void));
106 void packsymbols
PARAMS((void));
107 void output_token_defines
PARAMS((FILE *));
108 void packgram
PARAMS((void));
109 int read_signed_integer
PARAMS((FILE *));
110 static int get_type
PARAMS((void));
114 symbol_list
*grammar
;
120 /* Nonzero if components of semantic values are used, implying
121 they must be unions. */
122 static int value_components_used
;
124 static int typed
; /* nonzero if %union has been seen. */
126 static int lastprec
; /* incremented for each %left, %right or %nonassoc seen */
128 static int gensym_count
; /* incremented for each generated symbol */
130 static bucket
*errtoken
;
131 static bucket
*undeftoken
;
133 /* Nonzero if any action or guard uses the @n construct. */
134 static int yylsp_needed
;
136 extern char *version_string
;
140 skip_to_char (int target
)
144 warn(_(" Skipping to next \\n"));
146 warni(_(" Skipping to next %c"), target
);
149 c
= skip_white_space();
150 while (c
!= target
&& c
!= EOF
);
160 startval
= NULL
; /* start symbol not specified yet. */
163 translations
= 0; /* initially assume token number translation not needed. */
165 /* Nowadays translations is always set to 1,
166 since we give `error' a user-token-number
167 to satisfy the Posix demand for YYERRCODE==256. */
174 rline_allocated
= 10;
175 rline
= NEW2(rline_allocated
, short);
191 /* initialize the symbol table. */
193 /* construct the error token */
194 errtoken
= getsym("error");
195 errtoken
->class = STOKEN
;
196 errtoken
->user_token_number
= 256; /* Value specified by posix. */
197 /* construct a token that represents all undefined literal tokens. */
198 /* it is always token number 2. */
199 undeftoken
= getsym("$undefined.");
200 undeftoken
->class = STOKEN
;
201 undeftoken
->user_token_number
= 2;
202 /* Read the declaration section. Copy %{ ... %} groups to ftable and fdefines file.
203 Also notice any %token, %left, etc. found there. */
205 fprintf(ftable
, "\n/* Bison-generated parse tables, made from %s\n",
208 fprintf(ftable
, "\n/* A Bison parser, made from %s\n", infile
);
209 fprintf(ftable
, " by %s */\n\n", version_string
);
210 fprintf(ftable
, "#define YYBISON 1 /* Identify Bison output. */\n\n");
212 /* start writing the guard and action files, if they are needed. */
214 /* read in the grammar, build grammar in list form. write out guards and actions. */
216 /* Now we know whether we need the line-number stack.
217 If we do, write its type into the .tab.h file. */
219 reader_output_yylsp(fdefines
);
220 /* write closing delimiters for actions and guards. */
223 fprintf(ftable
, "#define YYLSP_NEEDED\n\n");
224 /* assign the symbols their symbol numbers.
225 Write #defines for the token symbols into fdefines if requested. */
227 /* convert the grammar into the format described in gram.h. */
229 /* free the symbol table data structure
230 since symbols are now all referred to by symbol number. */
235 reader_output_yylsp (FILE *f
)
238 fprintf(f
, LTYPESTR
);
241 /* read from finput until %% is seen. Discard the %%.
242 Handle any % declarations,
243 and copy the contents of any %{ ... %} groups to fattrs. */
246 read_declarations (void)
253 c
= skip_white_space();
257 tok
= parse_percent_token();
264 case PERCENT_LEFT_CURLY
:
269 parse_token_decl (STOKEN
, SNTERM
);
273 parse_token_decl (SNTERM
, STOKEN
);
295 parse_assoc_decl(LEFT_ASSOC
);
299 parse_assoc_decl(RIGHT_ASSOC
);
303 parse_assoc_decl(NON_ASSOC
);
306 case SEMANTIC_PARSER
:
307 if (semantic_parser
== 0)
322 warns(_("unrecognized: %s"), token_buffer
);
327 fatal(_("no input grammar"));
331 sprintf(buff
, _("unknown character: %s"), printable_version(c
));
339 /* copy the contents of a %{ ... %} into the definitions file.
340 The %{ has already been read. Return after reading the %}. */
343 copy_definition (void)
348 register int after_percent
; /* -1 while reading a character if prev char was % */
352 fprintf(fattrs
, "#line %d \"%s\"\n", lineno
, infile
);
380 fatal(_("unterminated string at end of file"));
383 warn(_("unterminated string"));
395 fatal(_("unterminated string at end of file"));
410 if (c
!= '*' && c
!= '/')
413 cplus_comment
= (c
== '/');
420 if (!cplus_comment
&& c
== '*')
444 fatal(_("unterminated comment in `%{' definition"));
455 fatal(_("unterminated `%{' definition"));
477 /* parse what comes after %token or %nterm.
478 For %token, what_is is STOKEN and what_is_not is SNTERM.
479 For %nterm, the arguments are reversed. */
482 parse_token_decl (int what_is
, int what_is_not
)
484 register int token
= 0;
485 register char *typename
= 0;
486 register struct bucket
*symbol
= NULL
; /* pts to symbol being defined */
491 int tmp_char
= ungetc (skip_white_space (), finput
);
496 fatals ("Premature EOF after %s", token_buffer
);
504 if (token
== TYPENAME
)
506 k
= strlen(token_buffer
);
507 typename
= NEW2(k
+ 1, char);
508 strcpy(typename
, token_buffer
);
509 value_components_used
= 1;
512 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"'
516 symval
->class = STOKEN
;
517 symval
->type_name
= typename
;
518 symval
->user_token_number
= symbol
->user_token_number
;
519 symbol
->user_token_number
= SALIAS
;
521 symval
->alias
= symbol
;
522 symbol
->alias
= symval
;
525 nsyms
--; /* symbol and symval combined are only one symbol */
527 else if (token
== IDENTIFIER
)
529 int oldclass
= symval
->class;
532 if (symbol
->class == what_is_not
)
533 warns(_("symbol %s redefined"), symbol
->tag
);
534 symbol
->class = what_is
;
535 if (what_is
== SNTERM
&& oldclass
!= SNTERM
)
536 symbol
->value
= nvars
++;
540 if (symbol
->type_name
== NULL
)
541 symbol
->type_name
= typename
;
542 else if (strcmp(typename
, symbol
->type_name
) != 0)
543 warns(_("type redeclaration for %s"), symbol
->tag
);
546 else if (symbol
&& token
== NUMBER
)
548 symbol
->user_token_number
= numval
;
553 warnss(_("`%s' is invalid in %s"),
555 (what_is
== STOKEN
) ? "%token" : "%nterm");
562 /* parse what comes after %thong
564 %thong <type> token number literal
565 the <type> or number may be omitted. The number specifies the
568 Two symbols are entered in the table, one for the token symbol and
569 one for the literal. Both are given the <type>, if any, from the declaration.
570 The ->user_token_number of the first is SALIAS and the ->user_token_number
571 of the second is set to the number, if any, from the declaration.
572 The two symbols are linked via pointers in their ->alias fields.
574 during output_defines_table, the symbol is reported
575 thereafter, only the literal string is retained
576 it is the literal string that is output to yytname
580 parse_thong_decl (void)
583 register struct bucket
*symbol
;
584 register char *typename
= 0;
588 token
= lex(); /* fetch typename or first token */
589 if (token
== TYPENAME
) {
590 k
= strlen(token_buffer
);
591 typename
= NEW2(k
+ 1, char);
592 strcpy(typename
, token_buffer
);
593 value_components_used
= 1;
594 token
= lex(); /* fetch first token */
597 /* process first token */
599 if (token
!= IDENTIFIER
)
601 warns(_("unrecognized item %s, expected an identifier"),
606 symval
->class = STOKEN
;
607 symval
->type_name
= typename
;
608 symval
->user_token_number
= SALIAS
;
611 token
= lex(); /* get number or literal string */
613 if (token
== NUMBER
) {
615 token
= lex(); /* okay, did number, now get literal */
619 /* process literal string token */
621 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
623 warns(_("expected string constant instead of %s"),
628 symval
->class = STOKEN
;
629 symval
->type_name
= typename
;
630 symval
->user_token_number
= usrtoknum
;
632 symval
->alias
= symbol
;
633 symbol
->alias
= symval
;
635 nsyms
--; /* symbol and symval combined are only one symbol */
639 /* parse what comes after %start */
642 parse_start_decl (void)
645 warn(_("multiple %start declarations"));
646 if (lex() != IDENTIFIER
)
647 warn(_("invalid %start declaration"));
657 /* read in a %type declaration and record its information for get_type_name to access */
660 parse_type_decl (void)
665 if (lex() != TYPENAME
)
667 warn(_("%type declaration has no <typename>"));
672 k
= strlen(token_buffer
);
673 name
= NEW2(k
+ 1, char);
674 strcpy(name
, token_buffer
);
679 int tmp_char
= ungetc (skip_white_space (), finput
);
684 fatals ("Premature EOF after %s", token_buffer
);
696 if (symval
->type_name
== NULL
)
697 symval
->type_name
= name
;
698 else if (strcmp(name
, symval
->type_name
) != 0)
699 warns(_("type redeclaration for %s"), symval
->tag
);
704 warns(_("invalid %%type declaration due to item: `%s'"), token_buffer
);
712 /* read in a %left, %right or %nonassoc declaration and record its information. */
713 /* assoc is either LEFT_ASSOC, RIGHT_ASSOC or NON_ASSOC. */
716 parse_assoc_decl (int assoc
)
719 register char *name
= NULL
;
720 register int prev
= 0;
722 lastprec
++; /* Assign a new precedence level, never 0. */
727 int tmp_char
= ungetc (skip_white_space (), finput
);
732 fatals ("Premature EOF after %s", token_buffer
);
740 k
= strlen(token_buffer
);
741 name
= NEW2(k
+ 1, char);
742 strcpy(name
, token_buffer
);
749 if (symval
->prec
!= 0)
750 warns(_("redefining precedence of %s"), symval
->tag
);
751 symval
->prec
= lastprec
;
752 symval
->assoc
= assoc
;
753 if (symval
->class == SNTERM
)
754 warns(_("symbol %s redefined"), symval
->tag
);
755 symval
->class = STOKEN
;
757 { /* record the type, if one is specified */
758 if (symval
->type_name
== NULL
)
759 symval
->type_name
= name
;
760 else if (strcmp(name
, symval
->type_name
) != 0)
761 warns(_("type redeclaration for %s"), symval
->tag
);
766 if (prev
== IDENTIFIER
)
768 symval
->user_token_number
= numval
;
773 warns(_("invalid text (%s) - number should be after identifier"),
783 warns(_("unexpected item: %s"), token_buffer
);
794 /* copy the union declaration into fattrs (and fdefines),
795 where it is made into the
796 definition of YYSTYPE, the type of elements of the parser value stack. */
799 parse_union_decl (void)
803 register int in_comment
;
807 warn(_("multiple %union declarations"));
812 fprintf(fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
814 fprintf(fattrs
, "\n");
816 fprintf(fattrs
, "typedef union");
818 fprintf(fdefines
, "typedef union");
839 if (c
!= '*' && c
!= '/')
846 cplus_comment
= (c
== '/');
865 fatal(_("unterminated comment at end of file"));
867 if (!cplus_comment
&& c
== '*')
891 warn (_("unmatched close-brace (`}')"));
895 fprintf(fattrs
, " YYSTYPE;\n");
897 fprintf(fdefines
, " YYSTYPE;\n");
898 /* JF don't choke on trailing semi */
899 c
=skip_white_space();
900 if(c
!=';') ungetc(c
,finput
);
909 /* parse the declaration %expect N which says to expect N
910 shift-reduce conflicts. */
913 parse_expect_decl (void)
920 while (c
== ' ' || c
== '\t')
924 while (c
>= '0' && c
<= '9')
934 if (count
<= 0 || count
> 10)
935 warn(_("argument of %expect is not an integer"));
936 expected_conflicts
= atoi (buffer
);
939 /* that's all of parsing the declaration section */
941 /* Get the data type (alternative in the union) of the value for symbol n in rule rule. */
944 get_type_name (int n
, symbol_list
*rule
)
946 static char *msg
= N_("invalid $ value");
949 register symbol_list
*rp
;
963 if (rp
== NULL
|| rp
->sym
== NULL
)
971 return (rp
->sym
->type_name
);
975 /* after %guard is seen in the input file,
976 copy the actual guard into the guards file.
977 If the guard is followed by an action, copy that into the actions file.
978 stack_offset is the number of values in the current rule so far,
979 which says where to find $0 with respect to the top of the stack,
980 for the simple parser in which the stack is not popped until after the guard is run. */
983 copy_guard (symbol_list
*rule
, int stack_offset
)
990 register char *type_name
;
994 /* offset is always 0 if parser has already popped the stack pointer */
995 if (semantic_parser
) stack_offset
= 0;
997 fprintf(fguard
, "\ncase %d:\n", nrules
);
999 fprintf(fguard
, "#line %d \"%s\"\n", lineno
, infile
);
1005 while (brace_flag
? (count
> 0) : (c
!= ';'))
1026 warn(_("unmatched right brace (`}')"));
1027 c
= getc(finput
); /* skip it */
1040 fatal(_("unterminated string at end of file"));
1043 warn(_("unterminated string"));
1045 c
= match
; /* invent terminator */
1055 fatal(_("unterminated string"));
1070 if (c
!= '*' && c
!= '/')
1073 cplus_comment
= (c
== '/');
1080 if (!cplus_comment
&& c
== '*')
1104 fatal(_("unterminated comment"));
1120 register char *cp
= token_buffer
;
1122 while ((c
= getc(finput
)) != '>' && c
> 0)
1124 if (cp
== token_buffer
+ maxtoken
)
1125 cp
= grow_token_buffer(cp
);
1130 type_name
= token_buffer
;
1137 fprintf(fguard
, "yyval");
1138 if (!type_name
) type_name
= rule
->sym
->type_name
;
1140 fprintf(fguard
, ".%s", type_name
);
1141 if(!type_name
&& typed
)
1142 warns(_("$$ of `%s' has no declared type"), rule
->sym
->tag
);
1145 else if (isdigit(c
) || c
== '-')
1148 n
= read_signed_integer(finput
);
1151 if (!type_name
&& n
> 0)
1152 type_name
= get_type_name(n
, rule
);
1154 fprintf(fguard
, "yyvsp[%d]", n
- stack_offset
);
1156 fprintf(fguard
, ".%s", type_name
);
1157 if(!type_name
&& typed
)
1158 warnss(_("$%s of `%s' has no declared type"), int_to_string(n
), rule
->sym
->tag
);
1162 warns(_("$%s is invalid"), printable_version(c
));
1168 if (isdigit(c
) || c
== '-')
1171 n
= read_signed_integer(finput
);
1176 warns(_("@%s is invalid"), printable_version(c
));
1180 fprintf(fguard
, "yylsp[%d]", n
- stack_offset
);
1186 fatal(_("unterminated %%guard clause"));
1192 if (c
!= '}' || count
!= 0)
1196 c
= skip_white_space();
1198 fprintf(fguard
, ";\n break;}");
1200 copy_action(rule
, stack_offset
);
1203 c
= getc(finput
); /* why not skip_white_space -wjh */
1205 copy_action(rule
, stack_offset
);
1213 /* Assuming that a { has just been seen, copy everything up to the matching }
1214 into the actions file.
1215 stack_offset is the number of values in the current rule so far,
1216 which says where to find $0 with respect to the top of the stack. */
1219 copy_action (symbol_list
*rule
, int stack_offset
)
1226 register char *type_name
;
1229 /* offset is always 0 if parser has already popped the stack pointer */
1230 if (semantic_parser
) stack_offset
= 0;
1232 fprintf(faction
, "\ncase %d:\n", nrules
);
1234 fprintf(faction
, "#line %d \"%s\"\n", lineno
, infile
);
1266 warn(_("unterminated string"));
1272 fatal(_("unterminated string at end of file"));
1280 fatal(_("unterminated string"));
1295 if (c
!= '*' && c
!= '/')
1298 cplus_comment
= (c
== '/');
1305 if (!cplus_comment
&& c
== '*')
1329 fatal(_("unterminated comment"));
1345 register char *cp
= token_buffer
;
1347 while ((c
= getc(finput
)) != '>' && c
> 0)
1349 if (cp
== token_buffer
+ maxtoken
)
1350 cp
= grow_token_buffer(cp
);
1355 type_name
= token_buffer
;
1356 value_components_used
= 1;
1362 fprintf(faction
, "yyval");
1363 if (!type_name
) type_name
= get_type_name(0, rule
);
1365 fprintf(faction
, ".%s", type_name
);
1366 if(!type_name
&& typed
)
1367 warns(_("$$ of `%s' has no declared type"), rule
->sym
->tag
);
1369 else if (isdigit(c
) || c
== '-')
1372 n
= read_signed_integer(finput
);
1375 if (!type_name
&& n
> 0)
1376 type_name
= get_type_name(n
, rule
);
1378 fprintf(faction
, "yyvsp[%d]", n
- stack_offset
);
1380 fprintf(faction
, ".%s", type_name
);
1381 if(!type_name
&& typed
)
1382 warnss(_("$%s of `%s' has no declared type"),
1383 int_to_string(n
), rule
->sym
->tag
);
1387 warns(_("$%s is invalid"), printable_version(c
));
1393 if (isdigit(c
) || c
== '-')
1396 n
= read_signed_integer(finput
);
1401 warn(_("invalid @-construct"));
1405 fprintf(faction
, "yylsp[%d]", n
- stack_offset
);
1411 fatal(_("unmatched `{'"));
1420 /* above loop exits when c is '}' */
1429 fprintf(faction
, ";\n break;}");
1434 /* generate a dummy symbol, a nonterminal,
1435 whose name cannot conflict with the user's names. */
1440 register bucket
*sym
;
1442 sprintf (token_buffer
, "@%d", ++gensym_count
);
1443 sym
= getsym(token_buffer
);
1444 sym
->class = SNTERM
;
1445 sym
->value
= nvars
++;
1449 /* Parse the input grammar into a one symbol_list structure.
1450 Each rule is represented by a sequence of symbols: the left hand side
1451 followed by the contents of the right hand side, followed by a null pointer
1452 instead of a symbol to terminate the rule.
1453 The next symbol is the lhs of the following rule.
1455 All guards and actions are copied out to the appropriate files,
1456 labelled by the rule number they apply to. */
1462 register bucket
*lhs
;
1463 register symbol_list
*p
;
1464 register symbol_list
*p1
;
1465 register bucket
*bp
;
1467 symbol_list
*crule
; /* points to first symbol_list of current rule. */
1468 /* its symbol is the lhs of the rule. */
1469 symbol_list
*crule1
; /* points to the symbol_list preceding crule. */
1475 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1477 if (t
== IDENTIFIER
|| t
== BAR
)
1479 register int actionflag
= 0;
1480 int rulelength
= 0; /* number of symbols in rhs of this rule so far */
1481 int xactions
= 0; /* JF for error checking */
1482 bucket
*first_rhs
= 0;
1484 if (t
== IDENTIFIER
)
1497 warn(_("ill-formed rule: initial symbol not followed by colon"));
1502 if (nrules
== 0 && t
== BAR
)
1504 warn(_("grammar starts with vertical bar"));
1505 lhs
= symval
; /* BOGUS: use a random symval */
1507 /* start a new rule and record its lhs. */
1512 record_rule_line ();
1514 p
= NEW(symbol_list
);
1526 /* mark the rule's lhs as a nonterminal if not already so. */
1528 if (lhs
->class == SUNKNOWN
)
1530 lhs
->class = SNTERM
;
1534 else if (lhs
->class == STOKEN
)
1535 warns(_("rule given for %s, which is a token"), lhs
->tag
);
1537 /* read the rhs of the rule. */
1545 crule
->ruleprec
= symval
;
1549 if (! (t
== IDENTIFIER
|| t
== LEFT_CURLY
)) break;
1551 /* If next token is an identifier, see if a colon follows it.
1552 If one does, exit this rule now. */
1553 if (t
== IDENTIFIER
)
1555 register bucket
*ssave
;
1562 if (t1
== COLON
) break;
1564 if(!first_rhs
) /* JF */
1566 /* Not followed by colon =>
1567 process as part of this rule's rhs. */
1570 /* If we just passed an action, that action was in the middle
1571 of a rule, so make a dummy rule to reduce it to a
1575 register bucket
*sdummy
;
1577 /* Since the action was written out with this rule's */
1578 /* number, we must give the new rule this number */
1579 /* by inserting the new rule before it. */
1581 /* Make a dummy nonterminal, a gensym. */
1584 /* Make a new rule, whose body is empty,
1585 before the current one, so that the action
1586 just read can belong to it. */
1589 record_rule_line ();
1590 p
= NEW(symbol_list
);
1595 crule1
= NEW(symbol_list
);
1597 crule1
->next
= crule
;
1599 /* insert the dummy generated by that rule into this rule. */
1601 p
= NEW(symbol_list
);
1609 if (t
== IDENTIFIER
)
1612 p
= NEW(symbol_list
);
1617 else /* handle an action. */
1619 copy_action(crule
, rulelength
);
1621 xactions
++; /* JF */
1624 } /* end of read rhs of rule */
1626 /* Put an empty link in the list to mark the end of this rule */
1627 p
= NEW(symbol_list
);
1633 warn(_("two @prec's in a row"));
1635 crule
->ruleprec
= symval
;
1640 if (! semantic_parser
)
1641 warn(_("%%guard present but %%semantic_parser not specified"));
1643 copy_guard(crule
, rulelength
);
1646 else if (t
== LEFT_CURLY
)
1648 /* This case never occurs -wjh */
1649 if (actionflag
) warn(_("two actions at end of one rule"));
1650 copy_action(crule
, rulelength
);
1652 xactions
++; /* -wjh */
1655 /* If $$ is being set in default way,
1656 warn if any type mismatch. */
1657 else if (!xactions
&& first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1659 if (lhs
->type_name
== 0 || first_rhs
->type_name
== 0
1660 || strcmp(lhs
->type_name
,first_rhs
->type_name
))
1661 warnss(_("type clash (`%s' `%s') on default action"),
1662 lhs
->type_name
? lhs
->type_name
: "",
1663 first_rhs
->type_name
? first_rhs
->type_name
: "");
1665 /* Warn if there is no default for $$ but we need one. */
1666 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1667 warn(_("empty rule for typed nonterminal, and no action"));
1672 /* these things can appear as alternatives to rules. */
1674 a) none of the documentation allows them
1675 b) most of them scan forward until finding a next %
1676 thus they may swallow lots of intervening rules
1678 else if (t
== TOKEN
)
1680 parse_token_decl(STOKEN
, SNTERM
);
1683 else if (t
== NTERM
)
1685 parse_token_decl(SNTERM
, STOKEN
);
1692 else if (t
== UNION
)
1697 else if (t
== EXPECT
)
1699 parse_expect_decl();
1702 else if (t
== START
)
1711 warns(_("invalid input: %s"), token_buffer
);
1716 /* grammar has been read. Do some checking */
1718 if (nsyms
> MAXSHORT
)
1719 fatals(_("too many symbols (tokens plus nonterminals); maximum %s"),
1720 int_to_string(MAXSHORT
));
1722 fatal(_("no rules in the input grammar"));
1724 if (typed
== 0 /* JF put out same default YYSTYPE as YACC does */
1725 && !value_components_used
)
1727 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1728 but it seems better to be consistent.
1729 Most programs should declare their own type anyway. */
1730 fprintf(fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1732 fprintf(fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1735 /* Report any undefined symbols and consider them nonterminals. */
1737 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1738 if (bp
->class == SUNKNOWN
)
1740 warns(_("symbol %s is used, but is not defined as a token and has no rules"),
1743 bp
->value
= nvars
++;
1746 ntokens
= nsyms
- nvars
;
1751 record_rule_line (void)
1753 /* Record each rule's source line number in rline table. */
1755 if (nrules
>= rline_allocated
)
1757 rline_allocated
= nrules
* 2;
1758 rline
= (short *) xrealloc ((char *) rline
,
1759 rline_allocated
* sizeof (short));
1761 rline
[nrules
] = lineno
;
1765 /* read in a %type declaration and record its information for get_type_name to access */
1766 /* this is unused. it is only called from the #if 0 part of readgram */
1772 register char *name
;
1778 warn(_("ill-formed %type declaration"));
1782 k
= strlen(token_buffer
);
1783 name
= NEW2(k
+ 1, char);
1784 strcpy(name
, token_buffer
);
1799 if (symval
->type_name
== NULL
)
1800 symval
->type_name
= name
;
1801 else if (strcmp(name
, symval
->type_name
) != 0)
1802 warns(_("type redeclaration for %s"), symval
->tag
);
1814 /* assign symbol numbers, and write definition of token names into fdefines.
1815 Set up vectors tags and sprec of names and precedences of symbols. */
1820 register bucket
*bp
;
1821 register int tokno
= 1;
1823 register int last_user_token_number
;
1825 /* int lossage = 0; JF set but not used */
1827 tags
= NEW2(nsyms
+ 1, char *);
1829 user_toknums
= NEW2(nsyms
+ 1, int);
1830 user_toknums
[0] = 0;
1832 sprec
= NEW2(nsyms
, short);
1833 sassoc
= NEW2(nsyms
, short);
1835 max_user_token_number
= 256;
1836 last_user_token_number
= 256;
1838 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1840 if (bp
->class == SNTERM
)
1842 bp
->value
+= ntokens
;
1846 /* this symbol and its alias are a single token defn.
1847 allocate a tokno, and assign to both
1848 check agreement of ->prec and ->assoc fields
1849 and make both the same
1852 bp
->value
= bp
->alias
->value
= tokno
++;
1854 if (bp
->prec
!= bp
->alias
->prec
) {
1855 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1856 && bp
->user_token_number
== SALIAS
)
1857 warnss(_("conflicting precedences for %s and %s"),
1858 bp
->tag
, bp
->alias
->tag
);
1859 if (bp
->prec
!= 0) bp
->alias
->prec
= bp
->prec
;
1860 else bp
->prec
= bp
->alias
->prec
;
1863 if (bp
->assoc
!= bp
->alias
->assoc
) {
1864 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1865 && bp
->user_token_number
== SALIAS
)
1866 warnss(_("conflicting assoc values for %s and %s"),
1867 bp
->tag
, bp
->alias
->tag
);
1868 if (bp
->assoc
!= 0) bp
->alias
->assoc
= bp
->assoc
;
1869 else bp
->assoc
= bp
->alias
->assoc
;
1872 if (bp
->user_token_number
== SALIAS
)
1873 continue; /* do not do processing below for SALIASs */
1876 else /* bp->class == STOKEN */
1878 bp
->value
= tokno
++;
1881 if (bp
->class == STOKEN
)
1883 if (translations
&& !(bp
->user_token_number
))
1884 bp
->user_token_number
= ++last_user_token_number
;
1885 if (bp
->user_token_number
> max_user_token_number
)
1886 max_user_token_number
= bp
->user_token_number
;
1889 tags
[bp
->value
] = bp
->tag
;
1890 user_toknums
[bp
->value
] = bp
->user_token_number
;
1891 sprec
[bp
->value
] = bp
->prec
;
1892 sassoc
[bp
->value
] = bp
->assoc
;
1900 token_translations
= NEW2(max_user_token_number
+1, short);
1902 /* initialize all entries for literal tokens to 2,
1903 the internal token number for $undefined.,
1904 which represents all invalid inputs. */
1905 for (i
= 0; i
<= max_user_token_number
; i
++)
1906 token_translations
[i
] = 2;
1908 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1910 if (bp
->value
>= ntokens
) continue; /* non-terminal */
1911 if (bp
->user_token_number
== SALIAS
) continue;
1912 if (token_translations
[bp
->user_token_number
] != 2)
1913 warnsss(_("tokens %s and %s both assigned number %s"),
1914 tags
[token_translations
[bp
->user_token_number
]],
1916 int_to_string(bp
->user_token_number
));
1917 token_translations
[bp
->user_token_number
] = bp
->value
;
1921 error_token_number
= errtoken
->value
;
1924 output_token_defines(ftable
);
1926 if (startval
->class == SUNKNOWN
)
1927 fatals(_("the start symbol %s is undefined"), startval
->tag
);
1928 else if (startval
->class == STOKEN
)
1929 fatals(_("the start symbol %s is a token"), startval
->tag
);
1931 start_symbol
= startval
->value
;
1935 output_token_defines(fdefines
);
1939 if (spec_name_prefix
)
1940 fprintf(fdefines
, "\nextern YYSTYPE %slval;\n", spec_name_prefix
);
1942 fprintf(fdefines
, "\nextern YYSTYPE yylval;\n");
1945 if (semantic_parser
)
1946 for (i
= ntokens
; i
< nsyms
; i
++)
1948 /* don't make these for dummy nonterminals made by gensym. */
1949 if (*tags
[i
] != '@')
1950 fprintf(fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1953 /* `fdefines' is now a temporary file, so we need to copy its
1954 contents in `done', so we can't close it here. */
1961 /* For named tokens, but not literal ones, define the name.
1962 The value is the user token number.
1965 output_token_defines (FILE *file
)
1968 register char *cp
, *symbol
;
1971 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1973 symbol
= bp
->tag
; /* get symbol */
1975 if (bp
->value
>= ntokens
) continue;
1976 if (bp
->user_token_number
== SALIAS
) continue;
1977 if ('\'' == *symbol
) continue; /* skip literal character */
1978 if (bp
== errtoken
) continue; /* skip error token */
1979 if ('\"' == *symbol
)
1981 /* use literal string only if given a symbol with an alias */
1983 symbol
= bp
->alias
->tag
;
1988 /* Don't #define nonliteral tokens whose names contain periods. */
1990 while ((c
= *cp
++) && c
!= '.');
1991 if (c
!= '\0') continue;
1993 fprintf(file
, "#define\t%s\t%d\n", symbol
,
1994 ((translations
&& ! rawtoknumflag
)
1995 ? bp
->user_token_number
1997 if (semantic_parser
)
1998 fprintf(file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
2006 /* convert the rules into the representation using rrhs, rlhs and ritems. */
2011 register int itemno
;
2012 register int ruleno
;
2013 register symbol_list
*p
;
2014 /* register bucket *bp; JF unused */
2018 ritem
= NEW2(nitems
+ 1, short);
2019 rlhs
= NEW2(nrules
, short) - 1;
2020 rrhs
= NEW2(nrules
, short) - 1;
2021 rprec
= NEW2(nrules
, short) - 1;
2022 rprecsym
= NEW2(nrules
, short) - 1;
2023 rassoc
= NEW2(nrules
, short) - 1;
2031 rlhs
[ruleno
] = p
->sym
->value
;
2032 rrhs
[ruleno
] = itemno
;
2033 ruleprec
= p
->ruleprec
;
2038 ritem
[itemno
++] = p
->sym
->value
;
2039 /* A rule gets by default the precedence and associativity
2040 of the last token in it. */
2041 if (p
->sym
->class == STOKEN
)
2043 rprec
[ruleno
] = p
->sym
->prec
;
2044 rassoc
[ruleno
] = p
->sym
->assoc
;
2049 /* If this rule has a %prec,
2050 the specified symbol's precedence replaces the default. */
2053 rprec
[ruleno
] = ruleprec
->prec
;
2054 rassoc
[ruleno
] = ruleprec
->assoc
;
2055 rprecsym
[ruleno
] = ruleprec
->value
;
2058 ritem
[itemno
++] = -ruleno
;
2067 /* Read a signed integer from STREAM and return its value. */
2070 read_signed_integer (FILE *stream
)
2072 register int c
= getc(stream
);
2073 register int sign
= 1;
2084 n
= 10*n
+ (c
- '0');