]>
git.saurik.com Git - bison.git/blob - src/reader.c
5094bc4158a2cbbf488bbb290fb4dba71a18e1cf
1 /* Input parser for bison
2 Copyright (C) 1984, 86, 89, 92, 98, 2000 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
6 Bison is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 Bison is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* Read in the grammar specification and record it in the format
23 described in gram.h. All guards are copied into the fguard file
24 and all actions into faction, in each case forming the body of a C
25 function (yyguard or yyaction) which contains a switch statement to
26 decide which guard or action to execute.
28 The entry point is reader (). */
58 #define YYLTYPE yyltype\n\
62 /* Number of slots allocated (but not necessarily used yet) in `rline' */
63 static int rline_allocated
;
65 extern bucket
*symval
;
67 extern int expected_conflicts
;
68 extern char *token_buffer
;
71 extern void init_lex
PARAMS((void));
72 extern char *grow_token_buffer
PARAMS((char *));
73 extern void tabinit
PARAMS((void));
74 extern void free_symtab
PARAMS((void));
75 extern void open_extra_files
PARAMS((void));
76 extern char *printable_version
PARAMS((int));
77 extern void unlex
PARAMS((int));
79 extern int skip_white_space
PARAMS((void));
80 extern int parse_percent_token
PARAMS((void));
81 extern int lex
PARAMS((void));
86 struct symbol_list
*next
;
93 extern void reader
PARAMS((void));
94 extern void reader_output_yylsp
PARAMS((FILE *));
96 static void read_declarations
PARAMS((void));
97 static void copy_definition
PARAMS((void));
98 static void parse_token_decl
PARAMS((int, int));
99 static void parse_start_decl
PARAMS((void));
100 static void parse_type_decl
PARAMS((void));
101 static void parse_assoc_decl
PARAMS((int));
102 static void parse_union_decl
PARAMS((void));
103 static void parse_expect_decl
PARAMS((void));
104 static char *get_type_name
PARAMS((int, symbol_list
*));
105 static void copy_guard
PARAMS((symbol_list
*, int));
106 static void parse_thong_decl
PARAMS((void));
107 static void copy_action
PARAMS((symbol_list
*, int));
108 static bucket
*gensym
PARAMS((void));
109 static void readgram
PARAMS((void));
110 static void record_rule_line
PARAMS((void));
111 static void packsymbols
PARAMS((void));
112 static void output_token_defines
PARAMS((FILE *));
113 static void packgram
PARAMS((void));
116 static int get_type
PARAMS((void));
122 static symbol_list
*grammar
;
123 static int start_flag
;
124 static bucket
*startval
;
126 /* Nonzero if components of semantic values are used, implying
127 they must be unions. */
128 static int value_components_used
;
130 static int typed
; /* nonzero if %union has been seen. */
132 static int lastprec
; /* incremented for each %left, %right or %nonassoc seen */
134 static int gensym_count
; /* incremented for each generated symbol */
136 static bucket
*errtoken
;
137 static bucket
*undeftoken
;
139 /* Nonzero if any action or guard uses the @n construct. */
140 static int yylsp_needed
;
143 /*===================\
144 | Low level lexing. |
145 \===================*/
148 skip_to_char (int target
)
152 complain (_(" Skipping to next \\n"));
154 complain (_(" Skipping to next %c"), target
);
157 c
= skip_white_space ();
158 while (c
!= target
&& c
!= EOF
);
164 /*---------------------------------------------------------.
165 | Read a signed integer from STREAM and return its value. |
166 `---------------------------------------------------------*/
169 read_signed_integer (FILE *stream
)
171 register int c
= getc (stream
);
172 register int sign
= 1;
183 n
= 10 * n
+ (c
- '0');
192 /*-------------------------------------------------------------------.
193 | Dump the string from FINPUT to FOUTPUT. MATCH is the delimiter of |
194 | the string (either ' or "). |
195 `-------------------------------------------------------------------*/
198 copy_string (FILE *fin
, FILE *fout
, int match
)
208 fatal (_("unterminated string at end of file"));
211 complain (_("unterminated string"));
213 c
= match
; /* invent terminator */
223 fatal (_("unterminated string at end of file"));
236 /*---------------------------------------------------------------.
237 | Dump the comment from IN to OUT1 and OUT2. C is either `*' or |
238 | `/', depending upon the type of comments used. OUT2 might be |
240 `---------------------------------------------------------------*/
243 copy_comment2 (FILE *in
, FILE *out1
, FILE* out2
, int c
)
248 cplus_comment
= (c
== '/');
257 if (!cplus_comment
&& c
== '*')
287 fatal (_("unterminated comment"));
299 /* Dump the comment from FIN to FOUT. C is either `*' or `/',
300 depending upon the type of comments used. */
303 copy_comment (FILE *fin
, FILE *fout
, int c
)
305 copy_comment2 (fin
, fout
, NULL
, c
);
313 startval
= NULL
; /* start symbol not specified yet. */
316 /* initially assume token number translation not needed. */
319 /* Nowadays translations is always set to 1, since we give `error' a
320 user-token-number to satisfy the Posix demand for YYERRCODE==256.
328 rline_allocated
= 10;
329 rline
= NEW2 (rline_allocated
, short);
345 /* Initialize the symbol table. */
347 /* Construct the error token */
348 errtoken
= getsym ("error");
349 errtoken
->class = STOKEN
;
350 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
351 /* Construct a token that represents all undefined literal tokens.
352 It is always token number 2. */
353 undeftoken
= getsym ("$undefined.");
354 undeftoken
->class = STOKEN
;
355 undeftoken
->user_token_number
= 2;
357 /* Read the declaration section. Copy %{ ... %} groups to FTABLE
358 and FDEFINES file. Also notice any %token, %left, etc. found
362 /* %s, made from %s\n\
363 by GNU bison %s. */\n\
365 noparserflag
? "Bison-generated parse tables" : "A Bison parser",
369 fputs ("#define YYBISON 1 /* Identify Bison output. */\n\n", ftable
);
370 read_declarations ();
371 /* Start writing the guard and action files, if they are needed. */
373 /* Read in the grammar, build grammar in list form. Write out
374 guards and actions. */
376 /* Now we know whether we need the line-number stack. If we do,
377 write its type into the .tab.h file. */
379 reader_output_yylsp (fdefines
);
380 /* Write closing delimiters for actions and guards. */
383 fputs ("#define YYLSP_NEEDED\n\n", ftable
);
384 /* Assign the symbols their symbol numbers. Write #defines for the
385 token symbols into FDEFINES if requested. */
387 /* Convert the grammar into the format described in gram.h. */
389 /* Free the symbol table data structure since symbols are now all
390 referred to by symbol number. */
395 reader_output_yylsp (FILE *f
)
398 fprintf(f
, LTYPESTR
);
401 /* Read from finput until `%%' is seen. Discard the `%%'. Handle any
402 `%' declarations, and copy the contents of any `%{ ... %}' groups
406 read_declarations (void)
413 c
= skip_white_space();
417 tok
= parse_percent_token();
424 case PERCENT_LEFT_CURLY
:
429 parse_token_decl (STOKEN
, SNTERM
);
433 parse_token_decl (SNTERM
, STOKEN
);
455 parse_assoc_decl(LEFT_ASSOC
);
459 parse_assoc_decl(RIGHT_ASSOC
);
463 parse_assoc_decl(NON_ASSOC
);
466 case SEMANTIC_PARSER
:
467 if (semantic_parser
== 0)
482 complain (_("unrecognized: %s"), token_buffer
);
487 fatal (_("no input grammar"));
490 complain (_("unknown character: %s"), printable_version(c
));
497 /* Copy the contents of a `%{ ... %}' into the definitions file. The
498 `%{' has already been read. Return after reading the `%}'. */
501 copy_definition (void)
504 /* -1 while reading a character if prev char was %. */
505 register int after_percent
;
508 fprintf(fattrs
, "#line %d \"%s\"\n", lineno
, infile
);
529 copy_string (finput
, fattrs
, c
);
535 if (c
!= '*' && c
!= '/')
537 copy_comment (finput
, fattrs
, c
);
542 _("unterminated `%{' definition"));
564 /* parse what comes after %token or %nterm.
565 For %token, what_is is STOKEN and what_is_not is SNTERM.
566 For %nterm, the arguments are reversed. */
569 parse_token_decl (int what_is
, int what_is_not
)
571 register int token
= 0;
572 register char *typename
= 0;
573 register struct bucket
*symbol
= NULL
; /* pts to symbol being defined */
578 int tmp_char
= ungetc (skip_white_space (), finput
);
583 fatal (_("Premature EOF after %s"), token_buffer
);
591 if (token
== TYPENAME
)
593 k
= strlen(token_buffer
);
594 typename
= NEW2(k
+ 1, char);
595 strcpy(typename
, token_buffer
);
596 value_components_used
= 1;
599 else if (token
== IDENTIFIER
&& *symval
->tag
== '\"'
603 symval
->class = STOKEN
;
604 symval
->type_name
= typename
;
605 symval
->user_token_number
= symbol
->user_token_number
;
606 symbol
->user_token_number
= SALIAS
;
608 symval
->alias
= symbol
;
609 symbol
->alias
= symval
;
612 nsyms
--; /* symbol and symval combined are only one symbol */
614 else if (token
== IDENTIFIER
)
616 int oldclass
= symval
->class;
619 if (symbol
->class == what_is_not
)
620 complain (_("symbol %s redefined"), symbol
->tag
);
621 symbol
->class = what_is
;
622 if (what_is
== SNTERM
&& oldclass
!= SNTERM
)
623 symbol
->value
= nvars
++;
627 if (symbol
->type_name
== NULL
)
628 symbol
->type_name
= typename
;
629 else if (strcmp(typename
, symbol
->type_name
) != 0)
630 complain (_("type redeclaration for %s"), symbol
->tag
);
633 else if (symbol
&& token
== NUMBER
)
635 symbol
->user_token_number
= numval
;
640 complain (_("`%s' is invalid in %s"),
642 (what_is
== STOKEN
) ? "%token" : "%nterm");
649 /* parse what comes after %thong
651 %thong <type> token number literal
652 the <type> or number may be omitted. The number specifies the
655 Two symbols are entered in the table, one for the token symbol and
656 one for the literal. Both are given the <type>, if any, from the declaration.
657 The ->user_token_number of the first is SALIAS and the ->user_token_number
658 of the second is set to the number, if any, from the declaration.
659 The two symbols are linked via pointers in their ->alias fields.
661 during output_defines_table, the symbol is reported
662 thereafter, only the literal string is retained
663 it is the literal string that is output to yytname
667 parse_thong_decl (void)
670 register struct bucket
*symbol
;
671 register char *typename
= 0;
675 token
= lex(); /* fetch typename or first token */
676 if (token
== TYPENAME
) {
677 k
= strlen(token_buffer
);
678 typename
= NEW2(k
+ 1, char);
679 strcpy(typename
, token_buffer
);
680 value_components_used
= 1;
681 token
= lex(); /* fetch first token */
684 /* process first token */
686 if (token
!= IDENTIFIER
)
688 complain (_("unrecognized item %s, expected an identifier"),
693 symval
->class = STOKEN
;
694 symval
->type_name
= typename
;
695 symval
->user_token_number
= SALIAS
;
698 token
= lex(); /* get number or literal string */
700 if (token
== NUMBER
) {
702 token
= lex(); /* okay, did number, now get literal */
706 /* process literal string token */
708 if (token
!= IDENTIFIER
|| *symval
->tag
!= '\"')
710 complain (_("expected string constant instead of %s"),
715 symval
->class = STOKEN
;
716 symval
->type_name
= typename
;
717 symval
->user_token_number
= usrtoknum
;
719 symval
->alias
= symbol
;
720 symbol
->alias
= symval
;
722 nsyms
--; /* symbol and symval combined are only one symbol */
726 /* Parse what comes after %start */
729 parse_start_decl (void)
732 complain (_("multiple %s declarations"), "%start");
733 if (lex () != IDENTIFIER
)
734 complain (_("invalid %s declaration"), "%start");
744 /* read in a %type declaration and record its information for get_type_name to access */
747 parse_type_decl (void)
752 if (lex() != TYPENAME
)
754 complain ("%s", _("%type declaration has no <typename>"));
759 k
= strlen(token_buffer
);
760 name
= NEW2(k
+ 1, char);
761 strcpy(name
, token_buffer
);
766 int tmp_char
= ungetc (skip_white_space (), finput
);
771 fatal (_("Premature EOF after %s"), token_buffer
);
783 if (symval
->type_name
== NULL
)
784 symval
->type_name
= name
;
785 else if (strcmp(name
, symval
->type_name
) != 0)
786 complain (_("type redeclaration for %s"), symval
->tag
);
791 complain (_("invalid %%type declaration due to item: %s"),
800 /* read in a %left, %right or %nonassoc declaration and record its information. */
801 /* assoc is either LEFT_ASSOC, RIGHT_ASSOC or NON_ASSOC. */
804 parse_assoc_decl (int assoc
)
807 register char *name
= NULL
;
808 register int prev
= 0;
810 lastprec
++; /* Assign a new precedence level, never 0. */
815 int tmp_char
= ungetc (skip_white_space (), finput
);
820 fatal (_("Premature EOF after %s"), token_buffer
);
828 k
= strlen(token_buffer
);
829 name
= NEW2(k
+ 1, char);
830 strcpy(name
, token_buffer
);
837 if (symval
->prec
!= 0)
838 complain (_("redefining precedence of %s"), symval
->tag
);
839 symval
->prec
= lastprec
;
840 symval
->assoc
= assoc
;
841 if (symval
->class == SNTERM
)
842 complain (_("symbol %s redefined"), symval
->tag
);
843 symval
->class = STOKEN
;
845 { /* record the type, if one is specified */
846 if (symval
->type_name
== NULL
)
847 symval
->type_name
= name
;
848 else if (strcmp(name
, symval
->type_name
) != 0)
849 complain (_("type redeclaration for %s"), symval
->tag
);
854 if (prev
== IDENTIFIER
)
856 symval
->user_token_number
= numval
;
861 complain (_("invalid text (%s) - number should be after identifier"),
871 complain (_("unexpected item: %s"), token_buffer
);
882 /* copy the union declaration into fattrs (and fdefines),
883 where it is made into the
884 definition of YYSTYPE, the type of elements of the parser value stack. */
887 parse_union_decl (void)
890 register int count
= 0;
893 complain (_("multiple %s declarations"), "%union");
898 fprintf (fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
900 fprintf (fattrs
, "\n");
902 fprintf (fattrs
, "typedef union");
904 fprintf (fdefines
, "typedef union");
922 if (c
!= '*' && c
!= '/')
924 copy_comment2 (finput
, fattrs
, fdefines
, c
);
934 complain (_("unmatched %s"), "`}'");
938 fprintf (fattrs
, " YYSTYPE;\n");
940 fprintf (fdefines
, " YYSTYPE;\n");
941 /* JF don't choke on trailing semi */
942 c
= skip_white_space ();
953 /* parse the declaration %expect N which says to expect N
954 shift-reduce conflicts. */
957 parse_expect_decl (void)
964 while (c
== ' ' || c
== '\t')
968 while (c
>= '0' && c
<= '9')
978 if (count
<= 0 || count
> 10)
979 complain ("%s", _("argument of %expect is not an integer"));
980 expected_conflicts
= atoi (buffer
);
983 /* that's all of parsing the declaration section */
985 /* FIN is pointing to a location (i.e., a `@'). Output to FOUT
986 a reference to this location. STACK_OFFSET is the number of values
987 in the current rule so far, which says where to find `$0' with
988 respect to the top of the stack. */
990 copy_at (FILE *fin
, FILE *fout
, int stack_offset
)
997 fprintf (fout
, "yyloc");
1000 else if (isdigit(c
) || c
== '-')
1005 n
= read_signed_integer (fin
);
1007 fprintf (fout
, "yylsp[%d]", n
- stack_offset
);
1011 complain (_("@%s is invalid"), printable_version (c
));
1015 /* Get the data type (alternative in the union) of the value for
1016 symbol n in rule rule. */
1019 get_type_name (int n
, symbol_list
*rule
)
1022 register symbol_list
*rp
;
1026 complain (_("invalid $ value"));
1036 if (rp
== NULL
|| rp
->sym
== NULL
)
1038 complain (_("invalid $ value"));
1044 return rp
->sym
->type_name
;
1049 /* After `%guard' is seen in the input file, copy the actual guard
1050 into the guards file. If the guard is followed by an action, copy
1051 that into the actions file. STACK_OFFSET is the number of values
1052 in the current rule so far, which says where to find `$0' with
1053 respect to the top of the stack, for the simple parser in which the
1054 stack is not popped until after the guard is run. */
1057 copy_guard (symbol_list
*rule
, int stack_offset
)
1062 register char *type_name
;
1065 /* offset is always 0 if parser has already popped the stack pointer */
1066 if (semantic_parser
) stack_offset
= 0;
1068 fprintf(fguard
, "\ncase %d:\n", nrules
);
1070 fprintf (fguard
, "#line %d \"%s\"\n", lineno
, infile
);
1076 while (brace_flag
? (count
> 0) : (c
!= ';'))
1097 complain (_("unmatched %s"), "`}'");
1098 c
= getc(finput
); /* skip it */
1104 copy_string (finput
, fguard
, c
);
1110 if (c
!= '*' && c
!= '/')
1112 copy_comment (finput
, fguard
, c
);
1121 register char *cp
= token_buffer
;
1123 while ((c
= getc(finput
)) != '>' && c
> 0)
1125 if (cp
== token_buffer
+ maxtoken
)
1126 cp
= grow_token_buffer(cp
);
1131 type_name
= token_buffer
;
1138 fprintf(fguard
, "yyval");
1140 type_name
= rule
->sym
->type_name
;
1142 fprintf(fguard
, ".%s", type_name
);
1143 if(!type_name
&& typed
)
1144 complain (_("$$ of `%s' has no declared type"),
1147 else if (isdigit(c
) || c
== '-')
1150 n
= read_signed_integer (finput
);
1153 if (!type_name
&& n
> 0)
1154 type_name
= get_type_name(n
, rule
);
1156 fprintf(fguard
, "yyvsp[%d]", n
- stack_offset
);
1158 fprintf(fguard
, ".%s", type_name
);
1159 if (!type_name
&& typed
)
1160 complain (_("$%d of `%s' has no declared type"),
1165 complain (_("$%s is invalid"), printable_version (c
));
1169 copy_at (finput
, fguard
, stack_offset
);
1174 _("unterminated %guard clause"));
1180 if (c
!= '}' || count
!= 0)
1184 c
= skip_white_space();
1186 fprintf(fguard
, ";\n break;}");
1188 copy_action (rule
, stack_offset
);
1191 c
= getc(finput
); /* why not skip_white_space -wjh */
1193 copy_action (rule
, stack_offset
);
1201 /* Assuming that a `{' has just been seen, copy everything up to the
1202 matching `}' into the actions file. STACK_OFFSET is the number of
1203 values in the current rule so far, which says where to find `$0'
1204 with respect to the top of the stack. */
1207 copy_action (symbol_list
*rule
, int stack_offset
)
1212 register char *type_name
;
1214 /* offset is always 0 if parser has already popped the stack pointer */
1215 if (semantic_parser
)
1218 fprintf (faction
, "\ncase %d:\n", nrules
);
1220 fprintf (faction
, "#line %d \"%s\"\n", lineno
, infile
);
1221 putc ('{', faction
);
1244 copy_string (finput
, faction
, c
);
1250 if (c
!= '*' && c
!= '/')
1252 copy_comment (finput
, faction
, c
);
1261 register char *cp
= token_buffer
;
1263 while ((c
= getc(finput
)) != '>' && c
> 0)
1265 if (cp
== token_buffer
+ maxtoken
)
1266 cp
= grow_token_buffer(cp
);
1271 type_name
= token_buffer
;
1272 value_components_used
= 1;
1278 fprintf(faction
, "yyval");
1280 type_name
= get_type_name(0, rule
);
1282 fprintf(faction
, ".%s", type_name
);
1283 if(!type_name
&& typed
)
1284 complain (_("$$ of `%s' has no declared type"),
1287 else if (isdigit(c
) || c
== '-')
1290 n
= read_signed_integer(finput
);
1293 if (!type_name
&& n
> 0)
1294 type_name
= get_type_name(n
, rule
);
1296 fprintf(faction
, "yyvsp[%d]", n
- stack_offset
);
1298 fprintf(faction
, ".%s", type_name
);
1299 if(!type_name
&& typed
)
1300 complain (_("$%d of `%s' has no declared type"),
1305 complain (_("$%s is invalid"), printable_version (c
));
1310 copy_at (finput
, faction
, stack_offset
);
1314 fatal (_("unmatched %s"), "`{'");
1323 /* above loop exits when c is '}' */
1332 fprintf(faction
, ";\n break;}");
1337 /* generate a dummy symbol, a nonterminal,
1338 whose name cannot conflict with the user's names. */
1343 register bucket
*sym
;
1345 sprintf (token_buffer
, "@%d", ++gensym_count
);
1346 sym
= getsym(token_buffer
);
1347 sym
->class = SNTERM
;
1348 sym
->value
= nvars
++;
1352 /* Parse the input grammar into a one symbol_list structure.
1353 Each rule is represented by a sequence of symbols: the left hand side
1354 followed by the contents of the right hand side, followed by a null pointer
1355 instead of a symbol to terminate the rule.
1356 The next symbol is the lhs of the following rule.
1358 All guards and actions are copied out to the appropriate files,
1359 labelled by the rule number they apply to. */
1365 register bucket
*lhs
= NULL
;
1366 register symbol_list
*p
;
1367 register symbol_list
*p1
;
1368 register bucket
*bp
;
1370 symbol_list
*crule
; /* points to first symbol_list of current rule. */
1371 /* its symbol is the lhs of the rule. */
1372 symbol_list
*crule1
; /* points to the symbol_list preceding crule. */
1378 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1380 if (t
== IDENTIFIER
|| t
== BAR
)
1382 register int actionflag
= 0;
1383 int rulelength
= 0; /* number of symbols in rhs of this rule so far */
1384 int xactions
= 0; /* JF for error checking */
1385 bucket
*first_rhs
= 0;
1387 if (t
== IDENTIFIER
)
1400 complain (_("ill-formed rule: initial symbol not followed by colon"));
1405 if (nrules
== 0 && t
== BAR
)
1407 complain (_("grammar starts with vertical bar"));
1408 lhs
= symval
; /* BOGUS: use a random symval */
1410 /* start a new rule and record its lhs. */
1415 record_rule_line ();
1417 p
= NEW(symbol_list
);
1429 /* mark the rule's lhs as a nonterminal if not already so. */
1431 if (lhs
->class == SUNKNOWN
)
1433 lhs
->class = SNTERM
;
1437 else if (lhs
->class == STOKEN
)
1438 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1440 /* read the rhs of the rule. */
1448 crule
->ruleprec
= symval
;
1452 if (! (t
== IDENTIFIER
|| t
== LEFT_CURLY
)) break;
1454 /* If next token is an identifier, see if a colon follows it.
1455 If one does, exit this rule now. */
1456 if (t
== IDENTIFIER
)
1458 register bucket
*ssave
;
1465 if (t1
== COLON
) break;
1467 if(!first_rhs
) /* JF */
1469 /* Not followed by colon =>
1470 process as part of this rule's rhs. */
1473 /* If we just passed an action, that action was in the middle
1474 of a rule, so make a dummy rule to reduce it to a
1478 register bucket
*sdummy
;
1480 /* Since the action was written out with this rule's */
1481 /* number, we must give the new rule this number */
1482 /* by inserting the new rule before it. */
1484 /* Make a dummy nonterminal, a gensym. */
1487 /* Make a new rule, whose body is empty,
1488 before the current one, so that the action
1489 just read can belong to it. */
1492 record_rule_line ();
1493 p
= NEW(symbol_list
);
1498 crule1
= NEW(symbol_list
);
1500 crule1
->next
= crule
;
1502 /* insert the dummy generated by that rule into this rule. */
1504 p
= NEW(symbol_list
);
1512 if (t
== IDENTIFIER
)
1515 p
= NEW(symbol_list
);
1520 else /* handle an action. */
1522 copy_action(crule
, rulelength
);
1524 xactions
++; /* JF */
1527 } /* end of read rhs of rule */
1529 /* Put an empty link in the list to mark the end of this rule */
1530 p
= NEW(symbol_list
);
1536 complain (_("two @prec's in a row"));
1538 crule
->ruleprec
= symval
;
1543 if (! semantic_parser
)
1545 _("%guard present but %semantic_parser not specified"));
1547 copy_guard(crule
, rulelength
);
1550 else if (t
== LEFT_CURLY
)
1552 /* This case never occurs -wjh */
1554 complain (_("two actions at end of one rule"));
1555 copy_action(crule
, rulelength
);
1557 xactions
++; /* -wjh */
1560 /* If $$ is being set in default way, report if any type
1564 && lhs
->type_name
!= first_rhs
->type_name
)
1566 if (lhs
->type_name
== 0
1567 || first_rhs
->type_name
== 0
1568 || strcmp(lhs
->type_name
,first_rhs
->type_name
))
1569 complain (_("type clash (`%s' `%s') on default action"),
1570 lhs
->type_name
? lhs
->type_name
: "",
1571 first_rhs
->type_name
? first_rhs
->type_name
: "");
1573 /* Warn if there is no default for $$ but we need one. */
1574 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1575 complain (_("empty rule for typed nonterminal, and no action"));
1580 /* these things can appear as alternatives to rules. */
1582 a) none of the documentation allows them
1583 b) most of them scan forward until finding a next %
1584 thus they may swallow lots of intervening rules
1586 else if (t
== TOKEN
)
1588 parse_token_decl(STOKEN
, SNTERM
);
1591 else if (t
== NTERM
)
1593 parse_token_decl(SNTERM
, STOKEN
);
1600 else if (t
== UNION
)
1605 else if (t
== EXPECT
)
1607 parse_expect_decl();
1610 else if (t
== START
)
1619 complain (_("invalid input: %s"), token_buffer
);
1624 /* grammar has been read. Do some checking */
1626 if (nsyms
> MAXSHORT
)
1627 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1630 fatal (_("no rules in the input grammar"));
1632 if (typed
== 0 /* JF put out same default YYSTYPE as YACC does */
1633 && !value_components_used
)
1635 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1636 but it seems better to be consistent.
1637 Most programs should declare their own type anyway. */
1638 fprintf(fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1640 fprintf(fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1643 /* Report any undefined symbols and consider them nonterminals. */
1645 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1646 if (bp
->class == SUNKNOWN
)
1648 complain (_("symbol %s is used, but is not defined as a token and has no rules"),
1651 bp
->value
= nvars
++;
1654 ntokens
= nsyms
- nvars
;
1659 record_rule_line (void)
1661 /* Record each rule's source line number in rline table. */
1663 if (nrules
>= rline_allocated
)
1665 rline_allocated
= nrules
* 2;
1666 rline
= (short *) xrealloc ((char *) rline
,
1667 rline_allocated
* sizeof (short));
1669 rline
[nrules
] = lineno
;
1674 /* read in a %type declaration and record its information for get_type_name to access */
1675 /* this is unused. it is only called from the #if 0 part of readgram */
1681 register char *name
;
1687 complain (_("invalid %s declaration"), "%type");
1691 k
= strlen(token_buffer
);
1692 name
= NEW2(k
+ 1, char);
1693 strcpy(name
, token_buffer
);
1708 if (symval
->type_name
== NULL
)
1709 symval
->type_name
= name
;
1710 else if (strcmp(name
, symval
->type_name
) != 0)
1711 complain (_("type redeclaration for %s"), symval
->tag
);
1723 /* Assign symbol numbers, and write definition of token names into
1724 fdefines. Set up vectors tags and sprec of names and precedences
1730 register bucket
*bp
;
1731 register int tokno
= 1;
1733 register int last_user_token_number
;
1734 static char DOLLAR
[] = "$";
1736 /* int lossage = 0; JF set but not used */
1738 tags
= NEW2(nsyms
+ 1, char *);
1740 user_toknums
= NEW2(nsyms
+ 1, int);
1741 user_toknums
[0] = 0;
1743 sprec
= NEW2(nsyms
, short);
1744 sassoc
= NEW2(nsyms
, short);
1746 max_user_token_number
= 256;
1747 last_user_token_number
= 256;
1749 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1751 if (bp
->class == SNTERM
)
1753 bp
->value
+= ntokens
;
1757 /* this symbol and its alias are a single token defn.
1758 allocate a tokno, and assign to both check agreement of
1759 ->prec and ->assoc fields and make both the same */
1761 bp
->value
= bp
->alias
->value
= tokno
++;
1763 if (bp
->prec
!= bp
->alias
->prec
)
1765 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1766 && bp
->user_token_number
== SALIAS
)
1767 complain (_("conflicting precedences for %s and %s"),
1768 bp
->tag
, bp
->alias
->tag
);
1770 bp
->alias
->prec
= bp
->prec
;
1772 bp
->prec
= bp
->alias
->prec
;
1775 if (bp
->assoc
!= bp
->alias
->assoc
)
1777 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1778 && bp
->user_token_number
== SALIAS
)
1779 complain (_("conflicting assoc values for %s and %s"),
1780 bp
->tag
, bp
->alias
->tag
);
1782 bp
->alias
->assoc
= bp
->assoc
;
1784 bp
->assoc
= bp
->alias
->assoc
;
1787 if (bp
->user_token_number
== SALIAS
)
1788 continue; /* do not do processing below for SALIASs */
1791 else /* bp->class == STOKEN */
1793 bp
->value
= tokno
++;
1796 if (bp
->class == STOKEN
)
1798 if (translations
&& !(bp
->user_token_number
))
1799 bp
->user_token_number
= ++last_user_token_number
;
1800 if (bp
->user_token_number
> max_user_token_number
)
1801 max_user_token_number
= bp
->user_token_number
;
1804 tags
[bp
->value
] = bp
->tag
;
1805 user_toknums
[bp
->value
] = bp
->user_token_number
;
1806 sprec
[bp
->value
] = bp
->prec
;
1807 sassoc
[bp
->value
] = bp
->assoc
;
1815 token_translations
= NEW2(max_user_token_number
+1, short);
1817 /* initialize all entries for literal tokens to 2, the internal
1818 token number for $undefined., which represents all invalid
1820 for (j
= 0; j
<= max_user_token_number
; j
++)
1821 token_translations
[j
] = 2;
1823 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1825 if (bp
->value
>= ntokens
)
1826 continue; /* non-terminal */
1827 if (bp
->user_token_number
== SALIAS
)
1829 if (token_translations
[bp
->user_token_number
] != 2)
1830 complain (_("tokens %s and %s both assigned number %d"),
1831 tags
[token_translations
[bp
->user_token_number
]],
1833 bp
->user_token_number
);
1834 token_translations
[bp
->user_token_number
] = bp
->value
;
1838 error_token_number
= errtoken
->value
;
1841 output_token_defines(ftable
);
1843 if (startval
->class == SUNKNOWN
)
1844 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1845 else if (startval
->class == STOKEN
)
1846 fatal (_("the start symbol %s is a token"), startval
->tag
);
1848 start_symbol
= startval
->value
;
1852 output_token_defines(fdefines
);
1856 if (spec_name_prefix
)
1857 fprintf(fdefines
, "\nextern YYSTYPE %slval;\n", spec_name_prefix
);
1859 fprintf(fdefines
, "\nextern YYSTYPE yylval;\n");
1862 if (semantic_parser
)
1863 for (i
= ntokens
; i
< nsyms
; i
++)
1865 /* don't make these for dummy nonterminals made by gensym. */
1866 if (*tags
[i
] != '@')
1867 fprintf(fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1870 /* `fdefines' is now a temporary file, so we need to copy its
1871 contents in `done', so we can't close it here. */
1878 /* For named tokens, but not literal ones, define the name. The value
1879 is the user token number. */
1882 output_token_defines (FILE *file
)
1885 register char *cp
, *symbol
;
1888 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1890 symbol
= bp
->tag
; /* get symbol */
1892 if (bp
->value
>= ntokens
) continue;
1893 if (bp
->user_token_number
== SALIAS
) continue;
1894 if ('\'' == *symbol
) continue; /* skip literal character */
1895 if (bp
== errtoken
) continue; /* skip error token */
1896 if ('\"' == *symbol
)
1898 /* use literal string only if given a symbol with an alias */
1900 symbol
= bp
->alias
->tag
;
1905 /* Don't #define nonliteral tokens whose names contain periods. */
1907 while ((c
= *cp
++) && c
!= '.');
1908 if (c
!= '\0') continue;
1910 fprintf (file
, "#define\t%s\t%d\n", symbol
,
1911 ((translations
&& ! rawtoknumflag
)
1912 ? bp
->user_token_number
1914 if (semantic_parser
)
1915 fprintf (file
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1923 /* convert the rules into the representation using rrhs, rlhs and ritems. */
1928 register int itemno
;
1929 register int ruleno
;
1930 register symbol_list
*p
;
1931 /* register bucket *bp; JF unused */
1935 ritem
= NEW2(nitems
+ 1, short);
1936 rlhs
= NEW2(nrules
, short) - 1;
1937 rrhs
= NEW2(nrules
, short) - 1;
1938 rprec
= NEW2(nrules
, short) - 1;
1939 rprecsym
= NEW2(nrules
, short) - 1;
1940 rassoc
= NEW2(nrules
, short) - 1;
1948 rlhs
[ruleno
] = p
->sym
->value
;
1949 rrhs
[ruleno
] = itemno
;
1950 ruleprec
= p
->ruleprec
;
1955 ritem
[itemno
++] = p
->sym
->value
;
1956 /* A rule gets by default the precedence and associativity
1957 of the last token in it. */
1958 if (p
->sym
->class == STOKEN
)
1960 rprec
[ruleno
] = p
->sym
->prec
;
1961 rassoc
[ruleno
] = p
->sym
->assoc
;
1966 /* If this rule has a %prec,
1967 the specified symbol's precedence replaces the default. */
1970 rprec
[ruleno
] = ruleprec
->prec
;
1971 rassoc
[ruleno
] = ruleprec
->assoc
;
1972 rprecsym
[ruleno
] = ruleprec
->value
;
1975 ritem
[itemno
++] = -ruleno
;