]>
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(). */
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 if(ungetc(skip_white_space(), finput
) == '%')
499 if (token
== TYPENAME
)
501 k
= strlen(token_buffer
);
502 typename
= NEW2(k
+ 1, char);
503 strcpy(typename
, token_buffer
);
504 value_components_used
= 1;
507 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"'
511 symval
->class = STOKEN
;
512 symval
->type_name
= typename
;
513 symval
->user_token_number
= symbol
->user_token_number
;
514 symbol
->user_token_number
= SALIAS
;
516 symval
->alias
= symbol
;
517 symbol
->alias
= symval
;
520 nsyms
--; /* symbol and symval combined are only one symbol */
522 else if (token
== IDENTIFIER
)
524 int oldclass
= symval
->class;
527 if (symbol
->class == what_is_not
)
528 warns(_("symbol %s redefined"), symbol
->tag
);
529 symbol
->class = what_is
;
530 if (what_is
== SNTERM
&& oldclass
!= SNTERM
)
531 symbol
->value
= nvars
++;
535 if (symbol
->type_name
== NULL
)
536 symbol
->type_name
= typename
;
537 else if (strcmp(typename
, symbol
->type_name
) != 0)
538 warns(_("type redeclaration for %s"), symbol
->tag
);
541 else if (symbol
&& token
== NUMBER
)
543 symbol
->user_token_number
= numval
;
548 warnss(_("`%s' is invalid in %s"),
550 (what_is
== STOKEN
) ? "%token" : "%nterm");
557 /* parse what comes after %thong
559 %thong <type> token number literal
560 the <type> or number may be omitted. The number specifies the
563 Two symbols are entered in the table, one for the token symbol and
564 one for the literal. Both are given the <type>, if any, from the declaration.
565 The ->user_token_number of the first is SALIAS and the ->user_token_number
566 of the second is set to the number, if any, from the declaration.
567 The two symbols are linked via pointers in their ->alias fields.
569 during output_defines_table, the symbol is reported
570 thereafter, only the literal string is retained
571 it is the literal string that is output to yytname
575 parse_thong_decl (void)
578 register struct bucket
*symbol
;
579 register char *typename
= 0;
583 token
= lex(); /* fetch typename or first token */
584 if (token
== TYPENAME
) {
585 k
= strlen(token_buffer
);
586 typename
= NEW2(k
+ 1, char);
587 strcpy(typename
, token_buffer
);
588 value_components_used
= 1;
589 token
= lex(); /* fetch first token */
592 /* process first token */
594 if (token
!= IDENTIFIER
)
596 warns(_("unrecognized item %s, expected an identifier"),
601 symval
->class = STOKEN
;
602 symval
->type_name
= typename
;
603 symval
->user_token_number
= SALIAS
;
606 token
= lex(); /* get number or literal string */
608 if (token
== NUMBER
) {
610 token
= lex(); /* okay, did number, now get literal */
614 /* process literal string token */
616 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
618 warns(_("expected string constant instead of %s"),
623 symval
->class = STOKEN
;
624 symval
->type_name
= typename
;
625 symval
->user_token_number
= usrtoknum
;
627 symval
->alias
= symbol
;
628 symbol
->alias
= symval
;
630 nsyms
--; /* symbol and symval combined are only one symbol */
634 /* parse what comes after %start */
637 parse_start_decl (void)
640 warn(_("multiple %start declarations"));
641 if (lex() != IDENTIFIER
)
642 warn(_("invalid %start declaration"));
652 /* read in a %type declaration and record its information for get_type_name to access */
655 parse_type_decl (void)
660 if (lex() != TYPENAME
)
662 warn(_("%type declaration has no <typename>"));
667 k
= strlen(token_buffer
);
668 name
= NEW2(k
+ 1, char);
669 strcpy(name
, token_buffer
);
675 if(ungetc(skip_white_space(), finput
) == '%')
688 if (symval
->type_name
== NULL
)
689 symval
->type_name
= name
;
690 else if (strcmp(name
, symval
->type_name
) != 0)
691 warns(_("type redeclaration for %s"), symval
->tag
);
696 warns(_("invalid %%type declaration due to item: `%s'"), token_buffer
);
704 /* read in a %left, %right or %nonassoc declaration and record its information. */
705 /* assoc is either LEFT_ASSOC, RIGHT_ASSOC or NON_ASSOC. */
708 parse_assoc_decl (int assoc
)
711 register char *name
= NULL
;
712 register int prev
= 0;
714 lastprec
++; /* Assign a new precedence level, never 0. */
720 if(ungetc(skip_white_space(), finput
) == '%')
729 k
= strlen(token_buffer
);
730 name
= NEW2(k
+ 1, char);
731 strcpy(name
, token_buffer
);
738 if (symval
->prec
!= 0)
739 warns(_("redefining precedence of %s"), symval
->tag
);
740 symval
->prec
= lastprec
;
741 symval
->assoc
= assoc
;
742 if (symval
->class == SNTERM
)
743 warns(_("symbol %s redefined"), symval
->tag
);
744 symval
->class = STOKEN
;
746 { /* record the type, if one is specified */
747 if (symval
->type_name
== NULL
)
748 symval
->type_name
= name
;
749 else if (strcmp(name
, symval
->type_name
) != 0)
750 warns(_("type redeclaration for %s"), symval
->tag
);
755 if (prev
== IDENTIFIER
)
757 symval
->user_token_number
= numval
;
762 warns(_("invalid text (%s) - number should be after identifier"),
772 warns(_("unexpected item: %s"), token_buffer
);
783 /* copy the union declaration into fattrs (and fdefines),
784 where it is made into the
785 definition of YYSTYPE, the type of elements of the parser value stack. */
788 parse_union_decl (void)
792 register int in_comment
;
796 warn(_("multiple %union declarations"));
801 fprintf(fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
803 fprintf(fattrs
, "\n");
805 fprintf(fattrs
, "typedef union");
807 fprintf(fdefines
, "typedef union");
828 if (c
!= '*' && c
!= '/')
835 cplus_comment
= (c
== '/');
854 fatal(_("unterminated comment at end of file"));
856 if (!cplus_comment
&& c
== '*')
880 warn (_("unmatched close-brace (`}')"));
884 fprintf(fattrs
, " YYSTYPE;\n");
886 fprintf(fdefines
, " YYSTYPE;\n");
887 /* JF don't choke on trailing semi */
888 c
=skip_white_space();
889 if(c
!=';') ungetc(c
,finput
);
898 /* parse the declaration %expect N which says to expect N
899 shift-reduce conflicts. */
902 parse_expect_decl (void)
909 while (c
== ' ' || c
== '\t')
913 while (c
>= '0' && c
<= '9')
923 if (count
<= 0 || count
> 10)
924 warn(_("argument of %expect is not an integer"));
925 expected_conflicts
= atoi (buffer
);
928 /* that's all of parsing the declaration section */
930 /* Get the data type (alternative in the union) of the value for symbol n in rule rule. */
933 get_type_name (int n
, symbol_list
*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 (symbol_list
*rule
, int stack_offset
)
979 register char *type_name
;
983 /* offset is always 0 if parser has already popped the stack pointer */
984 if (semantic_parser
) stack_offset
= 0;
986 fprintf(fguard
, "\ncase %d:\n", nrules
);
988 fprintf(fguard
, "#line %d \"%s\"\n", lineno
, infile
);
994 while (brace_flag
? (count
> 0) : (c
!= ';'))
1015 warn(_("unmatched right brace (`}')"));
1016 c
= getc(finput
); /* skip it */
1029 fatal(_("unterminated string at end of file"));
1032 warn(_("unterminated string"));
1034 c
= match
; /* invent terminator */
1044 fatal(_("unterminated string"));
1059 if (c
!= '*' && c
!= '/')
1062 cplus_comment
= (c
== '/');
1069 if (!cplus_comment
&& c
== '*')
1093 fatal(_("unterminated comment"));
1109 register char *cp
= token_buffer
;
1111 while ((c
= getc(finput
)) != '>' && c
> 0)
1113 if (cp
== token_buffer
+ maxtoken
)
1114 cp
= grow_token_buffer(cp
);
1119 type_name
= token_buffer
;
1126 fprintf(fguard
, "yyval");
1127 if (!type_name
) type_name
= rule
->sym
->type_name
;
1129 fprintf(fguard
, ".%s", type_name
);
1130 if(!type_name
&& typed
)
1131 warns(_("$$ of `%s' has no declared type"), rule
->sym
->tag
);
1134 else if (isdigit(c
) || c
== '-')
1137 n
= read_signed_integer(finput
);
1140 if (!type_name
&& n
> 0)
1141 type_name
= get_type_name(n
, rule
);
1143 fprintf(fguard
, "yyvsp[%d]", n
- stack_offset
);
1145 fprintf(fguard
, ".%s", type_name
);
1146 if(!type_name
&& typed
)
1147 warnss(_("$%s of `%s' has no declared type"), int_to_string(n
), rule
->sym
->tag
);
1151 warns(_("$%s is invalid"), printable_version(c
));
1157 if (isdigit(c
) || c
== '-')
1160 n
= read_signed_integer(finput
);
1165 warns(_("@%s is invalid"), printable_version(c
));
1169 fprintf(fguard
, "yylsp[%d]", n
- stack_offset
);
1175 fatal(_("unterminated %%guard clause"));
1181 if (c
!= '}' || count
!= 0)
1185 c
= skip_white_space();
1187 fprintf(fguard
, ";\n break;}");
1189 copy_action(rule
, stack_offset
);
1192 c
= getc(finput
); /* why not skip_white_space -wjh */
1194 copy_action(rule
, stack_offset
);
1202 /* Assuming that a { has just been seen, copy everything up to the matching }
1203 into the actions file.
1204 stack_offset is the number of values in the current rule so far,
1205 which says where to find $0 with respect to the top of the stack. */
1208 copy_action (symbol_list
*rule
, int stack_offset
)
1215 register char *type_name
;
1218 /* offset is always 0 if parser has already popped the stack pointer */
1219 if (semantic_parser
) stack_offset
= 0;
1221 fprintf(faction
, "\ncase %d:\n", nrules
);
1223 fprintf(faction
, "#line %d \"%s\"\n", lineno
, infile
);
1255 warn(_("unterminated string"));
1261 fatal(_("unterminated string at end of file"));
1269 fatal(_("unterminated string"));
1284 if (c
!= '*' && c
!= '/')
1287 cplus_comment
= (c
== '/');
1294 if (!cplus_comment
&& c
== '*')
1318 fatal(_("unterminated comment"));
1334 register char *cp
= token_buffer
;
1336 while ((c
= getc(finput
)) != '>' && c
> 0)
1338 if (cp
== token_buffer
+ maxtoken
)
1339 cp
= grow_token_buffer(cp
);
1344 type_name
= token_buffer
;
1345 value_components_used
= 1;
1351 fprintf(faction
, "yyval");
1352 if (!type_name
) type_name
= get_type_name(0, rule
);
1354 fprintf(faction
, ".%s", type_name
);
1355 if(!type_name
&& typed
)
1356 warns(_("$$ of `%s' has no declared type"), rule
->sym
->tag
);
1358 else if (isdigit(c
) || c
== '-')
1361 n
= read_signed_integer(finput
);
1364 if (!type_name
&& n
> 0)
1365 type_name
= get_type_name(n
, rule
);
1367 fprintf(faction
, "yyvsp[%d]", n
- stack_offset
);
1369 fprintf(faction
, ".%s", type_name
);
1370 if(!type_name
&& typed
)
1371 warnss(_("$%s of `%s' has no declared type"),
1372 int_to_string(n
), rule
->sym
->tag
);
1376 warns(_("$%s is invalid"), printable_version(c
));
1382 if (isdigit(c
) || c
== '-')
1385 n
= read_signed_integer(finput
);
1390 warn(_("invalid @-construct"));
1394 fprintf(faction
, "yylsp[%d]", n
- stack_offset
);
1400 fatal(_("unmatched `{'"));
1409 /* above loop exits when c is '}' */
1418 fprintf(faction
, ";\n break;}");
1423 /* generate a dummy symbol, a nonterminal,
1424 whose name cannot conflict with the user's names. */
1429 register bucket
*sym
;
1431 sprintf (token_buffer
, "@%d", ++gensym_count
);
1432 sym
= getsym(token_buffer
);
1433 sym
->class = SNTERM
;
1434 sym
->value
= nvars
++;
1438 /* Parse the input grammar into a one symbol_list structure.
1439 Each rule is represented by a sequence of symbols: the left hand side
1440 followed by the contents of the right hand side, followed by a null pointer
1441 instead of a symbol to terminate the rule.
1442 The next symbol is the lhs of the following rule.
1444 All guards and actions are copied out to the appropriate files,
1445 labelled by the rule number they apply to. */
1451 register bucket
*lhs
;
1452 register symbol_list
*p
;
1453 register symbol_list
*p1
;
1454 register bucket
*bp
;
1456 symbol_list
*crule
; /* points to first symbol_list of current rule. */
1457 /* its symbol is the lhs of the rule. */
1458 symbol_list
*crule1
; /* points to the symbol_list preceding crule. */
1464 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1466 if (t
== IDENTIFIER
|| t
== BAR
)
1468 register int actionflag
= 0;
1469 int rulelength
= 0; /* number of symbols in rhs of this rule so far */
1470 int xactions
= 0; /* JF for error checking */
1471 bucket
*first_rhs
= 0;
1473 if (t
== IDENTIFIER
)
1486 warn(_("ill-formed rule: initial symbol not followed by colon"));
1491 if (nrules
== 0 && t
== BAR
)
1493 warn(_("grammar starts with vertical bar"));
1494 lhs
= symval
; /* BOGUS: use a random symval */
1496 /* start a new rule and record its lhs. */
1501 record_rule_line ();
1503 p
= NEW(symbol_list
);
1515 /* mark the rule's lhs as a nonterminal if not already so. */
1517 if (lhs
->class == SUNKNOWN
)
1519 lhs
->class = SNTERM
;
1523 else if (lhs
->class == STOKEN
)
1524 warns(_("rule given for %s, which is a token"), lhs
->tag
);
1526 /* read the rhs of the rule. */
1534 crule
->ruleprec
= symval
;
1538 if (! (t
== IDENTIFIER
|| t
== LEFT_CURLY
)) break;
1540 /* If next token is an identifier, see if a colon follows it.
1541 If one does, exit this rule now. */
1542 if (t
== IDENTIFIER
)
1544 register bucket
*ssave
;
1551 if (t1
== COLON
) break;
1553 if(!first_rhs
) /* JF */
1555 /* Not followed by colon =>
1556 process as part of this rule's rhs. */
1559 /* If we just passed an action, that action was in the middle
1560 of a rule, so make a dummy rule to reduce it to a
1564 register bucket
*sdummy
;
1566 /* Since the action was written out with this rule's */
1567 /* number, we must give the new rule this number */
1568 /* by inserting the new rule before it. */
1570 /* Make a dummy nonterminal, a gensym. */
1573 /* Make a new rule, whose body is empty,
1574 before the current one, so that the action
1575 just read can belong to it. */
1578 record_rule_line ();
1579 p
= NEW(symbol_list
);
1584 crule1
= NEW(symbol_list
);
1586 crule1
->next
= crule
;
1588 /* insert the dummy generated by that rule into this rule. */
1590 p
= NEW(symbol_list
);
1598 if (t
== IDENTIFIER
)
1601 p
= NEW(symbol_list
);
1606 else /* handle an action. */
1608 copy_action(crule
, rulelength
);
1610 xactions
++; /* JF */
1613 } /* end of read rhs of rule */
1615 /* Put an empty link in the list to mark the end of this rule */
1616 p
= NEW(symbol_list
);
1622 warn(_("two @prec's in a row"));
1624 crule
->ruleprec
= symval
;
1629 if (! semantic_parser
)
1630 warn(_("%%guard present but %%semantic_parser not specified"));
1632 copy_guard(crule
, rulelength
);
1635 else if (t
== LEFT_CURLY
)
1637 /* This case never occurs -wjh */
1638 if (actionflag
) warn(_("two actions at end of one rule"));
1639 copy_action(crule
, rulelength
);
1641 xactions
++; /* -wjh */
1644 /* If $$ is being set in default way,
1645 warn if any type mismatch. */
1646 else if (!xactions
&& first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1648 if (lhs
->type_name
== 0 || first_rhs
->type_name
== 0
1649 || strcmp(lhs
->type_name
,first_rhs
->type_name
))
1650 warnss(_("type clash (`%s' `%s') on default action"),
1651 lhs
->type_name
? lhs
->type_name
: "",
1652 first_rhs
->type_name
? first_rhs
->type_name
: "");
1654 /* Warn if there is no default for $$ but we need one. */
1655 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1656 warn(_("empty rule for typed nonterminal, and no action"));
1661 /* these things can appear as alternatives to rules. */
1663 a) none of the documentation allows them
1664 b) most of them scan forward until finding a next %
1665 thus they may swallow lots of intervening rules
1667 else if (t
== TOKEN
)
1669 parse_token_decl(STOKEN
, SNTERM
);
1672 else if (t
== NTERM
)
1674 parse_token_decl(SNTERM
, STOKEN
);
1681 else if (t
== UNION
)
1686 else if (t
== EXPECT
)
1688 parse_expect_decl();
1691 else if (t
== START
)
1700 warns(_("invalid input: %s"), token_buffer
);
1705 /* grammar has been read. Do some checking */
1707 if (nsyms
> MAXSHORT
)
1708 fatals(_("too many symbols (tokens plus nonterminals); maximum %s"),
1709 int_to_string(MAXSHORT
));
1711 fatal(_("no rules in the input grammar"));
1713 if (typed
== 0 /* JF put out same default YYSTYPE as YACC does */
1714 && !value_components_used
)
1716 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1717 but it seems better to be consistent.
1718 Most programs should declare their own type anyway. */
1719 fprintf(fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1721 fprintf(fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1724 /* Report any undefined symbols and consider them nonterminals. */
1726 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1727 if (bp
->class == SUNKNOWN
)
1729 warns(_("symbol %s is used, but is not defined as a token and has no rules"),
1732 bp
->value
= nvars
++;
1735 ntokens
= nsyms
- nvars
;
1740 record_rule_line (void)
1742 /* Record each rule's source line number in rline table. */
1744 if (nrules
>= rline_allocated
)
1746 rline_allocated
= nrules
* 2;
1747 rline
= (short *) xrealloc ((char *) rline
,
1748 rline_allocated
* sizeof (short));
1750 rline
[nrules
] = lineno
;
1754 /* read in a %type declaration and record its information for get_type_name to access */
1755 /* this is unused. it is only called from the #if 0 part of readgram */
1761 register char *name
;
1767 warn(_("ill-formed %type declaration"));
1771 k
= strlen(token_buffer
);
1772 name
= NEW2(k
+ 1, char);
1773 strcpy(name
, token_buffer
);
1788 if (symval
->type_name
== NULL
)
1789 symval
->type_name
= name
;
1790 else if (strcmp(name
, symval
->type_name
) != 0)
1791 warns(_("type redeclaration for %s"), symval
->tag
);
1803 /* assign symbol numbers, and write definition of token names into fdefines.
1804 Set up vectors tags and sprec of names and precedences of symbols. */
1809 register bucket
*bp
;
1810 register int tokno
= 1;
1812 register int last_user_token_number
;
1814 /* int lossage = 0; JF set but not used */
1816 tags
= NEW2(nsyms
+ 1, char *);
1818 user_toknums
= NEW2(nsyms
+ 1, int);
1819 user_toknums
[0] = 0;
1821 sprec
= NEW2(nsyms
, short);
1822 sassoc
= NEW2(nsyms
, short);
1824 max_user_token_number
= 256;
1825 last_user_token_number
= 256;
1827 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1829 if (bp
->class == SNTERM
)
1831 bp
->value
+= ntokens
;
1835 /* this symbol and its alias are a single token defn.
1836 allocate a tokno, and assign to both
1837 check agreement of ->prec and ->assoc fields
1838 and make both the same
1841 bp
->value
= bp
->alias
->value
= tokno
++;
1843 if (bp
->prec
!= bp
->alias
->prec
) {
1844 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1845 && bp
->user_token_number
== SALIAS
)
1846 warnss(_("conflicting precedences for %s and %s"),
1847 bp
->tag
, bp
->alias
->tag
);
1848 if (bp
->prec
!= 0) bp
->alias
->prec
= bp
->prec
;
1849 else bp
->prec
= bp
->alias
->prec
;
1852 if (bp
->assoc
!= bp
->alias
->assoc
) {
1853 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1854 && bp
->user_token_number
== SALIAS
)
1855 warnss(_("conflicting assoc values for %s and %s"),
1856 bp
->tag
, bp
->alias
->tag
);
1857 if (bp
->assoc
!= 0) bp
->alias
->assoc
= bp
->assoc
;
1858 else bp
->assoc
= bp
->alias
->assoc
;
1861 if (bp
->user_token_number
== SALIAS
)
1862 continue; /* do not do processing below for SALIASs */
1865 else /* bp->class == STOKEN */
1867 bp
->value
= tokno
++;
1870 if (bp
->class == STOKEN
)
1872 if (translations
&& !(bp
->user_token_number
))
1873 bp
->user_token_number
= ++last_user_token_number
;
1874 if (bp
->user_token_number
> max_user_token_number
)
1875 max_user_token_number
= bp
->user_token_number
;
1878 tags
[bp
->value
] = bp
->tag
;
1879 user_toknums
[bp
->value
] = bp
->user_token_number
;
1880 sprec
[bp
->value
] = bp
->prec
;
1881 sassoc
[bp
->value
] = bp
->assoc
;
1889 token_translations
= NEW2(max_user_token_number
+1, short);
1891 /* initialize all entries for literal tokens to 2,
1892 the internal token number for $undefined.,
1893 which represents all invalid inputs. */
1894 for (i
= 0; i
<= max_user_token_number
; i
++)
1895 token_translations
[i
] = 2;
1897 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1899 if (bp
->value
>= ntokens
) continue; /* non-terminal */
1900 if (bp
->user_token_number
== SALIAS
) continue;
1901 if (token_translations
[bp
->user_token_number
] != 2)
1902 warnsss(_("tokens %s and %s both assigned number %s"),
1903 tags
[token_translations
[bp
->user_token_number
]],
1905 int_to_string(bp
->user_token_number
));
1906 token_translations
[bp
->user_token_number
] = bp
->value
;
1910 error_token_number
= errtoken
->value
;
1913 output_token_defines(ftable
);
1915 if (startval
->class == SUNKNOWN
)
1916 fatals(_("the start symbol %s is undefined"), startval
->tag
);
1917 else if (startval
->class == STOKEN
)
1918 fatals(_("the start symbol %s is a token"), startval
->tag
);
1920 start_symbol
= startval
->value
;
1924 output_token_defines(fdefines
);
1928 if (spec_name_prefix
)
1929 fprintf(fdefines
, "\nextern YYSTYPE %slval;\n", spec_name_prefix
);
1931 fprintf(fdefines
, "\nextern YYSTYPE yylval;\n");
1934 if (semantic_parser
)
1935 for (i
= ntokens
; i
< nsyms
; i
++)
1937 /* don't make these for dummy nonterminals made by gensym. */
1938 if (*tags
[i
] != '@')
1939 fprintf(fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1942 /* `fdefines' is now a temporary file, so we need to copy its
1943 contents in `done', so we can't close it here. */
1950 /* For named tokens, but not literal ones, define the name.
1951 The value is the user token number.
1954 output_token_defines (FILE *file
)
1957 register char *cp
, *symbol
;
1960 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1962 symbol
= bp
->tag
; /* get symbol */
1964 if (bp
->value
>= ntokens
) continue;
1965 if (bp
->user_token_number
== SALIAS
) continue;
1966 if ('\'' == *symbol
) continue; /* skip literal character */
1967 if (bp
== errtoken
) continue; /* skip error token */
1968 if ('\"' == *symbol
)
1970 /* use literal string only if given a symbol with an alias */
1972 symbol
= bp
->alias
->tag
;
1977 /* Don't #define nonliteral tokens whose names contain periods. */
1979 while ((c
= *cp
++) && c
!= '.');
1980 if (c
!= '\0') continue;
1982 fprintf(file
, "#define\t%s\t%d\n", symbol
,
1983 ((translations
&& ! rawtoknumflag
)
1984 ? bp
->user_token_number
1986 if (semantic_parser
)
1987 fprintf(file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1995 /* convert the rules into the representation using rrhs, rlhs and ritems. */
2000 register int itemno
;
2001 register int ruleno
;
2002 register symbol_list
*p
;
2003 /* register bucket *bp; JF unused */
2007 ritem
= NEW2(nitems
+ 1, short);
2008 rlhs
= NEW2(nrules
, short) - 1;
2009 rrhs
= NEW2(nrules
, short) - 1;
2010 rprec
= NEW2(nrules
, short) - 1;
2011 rprecsym
= NEW2(nrules
, short) - 1;
2012 rassoc
= NEW2(nrules
, short) - 1;
2020 rlhs
[ruleno
] = p
->sym
->value
;
2021 rrhs
[ruleno
] = itemno
;
2022 ruleprec
= p
->ruleprec
;
2027 ritem
[itemno
++] = p
->sym
->value
;
2028 /* A rule gets by default the precedence and associativity
2029 of the last token in it. */
2030 if (p
->sym
->class == STOKEN
)
2032 rprec
[ruleno
] = p
->sym
->prec
;
2033 rassoc
[ruleno
] = p
->sym
->assoc
;
2038 /* If this rule has a %prec,
2039 the specified symbol's precedence replaces the default. */
2042 rprec
[ruleno
] = ruleprec
->prec
;
2043 rassoc
[ruleno
] = ruleprec
->assoc
;
2044 rprecsym
[ruleno
] = ruleprec
->value
;
2047 ritem
[itemno
++] = -ruleno
;
2056 /* Read a signed integer from STREAM and return its value. */
2059 read_signed_integer (FILE *stream
)
2061 register int c
= getc(stream
);
2062 register int sign
= 1;
2073 n
= 10*n
+ (c
- '0');