]>
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' */
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 void reader
PARAMS((void));
100 void reader_output_yylsp
PARAMS((FILE *));
101 void read_declarations
PARAMS((void));
102 void copy_definition
PARAMS((void));
103 void parse_token_decl
PARAMS((int, int));
104 void parse_start_decl
PARAMS((void));
105 void parse_type_decl
PARAMS((void));
106 void parse_assoc_decl
PARAMS((int));
107 void parse_union_decl
PARAMS((void));
108 void parse_expect_decl
PARAMS((void));
109 char *get_type_name
PARAMS((int, symbol_list
*));
110 void copy_guard
PARAMS((symbol_list
*, int));
111 void parse_thong_decl
PARAMS((void));
112 void copy_action
PARAMS((symbol_list
*, int));
113 bucket
*gensym
PARAMS((void));
114 void readgram
PARAMS((void));
115 void record_rule_line
PARAMS((void));
116 void packsymbols
PARAMS((void));
117 void output_token_defines
PARAMS((FILE *));
118 void packgram
PARAMS((void));
119 int read_signed_integer
PARAMS((FILE *));
122 static int get_type
PARAMS((void));
126 symbol_list
*grammar
;
132 /* Nonzero if components of semantic values are used, implying
133 they must be unions. */
134 static int value_components_used
;
136 static int typed
; /* nonzero if %union has been seen. */
138 static int lastprec
; /* incremented for each %left, %right or %nonassoc seen */
140 static int gensym_count
; /* incremented for each generated symbol */
142 static bucket
*errtoken
;
143 static bucket
*undeftoken
;
145 /* Nonzero if any action or guard uses the @n construct. */
146 static int yylsp_needed
;
150 skip_to_char (int target
)
154 complain (_(" Skipping to next \\n"));
156 complain (_(" Skipping to next %c"), target
);
159 c
= skip_white_space();
160 while (c
!= target
&& c
!= EOF
);
166 /* Dump the string from FINPUT to FOUTPUT. MATCH is the delimiter of
167 the string (either ' or "). */
170 copy_string (FILE *finput
, FILE *foutput
, int match
)
174 putc (match
, foutput
);
180 fatal (_("unterminated string at end of file"));
183 complain (_("unterminated string"));
185 c
= match
; /* invent terminator */
195 fatal (_("unterminated string at end of file"));
208 /* Dump the comment from FINPUT to FOUTPUT. C is either `*' or `/',
209 depending upon the type of comments used. */
212 copy_comment (FILE *finput
, FILE *foutput
, int c
)
217 cplus_comment
= (c
== '/');
224 if (!cplus_comment
&& c
== '*')
248 fatal (_("unterminated comment"));
262 startval
= NULL
; /* start symbol not specified yet. */
265 translations
= 0; /* initially assume token number translation not needed. */
267 /* Nowadays translations is always set to 1,
268 since we give `error' a user-token-number
269 to satisfy the Posix demand for YYERRCODE==256. */
276 rline_allocated
= 10;
277 rline
= NEW2(rline_allocated
, short);
293 /* initialize the symbol table. */
295 /* construct the error token */
296 errtoken
= getsym("error");
297 errtoken
->class = STOKEN
;
298 errtoken
->user_token_number
= 256; /* Value specified by posix. */
299 /* construct a token that represents all undefined literal tokens. */
300 /* it is always token number 2. */
301 undeftoken
= getsym("$undefined.");
302 undeftoken
->class = STOKEN
;
303 undeftoken
->user_token_number
= 2;
304 /* Read the declaration section. Copy %{ ... %} groups to ftable and fdefines file.
305 Also notice any %token, %left, etc. found there. */
307 fprintf(ftable
, "\n/* Bison-generated parse tables, made from %s\n",
310 fprintf(ftable
, "\n/* A Bison parser, made from %s\n", infile
);
311 fprintf(ftable
, " by %s */\n\n", VERSION_STRING
);
312 fprintf(ftable
, "#define YYBISON 1 /* Identify Bison output. */\n\n");
314 /* start writing the guard and action files, if they are needed. */
316 /* read in the grammar, build grammar in list form. write out guards and actions. */
318 /* Now we know whether we need the line-number stack.
319 If we do, write its type into the .tab.h file. */
321 reader_output_yylsp(fdefines
);
322 /* write closing delimiters for actions and guards. */
325 fprintf(ftable
, "#define YYLSP_NEEDED\n\n");
326 /* assign the symbols their symbol numbers.
327 Write #defines for the token symbols into fdefines if requested. */
329 /* convert the grammar into the format described in gram.h. */
331 /* free the symbol table data structure
332 since symbols are now all referred to by symbol number. */
337 reader_output_yylsp (FILE *f
)
340 fprintf(f
, LTYPESTR
);
343 /* Read from finput until `%%' is seen. Discard the `%%'. Handle any
344 `%' declarations, and copy the contents of any `%{ ... %}' groups
348 read_declarations (void)
355 c
= skip_white_space();
359 tok
= parse_percent_token();
366 case PERCENT_LEFT_CURLY
:
371 parse_token_decl (STOKEN
, SNTERM
);
375 parse_token_decl (SNTERM
, STOKEN
);
397 parse_assoc_decl(LEFT_ASSOC
);
401 parse_assoc_decl(RIGHT_ASSOC
);
405 parse_assoc_decl(NON_ASSOC
);
408 case SEMANTIC_PARSER
:
409 if (semantic_parser
== 0)
424 complain (_("unrecognized: %s"), token_buffer
);
429 fatal (_("no input grammar"));
432 complain (_("unknown character: %s"), printable_version(c
));
439 /* Copy the contents of a `%{ ... %}' into the definitions file. The
440 `%{' has already been read. Return after reading the `%}'. */
443 copy_definition (void)
446 /* -1 while reading a character if prev char was %. */
447 register int after_percent
;
450 fprintf(fattrs
, "#line %d \"%s\"\n", lineno
, infile
);
471 copy_string (finput
, fattrs
, c
);
477 if (c
!= '*' && c
!= '/')
479 copy_comment (finput
, fattrs
, c
);
484 _("unterminated `%{' definition"));
506 /* parse what comes after %token or %nterm.
507 For %token, what_is is STOKEN and what_is_not is SNTERM.
508 For %nterm, the arguments are reversed. */
511 parse_token_decl (int what_is
, int what_is_not
)
513 register int token
= 0;
514 register char *typename
= 0;
515 register struct bucket
*symbol
= NULL
; /* pts to symbol being defined */
520 int tmp_char
= ungetc (skip_white_space (), finput
);
525 fatal (_("Premature EOF after %s"), token_buffer
);
533 if (token
== TYPENAME
)
535 k
= strlen(token_buffer
);
536 typename
= NEW2(k
+ 1, char);
537 strcpy(typename
, token_buffer
);
538 value_components_used
= 1;
541 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"'
545 symval
->class = STOKEN
;
546 symval
->type_name
= typename
;
547 symval
->user_token_number
= symbol
->user_token_number
;
548 symbol
->user_token_number
= SALIAS
;
550 symval
->alias
= symbol
;
551 symbol
->alias
= symval
;
554 nsyms
--; /* symbol and symval combined are only one symbol */
556 else if (token
== IDENTIFIER
)
558 int oldclass
= symval
->class;
561 if (symbol
->class == what_is_not
)
562 complain (_("symbol %s redefined"), symbol
->tag
);
563 symbol
->class = what_is
;
564 if (what_is
== SNTERM
&& oldclass
!= SNTERM
)
565 symbol
->value
= nvars
++;
569 if (symbol
->type_name
== NULL
)
570 symbol
->type_name
= typename
;
571 else if (strcmp(typename
, symbol
->type_name
) != 0)
572 complain (_("type redeclaration for %s"), symbol
->tag
);
575 else if (symbol
&& token
== NUMBER
)
577 symbol
->user_token_number
= numval
;
582 complain (_("`%s' is invalid in %s"),
584 (what_is
== STOKEN
) ? "%token" : "%nterm");
591 /* parse what comes after %thong
593 %thong <type> token number literal
594 the <type> or number may be omitted. The number specifies the
597 Two symbols are entered in the table, one for the token symbol and
598 one for the literal. Both are given the <type>, if any, from the declaration.
599 The ->user_token_number of the first is SALIAS and the ->user_token_number
600 of the second is set to the number, if any, from the declaration.
601 The two symbols are linked via pointers in their ->alias fields.
603 during output_defines_table, the symbol is reported
604 thereafter, only the literal string is retained
605 it is the literal string that is output to yytname
609 parse_thong_decl (void)
612 register struct bucket
*symbol
;
613 register char *typename
= 0;
617 token
= lex(); /* fetch typename or first token */
618 if (token
== TYPENAME
) {
619 k
= strlen(token_buffer
);
620 typename
= NEW2(k
+ 1, char);
621 strcpy(typename
, token_buffer
);
622 value_components_used
= 1;
623 token
= lex(); /* fetch first token */
626 /* process first token */
628 if (token
!= IDENTIFIER
)
630 complain (_("unrecognized item %s, expected an identifier"),
635 symval
->class = STOKEN
;
636 symval
->type_name
= typename
;
637 symval
->user_token_number
= SALIAS
;
640 token
= lex(); /* get number or literal string */
642 if (token
== NUMBER
) {
644 token
= lex(); /* okay, did number, now get literal */
648 /* process literal string token */
650 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
652 complain (_("expected string constant instead of %s"),
657 symval
->class = STOKEN
;
658 symval
->type_name
= typename
;
659 symval
->user_token_number
= usrtoknum
;
661 symval
->alias
= symbol
;
662 symbol
->alias
= symval
;
664 nsyms
--; /* symbol and symval combined are only one symbol */
668 /* Parse what comes after %start */
671 parse_start_decl (void)
674 complain ("%s", _("multiple %start declarations"));
675 if (lex() != IDENTIFIER
)
676 complain ("%s", _("invalid %start declaration"));
686 /* read in a %type declaration and record its information for get_type_name to access */
689 parse_type_decl (void)
694 if (lex() != TYPENAME
)
696 complain ("%s", _("%type declaration has no <typename>"));
701 k
= strlen(token_buffer
);
702 name
= NEW2(k
+ 1, char);
703 strcpy(name
, token_buffer
);
708 int tmp_char
= ungetc (skip_white_space (), finput
);
713 fatal (_("Premature EOF after %s"), token_buffer
);
725 if (symval
->type_name
== NULL
)
726 symval
->type_name
= name
;
727 else if (strcmp(name
, symval
->type_name
) != 0)
728 complain (_("type redeclaration for %s"), symval
->tag
);
733 complain (_("invalid %%type declaration due to item: %s"),
742 /* read in a %left, %right or %nonassoc declaration and record its information. */
743 /* assoc is either LEFT_ASSOC, RIGHT_ASSOC or NON_ASSOC. */
746 parse_assoc_decl (int assoc
)
749 register char *name
= NULL
;
750 register int prev
= 0;
752 lastprec
++; /* Assign a new precedence level, never 0. */
757 int tmp_char
= ungetc (skip_white_space (), finput
);
762 fatal (_("Premature EOF after %s"), token_buffer
);
770 k
= strlen(token_buffer
);
771 name
= NEW2(k
+ 1, char);
772 strcpy(name
, token_buffer
);
779 if (symval
->prec
!= 0)
780 complain (_("redefining precedence of %s"), symval
->tag
);
781 symval
->prec
= lastprec
;
782 symval
->assoc
= assoc
;
783 if (symval
->class == SNTERM
)
784 complain (_("symbol %s redefined"), symval
->tag
);
785 symval
->class = STOKEN
;
787 { /* record the type, if one is specified */
788 if (symval
->type_name
== NULL
)
789 symval
->type_name
= name
;
790 else if (strcmp(name
, symval
->type_name
) != 0)
791 complain (_("type redeclaration for %s"), symval
->tag
);
796 if (prev
== IDENTIFIER
)
798 symval
->user_token_number
= numval
;
803 complain (_("invalid text (%s) - number should be after identifier"),
813 complain (_("unexpected item: %s"), token_buffer
);
824 /* copy the union declaration into fattrs (and fdefines),
825 where it is made into the
826 definition of YYSTYPE, the type of elements of the parser value stack. */
829 parse_union_decl (void)
833 register int in_comment
;
837 complain ("%s", _("multiple %union declarations"));
842 fprintf(fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
844 fprintf(fattrs
, "\n");
846 fprintf(fattrs
, "typedef union");
848 fprintf(fdefines
, "typedef union");
869 if (c
!= '*' && c
!= '/')
876 cplus_comment
= (c
== '/');
895 fatal (_("unterminated comment at end of file"));
897 if (!cplus_comment
&& c
== '*')
921 complain (_("unmatched close-brace (`}')"));
925 fprintf(fattrs
, " YYSTYPE;\n");
927 fprintf(fdefines
, " YYSTYPE;\n");
928 /* JF don't choke on trailing semi */
929 c
=skip_white_space();
930 if(c
!=';') ungetc(c
,finput
);
939 /* parse the declaration %expect N which says to expect N
940 shift-reduce conflicts. */
943 parse_expect_decl (void)
950 while (c
== ' ' || c
== '\t')
954 while (c
>= '0' && c
<= '9')
964 if (count
<= 0 || count
> 10)
965 complain ("%s", _("argument of %expect is not an integer"));
966 expected_conflicts
= atoi (buffer
);
969 /* that's all of parsing the declaration section */
971 /* FINPUT is pointing to a location (i.e., a `@'). Output to FOUTPUT
972 a reference to this location. STACK_OFFSET is the number of values
973 in the current rule so far, which says where to find `$0' with
974 respect to the top of the stack. */
976 copy_at (FILE *finput
, FILE *foutput
, int stack_offset
)
983 fprintf (foutput
, "yyloc");
986 else if (isdigit(c
) || c
== '-')
991 n
= read_signed_integer (finput
);
993 fprintf (foutput
, "yylsp[%d]", n
- stack_offset
);
997 complain (_("@%s is invalid"), printable_version (c
));
1001 /* Get the data type (alternative in the union) of the value for
1002 symbol n in rule rule. */
1005 get_type_name (int n
, symbol_list
*rule
)
1008 register symbol_list
*rp
;
1012 complain (_("invalid $ value"));
1022 if (rp
== NULL
|| rp
->sym
== NULL
)
1024 complain (_("invalid $ value"));
1030 return rp
->sym
->type_name
;
1035 /* After `%guard' is seen in the input file, copy the actual guard
1036 into the guards file. If the guard is followed by an action, copy
1037 that into the actions file. STACK_OFFSET is the number of values
1038 in the current rule so far, which says where to find `$0' with
1039 respect to the top of the stack, for the simple parser in which the
1040 stack is not popped until after the guard is run. */
1043 copy_guard (symbol_list
*rule
, int stack_offset
)
1048 register char *type_name
;
1051 /* offset is always 0 if parser has already popped the stack pointer */
1052 if (semantic_parser
) stack_offset
= 0;
1054 fprintf(fguard
, "\ncase %d:\n", nrules
);
1056 fprintf (fguard
, "#line %d \"%s\"\n", lineno
, infile
);
1062 while (brace_flag
? (count
> 0) : (c
!= ';'))
1083 complain (_("unmatched right brace (`}')"));
1084 c
= getc(finput
); /* skip it */
1090 copy_string (finput
, fguard
, c
);
1096 if (c
!= '*' && c
!= '/')
1098 copy_comment (finput
, fguard
, c
);
1107 register char *cp
= token_buffer
;
1109 while ((c
= getc(finput
)) != '>' && c
> 0)
1111 if (cp
== token_buffer
+ maxtoken
)
1112 cp
= grow_token_buffer(cp
);
1117 type_name
= token_buffer
;
1124 fprintf(fguard
, "yyval");
1126 type_name
= rule
->sym
->type_name
;
1128 fprintf(fguard
, ".%s", type_name
);
1129 if(!type_name
&& typed
)
1130 complain (_("$$ of `%s' has no declared type"),
1133 else if (isdigit(c
) || c
== '-')
1136 n
= read_signed_integer (finput
);
1139 if (!type_name
&& n
> 0)
1140 type_name
= get_type_name(n
, rule
);
1142 fprintf(fguard
, "yyvsp[%d]", n
- stack_offset
);
1144 fprintf(fguard
, ".%s", type_name
);
1145 if (!type_name
&& typed
)
1146 complain (_("$%d of `%s' has no declared type"),
1151 complain (_("$%s is invalid"), printable_version (c
));
1155 copy_at (finput
, fguard
, stack_offset
);
1160 _("unterminated %guard clause"));
1166 if (c
!= '}' || count
!= 0)
1170 c
= skip_white_space();
1172 fprintf(fguard
, ";\n break;}");
1174 copy_action (rule
, stack_offset
);
1177 c
= getc(finput
); /* why not skip_white_space -wjh */
1179 copy_action (rule
, stack_offset
);
1187 /* Assuming that a `{' has just been seen, copy everything up to the
1188 matching `}' into the actions file. STACK_OFFSET is the number of
1189 values in the current rule so far, which says where to find `$0'
1190 with respect to the top of the stack. */
1193 copy_action (symbol_list
*rule
, int stack_offset
)
1198 register char *type_name
;
1200 /* offset is always 0 if parser has already popped the stack pointer */
1201 if (semantic_parser
)
1204 fprintf (faction
, "\ncase %d:\n", nrules
);
1206 fprintf (faction
, "#line %d \"%s\"\n", lineno
, infile
);
1207 putc ('{', faction
);
1230 copy_string (finput
, faction
, c
);
1236 if (c
!= '*' && c
!= '/')
1238 copy_comment (finput
, faction
, c
);
1247 register char *cp
= token_buffer
;
1249 while ((c
= getc(finput
)) != '>' && c
> 0)
1251 if (cp
== token_buffer
+ maxtoken
)
1252 cp
= grow_token_buffer(cp
);
1257 type_name
= token_buffer
;
1258 value_components_used
= 1;
1264 fprintf(faction
, "yyval");
1266 type_name
= get_type_name(0, rule
);
1268 fprintf(faction
, ".%s", type_name
);
1269 if(!type_name
&& typed
)
1270 complain (_("$$ of `%s' has no declared type"),
1273 else if (isdigit(c
) || c
== '-')
1276 n
= read_signed_integer(finput
);
1279 if (!type_name
&& n
> 0)
1280 type_name
= get_type_name(n
, rule
);
1282 fprintf(faction
, "yyvsp[%d]", n
- stack_offset
);
1284 fprintf(faction
, ".%s", type_name
);
1285 if(!type_name
&& typed
)
1286 complain (_("$%d of `%s' has no declared type"),
1291 complain (_("$%s is invalid"), printable_version (c
));
1296 copy_at (finput
, faction
, stack_offset
);
1300 fatal (_("unmatched `{'"));
1309 /* above loop exits when c is '}' */
1318 fprintf(faction
, ";\n break;}");
1323 /* generate a dummy symbol, a nonterminal,
1324 whose name cannot conflict with the user's names. */
1329 register bucket
*sym
;
1331 sprintf (token_buffer
, "@%d", ++gensym_count
);
1332 sym
= getsym(token_buffer
);
1333 sym
->class = SNTERM
;
1334 sym
->value
= nvars
++;
1338 /* Parse the input grammar into a one symbol_list structure.
1339 Each rule is represented by a sequence of symbols: the left hand side
1340 followed by the contents of the right hand side, followed by a null pointer
1341 instead of a symbol to terminate the rule.
1342 The next symbol is the lhs of the following rule.
1344 All guards and actions are copied out to the appropriate files,
1345 labelled by the rule number they apply to. */
1351 register bucket
*lhs
= NULL
;
1352 register symbol_list
*p
;
1353 register symbol_list
*p1
;
1354 register bucket
*bp
;
1356 symbol_list
*crule
; /* points to first symbol_list of current rule. */
1357 /* its symbol is the lhs of the rule. */
1358 symbol_list
*crule1
; /* points to the symbol_list preceding crule. */
1364 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1366 if (t
== IDENTIFIER
|| t
== BAR
)
1368 register int actionflag
= 0;
1369 int rulelength
= 0; /* number of symbols in rhs of this rule so far */
1370 int xactions
= 0; /* JF for error checking */
1371 bucket
*first_rhs
= 0;
1373 if (t
== IDENTIFIER
)
1386 complain (_("ill-formed rule: initial symbol not followed by colon"));
1391 if (nrules
== 0 && t
== BAR
)
1393 complain (_("grammar starts with vertical bar"));
1394 lhs
= symval
; /* BOGUS: use a random symval */
1396 /* start a new rule and record its lhs. */
1401 record_rule_line ();
1403 p
= NEW(symbol_list
);
1415 /* mark the rule's lhs as a nonterminal if not already so. */
1417 if (lhs
->class == SUNKNOWN
)
1419 lhs
->class = SNTERM
;
1423 else if (lhs
->class == STOKEN
)
1424 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1426 /* read the rhs of the rule. */
1434 crule
->ruleprec
= symval
;
1438 if (! (t
== IDENTIFIER
|| t
== LEFT_CURLY
)) break;
1440 /* If next token is an identifier, see if a colon follows it.
1441 If one does, exit this rule now. */
1442 if (t
== IDENTIFIER
)
1444 register bucket
*ssave
;
1451 if (t1
== COLON
) break;
1453 if(!first_rhs
) /* JF */
1455 /* Not followed by colon =>
1456 process as part of this rule's rhs. */
1459 /* If we just passed an action, that action was in the middle
1460 of a rule, so make a dummy rule to reduce it to a
1464 register bucket
*sdummy
;
1466 /* Since the action was written out with this rule's */
1467 /* number, we must give the new rule this number */
1468 /* by inserting the new rule before it. */
1470 /* Make a dummy nonterminal, a gensym. */
1473 /* Make a new rule, whose body is empty,
1474 before the current one, so that the action
1475 just read can belong to it. */
1478 record_rule_line ();
1479 p
= NEW(symbol_list
);
1484 crule1
= NEW(symbol_list
);
1486 crule1
->next
= crule
;
1488 /* insert the dummy generated by that rule into this rule. */
1490 p
= NEW(symbol_list
);
1498 if (t
== IDENTIFIER
)
1501 p
= NEW(symbol_list
);
1506 else /* handle an action. */
1508 copy_action(crule
, rulelength
);
1510 xactions
++; /* JF */
1513 } /* end of read rhs of rule */
1515 /* Put an empty link in the list to mark the end of this rule */
1516 p
= NEW(symbol_list
);
1522 complain (_("two @prec's in a row"));
1524 crule
->ruleprec
= symval
;
1529 if (! semantic_parser
)
1531 _("%guard present but %semantic_parser not specified"));
1533 copy_guard(crule
, rulelength
);
1536 else if (t
== LEFT_CURLY
)
1538 /* This case never occurs -wjh */
1540 complain (_("two actions at end of one rule"));
1541 copy_action(crule
, rulelength
);
1543 xactions
++; /* -wjh */
1546 /* If $$ is being set in default way, report if any type
1550 && lhs
->type_name
!= first_rhs
->type_name
)
1552 if (lhs
->type_name
== 0
1553 || first_rhs
->type_name
== 0
1554 || strcmp(lhs
->type_name
,first_rhs
->type_name
))
1555 complain (_("type clash (`%s' `%s') on default action"),
1556 lhs
->type_name
? lhs
->type_name
: "",
1557 first_rhs
->type_name
? first_rhs
->type_name
: "");
1559 /* Warn if there is no default for $$ but we need one. */
1560 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1561 complain (_("empty rule for typed nonterminal, and no action"));
1566 /* these things can appear as alternatives to rules. */
1568 a) none of the documentation allows them
1569 b) most of them scan forward until finding a next %
1570 thus they may swallow lots of intervening rules
1572 else if (t
== TOKEN
)
1574 parse_token_decl(STOKEN
, SNTERM
);
1577 else if (t
== NTERM
)
1579 parse_token_decl(SNTERM
, STOKEN
);
1586 else if (t
== UNION
)
1591 else if (t
== EXPECT
)
1593 parse_expect_decl();
1596 else if (t
== START
)
1605 complain (_("invalid input: %s"), token_buffer
);
1610 /* grammar has been read. Do some checking */
1612 if (nsyms
> MAXSHORT
)
1613 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1616 fatal (_("no rules in the input grammar"));
1618 if (typed
== 0 /* JF put out same default YYSTYPE as YACC does */
1619 && !value_components_used
)
1621 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1622 but it seems better to be consistent.
1623 Most programs should declare their own type anyway. */
1624 fprintf(fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1626 fprintf(fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1629 /* Report any undefined symbols and consider them nonterminals. */
1631 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1632 if (bp
->class == SUNKNOWN
)
1634 complain (_("symbol %s is used, but is not defined as a token and has no rules"),
1637 bp
->value
= nvars
++;
1640 ntokens
= nsyms
- nvars
;
1645 record_rule_line (void)
1647 /* Record each rule's source line number in rline table. */
1649 if (nrules
>= rline_allocated
)
1651 rline_allocated
= nrules
* 2;
1652 rline
= (short *) xrealloc ((char *) rline
,
1653 rline_allocated
* sizeof (short));
1655 rline
[nrules
] = lineno
;
1660 /* read in a %type declaration and record its information for get_type_name to access */
1661 /* this is unused. it is only called from the #if 0 part of readgram */
1667 register char *name
;
1673 complain (_("ill-formed %type declaration"));
1677 k
= strlen(token_buffer
);
1678 name
= NEW2(k
+ 1, char);
1679 strcpy(name
, token_buffer
);
1694 if (symval
->type_name
== NULL
)
1695 symval
->type_name
= name
;
1696 else if (strcmp(name
, symval
->type_name
) != 0)
1697 complain (_("type redeclaration for %s"), symval
->tag
);
1709 /* Assign symbol numbers, and write definition of token names into
1710 fdefines. Set up vectors tags and sprec of names and precedences
1716 register bucket
*bp
;
1717 register int tokno
= 1;
1719 register int last_user_token_number
;
1721 /* int lossage = 0; JF set but not used */
1723 tags
= NEW2(nsyms
+ 1, char *);
1725 user_toknums
= NEW2(nsyms
+ 1, int);
1726 user_toknums
[0] = 0;
1728 sprec
= NEW2(nsyms
, short);
1729 sassoc
= NEW2(nsyms
, short);
1731 max_user_token_number
= 256;
1732 last_user_token_number
= 256;
1734 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1736 if (bp
->class == SNTERM
)
1738 bp
->value
+= ntokens
;
1742 /* this symbol and its alias are a single token defn.
1743 allocate a tokno, and assign to both check agreement of
1744 ->prec and ->assoc fields and make both the same */
1746 bp
->value
= bp
->alias
->value
= tokno
++;
1748 if (bp
->prec
!= bp
->alias
->prec
)
1750 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1751 && bp
->user_token_number
== SALIAS
)
1752 complain (_("conflicting precedences for %s and %s"),
1753 bp
->tag
, bp
->alias
->tag
);
1755 bp
->alias
->prec
= bp
->prec
;
1757 bp
->prec
= bp
->alias
->prec
;
1760 if (bp
->assoc
!= bp
->alias
->assoc
)
1762 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1763 && bp
->user_token_number
== SALIAS
)
1764 complain (_("conflicting assoc values for %s and %s"),
1765 bp
->tag
, bp
->alias
->tag
);
1767 bp
->alias
->assoc
= bp
->assoc
;
1769 bp
->assoc
= bp
->alias
->assoc
;
1772 if (bp
->user_token_number
== SALIAS
)
1773 continue; /* do not do processing below for SALIASs */
1776 else /* bp->class == STOKEN */
1778 bp
->value
= tokno
++;
1781 if (bp
->class == STOKEN
)
1783 if (translations
&& !(bp
->user_token_number
))
1784 bp
->user_token_number
= ++last_user_token_number
;
1785 if (bp
->user_token_number
> max_user_token_number
)
1786 max_user_token_number
= bp
->user_token_number
;
1789 tags
[bp
->value
] = bp
->tag
;
1790 user_toknums
[bp
->value
] = bp
->user_token_number
;
1791 sprec
[bp
->value
] = bp
->prec
;
1792 sassoc
[bp
->value
] = bp
->assoc
;
1800 token_translations
= NEW2(max_user_token_number
+1, short);
1802 /* initialize all entries for literal tokens to 2, the internal
1803 token number for $undefined., which represents all invalid
1805 for (i
= 0; i
<= max_user_token_number
; i
++)
1806 token_translations
[i
] = 2;
1808 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1810 if (bp
->value
>= ntokens
)
1811 continue; /* non-terminal */
1812 if (bp
->user_token_number
== SALIAS
)
1814 if (token_translations
[bp
->user_token_number
] != 2)
1815 complain (_("tokens %s and %s both assigned number %d"),
1816 tags
[token_translations
[bp
->user_token_number
]],
1818 bp
->user_token_number
);
1819 token_translations
[bp
->user_token_number
] = bp
->value
;
1823 error_token_number
= errtoken
->value
;
1826 output_token_defines(ftable
);
1828 if (startval
->class == SUNKNOWN
)
1829 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1830 else if (startval
->class == STOKEN
)
1831 fatal (_("the start symbol %s is a token"), startval
->tag
);
1833 start_symbol
= startval
->value
;
1837 output_token_defines(fdefines
);
1841 if (spec_name_prefix
)
1842 fprintf(fdefines
, "\nextern YYSTYPE %slval;\n", spec_name_prefix
);
1844 fprintf(fdefines
, "\nextern YYSTYPE yylval;\n");
1847 if (semantic_parser
)
1848 for (i
= ntokens
; i
< nsyms
; i
++)
1850 /* don't make these for dummy nonterminals made by gensym. */
1851 if (*tags
[i
] != '@')
1852 fprintf(fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1855 /* `fdefines' is now a temporary file, so we need to copy its
1856 contents in `done', so we can't close it here. */
1863 /* For named tokens, but not literal ones, define the name. The value
1864 is the user token number. */
1867 output_token_defines (FILE *file
)
1870 register char *cp
, *symbol
;
1873 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1875 symbol
= bp
->tag
; /* get symbol */
1877 if (bp
->value
>= ntokens
) continue;
1878 if (bp
->user_token_number
== SALIAS
) continue;
1879 if ('\'' == *symbol
) continue; /* skip literal character */
1880 if (bp
== errtoken
) continue; /* skip error token */
1881 if ('\"' == *symbol
)
1883 /* use literal string only if given a symbol with an alias */
1885 symbol
= bp
->alias
->tag
;
1890 /* Don't #define nonliteral tokens whose names contain periods. */
1892 while ((c
= *cp
++) && c
!= '.');
1893 if (c
!= '\0') continue;
1895 fprintf (file
, "#define\t%s\t%d\n", symbol
,
1896 ((translations
&& ! rawtoknumflag
)
1897 ? bp
->user_token_number
1899 if (semantic_parser
)
1900 fprintf (file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1908 /* convert the rules into the representation using rrhs, rlhs and ritems. */
1913 register int itemno
;
1914 register int ruleno
;
1915 register symbol_list
*p
;
1916 /* register bucket *bp; JF unused */
1920 ritem
= NEW2(nitems
+ 1, short);
1921 rlhs
= NEW2(nrules
, short) - 1;
1922 rrhs
= NEW2(nrules
, short) - 1;
1923 rprec
= NEW2(nrules
, short) - 1;
1924 rprecsym
= NEW2(nrules
, short) - 1;
1925 rassoc
= NEW2(nrules
, short) - 1;
1933 rlhs
[ruleno
] = p
->sym
->value
;
1934 rrhs
[ruleno
] = itemno
;
1935 ruleprec
= p
->ruleprec
;
1940 ritem
[itemno
++] = p
->sym
->value
;
1941 /* A rule gets by default the precedence and associativity
1942 of the last token in it. */
1943 if (p
->sym
->class == STOKEN
)
1945 rprec
[ruleno
] = p
->sym
->prec
;
1946 rassoc
[ruleno
] = p
->sym
->assoc
;
1951 /* If this rule has a %prec,
1952 the specified symbol's precedence replaces the default. */
1955 rprec
[ruleno
] = ruleprec
->prec
;
1956 rassoc
[ruleno
] = ruleprec
->assoc
;
1957 rprecsym
[ruleno
] = ruleprec
->value
;
1960 ritem
[itemno
++] = -ruleno
;
1969 /* Read a signed integer from STREAM and return its value. */
1972 read_signed_integer (FILE *stream
)
1974 register int c
= getc(stream
);
1975 register int sign
= 1;
1986 n
= 10*n
+ (c
- '0');