]>
git.saurik.com Git - bison.git/blob - src/reader.c
0d5e71fbc436336ad6ee1a4ca841dfc8fd726720
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));
122 static int get_type
PARAMS((void));
128 static symbol_list
*grammar
;
129 static int start_flag
;
130 static bucket
*startval
;
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
;
149 /*===================\
150 | Low level lexing. |
151 \===================*/
154 skip_to_char (int target
)
158 complain (_(" Skipping to next \\n"));
160 complain (_(" Skipping to next %c"), target
);
163 c
= skip_white_space ();
164 while (c
!= target
&& c
!= EOF
);
170 /*---------------------------------------------------------.
171 | Read a signed integer from STREAM and return its value. |
172 `---------------------------------------------------------*/
175 read_signed_integer (FILE *stream
)
177 register int c
= getc (stream
);
178 register int sign
= 1;
189 n
= 10 * n
+ (c
- '0');
198 /*-------------------------------------------------------------------.
199 | Dump the string from FINPUT to FOUTPUT. MATCH is the delimiter of |
200 | the string (either ' or "). |
201 `-------------------------------------------------------------------*/
204 copy_string (FILE *fin
, FILE *fout
, int match
)
214 fatal (_("unterminated string at end of file"));
217 complain (_("unterminated string"));
219 c
= match
; /* invent terminator */
229 fatal (_("unterminated string at end of file"));
242 /* Dump the comment from IN to OUT1 and OUT2. C is either `*' or `/',
243 depending upon the type of comments used. OUT2 might be NULL. */
246 copy_comment2 (FILE *in
, FILE *out1
, FILE* out2
, int c
)
251 cplus_comment
= (c
== '/');
260 if (!cplus_comment
&& c
== '*')
290 fatal (_("unterminated comment"));
302 /* Dump the comment from FIN to FOUT. C is either `*' or `/',
303 depending upon the type of comments used. */
306 copy_comment (FILE *fin
, FILE *fout
, int c
)
308 copy_comment2 (fin
, fout
, NULL
, c
);
316 startval
= NULL
; /* start symbol not specified yet. */
319 /* initially assume token number translation not needed. */
322 /* Nowadays translations is always set to 1, since we give `error' a
323 user-token-number to satisfy the Posix demand for YYERRCODE==256.
331 rline_allocated
= 10;
332 rline
= NEW2 (rline_allocated
, short);
348 /* Initialize the symbol table. */
350 /* Construct the error token */
351 errtoken
= getsym ("error");
352 errtoken
->class = STOKEN
;
353 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
354 /* Construct a token that represents all undefined literal tokens.
355 It is always token number 2. */
356 undeftoken
= getsym ("$undefined.");
357 undeftoken
->class = STOKEN
;
358 undeftoken
->user_token_number
= 2;
360 /* Read the declaration section. Copy %{ ... %} groups to FTABLE
361 and FDEFINES file. Also notice any %token, %left, etc. found
365 /* %s, made from %s\n\
366 by GNU bison %s. */\n\
368 noparserflag
? "Bison-generated parse tables" : "A Bison parser",
372 fputs ("#define YYBISON 1 /* Identify Bison output. */\n\n", ftable
);
373 read_declarations ();
374 /* Start writing the guard and action files, if they are needed. */
376 /* Read in the grammar, build grammar in list form. Write out
377 guards and actions. */
379 /* Now we know whether we need the line-number stack. If we do,
380 write its type into the .tab.h file. */
382 reader_output_yylsp (fdefines
);
383 /* Write closing delimiters for actions and guards. */
386 fputs ("#define YYLSP_NEEDED\n\n", ftable
);
387 /* Assign the symbols their symbol numbers. Write #defines for the
388 token symbols into FDEFINES if requested. */
390 /* Convert the grammar into the format described in gram.h. */
392 /* Free the symbol table data structure since symbols are now all
393 referred to by symbol number. */
398 reader_output_yylsp (FILE *f
)
401 fprintf(f
, LTYPESTR
);
404 /* Read from finput until `%%' is seen. Discard the `%%'. Handle any
405 `%' declarations, and copy the contents of any `%{ ... %}' groups
409 read_declarations (void)
416 c
= skip_white_space();
420 tok
= parse_percent_token();
427 case PERCENT_LEFT_CURLY
:
432 parse_token_decl (STOKEN
, SNTERM
);
436 parse_token_decl (SNTERM
, STOKEN
);
458 parse_assoc_decl(LEFT_ASSOC
);
462 parse_assoc_decl(RIGHT_ASSOC
);
466 parse_assoc_decl(NON_ASSOC
);
469 case SEMANTIC_PARSER
:
470 if (semantic_parser
== 0)
485 complain (_("unrecognized: %s"), token_buffer
);
490 fatal (_("no input grammar"));
493 complain (_("unknown character: %s"), printable_version(c
));
500 /* Copy the contents of a `%{ ... %}' into the definitions file. The
501 `%{' has already been read. Return after reading the `%}'. */
504 copy_definition (void)
507 /* -1 while reading a character if prev char was %. */
508 register int after_percent
;
511 fprintf(fattrs
, "#line %d \"%s\"\n", lineno
, infile
);
532 copy_string (finput
, fattrs
, c
);
538 if (c
!= '*' && c
!= '/')
540 copy_comment (finput
, fattrs
, c
);
545 _("unterminated `%{' definition"));
567 /* parse what comes after %token or %nterm.
568 For %token, what_is is STOKEN and what_is_not is SNTERM.
569 For %nterm, the arguments are reversed. */
572 parse_token_decl (int what_is
, int what_is_not
)
574 register int token
= 0;
575 register char *typename
= 0;
576 register struct bucket
*symbol
= NULL
; /* pts to symbol being defined */
581 int tmp_char
= ungetc (skip_white_space (), finput
);
586 fatal (_("Premature EOF after %s"), token_buffer
);
594 if (token
== TYPENAME
)
596 k
= strlen(token_buffer
);
597 typename
= NEW2(k
+ 1, char);
598 strcpy(typename
, token_buffer
);
599 value_components_used
= 1;
602 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"'
606 symval
->class = STOKEN
;
607 symval
->type_name
= typename
;
608 symval
->user_token_number
= symbol
->user_token_number
;
609 symbol
->user_token_number
= SALIAS
;
611 symval
->alias
= symbol
;
612 symbol
->alias
= symval
;
615 nsyms
--; /* symbol and symval combined are only one symbol */
617 else if (token
== IDENTIFIER
)
619 int oldclass
= symval
->class;
622 if (symbol
->class == what_is_not
)
623 complain (_("symbol %s redefined"), symbol
->tag
);
624 symbol
->class = what_is
;
625 if (what_is
== SNTERM
&& oldclass
!= SNTERM
)
626 symbol
->value
= nvars
++;
630 if (symbol
->type_name
== NULL
)
631 symbol
->type_name
= typename
;
632 else if (strcmp(typename
, symbol
->type_name
) != 0)
633 complain (_("type redeclaration for %s"), symbol
->tag
);
636 else if (symbol
&& token
== NUMBER
)
638 symbol
->user_token_number
= numval
;
643 complain (_("`%s' is invalid in %s"),
645 (what_is
== STOKEN
) ? "%token" : "%nterm");
652 /* parse what comes after %thong
654 %thong <type> token number literal
655 the <type> or number may be omitted. The number specifies the
658 Two symbols are entered in the table, one for the token symbol and
659 one for the literal. Both are given the <type>, if any, from the declaration.
660 The ->user_token_number of the first is SALIAS and the ->user_token_number
661 of the second is set to the number, if any, from the declaration.
662 The two symbols are linked via pointers in their ->alias fields.
664 during output_defines_table, the symbol is reported
665 thereafter, only the literal string is retained
666 it is the literal string that is output to yytname
670 parse_thong_decl (void)
673 register struct bucket
*symbol
;
674 register char *typename
= 0;
678 token
= lex(); /* fetch typename or first token */
679 if (token
== TYPENAME
) {
680 k
= strlen(token_buffer
);
681 typename
= NEW2(k
+ 1, char);
682 strcpy(typename
, token_buffer
);
683 value_components_used
= 1;
684 token
= lex(); /* fetch first token */
687 /* process first token */
689 if (token
!= IDENTIFIER
)
691 complain (_("unrecognized item %s, expected an identifier"),
696 symval
->class = STOKEN
;
697 symval
->type_name
= typename
;
698 symval
->user_token_number
= SALIAS
;
701 token
= lex(); /* get number or literal string */
703 if (token
== NUMBER
) {
705 token
= lex(); /* okay, did number, now get literal */
709 /* process literal string token */
711 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
713 complain (_("expected string constant instead of %s"),
718 symval
->class = STOKEN
;
719 symval
->type_name
= typename
;
720 symval
->user_token_number
= usrtoknum
;
722 symval
->alias
= symbol
;
723 symbol
->alias
= symval
;
725 nsyms
--; /* symbol and symval combined are only one symbol */
729 /* Parse what comes after %start */
732 parse_start_decl (void)
735 complain (_("multiple %s declarations"), "%start");
736 if (lex () != IDENTIFIER
)
737 complain (_("invalid %s declaration"), "%start");
747 /* read in a %type declaration and record its information for get_type_name to access */
750 parse_type_decl (void)
755 if (lex() != TYPENAME
)
757 complain ("%s", _("%type declaration has no <typename>"));
762 k
= strlen(token_buffer
);
763 name
= NEW2(k
+ 1, char);
764 strcpy(name
, token_buffer
);
769 int tmp_char
= ungetc (skip_white_space (), finput
);
774 fatal (_("Premature EOF after %s"), token_buffer
);
786 if (symval
->type_name
== NULL
)
787 symval
->type_name
= name
;
788 else if (strcmp(name
, symval
->type_name
) != 0)
789 complain (_("type redeclaration for %s"), symval
->tag
);
794 complain (_("invalid %%type declaration due to item: %s"),
803 /* read in a %left, %right or %nonassoc declaration and record its information. */
804 /* assoc is either LEFT_ASSOC, RIGHT_ASSOC or NON_ASSOC. */
807 parse_assoc_decl (int assoc
)
810 register char *name
= NULL
;
811 register int prev
= 0;
813 lastprec
++; /* Assign a new precedence level, never 0. */
818 int tmp_char
= ungetc (skip_white_space (), finput
);
823 fatal (_("Premature EOF after %s"), token_buffer
);
831 k
= strlen(token_buffer
);
832 name
= NEW2(k
+ 1, char);
833 strcpy(name
, token_buffer
);
840 if (symval
->prec
!= 0)
841 complain (_("redefining precedence of %s"), symval
->tag
);
842 symval
->prec
= lastprec
;
843 symval
->assoc
= assoc
;
844 if (symval
->class == SNTERM
)
845 complain (_("symbol %s redefined"), symval
->tag
);
846 symval
->class = STOKEN
;
848 { /* record the type, if one is specified */
849 if (symval
->type_name
== NULL
)
850 symval
->type_name
= name
;
851 else if (strcmp(name
, symval
->type_name
) != 0)
852 complain (_("type redeclaration for %s"), symval
->tag
);
857 if (prev
== IDENTIFIER
)
859 symval
->user_token_number
= numval
;
864 complain (_("invalid text (%s) - number should be after identifier"),
874 complain (_("unexpected item: %s"), token_buffer
);
885 /* copy the union declaration into fattrs (and fdefines),
886 where it is made into the
887 definition of YYSTYPE, the type of elements of the parser value stack. */
890 parse_union_decl (void)
893 register int count
= 0;
896 complain (_("multiple %s declarations"), "%union");
901 fprintf (fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
903 fprintf (fattrs
, "\n");
905 fprintf (fattrs
, "typedef union");
907 fprintf (fdefines
, "typedef union");
925 if (c
!= '*' && c
!= '/')
927 copy_comment2 (finput
, fattrs
, fdefines
, c
);
937 complain (_("unmatched %s"), "`}'");
941 fprintf (fattrs
, " YYSTYPE;\n");
943 fprintf (fdefines
, " YYSTYPE;\n");
944 /* JF don't choke on trailing semi */
945 c
= skip_white_space ();
956 /* parse the declaration %expect N which says to expect N
957 shift-reduce conflicts. */
960 parse_expect_decl (void)
967 while (c
== ' ' || c
== '\t')
971 while (c
>= '0' && c
<= '9')
981 if (count
<= 0 || count
> 10)
982 complain ("%s", _("argument of %expect is not an integer"));
983 expected_conflicts
= atoi (buffer
);
986 /* that's all of parsing the declaration section */
988 /* FIN is pointing to a location (i.e., a `@'). Output to FOUT
989 a reference to this location. STACK_OFFSET is the number of values
990 in the current rule so far, which says where to find `$0' with
991 respect to the top of the stack. */
993 copy_at (FILE *fin
, FILE *fout
, int stack_offset
)
1000 fprintf (fout
, "yyloc");
1003 else if (isdigit(c
) || c
== '-')
1008 n
= read_signed_integer (fin
);
1010 fprintf (fout
, "yylsp[%d]", n
- stack_offset
);
1014 complain (_("@%s is invalid"), printable_version (c
));
1018 /* Get the data type (alternative in the union) of the value for
1019 symbol n in rule rule. */
1022 get_type_name (int n
, symbol_list
*rule
)
1025 register symbol_list
*rp
;
1029 complain (_("invalid $ value"));
1039 if (rp
== NULL
|| rp
->sym
== NULL
)
1041 complain (_("invalid $ value"));
1047 return rp
->sym
->type_name
;
1052 /* After `%guard' is seen in the input file, copy the actual guard
1053 into the guards file. If the guard is followed by an action, copy
1054 that into the actions file. STACK_OFFSET is the number of values
1055 in the current rule so far, which says where to find `$0' with
1056 respect to the top of the stack, for the simple parser in which the
1057 stack is not popped until after the guard is run. */
1060 copy_guard (symbol_list
*rule
, int stack_offset
)
1065 register char *type_name
;
1068 /* offset is always 0 if parser has already popped the stack pointer */
1069 if (semantic_parser
) stack_offset
= 0;
1071 fprintf(fguard
, "\ncase %d:\n", nrules
);
1073 fprintf (fguard
, "#line %d \"%s\"\n", lineno
, infile
);
1079 while (brace_flag
? (count
> 0) : (c
!= ';'))
1100 complain (_("unmatched %s"), "`}'");
1101 c
= getc(finput
); /* skip it */
1107 copy_string (finput
, fguard
, c
);
1113 if (c
!= '*' && c
!= '/')
1115 copy_comment (finput
, fguard
, c
);
1124 register char *cp
= token_buffer
;
1126 while ((c
= getc(finput
)) != '>' && c
> 0)
1128 if (cp
== token_buffer
+ maxtoken
)
1129 cp
= grow_token_buffer(cp
);
1134 type_name
= token_buffer
;
1141 fprintf(fguard
, "yyval");
1143 type_name
= rule
->sym
->type_name
;
1145 fprintf(fguard
, ".%s", type_name
);
1146 if(!type_name
&& typed
)
1147 complain (_("$$ of `%s' has no declared type"),
1150 else if (isdigit(c
) || c
== '-')
1153 n
= read_signed_integer (finput
);
1156 if (!type_name
&& n
> 0)
1157 type_name
= get_type_name(n
, rule
);
1159 fprintf(fguard
, "yyvsp[%d]", n
- stack_offset
);
1161 fprintf(fguard
, ".%s", type_name
);
1162 if (!type_name
&& typed
)
1163 complain (_("$%d of `%s' has no declared type"),
1168 complain (_("$%s is invalid"), printable_version (c
));
1172 copy_at (finput
, fguard
, stack_offset
);
1177 _("unterminated %guard clause"));
1183 if (c
!= '}' || count
!= 0)
1187 c
= skip_white_space();
1189 fprintf(fguard
, ";\n break;}");
1191 copy_action (rule
, stack_offset
);
1194 c
= getc(finput
); /* why not skip_white_space -wjh */
1196 copy_action (rule
, stack_offset
);
1204 /* Assuming that a `{' has just been seen, copy everything up to the
1205 matching `}' into the actions file. STACK_OFFSET is the number of
1206 values in the current rule so far, which says where to find `$0'
1207 with respect to the top of the stack. */
1210 copy_action (symbol_list
*rule
, int stack_offset
)
1215 register char *type_name
;
1217 /* offset is always 0 if parser has already popped the stack pointer */
1218 if (semantic_parser
)
1221 fprintf (faction
, "\ncase %d:\n", nrules
);
1223 fprintf (faction
, "#line %d \"%s\"\n", lineno
, infile
);
1224 putc ('{', faction
);
1247 copy_string (finput
, faction
, c
);
1253 if (c
!= '*' && c
!= '/')
1255 copy_comment (finput
, faction
, c
);
1264 register char *cp
= token_buffer
;
1266 while ((c
= getc(finput
)) != '>' && c
> 0)
1268 if (cp
== token_buffer
+ maxtoken
)
1269 cp
= grow_token_buffer(cp
);
1274 type_name
= token_buffer
;
1275 value_components_used
= 1;
1281 fprintf(faction
, "yyval");
1283 type_name
= get_type_name(0, rule
);
1285 fprintf(faction
, ".%s", type_name
);
1286 if(!type_name
&& typed
)
1287 complain (_("$$ of `%s' has no declared type"),
1290 else if (isdigit(c
) || c
== '-')
1293 n
= read_signed_integer(finput
);
1296 if (!type_name
&& n
> 0)
1297 type_name
= get_type_name(n
, rule
);
1299 fprintf(faction
, "yyvsp[%d]", n
- stack_offset
);
1301 fprintf(faction
, ".%s", type_name
);
1302 if(!type_name
&& typed
)
1303 complain (_("$%d of `%s' has no declared type"),
1308 complain (_("$%s is invalid"), printable_version (c
));
1313 copy_at (finput
, faction
, stack_offset
);
1317 fatal (_("unmatched %s"), "`{'");
1326 /* above loop exits when c is '}' */
1335 fprintf(faction
, ";\n break;}");
1340 /* generate a dummy symbol, a nonterminal,
1341 whose name cannot conflict with the user's names. */
1346 register bucket
*sym
;
1348 sprintf (token_buffer
, "@%d", ++gensym_count
);
1349 sym
= getsym(token_buffer
);
1350 sym
->class = SNTERM
;
1351 sym
->value
= nvars
++;
1355 /* Parse the input grammar into a one symbol_list structure.
1356 Each rule is represented by a sequence of symbols: the left hand side
1357 followed by the contents of the right hand side, followed by a null pointer
1358 instead of a symbol to terminate the rule.
1359 The next symbol is the lhs of the following rule.
1361 All guards and actions are copied out to the appropriate files,
1362 labelled by the rule number they apply to. */
1368 register bucket
*lhs
= NULL
;
1369 register symbol_list
*p
;
1370 register symbol_list
*p1
;
1371 register bucket
*bp
;
1373 symbol_list
*crule
; /* points to first symbol_list of current rule. */
1374 /* its symbol is the lhs of the rule. */
1375 symbol_list
*crule1
; /* points to the symbol_list preceding crule. */
1381 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1383 if (t
== IDENTIFIER
|| t
== BAR
)
1385 register int actionflag
= 0;
1386 int rulelength
= 0; /* number of symbols in rhs of this rule so far */
1387 int xactions
= 0; /* JF for error checking */
1388 bucket
*first_rhs
= 0;
1390 if (t
== IDENTIFIER
)
1403 complain (_("ill-formed rule: initial symbol not followed by colon"));
1408 if (nrules
== 0 && t
== BAR
)
1410 complain (_("grammar starts with vertical bar"));
1411 lhs
= symval
; /* BOGUS: use a random symval */
1413 /* start a new rule and record its lhs. */
1418 record_rule_line ();
1420 p
= NEW(symbol_list
);
1432 /* mark the rule's lhs as a nonterminal if not already so. */
1434 if (lhs
->class == SUNKNOWN
)
1436 lhs
->class = SNTERM
;
1440 else if (lhs
->class == STOKEN
)
1441 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1443 /* read the rhs of the rule. */
1451 crule
->ruleprec
= symval
;
1455 if (! (t
== IDENTIFIER
|| t
== LEFT_CURLY
)) break;
1457 /* If next token is an identifier, see if a colon follows it.
1458 If one does, exit this rule now. */
1459 if (t
== IDENTIFIER
)
1461 register bucket
*ssave
;
1468 if (t1
== COLON
) break;
1470 if(!first_rhs
) /* JF */
1472 /* Not followed by colon =>
1473 process as part of this rule's rhs. */
1476 /* If we just passed an action, that action was in the middle
1477 of a rule, so make a dummy rule to reduce it to a
1481 register bucket
*sdummy
;
1483 /* Since the action was written out with this rule's */
1484 /* number, we must give the new rule this number */
1485 /* by inserting the new rule before it. */
1487 /* Make a dummy nonterminal, a gensym. */
1490 /* Make a new rule, whose body is empty,
1491 before the current one, so that the action
1492 just read can belong to it. */
1495 record_rule_line ();
1496 p
= NEW(symbol_list
);
1501 crule1
= NEW(symbol_list
);
1503 crule1
->next
= crule
;
1505 /* insert the dummy generated by that rule into this rule. */
1507 p
= NEW(symbol_list
);
1515 if (t
== IDENTIFIER
)
1518 p
= NEW(symbol_list
);
1523 else /* handle an action. */
1525 copy_action(crule
, rulelength
);
1527 xactions
++; /* JF */
1530 } /* end of read rhs of rule */
1532 /* Put an empty link in the list to mark the end of this rule */
1533 p
= NEW(symbol_list
);
1539 complain (_("two @prec's in a row"));
1541 crule
->ruleprec
= symval
;
1546 if (! semantic_parser
)
1548 _("%guard present but %semantic_parser not specified"));
1550 copy_guard(crule
, rulelength
);
1553 else if (t
== LEFT_CURLY
)
1555 /* This case never occurs -wjh */
1557 complain (_("two actions at end of one rule"));
1558 copy_action(crule
, rulelength
);
1560 xactions
++; /* -wjh */
1563 /* If $$ is being set in default way, report if any type
1567 && lhs
->type_name
!= first_rhs
->type_name
)
1569 if (lhs
->type_name
== 0
1570 || first_rhs
->type_name
== 0
1571 || strcmp(lhs
->type_name
,first_rhs
->type_name
))
1572 complain (_("type clash (`%s' `%s') on default action"),
1573 lhs
->type_name
? lhs
->type_name
: "",
1574 first_rhs
->type_name
? first_rhs
->type_name
: "");
1576 /* Warn if there is no default for $$ but we need one. */
1577 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1578 complain (_("empty rule for typed nonterminal, and no action"));
1583 /* these things can appear as alternatives to rules. */
1585 a) none of the documentation allows them
1586 b) most of them scan forward until finding a next %
1587 thus they may swallow lots of intervening rules
1589 else if (t
== TOKEN
)
1591 parse_token_decl(STOKEN
, SNTERM
);
1594 else if (t
== NTERM
)
1596 parse_token_decl(SNTERM
, STOKEN
);
1603 else if (t
== UNION
)
1608 else if (t
== EXPECT
)
1610 parse_expect_decl();
1613 else if (t
== START
)
1622 complain (_("invalid input: %s"), token_buffer
);
1627 /* grammar has been read. Do some checking */
1629 if (nsyms
> MAXSHORT
)
1630 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1633 fatal (_("no rules in the input grammar"));
1635 if (typed
== 0 /* JF put out same default YYSTYPE as YACC does */
1636 && !value_components_used
)
1638 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1639 but it seems better to be consistent.
1640 Most programs should declare their own type anyway. */
1641 fprintf(fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1643 fprintf(fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1646 /* Report any undefined symbols and consider them nonterminals. */
1648 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1649 if (bp
->class == SUNKNOWN
)
1651 complain (_("symbol %s is used, but is not defined as a token and has no rules"),
1654 bp
->value
= nvars
++;
1657 ntokens
= nsyms
- nvars
;
1662 record_rule_line (void)
1664 /* Record each rule's source line number in rline table. */
1666 if (nrules
>= rline_allocated
)
1668 rline_allocated
= nrules
* 2;
1669 rline
= (short *) xrealloc ((char *) rline
,
1670 rline_allocated
* sizeof (short));
1672 rline
[nrules
] = lineno
;
1677 /* read in a %type declaration and record its information for get_type_name to access */
1678 /* this is unused. it is only called from the #if 0 part of readgram */
1684 register char *name
;
1690 complain (_("invalid %s declaration"), "%type");
1694 k
= strlen(token_buffer
);
1695 name
= NEW2(k
+ 1, char);
1696 strcpy(name
, token_buffer
);
1711 if (symval
->type_name
== NULL
)
1712 symval
->type_name
= name
;
1713 else if (strcmp(name
, symval
->type_name
) != 0)
1714 complain (_("type redeclaration for %s"), symval
->tag
);
1726 /* Assign symbol numbers, and write definition of token names into
1727 fdefines. Set up vectors tags and sprec of names and precedences
1733 register bucket
*bp
;
1734 register int tokno
= 1;
1736 register int last_user_token_number
;
1737 static char DOLLAR
[] = "$";
1739 /* int lossage = 0; JF set but not used */
1741 tags
= NEW2(nsyms
+ 1, char *);
1743 user_toknums
= NEW2(nsyms
+ 1, int);
1744 user_toknums
[0] = 0;
1746 sprec
= NEW2(nsyms
, short);
1747 sassoc
= NEW2(nsyms
, short);
1749 max_user_token_number
= 256;
1750 last_user_token_number
= 256;
1752 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1754 if (bp
->class == SNTERM
)
1756 bp
->value
+= ntokens
;
1760 /* this symbol and its alias are a single token defn.
1761 allocate a tokno, and assign to both check agreement of
1762 ->prec and ->assoc fields and make both the same */
1764 bp
->value
= bp
->alias
->value
= tokno
++;
1766 if (bp
->prec
!= bp
->alias
->prec
)
1768 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1769 && bp
->user_token_number
== SALIAS
)
1770 complain (_("conflicting precedences for %s and %s"),
1771 bp
->tag
, bp
->alias
->tag
);
1773 bp
->alias
->prec
= bp
->prec
;
1775 bp
->prec
= bp
->alias
->prec
;
1778 if (bp
->assoc
!= bp
->alias
->assoc
)
1780 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1781 && bp
->user_token_number
== SALIAS
)
1782 complain (_("conflicting assoc values for %s and %s"),
1783 bp
->tag
, bp
->alias
->tag
);
1785 bp
->alias
->assoc
= bp
->assoc
;
1787 bp
->assoc
= bp
->alias
->assoc
;
1790 if (bp
->user_token_number
== SALIAS
)
1791 continue; /* do not do processing below for SALIASs */
1794 else /* bp->class == STOKEN */
1796 bp
->value
= tokno
++;
1799 if (bp
->class == STOKEN
)
1801 if (translations
&& !(bp
->user_token_number
))
1802 bp
->user_token_number
= ++last_user_token_number
;
1803 if (bp
->user_token_number
> max_user_token_number
)
1804 max_user_token_number
= bp
->user_token_number
;
1807 tags
[bp
->value
] = bp
->tag
;
1808 user_toknums
[bp
->value
] = bp
->user_token_number
;
1809 sprec
[bp
->value
] = bp
->prec
;
1810 sassoc
[bp
->value
] = bp
->assoc
;
1818 token_translations
= NEW2(max_user_token_number
+1, short);
1820 /* initialize all entries for literal tokens to 2, the internal
1821 token number for $undefined., which represents all invalid
1823 for (j
= 0; j
<= max_user_token_number
; j
++)
1824 token_translations
[j
] = 2;
1826 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1828 if (bp
->value
>= ntokens
)
1829 continue; /* non-terminal */
1830 if (bp
->user_token_number
== SALIAS
)
1832 if (token_translations
[bp
->user_token_number
] != 2)
1833 complain (_("tokens %s and %s both assigned number %d"),
1834 tags
[token_translations
[bp
->user_token_number
]],
1836 bp
->user_token_number
);
1837 token_translations
[bp
->user_token_number
] = bp
->value
;
1841 error_token_number
= errtoken
->value
;
1844 output_token_defines(ftable
);
1846 if (startval
->class == SUNKNOWN
)
1847 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1848 else if (startval
->class == STOKEN
)
1849 fatal (_("the start symbol %s is a token"), startval
->tag
);
1851 start_symbol
= startval
->value
;
1855 output_token_defines(fdefines
);
1859 if (spec_name_prefix
)
1860 fprintf(fdefines
, "\nextern YYSTYPE %slval;\n", spec_name_prefix
);
1862 fprintf(fdefines
, "\nextern YYSTYPE yylval;\n");
1865 if (semantic_parser
)
1866 for (i
= ntokens
; i
< nsyms
; i
++)
1868 /* don't make these for dummy nonterminals made by gensym. */
1869 if (*tags
[i
] != '@')
1870 fprintf(fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1873 /* `fdefines' is now a temporary file, so we need to copy its
1874 contents in `done', so we can't close it here. */
1881 /* For named tokens, but not literal ones, define the name. The value
1882 is the user token number. */
1885 output_token_defines (FILE *file
)
1888 register char *cp
, *symbol
;
1891 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1893 symbol
= bp
->tag
; /* get symbol */
1895 if (bp
->value
>= ntokens
) continue;
1896 if (bp
->user_token_number
== SALIAS
) continue;
1897 if ('\'' == *symbol
) continue; /* skip literal character */
1898 if (bp
== errtoken
) continue; /* skip error token */
1899 if ('\"' == *symbol
)
1901 /* use literal string only if given a symbol with an alias */
1903 symbol
= bp
->alias
->tag
;
1908 /* Don't #define nonliteral tokens whose names contain periods. */
1910 while ((c
= *cp
++) && c
!= '.');
1911 if (c
!= '\0') continue;
1913 fprintf (file
, "#define\t%s\t%d\n", symbol
,
1914 ((translations
&& ! rawtoknumflag
)
1915 ? bp
->user_token_number
1917 if (semantic_parser
)
1918 fprintf (file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1926 /* convert the rules into the representation using rrhs, rlhs and ritems. */
1931 register int itemno
;
1932 register int ruleno
;
1933 register symbol_list
*p
;
1934 /* register bucket *bp; JF unused */
1938 ritem
= NEW2(nitems
+ 1, short);
1939 rlhs
= NEW2(nrules
, short) - 1;
1940 rrhs
= NEW2(nrules
, short) - 1;
1941 rprec
= NEW2(nrules
, short) - 1;
1942 rprecsym
= NEW2(nrules
, short) - 1;
1943 rassoc
= NEW2(nrules
, short) - 1;
1951 rlhs
[ruleno
] = p
->sym
->value
;
1952 rrhs
[ruleno
] = itemno
;
1953 ruleprec
= p
->ruleprec
;
1958 ritem
[itemno
++] = p
->sym
->value
;
1959 /* A rule gets by default the precedence and associativity
1960 of the last token in it. */
1961 if (p
->sym
->class == STOKEN
)
1963 rprec
[ruleno
] = p
->sym
->prec
;
1964 rassoc
[ruleno
] = p
->sym
->assoc
;
1969 /* If this rule has a %prec,
1970 the specified symbol's precedence replaces the default. */
1973 rprec
[ruleno
] = ruleprec
->prec
;
1974 rassoc
[ruleno
] = ruleprec
->assoc
;
1975 rprecsym
[ruleno
] = ruleprec
->value
;
1978 ritem
[itemno
++] = -ruleno
;