]>
git.saurik.com Git - bison.git/blob - src/reader.c
9d247c42161e5f24499c6da9ac23c6ca2802052d
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 int definesflag
;
66 extern int nolinesflag
;
67 extern int noparserflag
;
68 extern int rawtoknumflag
;
69 extern bucket
*symval
;
71 extern int expected_conflicts
;
72 extern char *token_buffer
;
75 extern void init_lex
PARAMS((void));
76 extern char *grow_token_buffer
PARAMS((char *));
77 extern void tabinit
PARAMS((void));
78 extern void output_headers
PARAMS((void));
79 extern void output_trailers
PARAMS((void));
80 extern void free_symtab
PARAMS((void));
81 extern void open_extra_files
PARAMS((void));
82 extern char *printable_version
PARAMS((int));
83 extern void unlex
PARAMS((int));
85 extern int skip_white_space
PARAMS((void));
86 extern int parse_percent_token
PARAMS((void));
87 extern int lex
PARAMS((void));
92 struct symbol_list
*next
;
99 extern void reader
PARAMS((void));
100 extern void reader_output_yylsp
PARAMS((FILE *));
102 static void read_declarations
PARAMS((void));
103 static void copy_definition
PARAMS((void));
104 static void parse_token_decl
PARAMS((int, int));
105 static void parse_start_decl
PARAMS((void));
106 static void parse_type_decl
PARAMS((void));
107 static void parse_assoc_decl
PARAMS((int));
108 static void parse_union_decl
PARAMS((void));
109 static void parse_expect_decl
PARAMS((void));
110 static char *get_type_name
PARAMS((int, symbol_list
*));
111 static void copy_guard
PARAMS((symbol_list
*, int));
112 static void parse_thong_decl
PARAMS((void));
113 static void copy_action
PARAMS((symbol_list
*, int));
114 static bucket
*gensym
PARAMS((void));
115 static void readgram
PARAMS((void));
116 static void record_rule_line
PARAMS((void));
117 static void packsymbols
PARAMS((void));
118 static void output_token_defines
PARAMS((FILE *));
119 static void packgram
PARAMS((void));
120 static int read_signed_integer
PARAMS((FILE *));
123 static int get_type
PARAMS((void));
129 static symbol_list
*grammar
;
130 static int start_flag
;
131 static bucket
*startval
;
133 /* Nonzero if components of semantic values are used, implying
134 they must be unions. */
135 static int value_components_used
;
137 static int typed
; /* nonzero if %union has been seen. */
139 static int lastprec
; /* incremented for each %left, %right or %nonassoc seen */
141 static int gensym_count
; /* incremented for each generated symbol */
143 static bucket
*errtoken
;
144 static bucket
*undeftoken
;
146 /* Nonzero if any action or guard uses the @n construct. */
147 static int yylsp_needed
;
151 skip_to_char (int target
)
155 complain (_(" Skipping to next \\n"));
157 complain (_(" Skipping to next %c"), target
);
160 c
= skip_white_space();
161 while (c
!= target
&& c
!= EOF
);
167 /* Dump the string from FIN to FOUT. MATCH is the delimiter of
168 the string (either ' or "). */
171 copy_string (FILE *fin
, FILE *fout
, int match
)
181 fatal (_("unterminated string at end of file"));
184 complain (_("unterminated string"));
186 c
= match
; /* invent terminator */
196 fatal (_("unterminated string at end of file"));
209 /* Dump the comment from IN to OUT1 and OUT2. C is either `*' or `/',
210 depending upon the type of comments used. OUT2 might be NULL. */
213 copy_comment2 (FILE *in
, FILE *out1
, FILE* out2
, int c
)
218 cplus_comment
= (c
== '/');
227 if (!cplus_comment
&& c
== '*')
257 fatal (_("unterminated comment"));
269 /* Dump the comment from FIN to FOUT. C is either `*' or `/',
270 depending upon the type of comments used. */
273 copy_comment (FILE *fin
, FILE *fout
, int c
)
275 copy_comment2 (fin
, fout
, NULL
, c
);
283 startval
= NULL
; /* start symbol not specified yet. */
286 translations
= 0; /* initially assume token number translation not needed. */
288 /* Nowadays translations is always set to 1,
289 since we give `error' a user-token-number
290 to satisfy the Posix demand for YYERRCODE==256. */
297 rline_allocated
= 10;
298 rline
= NEW2(rline_allocated
, short);
314 /* initialize the symbol table. */
316 /* construct the error token */
317 errtoken
= getsym("error");
318 errtoken
->class = STOKEN
;
319 errtoken
->user_token_number
= 256; /* Value specified by posix. */
320 /* construct a token that represents all undefined literal tokens. */
321 /* it is always token number 2. */
322 undeftoken
= getsym("$undefined.");
323 undeftoken
->class = STOKEN
;
324 undeftoken
->user_token_number
= 2;
325 /* Read the declaration section. Copy %{ ... %} groups to ftable and fdefines file.
326 Also notice any %token, %left, etc. found there. */
328 fprintf(ftable
, "\n/* Bison-generated parse tables, made from %s\n",
331 fprintf(ftable
, "\n/* A Bison parser, made from %s\n", infile
);
332 fprintf(ftable
, " by %s */\n\n", VERSION_STRING
);
333 fprintf(ftable
, "#define YYBISON 1 /* Identify Bison output. */\n\n");
335 /* start writing the guard and action files, if they are needed. */
337 /* read in the grammar, build grammar in list form. write out guards and actions. */
339 /* Now we know whether we need the line-number stack.
340 If we do, write its type into the .tab.h file. */
342 reader_output_yylsp(fdefines
);
343 /* write closing delimiters for actions and guards. */
346 fprintf(ftable
, "#define YYLSP_NEEDED\n\n");
347 /* assign the symbols their symbol numbers.
348 Write #defines for the token symbols into fdefines if requested. */
350 /* convert the grammar into the format described in gram.h. */
352 /* free the symbol table data structure
353 since symbols are now all referred to by symbol number. */
358 reader_output_yylsp (FILE *f
)
361 fprintf(f
, LTYPESTR
);
364 /* Read from finput until `%%' is seen. Discard the `%%'. Handle any
365 `%' declarations, and copy the contents of any `%{ ... %}' groups
369 read_declarations (void)
376 c
= skip_white_space();
380 tok
= parse_percent_token();
387 case PERCENT_LEFT_CURLY
:
392 parse_token_decl (STOKEN
, SNTERM
);
396 parse_token_decl (SNTERM
, STOKEN
);
418 parse_assoc_decl(LEFT_ASSOC
);
422 parse_assoc_decl(RIGHT_ASSOC
);
426 parse_assoc_decl(NON_ASSOC
);
429 case SEMANTIC_PARSER
:
430 if (semantic_parser
== 0)
445 complain (_("unrecognized: %s"), token_buffer
);
450 fatal (_("no input grammar"));
453 complain (_("unknown character: %s"), printable_version(c
));
460 /* Copy the contents of a `%{ ... %}' into the definitions file. The
461 `%{' has already been read. Return after reading the `%}'. */
464 copy_definition (void)
467 /* -1 while reading a character if prev char was %. */
468 register int after_percent
;
471 fprintf(fattrs
, "#line %d \"%s\"\n", lineno
, infile
);
492 copy_string (finput
, fattrs
, c
);
498 if (c
!= '*' && c
!= '/')
500 copy_comment (finput
, fattrs
, c
);
505 _("unterminated `%{' definition"));
527 /* parse what comes after %token or %nterm.
528 For %token, what_is is STOKEN and what_is_not is SNTERM.
529 For %nterm, the arguments are reversed. */
532 parse_token_decl (int what_is
, int what_is_not
)
534 register int token
= 0;
535 register char *typename
= 0;
536 register struct bucket
*symbol
= NULL
; /* pts to symbol being defined */
541 int tmp_char
= ungetc (skip_white_space (), finput
);
546 fatal (_("Premature EOF after %s"), token_buffer
);
554 if (token
== TYPENAME
)
556 k
= strlen(token_buffer
);
557 typename
= NEW2(k
+ 1, char);
558 strcpy(typename
, token_buffer
);
559 value_components_used
= 1;
562 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"'
566 symval
->class = STOKEN
;
567 symval
->type_name
= typename
;
568 symval
->user_token_number
= symbol
->user_token_number
;
569 symbol
->user_token_number
= SALIAS
;
571 symval
->alias
= symbol
;
572 symbol
->alias
= symval
;
575 nsyms
--; /* symbol and symval combined are only one symbol */
577 else if (token
== IDENTIFIER
)
579 int oldclass
= symval
->class;
582 if (symbol
->class == what_is_not
)
583 complain (_("symbol %s redefined"), symbol
->tag
);
584 symbol
->class = what_is
;
585 if (what_is
== SNTERM
&& oldclass
!= SNTERM
)
586 symbol
->value
= nvars
++;
590 if (symbol
->type_name
== NULL
)
591 symbol
->type_name
= typename
;
592 else if (strcmp(typename
, symbol
->type_name
) != 0)
593 complain (_("type redeclaration for %s"), symbol
->tag
);
596 else if (symbol
&& token
== NUMBER
)
598 symbol
->user_token_number
= numval
;
603 complain (_("`%s' is invalid in %s"),
605 (what_is
== STOKEN
) ? "%token" : "%nterm");
612 /* parse what comes after %thong
614 %thong <type> token number literal
615 the <type> or number may be omitted. The number specifies the
618 Two symbols are entered in the table, one for the token symbol and
619 one for the literal. Both are given the <type>, if any, from the declaration.
620 The ->user_token_number of the first is SALIAS and the ->user_token_number
621 of the second is set to the number, if any, from the declaration.
622 The two symbols are linked via pointers in their ->alias fields.
624 during output_defines_table, the symbol is reported
625 thereafter, only the literal string is retained
626 it is the literal string that is output to yytname
630 parse_thong_decl (void)
633 register struct bucket
*symbol
;
634 register char *typename
= 0;
638 token
= lex(); /* fetch typename or first token */
639 if (token
== TYPENAME
) {
640 k
= strlen(token_buffer
);
641 typename
= NEW2(k
+ 1, char);
642 strcpy(typename
, token_buffer
);
643 value_components_used
= 1;
644 token
= lex(); /* fetch first token */
647 /* process first token */
649 if (token
!= IDENTIFIER
)
651 complain (_("unrecognized item %s, expected an identifier"),
656 symval
->class = STOKEN
;
657 symval
->type_name
= typename
;
658 symval
->user_token_number
= SALIAS
;
661 token
= lex(); /* get number or literal string */
663 if (token
== NUMBER
) {
665 token
= lex(); /* okay, did number, now get literal */
669 /* process literal string token */
671 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
673 complain (_("expected string constant instead of %s"),
678 symval
->class = STOKEN
;
679 symval
->type_name
= typename
;
680 symval
->user_token_number
= usrtoknum
;
682 symval
->alias
= symbol
;
683 symbol
->alias
= symval
;
685 nsyms
--; /* symbol and symval combined are only one symbol */
689 /* Parse what comes after %start */
692 parse_start_decl (void)
695 complain (_("multiple %s declarations"), "%start");
696 if (lex () != IDENTIFIER
)
697 complain (_("invalid %s declaration"), "%start");
707 /* read in a %type declaration and record its information for get_type_name to access */
710 parse_type_decl (void)
715 if (lex() != TYPENAME
)
717 complain ("%s", _("%type declaration has no <typename>"));
722 k
= strlen(token_buffer
);
723 name
= NEW2(k
+ 1, char);
724 strcpy(name
, token_buffer
);
729 int tmp_char
= ungetc (skip_white_space (), finput
);
734 fatal (_("Premature EOF after %s"), token_buffer
);
746 if (symval
->type_name
== NULL
)
747 symval
->type_name
= name
;
748 else if (strcmp(name
, symval
->type_name
) != 0)
749 complain (_("type redeclaration for %s"), symval
->tag
);
754 complain (_("invalid %%type declaration due to item: %s"),
763 /* read in a %left, %right or %nonassoc declaration and record its information. */
764 /* assoc is either LEFT_ASSOC, RIGHT_ASSOC or NON_ASSOC. */
767 parse_assoc_decl (int assoc
)
770 register char *name
= NULL
;
771 register int prev
= 0;
773 lastprec
++; /* Assign a new precedence level, never 0. */
778 int tmp_char
= ungetc (skip_white_space (), finput
);
783 fatal (_("Premature EOF after %s"), token_buffer
);
791 k
= strlen(token_buffer
);
792 name
= NEW2(k
+ 1, char);
793 strcpy(name
, token_buffer
);
800 if (symval
->prec
!= 0)
801 complain (_("redefining precedence of %s"), symval
->tag
);
802 symval
->prec
= lastprec
;
803 symval
->assoc
= assoc
;
804 if (symval
->class == SNTERM
)
805 complain (_("symbol %s redefined"), symval
->tag
);
806 symval
->class = STOKEN
;
808 { /* record the type, if one is specified */
809 if (symval
->type_name
== NULL
)
810 symval
->type_name
= name
;
811 else if (strcmp(name
, symval
->type_name
) != 0)
812 complain (_("type redeclaration for %s"), symval
->tag
);
817 if (prev
== IDENTIFIER
)
819 symval
->user_token_number
= numval
;
824 complain (_("invalid text (%s) - number should be after identifier"),
834 complain (_("unexpected item: %s"), token_buffer
);
845 /* copy the union declaration into fattrs (and fdefines),
846 where it is made into the
847 definition of YYSTYPE, the type of elements of the parser value stack. */
850 parse_union_decl (void)
853 register int count
= 0;
856 complain (_("multiple %s declarations"), "%union");
861 fprintf (fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
863 fprintf (fattrs
, "\n");
865 fprintf (fattrs
, "typedef union");
867 fprintf (fdefines
, "typedef union");
885 if (c
!= '*' && c
!= '/')
887 copy_comment2 (finput
, fattrs
, fdefines
, c
);
897 complain (_("unmatched %s"), "`}'");
901 fprintf (fattrs
, " YYSTYPE;\n");
903 fprintf (fdefines
, " YYSTYPE;\n");
904 /* JF don't choke on trailing semi */
905 c
= skip_white_space ();
916 /* parse the declaration %expect N which says to expect N
917 shift-reduce conflicts. */
920 parse_expect_decl (void)
927 while (c
== ' ' || c
== '\t')
931 while (c
>= '0' && c
<= '9')
941 if (count
<= 0 || count
> 10)
942 complain ("%s", _("argument of %expect is not an integer"));
943 expected_conflicts
= atoi (buffer
);
946 /* that's all of parsing the declaration section */
948 /* FIN is pointing to a location (i.e., a `@'). Output to FOUT
949 a reference to this location. STACK_OFFSET is the number of values
950 in the current rule so far, which says where to find `$0' with
951 respect to the top of the stack. */
953 copy_at (FILE *fin
, FILE *fout
, int stack_offset
)
960 fprintf (fout
, "yyloc");
963 else if (isdigit(c
) || c
== '-')
968 n
= read_signed_integer (fin
);
970 fprintf (fout
, "yylsp[%d]", n
- stack_offset
);
974 complain (_("@%s is invalid"), printable_version (c
));
978 /* Get the data type (alternative in the union) of the value for
979 symbol n in rule rule. */
982 get_type_name (int n
, symbol_list
*rule
)
985 register symbol_list
*rp
;
989 complain (_("invalid $ value"));
999 if (rp
== NULL
|| rp
->sym
== NULL
)
1001 complain (_("invalid $ value"));
1007 return rp
->sym
->type_name
;
1012 /* After `%guard' is seen in the input file, copy the actual guard
1013 into the guards file. If the guard is followed by an action, copy
1014 that into the actions file. STACK_OFFSET is the number of values
1015 in the current rule so far, which says where to find `$0' with
1016 respect to the top of the stack, for the simple parser in which the
1017 stack is not popped until after the guard is run. */
1020 copy_guard (symbol_list
*rule
, int stack_offset
)
1025 register char *type_name
;
1028 /* offset is always 0 if parser has already popped the stack pointer */
1029 if (semantic_parser
) stack_offset
= 0;
1031 fprintf(fguard
, "\ncase %d:\n", nrules
);
1033 fprintf (fguard
, "#line %d \"%s\"\n", lineno
, infile
);
1039 while (brace_flag
? (count
> 0) : (c
!= ';'))
1060 complain (_("unmatched %s"), "`}'");
1061 c
= getc(finput
); /* skip it */
1067 copy_string (finput
, fguard
, c
);
1073 if (c
!= '*' && c
!= '/')
1075 copy_comment (finput
, fguard
, c
);
1084 register char *cp
= token_buffer
;
1086 while ((c
= getc(finput
)) != '>' && c
> 0)
1088 if (cp
== token_buffer
+ maxtoken
)
1089 cp
= grow_token_buffer(cp
);
1094 type_name
= token_buffer
;
1101 fprintf(fguard
, "yyval");
1103 type_name
= rule
->sym
->type_name
;
1105 fprintf(fguard
, ".%s", type_name
);
1106 if(!type_name
&& typed
)
1107 complain (_("$$ of `%s' has no declared type"),
1110 else if (isdigit(c
) || c
== '-')
1113 n
= read_signed_integer (finput
);
1116 if (!type_name
&& n
> 0)
1117 type_name
= get_type_name(n
, rule
);
1119 fprintf(fguard
, "yyvsp[%d]", n
- stack_offset
);
1121 fprintf(fguard
, ".%s", type_name
);
1122 if (!type_name
&& typed
)
1123 complain (_("$%d of `%s' has no declared type"),
1128 complain (_("$%s is invalid"), printable_version (c
));
1132 copy_at (finput
, fguard
, stack_offset
);
1137 _("unterminated %guard clause"));
1143 if (c
!= '}' || count
!= 0)
1147 c
= skip_white_space();
1149 fprintf(fguard
, ";\n break;}");
1151 copy_action (rule
, stack_offset
);
1154 c
= getc(finput
); /* why not skip_white_space -wjh */
1156 copy_action (rule
, stack_offset
);
1164 /* Assuming that a `{' has just been seen, copy everything up to the
1165 matching `}' into the actions file. STACK_OFFSET is the number of
1166 values in the current rule so far, which says where to find `$0'
1167 with respect to the top of the stack. */
1170 copy_action (symbol_list
*rule
, int stack_offset
)
1175 register char *type_name
;
1177 /* offset is always 0 if parser has already popped the stack pointer */
1178 if (semantic_parser
)
1181 fprintf (faction
, "\ncase %d:\n", nrules
);
1183 fprintf (faction
, "#line %d \"%s\"\n", lineno
, infile
);
1184 putc ('{', faction
);
1207 copy_string (finput
, faction
, c
);
1213 if (c
!= '*' && c
!= '/')
1215 copy_comment (finput
, faction
, c
);
1224 register char *cp
= token_buffer
;
1226 while ((c
= getc(finput
)) != '>' && c
> 0)
1228 if (cp
== token_buffer
+ maxtoken
)
1229 cp
= grow_token_buffer(cp
);
1234 type_name
= token_buffer
;
1235 value_components_used
= 1;
1241 fprintf(faction
, "yyval");
1243 type_name
= get_type_name(0, rule
);
1245 fprintf(faction
, ".%s", type_name
);
1246 if(!type_name
&& typed
)
1247 complain (_("$$ of `%s' has no declared type"),
1250 else if (isdigit(c
) || c
== '-')
1253 n
= read_signed_integer(finput
);
1256 if (!type_name
&& n
> 0)
1257 type_name
= get_type_name(n
, rule
);
1259 fprintf(faction
, "yyvsp[%d]", n
- stack_offset
);
1261 fprintf(faction
, ".%s", type_name
);
1262 if(!type_name
&& typed
)
1263 complain (_("$%d of `%s' has no declared type"),
1268 complain (_("$%s is invalid"), printable_version (c
));
1273 copy_at (finput
, faction
, stack_offset
);
1277 fatal (_("unmatched %s"), "`{'");
1286 /* above loop exits when c is '}' */
1295 fprintf(faction
, ";\n break;}");
1300 /* generate a dummy symbol, a nonterminal,
1301 whose name cannot conflict with the user's names. */
1306 register bucket
*sym
;
1308 sprintf (token_buffer
, "@%d", ++gensym_count
);
1309 sym
= getsym(token_buffer
);
1310 sym
->class = SNTERM
;
1311 sym
->value
= nvars
++;
1315 /* Parse the input grammar into a one symbol_list structure.
1316 Each rule is represented by a sequence of symbols: the left hand side
1317 followed by the contents of the right hand side, followed by a null pointer
1318 instead of a symbol to terminate the rule.
1319 The next symbol is the lhs of the following rule.
1321 All guards and actions are copied out to the appropriate files,
1322 labelled by the rule number they apply to. */
1328 register bucket
*lhs
= NULL
;
1329 register symbol_list
*p
;
1330 register symbol_list
*p1
;
1331 register bucket
*bp
;
1333 symbol_list
*crule
; /* points to first symbol_list of current rule. */
1334 /* its symbol is the lhs of the rule. */
1335 symbol_list
*crule1
; /* points to the symbol_list preceding crule. */
1341 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1343 if (t
== IDENTIFIER
|| t
== BAR
)
1345 register int actionflag
= 0;
1346 int rulelength
= 0; /* number of symbols in rhs of this rule so far */
1347 int xactions
= 0; /* JF for error checking */
1348 bucket
*first_rhs
= 0;
1350 if (t
== IDENTIFIER
)
1363 complain (_("ill-formed rule: initial symbol not followed by colon"));
1368 if (nrules
== 0 && t
== BAR
)
1370 complain (_("grammar starts with vertical bar"));
1371 lhs
= symval
; /* BOGUS: use a random symval */
1373 /* start a new rule and record its lhs. */
1378 record_rule_line ();
1380 p
= NEW(symbol_list
);
1392 /* mark the rule's lhs as a nonterminal if not already so. */
1394 if (lhs
->class == SUNKNOWN
)
1396 lhs
->class = SNTERM
;
1400 else if (lhs
->class == STOKEN
)
1401 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1403 /* read the rhs of the rule. */
1411 crule
->ruleprec
= symval
;
1415 if (! (t
== IDENTIFIER
|| t
== LEFT_CURLY
)) break;
1417 /* If next token is an identifier, see if a colon follows it.
1418 If one does, exit this rule now. */
1419 if (t
== IDENTIFIER
)
1421 register bucket
*ssave
;
1428 if (t1
== COLON
) break;
1430 if(!first_rhs
) /* JF */
1432 /* Not followed by colon =>
1433 process as part of this rule's rhs. */
1436 /* If we just passed an action, that action was in the middle
1437 of a rule, so make a dummy rule to reduce it to a
1441 register bucket
*sdummy
;
1443 /* Since the action was written out with this rule's */
1444 /* number, we must give the new rule this number */
1445 /* by inserting the new rule before it. */
1447 /* Make a dummy nonterminal, a gensym. */
1450 /* Make a new rule, whose body is empty,
1451 before the current one, so that the action
1452 just read can belong to it. */
1455 record_rule_line ();
1456 p
= NEW(symbol_list
);
1461 crule1
= NEW(symbol_list
);
1463 crule1
->next
= crule
;
1465 /* insert the dummy generated by that rule into this rule. */
1467 p
= NEW(symbol_list
);
1475 if (t
== IDENTIFIER
)
1478 p
= NEW(symbol_list
);
1483 else /* handle an action. */
1485 copy_action(crule
, rulelength
);
1487 xactions
++; /* JF */
1490 } /* end of read rhs of rule */
1492 /* Put an empty link in the list to mark the end of this rule */
1493 p
= NEW(symbol_list
);
1499 complain (_("two @prec's in a row"));
1501 crule
->ruleprec
= symval
;
1506 if (! semantic_parser
)
1508 _("%guard present but %semantic_parser not specified"));
1510 copy_guard(crule
, rulelength
);
1513 else if (t
== LEFT_CURLY
)
1515 /* This case never occurs -wjh */
1517 complain (_("two actions at end of one rule"));
1518 copy_action(crule
, rulelength
);
1520 xactions
++; /* -wjh */
1523 /* If $$ is being set in default way, report if any type
1527 && lhs
->type_name
!= first_rhs
->type_name
)
1529 if (lhs
->type_name
== 0
1530 || first_rhs
->type_name
== 0
1531 || strcmp(lhs
->type_name
,first_rhs
->type_name
))
1532 complain (_("type clash (`%s' `%s') on default action"),
1533 lhs
->type_name
? lhs
->type_name
: "",
1534 first_rhs
->type_name
? first_rhs
->type_name
: "");
1536 /* Warn if there is no default for $$ but we need one. */
1537 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1538 complain (_("empty rule for typed nonterminal, and no action"));
1543 /* these things can appear as alternatives to rules. */
1545 a) none of the documentation allows them
1546 b) most of them scan forward until finding a next %
1547 thus they may swallow lots of intervening rules
1549 else if (t
== TOKEN
)
1551 parse_token_decl(STOKEN
, SNTERM
);
1554 else if (t
== NTERM
)
1556 parse_token_decl(SNTERM
, STOKEN
);
1563 else if (t
== UNION
)
1568 else if (t
== EXPECT
)
1570 parse_expect_decl();
1573 else if (t
== START
)
1582 complain (_("invalid input: %s"), token_buffer
);
1587 /* grammar has been read. Do some checking */
1589 if (nsyms
> MAXSHORT
)
1590 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1593 fatal (_("no rules in the input grammar"));
1595 if (typed
== 0 /* JF put out same default YYSTYPE as YACC does */
1596 && !value_components_used
)
1598 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1599 but it seems better to be consistent.
1600 Most programs should declare their own type anyway. */
1601 fprintf(fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1603 fprintf(fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1606 /* Report any undefined symbols and consider them nonterminals. */
1608 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1609 if (bp
->class == SUNKNOWN
)
1611 complain (_("symbol %s is used, but is not defined as a token and has no rules"),
1614 bp
->value
= nvars
++;
1617 ntokens
= nsyms
- nvars
;
1622 record_rule_line (void)
1624 /* Record each rule's source line number in rline table. */
1626 if (nrules
>= rline_allocated
)
1628 rline_allocated
= nrules
* 2;
1629 rline
= (short *) xrealloc ((char *) rline
,
1630 rline_allocated
* sizeof (short));
1632 rline
[nrules
] = lineno
;
1637 /* read in a %type declaration and record its information for get_type_name to access */
1638 /* this is unused. it is only called from the #if 0 part of readgram */
1644 register char *name
;
1650 complain (_("invalid %s declaration"), "%type");
1654 k
= strlen(token_buffer
);
1655 name
= NEW2(k
+ 1, char);
1656 strcpy(name
, token_buffer
);
1671 if (symval
->type_name
== NULL
)
1672 symval
->type_name
= name
;
1673 else if (strcmp(name
, symval
->type_name
) != 0)
1674 complain (_("type redeclaration for %s"), symval
->tag
);
1686 /* Assign symbol numbers, and write definition of token names into
1687 fdefines. Set up vectors tags and sprec of names and precedences
1693 register bucket
*bp
;
1694 register int tokno
= 1;
1696 register int last_user_token_number
;
1697 static char DOLLAR
[] = "$";
1699 /* int lossage = 0; JF set but not used */
1701 tags
= NEW2(nsyms
+ 1, char *);
1703 user_toknums
= NEW2(nsyms
+ 1, int);
1704 user_toknums
[0] = 0;
1706 sprec
= NEW2(nsyms
, short);
1707 sassoc
= NEW2(nsyms
, short);
1709 max_user_token_number
= 256;
1710 last_user_token_number
= 256;
1712 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1714 if (bp
->class == SNTERM
)
1716 bp
->value
+= ntokens
;
1720 /* this symbol and its alias are a single token defn.
1721 allocate a tokno, and assign to both check agreement of
1722 ->prec and ->assoc fields and make both the same */
1724 bp
->value
= bp
->alias
->value
= tokno
++;
1726 if (bp
->prec
!= bp
->alias
->prec
)
1728 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1729 && bp
->user_token_number
== SALIAS
)
1730 complain (_("conflicting precedences for %s and %s"),
1731 bp
->tag
, bp
->alias
->tag
);
1733 bp
->alias
->prec
= bp
->prec
;
1735 bp
->prec
= bp
->alias
->prec
;
1738 if (bp
->assoc
!= bp
->alias
->assoc
)
1740 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1741 && bp
->user_token_number
== SALIAS
)
1742 complain (_("conflicting assoc values for %s and %s"),
1743 bp
->tag
, bp
->alias
->tag
);
1745 bp
->alias
->assoc
= bp
->assoc
;
1747 bp
->assoc
= bp
->alias
->assoc
;
1750 if (bp
->user_token_number
== SALIAS
)
1751 continue; /* do not do processing below for SALIASs */
1754 else /* bp->class == STOKEN */
1756 bp
->value
= tokno
++;
1759 if (bp
->class == STOKEN
)
1761 if (translations
&& !(bp
->user_token_number
))
1762 bp
->user_token_number
= ++last_user_token_number
;
1763 if (bp
->user_token_number
> max_user_token_number
)
1764 max_user_token_number
= bp
->user_token_number
;
1767 tags
[bp
->value
] = bp
->tag
;
1768 user_toknums
[bp
->value
] = bp
->user_token_number
;
1769 sprec
[bp
->value
] = bp
->prec
;
1770 sassoc
[bp
->value
] = bp
->assoc
;
1778 token_translations
= NEW2(max_user_token_number
+1, short);
1780 /* initialize all entries for literal tokens to 2, the internal
1781 token number for $undefined., which represents all invalid
1783 for (j
= 0; j
<= max_user_token_number
; j
++)
1784 token_translations
[j
] = 2;
1786 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1788 if (bp
->value
>= ntokens
)
1789 continue; /* non-terminal */
1790 if (bp
->user_token_number
== SALIAS
)
1792 if (token_translations
[bp
->user_token_number
] != 2)
1793 complain (_("tokens %s and %s both assigned number %d"),
1794 tags
[token_translations
[bp
->user_token_number
]],
1796 bp
->user_token_number
);
1797 token_translations
[bp
->user_token_number
] = bp
->value
;
1801 error_token_number
= errtoken
->value
;
1804 output_token_defines(ftable
);
1806 if (startval
->class == SUNKNOWN
)
1807 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1808 else if (startval
->class == STOKEN
)
1809 fatal (_("the start symbol %s is a token"), startval
->tag
);
1811 start_symbol
= startval
->value
;
1815 output_token_defines(fdefines
);
1819 if (spec_name_prefix
)
1820 fprintf(fdefines
, "\nextern YYSTYPE %slval;\n", spec_name_prefix
);
1822 fprintf(fdefines
, "\nextern YYSTYPE yylval;\n");
1825 if (semantic_parser
)
1826 for (i
= ntokens
; i
< nsyms
; i
++)
1828 /* don't make these for dummy nonterminals made by gensym. */
1829 if (*tags
[i
] != '@')
1830 fprintf(fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1833 /* `fdefines' is now a temporary file, so we need to copy its
1834 contents in `done', so we can't close it here. */
1841 /* For named tokens, but not literal ones, define the name. The value
1842 is the user token number. */
1845 output_token_defines (FILE *file
)
1848 register char *cp
, *symbol
;
1851 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1853 symbol
= bp
->tag
; /* get symbol */
1855 if (bp
->value
>= ntokens
) continue;
1856 if (bp
->user_token_number
== SALIAS
) continue;
1857 if ('\'' == *symbol
) continue; /* skip literal character */
1858 if (bp
== errtoken
) continue; /* skip error token */
1859 if ('\"' == *symbol
)
1861 /* use literal string only if given a symbol with an alias */
1863 symbol
= bp
->alias
->tag
;
1868 /* Don't #define nonliteral tokens whose names contain periods. */
1870 while ((c
= *cp
++) && c
!= '.');
1871 if (c
!= '\0') continue;
1873 fprintf (file
, "#define\t%s\t%d\n", symbol
,
1874 ((translations
&& ! rawtoknumflag
)
1875 ? bp
->user_token_number
1877 if (semantic_parser
)
1878 fprintf (file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1886 /* convert the rules into the representation using rrhs, rlhs and ritems. */
1891 register int itemno
;
1892 register int ruleno
;
1893 register symbol_list
*p
;
1894 /* register bucket *bp; JF unused */
1898 ritem
= NEW2(nitems
+ 1, short);
1899 rlhs
= NEW2(nrules
, short) - 1;
1900 rrhs
= NEW2(nrules
, short) - 1;
1901 rprec
= NEW2(nrules
, short) - 1;
1902 rprecsym
= NEW2(nrules
, short) - 1;
1903 rassoc
= NEW2(nrules
, short) - 1;
1911 rlhs
[ruleno
] = p
->sym
->value
;
1912 rrhs
[ruleno
] = itemno
;
1913 ruleprec
= p
->ruleprec
;
1918 ritem
[itemno
++] = p
->sym
->value
;
1919 /* A rule gets by default the precedence and associativity
1920 of the last token in it. */
1921 if (p
->sym
->class == STOKEN
)
1923 rprec
[ruleno
] = p
->sym
->prec
;
1924 rassoc
[ruleno
] = p
->sym
->assoc
;
1929 /* If this rule has a %prec,
1930 the specified symbol's precedence replaces the default. */
1933 rprec
[ruleno
] = ruleprec
->prec
;
1934 rassoc
[ruleno
] = ruleprec
->assoc
;
1935 rprecsym
[ruleno
] = ruleprec
->value
;
1938 ritem
[itemno
++] = -ruleno
;
1947 /* Read a signed integer from STREAM and return its value. */
1950 read_signed_integer (FILE *stream
)
1952 register int c
= getc(stream
);
1953 register int sign
= 1;
1964 n
= 10*n
+ (c
- '0');