]>
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 tabinit
PARAMS((void));
72 extern void free_symtab
PARAMS((void));
73 extern void open_extra_files
PARAMS((void));
74 extern char *printable_version
PARAMS((int));
79 struct symbol_list
*next
;
86 extern void reader
PARAMS((void));
87 extern void reader_output_yylsp
PARAMS((FILE *));
89 static void read_declarations
PARAMS((void));
90 static void copy_definition
PARAMS((void));
91 static void parse_token_decl
PARAMS((int, int));
92 static void parse_start_decl
PARAMS((void));
93 static void parse_type_decl
PARAMS((void));
94 static void parse_assoc_decl
PARAMS((int));
95 static void parse_union_decl
PARAMS((void));
96 static void parse_expect_decl
PARAMS((void));
97 static char *get_type_name
PARAMS((int, symbol_list
*));
98 static void copy_guard
PARAMS((symbol_list
*, int));
99 static void parse_thong_decl
PARAMS((void));
100 static void copy_action
PARAMS((symbol_list
*, int));
101 static bucket
*gensym
PARAMS((void));
102 static void readgram
PARAMS((void));
103 static void record_rule_line
PARAMS((void));
104 static void packsymbols
PARAMS((void));
105 static void output_token_defines
PARAMS((FILE *));
106 static void packgram
PARAMS((void));
109 static int get_type
PARAMS((void));
115 static symbol_list
*grammar
;
116 static int start_flag
;
117 static bucket
*startval
;
119 /* Nonzero if components of semantic values are used, implying
120 they must be unions. */
121 static int value_components_used
;
123 static int typed
; /* nonzero if %union has been seen. */
125 static int lastprec
; /* incremented for each %left, %right or %nonassoc seen */
127 static int gensym_count
; /* incremented for each generated symbol */
129 static bucket
*errtoken
;
130 static bucket
*undeftoken
;
132 /* Nonzero if any action or guard uses the @n construct. */
133 static int yylsp_needed
;
136 /*===================\
137 | Low level lexing. |
138 \===================*/
141 skip_to_char (int target
)
145 complain (_(" Skipping to next \\n"));
147 complain (_(" Skipping to next %c"), target
);
150 c
= skip_white_space ();
151 while (c
!= target
&& c
!= EOF
);
157 /*---------------------------------------------------------.
158 | Read a signed integer from STREAM and return its value. |
159 `---------------------------------------------------------*/
162 read_signed_integer (FILE *stream
)
164 register int c
= getc (stream
);
165 register int sign
= 1;
176 n
= 10 * n
+ (c
- '0');
185 /*-------------------------------------------------------------------.
186 | Dump the string from FINPUT to FOUTPUT. MATCH is the delimiter of |
187 | the string (either ' or "). |
188 `-------------------------------------------------------------------*/
191 copy_string (FILE *fin
, FILE *fout
, int match
)
201 fatal (_("unterminated string at end of file"));
204 complain (_("unterminated string"));
206 c
= match
; /* invent terminator */
216 fatal (_("unterminated string at end of file"));
229 /*---------------------------------------------------------------.
230 | Dump the comment from IN to OUT1 and OUT2. C is either `*' or |
231 | `/', depending upon the type of comments used. OUT2 might be |
233 `---------------------------------------------------------------*/
236 copy_comment2 (FILE *in
, FILE *out1
, FILE* out2
, int c
)
241 cplus_comment
= (c
== '/');
250 if (!cplus_comment
&& c
== '*')
280 fatal (_("unterminated comment"));
292 /*------------------------------------------------------------.
293 | Dump the comment from FIN to FOUT. C is either `*' or `/', |
294 | depending upon the type of comments used. |
295 `------------------------------------------------------------*/
298 copy_comment (FILE *fin
, FILE *fout
, int c
)
300 copy_comment2 (fin
, fout
, NULL
, c
);
308 startval
= NULL
; /* start symbol not specified yet. */
311 /* initially assume token number translation not needed. */
314 /* Nowadays translations is always set to 1, since we give `error' a
315 user-token-number to satisfy the Posix demand for YYERRCODE==256.
323 rline_allocated
= 10;
324 rline
= NEW2 (rline_allocated
, short);
340 /* Initialize the symbol table. */
342 /* Construct the error token */
343 errtoken
= getsym ("error");
344 errtoken
->class = STOKEN
;
345 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
346 /* Construct a token that represents all undefined literal tokens.
347 It is always token number 2. */
348 undeftoken
= getsym ("$undefined.");
349 undeftoken
->class = STOKEN
;
350 undeftoken
->user_token_number
= 2;
352 /* Read the declaration section. Copy %{ ... %} groups to FTABLE
353 and FDEFINES file. Also notice any %token, %left, etc. found
357 /* %s, made from %s\n\
358 by GNU bison %s. */\n\
360 noparserflag
? "Bison-generated parse tables" : "A Bison parser",
364 fputs ("#define YYBISON 1 /* Identify Bison output. */\n\n", ftable
);
365 read_declarations ();
366 /* Start writing the guard and action files, if they are needed. */
368 /* Read in the grammar, build grammar in list form. Write out
369 guards and actions. */
371 /* Now we know whether we need the line-number stack. If we do,
372 write its type into the .tab.h file. */
374 reader_output_yylsp (fdefines
);
375 /* Write closing delimiters for actions and guards. */
378 fputs ("#define YYLSP_NEEDED\n\n", ftable
);
379 /* Assign the symbols their symbol numbers. Write #defines for the
380 token symbols into FDEFINES if requested. */
382 /* Convert the grammar into the format described in gram.h. */
384 /* Free the symbol table data structure since symbols are now all
385 referred to by symbol number. */
390 reader_output_yylsp (FILE *f
)
393 fprintf(f
, LTYPESTR
);
396 /* Read from finput until `%%' is seen. Discard the `%%'. Handle any
397 `%' declarations, and copy the contents of any `%{ ... %}' groups
401 read_declarations (void)
408 c
= skip_white_space();
412 tok
= parse_percent_token();
419 case PERCENT_LEFT_CURLY
:
424 parse_token_decl (STOKEN
, SNTERM
);
428 parse_token_decl (SNTERM
, STOKEN
);
450 parse_assoc_decl(LEFT_ASSOC
);
454 parse_assoc_decl(RIGHT_ASSOC
);
458 parse_assoc_decl(NON_ASSOC
);
461 case SEMANTIC_PARSER
:
462 if (semantic_parser
== 0)
477 complain (_("unrecognized: %s"), token_buffer
);
482 fatal (_("no input grammar"));
485 complain (_("unknown character: %s"), printable_version(c
));
492 /* Copy the contents of a `%{ ... %}' into the definitions file. The
493 `%{' has already been read. Return after reading the `%}'. */
496 copy_definition (void)
499 /* -1 while reading a character if prev char was %. */
500 register int after_percent
;
503 fprintf(fattrs
, "#line %d \"%s\"\n", lineno
, infile
);
524 copy_string (finput
, fattrs
, c
);
530 if (c
!= '*' && c
!= '/')
532 copy_comment (finput
, fattrs
, c
);
537 _("unterminated `%{' definition"));
559 /* parse what comes after %token or %nterm.
560 For %token, what_is is STOKEN and what_is_not is SNTERM.
561 For %nterm, the arguments are reversed. */
564 parse_token_decl (int what_is
, int what_is_not
)
566 register int token
= 0;
567 register char *typename
= 0;
568 register struct bucket
*symbol
= NULL
; /* pts to symbol being defined */
573 int tmp_char
= ungetc (skip_white_space (), finput
);
578 fatal (_("Premature EOF after %s"), token_buffer
);
586 if (token
== TYPENAME
)
588 k
= strlen(token_buffer
);
589 typename
= NEW2(k
+ 1, char);
590 strcpy(typename
, token_buffer
);
591 value_components_used
= 1;
594 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"'
598 symval
->class = STOKEN
;
599 symval
->type_name
= typename
;
600 symval
->user_token_number
= symbol
->user_token_number
;
601 symbol
->user_token_number
= SALIAS
;
603 symval
->alias
= symbol
;
604 symbol
->alias
= symval
;
607 nsyms
--; /* symbol and symval combined are only one symbol */
609 else if (token
== IDENTIFIER
)
611 int oldclass
= symval
->class;
614 if (symbol
->class == what_is_not
)
615 complain (_("symbol %s redefined"), symbol
->tag
);
616 symbol
->class = what_is
;
617 if (what_is
== SNTERM
&& oldclass
!= SNTERM
)
618 symbol
->value
= nvars
++;
622 if (symbol
->type_name
== NULL
)
623 symbol
->type_name
= typename
;
624 else if (strcmp(typename
, symbol
->type_name
) != 0)
625 complain (_("type redeclaration for %s"), symbol
->tag
);
628 else if (symbol
&& token
== NUMBER
)
630 symbol
->user_token_number
= numval
;
635 complain (_("`%s' is invalid in %s"),
637 (what_is
== STOKEN
) ? "%token" : "%nterm");
644 /* parse what comes after %thong
646 %thong <type> token number literal
647 the <type> or number may be omitted. The number specifies the
650 Two symbols are entered in the table, one for the token symbol and
651 one for the literal. Both are given the <type>, if any, from the declaration.
652 The ->user_token_number of the first is SALIAS and the ->user_token_number
653 of the second is set to the number, if any, from the declaration.
654 The two symbols are linked via pointers in their ->alias fields.
656 during output_defines_table, the symbol is reported
657 thereafter, only the literal string is retained
658 it is the literal string that is output to yytname
662 parse_thong_decl (void)
665 register struct bucket
*symbol
;
666 register char *typename
= 0;
670 token
= lex(); /* fetch typename or first token */
671 if (token
== TYPENAME
) {
672 k
= strlen(token_buffer
);
673 typename
= NEW2(k
+ 1, char);
674 strcpy(typename
, token_buffer
);
675 value_components_used
= 1;
676 token
= lex(); /* fetch first token */
679 /* process first token */
681 if (token
!= IDENTIFIER
)
683 complain (_("unrecognized item %s, expected an identifier"),
688 symval
->class = STOKEN
;
689 symval
->type_name
= typename
;
690 symval
->user_token_number
= SALIAS
;
693 token
= lex(); /* get number or literal string */
695 if (token
== NUMBER
) {
697 token
= lex(); /* okay, did number, now get literal */
701 /* process literal string token */
703 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
705 complain (_("expected string constant instead of %s"),
710 symval
->class = STOKEN
;
711 symval
->type_name
= typename
;
712 symval
->user_token_number
= usrtoknum
;
714 symval
->alias
= symbol
;
715 symbol
->alias
= symval
;
717 nsyms
--; /* symbol and symval combined are only one symbol */
721 /* Parse what comes after %start */
724 parse_start_decl (void)
727 complain (_("multiple %s declarations"), "%start");
728 if (lex () != IDENTIFIER
)
729 complain (_("invalid %s declaration"), "%start");
739 /* read in a %type declaration and record its information for get_type_name to access */
742 parse_type_decl (void)
747 if (lex() != TYPENAME
)
749 complain ("%s", _("%type declaration has no <typename>"));
754 k
= strlen(token_buffer
);
755 name
= NEW2(k
+ 1, char);
756 strcpy(name
, token_buffer
);
761 int tmp_char
= ungetc (skip_white_space (), finput
);
766 fatal (_("Premature EOF after %s"), token_buffer
);
778 if (symval
->type_name
== NULL
)
779 symval
->type_name
= name
;
780 else if (strcmp(name
, symval
->type_name
) != 0)
781 complain (_("type redeclaration for %s"), symval
->tag
);
786 complain (_("invalid %%type declaration due to item: %s"),
795 /* read in a %left, %right or %nonassoc declaration and record its information. */
796 /* assoc is either LEFT_ASSOC, RIGHT_ASSOC or NON_ASSOC. */
799 parse_assoc_decl (int assoc
)
802 register char *name
= NULL
;
803 register int prev
= 0;
805 lastprec
++; /* Assign a new precedence level, never 0. */
810 int tmp_char
= ungetc (skip_white_space (), finput
);
815 fatal (_("Premature EOF after %s"), token_buffer
);
823 k
= strlen(token_buffer
);
824 name
= NEW2(k
+ 1, char);
825 strcpy(name
, token_buffer
);
832 if (symval
->prec
!= 0)
833 complain (_("redefining precedence of %s"), symval
->tag
);
834 symval
->prec
= lastprec
;
835 symval
->assoc
= assoc
;
836 if (symval
->class == SNTERM
)
837 complain (_("symbol %s redefined"), symval
->tag
);
838 symval
->class = STOKEN
;
840 { /* record the type, if one is specified */
841 if (symval
->type_name
== NULL
)
842 symval
->type_name
= name
;
843 else if (strcmp(name
, symval
->type_name
) != 0)
844 complain (_("type redeclaration for %s"), symval
->tag
);
849 if (prev
== IDENTIFIER
)
851 symval
->user_token_number
= numval
;
856 complain (_("invalid text (%s) - number should be after identifier"),
866 complain (_("unexpected item: %s"), token_buffer
);
877 /* copy the union declaration into fattrs (and fdefines),
878 where it is made into the
879 definition of YYSTYPE, the type of elements of the parser value stack. */
882 parse_union_decl (void)
885 register int count
= 0;
888 complain (_("multiple %s declarations"), "%union");
893 fprintf (fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
895 fprintf (fattrs
, "\n");
897 fprintf (fattrs
, "typedef union");
899 fprintf (fdefines
, "typedef union");
917 if (c
!= '*' && c
!= '/')
919 copy_comment2 (finput
, fattrs
, fdefines
, c
);
929 complain (_("unmatched %s"), "`}'");
933 fprintf (fattrs
, " YYSTYPE;\n");
935 fprintf (fdefines
, " YYSTYPE;\n");
936 /* JF don't choke on trailing semi */
937 c
= skip_white_space ();
948 /* parse the declaration %expect N which says to expect N
949 shift-reduce conflicts. */
952 parse_expect_decl (void)
959 while (c
== ' ' || c
== '\t')
963 while (c
>= '0' && c
<= '9')
973 if (count
<= 0 || count
> 10)
974 complain ("%s", _("argument of %expect is not an integer"));
975 expected_conflicts
= atoi (buffer
);
978 /* that's all of parsing the declaration section */
980 /* FIN is pointing to a location (i.e., a `@'). Output to FOUT
981 a reference to this location. STACK_OFFSET is the number of values
982 in the current rule so far, which says where to find `$0' with
983 respect to the top of the stack. */
985 copy_at (FILE *fin
, FILE *fout
, int stack_offset
)
992 fprintf (fout
, "yyloc");
995 else if (isdigit(c
) || c
== '-')
1000 n
= read_signed_integer (fin
);
1002 fprintf (fout
, "yylsp[%d]", n
- stack_offset
);
1006 complain (_("@%s is invalid"), printable_version (c
));
1010 /* Get the data type (alternative in the union) of the value for
1011 symbol n in rule rule. */
1014 get_type_name (int n
, symbol_list
*rule
)
1017 register symbol_list
*rp
;
1021 complain (_("invalid $ value"));
1031 if (rp
== NULL
|| rp
->sym
== NULL
)
1033 complain (_("invalid $ value"));
1039 return rp
->sym
->type_name
;
1044 /* After `%guard' is seen in the input file, copy the actual guard
1045 into the guards file. If the guard is followed by an action, copy
1046 that into the actions file. STACK_OFFSET is the number of values
1047 in the current rule so far, which says where to find `$0' with
1048 respect to the top of the stack, for the simple parser in which the
1049 stack is not popped until after the guard is run. */
1052 copy_guard (symbol_list
*rule
, int stack_offset
)
1057 register char *type_name
;
1060 /* offset is always 0 if parser has already popped the stack pointer */
1061 if (semantic_parser
) stack_offset
= 0;
1063 fprintf(fguard
, "\ncase %d:\n", nrules
);
1065 fprintf (fguard
, "#line %d \"%s\"\n", lineno
, infile
);
1071 while (brace_flag
? (count
> 0) : (c
!= ';'))
1092 complain (_("unmatched %s"), "`}'");
1093 c
= getc(finput
); /* skip it */
1099 copy_string (finput
, fguard
, c
);
1105 if (c
!= '*' && c
!= '/')
1107 copy_comment (finput
, fguard
, c
);
1116 register char *cp
= token_buffer
;
1118 while ((c
= getc(finput
)) != '>' && c
> 0)
1120 if (cp
== token_buffer
+ maxtoken
)
1121 cp
= grow_token_buffer(cp
);
1126 type_name
= token_buffer
;
1133 fprintf(fguard
, "yyval");
1135 type_name
= rule
->sym
->type_name
;
1137 fprintf(fguard
, ".%s", type_name
);
1138 if(!type_name
&& typed
)
1139 complain (_("$$ of `%s' has no declared type"),
1142 else if (isdigit(c
) || c
== '-')
1145 n
= read_signed_integer (finput
);
1148 if (!type_name
&& n
> 0)
1149 type_name
= get_type_name(n
, rule
);
1151 fprintf(fguard
, "yyvsp[%d]", n
- stack_offset
);
1153 fprintf(fguard
, ".%s", type_name
);
1154 if (!type_name
&& typed
)
1155 complain (_("$%d of `%s' has no declared type"),
1160 complain (_("$%s is invalid"), printable_version (c
));
1164 copy_at (finput
, fguard
, stack_offset
);
1169 _("unterminated %guard clause"));
1175 if (c
!= '}' || count
!= 0)
1179 c
= skip_white_space();
1181 fprintf(fguard
, ";\n break;}");
1183 copy_action (rule
, stack_offset
);
1186 c
= getc(finput
); /* why not skip_white_space -wjh */
1188 copy_action (rule
, stack_offset
);
1196 /* Assuming that a `{' has just been seen, copy everything up to the
1197 matching `}' into the actions file. STACK_OFFSET is the number of
1198 values in the current rule so far, which says where to find `$0'
1199 with respect to the top of the stack. */
1202 copy_action (symbol_list
*rule
, int stack_offset
)
1207 register char *type_name
;
1209 /* offset is always 0 if parser has already popped the stack pointer */
1210 if (semantic_parser
)
1213 fprintf (faction
, "\ncase %d:\n", nrules
);
1215 fprintf (faction
, "#line %d \"%s\"\n", lineno
, infile
);
1216 putc ('{', faction
);
1239 copy_string (finput
, faction
, c
);
1245 if (c
!= '*' && c
!= '/')
1247 copy_comment (finput
, faction
, c
);
1256 register char *cp
= token_buffer
;
1258 while ((c
= getc(finput
)) != '>' && c
> 0)
1260 if (cp
== token_buffer
+ maxtoken
)
1261 cp
= grow_token_buffer(cp
);
1266 type_name
= token_buffer
;
1267 value_components_used
= 1;
1273 fprintf(faction
, "yyval");
1275 type_name
= get_type_name(0, rule
);
1277 fprintf(faction
, ".%s", type_name
);
1278 if(!type_name
&& typed
)
1279 complain (_("$$ of `%s' has no declared type"),
1282 else if (isdigit(c
) || c
== '-')
1285 n
= read_signed_integer(finput
);
1288 if (!type_name
&& n
> 0)
1289 type_name
= get_type_name(n
, rule
);
1291 fprintf(faction
, "yyvsp[%d]", n
- stack_offset
);
1293 fprintf(faction
, ".%s", type_name
);
1294 if(!type_name
&& typed
)
1295 complain (_("$%d of `%s' has no declared type"),
1300 complain (_("$%s is invalid"), printable_version (c
));
1305 copy_at (finput
, faction
, stack_offset
);
1309 fatal (_("unmatched %s"), "`{'");
1318 /* above loop exits when c is '}' */
1327 fprintf(faction
, ";\n break;}");
1332 /* generate a dummy symbol, a nonterminal,
1333 whose name cannot conflict with the user's names. */
1338 register bucket
*sym
;
1340 sprintf (token_buffer
, "@%d", ++gensym_count
);
1341 sym
= getsym(token_buffer
);
1342 sym
->class = SNTERM
;
1343 sym
->value
= nvars
++;
1347 /* Parse the input grammar into a one symbol_list structure.
1348 Each rule is represented by a sequence of symbols: the left hand side
1349 followed by the contents of the right hand side, followed by a null pointer
1350 instead of a symbol to terminate the rule.
1351 The next symbol is the lhs of the following rule.
1353 All guards and actions are copied out to the appropriate files,
1354 labelled by the rule number they apply to. */
1360 register bucket
*lhs
= NULL
;
1361 register symbol_list
*p
;
1362 register symbol_list
*p1
;
1363 register bucket
*bp
;
1365 symbol_list
*crule
; /* points to first symbol_list of current rule. */
1366 /* its symbol is the lhs of the rule. */
1367 symbol_list
*crule1
; /* points to the symbol_list preceding crule. */
1373 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1375 if (t
== IDENTIFIER
|| t
== BAR
)
1377 register int actionflag
= 0;
1378 int rulelength
= 0; /* number of symbols in rhs of this rule so far */
1379 int xactions
= 0; /* JF for error checking */
1380 bucket
*first_rhs
= 0;
1382 if (t
== IDENTIFIER
)
1395 complain (_("ill-formed rule: initial symbol not followed by colon"));
1400 if (nrules
== 0 && t
== BAR
)
1402 complain (_("grammar starts with vertical bar"));
1403 lhs
= symval
; /* BOGUS: use a random symval */
1405 /* start a new rule and record its lhs. */
1410 record_rule_line ();
1412 p
= NEW(symbol_list
);
1424 /* mark the rule's lhs as a nonterminal if not already so. */
1426 if (lhs
->class == SUNKNOWN
)
1428 lhs
->class = SNTERM
;
1432 else if (lhs
->class == STOKEN
)
1433 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1435 /* read the rhs of the rule. */
1443 crule
->ruleprec
= symval
;
1447 if (! (t
== IDENTIFIER
|| t
== LEFT_CURLY
)) break;
1449 /* If next token is an identifier, see if a colon follows it.
1450 If one does, exit this rule now. */
1451 if (t
== IDENTIFIER
)
1453 register bucket
*ssave
;
1460 if (t1
== COLON
) break;
1462 if(!first_rhs
) /* JF */
1464 /* Not followed by colon =>
1465 process as part of this rule's rhs. */
1468 /* If we just passed an action, that action was in the middle
1469 of a rule, so make a dummy rule to reduce it to a
1473 register bucket
*sdummy
;
1475 /* Since the action was written out with this rule's */
1476 /* number, we must give the new rule this number */
1477 /* by inserting the new rule before it. */
1479 /* Make a dummy nonterminal, a gensym. */
1482 /* Make a new rule, whose body is empty,
1483 before the current one, so that the action
1484 just read can belong to it. */
1487 record_rule_line ();
1488 p
= NEW(symbol_list
);
1493 crule1
= NEW(symbol_list
);
1495 crule1
->next
= crule
;
1497 /* insert the dummy generated by that rule into this rule. */
1499 p
= NEW(symbol_list
);
1507 if (t
== IDENTIFIER
)
1510 p
= NEW(symbol_list
);
1515 else /* handle an action. */
1517 copy_action(crule
, rulelength
);
1519 xactions
++; /* JF */
1522 } /* end of read rhs of rule */
1524 /* Put an empty link in the list to mark the end of this rule */
1525 p
= NEW(symbol_list
);
1531 complain (_("two @prec's in a row"));
1533 crule
->ruleprec
= symval
;
1538 if (! semantic_parser
)
1540 _("%guard present but %semantic_parser not specified"));
1542 copy_guard(crule
, rulelength
);
1545 else if (t
== LEFT_CURLY
)
1547 /* This case never occurs -wjh */
1549 complain (_("two actions at end of one rule"));
1550 copy_action(crule
, rulelength
);
1552 xactions
++; /* -wjh */
1555 /* If $$ is being set in default way, report if any type
1559 && lhs
->type_name
!= first_rhs
->type_name
)
1561 if (lhs
->type_name
== 0
1562 || first_rhs
->type_name
== 0
1563 || strcmp(lhs
->type_name
,first_rhs
->type_name
))
1564 complain (_("type clash (`%s' `%s') on default action"),
1565 lhs
->type_name
? lhs
->type_name
: "",
1566 first_rhs
->type_name
? first_rhs
->type_name
: "");
1568 /* Warn if there is no default for $$ but we need one. */
1569 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1570 complain (_("empty rule for typed nonterminal, and no action"));
1575 /* these things can appear as alternatives to rules. */
1577 a) none of the documentation allows them
1578 b) most of them scan forward until finding a next %
1579 thus they may swallow lots of intervening rules
1581 else if (t
== TOKEN
)
1583 parse_token_decl(STOKEN
, SNTERM
);
1586 else if (t
== NTERM
)
1588 parse_token_decl(SNTERM
, STOKEN
);
1595 else if (t
== UNION
)
1600 else if (t
== EXPECT
)
1602 parse_expect_decl();
1605 else if (t
== START
)
1614 complain (_("invalid input: %s"), token_buffer
);
1619 /* grammar has been read. Do some checking */
1621 if (nsyms
> MAXSHORT
)
1622 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1625 fatal (_("no rules in the input grammar"));
1627 if (typed
== 0 /* JF put out same default YYSTYPE as YACC does */
1628 && !value_components_used
)
1630 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1631 but it seems better to be consistent.
1632 Most programs should declare their own type anyway. */
1633 fprintf(fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1635 fprintf(fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1638 /* Report any undefined symbols and consider them nonterminals. */
1640 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1641 if (bp
->class == SUNKNOWN
)
1643 complain (_("symbol %s is used, but is not defined as a token and has no rules"),
1646 bp
->value
= nvars
++;
1649 ntokens
= nsyms
- nvars
;
1654 record_rule_line (void)
1656 /* Record each rule's source line number in rline table. */
1658 if (nrules
>= rline_allocated
)
1660 rline_allocated
= nrules
* 2;
1661 rline
= (short *) xrealloc ((char *) rline
,
1662 rline_allocated
* sizeof (short));
1664 rline
[nrules
] = lineno
;
1669 /* read in a %type declaration and record its information for get_type_name to access */
1670 /* this is unused. it is only called from the #if 0 part of readgram */
1676 register char *name
;
1682 complain (_("invalid %s declaration"), "%type");
1686 k
= strlen(token_buffer
);
1687 name
= NEW2(k
+ 1, char);
1688 strcpy(name
, token_buffer
);
1703 if (symval
->type_name
== NULL
)
1704 symval
->type_name
= name
;
1705 else if (strcmp(name
, symval
->type_name
) != 0)
1706 complain (_("type redeclaration for %s"), symval
->tag
);
1718 /* Assign symbol numbers, and write definition of token names into
1719 fdefines. Set up vectors tags and sprec of names and precedences
1725 register bucket
*bp
;
1726 register int tokno
= 1;
1728 register int last_user_token_number
;
1729 static char DOLLAR
[] = "$";
1731 /* int lossage = 0; JF set but not used */
1733 tags
= NEW2(nsyms
+ 1, char *);
1735 user_toknums
= NEW2(nsyms
+ 1, short);
1736 user_toknums
[0] = 0;
1738 sprec
= NEW2(nsyms
, short);
1739 sassoc
= NEW2(nsyms
, short);
1741 max_user_token_number
= 256;
1742 last_user_token_number
= 256;
1744 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1746 if (bp
->class == SNTERM
)
1748 bp
->value
+= ntokens
;
1752 /* this symbol and its alias are a single token defn.
1753 allocate a tokno, and assign to both check agreement of
1754 ->prec and ->assoc fields and make both the same */
1756 bp
->value
= bp
->alias
->value
= tokno
++;
1758 if (bp
->prec
!= bp
->alias
->prec
)
1760 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1761 && bp
->user_token_number
== SALIAS
)
1762 complain (_("conflicting precedences for %s and %s"),
1763 bp
->tag
, bp
->alias
->tag
);
1765 bp
->alias
->prec
= bp
->prec
;
1767 bp
->prec
= bp
->alias
->prec
;
1770 if (bp
->assoc
!= bp
->alias
->assoc
)
1772 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1773 && bp
->user_token_number
== SALIAS
)
1774 complain (_("conflicting assoc values for %s and %s"),
1775 bp
->tag
, bp
->alias
->tag
);
1777 bp
->alias
->assoc
= bp
->assoc
;
1779 bp
->assoc
= bp
->alias
->assoc
;
1782 if (bp
->user_token_number
== SALIAS
)
1783 continue; /* do not do processing below for SALIASs */
1786 else /* bp->class == STOKEN */
1788 bp
->value
= tokno
++;
1791 if (bp
->class == STOKEN
)
1793 if (translations
&& !(bp
->user_token_number
))
1794 bp
->user_token_number
= ++last_user_token_number
;
1795 if (bp
->user_token_number
> max_user_token_number
)
1796 max_user_token_number
= bp
->user_token_number
;
1799 tags
[bp
->value
] = bp
->tag
;
1800 user_toknums
[bp
->value
] = bp
->user_token_number
;
1801 sprec
[bp
->value
] = bp
->prec
;
1802 sassoc
[bp
->value
] = bp
->assoc
;
1810 token_translations
= NEW2(max_user_token_number
+1, short);
1812 /* initialize all entries for literal tokens to 2, the internal
1813 token number for $undefined., which represents all invalid
1815 for (j
= 0; j
<= max_user_token_number
; j
++)
1816 token_translations
[j
] = 2;
1818 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1820 if (bp
->value
>= ntokens
)
1821 continue; /* non-terminal */
1822 if (bp
->user_token_number
== SALIAS
)
1824 if (token_translations
[bp
->user_token_number
] != 2)
1825 complain (_("tokens %s and %s both assigned number %d"),
1826 tags
[token_translations
[bp
->user_token_number
]],
1828 bp
->user_token_number
);
1829 token_translations
[bp
->user_token_number
] = bp
->value
;
1833 error_token_number
= errtoken
->value
;
1836 output_token_defines(ftable
);
1838 if (startval
->class == SUNKNOWN
)
1839 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1840 else if (startval
->class == STOKEN
)
1841 fatal (_("the start symbol %s is a token"), startval
->tag
);
1843 start_symbol
= startval
->value
;
1847 output_token_defines(fdefines
);
1851 if (spec_name_prefix
)
1852 fprintf(fdefines
, "\nextern YYSTYPE %slval;\n", spec_name_prefix
);
1854 fprintf(fdefines
, "\nextern YYSTYPE yylval;\n");
1857 if (semantic_parser
)
1858 for (i
= ntokens
; i
< nsyms
; i
++)
1860 /* don't make these for dummy nonterminals made by gensym. */
1861 if (*tags
[i
] != '@')
1862 fprintf(fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1865 /* `fdefines' is now a temporary file, so we need to copy its
1866 contents in `done', so we can't close it here. */
1873 /* For named tokens, but not literal ones, define the name. The value
1874 is the user token number. */
1877 output_token_defines (FILE *file
)
1880 register char *cp
, *symbol
;
1883 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1885 symbol
= bp
->tag
; /* get symbol */
1887 if (bp
->value
>= ntokens
) continue;
1888 if (bp
->user_token_number
== SALIAS
) continue;
1889 if ('\'' == *symbol
) continue; /* skip literal character */
1890 if (bp
== errtoken
) continue; /* skip error token */
1891 if ('\"' == *symbol
)
1893 /* use literal string only if given a symbol with an alias */
1895 symbol
= bp
->alias
->tag
;
1900 /* Don't #define nonliteral tokens whose names contain periods. */
1902 while ((c
= *cp
++) && c
!= '.');
1903 if (c
!= '\0') continue;
1905 fprintf (file
, "#define\t%s\t%d\n", symbol
,
1906 ((translations
&& ! rawtoknumflag
)
1907 ? bp
->user_token_number
1909 if (semantic_parser
)
1910 fprintf (file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1918 /* convert the rules into the representation using rrhs, rlhs and ritems. */
1923 register int itemno
;
1924 register int ruleno
;
1925 register symbol_list
*p
;
1926 /* register bucket *bp; JF unused */
1930 ritem
= NEW2(nitems
+ 1, short);
1931 rlhs
= NEW2(nrules
, short) - 1;
1932 rrhs
= NEW2(nrules
, short) - 1;
1933 rprec
= NEW2(nrules
, short) - 1;
1934 rprecsym
= NEW2(nrules
, short) - 1;
1935 rassoc
= NEW2(nrules
, short) - 1;
1943 rlhs
[ruleno
] = p
->sym
->value
;
1944 rrhs
[ruleno
] = itemno
;
1945 ruleprec
= p
->ruleprec
;
1950 ritem
[itemno
++] = p
->sym
->value
;
1951 /* A rule gets by default the precedence and associativity
1952 of the last token in it. */
1953 if (p
->sym
->class == STOKEN
)
1955 rprec
[ruleno
] = p
->sym
->prec
;
1956 rassoc
[ruleno
] = p
->sym
->assoc
;
1961 /* If this rule has a %prec,
1962 the specified symbol's precedence replaces the default. */
1965 rprec
[ruleno
] = ruleprec
->prec
;
1966 rassoc
[ruleno
] = ruleprec
->assoc
;
1967 rprecsym
[ruleno
] = ruleprec
->value
;
1970 ritem
[itemno
++] = -ruleno
;