]>
git.saurik.com Git - bison.git/blob - src/reader.c
36af464672a444e6f083fdfc22cd6e3aa3a7f3a5
1 /* Input parser for bison
2 Copyright (C) 1984, 86, 89, 92, 98, 2000 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
23 described in gram.h. All guards are copied into the fguard file
24 and all actions into faction, in each case forming the body of a C
25 function (yyguard or yyaction) which contains a switch statement to
26 decide which guard or action to execute.
28 The entry point is reader (). */
39 #define LTYPESTR "\n#ifndef YYLTYPE\ntypedef\n struct yyltype\n\
40 {\n int timestamp;\n int first_line;\n int first_column;\
41 \n int last_line;\n int last_column;\n char *text;\n }\n\
42 yyltype;\n\n#define YYLTYPE yyltype\n#endif\n\n"
44 /* Number of slots allocated (but not necessarily used yet) in `rline' */
47 extern char *program_name
;
48 extern int definesflag
;
49 extern int nolinesflag
;
50 extern int noparserflag
;
51 extern int rawtoknumflag
;
52 extern bucket
*symval
;
54 extern int expected_conflicts
;
55 extern char *token_buffer
;
58 extern void init_lex
PARAMS((void));
59 extern char *grow_token_buffer
PARAMS((char *));
60 extern void tabinit
PARAMS((void));
61 extern void output_headers
PARAMS((void));
62 extern void output_trailers
PARAMS((void));
63 extern void free_symtab
PARAMS((void));
64 extern void open_extra_files
PARAMS((void));
65 extern char *int_to_string
PARAMS((int));
66 extern char *printable_version
PARAMS((int));
67 extern void fatal
PARAMS((char *));
68 extern void fatals
PARAMS((char *, char *));
69 extern void warn
PARAMS((char *));
70 extern void warni
PARAMS((char *, int));
71 extern void warns
PARAMS((char *, char *));
72 extern void warnss
PARAMS((char *, char *, char *));
73 extern void warnsss
PARAMS((char *, char *, char *, char *));
74 extern void unlex
PARAMS((int));
75 extern void done
PARAMS((int));
77 extern int skip_white_space
PARAMS((void));
78 extern int parse_percent_token
PARAMS((void));
79 extern int lex
PARAMS((void));
84 struct symbol_list
*next
;
91 void reader
PARAMS((void));
92 void reader_output_yylsp
PARAMS((FILE *));
93 void read_declarations
PARAMS((void));
94 void copy_definition
PARAMS((void));
95 void parse_token_decl
PARAMS((int, int));
96 void parse_start_decl
PARAMS((void));
97 void parse_type_decl
PARAMS((void));
98 void parse_assoc_decl
PARAMS((int));
99 void parse_union_decl
PARAMS((void));
100 void parse_expect_decl
PARAMS((void));
101 char *get_type_name
PARAMS((int, symbol_list
*));
102 void copy_guard
PARAMS((symbol_list
*, int));
103 void parse_thong_decl
PARAMS((void));
104 void copy_action
PARAMS((symbol_list
*, int));
105 bucket
*gensym
PARAMS((void));
106 void readgram
PARAMS((void));
107 void record_rule_line
PARAMS((void));
108 void packsymbols
PARAMS((void));
109 void output_token_defines
PARAMS((FILE *));
110 void packgram
PARAMS((void));
111 int read_signed_integer
PARAMS((FILE *));
114 static int get_type
PARAMS((void));
118 symbol_list
*grammar
;
124 /* Nonzero if components of semantic values are used, implying
125 they must be unions. */
126 static int value_components_used
;
128 static int typed
; /* nonzero if %union has been seen. */
130 static int lastprec
; /* incremented for each %left, %right or %nonassoc seen */
132 static int gensym_count
; /* incremented for each generated symbol */
134 static bucket
*errtoken
;
135 static bucket
*undeftoken
;
137 /* Nonzero if any action or guard uses the @n construct. */
138 static int yylsp_needed
;
142 skip_to_char (int target
)
146 warn(_(" Skipping to next \\n"));
148 warni(_(" Skipping to next %c"), target
);
151 c
= skip_white_space();
152 while (c
!= target
&& c
!= EOF
);
162 startval
= NULL
; /* start symbol not specified yet. */
165 translations
= 0; /* initially assume token number translation not needed. */
167 /* Nowadays translations is always set to 1,
168 since we give `error' a user-token-number
169 to satisfy the Posix demand for YYERRCODE==256. */
176 rline_allocated
= 10;
177 rline
= NEW2(rline_allocated
, short);
193 /* initialize the symbol table. */
195 /* construct the error token */
196 errtoken
= getsym("error");
197 errtoken
->class = STOKEN
;
198 errtoken
->user_token_number
= 256; /* Value specified by posix. */
199 /* construct a token that represents all undefined literal tokens. */
200 /* it is always token number 2. */
201 undeftoken
= getsym("$undefined.");
202 undeftoken
->class = STOKEN
;
203 undeftoken
->user_token_number
= 2;
204 /* Read the declaration section. Copy %{ ... %} groups to ftable and fdefines file.
205 Also notice any %token, %left, etc. found there. */
207 fprintf(ftable
, "\n/* Bison-generated parse tables, made from %s\n",
210 fprintf(ftable
, "\n/* A Bison parser, made from %s\n", infile
);
211 fprintf(ftable
, " by %s */\n\n", VERSION_STRING
);
212 fprintf(ftable
, "#define YYBISON 1 /* Identify Bison output. */\n\n");
214 /* start writing the guard and action files, if they are needed. */
216 /* read in the grammar, build grammar in list form. write out guards and actions. */
218 /* Now we know whether we need the line-number stack.
219 If we do, write its type into the .tab.h file. */
221 reader_output_yylsp(fdefines
);
222 /* write closing delimiters for actions and guards. */
225 fprintf(ftable
, "#define YYLSP_NEEDED\n\n");
226 /* assign the symbols their symbol numbers.
227 Write #defines for the token symbols into fdefines if requested. */
229 /* convert the grammar into the format described in gram.h. */
231 /* free the symbol table data structure
232 since symbols are now all referred to by symbol number. */
237 reader_output_yylsp (FILE *f
)
240 fprintf(f
, LTYPESTR
);
243 /* Read from finput until `%%' is seen. Discard the `%%'. Handle any
244 `%' declarations, and copy the contents of any `%{ ... %}' groups
248 read_declarations (void)
255 c
= skip_white_space();
259 tok
= parse_percent_token();
266 case PERCENT_LEFT_CURLY
:
271 parse_token_decl (STOKEN
, SNTERM
);
275 parse_token_decl (SNTERM
, STOKEN
);
297 parse_assoc_decl(LEFT_ASSOC
);
301 parse_assoc_decl(RIGHT_ASSOC
);
305 parse_assoc_decl(NON_ASSOC
);
308 case SEMANTIC_PARSER
:
309 if (semantic_parser
== 0)
324 warns(_("unrecognized: %s"), token_buffer
);
329 fatal(_("no input grammar"));
333 sprintf(buff
, _("unknown character: %s"), printable_version(c
));
341 /* copy the contents of a %{ ... %} into the definitions file.
342 The %{ has already been read. Return after reading the %}. */
345 copy_definition (void)
350 register int after_percent
; /* -1 while reading a character if prev char was % */
354 fprintf(fattrs
, "#line %d \"%s\"\n", lineno
, infile
);
382 fatal(_("unterminated string at end of file"));
385 warn(_("unterminated string"));
397 fatal(_("unterminated string at end of file"));
412 if (c
!= '*' && c
!= '/')
415 cplus_comment
= (c
== '/');
422 if (!cplus_comment
&& c
== '*')
446 fatal(_("unterminated comment in `%{' definition"));
457 fatal(_("unterminated `%{' definition"));
479 /* parse what comes after %token or %nterm.
480 For %token, what_is is STOKEN and what_is_not is SNTERM.
481 For %nterm, the arguments are reversed. */
484 parse_token_decl (int what_is
, int what_is_not
)
486 register int token
= 0;
487 register char *typename
= 0;
488 register struct bucket
*symbol
= NULL
; /* pts to symbol being defined */
493 int tmp_char
= ungetc (skip_white_space (), finput
);
498 fatals ("Premature EOF after %s", token_buffer
);
506 if (token
== TYPENAME
)
508 k
= strlen(token_buffer
);
509 typename
= NEW2(k
+ 1, char);
510 strcpy(typename
, token_buffer
);
511 value_components_used
= 1;
514 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"'
518 symval
->class = STOKEN
;
519 symval
->type_name
= typename
;
520 symval
->user_token_number
= symbol
->user_token_number
;
521 symbol
->user_token_number
= SALIAS
;
523 symval
->alias
= symbol
;
524 symbol
->alias
= symval
;
527 nsyms
--; /* symbol and symval combined are only one symbol */
529 else if (token
== IDENTIFIER
)
531 int oldclass
= symval
->class;
534 if (symbol
->class == what_is_not
)
535 warns(_("symbol %s redefined"), symbol
->tag
);
536 symbol
->class = what_is
;
537 if (what_is
== SNTERM
&& oldclass
!= SNTERM
)
538 symbol
->value
= nvars
++;
542 if (symbol
->type_name
== NULL
)
543 symbol
->type_name
= typename
;
544 else if (strcmp(typename
, symbol
->type_name
) != 0)
545 warns(_("type redeclaration for %s"), symbol
->tag
);
548 else if (symbol
&& token
== NUMBER
)
550 symbol
->user_token_number
= numval
;
555 warnss(_("`%s' is invalid in %s"),
557 (what_is
== STOKEN
) ? "%token" : "%nterm");
564 /* parse what comes after %thong
566 %thong <type> token number literal
567 the <type> or number may be omitted. The number specifies the
570 Two symbols are entered in the table, one for the token symbol and
571 one for the literal. Both are given the <type>, if any, from the declaration.
572 The ->user_token_number of the first is SALIAS and the ->user_token_number
573 of the second is set to the number, if any, from the declaration.
574 The two symbols are linked via pointers in their ->alias fields.
576 during output_defines_table, the symbol is reported
577 thereafter, only the literal string is retained
578 it is the literal string that is output to yytname
582 parse_thong_decl (void)
585 register struct bucket
*symbol
;
586 register char *typename
= 0;
590 token
= lex(); /* fetch typename or first token */
591 if (token
== TYPENAME
) {
592 k
= strlen(token_buffer
);
593 typename
= NEW2(k
+ 1, char);
594 strcpy(typename
, token_buffer
);
595 value_components_used
= 1;
596 token
= lex(); /* fetch first token */
599 /* process first token */
601 if (token
!= IDENTIFIER
)
603 warns(_("unrecognized item %s, expected an identifier"),
608 symval
->class = STOKEN
;
609 symval
->type_name
= typename
;
610 symval
->user_token_number
= SALIAS
;
613 token
= lex(); /* get number or literal string */
615 if (token
== NUMBER
) {
617 token
= lex(); /* okay, did number, now get literal */
621 /* process literal string token */
623 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
625 warns(_("expected string constant instead of %s"),
630 symval
->class = STOKEN
;
631 symval
->type_name
= typename
;
632 symval
->user_token_number
= usrtoknum
;
634 symval
->alias
= symbol
;
635 symbol
->alias
= symval
;
637 nsyms
--; /* symbol and symval combined are only one symbol */
641 /* parse what comes after %start */
644 parse_start_decl (void)
647 warn(_("multiple %start declarations"));
648 if (lex() != IDENTIFIER
)
649 warn(_("invalid %start declaration"));
659 /* read in a %type declaration and record its information for get_type_name to access */
662 parse_type_decl (void)
667 if (lex() != TYPENAME
)
669 warn(_("%type declaration has no <typename>"));
674 k
= strlen(token_buffer
);
675 name
= NEW2(k
+ 1, char);
676 strcpy(name
, token_buffer
);
681 int tmp_char
= ungetc (skip_white_space (), finput
);
686 fatals ("Premature EOF after %s", token_buffer
);
698 if (symval
->type_name
== NULL
)
699 symval
->type_name
= name
;
700 else if (strcmp(name
, symval
->type_name
) != 0)
701 warns(_("type redeclaration for %s"), symval
->tag
);
706 warns(_("invalid %%type declaration due to item: `%s'"), token_buffer
);
714 /* read in a %left, %right or %nonassoc declaration and record its information. */
715 /* assoc is either LEFT_ASSOC, RIGHT_ASSOC or NON_ASSOC. */
718 parse_assoc_decl (int assoc
)
721 register char *name
= NULL
;
722 register int prev
= 0;
724 lastprec
++; /* Assign a new precedence level, never 0. */
729 int tmp_char
= ungetc (skip_white_space (), finput
);
734 fatals ("Premature EOF after %s", token_buffer
);
742 k
= strlen(token_buffer
);
743 name
= NEW2(k
+ 1, char);
744 strcpy(name
, token_buffer
);
751 if (symval
->prec
!= 0)
752 warns(_("redefining precedence of %s"), symval
->tag
);
753 symval
->prec
= lastprec
;
754 symval
->assoc
= assoc
;
755 if (symval
->class == SNTERM
)
756 warns(_("symbol %s redefined"), symval
->tag
);
757 symval
->class = STOKEN
;
759 { /* record the type, if one is specified */
760 if (symval
->type_name
== NULL
)
761 symval
->type_name
= name
;
762 else if (strcmp(name
, symval
->type_name
) != 0)
763 warns(_("type redeclaration for %s"), symval
->tag
);
768 if (prev
== IDENTIFIER
)
770 symval
->user_token_number
= numval
;
775 warns(_("invalid text (%s) - number should be after identifier"),
785 warns(_("unexpected item: %s"), token_buffer
);
796 /* copy the union declaration into fattrs (and fdefines),
797 where it is made into the
798 definition of YYSTYPE, the type of elements of the parser value stack. */
801 parse_union_decl (void)
805 register int in_comment
;
809 warn(_("multiple %union declarations"));
814 fprintf(fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
816 fprintf(fattrs
, "\n");
818 fprintf(fattrs
, "typedef union");
820 fprintf(fdefines
, "typedef union");
841 if (c
!= '*' && c
!= '/')
848 cplus_comment
= (c
== '/');
867 fatal(_("unterminated comment at end of file"));
869 if (!cplus_comment
&& c
== '*')
893 warn (_("unmatched close-brace (`}')"));
897 fprintf(fattrs
, " YYSTYPE;\n");
899 fprintf(fdefines
, " YYSTYPE;\n");
900 /* JF don't choke on trailing semi */
901 c
=skip_white_space();
902 if(c
!=';') ungetc(c
,finput
);
911 /* parse the declaration %expect N which says to expect N
912 shift-reduce conflicts. */
915 parse_expect_decl (void)
922 while (c
== ' ' || c
== '\t')
926 while (c
>= '0' && c
<= '9')
936 if (count
<= 0 || count
> 10)
937 warn(_("argument of %expect is not an integer"));
938 expected_conflicts
= atoi (buffer
);
941 /* that's all of parsing the declaration section */
943 /* Get the data type (alternative in the union) of the value for symbol n in rule rule. */
946 get_type_name (int n
, symbol_list
*rule
)
948 static char *msg
= N_("invalid $ value");
951 register symbol_list
*rp
;
965 if (rp
== NULL
|| rp
->sym
== NULL
)
973 return (rp
->sym
->type_name
);
977 /* After `%guard' is seen in the input file, copy the actual guard
978 into the guards file. If the guard is followed by an action, copy
979 that into the actions file. STACK_OFFSET is the number of values
980 in the current rule so far, which says where to find `$0' with
981 respect to the top of the stack, for the simple parser in which the
982 stack is not popped until after the guard is run. */
985 copy_guard (symbol_list
*rule
, int stack_offset
)
992 register char *type_name
;
996 /* offset is always 0 if parser has already popped the stack pointer */
997 if (semantic_parser
) stack_offset
= 0;
999 fprintf(fguard
, "\ncase %d:\n", nrules
);
1001 fprintf (fguard
, "#line %d \"%s\"\n", lineno
, infile
);
1007 while (brace_flag
? (count
> 0) : (c
!= ';'))
1028 warn(_("unmatched right brace (`}')"));
1029 c
= getc(finput
); /* skip it */
1042 fatal(_("unterminated string at end of file"));
1045 warn(_("unterminated string"));
1047 c
= match
; /* invent terminator */
1057 fatal(_("unterminated string"));
1072 if (c
!= '*' && c
!= '/')
1075 cplus_comment
= (c
== '/');
1082 if (!cplus_comment
&& c
== '*')
1106 fatal(_("unterminated comment"));
1122 register char *cp
= token_buffer
;
1124 while ((c
= getc(finput
)) != '>' && c
> 0)
1126 if (cp
== token_buffer
+ maxtoken
)
1127 cp
= grow_token_buffer(cp
);
1132 type_name
= token_buffer
;
1139 fprintf(fguard
, "yyval");
1141 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
);
1147 else if (isdigit(c
) || c
== '-')
1150 n
= read_signed_integer (finput
);
1153 if (!type_name
&& n
> 0)
1154 type_name
= get_type_name(n
, rule
);
1156 fprintf(fguard
, "yyvsp[%d]", n
- stack_offset
);
1158 fprintf(fguard
, ".%s", type_name
);
1159 if(!type_name
&& typed
)
1160 warnss(_("$%s of `%s' has no declared type"), int_to_string(n
), rule
->sym
->tag
);
1164 warns(_("$%s is invalid"), printable_version(c
));
1170 if (isdigit(c
) || c
== '-')
1173 n
= read_signed_integer(finput
);
1178 warns(_("@%s is invalid"), printable_version(c
));
1182 fprintf(fguard
, "yylsp[%d]", n
- stack_offset
);
1188 fatal(_("unterminated %%guard clause"));
1194 if (c
!= '}' || count
!= 0)
1198 c
= skip_white_space();
1200 fprintf(fguard
, ";\n break;}");
1202 copy_action(rule
, stack_offset
);
1205 c
= getc(finput
); /* why not skip_white_space -wjh */
1207 copy_action(rule
, stack_offset
);
1215 /* Assuming that a `{' has just been seen, copy everything up to the
1216 matching `}' into the actions file. STACK_OFFSET is the number of
1217 values in the current rule so far, which says where to find `$0'
1218 with respect to the top of the stack. */
1221 copy_action (symbol_list
*rule
, int stack_offset
)
1228 register char *type_name
;
1231 /* offset is always 0 if parser has already popped the stack pointer */
1232 if (semantic_parser
)
1235 fprintf (faction
, "\ncase %d:\n", nrules
);
1237 fprintf (faction
, "#line %d \"%s\"\n", lineno
, infile
);
1238 putc ('{', faction
);
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");
1367 type_name
= get_type_name(0, rule
);
1369 fprintf(faction
, ".%s", type_name
);
1370 if(!type_name
&& typed
)
1371 warns(_("$$ of `%s' has no declared type"),
1374 else if (isdigit(c
) || c
== '-')
1377 n
= read_signed_integer(finput
);
1380 if (!type_name
&& n
> 0)
1381 type_name
= get_type_name(n
, rule
);
1383 fprintf(faction
, "yyvsp[%d]", n
- stack_offset
);
1385 fprintf(faction
, ".%s", type_name
);
1386 if(!type_name
&& typed
)
1387 warnss(_("$%s of `%s' has no declared type"),
1388 int_to_string(n
), rule
->sym
->tag
);
1392 warns(_("$%s is invalid"), printable_version(c
));
1398 if (isdigit(c
) || c
== '-')
1401 n
= read_signed_integer(finput
);
1406 warn(_("invalid @-construct"));
1410 fprintf(faction
, "yylsp[%d]", n
- stack_offset
);
1416 fatal(_("unmatched `{'"));
1425 /* above loop exits when c is '}' */
1434 fprintf(faction
, ";\n break;}");
1439 /* generate a dummy symbol, a nonterminal,
1440 whose name cannot conflict with the user's names. */
1445 register bucket
*sym
;
1447 sprintf (token_buffer
, "@%d", ++gensym_count
);
1448 sym
= getsym(token_buffer
);
1449 sym
->class = SNTERM
;
1450 sym
->value
= nvars
++;
1454 /* Parse the input grammar into a one symbol_list structure.
1455 Each rule is represented by a sequence of symbols: the left hand side
1456 followed by the contents of the right hand side, followed by a null pointer
1457 instead of a symbol to terminate the rule.
1458 The next symbol is the lhs of the following rule.
1460 All guards and actions are copied out to the appropriate files,
1461 labelled by the rule number they apply to. */
1467 register bucket
*lhs
= NULL
;
1468 register symbol_list
*p
;
1469 register symbol_list
*p1
;
1470 register bucket
*bp
;
1472 symbol_list
*crule
; /* points to first symbol_list of current rule. */
1473 /* its symbol is the lhs of the rule. */
1474 symbol_list
*crule1
; /* points to the symbol_list preceding crule. */
1480 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1482 if (t
== IDENTIFIER
|| t
== BAR
)
1484 register int actionflag
= 0;
1485 int rulelength
= 0; /* number of symbols in rhs of this rule so far */
1486 int xactions
= 0; /* JF for error checking */
1487 bucket
*first_rhs
= 0;
1489 if (t
== IDENTIFIER
)
1502 warn(_("ill-formed rule: initial symbol not followed by colon"));
1507 if (nrules
== 0 && t
== BAR
)
1509 warn(_("grammar starts with vertical bar"));
1510 lhs
= symval
; /* BOGUS: use a random symval */
1512 /* start a new rule and record its lhs. */
1517 record_rule_line ();
1519 p
= NEW(symbol_list
);
1531 /* mark the rule's lhs as a nonterminal if not already so. */
1533 if (lhs
->class == SUNKNOWN
)
1535 lhs
->class = SNTERM
;
1539 else if (lhs
->class == STOKEN
)
1540 warns(_("rule given for %s, which is a token"), lhs
->tag
);
1542 /* read the rhs of the rule. */
1550 crule
->ruleprec
= symval
;
1554 if (! (t
== IDENTIFIER
|| t
== LEFT_CURLY
)) break;
1556 /* If next token is an identifier, see if a colon follows it.
1557 If one does, exit this rule now. */
1558 if (t
== IDENTIFIER
)
1560 register bucket
*ssave
;
1567 if (t1
== COLON
) break;
1569 if(!first_rhs
) /* JF */
1571 /* Not followed by colon =>
1572 process as part of this rule's rhs. */
1575 /* If we just passed an action, that action was in the middle
1576 of a rule, so make a dummy rule to reduce it to a
1580 register bucket
*sdummy
;
1582 /* Since the action was written out with this rule's */
1583 /* number, we must give the new rule this number */
1584 /* by inserting the new rule before it. */
1586 /* Make a dummy nonterminal, a gensym. */
1589 /* Make a new rule, whose body is empty,
1590 before the current one, so that the action
1591 just read can belong to it. */
1594 record_rule_line ();
1595 p
= NEW(symbol_list
);
1600 crule1
= NEW(symbol_list
);
1602 crule1
->next
= crule
;
1604 /* insert the dummy generated by that rule into this rule. */
1606 p
= NEW(symbol_list
);
1614 if (t
== IDENTIFIER
)
1617 p
= NEW(symbol_list
);
1622 else /* handle an action. */
1624 copy_action(crule
, rulelength
);
1626 xactions
++; /* JF */
1629 } /* end of read rhs of rule */
1631 /* Put an empty link in the list to mark the end of this rule */
1632 p
= NEW(symbol_list
);
1638 warn(_("two @prec's in a row"));
1640 crule
->ruleprec
= symval
;
1645 if (! semantic_parser
)
1646 warn(_("%%guard present but %%semantic_parser not specified"));
1648 copy_guard(crule
, rulelength
);
1651 else if (t
== LEFT_CURLY
)
1653 /* This case never occurs -wjh */
1654 if (actionflag
) warn(_("two actions at end of one rule"));
1655 copy_action(crule
, rulelength
);
1657 xactions
++; /* -wjh */
1660 /* If $$ is being set in default way,
1661 warn if any type mismatch. */
1662 else if (!xactions
&& first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1664 if (lhs
->type_name
== 0 || first_rhs
->type_name
== 0
1665 || strcmp(lhs
->type_name
,first_rhs
->type_name
))
1666 warnss(_("type clash (`%s' `%s') on default action"),
1667 lhs
->type_name
? lhs
->type_name
: "",
1668 first_rhs
->type_name
? first_rhs
->type_name
: "");
1670 /* Warn if there is no default for $$ but we need one. */
1671 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1672 warn(_("empty rule for typed nonterminal, and no action"));
1677 /* these things can appear as alternatives to rules. */
1679 a) none of the documentation allows them
1680 b) most of them scan forward until finding a next %
1681 thus they may swallow lots of intervening rules
1683 else if (t
== TOKEN
)
1685 parse_token_decl(STOKEN
, SNTERM
);
1688 else if (t
== NTERM
)
1690 parse_token_decl(SNTERM
, STOKEN
);
1697 else if (t
== UNION
)
1702 else if (t
== EXPECT
)
1704 parse_expect_decl();
1707 else if (t
== START
)
1716 warns(_("invalid input: %s"), token_buffer
);
1721 /* grammar has been read. Do some checking */
1723 if (nsyms
> MAXSHORT
)
1724 fatals(_("too many symbols (tokens plus nonterminals); maximum %s"),
1725 int_to_string(MAXSHORT
));
1727 fatal(_("no rules in the input grammar"));
1729 if (typed
== 0 /* JF put out same default YYSTYPE as YACC does */
1730 && !value_components_used
)
1732 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1733 but it seems better to be consistent.
1734 Most programs should declare their own type anyway. */
1735 fprintf(fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1737 fprintf(fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1740 /* Report any undefined symbols and consider them nonterminals. */
1742 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1743 if (bp
->class == SUNKNOWN
)
1745 warns(_("symbol %s is used, but is not defined as a token and has no rules"),
1748 bp
->value
= nvars
++;
1751 ntokens
= nsyms
- nvars
;
1756 record_rule_line (void)
1758 /* Record each rule's source line number in rline table. */
1760 if (nrules
>= rline_allocated
)
1762 rline_allocated
= nrules
* 2;
1763 rline
= (short *) xrealloc ((char *) rline
,
1764 rline_allocated
* sizeof (short));
1766 rline
[nrules
] = lineno
;
1771 /* read in a %type declaration and record its information for get_type_name to access */
1772 /* this is unused. it is only called from the #if 0 part of readgram */
1778 register char *name
;
1784 warn(_("ill-formed %type declaration"));
1788 k
= strlen(token_buffer
);
1789 name
= NEW2(k
+ 1, char);
1790 strcpy(name
, token_buffer
);
1805 if (symval
->type_name
== NULL
)
1806 symval
->type_name
= name
;
1807 else if (strcmp(name
, symval
->type_name
) != 0)
1808 warns(_("type redeclaration for %s"), symval
->tag
);
1820 /* assign symbol numbers, and write definition of token names into fdefines.
1821 Set up vectors tags and sprec of names and precedences of symbols. */
1826 register bucket
*bp
;
1827 register int tokno
= 1;
1829 register int last_user_token_number
;
1831 /* int lossage = 0; JF set but not used */
1833 tags
= NEW2(nsyms
+ 1, char *);
1835 user_toknums
= NEW2(nsyms
+ 1, int);
1836 user_toknums
[0] = 0;
1838 sprec
= NEW2(nsyms
, short);
1839 sassoc
= NEW2(nsyms
, short);
1841 max_user_token_number
= 256;
1842 last_user_token_number
= 256;
1844 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1846 if (bp
->class == SNTERM
)
1848 bp
->value
+= ntokens
;
1852 /* this symbol and its alias are a single token defn.
1853 allocate a tokno, and assign to both
1854 check agreement of ->prec and ->assoc fields
1855 and make both the same
1858 bp
->value
= bp
->alias
->value
= tokno
++;
1860 if (bp
->prec
!= bp
->alias
->prec
) {
1861 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1862 && bp
->user_token_number
== SALIAS
)
1863 warnss(_("conflicting precedences for %s and %s"),
1864 bp
->tag
, bp
->alias
->tag
);
1865 if (bp
->prec
!= 0) bp
->alias
->prec
= bp
->prec
;
1866 else bp
->prec
= bp
->alias
->prec
;
1869 if (bp
->assoc
!= bp
->alias
->assoc
) {
1870 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1871 && bp
->user_token_number
== SALIAS
)
1872 warnss(_("conflicting assoc values for %s and %s"),
1873 bp
->tag
, bp
->alias
->tag
);
1874 if (bp
->assoc
!= 0) bp
->alias
->assoc
= bp
->assoc
;
1875 else bp
->assoc
= bp
->alias
->assoc
;
1878 if (bp
->user_token_number
== SALIAS
)
1879 continue; /* do not do processing below for SALIASs */
1882 else /* bp->class == STOKEN */
1884 bp
->value
= tokno
++;
1887 if (bp
->class == STOKEN
)
1889 if (translations
&& !(bp
->user_token_number
))
1890 bp
->user_token_number
= ++last_user_token_number
;
1891 if (bp
->user_token_number
> max_user_token_number
)
1892 max_user_token_number
= bp
->user_token_number
;
1895 tags
[bp
->value
] = bp
->tag
;
1896 user_toknums
[bp
->value
] = bp
->user_token_number
;
1897 sprec
[bp
->value
] = bp
->prec
;
1898 sassoc
[bp
->value
] = bp
->assoc
;
1906 token_translations
= NEW2(max_user_token_number
+1, short);
1908 /* initialize all entries for literal tokens to 2,
1909 the internal token number for $undefined.,
1910 which represents all invalid inputs. */
1911 for (i
= 0; i
<= max_user_token_number
; i
++)
1912 token_translations
[i
] = 2;
1914 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1916 if (bp
->value
>= ntokens
) continue; /* non-terminal */
1917 if (bp
->user_token_number
== SALIAS
) continue;
1918 if (token_translations
[bp
->user_token_number
] != 2)
1919 warnsss(_("tokens %s and %s both assigned number %s"),
1920 tags
[token_translations
[bp
->user_token_number
]],
1922 int_to_string(bp
->user_token_number
));
1923 token_translations
[bp
->user_token_number
] = bp
->value
;
1927 error_token_number
= errtoken
->value
;
1930 output_token_defines(ftable
);
1932 if (startval
->class == SUNKNOWN
)
1933 fatals(_("the start symbol %s is undefined"), startval
->tag
);
1934 else if (startval
->class == STOKEN
)
1935 fatals(_("the start symbol %s is a token"), startval
->tag
);
1937 start_symbol
= startval
->value
;
1941 output_token_defines(fdefines
);
1945 if (spec_name_prefix
)
1946 fprintf(fdefines
, "\nextern YYSTYPE %slval;\n", spec_name_prefix
);
1948 fprintf(fdefines
, "\nextern YYSTYPE yylval;\n");
1951 if (semantic_parser
)
1952 for (i
= ntokens
; i
< nsyms
; i
++)
1954 /* don't make these for dummy nonterminals made by gensym. */
1955 if (*tags
[i
] != '@')
1956 fprintf(fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1959 /* `fdefines' is now a temporary file, so we need to copy its
1960 contents in `done', so we can't close it here. */
1967 /* For named tokens, but not literal ones, define the name.
1968 The value is the user token number.
1971 output_token_defines (FILE *file
)
1974 register char *cp
, *symbol
;
1977 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1979 symbol
= bp
->tag
; /* get symbol */
1981 if (bp
->value
>= ntokens
) continue;
1982 if (bp
->user_token_number
== SALIAS
) continue;
1983 if ('\'' == *symbol
) continue; /* skip literal character */
1984 if (bp
== errtoken
) continue; /* skip error token */
1985 if ('\"' == *symbol
)
1987 /* use literal string only if given a symbol with an alias */
1989 symbol
= bp
->alias
->tag
;
1994 /* Don't #define nonliteral tokens whose names contain periods. */
1996 while ((c
= *cp
++) && c
!= '.');
1997 if (c
!= '\0') continue;
1999 fprintf(file
, "#define\t%s\t%d\n", symbol
,
2000 ((translations
&& ! rawtoknumflag
)
2001 ? bp
->user_token_number
2003 if (semantic_parser
)
2004 fprintf(file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
2012 /* convert the rules into the representation using rrhs, rlhs and ritems. */
2017 register int itemno
;
2018 register int ruleno
;
2019 register symbol_list
*p
;
2020 /* register bucket *bp; JF unused */
2024 ritem
= NEW2(nitems
+ 1, short);
2025 rlhs
= NEW2(nrules
, short) - 1;
2026 rrhs
= NEW2(nrules
, short) - 1;
2027 rprec
= NEW2(nrules
, short) - 1;
2028 rprecsym
= NEW2(nrules
, short) - 1;
2029 rassoc
= NEW2(nrules
, short) - 1;
2037 rlhs
[ruleno
] = p
->sym
->value
;
2038 rrhs
[ruleno
] = itemno
;
2039 ruleprec
= p
->ruleprec
;
2044 ritem
[itemno
++] = p
->sym
->value
;
2045 /* A rule gets by default the precedence and associativity
2046 of the last token in it. */
2047 if (p
->sym
->class == STOKEN
)
2049 rprec
[ruleno
] = p
->sym
->prec
;
2050 rassoc
[ruleno
] = p
->sym
->assoc
;
2055 /* If this rule has a %prec,
2056 the specified symbol's precedence replaces the default. */
2059 rprec
[ruleno
] = ruleprec
->prec
;
2060 rassoc
[ruleno
] = ruleprec
->assoc
;
2061 rprecsym
[ruleno
] = ruleprec
->value
;
2064 ritem
[itemno
++] = -ruleno
;
2073 /* Read a signed integer from STREAM and return its value. */
2076 read_signed_integer (FILE *stream
)
2078 register int c
= getc(stream
);
2079 register int sign
= 1;
2090 n
= 10*n
+ (c
- '0');