]>
git.saurik.com Git - bison.git/blob - src/reader.c
33332b59f9fba7365a0980de18abd8d050a1bf79
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, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* read in the grammar specification and record it in the format described in gram.h.
23 All guards are copied into the fguard file and all actions into faction,
24 in each case forming the body of a C function (yyguard or yyaction)
25 which contains a switch statement to decide which guard or action to execute.
27 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
;
57 extern void init_lex
PARAMS((void));
58 extern char *grow_token_buffer
PARAMS((char *));
59 extern void tabinit
PARAMS((void));
60 extern void output_headers
PARAMS((void));
61 extern void output_trailers
PARAMS((void));
62 extern void free_symtab
PARAMS((void));
63 extern void open_extra_files
PARAMS((void));
64 extern char *int_to_string
PARAMS((int));
65 extern char *printable_version
PARAMS((int));
66 extern void fatal
PARAMS((char *));
67 extern void fatals
PARAMS((char *, char *));
68 extern void warn
PARAMS((char *));
69 extern void warni
PARAMS((char *, int));
70 extern void warns
PARAMS((char *, char *));
71 extern void warnss
PARAMS((char *, char *, char *));
72 extern void warnsss
PARAMS((char *, char *, char *, char *));
73 extern void unlex
PARAMS((int));
74 extern void done
PARAMS((int));
76 extern int skip_white_space
PARAMS((void));
77 extern int parse_percent_token
PARAMS((void));
78 extern int lex
PARAMS((void));
83 struct symbol_list
*next
;
90 void reader
PARAMS((void));
91 void reader_output_yylsp
PARAMS((FILE *));
92 void read_declarations
PARAMS((void));
93 void copy_definition
PARAMS((void));
94 void parse_token_decl
PARAMS((int, int));
95 void parse_start_decl
PARAMS((void));
96 void parse_type_decl
PARAMS((void));
97 void parse_assoc_decl
PARAMS((int));
98 void parse_union_decl
PARAMS((void));
99 void parse_expect_decl
PARAMS((void));
100 char *get_type_name
PARAMS((int, symbol_list
*));
101 void copy_guard
PARAMS((symbol_list
*, int));
102 void parse_thong_decl
PARAMS((void));
103 void copy_action
PARAMS((symbol_list
*, int));
104 bucket
*gensym
PARAMS((void));
105 void readgram
PARAMS((void));
106 void record_rule_line
PARAMS((void));
107 void packsymbols
PARAMS((void));
108 void output_token_defines
PARAMS((FILE *));
109 void packgram
PARAMS((void));
110 int read_signed_integer
PARAMS((FILE *));
111 static int get_type
PARAMS((void));
115 symbol_list
*grammar
;
121 /* Nonzero if components of semantic values are used, implying
122 they must be unions. */
123 static int value_components_used
;
125 static int typed
; /* nonzero if %union has been seen. */
127 static int lastprec
; /* incremented for each %left, %right or %nonassoc seen */
129 static int gensym_count
; /* incremented for each generated symbol */
131 static bucket
*errtoken
;
132 static bucket
*undeftoken
;
134 /* Nonzero if any action or guard uses the @n construct. */
135 static int yylsp_needed
;
137 extern char *version_string
;
141 skip_to_char (int target
)
145 warn(_(" Skipping to next \\n"));
147 warni(_(" Skipping to next %c"), target
);
150 c
= skip_white_space();
151 while (c
!= target
&& c
!= EOF
);
161 startval
= NULL
; /* start symbol not specified yet. */
164 translations
= 0; /* initially assume token number translation not needed. */
166 /* Nowadays translations is always set to 1,
167 since we give `error' a user-token-number
168 to satisfy the Posix demand for YYERRCODE==256. */
175 rline_allocated
= 10;
176 rline
= NEW2(rline_allocated
, short);
192 /* initialize the symbol table. */
194 /* construct the error token */
195 errtoken
= getsym("error");
196 errtoken
->class = STOKEN
;
197 errtoken
->user_token_number
= 256; /* Value specified by posix. */
198 /* construct a token that represents all undefined literal tokens. */
199 /* it is always token number 2. */
200 undeftoken
= getsym("$undefined.");
201 undeftoken
->class = STOKEN
;
202 undeftoken
->user_token_number
= 2;
203 /* Read the declaration section. Copy %{ ... %} groups to ftable and fdefines file.
204 Also notice any %token, %left, etc. found there. */
206 fprintf(ftable
, "\n/* Bison-generated parse tables, made from %s\n",
209 fprintf(ftable
, "\n/* A Bison parser, made from %s\n", infile
);
210 fprintf(ftable
, " by %s */\n\n", version_string
);
211 fprintf(ftable
, "#define YYBISON 1 /* Identify Bison output. */\n\n");
213 /* start writing the guard and action files, if they are needed. */
215 /* read in the grammar, build grammar in list form. write out guards and actions. */
217 /* Now we know whether we need the line-number stack.
218 If we do, write its type into the .tab.h file. */
220 reader_output_yylsp(fdefines
);
221 /* write closing delimiters for actions and guards. */
224 fprintf(ftable
, "#define YYLSP_NEEDED\n\n");
225 /* assign the symbols their symbol numbers.
226 Write #defines for the token symbols into fdefines if requested. */
228 /* convert the grammar into the format described in gram.h. */
230 /* free the symbol table data structure
231 since symbols are now all referred to by symbol number. */
236 reader_output_yylsp (FILE *f
)
239 fprintf(f
, LTYPESTR
);
242 /* read from finput until %% is seen. Discard the %%.
243 Handle any % declarations,
244 and copy the contents of any %{ ... %} groups to fattrs. */
247 read_declarations (void)
254 c
= skip_white_space();
258 tok
= parse_percent_token();
265 case PERCENT_LEFT_CURLY
:
270 parse_token_decl (STOKEN
, SNTERM
);
274 parse_token_decl (SNTERM
, STOKEN
);
296 parse_assoc_decl(LEFT_ASSOC
);
300 parse_assoc_decl(RIGHT_ASSOC
);
304 parse_assoc_decl(NON_ASSOC
);
307 case SEMANTIC_PARSER
:
308 if (semantic_parser
== 0)
323 warns(_("unrecognized: %s"), token_buffer
);
328 fatal(_("no input grammar"));
332 sprintf(buff
, _("unknown character: %s"), printable_version(c
));
340 /* copy the contents of a %{ ... %} into the definitions file.
341 The %{ has already been read. Return after reading the %}. */
344 copy_definition (void)
349 register int after_percent
; /* -1 while reading a character if prev char was % */
353 fprintf(fattrs
, "#line %d \"%s\"\n", lineno
, infile
);
381 fatal(_("unterminated string at end of file"));
384 warn(_("unterminated string"));
396 fatal(_("unterminated string at end of file"));
411 if (c
!= '*' && c
!= '/')
414 cplus_comment
= (c
== '/');
421 if (!cplus_comment
&& c
== '*')
445 fatal(_("unterminated comment in `%{' definition"));
456 fatal(_("unterminated `%{' definition"));
478 /* parse what comes after %token or %nterm.
479 For %token, what_is is STOKEN and what_is_not is SNTERM.
480 For %nterm, the arguments are reversed. */
483 parse_token_decl (int what_is
, int what_is_not
)
485 register int token
= 0;
486 register char *typename
= 0;
487 register struct bucket
*symbol
= NULL
; /* pts to symbol being defined */
492 int tmp_char
= ungetc (skip_white_space (), finput
);
497 fatals ("Premature EOF after %s", token_buffer
);
505 if (token
== TYPENAME
)
507 k
= strlen(token_buffer
);
508 typename
= NEW2(k
+ 1, char);
509 strcpy(typename
, token_buffer
);
510 value_components_used
= 1;
513 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"'
517 symval
->class = STOKEN
;
518 symval
->type_name
= typename
;
519 symval
->user_token_number
= symbol
->user_token_number
;
520 symbol
->user_token_number
= SALIAS
;
522 symval
->alias
= symbol
;
523 symbol
->alias
= symval
;
526 nsyms
--; /* symbol and symval combined are only one symbol */
528 else if (token
== IDENTIFIER
)
530 int oldclass
= symval
->class;
533 if (symbol
->class == what_is_not
)
534 warns(_("symbol %s redefined"), symbol
->tag
);
535 symbol
->class = what_is
;
536 if (what_is
== SNTERM
&& oldclass
!= SNTERM
)
537 symbol
->value
= nvars
++;
541 if (symbol
->type_name
== NULL
)
542 symbol
->type_name
= typename
;
543 else if (strcmp(typename
, symbol
->type_name
) != 0)
544 warns(_("type redeclaration for %s"), symbol
->tag
);
547 else if (symbol
&& token
== NUMBER
)
549 symbol
->user_token_number
= numval
;
554 warnss(_("`%s' is invalid in %s"),
556 (what_is
== STOKEN
) ? "%token" : "%nterm");
563 /* parse what comes after %thong
565 %thong <type> token number literal
566 the <type> or number may be omitted. The number specifies the
569 Two symbols are entered in the table, one for the token symbol and
570 one for the literal. Both are given the <type>, if any, from the declaration.
571 The ->user_token_number of the first is SALIAS and the ->user_token_number
572 of the second is set to the number, if any, from the declaration.
573 The two symbols are linked via pointers in their ->alias fields.
575 during output_defines_table, the symbol is reported
576 thereafter, only the literal string is retained
577 it is the literal string that is output to yytname
581 parse_thong_decl (void)
584 register struct bucket
*symbol
;
585 register char *typename
= 0;
589 token
= lex(); /* fetch typename or first token */
590 if (token
== TYPENAME
) {
591 k
= strlen(token_buffer
);
592 typename
= NEW2(k
+ 1, char);
593 strcpy(typename
, token_buffer
);
594 value_components_used
= 1;
595 token
= lex(); /* fetch first token */
598 /* process first token */
600 if (token
!= IDENTIFIER
)
602 warns(_("unrecognized item %s, expected an identifier"),
607 symval
->class = STOKEN
;
608 symval
->type_name
= typename
;
609 symval
->user_token_number
= SALIAS
;
612 token
= lex(); /* get number or literal string */
614 if (token
== NUMBER
) {
616 token
= lex(); /* okay, did number, now get literal */
620 /* process literal string token */
622 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
624 warns(_("expected string constant instead of %s"),
629 symval
->class = STOKEN
;
630 symval
->type_name
= typename
;
631 symval
->user_token_number
= usrtoknum
;
633 symval
->alias
= symbol
;
634 symbol
->alias
= symval
;
636 nsyms
--; /* symbol and symval combined are only one symbol */
640 /* parse what comes after %start */
643 parse_start_decl (void)
646 warn(_("multiple %start declarations"));
647 if (lex() != IDENTIFIER
)
648 warn(_("invalid %start declaration"));
658 /* read in a %type declaration and record its information for get_type_name to access */
661 parse_type_decl (void)
666 if (lex() != TYPENAME
)
668 warn(_("%type declaration has no <typename>"));
673 k
= strlen(token_buffer
);
674 name
= NEW2(k
+ 1, char);
675 strcpy(name
, token_buffer
);
680 int tmp_char
= ungetc (skip_white_space (), finput
);
685 fatals ("Premature EOF after %s", token_buffer
);
697 if (symval
->type_name
== NULL
)
698 symval
->type_name
= name
;
699 else if (strcmp(name
, symval
->type_name
) != 0)
700 warns(_("type redeclaration for %s"), symval
->tag
);
705 warns(_("invalid %%type declaration due to item: `%s'"), token_buffer
);
713 /* read in a %left, %right or %nonassoc declaration and record its information. */
714 /* assoc is either LEFT_ASSOC, RIGHT_ASSOC or NON_ASSOC. */
717 parse_assoc_decl (int assoc
)
720 register char *name
= NULL
;
721 register int prev
= 0;
723 lastprec
++; /* Assign a new precedence level, never 0. */
728 int tmp_char
= ungetc (skip_white_space (), finput
);
733 fatals ("Premature EOF after %s", token_buffer
);
741 k
= strlen(token_buffer
);
742 name
= NEW2(k
+ 1, char);
743 strcpy(name
, token_buffer
);
750 if (symval
->prec
!= 0)
751 warns(_("redefining precedence of %s"), symval
->tag
);
752 symval
->prec
= lastprec
;
753 symval
->assoc
= assoc
;
754 if (symval
->class == SNTERM
)
755 warns(_("symbol %s redefined"), symval
->tag
);
756 symval
->class = STOKEN
;
758 { /* record the type, if one is specified */
759 if (symval
->type_name
== NULL
)
760 symval
->type_name
= name
;
761 else if (strcmp(name
, symval
->type_name
) != 0)
762 warns(_("type redeclaration for %s"), symval
->tag
);
767 if (prev
== IDENTIFIER
)
769 symval
->user_token_number
= numval
;
774 warns(_("invalid text (%s) - number should be after identifier"),
784 warns(_("unexpected item: %s"), token_buffer
);
795 /* copy the union declaration into fattrs (and fdefines),
796 where it is made into the
797 definition of YYSTYPE, the type of elements of the parser value stack. */
800 parse_union_decl (void)
804 register int in_comment
;
808 warn(_("multiple %union declarations"));
813 fprintf(fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
815 fprintf(fattrs
, "\n");
817 fprintf(fattrs
, "typedef union");
819 fprintf(fdefines
, "typedef union");
840 if (c
!= '*' && c
!= '/')
847 cplus_comment
= (c
== '/');
866 fatal(_("unterminated comment at end of file"));
868 if (!cplus_comment
&& c
== '*')
892 warn (_("unmatched close-brace (`}')"));
896 fprintf(fattrs
, " YYSTYPE;\n");
898 fprintf(fdefines
, " YYSTYPE;\n");
899 /* JF don't choke on trailing semi */
900 c
=skip_white_space();
901 if(c
!=';') ungetc(c
,finput
);
910 /* parse the declaration %expect N which says to expect N
911 shift-reduce conflicts. */
914 parse_expect_decl (void)
921 while (c
== ' ' || c
== '\t')
925 while (c
>= '0' && c
<= '9')
935 if (count
<= 0 || count
> 10)
936 warn(_("argument of %expect is not an integer"));
937 expected_conflicts
= atoi (buffer
);
940 /* that's all of parsing the declaration section */
942 /* Get the data type (alternative in the union) of the value for symbol n in rule rule. */
945 get_type_name (int n
, symbol_list
*rule
)
947 static char *msg
= N_("invalid $ value");
950 register symbol_list
*rp
;
964 if (rp
== NULL
|| rp
->sym
== NULL
)
972 return (rp
->sym
->type_name
);
976 /* after %guard is seen in the input file,
977 copy the actual guard into the guards file.
978 If the guard is followed by an action, copy that into the actions file.
979 stack_offset is the number of values in the current rule so far,
980 which says where to find $0 with respect to the top of the stack,
981 for the simple parser in which the stack is not popped until after the guard is run. */
984 copy_guard (symbol_list
*rule
, int stack_offset
)
991 register char *type_name
;
995 /* offset is always 0 if parser has already popped the stack pointer */
996 if (semantic_parser
) stack_offset
= 0;
998 fprintf(fguard
, "\ncase %d:\n", nrules
);
1000 fprintf(fguard
, "#line %d \"%s\"\n", lineno
, infile
);
1006 while (brace_flag
? (count
> 0) : (c
!= ';'))
1027 warn(_("unmatched right brace (`}')"));
1028 c
= getc(finput
); /* skip it */
1041 fatal(_("unterminated string at end of file"));
1044 warn(_("unterminated string"));
1046 c
= match
; /* invent terminator */
1056 fatal(_("unterminated string"));
1071 if (c
!= '*' && c
!= '/')
1074 cplus_comment
= (c
== '/');
1081 if (!cplus_comment
&& c
== '*')
1105 fatal(_("unterminated comment"));
1121 register char *cp
= token_buffer
;
1123 while ((c
= getc(finput
)) != '>' && c
> 0)
1125 if (cp
== token_buffer
+ maxtoken
)
1126 cp
= grow_token_buffer(cp
);
1131 type_name
= token_buffer
;
1138 fprintf(fguard
, "yyval");
1139 if (!type_name
) type_name
= rule
->sym
->type_name
;
1141 fprintf(fguard
, ".%s", type_name
);
1142 if(!type_name
&& typed
)
1143 warns(_("$$ of `%s' has no declared type"), rule
->sym
->tag
);
1146 else if (isdigit(c
) || c
== '-')
1149 n
= read_signed_integer(finput
);
1152 if (!type_name
&& n
> 0)
1153 type_name
= get_type_name(n
, rule
);
1155 fprintf(fguard
, "yyvsp[%d]", n
- stack_offset
);
1157 fprintf(fguard
, ".%s", type_name
);
1158 if(!type_name
&& typed
)
1159 warnss(_("$%s of `%s' has no declared type"), int_to_string(n
), rule
->sym
->tag
);
1163 warns(_("$%s is invalid"), printable_version(c
));
1169 if (isdigit(c
) || c
== '-')
1172 n
= read_signed_integer(finput
);
1177 warns(_("@%s is invalid"), printable_version(c
));
1181 fprintf(fguard
, "yylsp[%d]", n
- stack_offset
);
1187 fatal(_("unterminated %%guard clause"));
1193 if (c
!= '}' || count
!= 0)
1197 c
= skip_white_space();
1199 fprintf(fguard
, ";\n break;}");
1201 copy_action(rule
, stack_offset
);
1204 c
= getc(finput
); /* why not skip_white_space -wjh */
1206 copy_action(rule
, stack_offset
);
1214 /* Assuming that a { has just been seen, copy everything up to the matching }
1215 into the actions file.
1216 stack_offset is the number of values in the current rule so far,
1217 which says where to find $0 with respect to the top of the stack. */
1220 copy_action (symbol_list
*rule
, int stack_offset
)
1227 register char *type_name
;
1230 /* offset is always 0 if parser has already popped the stack pointer */
1231 if (semantic_parser
) stack_offset
= 0;
1233 fprintf(faction
, "\ncase %d:\n", nrules
);
1235 fprintf(faction
, "#line %d \"%s\"\n", lineno
, infile
);
1267 warn(_("unterminated string"));
1273 fatal(_("unterminated string at end of file"));
1281 fatal(_("unterminated string"));
1296 if (c
!= '*' && c
!= '/')
1299 cplus_comment
= (c
== '/');
1306 if (!cplus_comment
&& c
== '*')
1330 fatal(_("unterminated comment"));
1346 register char *cp
= token_buffer
;
1348 while ((c
= getc(finput
)) != '>' && c
> 0)
1350 if (cp
== token_buffer
+ maxtoken
)
1351 cp
= grow_token_buffer(cp
);
1356 type_name
= token_buffer
;
1357 value_components_used
= 1;
1363 fprintf(faction
, "yyval");
1364 if (!type_name
) type_name
= get_type_name(0, rule
);
1366 fprintf(faction
, ".%s", type_name
);
1367 if(!type_name
&& typed
)
1368 warns(_("$$ of `%s' has no declared type"), rule
->sym
->tag
);
1370 else if (isdigit(c
) || c
== '-')
1373 n
= read_signed_integer(finput
);
1376 if (!type_name
&& n
> 0)
1377 type_name
= get_type_name(n
, rule
);
1379 fprintf(faction
, "yyvsp[%d]", n
- stack_offset
);
1381 fprintf(faction
, ".%s", type_name
);
1382 if(!type_name
&& typed
)
1383 warnss(_("$%s of `%s' has no declared type"),
1384 int_to_string(n
), rule
->sym
->tag
);
1388 warns(_("$%s is invalid"), printable_version(c
));
1394 if (isdigit(c
) || c
== '-')
1397 n
= read_signed_integer(finput
);
1402 warn(_("invalid @-construct"));
1406 fprintf(faction
, "yylsp[%d]", n
- stack_offset
);
1412 fatal(_("unmatched `{'"));
1421 /* above loop exits when c is '}' */
1430 fprintf(faction
, ";\n break;}");
1435 /* generate a dummy symbol, a nonterminal,
1436 whose name cannot conflict with the user's names. */
1441 register bucket
*sym
;
1443 sprintf (token_buffer
, "@%d", ++gensym_count
);
1444 sym
= getsym(token_buffer
);
1445 sym
->class = SNTERM
;
1446 sym
->value
= nvars
++;
1450 /* Parse the input grammar into a one symbol_list structure.
1451 Each rule is represented by a sequence of symbols: the left hand side
1452 followed by the contents of the right hand side, followed by a null pointer
1453 instead of a symbol to terminate the rule.
1454 The next symbol is the lhs of the following rule.
1456 All guards and actions are copied out to the appropriate files,
1457 labelled by the rule number they apply to. */
1463 register bucket
*lhs
;
1464 register symbol_list
*p
;
1465 register symbol_list
*p1
;
1466 register bucket
*bp
;
1468 symbol_list
*crule
; /* points to first symbol_list of current rule. */
1469 /* its symbol is the lhs of the rule. */
1470 symbol_list
*crule1
; /* points to the symbol_list preceding crule. */
1476 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1478 if (t
== IDENTIFIER
|| t
== BAR
)
1480 register int actionflag
= 0;
1481 int rulelength
= 0; /* number of symbols in rhs of this rule so far */
1482 int xactions
= 0; /* JF for error checking */
1483 bucket
*first_rhs
= 0;
1485 if (t
== IDENTIFIER
)
1498 warn(_("ill-formed rule: initial symbol not followed by colon"));
1503 if (nrules
== 0 && t
== BAR
)
1505 warn(_("grammar starts with vertical bar"));
1506 lhs
= symval
; /* BOGUS: use a random symval */
1508 /* start a new rule and record its lhs. */
1513 record_rule_line ();
1515 p
= NEW(symbol_list
);
1527 /* mark the rule's lhs as a nonterminal if not already so. */
1529 if (lhs
->class == SUNKNOWN
)
1531 lhs
->class = SNTERM
;
1535 else if (lhs
->class == STOKEN
)
1536 warns(_("rule given for %s, which is a token"), lhs
->tag
);
1538 /* read the rhs of the rule. */
1546 crule
->ruleprec
= symval
;
1550 if (! (t
== IDENTIFIER
|| t
== LEFT_CURLY
)) break;
1552 /* If next token is an identifier, see if a colon follows it.
1553 If one does, exit this rule now. */
1554 if (t
== IDENTIFIER
)
1556 register bucket
*ssave
;
1563 if (t1
== COLON
) break;
1565 if(!first_rhs
) /* JF */
1567 /* Not followed by colon =>
1568 process as part of this rule's rhs. */
1571 /* If we just passed an action, that action was in the middle
1572 of a rule, so make a dummy rule to reduce it to a
1576 register bucket
*sdummy
;
1578 /* Since the action was written out with this rule's */
1579 /* number, we must give the new rule this number */
1580 /* by inserting the new rule before it. */
1582 /* Make a dummy nonterminal, a gensym. */
1585 /* Make a new rule, whose body is empty,
1586 before the current one, so that the action
1587 just read can belong to it. */
1590 record_rule_line ();
1591 p
= NEW(symbol_list
);
1596 crule1
= NEW(symbol_list
);
1598 crule1
->next
= crule
;
1600 /* insert the dummy generated by that rule into this rule. */
1602 p
= NEW(symbol_list
);
1610 if (t
== IDENTIFIER
)
1613 p
= NEW(symbol_list
);
1618 else /* handle an action. */
1620 copy_action(crule
, rulelength
);
1622 xactions
++; /* JF */
1625 } /* end of read rhs of rule */
1627 /* Put an empty link in the list to mark the end of this rule */
1628 p
= NEW(symbol_list
);
1634 warn(_("two @prec's in a row"));
1636 crule
->ruleprec
= symval
;
1641 if (! semantic_parser
)
1642 warn(_("%%guard present but %%semantic_parser not specified"));
1644 copy_guard(crule
, rulelength
);
1647 else if (t
== LEFT_CURLY
)
1649 /* This case never occurs -wjh */
1650 if (actionflag
) warn(_("two actions at end of one rule"));
1651 copy_action(crule
, rulelength
);
1653 xactions
++; /* -wjh */
1656 /* If $$ is being set in default way,
1657 warn if any type mismatch. */
1658 else if (!xactions
&& first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1660 if (lhs
->type_name
== 0 || first_rhs
->type_name
== 0
1661 || strcmp(lhs
->type_name
,first_rhs
->type_name
))
1662 warnss(_("type clash (`%s' `%s') on default action"),
1663 lhs
->type_name
? lhs
->type_name
: "",
1664 first_rhs
->type_name
? first_rhs
->type_name
: "");
1666 /* Warn if there is no default for $$ but we need one. */
1667 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1668 warn(_("empty rule for typed nonterminal, and no action"));
1673 /* these things can appear as alternatives to rules. */
1675 a) none of the documentation allows them
1676 b) most of them scan forward until finding a next %
1677 thus they may swallow lots of intervening rules
1679 else if (t
== TOKEN
)
1681 parse_token_decl(STOKEN
, SNTERM
);
1684 else if (t
== NTERM
)
1686 parse_token_decl(SNTERM
, STOKEN
);
1693 else if (t
== UNION
)
1698 else if (t
== EXPECT
)
1700 parse_expect_decl();
1703 else if (t
== START
)
1712 warns(_("invalid input: %s"), token_buffer
);
1717 /* grammar has been read. Do some checking */
1719 if (nsyms
> MAXSHORT
)
1720 fatals(_("too many symbols (tokens plus nonterminals); maximum %s"),
1721 int_to_string(MAXSHORT
));
1723 fatal(_("no rules in the input grammar"));
1725 if (typed
== 0 /* JF put out same default YYSTYPE as YACC does */
1726 && !value_components_used
)
1728 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1729 but it seems better to be consistent.
1730 Most programs should declare their own type anyway. */
1731 fprintf(fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1733 fprintf(fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1736 /* Report any undefined symbols and consider them nonterminals. */
1738 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1739 if (bp
->class == SUNKNOWN
)
1741 warns(_("symbol %s is used, but is not defined as a token and has no rules"),
1744 bp
->value
= nvars
++;
1747 ntokens
= nsyms
- nvars
;
1752 record_rule_line (void)
1754 /* Record each rule's source line number in rline table. */
1756 if (nrules
>= rline_allocated
)
1758 rline_allocated
= nrules
* 2;
1759 rline
= (short *) xrealloc ((char *) rline
,
1760 rline_allocated
* sizeof (short));
1762 rline
[nrules
] = lineno
;
1766 /* read in a %type declaration and record its information for get_type_name to access */
1767 /* this is unused. it is only called from the #if 0 part of readgram */
1773 register char *name
;
1779 warn(_("ill-formed %type declaration"));
1783 k
= strlen(token_buffer
);
1784 name
= NEW2(k
+ 1, char);
1785 strcpy(name
, token_buffer
);
1800 if (symval
->type_name
== NULL
)
1801 symval
->type_name
= name
;
1802 else if (strcmp(name
, symval
->type_name
) != 0)
1803 warns(_("type redeclaration for %s"), symval
->tag
);
1815 /* assign symbol numbers, and write definition of token names into fdefines.
1816 Set up vectors tags and sprec of names and precedences of symbols. */
1821 register bucket
*bp
;
1822 register int tokno
= 1;
1824 register int last_user_token_number
;
1826 /* int lossage = 0; JF set but not used */
1828 tags
= NEW2(nsyms
+ 1, char *);
1830 user_toknums
= NEW2(nsyms
+ 1, int);
1831 user_toknums
[0] = 0;
1833 sprec
= NEW2(nsyms
, short);
1834 sassoc
= NEW2(nsyms
, short);
1836 max_user_token_number
= 256;
1837 last_user_token_number
= 256;
1839 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1841 if (bp
->class == SNTERM
)
1843 bp
->value
+= ntokens
;
1847 /* this symbol and its alias are a single token defn.
1848 allocate a tokno, and assign to both
1849 check agreement of ->prec and ->assoc fields
1850 and make both the same
1853 bp
->value
= bp
->alias
->value
= tokno
++;
1855 if (bp
->prec
!= bp
->alias
->prec
) {
1856 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1857 && bp
->user_token_number
== SALIAS
)
1858 warnss(_("conflicting precedences for %s and %s"),
1859 bp
->tag
, bp
->alias
->tag
);
1860 if (bp
->prec
!= 0) bp
->alias
->prec
= bp
->prec
;
1861 else bp
->prec
= bp
->alias
->prec
;
1864 if (bp
->assoc
!= bp
->alias
->assoc
) {
1865 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1866 && bp
->user_token_number
== SALIAS
)
1867 warnss(_("conflicting assoc values for %s and %s"),
1868 bp
->tag
, bp
->alias
->tag
);
1869 if (bp
->assoc
!= 0) bp
->alias
->assoc
= bp
->assoc
;
1870 else bp
->assoc
= bp
->alias
->assoc
;
1873 if (bp
->user_token_number
== SALIAS
)
1874 continue; /* do not do processing below for SALIASs */
1877 else /* bp->class == STOKEN */
1879 bp
->value
= tokno
++;
1882 if (bp
->class == STOKEN
)
1884 if (translations
&& !(bp
->user_token_number
))
1885 bp
->user_token_number
= ++last_user_token_number
;
1886 if (bp
->user_token_number
> max_user_token_number
)
1887 max_user_token_number
= bp
->user_token_number
;
1890 tags
[bp
->value
] = bp
->tag
;
1891 user_toknums
[bp
->value
] = bp
->user_token_number
;
1892 sprec
[bp
->value
] = bp
->prec
;
1893 sassoc
[bp
->value
] = bp
->assoc
;
1901 token_translations
= NEW2(max_user_token_number
+1, short);
1903 /* initialize all entries for literal tokens to 2,
1904 the internal token number for $undefined.,
1905 which represents all invalid inputs. */
1906 for (i
= 0; i
<= max_user_token_number
; i
++)
1907 token_translations
[i
] = 2;
1909 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1911 if (bp
->value
>= ntokens
) continue; /* non-terminal */
1912 if (bp
->user_token_number
== SALIAS
) continue;
1913 if (token_translations
[bp
->user_token_number
] != 2)
1914 warnsss(_("tokens %s and %s both assigned number %s"),
1915 tags
[token_translations
[bp
->user_token_number
]],
1917 int_to_string(bp
->user_token_number
));
1918 token_translations
[bp
->user_token_number
] = bp
->value
;
1922 error_token_number
= errtoken
->value
;
1925 output_token_defines(ftable
);
1927 if (startval
->class == SUNKNOWN
)
1928 fatals(_("the start symbol %s is undefined"), startval
->tag
);
1929 else if (startval
->class == STOKEN
)
1930 fatals(_("the start symbol %s is a token"), startval
->tag
);
1932 start_symbol
= startval
->value
;
1936 output_token_defines(fdefines
);
1940 if (spec_name_prefix
)
1941 fprintf(fdefines
, "\nextern YYSTYPE %slval;\n", spec_name_prefix
);
1943 fprintf(fdefines
, "\nextern YYSTYPE yylval;\n");
1946 if (semantic_parser
)
1947 for (i
= ntokens
; i
< nsyms
; i
++)
1949 /* don't make these for dummy nonterminals made by gensym. */
1950 if (*tags
[i
] != '@')
1951 fprintf(fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1954 /* `fdefines' is now a temporary file, so we need to copy its
1955 contents in `done', so we can't close it here. */
1962 /* For named tokens, but not literal ones, define the name.
1963 The value is the user token number.
1966 output_token_defines (FILE *file
)
1969 register char *cp
, *symbol
;
1972 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1974 symbol
= bp
->tag
; /* get symbol */
1976 if (bp
->value
>= ntokens
) continue;
1977 if (bp
->user_token_number
== SALIAS
) continue;
1978 if ('\'' == *symbol
) continue; /* skip literal character */
1979 if (bp
== errtoken
) continue; /* skip error token */
1980 if ('\"' == *symbol
)
1982 /* use literal string only if given a symbol with an alias */
1984 symbol
= bp
->alias
->tag
;
1989 /* Don't #define nonliteral tokens whose names contain periods. */
1991 while ((c
= *cp
++) && c
!= '.');
1992 if (c
!= '\0') continue;
1994 fprintf(file
, "#define\t%s\t%d\n", symbol
,
1995 ((translations
&& ! rawtoknumflag
)
1996 ? bp
->user_token_number
1998 if (semantic_parser
)
1999 fprintf(file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
2007 /* convert the rules into the representation using rrhs, rlhs and ritems. */
2012 register int itemno
;
2013 register int ruleno
;
2014 register symbol_list
*p
;
2015 /* register bucket *bp; JF unused */
2019 ritem
= NEW2(nitems
+ 1, short);
2020 rlhs
= NEW2(nrules
, short) - 1;
2021 rrhs
= NEW2(nrules
, short) - 1;
2022 rprec
= NEW2(nrules
, short) - 1;
2023 rprecsym
= NEW2(nrules
, short) - 1;
2024 rassoc
= NEW2(nrules
, short) - 1;
2032 rlhs
[ruleno
] = p
->sym
->value
;
2033 rrhs
[ruleno
] = itemno
;
2034 ruleprec
= p
->ruleprec
;
2039 ritem
[itemno
++] = p
->sym
->value
;
2040 /* A rule gets by default the precedence and associativity
2041 of the last token in it. */
2042 if (p
->sym
->class == STOKEN
)
2044 rprec
[ruleno
] = p
->sym
->prec
;
2045 rassoc
[ruleno
] = p
->sym
->assoc
;
2050 /* If this rule has a %prec,
2051 the specified symbol's precedence replaces the default. */
2054 rprec
[ruleno
] = ruleprec
->prec
;
2055 rassoc
[ruleno
] = ruleprec
->assoc
;
2056 rprecsym
[ruleno
] = ruleprec
->value
;
2059 ritem
[itemno
++] = -ruleno
;
2068 /* Read a signed integer from STREAM and return its value. */
2071 read_signed_integer (FILE *stream
)
2073 register int c
= getc(stream
);
2074 register int sign
= 1;
2085 n
= 10*n
+ (c
- '0');