]>
git.saurik.com Git - bison.git/blob - src/reader.c
1 /* Input parser for bison
2 Copyright 1984, 1986, 1989, 1992, 1998, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
7 Bison is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 Bison is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
36 #include "conflicts.h"
37 #include "muscle_tab.h"
39 typedef struct symbol_list
41 struct symbol_list
*next
;
51 static symbol_list
*grammar
;
52 static int start_flag
;
53 static bucket
*startval
;
55 /* Nonzero if components of semantic values are used, implying
56 they must be unions. */
57 static int value_components_used
;
59 /* Nonzero if %union has been seen. */
62 /* Incremented for each %left, %right or %nonassoc seen */
65 static bucket
*errtoken
;
66 static bucket
*undeftoken
;
70 symbol_list_new (bucket
*sym
)
72 symbol_list
*res
= XMALLOC (symbol_list
, 1);
82 /*===================\
84 \===================*/
87 skip_to_char (int target
)
91 complain (_(" Skipping to next \\n"));
93 complain (_(" Skipping to next %c"), target
);
96 c
= skip_white_space ();
97 while (c
!= target
&& c
!= EOF
);
103 /*---------------------------------------------------------.
104 | Read a signed integer from STREAM and return its value. |
105 `---------------------------------------------------------*/
108 read_signed_integer (FILE *stream
)
110 int c
= getc (stream
);
122 n
= 10 * n
+ (c
- '0');
131 /*--------------------------------------------------------------.
132 | Get the data type (alternative in the union) of the value for |
133 | symbol N in rule RULE. |
134 `--------------------------------------------------------------*/
137 get_type_name (int n
, symbol_list
*rule
)
144 complain (_("invalid $ value"));
154 if (rp
== NULL
|| rp
->sym
== NULL
)
156 complain (_("invalid $ value"));
162 return rp
->sym
->type_name
;
165 /*------------------------------------------------------------.
166 | Dump the string from FIN to OOUT if non null. MATCH is the |
167 | delimiter of the string (either ' or "). |
168 `------------------------------------------------------------*/
171 copy_string2 (FILE *fin
, struct obstack
*oout
, int match
, int store
)
176 obstack_1grow (oout
, match
);
183 fatal (_("unterminated string at end of file"));
186 complain (_("unterminated string"));
188 c
= match
; /* invent terminator */
192 obstack_1grow (oout
, c
);
198 fatal (_("unterminated string at end of file"));
199 obstack_1grow (oout
, c
);
209 obstack_1grow (oout
, c
);
215 copy_string (FILE *fin
, struct obstack
*oout
, int match
)
217 copy_string2 (fin
, oout
, match
, 1);
223 copy_identifier (FILE *fin
, struct obstack
*oout
)
227 while (isalnum (c
= getc (fin
)) || c
== '_')
228 obstack_1grow (oout
, c
);
233 /*-----------------------------------------------------------------.
234 | Dump the wannabee comment from IN to OUT1 and OUT2 (which can be |
235 | NULL). In fact we just saw a `/', which might or might not be a |
236 | comment. In any case, copy what we saw. |
238 | OUT2 might be NULL. |
239 `-----------------------------------------------------------------*/
242 copy_comment2 (FILE *fin
, struct obstack
*oout1
, struct obstack
*oout2
)
248 /* We read a `/', output it. */
249 obstack_1grow (oout1
, '/');
251 obstack_1grow (oout2
, '/');
253 switch ((c
= getc (fin
)))
266 obstack_1grow (oout1
, c
);
268 obstack_1grow (oout2
, c
);
274 if (!cplus_comment
&& c
== '*')
278 obstack_1grow (oout1
, c
);
280 obstack_1grow (oout2
, c
);
286 obstack_1grow (oout1
, c
);
288 obstack_1grow (oout2
, c
);
295 obstack_1grow (oout1
, c
);
297 obstack_1grow (oout2
, c
);
304 fatal (_("unterminated comment"));
307 obstack_1grow (oout1
, c
);
309 obstack_1grow (oout2
, c
);
316 /*-------------------------------------------------------------------.
317 | Dump the comment (actually the current string starting with a `/') |
318 | from FIN to OOUT. |
319 `-------------------------------------------------------------------*/
322 copy_comment (FILE *fin
, struct obstack
*oout
)
324 copy_comment2 (fin
, oout
, NULL
);
328 /*-----------------------------------------------------------------.
329 | FIN is pointing to a location (i.e., a `@'). Output to OOUT a |
330 | reference to this location. STACK_OFFSET is the number of values |
331 | in the current rule so far, which says where to find `$0' with |
332 | respect to the top of the stack. |
333 `-----------------------------------------------------------------*/
336 copy_at (FILE *fin
, struct obstack
*oout
, int stack_offset
)
343 obstack_sgrow (oout
, "yyloc");
346 else if (isdigit (c
) || c
== '-')
351 n
= read_signed_integer (fin
);
353 obstack_fgrow1 (oout
, "yylsp[%d]", n
- stack_offset
);
360 complain (_("%s is invalid"), quote (buf
));
365 /*-------------------------------------------------------------------.
366 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
368 | Possible inputs: $[<TYPENAME>]($|integer) |
370 | Output to OOUT a reference to this semantic value. STACK_OFFSET is |
371 | the number of values in the current rule so far, which says where |
372 | to find `$0' with respect to the top of the stack. |
373 `-------------------------------------------------------------------*/
376 copy_dollar (FILE *fin
, struct obstack
*oout
,
377 symbol_list
*rule
, int stack_offset
)
380 const char *type_name
= NULL
;
382 /* Get the type name if explicit. */
385 read_type_name (fin
);
386 type_name
= token_buffer
;
387 value_components_used
= 1;
393 obstack_sgrow (oout
, "yyval");
396 type_name
= get_type_name (0, rule
);
398 obstack_fgrow1 (oout
, ".%s", type_name
);
399 if (!type_name
&& typed
)
400 complain (_("$$ of `%s' has no declared type"),
403 else if (isdigit (c
) || c
== '-')
407 n
= read_signed_integer (fin
);
409 if (!type_name
&& n
> 0)
410 type_name
= get_type_name (n
, rule
);
412 obstack_fgrow1 (oout
, "yyvsp[%d]", n
- stack_offset
);
415 obstack_fgrow1 (oout
, ".%s", type_name
);
416 if (!type_name
&& typed
)
417 complain (_("$%d of `%s' has no declared type"),
424 complain (_("%s is invalid"), quote (buf
));
428 /*-------------------------------------------------------------------.
429 | Copy the contents of a `%{ ... %}' into the definitions file. The |
430 | `%{' has already been read. Return after reading the `%}'. |
431 `-------------------------------------------------------------------*/
434 copy_definition (void)
437 /* -1 while reading a character if prev char was %. */
443 obstack_fgrow2 (&attrs_obstack
, muscle_find ("linef"),
444 lineno
, quotearg_style (c_quoting_style
,
445 muscle_find("filename")));
458 obstack_1grow (&attrs_obstack
, c
);
468 copy_string (finput
, &attrs_obstack
, c
);
472 copy_comment (finput
, &attrs_obstack
);
476 fatal ("%s", _("unterminated `%{' definition"));
479 obstack_1grow (&attrs_obstack
, c
);
488 obstack_1grow (&attrs_obstack
, '%');
495 /*-------------------------------------------------------------------.
496 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
497 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
499 `-------------------------------------------------------------------*/
502 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
504 token_t token
= tok_undef
;
505 char *typename
= NULL
;
507 /* The symbol being defined. */
508 struct bucket
*symbol
= NULL
;
510 /* After `%token' and `%nterm', any number of symbols maybe be
514 int tmp_char
= ungetc (skip_white_space (), finput
);
516 /* `%' (for instance from `%token', or from `%%' etc.) is the
517 only valid means to end this declaration. */
521 fatal (_("Premature EOF after %s"), token_buffer
);
524 if (token
== tok_comma
)
529 if (token
== tok_typename
)
531 typename
= xstrdup (token_buffer
);
532 value_components_used
= 1;
535 else if (token
== tok_identifier
&& *symval
->tag
== '\"' && symbol
)
538 warn (_("symbol `%s' used more than once as a literal string"),
540 else if (symbol
->alias
)
541 warn (_("symbol `%s' given more than one literal string"),
545 symval
->class = token_sym
;
546 symval
->type_name
= typename
;
547 symval
->user_token_number
= symbol
->user_token_number
;
548 symbol
->user_token_number
= SALIAS
;
549 symval
->alias
= symbol
;
550 symbol
->alias
= symval
;
551 /* symbol and symval combined are only one symbol */
556 else if (token
== tok_identifier
)
558 int oldclass
= symval
->class;
561 if (symbol
->class == what_is_not
)
562 complain (_("symbol %s redefined"), symbol
->tag
);
563 symbol
->class = what_is
;
564 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
565 symbol
->value
= nvars
++;
569 if (symbol
->type_name
== NULL
)
570 symbol
->type_name
= typename
;
571 else if (strcmp (typename
, symbol
->type_name
) != 0)
572 complain (_("type redeclaration for %s"), symbol
->tag
);
575 else if (symbol
&& token
== tok_number
)
577 symbol
->user_token_number
= numval
;
581 complain (_("`%s' is invalid in %s"),
583 (what_is
== token_sym
) ? "%token" : "%nterm");
591 /*------------------------------.
592 | Parse what comes after %start |
593 `------------------------------*/
596 parse_start_decl (void)
599 complain (_("multiple %s declarations"), "%start");
600 if (lex () != tok_identifier
)
601 complain (_("invalid %s declaration"), "%start");
609 /*-----------------------------------------------------------.
610 | read in a %type declaration and record its information for |
611 | get_type_name to access |
612 `-----------------------------------------------------------*/
615 parse_type_decl (void)
619 if (lex () != tok_typename
)
621 complain ("%s", _("%type declaration has no <typename>"));
626 name
= xstrdup (token_buffer
);
631 int tmp_char
= ungetc (skip_white_space (), finput
);
636 fatal (_("Premature EOF after %s"), token_buffer
);
648 if (symval
->type_name
== NULL
)
649 symval
->type_name
= name
;
650 else if (strcmp (name
, symval
->type_name
) != 0)
651 complain (_("type redeclaration for %s"), symval
->tag
);
656 complain (_("invalid %%type declaration due to item: %s"),
665 /*----------------------------------------------------------------.
666 | Read in a %left, %right or %nonassoc declaration and record its |
668 `----------------------------------------------------------------*/
671 parse_assoc_decl (associativity assoc
)
676 lastprec
++; /* Assign a new precedence level, never 0. */
681 int tmp_char
= ungetc (skip_white_space (), finput
);
686 fatal (_("Premature EOF after %s"), token_buffer
);
693 name
= xstrdup (token_buffer
);
700 if (symval
->prec
!= 0)
701 complain (_("redefining precedence of %s"), symval
->tag
);
702 symval
->prec
= lastprec
;
703 symval
->assoc
= assoc
;
704 if (symval
->class == nterm_sym
)
705 complain (_("symbol %s redefined"), symval
->tag
);
706 symval
->class = token_sym
;
708 { /* record the type, if one is specified */
709 if (symval
->type_name
== NULL
)
710 symval
->type_name
= name
;
711 else if (strcmp (name
, symval
->type_name
) != 0)
712 complain (_("type redeclaration for %s"), symval
->tag
);
717 if (prev
== tok_identifier
)
719 symval
->user_token_number
= numval
;
724 ("invalid text (%s) - number should be after identifier"),
734 complain (_("unexpected item: %s"), token_buffer
);
744 /*--------------------------------------------------------------.
745 | Copy the union declaration into the stype muscle |
746 | (and fdefines), where it is made into the definition of |
747 | YYSTYPE, the type of elements of the parser value stack. |
748 `--------------------------------------------------------------*/
751 parse_union_decl (void)
755 struct obstack union_obstack
;
756 const char *prologue
= "\
759 const char *epilogue
= "\
761 # define YYSTYPE yystype\n\
765 complain (_("multiple %s declarations"), "%union");
769 /* FIXME: I'm worried: are you sure attrs_obstack is properly
771 /* I don't see any reasons to keep this line, because we should
772 create a special skeleton for this option. */
774 obstack_1grow (&attrs_obstack
, '\n');
776 obstack_init (&union_obstack
);
777 obstack_sgrow (&union_obstack
, "union");
779 obstack_sgrow (&defines_obstack
, prologue
);
785 /* If C contains '/', it is output by copy_comment (). */
788 obstack_1grow (&union_obstack
, c
);
790 obstack_1grow (&defines_obstack
, c
);
800 copy_comment2 (finput
, &defines_obstack
, &union_obstack
);
809 complain (_("unmatched %s"), "`}'");
814 obstack_sgrow (&defines_obstack
, epilogue
);
815 /* JF don't choke on trailing semi */
816 c
= skip_white_space ();
819 obstack_1grow (&union_obstack
, 0);
820 muscle_insert ("stype", obstack_finish (&union_obstack
));
831 /*-------------------------------------------------------.
832 | Parse the declaration %expect N which says to expect N |
833 | shift-reduce conflicts. |
834 `-------------------------------------------------------*/
837 parse_expect_decl (void)
839 int c
= skip_white_space ();
843 complain (_("argument of %%expect is not an integer"));
845 expected_conflicts
= read_signed_integer (finput
);
849 /*-------------------------------------------------------------------.
850 | Parse what comes after %thong. the full syntax is |
852 | %thong <type> token number literal |
854 | the <type> or number may be omitted. The number specifies the |
855 | user_token_number. |
857 | Two symbols are entered in the table, one for the token symbol and |
858 | one for the literal. Both are given the <type>, if any, from the |
859 | declaration. The ->user_token_number of the first is SALIAS and |
860 | the ->user_token_number of the second is set to the number, if |
861 | any, from the declaration. The two symbols are linked via |
862 | pointers in their ->alias fields. |
864 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
865 | only the literal string is retained it is the literal string that |
866 | is output to yytname |
867 `-------------------------------------------------------------------*/
870 parse_thong_decl (void)
873 struct bucket
*symbol
;
875 int usrtoknum
= SUNDEF
;
877 token
= lex (); /* fetch typename or first token */
878 if (token
== tok_typename
)
880 typename
= xstrdup (token_buffer
);
881 value_components_used
= 1;
882 token
= lex (); /* fetch first token */
885 /* process first token */
887 if (token
!= tok_identifier
)
889 complain (_("unrecognized item %s, expected an identifier"),
894 symval
->class = token_sym
;
895 symval
->type_name
= typename
;
896 symval
->user_token_number
= SALIAS
;
899 token
= lex (); /* get number or literal string */
901 if (token
== tok_number
)
904 token
= lex (); /* okay, did number, now get literal */
907 /* process literal string token */
909 if (token
!= tok_identifier
|| *symval
->tag
!= '\"')
911 complain (_("expected string constant instead of %s"), token_buffer
);
915 symval
->class = token_sym
;
916 symval
->type_name
= typename
;
917 symval
->user_token_number
= usrtoknum
;
919 symval
->alias
= symbol
;
920 symbol
->alias
= symval
;
922 /* symbol and symval combined are only one symbol. */
927 parse_muscle_decl (void)
929 int ch
= ungetc (skip_white_space (), finput
);
934 if (!isalpha (ch
) && ch
!= '_')
936 complain (_("invalid %s declaration"), "%define");
940 copy_identifier (finput
, &muscle_obstack
);
941 obstack_1grow (&muscle_obstack
, 0);
942 muscle_key
= obstack_finish (&muscle_obstack
);
945 ch
= skip_white_space ();
951 complain (_("invalid %s declaration"), "%define");
956 fatal (_("Premature EOF after %s"), "\"");
958 copy_string2 (finput
, &muscle_obstack
, '"', 0);
959 obstack_1grow (&muscle_obstack
, 0);
960 muscle_value
= obstack_finish (&muscle_obstack
);
962 /* Store the (key, value) pair in the environment. */
963 muscle_insert (muscle_key
, muscle_value
);
968 /*---------------------------------.
969 | Parse a double quoted parameter. |
970 `---------------------------------*/
973 parse_dquoted_param (const char *from
)
975 struct obstack param_obstack
;
976 const char *param
= NULL
;
979 obstack_init (¶m_obstack
);
980 c
= skip_white_space ();
984 complain (_("invalid %s declaration"), from
);
992 if (literalchar (NULL
, &c
, '\"'))
993 obstack_1grow (¶m_obstack
, c
);
998 obstack_1grow (¶m_obstack
, '\0');
999 param
= obstack_finish (¶m_obstack
);
1001 if (c
!= '"' || strlen (param
) == 0)
1003 complain (_("invalid %s declaration"), from
);
1013 /*----------------------------------.
1014 | Parse what comes after %skeleton. |
1015 `----------------------------------*/
1018 parse_skel_decl (void)
1020 skeleton
= parse_dquoted_param ("%skeleton");
1023 /*----------------------------------------------------------------.
1024 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
1025 | any `%' declarations, and copy the contents of any `%{ ... %}' |
1026 | groups to ATTRS_OBSTACK. |
1027 `----------------------------------------------------------------*/
1030 read_declarations (void)
1034 int c
= skip_white_space ();
1038 token_t tok
= parse_percent_token ();
1042 case tok_two_percents
:
1045 case tok_percent_left_curly
:
1050 parse_token_decl (token_sym
, nterm_sym
);
1054 parse_token_decl (nterm_sym
, token_sym
);
1062 parse_start_decl ();
1066 parse_union_decl ();
1070 parse_expect_decl ();
1074 parse_thong_decl ();
1078 parse_assoc_decl (left_assoc
);
1082 parse_assoc_decl (right_assoc
);
1086 parse_assoc_decl (non_assoc
);
1090 parse_muscle_decl ();
1108 complain (_("unrecognized: %s"), token_buffer
);
1113 fatal (_("no input grammar"));
1118 complain (_("unknown character: %s"), quote (buf
));
1124 /*-------------------------------------------------------------------.
1125 | Assuming that a `{' has just been seen, copy everything up to the |
1126 | matching `}' into the actions file. STACK_OFFSET is the number of |
1127 | values in the current rule so far, which says where to find `$0' |
1128 | with respect to the top of the stack. |
1129 `-------------------------------------------------------------------*/
1132 copy_action (symbol_list
*rule
, int stack_offset
)
1137 /* offset is always 0 if parser has already popped the stack pointer */
1138 if (semantic_parser
)
1141 obstack_fgrow1 (&action_obstack
, "\ncase %d:\n", nrules
);
1145 obstack_fgrow2 (&action_obstack
, muscle_find ("linef"),
1146 lineno
, quotearg_style (c_quoting_style
,
1147 muscle_find ("filename")));
1149 obstack_1grow (&action_obstack
, '{');
1161 obstack_1grow (&action_obstack
, c
);
1166 obstack_1grow (&action_obstack
, c
);
1172 copy_string (finput
, &action_obstack
, c
);
1176 copy_comment (finput
, &action_obstack
);
1180 copy_dollar (finput
, &action_obstack
,
1181 rule
, stack_offset
);
1185 copy_at (finput
, &action_obstack
,
1190 fatal (_("unmatched %s"), "`{'");
1193 obstack_1grow (&action_obstack
, c
);
1199 /* above loop exits when c is '}' */
1203 obstack_1grow (&action_obstack
, c
);
1208 obstack_sgrow (&action_obstack
, ";\n break;}");
1211 /*-------------------------------------------------------------------.
1212 | After `%guard' is seen in the input file, copy the actual guard |
1213 | into the guards file. If the guard is followed by an action, copy |
1214 | that into the actions file. STACK_OFFSET is the number of values |
1215 | in the current rule so far, which says where to find `$0' with |
1216 | respect to the top of the stack, for the simple parser in which |
1217 | the stack is not popped until after the guard is run. |
1218 `-------------------------------------------------------------------*/
1221 copy_guard (symbol_list
*rule
, int stack_offset
)
1227 /* offset is always 0 if parser has already popped the stack pointer */
1228 if (semantic_parser
)
1231 obstack_fgrow1 (&guard_obstack
, "\ncase %d:\n", nrules
);
1233 obstack_fgrow2 (&guard_obstack
, muscle_find ("linef"),
1234 lineno
, quotearg_style (c_quoting_style
,
1235 muscle_find ("filename")));
1236 obstack_1grow (&guard_obstack
, '{');
1241 while (brace_flag
? (count
> 0) : (c
!= ';'))
1246 obstack_1grow (&guard_obstack
, c
);
1251 obstack_1grow (&guard_obstack
, c
);
1257 obstack_1grow (&guard_obstack
, c
);
1262 complain (_("unmatched %s"), "`}'");
1263 c
= getc (finput
); /* skip it */
1269 copy_string (finput
, &guard_obstack
, c
);
1273 copy_comment (finput
, &guard_obstack
);
1277 copy_dollar (finput
, &guard_obstack
, rule
, stack_offset
);
1281 copy_at (finput
, &guard_obstack
, stack_offset
);
1285 fatal ("%s", _("unterminated %guard clause"));
1288 obstack_1grow (&guard_obstack
, c
);
1291 if (c
!= '}' || count
!= 0)
1295 c
= skip_white_space ();
1297 obstack_sgrow (&guard_obstack
, ";\n break;}");
1299 copy_action (rule
, stack_offset
);
1302 c
= getc (finput
); /* why not skip_white_space -wjh */
1304 copy_action (rule
, stack_offset
);
1311 /*-------------------------------------------------------------------.
1312 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1313 | with the user's names. |
1314 `-------------------------------------------------------------------*/
1319 /* Incremented for each generated symbol */
1320 static int gensym_count
= 0;
1321 static char buf
[256];
1325 sprintf (buf
, "@%d", ++gensym_count
);
1327 sym
= getsym (token_buffer
);
1328 sym
->class = nterm_sym
;
1329 sym
->value
= nvars
++;
1334 /*------------------------------------------------------------------.
1335 | read in a %type declaration and record its information for |
1336 | get_type_name to access. This is unused. It is only called from |
1337 | the #if 0 part of readgram |
1338 `------------------------------------------------------------------*/
1349 if (token
!= tok_typename
)
1351 complain (_("invalid %s declaration"), "%type");
1355 name
= xstrdup (token_buffer
);
1369 case tok_identifier
:
1370 if (symval
->type_name
== NULL
)
1371 symval
->type_name
= name
;
1372 else if (strcmp (name
, symval
->type_name
) != 0)
1373 complain (_("type redeclaration for %s"), symval
->tag
);
1385 /*------------------------------------------------------------------.
1386 | Parse the input grammar into a one symbol_list structure. Each |
1387 | rule is represented by a sequence of symbols: the left hand side |
1388 | followed by the contents of the right hand side, followed by a |
1389 | null pointer instead of a symbol to terminate the rule. The next |
1390 | symbol is the lhs of the following rule. |
1392 | All guards and actions are copied out to the appropriate files, |
1393 | labelled by the rule number they apply to. |
1394 `------------------------------------------------------------------*/
1405 /* Points to first symbol_list of current rule. its symbol is the
1408 /* Points to the symbol_list preceding crule. */
1409 symbol_list
*crule1
;
1415 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1417 if (t
== tok_identifier
|| t
== tok_bar
)
1419 int action_flag
= 0;
1420 /* Number of symbols in rhs of this rule so far */
1422 int xactions
= 0; /* JF for error checking */
1423 bucket
*first_rhs
= 0;
1425 if (t
== tok_identifier
)
1438 complain (_("ill-formed rule: initial symbol not followed by colon"));
1443 if (nrules
== 0 && t
== tok_bar
)
1445 complain (_("grammar starts with vertical bar"));
1446 lhs
= symval
; /* BOGUS: use a random symval */
1448 /* start a new rule and record its lhs. */
1453 p
= symbol_list_new (lhs
);
1464 /* mark the rule's lhs as a nonterminal if not already so. */
1466 if (lhs
->class == unknown_sym
)
1468 lhs
->class = nterm_sym
;
1472 else if (lhs
->class == token_sym
)
1473 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1475 /* read the rhs of the rule. */
1483 crule
->ruleprec
= symval
;
1487 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1490 /* If next token is an identifier, see if a colon follows it.
1491 If one does, exit this rule now. */
1492 if (t
== tok_identifier
)
1501 if (t1
== tok_colon
)
1504 if (!first_rhs
) /* JF */
1506 /* Not followed by colon =>
1507 process as part of this rule's rhs. */
1510 /* If we just passed an action, that action was in the middle
1511 of a rule, so make a dummy rule to reduce it to a
1515 /* Since the action was written out with this rule's
1516 number, we must give the new rule this number by
1517 inserting the new rule before it. */
1519 /* Make a dummy nonterminal, a gensym. */
1520 bucket
*sdummy
= gensym ();
1522 /* Make a new rule, whose body is empty, before the
1523 current one, so that the action just read can
1527 p
= symbol_list_new (sdummy
);
1528 /* Attach its lineno to that of the host rule. */
1529 p
->line
= crule
->line
;
1534 /* End of the rule. */
1535 crule1
= symbol_list_new (NULL
);
1536 crule1
->next
= crule
;
1540 /* Insert the dummy generated by that rule into this
1543 p
= symbol_list_new (sdummy
);
1550 if (t
== tok_identifier
)
1553 p
= symbol_list_new (symval
);
1557 else /* handle an action. */
1559 copy_action (crule
, rulelength
);
1561 xactions
++; /* JF */
1564 } /* end of read rhs of rule */
1566 /* Put an empty link in the list to mark the end of this rule */
1567 p
= symbol_list_new (NULL
);
1573 complain (_("two @prec's in a row"));
1575 crule
->ruleprec
= symval
;
1580 if (!semantic_parser
)
1581 complain (_("%%guard present but %%semantic_parser not specified"));
1583 copy_guard (crule
, rulelength
);
1586 else if (t
== tok_left_curly
)
1588 /* This case never occurs -wjh */
1590 complain (_("two actions at end of one rule"));
1591 copy_action (crule
, rulelength
);
1593 xactions
++; /* -wjh */
1596 /* If $$ is being set in default way, report if any type
1599 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1601 if (lhs
->type_name
== 0
1602 || first_rhs
->type_name
== 0
1603 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1604 complain (_("type clash (`%s' `%s') on default action"),
1605 lhs
->type_name
? lhs
->type_name
: "",
1606 first_rhs
->type_name
? first_rhs
->type_name
: "");
1608 /* Warn if there is no default for $$ but we need one. */
1609 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1610 complain (_("empty rule for typed nonterminal, and no action"));
1611 if (t
== tok_semicolon
)
1615 /* these things can appear as alternatives to rules. */
1617 a) none of the documentation allows them
1618 b) most of them scan forward until finding a next %
1619 thus they may swallow lots of intervening rules
1621 else if (t
== tok_token
)
1623 parse_token_decl (token_sym
, nterm_sym
);
1626 else if (t
== tok_nterm
)
1628 parse_token_decl (nterm_sym
, token_sym
);
1631 else if (t
== tok_type
)
1635 else if (t
== tok_union
)
1637 parse_union_decl ();
1640 else if (t
== tok_expect
)
1642 parse_expect_decl ();
1645 else if (t
== tok_start
)
1647 parse_start_decl ();
1654 complain (_("invalid input: %s"), quote (token_buffer
));
1659 /* grammar has been read. Do some checking */
1661 if (nsyms
> MAXSHORT
)
1662 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1665 fatal (_("no rules in the input grammar"));
1667 /* Report any undefined symbols and consider them nonterminals. */
1669 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1670 if (bp
->class == unknown_sym
)
1673 ("symbol %s is used, but is not defined as a token and has no rules"),
1675 bp
->class = nterm_sym
;
1676 bp
->value
= nvars
++;
1679 ntokens
= nsyms
- nvars
;
1682 /* At the end of the grammar file, some C source code must
1683 be stored. It is going to be associated to the epilogue
1686 read_additionnal_code (void)
1689 struct obstack el_obstack
;
1691 obstack_init (&el_obstack
);
1693 while ((c
= getc (finput
)) != EOF
)
1694 obstack_1grow (&el_obstack
, c
);
1696 obstack_1grow (&el_obstack
, 0);
1697 muscle_insert ("epilogue", obstack_finish (&el_obstack
));
1701 /*--------------------------------------------------------------.
1702 | For named tokens, but not literal ones, define the name. The |
1703 | value is the user token number. |
1704 `--------------------------------------------------------------*/
1707 output_token_defines (struct obstack
*oout
)
1713 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1715 symbol
= bp
->tag
; /* get symbol */
1717 if (bp
->value
>= ntokens
)
1719 if (bp
->user_token_number
== SALIAS
)
1721 if ('\'' == *symbol
)
1722 continue; /* skip literal character */
1724 continue; /* skip error token */
1725 if ('\"' == *symbol
)
1727 /* use literal string only if given a symbol with an alias */
1729 symbol
= bp
->alias
->tag
;
1734 /* Don't #define nonliteral tokens whose names contain periods. */
1736 while ((c
= *cp
++) && c
!= '.');
1740 obstack_fgrow2 (oout
, "# define\t%s\t%d\n",
1741 symbol
, bp
->user_token_number
);
1742 if (semantic_parser
)
1743 /* FIXME: This is certainly dead wrong, and should be just as
1745 obstack_fgrow2 (oout
, "# define\tT%s\t%d\n", symbol
, bp
->value
);
1750 /*------------------------------------------------------------------.
1751 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
1753 `------------------------------------------------------------------*/
1756 token_translations_init (void)
1761 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1763 /* Initialize all entries for literal tokens to 2, the internal
1764 token number for $undefined., which represents all invalid
1766 for (i
= 0; i
<= max_user_token_number
; i
++)
1767 token_translations
[i
] = 2;
1769 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1772 if (bp
->value
>= ntokens
)
1774 /* A token string alias? */
1775 if (bp
->user_token_number
== SALIAS
)
1778 assert (bp
->user_token_number
!= SUNDEF
);
1780 /* A token which translation has already been set? */
1781 if (token_translations
[bp
->user_token_number
] != 2)
1782 complain (_("tokens %s and %s both assigned number %d"),
1783 tags
[token_translations
[bp
->user_token_number
]],
1784 bp
->tag
, bp
->user_token_number
);
1785 token_translations
[bp
->user_token_number
] = bp
->value
;
1790 /*------------------------------------------------------------------.
1791 | Assign symbol numbers, and write definition of token names into |
1792 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1794 `------------------------------------------------------------------*/
1801 int last_user_token_number
;
1802 static char DOLLAR
[] = "$";
1804 tags
= XCALLOC (char *, nsyms
+ 1);
1805 user_toknums
= XCALLOC (short, nsyms
+ 1);
1807 sprec
= XCALLOC (short, nsyms
);
1808 sassoc
= XCALLOC (short, nsyms
);
1810 /* The EOF token. */
1812 user_toknums
[0] = 0;
1814 max_user_token_number
= 256;
1815 last_user_token_number
= 256;
1817 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1819 if (bp
->class == nterm_sym
)
1821 bp
->value
+= ntokens
;
1825 /* this symbol and its alias are a single token defn.
1826 allocate a tokno, and assign to both check agreement of
1827 ->prec and ->assoc fields and make both the same */
1829 bp
->value
= bp
->alias
->value
= tokno
++;
1831 if (bp
->prec
!= bp
->alias
->prec
)
1833 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1834 && bp
->user_token_number
== SALIAS
)
1835 complain (_("conflicting precedences for %s and %s"),
1836 bp
->tag
, bp
->alias
->tag
);
1838 bp
->alias
->prec
= bp
->prec
;
1840 bp
->prec
= bp
->alias
->prec
;
1843 if (bp
->assoc
!= bp
->alias
->assoc
)
1845 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1846 && bp
->user_token_number
== SALIAS
)
1847 complain (_("conflicting assoc values for %s and %s"),
1848 bp
->tag
, bp
->alias
->tag
);
1850 bp
->alias
->assoc
= bp
->assoc
;
1852 bp
->assoc
= bp
->alias
->assoc
;
1855 if (bp
->user_token_number
== SALIAS
)
1856 continue; /* do not do processing below for SALIASs */
1859 else /* bp->class == token_sym */
1861 bp
->value
= tokno
++;
1864 if (bp
->class == token_sym
)
1866 if (bp
->user_token_number
== SUNDEF
)
1867 bp
->user_token_number
= ++last_user_token_number
;
1868 if (bp
->user_token_number
> max_user_token_number
)
1869 max_user_token_number
= bp
->user_token_number
;
1872 tags
[bp
->value
] = bp
->tag
;
1873 user_toknums
[bp
->value
] = bp
->user_token_number
;
1874 sprec
[bp
->value
] = bp
->prec
;
1875 sassoc
[bp
->value
] = bp
->assoc
;
1878 token_translations_init ();
1880 error_token_number
= errtoken
->value
;
1882 if (startval
->class == unknown_sym
)
1883 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1884 else if (startval
->class == token_sym
)
1885 fatal (_("the start symbol %s is a token"), startval
->tag
);
1887 start_symbol
= startval
->value
;
1891 /*-----------------------------------.
1892 | Output definition of token names. |
1893 `-----------------------------------*/
1896 symbols_output (void)
1899 struct obstack tokendefs
;
1900 obstack_init (&tokendefs
);
1901 output_token_defines (&tokendefs
);
1902 obstack_1grow (&tokendefs
, 0);
1903 muscle_insert ("tokendef", xstrdup (obstack_finish (&tokendefs
)));
1904 obstack_free (&tokendefs
, NULL
);
1908 if (!no_parser_flag
)
1909 output_token_defines (&table_obstack
);
1914 output_token_defines (&defines_obstack
);
1918 if (spec_name_prefix
)
1919 obstack_fgrow1 (&defines_obstack
, "\nextern YYSTYPE %slval;\n",
1922 obstack_sgrow (&defines_obstack
,
1923 "\nextern YYSTYPE yylval;\n");
1926 if (semantic_parser
)
1930 for (i
= ntokens
; i
< nsyms
; i
++)
1932 /* don't make these for dummy nonterminals made by gensym. */
1933 if (*tags
[i
] != '@')
1934 obstack_fgrow2 (&defines_obstack
,
1935 "# define\tNT%s\t%d\n", tags
[i
], i
);
1938 /* `fdefines' is now a temporary file, so we need to copy its
1939 contents in `done', so we can't close it here. */
1948 /*---------------------------------------------------------------.
1949 | Convert the rules into the representation using RRHS, RLHS and |
1951 `---------------------------------------------------------------*/
1960 ritem
= XCALLOC (short, nitems
+ 1);
1961 rule_table
= XCALLOC (rule_t
, nrules
) - 1;
1969 bucket
*ruleprec
= p
->ruleprec
;
1970 rule_table
[ruleno
].lhs
= p
->sym
->value
;
1971 rule_table
[ruleno
].rhs
= itemno
;
1972 rule_table
[ruleno
].line
= p
->line
;
1973 rule_table
[ruleno
].useful
= TRUE
;
1978 ritem
[itemno
++] = p
->sym
->value
;
1979 /* A rule gets by default the precedence and associativity
1980 of the last token in it. */
1981 if (p
->sym
->class == token_sym
)
1983 rule_table
[ruleno
].prec
= p
->sym
->prec
;
1984 rule_table
[ruleno
].assoc
= p
->sym
->assoc
;
1990 /* If this rule has a %prec,
1991 the specified symbol's precedence replaces the default. */
1994 rule_table
[ruleno
].prec
= ruleprec
->prec
;
1995 rule_table
[ruleno
].assoc
= ruleprec
->assoc
;
1996 rule_table
[ruleno
].precsym
= ruleprec
->value
;
1999 ritem
[itemno
++] = -ruleno
;
2009 ritem_print (stderr
);
2012 /*-------------------------------------------------------------------.
2013 | Read in the grammar specification and record it in the format |
2014 | described in gram.h. All guards are copied into the GUARD_OBSTACK |
2015 | and all actions into ACTION_OBSTACK, in each case forming the body |
2016 | of a C function (YYGUARD or YYACTION) which contains a switch |
2017 | statement to decide which guard or action to execute. |
2018 `-------------------------------------------------------------------*/
2024 startval
= NULL
; /* start symbol not specified yet. */
2034 semantic_parser
= 0;
2042 /* Initialize the muscle obstack. */
2043 obstack_init (&muscle_obstack
);
2045 /* Initialize the symbol table. */
2048 /* Construct the error token */
2049 errtoken
= getsym ("error");
2050 errtoken
->class = token_sym
;
2051 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
2053 /* Construct a token that represents all undefined literal tokens.
2054 It is always token number 2. */
2055 undeftoken
= getsym ("$undefined.");
2056 undeftoken
->class = token_sym
;
2057 undeftoken
->user_token_number
= 2;
2059 /* Read the declaration section. Copy %{ ... %} groups to
2060 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
2061 etc. found there. */
2062 read_declarations ();
2063 /* Read in the grammar, build grammar in list form. Write out
2064 guards and actions. */
2066 /* Some C code is given at the end of the grammar file. */
2067 read_additionnal_code ();
2069 /* Now we know whether we need the line-number stack. If we do,
2070 write its type into the .tab.h file.
2071 This is no longer need with header skeleton. */
2073 /* Assign the symbols their symbol numbers. Write #defines for the
2074 token symbols into FDEFINES if requested. */
2077 /* Convert the grammar into the format described in gram.h. */
2079 /* Output the headers. */