]>
git.saurik.com Git - bison.git/blob - src/reader.c
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 *));
113 static int get_type
PARAMS((void));
117 symbol_list
*grammar
;
123 /* Nonzero if components of semantic values are used, implying
124 they must be unions. */
125 static int value_components_used
;
127 static int typed
; /* nonzero if %union has been seen. */
129 static int lastprec
; /* incremented for each %left, %right or %nonassoc seen */
131 static int gensym_count
; /* incremented for each generated symbol */
133 static bucket
*errtoken
;
134 static bucket
*undeftoken
;
136 /* Nonzero if any action or guard uses the @n construct. */
137 static int yylsp_needed
;
139 extern char *version_string
;
143 skip_to_char (int target
)
147 warn(_(" Skipping to next \\n"));
149 warni(_(" Skipping to next %c"), target
);
152 c
= skip_white_space();
153 while (c
!= target
&& c
!= EOF
);
163 startval
= NULL
; /* start symbol not specified yet. */
166 translations
= 0; /* initially assume token number translation not needed. */
168 /* Nowadays translations is always set to 1,
169 since we give `error' a user-token-number
170 to satisfy the Posix demand for YYERRCODE==256. */
177 rline_allocated
= 10;
178 rline
= NEW2(rline_allocated
, short);
194 /* initialize the symbol table. */
196 /* construct the error token */
197 errtoken
= getsym("error");
198 errtoken
->class = STOKEN
;
199 errtoken
->user_token_number
= 256; /* Value specified by posix. */
200 /* construct a token that represents all undefined literal tokens. */
201 /* it is always token number 2. */
202 undeftoken
= getsym("$undefined.");
203 undeftoken
->class = STOKEN
;
204 undeftoken
->user_token_number
= 2;
205 /* Read the declaration section. Copy %{ ... %} groups to ftable and fdefines file.
206 Also notice any %token, %left, etc. found there. */
208 fprintf(ftable
, "\n/* Bison-generated parse tables, made from %s\n",
211 fprintf(ftable
, "\n/* A Bison parser, made from %s\n", infile
);
212 fprintf(ftable
, " by %s */\n\n", version_string
);
213 fprintf(ftable
, "#define YYBISON 1 /* Identify Bison output. */\n\n");
215 /* start writing the guard and action files, if they are needed. */
217 /* read in the grammar, build grammar in list form. write out guards and actions. */
219 /* Now we know whether we need the line-number stack.
220 If we do, write its type into the .tab.h file. */
222 reader_output_yylsp(fdefines
);
223 /* write closing delimiters for actions and guards. */
226 fprintf(ftable
, "#define YYLSP_NEEDED\n\n");
227 /* assign the symbols their symbol numbers.
228 Write #defines for the token symbols into fdefines if requested. */
230 /* convert the grammar into the format described in gram.h. */
232 /* free the symbol table data structure
233 since symbols are now all referred to by symbol number. */
238 reader_output_yylsp (FILE *f
)
241 fprintf(f
, LTYPESTR
);
244 /* read from finput until %% is seen. Discard the %%.
245 Handle any % declarations,
246 and copy the contents of any %{ ... %} groups to fattrs. */
249 read_declarations (void)
256 c
= skip_white_space();
260 tok
= parse_percent_token();
267 case PERCENT_LEFT_CURLY
:
272 parse_token_decl (STOKEN
, SNTERM
);
276 parse_token_decl (SNTERM
, STOKEN
);
298 parse_assoc_decl(LEFT_ASSOC
);
302 parse_assoc_decl(RIGHT_ASSOC
);
306 parse_assoc_decl(NON_ASSOC
);
309 case SEMANTIC_PARSER
:
310 if (semantic_parser
== 0)
325 warns(_("unrecognized: %s"), token_buffer
);
330 fatal(_("no input grammar"));
334 sprintf(buff
, _("unknown character: %s"), printable_version(c
));
342 /* copy the contents of a %{ ... %} into the definitions file.
343 The %{ has already been read. Return after reading the %}. */
346 copy_definition (void)
351 register int after_percent
; /* -1 while reading a character if prev char was % */
355 fprintf(fattrs
, "#line %d \"%s\"\n", lineno
, infile
);
383 fatal(_("unterminated string at end of file"));
386 warn(_("unterminated string"));
398 fatal(_("unterminated string at end of file"));
413 if (c
!= '*' && c
!= '/')
416 cplus_comment
= (c
== '/');
423 if (!cplus_comment
&& c
== '*')
447 fatal(_("unterminated comment in `%{' definition"));
458 fatal(_("unterminated `%{' definition"));
480 /* parse what comes after %token or %nterm.
481 For %token, what_is is STOKEN and what_is_not is SNTERM.
482 For %nterm, the arguments are reversed. */
485 parse_token_decl (int what_is
, int what_is_not
)
487 register int token
= 0;
488 register char *typename
= 0;
489 register struct bucket
*symbol
= NULL
; /* pts to symbol being defined */
494 int tmp_char
= ungetc (skip_white_space (), finput
);
499 fatals ("Premature EOF after %s", token_buffer
);
507 if (token
== TYPENAME
)
509 k
= strlen(token_buffer
);
510 typename
= NEW2(k
+ 1, char);
511 strcpy(typename
, token_buffer
);
512 value_components_used
= 1;
515 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"'
519 symval
->class = STOKEN
;
520 symval
->type_name
= typename
;
521 symval
->user_token_number
= symbol
->user_token_number
;
522 symbol
->user_token_number
= SALIAS
;
524 symval
->alias
= symbol
;
525 symbol
->alias
= symval
;
528 nsyms
--; /* symbol and symval combined are only one symbol */
530 else if (token
== IDENTIFIER
)
532 int oldclass
= symval
->class;
535 if (symbol
->class == what_is_not
)
536 warns(_("symbol %s redefined"), symbol
->tag
);
537 symbol
->class = what_is
;
538 if (what_is
== SNTERM
&& oldclass
!= SNTERM
)
539 symbol
->value
= nvars
++;
543 if (symbol
->type_name
== NULL
)
544 symbol
->type_name
= typename
;
545 else if (strcmp(typename
, symbol
->type_name
) != 0)
546 warns(_("type redeclaration for %s"), symbol
->tag
);
549 else if (symbol
&& token
== NUMBER
)
551 symbol
->user_token_number
= numval
;
556 warnss(_("`%s' is invalid in %s"),
558 (what_is
== STOKEN
) ? "%token" : "%nterm");
565 /* parse what comes after %thong
567 %thong <type> token number literal
568 the <type> or number may be omitted. The number specifies the
571 Two symbols are entered in the table, one for the token symbol and
572 one for the literal. Both are given the <type>, if any, from the declaration.
573 The ->user_token_number of the first is SALIAS and the ->user_token_number
574 of the second is set to the number, if any, from the declaration.
575 The two symbols are linked via pointers in their ->alias fields.
577 during output_defines_table, the symbol is reported
578 thereafter, only the literal string is retained
579 it is the literal string that is output to yytname
583 parse_thong_decl (void)
586 register struct bucket
*symbol
;
587 register char *typename
= 0;
591 token
= lex(); /* fetch typename or first token */
592 if (token
== TYPENAME
) {
593 k
= strlen(token_buffer
);
594 typename
= NEW2(k
+ 1, char);
595 strcpy(typename
, token_buffer
);
596 value_components_used
= 1;
597 token
= lex(); /* fetch first token */
600 /* process first token */
602 if (token
!= IDENTIFIER
)
604 warns(_("unrecognized item %s, expected an identifier"),
609 symval
->class = STOKEN
;
610 symval
->type_name
= typename
;
611 symval
->user_token_number
= SALIAS
;
614 token
= lex(); /* get number or literal string */
616 if (token
== NUMBER
) {
618 token
= lex(); /* okay, did number, now get literal */
622 /* process literal string token */
624 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
626 warns(_("expected string constant instead of %s"),
631 symval
->class = STOKEN
;
632 symval
->type_name
= typename
;
633 symval
->user_token_number
= usrtoknum
;
635 symval
->alias
= symbol
;
636 symbol
->alias
= symval
;
638 nsyms
--; /* symbol and symval combined are only one symbol */
642 /* parse what comes after %start */
645 parse_start_decl (void)
648 warn(_("multiple %start declarations"));
649 if (lex() != IDENTIFIER
)
650 warn(_("invalid %start declaration"));
660 /* read in a %type declaration and record its information for get_type_name to access */
663 parse_type_decl (void)
668 if (lex() != TYPENAME
)
670 warn(_("%type declaration has no <typename>"));
675 k
= strlen(token_buffer
);
676 name
= NEW2(k
+ 1, char);
677 strcpy(name
, token_buffer
);
682 int tmp_char
= ungetc (skip_white_space (), finput
);
687 fatals ("Premature EOF after %s", token_buffer
);
699 if (symval
->type_name
== NULL
)
700 symval
->type_name
= name
;
701 else if (strcmp(name
, symval
->type_name
) != 0)
702 warns(_("type redeclaration for %s"), symval
->tag
);
707 warns(_("invalid %%type declaration due to item: `%s'"), token_buffer
);
715 /* read in a %left, %right or %nonassoc declaration and record its information. */
716 /* assoc is either LEFT_ASSOC, RIGHT_ASSOC or NON_ASSOC. */
719 parse_assoc_decl (int assoc
)
722 register char *name
= NULL
;
723 register int prev
= 0;
725 lastprec
++; /* Assign a new precedence level, never 0. */
730 int tmp_char
= ungetc (skip_white_space (), finput
);
735 fatals ("Premature EOF after %s", token_buffer
);
743 k
= strlen(token_buffer
);
744 name
= NEW2(k
+ 1, char);
745 strcpy(name
, token_buffer
);
752 if (symval
->prec
!= 0)
753 warns(_("redefining precedence of %s"), symval
->tag
);
754 symval
->prec
= lastprec
;
755 symval
->assoc
= assoc
;
756 if (symval
->class == SNTERM
)
757 warns(_("symbol %s redefined"), symval
->tag
);
758 symval
->class = STOKEN
;
760 { /* record the type, if one is specified */
761 if (symval
->type_name
== NULL
)
762 symval
->type_name
= name
;
763 else if (strcmp(name
, symval
->type_name
) != 0)
764 warns(_("type redeclaration for %s"), symval
->tag
);
769 if (prev
== IDENTIFIER
)
771 symval
->user_token_number
= numval
;
776 warns(_("invalid text (%s) - number should be after identifier"),
786 warns(_("unexpected item: %s"), token_buffer
);
797 /* copy the union declaration into fattrs (and fdefines),
798 where it is made into the
799 definition of YYSTYPE, the type of elements of the parser value stack. */
802 parse_union_decl (void)
806 register int in_comment
;
810 warn(_("multiple %union declarations"));
815 fprintf(fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
817 fprintf(fattrs
, "\n");
819 fprintf(fattrs
, "typedef union");
821 fprintf(fdefines
, "typedef union");
842 if (c
!= '*' && c
!= '/')
849 cplus_comment
= (c
== '/');
868 fatal(_("unterminated comment at end of file"));
870 if (!cplus_comment
&& c
== '*')
894 warn (_("unmatched close-brace (`}')"));
898 fprintf(fattrs
, " YYSTYPE;\n");
900 fprintf(fdefines
, " YYSTYPE;\n");
901 /* JF don't choke on trailing semi */
902 c
=skip_white_space();
903 if(c
!=';') ungetc(c
,finput
);
912 /* parse the declaration %expect N which says to expect N
913 shift-reduce conflicts. */
916 parse_expect_decl (void)
923 while (c
== ' ' || c
== '\t')
927 while (c
>= '0' && c
<= '9')
937 if (count
<= 0 || count
> 10)
938 warn(_("argument of %expect is not an integer"));
939 expected_conflicts
= atoi (buffer
);
942 /* that's all of parsing the declaration section */
944 /* Get the data type (alternative in the union) of the value for symbol n in rule rule. */
947 get_type_name (int n
, symbol_list
*rule
)
949 static char *msg
= N_("invalid $ value");
952 register symbol_list
*rp
;
966 if (rp
== NULL
|| rp
->sym
== NULL
)
974 return (rp
->sym
->type_name
);
978 /* after %guard is seen in the input file,
979 copy the actual guard into the guards file.
980 If the guard is followed by an action, copy that into the actions file.
981 stack_offset is the number of values in the current rule so far,
982 which says where to find $0 with respect to the top of the stack,
983 for the simple parser in which the stack is not popped until after the guard is run. */
986 copy_guard (symbol_list
*rule
, int stack_offset
)
993 register char *type_name
;
997 /* offset is always 0 if parser has already popped the stack pointer */
998 if (semantic_parser
) stack_offset
= 0;
1000 fprintf(fguard
, "\ncase %d:\n", nrules
);
1002 fprintf(fguard
, "#line %d \"%s\"\n", lineno
, infile
);
1008 while (brace_flag
? (count
> 0) : (c
!= ';'))
1029 warn(_("unmatched right brace (`}')"));
1030 c
= getc(finput
); /* skip it */
1043 fatal(_("unterminated string at end of file"));
1046 warn(_("unterminated string"));
1048 c
= match
; /* invent terminator */
1058 fatal(_("unterminated string"));
1073 if (c
!= '*' && c
!= '/')
1076 cplus_comment
= (c
== '/');
1083 if (!cplus_comment
&& c
== '*')
1107 fatal(_("unterminated comment"));
1123 register char *cp
= token_buffer
;
1125 while ((c
= getc(finput
)) != '>' && c
> 0)
1127 if (cp
== token_buffer
+ maxtoken
)
1128 cp
= grow_token_buffer(cp
);
1133 type_name
= token_buffer
;
1140 fprintf(fguard
, "yyval");
1141 if (!type_name
) type_name
= rule
->sym
->type_name
;
1143 fprintf(fguard
, ".%s", type_name
);
1144 if(!type_name
&& typed
)
1145 warns(_("$$ of `%s' has no declared type"), rule
->sym
->tag
);
1148 else if (isdigit(c
) || c
== '-')
1151 n
= read_signed_integer(finput
);
1154 if (!type_name
&& n
> 0)
1155 type_name
= get_type_name(n
, rule
);
1157 fprintf(fguard
, "yyvsp[%d]", n
- stack_offset
);
1159 fprintf(fguard
, ".%s", type_name
);
1160 if(!type_name
&& typed
)
1161 warnss(_("$%s of `%s' has no declared type"), int_to_string(n
), rule
->sym
->tag
);
1165 warns(_("$%s is invalid"), printable_version(c
));
1171 if (isdigit(c
) || c
== '-')
1174 n
= read_signed_integer(finput
);
1179 warns(_("@%s is invalid"), printable_version(c
));
1183 fprintf(fguard
, "yylsp[%d]", n
- stack_offset
);
1189 fatal(_("unterminated %%guard clause"));
1195 if (c
!= '}' || count
!= 0)
1199 c
= skip_white_space();
1201 fprintf(fguard
, ";\n break;}");
1203 copy_action(rule
, stack_offset
);
1206 c
= getc(finput
); /* why not skip_white_space -wjh */
1208 copy_action(rule
, stack_offset
);
1216 /* Assuming that a { has just been seen, copy everything up to the matching }
1217 into the actions file.
1218 stack_offset is the number of values in the current rule so far,
1219 which says where to find $0 with respect to the top of the stack. */
1222 copy_action (symbol_list
*rule
, int stack_offset
)
1229 register char *type_name
;
1232 /* offset is always 0 if parser has already popped the stack pointer */
1233 if (semantic_parser
) stack_offset
= 0;
1235 fprintf(faction
, "\ncase %d:\n", nrules
);
1237 fprintf(faction
, "#line %d \"%s\"\n", lineno
, infile
);
1269 warn(_("unterminated string"));
1275 fatal(_("unterminated string at end of file"));
1283 fatal(_("unterminated string"));
1298 if (c
!= '*' && c
!= '/')
1301 cplus_comment
= (c
== '/');
1308 if (!cplus_comment
&& c
== '*')
1332 fatal(_("unterminated comment"));
1348 register char *cp
= token_buffer
;
1350 while ((c
= getc(finput
)) != '>' && c
> 0)
1352 if (cp
== token_buffer
+ maxtoken
)
1353 cp
= grow_token_buffer(cp
);
1358 type_name
= token_buffer
;
1359 value_components_used
= 1;
1365 fprintf(faction
, "yyval");
1366 if (!type_name
) type_name
= get_type_name(0, rule
);
1368 fprintf(faction
, ".%s", type_name
);
1369 if(!type_name
&& typed
)
1370 warns(_("$$ of `%s' has no declared type"), rule
->sym
->tag
);
1372 else if (isdigit(c
) || c
== '-')
1375 n
= read_signed_integer(finput
);
1378 if (!type_name
&& n
> 0)
1379 type_name
= get_type_name(n
, rule
);
1381 fprintf(faction
, "yyvsp[%d]", n
- stack_offset
);
1383 fprintf(faction
, ".%s", type_name
);
1384 if(!type_name
&& typed
)
1385 warnss(_("$%s of `%s' has no declared type"),
1386 int_to_string(n
), rule
->sym
->tag
);
1390 warns(_("$%s is invalid"), printable_version(c
));
1396 if (isdigit(c
) || c
== '-')
1399 n
= read_signed_integer(finput
);
1404 warn(_("invalid @-construct"));
1408 fprintf(faction
, "yylsp[%d]", n
- stack_offset
);
1414 fatal(_("unmatched `{'"));
1423 /* above loop exits when c is '}' */
1432 fprintf(faction
, ";\n break;}");
1437 /* generate a dummy symbol, a nonterminal,
1438 whose name cannot conflict with the user's names. */
1443 register bucket
*sym
;
1445 sprintf (token_buffer
, "@%d", ++gensym_count
);
1446 sym
= getsym(token_buffer
);
1447 sym
->class = SNTERM
;
1448 sym
->value
= nvars
++;
1452 /* Parse the input grammar into a one symbol_list structure.
1453 Each rule is represented by a sequence of symbols: the left hand side
1454 followed by the contents of the right hand side, followed by a null pointer
1455 instead of a symbol to terminate the rule.
1456 The next symbol is the lhs of the following rule.
1458 All guards and actions are copied out to the appropriate files,
1459 labelled by the rule number they apply to. */
1465 register bucket
*lhs
= NULL
;
1466 register symbol_list
*p
;
1467 register symbol_list
*p1
;
1468 register bucket
*bp
;
1470 symbol_list
*crule
; /* points to first symbol_list of current rule. */
1471 /* its symbol is the lhs of the rule. */
1472 symbol_list
*crule1
; /* points to the symbol_list preceding crule. */
1478 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1480 if (t
== IDENTIFIER
|| t
== BAR
)
1482 register int actionflag
= 0;
1483 int rulelength
= 0; /* number of symbols in rhs of this rule so far */
1484 int xactions
= 0; /* JF for error checking */
1485 bucket
*first_rhs
= 0;
1487 if (t
== IDENTIFIER
)
1500 warn(_("ill-formed rule: initial symbol not followed by colon"));
1505 if (nrules
== 0 && t
== BAR
)
1507 warn(_("grammar starts with vertical bar"));
1508 lhs
= symval
; /* BOGUS: use a random symval */
1510 /* start a new rule and record its lhs. */
1515 record_rule_line ();
1517 p
= NEW(symbol_list
);
1529 /* mark the rule's lhs as a nonterminal if not already so. */
1531 if (lhs
->class == SUNKNOWN
)
1533 lhs
->class = SNTERM
;
1537 else if (lhs
->class == STOKEN
)
1538 warns(_("rule given for %s, which is a token"), lhs
->tag
);
1540 /* read the rhs of the rule. */
1548 crule
->ruleprec
= symval
;
1552 if (! (t
== IDENTIFIER
|| t
== LEFT_CURLY
)) break;
1554 /* If next token is an identifier, see if a colon follows it.
1555 If one does, exit this rule now. */
1556 if (t
== IDENTIFIER
)
1558 register bucket
*ssave
;
1565 if (t1
== COLON
) break;
1567 if(!first_rhs
) /* JF */
1569 /* Not followed by colon =>
1570 process as part of this rule's rhs. */
1573 /* If we just passed an action, that action was in the middle
1574 of a rule, so make a dummy rule to reduce it to a
1578 register bucket
*sdummy
;
1580 /* Since the action was written out with this rule's */
1581 /* number, we must give the new rule this number */
1582 /* by inserting the new rule before it. */
1584 /* Make a dummy nonterminal, a gensym. */
1587 /* Make a new rule, whose body is empty,
1588 before the current one, so that the action
1589 just read can belong to it. */
1592 record_rule_line ();
1593 p
= NEW(symbol_list
);
1598 crule1
= NEW(symbol_list
);
1600 crule1
->next
= crule
;
1602 /* insert the dummy generated by that rule into this rule. */
1604 p
= NEW(symbol_list
);
1612 if (t
== IDENTIFIER
)
1615 p
= NEW(symbol_list
);
1620 else /* handle an action. */
1622 copy_action(crule
, rulelength
);
1624 xactions
++; /* JF */
1627 } /* end of read rhs of rule */
1629 /* Put an empty link in the list to mark the end of this rule */
1630 p
= NEW(symbol_list
);
1636 warn(_("two @prec's in a row"));
1638 crule
->ruleprec
= symval
;
1643 if (! semantic_parser
)
1644 warn(_("%%guard present but %%semantic_parser not specified"));
1646 copy_guard(crule
, rulelength
);
1649 else if (t
== LEFT_CURLY
)
1651 /* This case never occurs -wjh */
1652 if (actionflag
) warn(_("two actions at end of one rule"));
1653 copy_action(crule
, rulelength
);
1655 xactions
++; /* -wjh */
1658 /* If $$ is being set in default way,
1659 warn if any type mismatch. */
1660 else if (!xactions
&& first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1662 if (lhs
->type_name
== 0 || first_rhs
->type_name
== 0
1663 || strcmp(lhs
->type_name
,first_rhs
->type_name
))
1664 warnss(_("type clash (`%s' `%s') on default action"),
1665 lhs
->type_name
? lhs
->type_name
: "",
1666 first_rhs
->type_name
? first_rhs
->type_name
: "");
1668 /* Warn if there is no default for $$ but we need one. */
1669 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1670 warn(_("empty rule for typed nonterminal, and no action"));
1675 /* these things can appear as alternatives to rules. */
1677 a) none of the documentation allows them
1678 b) most of them scan forward until finding a next %
1679 thus they may swallow lots of intervening rules
1681 else if (t
== TOKEN
)
1683 parse_token_decl(STOKEN
, SNTERM
);
1686 else if (t
== NTERM
)
1688 parse_token_decl(SNTERM
, STOKEN
);
1695 else if (t
== UNION
)
1700 else if (t
== EXPECT
)
1702 parse_expect_decl();
1705 else if (t
== START
)
1714 warns(_("invalid input: %s"), token_buffer
);
1719 /* grammar has been read. Do some checking */
1721 if (nsyms
> MAXSHORT
)
1722 fatals(_("too many symbols (tokens plus nonterminals); maximum %s"),
1723 int_to_string(MAXSHORT
));
1725 fatal(_("no rules in the input grammar"));
1727 if (typed
== 0 /* JF put out same default YYSTYPE as YACC does */
1728 && !value_components_used
)
1730 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1731 but it seems better to be consistent.
1732 Most programs should declare their own type anyway. */
1733 fprintf(fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1735 fprintf(fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1738 /* Report any undefined symbols and consider them nonterminals. */
1740 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1741 if (bp
->class == SUNKNOWN
)
1743 warns(_("symbol %s is used, but is not defined as a token and has no rules"),
1746 bp
->value
= nvars
++;
1749 ntokens
= nsyms
- nvars
;
1754 record_rule_line (void)
1756 /* Record each rule's source line number in rline table. */
1758 if (nrules
>= rline_allocated
)
1760 rline_allocated
= nrules
* 2;
1761 rline
= (short *) xrealloc ((char *) rline
,
1762 rline_allocated
* sizeof (short));
1764 rline
[nrules
] = lineno
;
1769 /* read in a %type declaration and record its information for get_type_name to access */
1770 /* this is unused. it is only called from the #if 0 part of readgram */
1776 register char *name
;
1782 warn(_("ill-formed %type declaration"));
1786 k
= strlen(token_buffer
);
1787 name
= NEW2(k
+ 1, char);
1788 strcpy(name
, token_buffer
);
1803 if (symval
->type_name
== NULL
)
1804 symval
->type_name
= name
;
1805 else if (strcmp(name
, symval
->type_name
) != 0)
1806 warns(_("type redeclaration for %s"), symval
->tag
);
1818 /* assign symbol numbers, and write definition of token names into fdefines.
1819 Set up vectors tags and sprec of names and precedences of symbols. */
1824 register bucket
*bp
;
1825 register int tokno
= 1;
1827 register int last_user_token_number
;
1829 /* int lossage = 0; JF set but not used */
1831 tags
= NEW2(nsyms
+ 1, char *);
1833 user_toknums
= NEW2(nsyms
+ 1, int);
1834 user_toknums
[0] = 0;
1836 sprec
= NEW2(nsyms
, short);
1837 sassoc
= NEW2(nsyms
, short);
1839 max_user_token_number
= 256;
1840 last_user_token_number
= 256;
1842 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1844 if (bp
->class == SNTERM
)
1846 bp
->value
+= ntokens
;
1850 /* this symbol and its alias are a single token defn.
1851 allocate a tokno, and assign to both
1852 check agreement of ->prec and ->assoc fields
1853 and make both the same
1856 bp
->value
= bp
->alias
->value
= tokno
++;
1858 if (bp
->prec
!= bp
->alias
->prec
) {
1859 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1860 && bp
->user_token_number
== SALIAS
)
1861 warnss(_("conflicting precedences for %s and %s"),
1862 bp
->tag
, bp
->alias
->tag
);
1863 if (bp
->prec
!= 0) bp
->alias
->prec
= bp
->prec
;
1864 else bp
->prec
= bp
->alias
->prec
;
1867 if (bp
->assoc
!= bp
->alias
->assoc
) {
1868 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1869 && bp
->user_token_number
== SALIAS
)
1870 warnss(_("conflicting assoc values for %s and %s"),
1871 bp
->tag
, bp
->alias
->tag
);
1872 if (bp
->assoc
!= 0) bp
->alias
->assoc
= bp
->assoc
;
1873 else bp
->assoc
= bp
->alias
->assoc
;
1876 if (bp
->user_token_number
== SALIAS
)
1877 continue; /* do not do processing below for SALIASs */
1880 else /* bp->class == STOKEN */
1882 bp
->value
= tokno
++;
1885 if (bp
->class == STOKEN
)
1887 if (translations
&& !(bp
->user_token_number
))
1888 bp
->user_token_number
= ++last_user_token_number
;
1889 if (bp
->user_token_number
> max_user_token_number
)
1890 max_user_token_number
= bp
->user_token_number
;
1893 tags
[bp
->value
] = bp
->tag
;
1894 user_toknums
[bp
->value
] = bp
->user_token_number
;
1895 sprec
[bp
->value
] = bp
->prec
;
1896 sassoc
[bp
->value
] = bp
->assoc
;
1904 token_translations
= NEW2(max_user_token_number
+1, short);
1906 /* initialize all entries for literal tokens to 2,
1907 the internal token number for $undefined.,
1908 which represents all invalid inputs. */
1909 for (i
= 0; i
<= max_user_token_number
; i
++)
1910 token_translations
[i
] = 2;
1912 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1914 if (bp
->value
>= ntokens
) continue; /* non-terminal */
1915 if (bp
->user_token_number
== SALIAS
) continue;
1916 if (token_translations
[bp
->user_token_number
] != 2)
1917 warnsss(_("tokens %s and %s both assigned number %s"),
1918 tags
[token_translations
[bp
->user_token_number
]],
1920 int_to_string(bp
->user_token_number
));
1921 token_translations
[bp
->user_token_number
] = bp
->value
;
1925 error_token_number
= errtoken
->value
;
1928 output_token_defines(ftable
);
1930 if (startval
->class == SUNKNOWN
)
1931 fatals(_("the start symbol %s is undefined"), startval
->tag
);
1932 else if (startval
->class == STOKEN
)
1933 fatals(_("the start symbol %s is a token"), startval
->tag
);
1935 start_symbol
= startval
->value
;
1939 output_token_defines(fdefines
);
1943 if (spec_name_prefix
)
1944 fprintf(fdefines
, "\nextern YYSTYPE %slval;\n", spec_name_prefix
);
1946 fprintf(fdefines
, "\nextern YYSTYPE yylval;\n");
1949 if (semantic_parser
)
1950 for (i
= ntokens
; i
< nsyms
; i
++)
1952 /* don't make these for dummy nonterminals made by gensym. */
1953 if (*tags
[i
] != '@')
1954 fprintf(fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1957 /* `fdefines' is now a temporary file, so we need to copy its
1958 contents in `done', so we can't close it here. */
1965 /* For named tokens, but not literal ones, define the name.
1966 The value is the user token number.
1969 output_token_defines (FILE *file
)
1972 register char *cp
, *symbol
;
1975 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1977 symbol
= bp
->tag
; /* get symbol */
1979 if (bp
->value
>= ntokens
) continue;
1980 if (bp
->user_token_number
== SALIAS
) continue;
1981 if ('\'' == *symbol
) continue; /* skip literal character */
1982 if (bp
== errtoken
) continue; /* skip error token */
1983 if ('\"' == *symbol
)
1985 /* use literal string only if given a symbol with an alias */
1987 symbol
= bp
->alias
->tag
;
1992 /* Don't #define nonliteral tokens whose names contain periods. */
1994 while ((c
= *cp
++) && c
!= '.');
1995 if (c
!= '\0') continue;
1997 fprintf(file
, "#define\t%s\t%d\n", symbol
,
1998 ((translations
&& ! rawtoknumflag
)
1999 ? bp
->user_token_number
2001 if (semantic_parser
)
2002 fprintf(file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
2010 /* convert the rules into the representation using rrhs, rlhs and ritems. */
2015 register int itemno
;
2016 register int ruleno
;
2017 register symbol_list
*p
;
2018 /* register bucket *bp; JF unused */
2022 ritem
= NEW2(nitems
+ 1, short);
2023 rlhs
= NEW2(nrules
, short) - 1;
2024 rrhs
= NEW2(nrules
, short) - 1;
2025 rprec
= NEW2(nrules
, short) - 1;
2026 rprecsym
= NEW2(nrules
, short) - 1;
2027 rassoc
= NEW2(nrules
, short) - 1;
2035 rlhs
[ruleno
] = p
->sym
->value
;
2036 rrhs
[ruleno
] = itemno
;
2037 ruleprec
= p
->ruleprec
;
2042 ritem
[itemno
++] = p
->sym
->value
;
2043 /* A rule gets by default the precedence and associativity
2044 of the last token in it. */
2045 if (p
->sym
->class == STOKEN
)
2047 rprec
[ruleno
] = p
->sym
->prec
;
2048 rassoc
[ruleno
] = p
->sym
->assoc
;
2053 /* If this rule has a %prec,
2054 the specified symbol's precedence replaces the default. */
2057 rprec
[ruleno
] = ruleprec
->prec
;
2058 rassoc
[ruleno
] = ruleprec
->assoc
;
2059 rprecsym
[ruleno
] = ruleprec
->value
;
2062 ritem
[itemno
++] = -ruleno
;
2071 /* Read a signed integer from STREAM and return its value. */
2074 read_signed_integer (FILE *stream
)
2076 register int c
= getc(stream
);
2077 register int sign
= 1;
2088 n
= 10*n
+ (c
- '0');