]>
git.saurik.com Git - bison.git/blob - src/reader.c
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 (). */
57 #define YYLTYPE yyltype\n\
61 /* Number of slots allocated (but not necessarily used yet) in `rline' */
62 static int rline_allocated
;
64 extern bucket
*symval
;
66 extern int expected_conflicts
;
67 extern char *token_buffer
;
70 extern void init_lex
PARAMS((void));
71 extern char *grow_token_buffer
PARAMS((char *));
72 extern void tabinit
PARAMS((void));
73 extern void output_headers
PARAMS((void));
74 extern void output_trailers
PARAMS((void));
75 extern void free_symtab
PARAMS((void));
76 extern void open_extra_files
PARAMS((void));
77 extern char *printable_version
PARAMS((int));
78 extern void unlex
PARAMS((int));
80 extern int skip_white_space
PARAMS((void));
81 extern int parse_percent_token
PARAMS((void));
82 extern int lex
PARAMS((void));
87 struct symbol_list
*next
;
94 extern void reader
PARAMS((void));
95 extern void reader_output_yylsp
PARAMS((FILE *));
97 static void read_declarations
PARAMS((void));
98 static void copy_definition
PARAMS((void));
99 static void parse_token_decl
PARAMS((int, int));
100 static void parse_start_decl
PARAMS((void));
101 static void parse_type_decl
PARAMS((void));
102 static void parse_assoc_decl
PARAMS((int));
103 static void parse_union_decl
PARAMS((void));
104 static void parse_expect_decl
PARAMS((void));
105 static char *get_type_name
PARAMS((int, symbol_list
*));
106 static void copy_guard
PARAMS((symbol_list
*, int));
107 static void parse_thong_decl
PARAMS((void));
108 static void copy_action
PARAMS((symbol_list
*, int));
109 static bucket
*gensym
PARAMS((void));
110 static void readgram
PARAMS((void));
111 static void record_rule_line
PARAMS((void));
112 static void packsymbols
PARAMS((void));
113 static void output_token_defines
PARAMS((FILE *));
114 static void packgram
PARAMS((void));
117 static int get_type
PARAMS((void));
123 static symbol_list
*grammar
;
124 static int start_flag
;
125 static bucket
*startval
;
127 /* Nonzero if components of semantic values are used, implying
128 they must be unions. */
129 static int value_components_used
;
131 static int typed
; /* nonzero if %union has been seen. */
133 static int lastprec
; /* incremented for each %left, %right or %nonassoc seen */
135 static int gensym_count
; /* incremented for each generated symbol */
137 static bucket
*errtoken
;
138 static bucket
*undeftoken
;
140 /* Nonzero if any action or guard uses the @n construct. */
141 static int yylsp_needed
;
144 /*===================\
145 | Low level lexing. |
146 \===================*/
149 skip_to_char (int target
)
153 complain (_(" Skipping to next \\n"));
155 complain (_(" Skipping to next %c"), target
);
158 c
= skip_white_space ();
159 while (c
!= target
&& c
!= EOF
);
165 /*---------------------------------------------------------.
166 | Read a signed integer from STREAM and return its value. |
167 `---------------------------------------------------------*/
170 read_signed_integer (FILE *stream
)
172 register int c
= getc (stream
);
173 register int sign
= 1;
184 n
= 10 * n
+ (c
- '0');
193 /*-------------------------------------------------------------------.
194 | Dump the string from FINPUT to FOUTPUT. MATCH is the delimiter of |
195 | the string (either ' or "). |
196 `-------------------------------------------------------------------*/
199 copy_string (FILE *fin
, FILE *fout
, int match
)
209 fatal (_("unterminated string at end of file"));
212 complain (_("unterminated string"));
214 c
= match
; /* invent terminator */
224 fatal (_("unterminated string at end of file"));
237 /* Dump the comment from IN to OUT1 and OUT2. C is either `*' or `/',
238 depending upon the type of comments used. OUT2 might be NULL. */
241 copy_comment2 (FILE *in
, FILE *out1
, FILE* out2
, int c
)
246 cplus_comment
= (c
== '/');
255 if (!cplus_comment
&& c
== '*')
285 fatal (_("unterminated comment"));
297 /* Dump the comment from FIN to FOUT. C is either `*' or `/',
298 depending upon the type of comments used. */
301 copy_comment (FILE *fin
, FILE *fout
, int c
)
303 copy_comment2 (fin
, fout
, NULL
, c
);
311 startval
= NULL
; /* start symbol not specified yet. */
314 /* initially assume token number translation not needed. */
317 /* Nowadays translations is always set to 1, since we give `error' a
318 user-token-number to satisfy the Posix demand for YYERRCODE==256.
326 rline_allocated
= 10;
327 rline
= NEW2 (rline_allocated
, short);
343 /* Initialize the symbol table. */
345 /* Construct the error token */
346 errtoken
= getsym ("error");
347 errtoken
->class = STOKEN
;
348 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
349 /* Construct a token that represents all undefined literal tokens.
350 It is always token number 2. */
351 undeftoken
= getsym ("$undefined.");
352 undeftoken
->class = STOKEN
;
353 undeftoken
->user_token_number
= 2;
355 /* Read the declaration section. Copy %{ ... %} groups to FTABLE
356 and FDEFINES file. Also notice any %token, %left, etc. found
360 /* %s, made from %s\n\
361 by GNU bison %s. */\n\
363 noparserflag
? "Bison-generated parse tables" : "A Bison parser",
367 fputs ("#define YYBISON 1 /* Identify Bison output. */\n\n", ftable
);
368 read_declarations ();
369 /* Start writing the guard and action files, if they are needed. */
371 /* Read in the grammar, build grammar in list form. Write out
372 guards and actions. */
374 /* Now we know whether we need the line-number stack. If we do,
375 write its type into the .tab.h file. */
377 reader_output_yylsp (fdefines
);
378 /* Write closing delimiters for actions and guards. */
381 fputs ("#define YYLSP_NEEDED\n\n", ftable
);
382 /* Assign the symbols their symbol numbers. Write #defines for the
383 token symbols into FDEFINES if requested. */
385 /* Convert the grammar into the format described in gram.h. */
387 /* Free the symbol table data structure since symbols are now all
388 referred to by symbol number. */
393 reader_output_yylsp (FILE *f
)
396 fprintf(f
, LTYPESTR
);
399 /* Read from finput until `%%' is seen. Discard the `%%'. Handle any
400 `%' declarations, and copy the contents of any `%{ ... %}' groups
404 read_declarations (void)
411 c
= skip_white_space();
415 tok
= parse_percent_token();
422 case PERCENT_LEFT_CURLY
:
427 parse_token_decl (STOKEN
, SNTERM
);
431 parse_token_decl (SNTERM
, STOKEN
);
453 parse_assoc_decl(LEFT_ASSOC
);
457 parse_assoc_decl(RIGHT_ASSOC
);
461 parse_assoc_decl(NON_ASSOC
);
464 case SEMANTIC_PARSER
:
465 if (semantic_parser
== 0)
480 complain (_("unrecognized: %s"), token_buffer
);
485 fatal (_("no input grammar"));
488 complain (_("unknown character: %s"), printable_version(c
));
495 /* Copy the contents of a `%{ ... %}' into the definitions file. The
496 `%{' has already been read. Return after reading the `%}'. */
499 copy_definition (void)
502 /* -1 while reading a character if prev char was %. */
503 register int after_percent
;
506 fprintf(fattrs
, "#line %d \"%s\"\n", lineno
, infile
);
527 copy_string (finput
, fattrs
, c
);
533 if (c
!= '*' && c
!= '/')
535 copy_comment (finput
, fattrs
, c
);
540 _("unterminated `%{' definition"));
562 /* parse what comes after %token or %nterm.
563 For %token, what_is is STOKEN and what_is_not is SNTERM.
564 For %nterm, the arguments are reversed. */
567 parse_token_decl (int what_is
, int what_is_not
)
569 register int token
= 0;
570 register char *typename
= 0;
571 register struct bucket
*symbol
= NULL
; /* pts to symbol being defined */
576 int tmp_char
= ungetc (skip_white_space (), finput
);
581 fatal (_("Premature EOF after %s"), token_buffer
);
589 if (token
== TYPENAME
)
591 k
= strlen(token_buffer
);
592 typename
= NEW2(k
+ 1, char);
593 strcpy(typename
, token_buffer
);
594 value_components_used
= 1;
597 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"'
601 symval
->class = STOKEN
;
602 symval
->type_name
= typename
;
603 symval
->user_token_number
= symbol
->user_token_number
;
604 symbol
->user_token_number
= SALIAS
;
606 symval
->alias
= symbol
;
607 symbol
->alias
= symval
;
610 nsyms
--; /* symbol and symval combined are only one symbol */
612 else if (token
== IDENTIFIER
)
614 int oldclass
= symval
->class;
617 if (symbol
->class == what_is_not
)
618 complain (_("symbol %s redefined"), symbol
->tag
);
619 symbol
->class = what_is
;
620 if (what_is
== SNTERM
&& oldclass
!= SNTERM
)
621 symbol
->value
= nvars
++;
625 if (symbol
->type_name
== NULL
)
626 symbol
->type_name
= typename
;
627 else if (strcmp(typename
, symbol
->type_name
) != 0)
628 complain (_("type redeclaration for %s"), symbol
->tag
);
631 else if (symbol
&& token
== NUMBER
)
633 symbol
->user_token_number
= numval
;
638 complain (_("`%s' is invalid in %s"),
640 (what_is
== STOKEN
) ? "%token" : "%nterm");
647 /* parse what comes after %thong
649 %thong <type> token number literal
650 the <type> or number may be omitted. The number specifies the
653 Two symbols are entered in the table, one for the token symbol and
654 one for the literal. Both are given the <type>, if any, from the declaration.
655 The ->user_token_number of the first is SALIAS and the ->user_token_number
656 of the second is set to the number, if any, from the declaration.
657 The two symbols are linked via pointers in their ->alias fields.
659 during output_defines_table, the symbol is reported
660 thereafter, only the literal string is retained
661 it is the literal string that is output to yytname
665 parse_thong_decl (void)
668 register struct bucket
*symbol
;
669 register char *typename
= 0;
673 token
= lex(); /* fetch typename or first token */
674 if (token
== TYPENAME
) {
675 k
= strlen(token_buffer
);
676 typename
= NEW2(k
+ 1, char);
677 strcpy(typename
, token_buffer
);
678 value_components_used
= 1;
679 token
= lex(); /* fetch first token */
682 /* process first token */
684 if (token
!= IDENTIFIER
)
686 complain (_("unrecognized item %s, expected an identifier"),
691 symval
->class = STOKEN
;
692 symval
->type_name
= typename
;
693 symval
->user_token_number
= SALIAS
;
696 token
= lex(); /* get number or literal string */
698 if (token
== NUMBER
) {
700 token
= lex(); /* okay, did number, now get literal */
704 /* process literal string token */
706 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
708 complain (_("expected string constant instead of %s"),
713 symval
->class = STOKEN
;
714 symval
->type_name
= typename
;
715 symval
->user_token_number
= usrtoknum
;
717 symval
->alias
= symbol
;
718 symbol
->alias
= symval
;
720 nsyms
--; /* symbol and symval combined are only one symbol */
724 /* Parse what comes after %start */
727 parse_start_decl (void)
730 complain (_("multiple %s declarations"), "%start");
731 if (lex () != IDENTIFIER
)
732 complain (_("invalid %s declaration"), "%start");
742 /* read in a %type declaration and record its information for get_type_name to access */
745 parse_type_decl (void)
750 if (lex() != TYPENAME
)
752 complain ("%s", _("%type declaration has no <typename>"));
757 k
= strlen(token_buffer
);
758 name
= NEW2(k
+ 1, char);
759 strcpy(name
, token_buffer
);
764 int tmp_char
= ungetc (skip_white_space (), finput
);
769 fatal (_("Premature EOF after %s"), token_buffer
);
781 if (symval
->type_name
== NULL
)
782 symval
->type_name
= name
;
783 else if (strcmp(name
, symval
->type_name
) != 0)
784 complain (_("type redeclaration for %s"), symval
->tag
);
789 complain (_("invalid %%type declaration due to item: %s"),
798 /* read in a %left, %right or %nonassoc declaration and record its information. */
799 /* assoc is either LEFT_ASSOC, RIGHT_ASSOC or NON_ASSOC. */
802 parse_assoc_decl (int assoc
)
805 register char *name
= NULL
;
806 register int prev
= 0;
808 lastprec
++; /* Assign a new precedence level, never 0. */
813 int tmp_char
= ungetc (skip_white_space (), finput
);
818 fatal (_("Premature EOF after %s"), token_buffer
);
826 k
= strlen(token_buffer
);
827 name
= NEW2(k
+ 1, char);
828 strcpy(name
, token_buffer
);
835 if (symval
->prec
!= 0)
836 complain (_("redefining precedence of %s"), symval
->tag
);
837 symval
->prec
= lastprec
;
838 symval
->assoc
= assoc
;
839 if (symval
->class == SNTERM
)
840 complain (_("symbol %s redefined"), symval
->tag
);
841 symval
->class = STOKEN
;
843 { /* record the type, if one is specified */
844 if (symval
->type_name
== NULL
)
845 symval
->type_name
= name
;
846 else if (strcmp(name
, symval
->type_name
) != 0)
847 complain (_("type redeclaration for %s"), symval
->tag
);
852 if (prev
== IDENTIFIER
)
854 symval
->user_token_number
= numval
;
859 complain (_("invalid text (%s) - number should be after identifier"),
869 complain (_("unexpected item: %s"), token_buffer
);
880 /* copy the union declaration into fattrs (and fdefines),
881 where it is made into the
882 definition of YYSTYPE, the type of elements of the parser value stack. */
885 parse_union_decl (void)
888 register int count
= 0;
891 complain (_("multiple %s declarations"), "%union");
896 fprintf (fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
898 fprintf (fattrs
, "\n");
900 fprintf (fattrs
, "typedef union");
902 fprintf (fdefines
, "typedef union");
920 if (c
!= '*' && c
!= '/')
922 copy_comment2 (finput
, fattrs
, fdefines
, c
);
932 complain (_("unmatched %s"), "`}'");
936 fprintf (fattrs
, " YYSTYPE;\n");
938 fprintf (fdefines
, " YYSTYPE;\n");
939 /* JF don't choke on trailing semi */
940 c
= skip_white_space ();
951 /* parse the declaration %expect N which says to expect N
952 shift-reduce conflicts. */
955 parse_expect_decl (void)
962 while (c
== ' ' || c
== '\t')
966 while (c
>= '0' && c
<= '9')
976 if (count
<= 0 || count
> 10)
977 complain ("%s", _("argument of %expect is not an integer"));
978 expected_conflicts
= atoi (buffer
);
981 /* that's all of parsing the declaration section */
983 /* FIN is pointing to a location (i.e., a `@'). Output to FOUT
984 a reference to this location. STACK_OFFSET is the number of values
985 in the current rule so far, which says where to find `$0' with
986 respect to the top of the stack. */
988 copy_at (FILE *fin
, FILE *fout
, int stack_offset
)
995 fprintf (fout
, "yyloc");
998 else if (isdigit(c
) || c
== '-')
1003 n
= read_signed_integer (fin
);
1005 fprintf (fout
, "yylsp[%d]", n
- stack_offset
);
1009 complain (_("@%s is invalid"), printable_version (c
));
1013 /* Get the data type (alternative in the union) of the value for
1014 symbol n in rule rule. */
1017 get_type_name (int n
, symbol_list
*rule
)
1020 register symbol_list
*rp
;
1024 complain (_("invalid $ value"));
1034 if (rp
== NULL
|| rp
->sym
== NULL
)
1036 complain (_("invalid $ value"));
1042 return rp
->sym
->type_name
;
1047 /* After `%guard' is seen in the input file, copy the actual guard
1048 into the guards file. If the guard is followed by an action, copy
1049 that into the actions file. STACK_OFFSET is the number of values
1050 in the current rule so far, which says where to find `$0' with
1051 respect to the top of the stack, for the simple parser in which the
1052 stack is not popped until after the guard is run. */
1055 copy_guard (symbol_list
*rule
, int stack_offset
)
1060 register char *type_name
;
1063 /* offset is always 0 if parser has already popped the stack pointer */
1064 if (semantic_parser
) stack_offset
= 0;
1066 fprintf(fguard
, "\ncase %d:\n", nrules
);
1068 fprintf (fguard
, "#line %d \"%s\"\n", lineno
, infile
);
1074 while (brace_flag
? (count
> 0) : (c
!= ';'))
1095 complain (_("unmatched %s"), "`}'");
1096 c
= getc(finput
); /* skip it */
1102 copy_string (finput
, fguard
, c
);
1108 if (c
!= '*' && c
!= '/')
1110 copy_comment (finput
, fguard
, c
);
1119 register char *cp
= token_buffer
;
1121 while ((c
= getc(finput
)) != '>' && c
> 0)
1123 if (cp
== token_buffer
+ maxtoken
)
1124 cp
= grow_token_buffer(cp
);
1129 type_name
= token_buffer
;
1136 fprintf(fguard
, "yyval");
1138 type_name
= rule
->sym
->type_name
;
1140 fprintf(fguard
, ".%s", type_name
);
1141 if(!type_name
&& typed
)
1142 complain (_("$$ of `%s' has no declared type"),
1145 else if (isdigit(c
) || c
== '-')
1148 n
= read_signed_integer (finput
);
1151 if (!type_name
&& n
> 0)
1152 type_name
= get_type_name(n
, rule
);
1154 fprintf(fguard
, "yyvsp[%d]", n
- stack_offset
);
1156 fprintf(fguard
, ".%s", type_name
);
1157 if (!type_name
&& typed
)
1158 complain (_("$%d of `%s' has no declared type"),
1163 complain (_("$%s is invalid"), printable_version (c
));
1167 copy_at (finput
, fguard
, stack_offset
);
1172 _("unterminated %guard clause"));
1178 if (c
!= '}' || count
!= 0)
1182 c
= skip_white_space();
1184 fprintf(fguard
, ";\n break;}");
1186 copy_action (rule
, stack_offset
);
1189 c
= getc(finput
); /* why not skip_white_space -wjh */
1191 copy_action (rule
, stack_offset
);
1199 /* Assuming that a `{' has just been seen, copy everything up to the
1200 matching `}' into the actions file. STACK_OFFSET is the number of
1201 values in the current rule so far, which says where to find `$0'
1202 with respect to the top of the stack. */
1205 copy_action (symbol_list
*rule
, int stack_offset
)
1210 register char *type_name
;
1212 /* offset is always 0 if parser has already popped the stack pointer */
1213 if (semantic_parser
)
1216 fprintf (faction
, "\ncase %d:\n", nrules
);
1218 fprintf (faction
, "#line %d \"%s\"\n", lineno
, infile
);
1219 putc ('{', faction
);
1242 copy_string (finput
, faction
, c
);
1248 if (c
!= '*' && c
!= '/')
1250 copy_comment (finput
, faction
, c
);
1259 register char *cp
= token_buffer
;
1261 while ((c
= getc(finput
)) != '>' && c
> 0)
1263 if (cp
== token_buffer
+ maxtoken
)
1264 cp
= grow_token_buffer(cp
);
1269 type_name
= token_buffer
;
1270 value_components_used
= 1;
1276 fprintf(faction
, "yyval");
1278 type_name
= get_type_name(0, rule
);
1280 fprintf(faction
, ".%s", type_name
);
1281 if(!type_name
&& typed
)
1282 complain (_("$$ of `%s' has no declared type"),
1285 else if (isdigit(c
) || c
== '-')
1288 n
= read_signed_integer(finput
);
1291 if (!type_name
&& n
> 0)
1292 type_name
= get_type_name(n
, rule
);
1294 fprintf(faction
, "yyvsp[%d]", n
- stack_offset
);
1296 fprintf(faction
, ".%s", type_name
);
1297 if(!type_name
&& typed
)
1298 complain (_("$%d of `%s' has no declared type"),
1303 complain (_("$%s is invalid"), printable_version (c
));
1308 copy_at (finput
, faction
, stack_offset
);
1312 fatal (_("unmatched %s"), "`{'");
1321 /* above loop exits when c is '}' */
1330 fprintf(faction
, ";\n break;}");
1335 /* generate a dummy symbol, a nonterminal,
1336 whose name cannot conflict with the user's names. */
1341 register bucket
*sym
;
1343 sprintf (token_buffer
, "@%d", ++gensym_count
);
1344 sym
= getsym(token_buffer
);
1345 sym
->class = SNTERM
;
1346 sym
->value
= nvars
++;
1350 /* Parse the input grammar into a one symbol_list structure.
1351 Each rule is represented by a sequence of symbols: the left hand side
1352 followed by the contents of the right hand side, followed by a null pointer
1353 instead of a symbol to terminate the rule.
1354 The next symbol is the lhs of the following rule.
1356 All guards and actions are copied out to the appropriate files,
1357 labelled by the rule number they apply to. */
1363 register bucket
*lhs
= NULL
;
1364 register symbol_list
*p
;
1365 register symbol_list
*p1
;
1366 register bucket
*bp
;
1368 symbol_list
*crule
; /* points to first symbol_list of current rule. */
1369 /* its symbol is the lhs of the rule. */
1370 symbol_list
*crule1
; /* points to the symbol_list preceding crule. */
1376 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1378 if (t
== IDENTIFIER
|| t
== BAR
)
1380 register int actionflag
= 0;
1381 int rulelength
= 0; /* number of symbols in rhs of this rule so far */
1382 int xactions
= 0; /* JF for error checking */
1383 bucket
*first_rhs
= 0;
1385 if (t
== IDENTIFIER
)
1398 complain (_("ill-formed rule: initial symbol not followed by colon"));
1403 if (nrules
== 0 && t
== BAR
)
1405 complain (_("grammar starts with vertical bar"));
1406 lhs
= symval
; /* BOGUS: use a random symval */
1408 /* start a new rule and record its lhs. */
1413 record_rule_line ();
1415 p
= NEW(symbol_list
);
1427 /* mark the rule's lhs as a nonterminal if not already so. */
1429 if (lhs
->class == SUNKNOWN
)
1431 lhs
->class = SNTERM
;
1435 else if (lhs
->class == STOKEN
)
1436 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1438 /* read the rhs of the rule. */
1446 crule
->ruleprec
= symval
;
1450 if (! (t
== IDENTIFIER
|| t
== LEFT_CURLY
)) break;
1452 /* If next token is an identifier, see if a colon follows it.
1453 If one does, exit this rule now. */
1454 if (t
== IDENTIFIER
)
1456 register bucket
*ssave
;
1463 if (t1
== COLON
) break;
1465 if(!first_rhs
) /* JF */
1467 /* Not followed by colon =>
1468 process as part of this rule's rhs. */
1471 /* If we just passed an action, that action was in the middle
1472 of a rule, so make a dummy rule to reduce it to a
1476 register bucket
*sdummy
;
1478 /* Since the action was written out with this rule's */
1479 /* number, we must give the new rule this number */
1480 /* by inserting the new rule before it. */
1482 /* Make a dummy nonterminal, a gensym. */
1485 /* Make a new rule, whose body is empty,
1486 before the current one, so that the action
1487 just read can belong to it. */
1490 record_rule_line ();
1491 p
= NEW(symbol_list
);
1496 crule1
= NEW(symbol_list
);
1498 crule1
->next
= crule
;
1500 /* insert the dummy generated by that rule into this rule. */
1502 p
= NEW(symbol_list
);
1510 if (t
== IDENTIFIER
)
1513 p
= NEW(symbol_list
);
1518 else /* handle an action. */
1520 copy_action(crule
, rulelength
);
1522 xactions
++; /* JF */
1525 } /* end of read rhs of rule */
1527 /* Put an empty link in the list to mark the end of this rule */
1528 p
= NEW(symbol_list
);
1534 complain (_("two @prec's in a row"));
1536 crule
->ruleprec
= symval
;
1541 if (! semantic_parser
)
1543 _("%guard present but %semantic_parser not specified"));
1545 copy_guard(crule
, rulelength
);
1548 else if (t
== LEFT_CURLY
)
1550 /* This case never occurs -wjh */
1552 complain (_("two actions at end of one rule"));
1553 copy_action(crule
, rulelength
);
1555 xactions
++; /* -wjh */
1558 /* If $$ is being set in default way, report if any type
1562 && lhs
->type_name
!= first_rhs
->type_name
)
1564 if (lhs
->type_name
== 0
1565 || first_rhs
->type_name
== 0
1566 || strcmp(lhs
->type_name
,first_rhs
->type_name
))
1567 complain (_("type clash (`%s' `%s') on default action"),
1568 lhs
->type_name
? lhs
->type_name
: "",
1569 first_rhs
->type_name
? first_rhs
->type_name
: "");
1571 /* Warn if there is no default for $$ but we need one. */
1572 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1573 complain (_("empty rule for typed nonterminal, and no action"));
1578 /* these things can appear as alternatives to rules. */
1580 a) none of the documentation allows them
1581 b) most of them scan forward until finding a next %
1582 thus they may swallow lots of intervening rules
1584 else if (t
== TOKEN
)
1586 parse_token_decl(STOKEN
, SNTERM
);
1589 else if (t
== NTERM
)
1591 parse_token_decl(SNTERM
, STOKEN
);
1598 else if (t
== UNION
)
1603 else if (t
== EXPECT
)
1605 parse_expect_decl();
1608 else if (t
== START
)
1617 complain (_("invalid input: %s"), token_buffer
);
1622 /* grammar has been read. Do some checking */
1624 if (nsyms
> MAXSHORT
)
1625 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1628 fatal (_("no rules in the input grammar"));
1630 if (typed
== 0 /* JF put out same default YYSTYPE as YACC does */
1631 && !value_components_used
)
1633 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1634 but it seems better to be consistent.
1635 Most programs should declare their own type anyway. */
1636 fprintf(fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1638 fprintf(fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1641 /* Report any undefined symbols and consider them nonterminals. */
1643 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1644 if (bp
->class == SUNKNOWN
)
1646 complain (_("symbol %s is used, but is not defined as a token and has no rules"),
1649 bp
->value
= nvars
++;
1652 ntokens
= nsyms
- nvars
;
1657 record_rule_line (void)
1659 /* Record each rule's source line number in rline table. */
1661 if (nrules
>= rline_allocated
)
1663 rline_allocated
= nrules
* 2;
1664 rline
= (short *) xrealloc ((char *) rline
,
1665 rline_allocated
* sizeof (short));
1667 rline
[nrules
] = lineno
;
1672 /* read in a %type declaration and record its information for get_type_name to access */
1673 /* this is unused. it is only called from the #if 0 part of readgram */
1679 register char *name
;
1685 complain (_("invalid %s declaration"), "%type");
1689 k
= strlen(token_buffer
);
1690 name
= NEW2(k
+ 1, char);
1691 strcpy(name
, token_buffer
);
1706 if (symval
->type_name
== NULL
)
1707 symval
->type_name
= name
;
1708 else if (strcmp(name
, symval
->type_name
) != 0)
1709 complain (_("type redeclaration for %s"), symval
->tag
);
1721 /* Assign symbol numbers, and write definition of token names into
1722 fdefines. Set up vectors tags and sprec of names and precedences
1728 register bucket
*bp
;
1729 register int tokno
= 1;
1731 register int last_user_token_number
;
1732 static char DOLLAR
[] = "$";
1734 /* int lossage = 0; JF set but not used */
1736 tags
= NEW2(nsyms
+ 1, char *);
1738 user_toknums
= NEW2(nsyms
+ 1, int);
1739 user_toknums
[0] = 0;
1741 sprec
= NEW2(nsyms
, short);
1742 sassoc
= NEW2(nsyms
, short);
1744 max_user_token_number
= 256;
1745 last_user_token_number
= 256;
1747 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1749 if (bp
->class == SNTERM
)
1751 bp
->value
+= ntokens
;
1755 /* this symbol and its alias are a single token defn.
1756 allocate a tokno, and assign to both check agreement of
1757 ->prec and ->assoc fields and make both the same */
1759 bp
->value
= bp
->alias
->value
= tokno
++;
1761 if (bp
->prec
!= bp
->alias
->prec
)
1763 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1764 && bp
->user_token_number
== SALIAS
)
1765 complain (_("conflicting precedences for %s and %s"),
1766 bp
->tag
, bp
->alias
->tag
);
1768 bp
->alias
->prec
= bp
->prec
;
1770 bp
->prec
= bp
->alias
->prec
;
1773 if (bp
->assoc
!= bp
->alias
->assoc
)
1775 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1776 && bp
->user_token_number
== SALIAS
)
1777 complain (_("conflicting assoc values for %s and %s"),
1778 bp
->tag
, bp
->alias
->tag
);
1780 bp
->alias
->assoc
= bp
->assoc
;
1782 bp
->assoc
= bp
->alias
->assoc
;
1785 if (bp
->user_token_number
== SALIAS
)
1786 continue; /* do not do processing below for SALIASs */
1789 else /* bp->class == STOKEN */
1791 bp
->value
= tokno
++;
1794 if (bp
->class == STOKEN
)
1796 if (translations
&& !(bp
->user_token_number
))
1797 bp
->user_token_number
= ++last_user_token_number
;
1798 if (bp
->user_token_number
> max_user_token_number
)
1799 max_user_token_number
= bp
->user_token_number
;
1802 tags
[bp
->value
] = bp
->tag
;
1803 user_toknums
[bp
->value
] = bp
->user_token_number
;
1804 sprec
[bp
->value
] = bp
->prec
;
1805 sassoc
[bp
->value
] = bp
->assoc
;
1813 token_translations
= NEW2(max_user_token_number
+1, short);
1815 /* initialize all entries for literal tokens to 2, the internal
1816 token number for $undefined., which represents all invalid
1818 for (j
= 0; j
<= max_user_token_number
; j
++)
1819 token_translations
[j
] = 2;
1821 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1823 if (bp
->value
>= ntokens
)
1824 continue; /* non-terminal */
1825 if (bp
->user_token_number
== SALIAS
)
1827 if (token_translations
[bp
->user_token_number
] != 2)
1828 complain (_("tokens %s and %s both assigned number %d"),
1829 tags
[token_translations
[bp
->user_token_number
]],
1831 bp
->user_token_number
);
1832 token_translations
[bp
->user_token_number
] = bp
->value
;
1836 error_token_number
= errtoken
->value
;
1839 output_token_defines(ftable
);
1841 if (startval
->class == SUNKNOWN
)
1842 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1843 else if (startval
->class == STOKEN
)
1844 fatal (_("the start symbol %s is a token"), startval
->tag
);
1846 start_symbol
= startval
->value
;
1850 output_token_defines(fdefines
);
1854 if (spec_name_prefix
)
1855 fprintf(fdefines
, "\nextern YYSTYPE %slval;\n", spec_name_prefix
);
1857 fprintf(fdefines
, "\nextern YYSTYPE yylval;\n");
1860 if (semantic_parser
)
1861 for (i
= ntokens
; i
< nsyms
; i
++)
1863 /* don't make these for dummy nonterminals made by gensym. */
1864 if (*tags
[i
] != '@')
1865 fprintf(fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1868 /* `fdefines' is now a temporary file, so we need to copy its
1869 contents in `done', so we can't close it here. */
1876 /* For named tokens, but not literal ones, define the name. The value
1877 is the user token number. */
1880 output_token_defines (FILE *file
)
1883 register char *cp
, *symbol
;
1886 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1888 symbol
= bp
->tag
; /* get symbol */
1890 if (bp
->value
>= ntokens
) continue;
1891 if (bp
->user_token_number
== SALIAS
) continue;
1892 if ('\'' == *symbol
) continue; /* skip literal character */
1893 if (bp
== errtoken
) continue; /* skip error token */
1894 if ('\"' == *symbol
)
1896 /* use literal string only if given a symbol with an alias */
1898 symbol
= bp
->alias
->tag
;
1903 /* Don't #define nonliteral tokens whose names contain periods. */
1905 while ((c
= *cp
++) && c
!= '.');
1906 if (c
!= '\0') continue;
1908 fprintf (file
, "#define\t%s\t%d\n", symbol
,
1909 ((translations
&& ! rawtoknumflag
)
1910 ? bp
->user_token_number
1912 if (semantic_parser
)
1913 fprintf (file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1921 /* convert the rules into the representation using rrhs, rlhs and ritems. */
1926 register int itemno
;
1927 register int ruleno
;
1928 register symbol_list
*p
;
1929 /* register bucket *bp; JF unused */
1933 ritem
= NEW2(nitems
+ 1, short);
1934 rlhs
= NEW2(nrules
, short) - 1;
1935 rrhs
= NEW2(nrules
, short) - 1;
1936 rprec
= NEW2(nrules
, short) - 1;
1937 rprecsym
= NEW2(nrules
, short) - 1;
1938 rassoc
= NEW2(nrules
, short) - 1;
1946 rlhs
[ruleno
] = p
->sym
->value
;
1947 rrhs
[ruleno
] = itemno
;
1948 ruleprec
= p
->ruleprec
;
1953 ritem
[itemno
++] = p
->sym
->value
;
1954 /* A rule gets by default the precedence and associativity
1955 of the last token in it. */
1956 if (p
->sym
->class == STOKEN
)
1958 rprec
[ruleno
] = p
->sym
->prec
;
1959 rassoc
[ruleno
] = p
->sym
->assoc
;
1964 /* If this rule has a %prec,
1965 the specified symbol's precedence replaces the default. */
1968 rprec
[ruleno
] = ruleprec
->prec
;
1969 rassoc
[ruleno
] = ruleprec
->assoc
;
1970 rprecsym
[ruleno
] = ruleprec
->value
;
1973 ritem
[itemno
++] = -ruleno
;