]>
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 (). */
58 #define YYLTYPE yyltype\n\
62 /* Number of slots allocated (but not necessarily used yet) in `rline' */
63 static int rline_allocated
;
65 extern bucket
*symval
;
67 extern int expected_conflicts
;
68 extern char *token_buffer
;
71 extern void init_lex
PARAMS((void));
72 extern char *grow_token_buffer
PARAMS((char *));
73 extern void tabinit
PARAMS((void));
74 extern void free_symtab
PARAMS((void));
75 extern void open_extra_files
PARAMS((void));
76 extern char *printable_version
PARAMS((int));
77 extern void unlex
PARAMS((int));
79 extern int skip_white_space
PARAMS((void));
80 extern int parse_percent_token
PARAMS((void));
81 extern int lex
PARAMS((void));
86 struct symbol_list
*next
;
93 extern void reader
PARAMS((void));
94 extern void reader_output_yylsp
PARAMS((FILE *));
96 static void read_declarations
PARAMS((void));
97 static void copy_definition
PARAMS((void));
98 static void parse_token_decl
PARAMS((int, int));
99 static void parse_start_decl
PARAMS((void));
100 static void parse_type_decl
PARAMS((void));
101 static void parse_assoc_decl
PARAMS((int));
102 static void parse_union_decl
PARAMS((void));
103 static void parse_expect_decl
PARAMS((void));
104 static char *get_type_name
PARAMS((int, symbol_list
*));
105 static void copy_guard
PARAMS((symbol_list
*, int));
106 static void parse_thong_decl
PARAMS((void));
107 static void copy_action
PARAMS((symbol_list
*, int));
108 static bucket
*gensym
PARAMS((void));
109 static void readgram
PARAMS((void));
110 static void record_rule_line
PARAMS((void));
111 static void packsymbols
PARAMS((void));
112 static void output_token_defines
PARAMS((FILE *));
113 static void packgram
PARAMS((void));
116 static int get_type
PARAMS((void));
122 static symbol_list
*grammar
;
123 static int start_flag
;
124 static bucket
*startval
;
126 /* Nonzero if components of semantic values are used, implying
127 they must be unions. */
128 static int value_components_used
;
130 static int typed
; /* nonzero if %union has been seen. */
132 static int lastprec
; /* incremented for each %left, %right or %nonassoc seen */
134 static int gensym_count
; /* incremented for each generated symbol */
136 static bucket
*errtoken
;
137 static bucket
*undeftoken
;
139 /* Nonzero if any action or guard uses the @n construct. */
140 static int yylsp_needed
;
143 /*===================\
144 | Low level lexing. |
145 \===================*/
148 skip_to_char (int target
)
152 complain (_(" Skipping to next \\n"));
154 complain (_(" Skipping to next %c"), target
);
157 c
= skip_white_space ();
158 while (c
!= target
&& c
!= EOF
);
164 /*---------------------------------------------------------.
165 | Read a signed integer from STREAM and return its value. |
166 `---------------------------------------------------------*/
169 read_signed_integer (FILE *stream
)
171 register int c
= getc (stream
);
172 register int sign
= 1;
183 n
= 10 * n
+ (c
- '0');
192 /*-------------------------------------------------------------------.
193 | Dump the string from FINPUT to FOUTPUT. MATCH is the delimiter of |
194 | the string (either ' or "). |
195 `-------------------------------------------------------------------*/
198 copy_string (FILE *fin
, FILE *fout
, int match
)
208 fatal (_("unterminated string at end of file"));
211 complain (_("unterminated string"));
213 c
= match
; /* invent terminator */
223 fatal (_("unterminated string at end of file"));
236 /*---------------------------------------------------------------.
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 |
240 `---------------------------------------------------------------*/
243 copy_comment2 (FILE *in
, FILE *out1
, FILE* out2
, int c
)
248 cplus_comment
= (c
== '/');
257 if (!cplus_comment
&& c
== '*')
287 fatal (_("unterminated comment"));
299 /*------------------------------------------------------------.
300 | Dump the comment from FIN to FOUT. C is either `*' or `/', |
301 | depending upon the type of comments used. |
302 `------------------------------------------------------------*/
305 copy_comment (FILE *fin
, FILE *fout
, int c
)
307 copy_comment2 (fin
, fout
, NULL
, c
);
315 startval
= NULL
; /* start symbol not specified yet. */
318 /* initially assume token number translation not needed. */
321 /* Nowadays translations is always set to 1, since we give `error' a
322 user-token-number to satisfy the Posix demand for YYERRCODE==256.
330 rline_allocated
= 10;
331 rline
= NEW2 (rline_allocated
, short);
347 /* Initialize the symbol table. */
349 /* Construct the error token */
350 errtoken
= getsym ("error");
351 errtoken
->class = STOKEN
;
352 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
353 /* Construct a token that represents all undefined literal tokens.
354 It is always token number 2. */
355 undeftoken
= getsym ("$undefined.");
356 undeftoken
->class = STOKEN
;
357 undeftoken
->user_token_number
= 2;
359 /* Read the declaration section. Copy %{ ... %} groups to FTABLE
360 and FDEFINES file. Also notice any %token, %left, etc. found
364 /* %s, made from %s\n\
365 by GNU bison %s. */\n\
367 noparserflag
? "Bison-generated parse tables" : "A Bison parser",
371 fputs ("#define YYBISON 1 /* Identify Bison output. */\n\n", ftable
);
372 read_declarations ();
373 /* Start writing the guard and action files, if they are needed. */
375 /* Read in the grammar, build grammar in list form. Write out
376 guards and actions. */
378 /* Now we know whether we need the line-number stack. If we do,
379 write its type into the .tab.h file. */
381 reader_output_yylsp (fdefines
);
382 /* Write closing delimiters for actions and guards. */
385 fputs ("#define YYLSP_NEEDED\n\n", ftable
);
386 /* Assign the symbols their symbol numbers. Write #defines for the
387 token symbols into FDEFINES if requested. */
389 /* Convert the grammar into the format described in gram.h. */
391 /* Free the symbol table data structure since symbols are now all
392 referred to by symbol number. */
397 reader_output_yylsp (FILE *f
)
400 fprintf(f
, LTYPESTR
);
403 /* Read from finput until `%%' is seen. Discard the `%%'. Handle any
404 `%' declarations, and copy the contents of any `%{ ... %}' groups
408 read_declarations (void)
415 c
= skip_white_space();
419 tok
= parse_percent_token();
426 case PERCENT_LEFT_CURLY
:
431 parse_token_decl (STOKEN
, SNTERM
);
435 parse_token_decl (SNTERM
, STOKEN
);
457 parse_assoc_decl(LEFT_ASSOC
);
461 parse_assoc_decl(RIGHT_ASSOC
);
465 parse_assoc_decl(NON_ASSOC
);
468 case SEMANTIC_PARSER
:
469 if (semantic_parser
== 0)
484 complain (_("unrecognized: %s"), token_buffer
);
489 fatal (_("no input grammar"));
492 complain (_("unknown character: %s"), printable_version(c
));
499 /* Copy the contents of a `%{ ... %}' into the definitions file. The
500 `%{' has already been read. Return after reading the `%}'. */
503 copy_definition (void)
506 /* -1 while reading a character if prev char was %. */
507 register int after_percent
;
510 fprintf(fattrs
, "#line %d \"%s\"\n", lineno
, infile
);
531 copy_string (finput
, fattrs
, c
);
537 if (c
!= '*' && c
!= '/')
539 copy_comment (finput
, fattrs
, c
);
544 _("unterminated `%{' definition"));
566 /* parse what comes after %token or %nterm.
567 For %token, what_is is STOKEN and what_is_not is SNTERM.
568 For %nterm, the arguments are reversed. */
571 parse_token_decl (int what_is
, int what_is_not
)
573 register int token
= 0;
574 register char *typename
= 0;
575 register struct bucket
*symbol
= NULL
; /* pts to symbol being defined */
580 int tmp_char
= ungetc (skip_white_space (), finput
);
585 fatal (_("Premature EOF after %s"), token_buffer
);
593 if (token
== TYPENAME
)
595 k
= strlen(token_buffer
);
596 typename
= NEW2(k
+ 1, char);
597 strcpy(typename
, token_buffer
);
598 value_components_used
= 1;
601 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"'
605 symval
->class = STOKEN
;
606 symval
->type_name
= typename
;
607 symval
->user_token_number
= symbol
->user_token_number
;
608 symbol
->user_token_number
= SALIAS
;
610 symval
->alias
= symbol
;
611 symbol
->alias
= symval
;
614 nsyms
--; /* symbol and symval combined are only one symbol */
616 else if (token
== IDENTIFIER
)
618 int oldclass
= symval
->class;
621 if (symbol
->class == what_is_not
)
622 complain (_("symbol %s redefined"), symbol
->tag
);
623 symbol
->class = what_is
;
624 if (what_is
== SNTERM
&& oldclass
!= SNTERM
)
625 symbol
->value
= nvars
++;
629 if (symbol
->type_name
== NULL
)
630 symbol
->type_name
= typename
;
631 else if (strcmp(typename
, symbol
->type_name
) != 0)
632 complain (_("type redeclaration for %s"), symbol
->tag
);
635 else if (symbol
&& token
== NUMBER
)
637 symbol
->user_token_number
= numval
;
642 complain (_("`%s' is invalid in %s"),
644 (what_is
== STOKEN
) ? "%token" : "%nterm");
651 /* parse what comes after %thong
653 %thong <type> token number literal
654 the <type> or number may be omitted. The number specifies the
657 Two symbols are entered in the table, one for the token symbol and
658 one for the literal. Both are given the <type>, if any, from the declaration.
659 The ->user_token_number of the first is SALIAS and the ->user_token_number
660 of the second is set to the number, if any, from the declaration.
661 The two symbols are linked via pointers in their ->alias fields.
663 during output_defines_table, the symbol is reported
664 thereafter, only the literal string is retained
665 it is the literal string that is output to yytname
669 parse_thong_decl (void)
672 register struct bucket
*symbol
;
673 register char *typename
= 0;
677 token
= lex(); /* fetch typename or first token */
678 if (token
== TYPENAME
) {
679 k
= strlen(token_buffer
);
680 typename
= NEW2(k
+ 1, char);
681 strcpy(typename
, token_buffer
);
682 value_components_used
= 1;
683 token
= lex(); /* fetch first token */
686 /* process first token */
688 if (token
!= IDENTIFIER
)
690 complain (_("unrecognized item %s, expected an identifier"),
695 symval
->class = STOKEN
;
696 symval
->type_name
= typename
;
697 symval
->user_token_number
= SALIAS
;
700 token
= lex(); /* get number or literal string */
702 if (token
== NUMBER
) {
704 token
= lex(); /* okay, did number, now get literal */
708 /* process literal string token */
710 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
712 complain (_("expected string constant instead of %s"),
717 symval
->class = STOKEN
;
718 symval
->type_name
= typename
;
719 symval
->user_token_number
= usrtoknum
;
721 symval
->alias
= symbol
;
722 symbol
->alias
= symval
;
724 nsyms
--; /* symbol and symval combined are only one symbol */
728 /* Parse what comes after %start */
731 parse_start_decl (void)
734 complain (_("multiple %s declarations"), "%start");
735 if (lex () != IDENTIFIER
)
736 complain (_("invalid %s declaration"), "%start");
746 /* read in a %type declaration and record its information for get_type_name to access */
749 parse_type_decl (void)
754 if (lex() != TYPENAME
)
756 complain ("%s", _("%type declaration has no <typename>"));
761 k
= strlen(token_buffer
);
762 name
= NEW2(k
+ 1, char);
763 strcpy(name
, token_buffer
);
768 int tmp_char
= ungetc (skip_white_space (), finput
);
773 fatal (_("Premature EOF after %s"), token_buffer
);
785 if (symval
->type_name
== NULL
)
786 symval
->type_name
= name
;
787 else if (strcmp(name
, symval
->type_name
) != 0)
788 complain (_("type redeclaration for %s"), symval
->tag
);
793 complain (_("invalid %%type declaration due to item: %s"),
802 /* read in a %left, %right or %nonassoc declaration and record its information. */
803 /* assoc is either LEFT_ASSOC, RIGHT_ASSOC or NON_ASSOC. */
806 parse_assoc_decl (int assoc
)
809 register char *name
= NULL
;
810 register int prev
= 0;
812 lastprec
++; /* Assign a new precedence level, never 0. */
817 int tmp_char
= ungetc (skip_white_space (), finput
);
822 fatal (_("Premature EOF after %s"), token_buffer
);
830 k
= strlen(token_buffer
);
831 name
= NEW2(k
+ 1, char);
832 strcpy(name
, token_buffer
);
839 if (symval
->prec
!= 0)
840 complain (_("redefining precedence of %s"), symval
->tag
);
841 symval
->prec
= lastprec
;
842 symval
->assoc
= assoc
;
843 if (symval
->class == SNTERM
)
844 complain (_("symbol %s redefined"), symval
->tag
);
845 symval
->class = STOKEN
;
847 { /* record the type, if one is specified */
848 if (symval
->type_name
== NULL
)
849 symval
->type_name
= name
;
850 else if (strcmp(name
, symval
->type_name
) != 0)
851 complain (_("type redeclaration for %s"), symval
->tag
);
856 if (prev
== IDENTIFIER
)
858 symval
->user_token_number
= numval
;
863 complain (_("invalid text (%s) - number should be after identifier"),
873 complain (_("unexpected item: %s"), token_buffer
);
884 /* copy the union declaration into fattrs (and fdefines),
885 where it is made into the
886 definition of YYSTYPE, the type of elements of the parser value stack. */
889 parse_union_decl (void)
892 register int count
= 0;
895 complain (_("multiple %s declarations"), "%union");
900 fprintf (fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
902 fprintf (fattrs
, "\n");
904 fprintf (fattrs
, "typedef union");
906 fprintf (fdefines
, "typedef union");
924 if (c
!= '*' && c
!= '/')
926 copy_comment2 (finput
, fattrs
, fdefines
, c
);
936 complain (_("unmatched %s"), "`}'");
940 fprintf (fattrs
, " YYSTYPE;\n");
942 fprintf (fdefines
, " YYSTYPE;\n");
943 /* JF don't choke on trailing semi */
944 c
= skip_white_space ();
955 /* parse the declaration %expect N which says to expect N
956 shift-reduce conflicts. */
959 parse_expect_decl (void)
966 while (c
== ' ' || c
== '\t')
970 while (c
>= '0' && c
<= '9')
980 if (count
<= 0 || count
> 10)
981 complain ("%s", _("argument of %expect is not an integer"));
982 expected_conflicts
= atoi (buffer
);
985 /* that's all of parsing the declaration section */
987 /* FIN is pointing to a location (i.e., a `@'). Output to FOUT
988 a reference to this location. STACK_OFFSET is the number of values
989 in the current rule so far, which says where to find `$0' with
990 respect to the top of the stack. */
992 copy_at (FILE *fin
, FILE *fout
, int stack_offset
)
999 fprintf (fout
, "yyloc");
1002 else if (isdigit(c
) || c
== '-')
1007 n
= read_signed_integer (fin
);
1009 fprintf (fout
, "yylsp[%d]", n
- stack_offset
);
1013 complain (_("@%s is invalid"), printable_version (c
));
1017 /* Get the data type (alternative in the union) of the value for
1018 symbol n in rule rule. */
1021 get_type_name (int n
, symbol_list
*rule
)
1024 register symbol_list
*rp
;
1028 complain (_("invalid $ value"));
1038 if (rp
== NULL
|| rp
->sym
== NULL
)
1040 complain (_("invalid $ value"));
1046 return rp
->sym
->type_name
;
1051 /* After `%guard' is seen in the input file, copy the actual guard
1052 into the guards file. If the guard is followed by an action, copy
1053 that into the actions file. STACK_OFFSET is the number of values
1054 in the current rule so far, which says where to find `$0' with
1055 respect to the top of the stack, for the simple parser in which the
1056 stack is not popped until after the guard is run. */
1059 copy_guard (symbol_list
*rule
, int stack_offset
)
1064 register char *type_name
;
1067 /* offset is always 0 if parser has already popped the stack pointer */
1068 if (semantic_parser
) stack_offset
= 0;
1070 fprintf(fguard
, "\ncase %d:\n", nrules
);
1072 fprintf (fguard
, "#line %d \"%s\"\n", lineno
, infile
);
1078 while (brace_flag
? (count
> 0) : (c
!= ';'))
1099 complain (_("unmatched %s"), "`}'");
1100 c
= getc(finput
); /* skip it */
1106 copy_string (finput
, fguard
, c
);
1112 if (c
!= '*' && c
!= '/')
1114 copy_comment (finput
, fguard
, c
);
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");
1142 type_name
= rule
->sym
->type_name
;
1144 fprintf(fguard
, ".%s", type_name
);
1145 if(!type_name
&& typed
)
1146 complain (_("$$ of `%s' has no declared type"),
1149 else if (isdigit(c
) || c
== '-')
1152 n
= read_signed_integer (finput
);
1155 if (!type_name
&& n
> 0)
1156 type_name
= get_type_name(n
, rule
);
1158 fprintf(fguard
, "yyvsp[%d]", n
- stack_offset
);
1160 fprintf(fguard
, ".%s", type_name
);
1161 if (!type_name
&& typed
)
1162 complain (_("$%d of `%s' has no declared type"),
1167 complain (_("$%s is invalid"), printable_version (c
));
1171 copy_at (finput
, fguard
, stack_offset
);
1176 _("unterminated %guard clause"));
1182 if (c
!= '}' || count
!= 0)
1186 c
= skip_white_space();
1188 fprintf(fguard
, ";\n break;}");
1190 copy_action (rule
, stack_offset
);
1193 c
= getc(finput
); /* why not skip_white_space -wjh */
1195 copy_action (rule
, stack_offset
);
1203 /* Assuming that a `{' has just been seen, copy everything up to the
1204 matching `}' into the actions file. STACK_OFFSET is the number of
1205 values in the current rule so far, which says where to find `$0'
1206 with respect to the top of the stack. */
1209 copy_action (symbol_list
*rule
, int stack_offset
)
1214 register char *type_name
;
1216 /* offset is always 0 if parser has already popped the stack pointer */
1217 if (semantic_parser
)
1220 fprintf (faction
, "\ncase %d:\n", nrules
);
1222 fprintf (faction
, "#line %d \"%s\"\n", lineno
, infile
);
1223 putc ('{', faction
);
1246 copy_string (finput
, faction
, c
);
1252 if (c
!= '*' && c
!= '/')
1254 copy_comment (finput
, faction
, c
);
1263 register char *cp
= token_buffer
;
1265 while ((c
= getc(finput
)) != '>' && c
> 0)
1267 if (cp
== token_buffer
+ maxtoken
)
1268 cp
= grow_token_buffer(cp
);
1273 type_name
= token_buffer
;
1274 value_components_used
= 1;
1280 fprintf(faction
, "yyval");
1282 type_name
= get_type_name(0, rule
);
1284 fprintf(faction
, ".%s", type_name
);
1285 if(!type_name
&& typed
)
1286 complain (_("$$ of `%s' has no declared type"),
1289 else if (isdigit(c
) || c
== '-')
1292 n
= read_signed_integer(finput
);
1295 if (!type_name
&& n
> 0)
1296 type_name
= get_type_name(n
, rule
);
1298 fprintf(faction
, "yyvsp[%d]", n
- stack_offset
);
1300 fprintf(faction
, ".%s", type_name
);
1301 if(!type_name
&& typed
)
1302 complain (_("$%d of `%s' has no declared type"),
1307 complain (_("$%s is invalid"), printable_version (c
));
1312 copy_at (finput
, faction
, stack_offset
);
1316 fatal (_("unmatched %s"), "`{'");
1325 /* above loop exits when c is '}' */
1334 fprintf(faction
, ";\n break;}");
1339 /* generate a dummy symbol, a nonterminal,
1340 whose name cannot conflict with the user's names. */
1345 register bucket
*sym
;
1347 sprintf (token_buffer
, "@%d", ++gensym_count
);
1348 sym
= getsym(token_buffer
);
1349 sym
->class = SNTERM
;
1350 sym
->value
= nvars
++;
1354 /* Parse the input grammar into a one symbol_list structure.
1355 Each rule is represented by a sequence of symbols: the left hand side
1356 followed by the contents of the right hand side, followed by a null pointer
1357 instead of a symbol to terminate the rule.
1358 The next symbol is the lhs of the following rule.
1360 All guards and actions are copied out to the appropriate files,
1361 labelled by the rule number they apply to. */
1367 register bucket
*lhs
= NULL
;
1368 register symbol_list
*p
;
1369 register symbol_list
*p1
;
1370 register bucket
*bp
;
1372 symbol_list
*crule
; /* points to first symbol_list of current rule. */
1373 /* its symbol is the lhs of the rule. */
1374 symbol_list
*crule1
; /* points to the symbol_list preceding crule. */
1380 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1382 if (t
== IDENTIFIER
|| t
== BAR
)
1384 register int actionflag
= 0;
1385 int rulelength
= 0; /* number of symbols in rhs of this rule so far */
1386 int xactions
= 0; /* JF for error checking */
1387 bucket
*first_rhs
= 0;
1389 if (t
== IDENTIFIER
)
1402 complain (_("ill-formed rule: initial symbol not followed by colon"));
1407 if (nrules
== 0 && t
== BAR
)
1409 complain (_("grammar starts with vertical bar"));
1410 lhs
= symval
; /* BOGUS: use a random symval */
1412 /* start a new rule and record its lhs. */
1417 record_rule_line ();
1419 p
= NEW(symbol_list
);
1431 /* mark the rule's lhs as a nonterminal if not already so. */
1433 if (lhs
->class == SUNKNOWN
)
1435 lhs
->class = SNTERM
;
1439 else if (lhs
->class == STOKEN
)
1440 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1442 /* read the rhs of the rule. */
1450 crule
->ruleprec
= symval
;
1454 if (! (t
== IDENTIFIER
|| t
== LEFT_CURLY
)) break;
1456 /* If next token is an identifier, see if a colon follows it.
1457 If one does, exit this rule now. */
1458 if (t
== IDENTIFIER
)
1460 register bucket
*ssave
;
1467 if (t1
== COLON
) break;
1469 if(!first_rhs
) /* JF */
1471 /* Not followed by colon =>
1472 process as part of this rule's rhs. */
1475 /* If we just passed an action, that action was in the middle
1476 of a rule, so make a dummy rule to reduce it to a
1480 register bucket
*sdummy
;
1482 /* Since the action was written out with this rule's */
1483 /* number, we must give the new rule this number */
1484 /* by inserting the new rule before it. */
1486 /* Make a dummy nonterminal, a gensym. */
1489 /* Make a new rule, whose body is empty,
1490 before the current one, so that the action
1491 just read can belong to it. */
1494 record_rule_line ();
1495 p
= NEW(symbol_list
);
1500 crule1
= NEW(symbol_list
);
1502 crule1
->next
= crule
;
1504 /* insert the dummy generated by that rule into this rule. */
1506 p
= NEW(symbol_list
);
1514 if (t
== IDENTIFIER
)
1517 p
= NEW(symbol_list
);
1522 else /* handle an action. */
1524 copy_action(crule
, rulelength
);
1526 xactions
++; /* JF */
1529 } /* end of read rhs of rule */
1531 /* Put an empty link in the list to mark the end of this rule */
1532 p
= NEW(symbol_list
);
1538 complain (_("two @prec's in a row"));
1540 crule
->ruleprec
= symval
;
1545 if (! semantic_parser
)
1547 _("%guard present but %semantic_parser not specified"));
1549 copy_guard(crule
, rulelength
);
1552 else if (t
== LEFT_CURLY
)
1554 /* This case never occurs -wjh */
1556 complain (_("two actions at end of one rule"));
1557 copy_action(crule
, rulelength
);
1559 xactions
++; /* -wjh */
1562 /* If $$ is being set in default way, report if any type
1566 && lhs
->type_name
!= first_rhs
->type_name
)
1568 if (lhs
->type_name
== 0
1569 || first_rhs
->type_name
== 0
1570 || strcmp(lhs
->type_name
,first_rhs
->type_name
))
1571 complain (_("type clash (`%s' `%s') on default action"),
1572 lhs
->type_name
? lhs
->type_name
: "",
1573 first_rhs
->type_name
? first_rhs
->type_name
: "");
1575 /* Warn if there is no default for $$ but we need one. */
1576 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1577 complain (_("empty rule for typed nonterminal, and no action"));
1582 /* these things can appear as alternatives to rules. */
1584 a) none of the documentation allows them
1585 b) most of them scan forward until finding a next %
1586 thus they may swallow lots of intervening rules
1588 else if (t
== TOKEN
)
1590 parse_token_decl(STOKEN
, SNTERM
);
1593 else if (t
== NTERM
)
1595 parse_token_decl(SNTERM
, STOKEN
);
1602 else if (t
== UNION
)
1607 else if (t
== EXPECT
)
1609 parse_expect_decl();
1612 else if (t
== START
)
1621 complain (_("invalid input: %s"), token_buffer
);
1626 /* grammar has been read. Do some checking */
1628 if (nsyms
> MAXSHORT
)
1629 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1632 fatal (_("no rules in the input grammar"));
1634 if (typed
== 0 /* JF put out same default YYSTYPE as YACC does */
1635 && !value_components_used
)
1637 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1638 but it seems better to be consistent.
1639 Most programs should declare their own type anyway. */
1640 fprintf(fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1642 fprintf(fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1645 /* Report any undefined symbols and consider them nonterminals. */
1647 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1648 if (bp
->class == SUNKNOWN
)
1650 complain (_("symbol %s is used, but is not defined as a token and has no rules"),
1653 bp
->value
= nvars
++;
1656 ntokens
= nsyms
- nvars
;
1661 record_rule_line (void)
1663 /* Record each rule's source line number in rline table. */
1665 if (nrules
>= rline_allocated
)
1667 rline_allocated
= nrules
* 2;
1668 rline
= (short *) xrealloc ((char *) rline
,
1669 rline_allocated
* sizeof (short));
1671 rline
[nrules
] = lineno
;
1676 /* read in a %type declaration and record its information for get_type_name to access */
1677 /* this is unused. it is only called from the #if 0 part of readgram */
1683 register char *name
;
1689 complain (_("invalid %s declaration"), "%type");
1693 k
= strlen(token_buffer
);
1694 name
= NEW2(k
+ 1, char);
1695 strcpy(name
, token_buffer
);
1710 if (symval
->type_name
== NULL
)
1711 symval
->type_name
= name
;
1712 else if (strcmp(name
, symval
->type_name
) != 0)
1713 complain (_("type redeclaration for %s"), symval
->tag
);
1725 /* Assign symbol numbers, and write definition of token names into
1726 fdefines. Set up vectors tags and sprec of names and precedences
1732 register bucket
*bp
;
1733 register int tokno
= 1;
1735 register int last_user_token_number
;
1736 static char DOLLAR
[] = "$";
1738 /* int lossage = 0; JF set but not used */
1740 tags
= NEW2(nsyms
+ 1, char *);
1742 user_toknums
= NEW2(nsyms
+ 1, short);
1743 user_toknums
[0] = 0;
1745 sprec
= NEW2(nsyms
, short);
1746 sassoc
= NEW2(nsyms
, short);
1748 max_user_token_number
= 256;
1749 last_user_token_number
= 256;
1751 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1753 if (bp
->class == SNTERM
)
1755 bp
->value
+= ntokens
;
1759 /* this symbol and its alias are a single token defn.
1760 allocate a tokno, and assign to both check agreement of
1761 ->prec and ->assoc fields and make both the same */
1763 bp
->value
= bp
->alias
->value
= tokno
++;
1765 if (bp
->prec
!= bp
->alias
->prec
)
1767 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1768 && bp
->user_token_number
== SALIAS
)
1769 complain (_("conflicting precedences for %s and %s"),
1770 bp
->tag
, bp
->alias
->tag
);
1772 bp
->alias
->prec
= bp
->prec
;
1774 bp
->prec
= bp
->alias
->prec
;
1777 if (bp
->assoc
!= bp
->alias
->assoc
)
1779 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1780 && bp
->user_token_number
== SALIAS
)
1781 complain (_("conflicting assoc values for %s and %s"),
1782 bp
->tag
, bp
->alias
->tag
);
1784 bp
->alias
->assoc
= bp
->assoc
;
1786 bp
->assoc
= bp
->alias
->assoc
;
1789 if (bp
->user_token_number
== SALIAS
)
1790 continue; /* do not do processing below for SALIASs */
1793 else /* bp->class == STOKEN */
1795 bp
->value
= tokno
++;
1798 if (bp
->class == STOKEN
)
1800 if (translations
&& !(bp
->user_token_number
))
1801 bp
->user_token_number
= ++last_user_token_number
;
1802 if (bp
->user_token_number
> max_user_token_number
)
1803 max_user_token_number
= bp
->user_token_number
;
1806 tags
[bp
->value
] = bp
->tag
;
1807 user_toknums
[bp
->value
] = bp
->user_token_number
;
1808 sprec
[bp
->value
] = bp
->prec
;
1809 sassoc
[bp
->value
] = bp
->assoc
;
1817 token_translations
= NEW2(max_user_token_number
+1, short);
1819 /* initialize all entries for literal tokens to 2, the internal
1820 token number for $undefined., which represents all invalid
1822 for (j
= 0; j
<= max_user_token_number
; j
++)
1823 token_translations
[j
] = 2;
1825 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1827 if (bp
->value
>= ntokens
)
1828 continue; /* non-terminal */
1829 if (bp
->user_token_number
== SALIAS
)
1831 if (token_translations
[bp
->user_token_number
] != 2)
1832 complain (_("tokens %s and %s both assigned number %d"),
1833 tags
[token_translations
[bp
->user_token_number
]],
1835 bp
->user_token_number
);
1836 token_translations
[bp
->user_token_number
] = bp
->value
;
1840 error_token_number
= errtoken
->value
;
1843 output_token_defines(ftable
);
1845 if (startval
->class == SUNKNOWN
)
1846 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1847 else if (startval
->class == STOKEN
)
1848 fatal (_("the start symbol %s is a token"), startval
->tag
);
1850 start_symbol
= startval
->value
;
1854 output_token_defines(fdefines
);
1858 if (spec_name_prefix
)
1859 fprintf(fdefines
, "\nextern YYSTYPE %slval;\n", spec_name_prefix
);
1861 fprintf(fdefines
, "\nextern YYSTYPE yylval;\n");
1864 if (semantic_parser
)
1865 for (i
= ntokens
; i
< nsyms
; i
++)
1867 /* don't make these for dummy nonterminals made by gensym. */
1868 if (*tags
[i
] != '@')
1869 fprintf(fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1872 /* `fdefines' is now a temporary file, so we need to copy its
1873 contents in `done', so we can't close it here. */
1880 /* For named tokens, but not literal ones, define the name. The value
1881 is the user token number. */
1884 output_token_defines (FILE *file
)
1887 register char *cp
, *symbol
;
1890 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1892 symbol
= bp
->tag
; /* get symbol */
1894 if (bp
->value
>= ntokens
) continue;
1895 if (bp
->user_token_number
== SALIAS
) continue;
1896 if ('\'' == *symbol
) continue; /* skip literal character */
1897 if (bp
== errtoken
) continue; /* skip error token */
1898 if ('\"' == *symbol
)
1900 /* use literal string only if given a symbol with an alias */
1902 symbol
= bp
->alias
->tag
;
1907 /* Don't #define nonliteral tokens whose names contain periods. */
1909 while ((c
= *cp
++) && c
!= '.');
1910 if (c
!= '\0') continue;
1912 fprintf (file
, "#define\t%s\t%d\n", symbol
,
1913 ((translations
&& ! rawtoknumflag
)
1914 ? bp
->user_token_number
1916 if (semantic_parser
)
1917 fprintf (file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1925 /* convert the rules into the representation using rrhs, rlhs and ritems. */
1930 register int itemno
;
1931 register int ruleno
;
1932 register symbol_list
*p
;
1933 /* register bucket *bp; JF unused */
1937 ritem
= NEW2(nitems
+ 1, short);
1938 rlhs
= NEW2(nrules
, short) - 1;
1939 rrhs
= NEW2(nrules
, short) - 1;
1940 rprec
= NEW2(nrules
, short) - 1;
1941 rprecsym
= NEW2(nrules
, short) - 1;
1942 rassoc
= NEW2(nrules
, short) - 1;
1950 rlhs
[ruleno
] = p
->sym
->value
;
1951 rrhs
[ruleno
] = itemno
;
1952 ruleprec
= p
->ruleprec
;
1957 ritem
[itemno
++] = p
->sym
->value
;
1958 /* A rule gets by default the precedence and associativity
1959 of the last token in it. */
1960 if (p
->sym
->class == STOKEN
)
1962 rprec
[ruleno
] = p
->sym
->prec
;
1963 rassoc
[ruleno
] = p
->sym
->assoc
;
1968 /* If this rule has a %prec,
1969 the specified symbol's precedence replaces the default. */
1972 rprec
[ruleno
] = ruleprec
->prec
;
1973 rassoc
[ruleno
] = ruleprec
->assoc
;
1974 rprecsym
[ruleno
] = ruleprec
->value
;
1977 ritem
[itemno
++] = -ruleno
;