]>
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
;
44 /* The action is attached to the LHS of a rule. */
54 static symbol_list
*grammar
;
55 static int start_flag
;
56 static bucket
*startval
;
58 /* Nonzero if components of semantic values are used, implying
59 they must be unions. */
60 static int value_components_used
;
62 /* Nonzero if %union has been seen. */
65 /* Incremented for each %left, %right or %nonassoc seen */
68 static bucket
*errtoken
;
69 static bucket
*undeftoken
;
73 symbol_list_new (bucket
*sym
)
75 symbol_list
*res
= XMALLOC (symbol_list
, 1);
85 /*===================\
87 \===================*/
90 skip_to_char (int target
)
94 complain (_(" Skipping to next \\n"));
96 complain (_(" Skipping to next %c"), target
);
99 c
= skip_white_space ();
100 while (c
!= target
&& c
!= EOF
);
106 /*---------------------------------------------------------.
107 | Read a signed integer from STREAM and return its value. |
108 `---------------------------------------------------------*/
111 read_signed_integer (FILE *stream
)
113 int c
= getc (stream
);
125 n
= 10 * n
+ (c
- '0');
134 /*--------------------------------------------------------------.
135 | Get the data type (alternative in the union) of the value for |
136 | symbol N in rule RULE. |
137 `--------------------------------------------------------------*/
140 get_type_name (int n
, symbol_list
*rule
)
147 complain (_("invalid $ value"));
157 if (rp
== NULL
|| rp
->sym
== NULL
)
159 complain (_("invalid $ value"));
165 return rp
->sym
->type_name
;
168 /*------------------------------------------------------------.
169 | Dump the string from FIN to OOUT if non null. MATCH is the |
170 | delimiter of the string (either ' or "). |
171 `------------------------------------------------------------*/
174 copy_string2 (FILE *fin
, struct obstack
*oout
, int match
, int store
)
179 obstack_1grow (oout
, match
);
186 fatal (_("unterminated string at end of file"));
189 complain (_("unterminated string"));
191 c
= match
; /* invent terminator */
195 obstack_1grow (oout
, c
);
201 fatal (_("unterminated string at end of file"));
202 obstack_1grow (oout
, c
);
212 obstack_1grow (oout
, c
);
218 copy_string (FILE *fin
, struct obstack
*oout
, int match
)
220 copy_string2 (fin
, oout
, match
, 1);
226 copy_identifier (FILE *fin
, struct obstack
*oout
)
230 while (isalnum (c
= getc (fin
)) || c
== '_')
231 obstack_1grow (oout
, c
);
236 /*-----------------------------------------------------------------.
237 | Dump the wannabee comment from IN to OUT1 and OUT2 (which can be |
238 | NULL). In fact we just saw a `/', which might or might not be a |
239 | comment. In any case, copy what we saw. |
241 | OUT2 might be NULL. |
242 `-----------------------------------------------------------------*/
245 copy_comment2 (FILE *fin
, struct obstack
*oout1
, struct obstack
*oout2
)
251 /* We read a `/', output it. */
252 obstack_1grow (oout1
, '/');
254 obstack_1grow (oout2
, '/');
256 switch ((c
= getc (fin
)))
269 obstack_1grow (oout1
, c
);
271 obstack_1grow (oout2
, c
);
277 if (!cplus_comment
&& c
== '*')
281 obstack_1grow (oout1
, c
);
283 obstack_1grow (oout2
, c
);
289 obstack_1grow (oout1
, c
);
291 obstack_1grow (oout2
, c
);
298 obstack_1grow (oout1
, c
);
300 obstack_1grow (oout2
, c
);
307 fatal (_("unterminated comment"));
310 obstack_1grow (oout1
, c
);
312 obstack_1grow (oout2
, c
);
319 /*-------------------------------------------------------------------.
320 | Dump the comment (actually the current string starting with a `/') |
321 | from FIN to OOUT. |
322 `-------------------------------------------------------------------*/
325 copy_comment (FILE *fin
, struct obstack
*oout
)
327 copy_comment2 (fin
, oout
, NULL
);
331 /*-----------------------------------------------------------------.
332 | FIN is pointing to a location (i.e., a `@'). Output to OOUT a |
333 | reference to this location. STACK_OFFSET is the number of values |
334 | in the current rule so far, which says where to find `$0' with |
335 | respect to the top of the stack. |
336 `-----------------------------------------------------------------*/
339 copy_at (FILE *fin
, struct obstack
*oout
, int stack_offset
)
346 obstack_sgrow (oout
, "yyloc");
349 else if (isdigit (c
) || c
== '-')
354 n
= read_signed_integer (fin
);
356 obstack_fgrow1 (oout
, "yylsp[%d]", n
- stack_offset
);
363 complain (_("%s is invalid"), quote (buf
));
368 /*-------------------------------------------------------------------.
369 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
371 | Possible inputs: $[<TYPENAME>]($|integer) |
373 | Output to OOUT a reference to this semantic value. STACK_OFFSET is |
374 | the number of values in the current rule so far, which says where |
375 | to find `$0' with respect to the top of the stack. |
376 `-------------------------------------------------------------------*/
379 copy_dollar (FILE *fin
, struct obstack
*oout
,
380 symbol_list
*rule
, int stack_offset
)
383 const char *type_name
= NULL
;
385 /* Get the type name if explicit. */
388 read_type_name (fin
);
389 type_name
= token_buffer
;
390 value_components_used
= 1;
396 obstack_sgrow (oout
, "yyval");
399 type_name
= get_type_name (0, rule
);
401 obstack_fgrow1 (oout
, ".%s", type_name
);
402 if (!type_name
&& typed
)
403 complain (_("$$ of `%s' has no declared type"),
406 else if (isdigit (c
) || c
== '-')
410 n
= read_signed_integer (fin
);
412 if (!type_name
&& n
> 0)
413 type_name
= get_type_name (n
, rule
);
415 obstack_fgrow1 (oout
, "yyvsp[%d]", n
- stack_offset
);
418 obstack_fgrow1 (oout
, ".%s", type_name
);
419 if (!type_name
&& typed
)
420 complain (_("$%d of `%s' has no declared type"),
427 complain (_("%s is invalid"), quote (buf
));
431 /*-------------------------------------------------------------------.
432 | Copy the contents of a `%{ ... %}' into the definitions file. The |
433 | `%{' has already been read. Return after reading the `%}'. |
434 `-------------------------------------------------------------------*/
437 copy_definition (void)
440 /* -1 while reading a character if prev char was %. */
445 obstack_fgrow2 (&attrs_obstack
, muscle_find ("linef"),
446 lineno
, quotearg_style (c_quoting_style
,
447 muscle_find("filename")));
459 obstack_1grow (&attrs_obstack
, c
);
469 copy_string (finput
, &attrs_obstack
, c
);
473 copy_comment (finput
, &attrs_obstack
);
477 fatal ("%s", _("unterminated `%{' definition"));
480 obstack_1grow (&attrs_obstack
, c
);
489 obstack_1grow (&attrs_obstack
, '%');
496 /*-------------------------------------------------------------------.
497 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
498 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
500 `-------------------------------------------------------------------*/
503 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
505 token_t token
= tok_undef
;
506 char *typename
= NULL
;
508 /* The symbol being defined. */
509 struct bucket
*symbol
= NULL
;
511 /* After `%token' and `%nterm', any number of symbols maybe be
515 int tmp_char
= ungetc (skip_white_space (), finput
);
517 /* `%' (for instance from `%token', or from `%%' etc.) is the
518 only valid means to end this declaration. */
522 fatal (_("Premature EOF after %s"), token_buffer
);
525 if (token
== tok_comma
)
530 if (token
== tok_typename
)
532 typename
= xstrdup (token_buffer
);
533 value_components_used
= 1;
536 else if (token
== tok_identifier
&& *symval
->tag
== '\"' && symbol
)
539 warn (_("symbol `%s' used more than once as a literal string"),
541 else if (symbol
->alias
)
542 warn (_("symbol `%s' given more than one literal string"),
546 symval
->class = token_sym
;
547 symval
->type_name
= typename
;
548 symval
->user_token_number
= symbol
->user_token_number
;
549 symbol
->user_token_number
= SALIAS
;
550 symval
->alias
= symbol
;
551 symbol
->alias
= symval
;
552 /* symbol and symval combined are only one symbol */
557 else if (token
== tok_identifier
)
559 int oldclass
= symval
->class;
562 if (symbol
->class == what_is_not
)
563 complain (_("symbol %s redefined"), symbol
->tag
);
564 symbol
->class = what_is
;
565 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
566 symbol
->value
= nvars
++;
570 if (symbol
->type_name
== NULL
)
571 symbol
->type_name
= typename
;
572 else if (strcmp (typename
, symbol
->type_name
) != 0)
573 complain (_("type redeclaration for %s"), symbol
->tag
);
576 else if (symbol
&& token
== tok_number
)
578 symbol
->user_token_number
= numval
;
582 complain (_("`%s' is invalid in %s"),
584 (what_is
== token_sym
) ? "%token" : "%nterm");
592 /*------------------------------.
593 | Parse what comes after %start |
594 `------------------------------*/
597 parse_start_decl (void)
600 complain (_("multiple %s declarations"), "%start");
601 if (lex () != tok_identifier
)
602 complain (_("invalid %s declaration"), "%start");
610 /*-----------------------------------------------------------.
611 | read in a %type declaration and record its information for |
612 | get_type_name to access |
613 `-----------------------------------------------------------*/
616 parse_type_decl (void)
620 if (lex () != tok_typename
)
622 complain ("%s", _("%type declaration has no <typename>"));
627 name
= xstrdup (token_buffer
);
632 int tmp_char
= ungetc (skip_white_space (), finput
);
637 fatal (_("Premature EOF after %s"), token_buffer
);
649 if (symval
->type_name
== NULL
)
650 symval
->type_name
= name
;
651 else if (strcmp (name
, symval
->type_name
) != 0)
652 complain (_("type redeclaration for %s"), symval
->tag
);
657 complain (_("invalid %%type declaration due to item: %s"),
666 /*----------------------------------------------------------------.
667 | Read in a %left, %right or %nonassoc declaration and record its |
669 `----------------------------------------------------------------*/
672 parse_assoc_decl (associativity assoc
)
677 lastprec
++; /* Assign a new precedence level, never 0. */
682 int tmp_char
= ungetc (skip_white_space (), finput
);
687 fatal (_("Premature EOF after %s"), token_buffer
);
694 name
= xstrdup (token_buffer
);
701 if (symval
->prec
!= 0)
702 complain (_("redefining precedence of %s"), symval
->tag
);
703 symval
->prec
= lastprec
;
704 symval
->assoc
= assoc
;
705 if (symval
->class == nterm_sym
)
706 complain (_("symbol %s redefined"), symval
->tag
);
707 symval
->class = token_sym
;
709 { /* record the type, if one is specified */
710 if (symval
->type_name
== NULL
)
711 symval
->type_name
= name
;
712 else if (strcmp (name
, symval
->type_name
) != 0)
713 complain (_("type redeclaration for %s"), symval
->tag
);
718 if (prev
== tok_identifier
)
720 symval
->user_token_number
= numval
;
725 ("invalid text (%s) - number should be after identifier"),
735 complain (_("unexpected item: %s"), token_buffer
);
745 /*--------------------------------------------------------------.
746 | Copy the union declaration into the stype muscle |
747 | (and fdefines), where it is made into the definition of |
748 | YYSTYPE, the type of elements of the parser value stack. |
749 `--------------------------------------------------------------*/
752 parse_union_decl (void)
756 struct obstack union_obstack
;
757 const char *prologue
= "\
760 const char *epilogue
= "\
762 # define YYSTYPE yystype\n\
766 complain (_("multiple %s declarations"), "%union");
770 /* FIXME: I'm worried: are you sure attrs_obstack is properly
772 /* I don't see any reasons to keep this line, because we should
773 create a special skeleton for this option. */
775 obstack_1grow (&attrs_obstack
, '\n');
777 obstack_init (&union_obstack
);
778 obstack_sgrow (&union_obstack
, "union");
780 obstack_sgrow (&defines_obstack
, prologue
);
786 /* If C contains '/', it is output by copy_comment (). */
789 obstack_1grow (&union_obstack
, c
);
791 obstack_1grow (&defines_obstack
, c
);
801 copy_comment2 (finput
, &defines_obstack
, &union_obstack
);
810 complain (_("unmatched %s"), "`}'");
815 obstack_sgrow (&defines_obstack
, epilogue
);
816 /* JF don't choke on trailing semi */
817 c
= skip_white_space ();
820 obstack_1grow (&union_obstack
, 0);
821 muscle_insert ("stype", obstack_finish (&union_obstack
));
832 /*-------------------------------------------------------.
833 | Parse the declaration %expect N which says to expect N |
834 | shift-reduce conflicts. |
835 `-------------------------------------------------------*/
838 parse_expect_decl (void)
840 int c
= skip_white_space ();
844 complain (_("argument of %%expect is not an integer"));
846 expected_conflicts
= read_signed_integer (finput
);
850 /*-------------------------------------------------------------------.
851 | Parse what comes after %thong. the full syntax is |
853 | %thong <type> token number literal |
855 | the <type> or number may be omitted. The number specifies the |
856 | user_token_number. |
858 | Two symbols are entered in the table, one for the token symbol and |
859 | one for the literal. Both are given the <type>, if any, from the |
860 | declaration. The ->user_token_number of the first is SALIAS and |
861 | the ->user_token_number of the second is set to the number, if |
862 | any, from the declaration. The two symbols are linked via |
863 | pointers in their ->alias fields. |
865 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
866 | only the literal string is retained it is the literal string that |
867 | is output to yytname |
868 `-------------------------------------------------------------------*/
871 parse_thong_decl (void)
874 struct bucket
*symbol
;
876 int usrtoknum
= SUNDEF
;
878 token
= lex (); /* fetch typename or first token */
879 if (token
== tok_typename
)
881 typename
= xstrdup (token_buffer
);
882 value_components_used
= 1;
883 token
= lex (); /* fetch first token */
886 /* process first token */
888 if (token
!= tok_identifier
)
890 complain (_("unrecognized item %s, expected an identifier"),
895 symval
->class = token_sym
;
896 symval
->type_name
= typename
;
897 symval
->user_token_number
= SALIAS
;
900 token
= lex (); /* get number or literal string */
902 if (token
== tok_number
)
905 token
= lex (); /* okay, did number, now get literal */
908 /* process literal string token */
910 if (token
!= tok_identifier
|| *symval
->tag
!= '\"')
912 complain (_("expected string constant instead of %s"), token_buffer
);
916 symval
->class = token_sym
;
917 symval
->type_name
= typename
;
918 symval
->user_token_number
= usrtoknum
;
920 symval
->alias
= symbol
;
921 symbol
->alias
= symval
;
923 /* symbol and symval combined are only one symbol. */
928 parse_muscle_decl (void)
930 int ch
= ungetc (skip_white_space (), finput
);
935 if (!isalpha (ch
) && ch
!= '_')
937 complain (_("invalid %s declaration"), "%define");
941 copy_identifier (finput
, &muscle_obstack
);
942 obstack_1grow (&muscle_obstack
, 0);
943 muscle_key
= obstack_finish (&muscle_obstack
);
946 ch
= skip_white_space ();
952 complain (_("invalid %s declaration"), "%define");
957 fatal (_("Premature EOF after %s"), "\"");
959 copy_string2 (finput
, &muscle_obstack
, '"', 0);
960 obstack_1grow (&muscle_obstack
, 0);
961 muscle_value
= obstack_finish (&muscle_obstack
);
963 /* Store the (key, value) pair in the environment. */
964 muscle_insert (muscle_key
, muscle_value
);
969 /*---------------------------------.
970 | Parse a double quoted parameter. |
971 `---------------------------------*/
974 parse_dquoted_param (const char *from
)
976 struct obstack param_obstack
;
977 const char *param
= NULL
;
980 obstack_init (¶m_obstack
);
981 c
= skip_white_space ();
985 complain (_("invalid %s declaration"), from
);
991 while ((c
= literalchar ()) != '"')
992 obstack_1grow (¶m_obstack
, c
);
994 obstack_1grow (¶m_obstack
, '\0');
995 param
= obstack_finish (¶m_obstack
);
997 if (c
!= '"' || strlen (param
) == 0)
999 complain (_("invalid %s declaration"), from
);
1009 /*----------------------------------.
1010 | Parse what comes after %skeleton. |
1011 `----------------------------------*/
1014 parse_skel_decl (void)
1016 skeleton
= parse_dquoted_param ("%skeleton");
1019 /*----------------------------------------------------------------.
1020 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
1021 | any `%' declarations, and copy the contents of any `%{ ... %}' |
1022 | groups to ATTRS_OBSTACK. |
1023 `----------------------------------------------------------------*/
1026 read_declarations (void)
1030 int c
= skip_white_space ();
1034 token_t tok
= parse_percent_token ();
1038 case tok_two_percents
:
1041 case tok_percent_left_curly
:
1046 parse_token_decl (token_sym
, nterm_sym
);
1050 parse_token_decl (nterm_sym
, token_sym
);
1058 parse_start_decl ();
1062 parse_union_decl ();
1066 parse_expect_decl ();
1070 parse_thong_decl ();
1074 parse_assoc_decl (left_assoc
);
1078 parse_assoc_decl (right_assoc
);
1082 parse_assoc_decl (non_assoc
);
1086 parse_muscle_decl ();
1104 complain (_("unrecognized: %s"), token_buffer
);
1109 fatal (_("no input grammar"));
1114 complain (_("unknown character: %s"), quote (buf
));
1120 /*-------------------------------------------------------------------.
1121 | Assuming that a `{' has just been seen, copy everything up to the |
1122 | matching `}' into the actions file. STACK_OFFSET is the number of |
1123 | values in the current rule so far, which says where to find `$0' |
1124 | with respect to the top of the stack. |
1125 `-------------------------------------------------------------------*/
1128 copy_action (symbol_list
*rule
, int stack_offset
)
1133 /* offset is always 0 if parser has already popped the stack pointer */
1134 if (semantic_parser
)
1147 obstack_1grow (&action_obstack
, c
);
1152 obstack_1grow (&action_obstack
, c
);
1158 copy_string (finput
, &action_obstack
, c
);
1162 copy_comment (finput
, &action_obstack
);
1166 copy_dollar (finput
, &action_obstack
,
1167 rule
, stack_offset
);
1171 copy_at (finput
, &action_obstack
,
1176 fatal (_("unmatched %s"), "`{'");
1179 obstack_1grow (&action_obstack
, c
);
1185 /* above loop exits when c is '}' */
1189 obstack_1grow (&action_obstack
, c
);
1194 obstack_1grow (&action_obstack
, '\0');
1195 rule
->action
= obstack_finish (&action_obstack
);
1196 rule
->action_line
= lineno
;
1199 /*-------------------------------------------------------------------.
1200 | After `%guard' is seen in the input file, copy the actual guard |
1201 | into the guards file. If the guard is followed by an action, copy |
1202 | that into the actions file. STACK_OFFSET is the number of values |
1203 | in the current rule so far, which says where to find `$0' with |
1204 | respect to the top of the stack, for the simple parser in which |
1205 | the stack is not popped until after the guard is run. |
1206 `-------------------------------------------------------------------*/
1209 copy_guard (symbol_list
*rule
, int stack_offset
)
1215 /* offset is always 0 if parser has already popped the stack pointer */
1216 if (semantic_parser
)
1219 obstack_fgrow1 (&guard_obstack
, "\ncase %d:\n", nrules
);
1221 obstack_fgrow2 (&guard_obstack
, muscle_find ("linef"),
1222 lineno
, quotearg_style (c_quoting_style
,
1223 muscle_find ("filename")));
1224 obstack_1grow (&guard_obstack
, '{');
1229 while (brace_flag
? (count
> 0) : (c
!= ';'))
1234 obstack_1grow (&guard_obstack
, c
);
1239 obstack_1grow (&guard_obstack
, c
);
1245 obstack_1grow (&guard_obstack
, c
);
1250 complain (_("unmatched %s"), "`}'");
1251 c
= getc (finput
); /* skip it */
1257 copy_string (finput
, &guard_obstack
, c
);
1261 copy_comment (finput
, &guard_obstack
);
1265 copy_dollar (finput
, &guard_obstack
, rule
, stack_offset
);
1269 copy_at (finput
, &guard_obstack
, stack_offset
);
1273 fatal ("%s", _("unterminated %guard clause"));
1276 obstack_1grow (&guard_obstack
, c
);
1279 if (c
!= '}' || count
!= 0)
1283 c
= skip_white_space ();
1285 obstack_sgrow (&guard_obstack
, ";\n break;}");
1287 copy_action (rule
, stack_offset
);
1290 c
= getc (finput
); /* why not skip_white_space -wjh */
1292 copy_action (rule
, stack_offset
);
1299 /*-------------------------------------------------------------------.
1300 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1301 | with the user's names. |
1302 `-------------------------------------------------------------------*/
1307 /* Incremented for each generated symbol */
1308 static int gensym_count
= 0;
1309 static char buf
[256];
1313 sprintf (buf
, "@%d", ++gensym_count
);
1315 sym
= getsym (token_buffer
);
1316 sym
->class = nterm_sym
;
1317 sym
->value
= nvars
++;
1321 /*-------------------------------------------------------------------.
1322 | Parse the input grammar into a one symbol_list structure. Each |
1323 | rule is represented by a sequence of symbols: the left hand side |
1324 | followed by the contents of the right hand side, followed by a |
1325 | null pointer instead of a symbol to terminate the rule. The next |
1326 | symbol is the lhs of the following rule. |
1328 | All guards and actions are copied out to the appropriate files, |
1329 | labelled by the rule number they apply to. |
1331 | Bison used to allow some %directives in the rules sections, but |
1332 | this is no longer consider appropriate: (i) the documented grammar |
1333 | doesn't claim it, (ii), it would promote bad style, (iii), error |
1334 | recovery for %directives consists in skipping the junk until a `%' |
1335 | is seen and helrp synchronizing. This scheme is definitely wrong |
1336 | in the rules section. |
1337 `-------------------------------------------------------------------*/
1344 symbol_list
*p
= NULL
;
1345 symbol_list
*p1
= NULL
;
1348 /* Points to first symbol_list of current rule. its symbol is the
1350 symbol_list
*crule
= NULL
;
1351 /* Points to the symbol_list preceding crule. */
1352 symbol_list
*crule1
= NULL
;
1356 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1357 if (t
== tok_identifier
|| t
== tok_bar
)
1359 int action_flag
= 0;
1360 /* Number of symbols in rhs of this rule so far */
1362 int xactions
= 0; /* JF for error checking */
1363 bucket
*first_rhs
= 0;
1365 if (t
== tok_identifier
)
1378 complain (_("ill-formed rule: initial symbol not followed by colon"));
1383 if (nrules
== 0 && t
== tok_bar
)
1385 complain (_("grammar starts with vertical bar"));
1386 lhs
= symval
; /* BOGUS: use a random symval */
1388 /* start a new rule and record its lhs. */
1393 p
= symbol_list_new (lhs
);
1404 /* mark the rule's lhs as a nonterminal if not already so. */
1406 if (lhs
->class == unknown_sym
)
1408 lhs
->class = nterm_sym
;
1412 else if (lhs
->class == token_sym
)
1413 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1415 /* read the rhs of the rule. */
1423 crule
->ruleprec
= symval
;
1427 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1430 /* If next token is an identifier, see if a colon follows it.
1431 If one does, exit this rule now. */
1432 if (t
== tok_identifier
)
1441 if (t1
== tok_colon
)
1444 if (!first_rhs
) /* JF */
1446 /* Not followed by colon =>
1447 process as part of this rule's rhs. */
1450 /* If we just passed an action, that action was in the middle
1451 of a rule, so make a dummy rule to reduce it to a
1455 /* Since the action was written out with this rule's
1456 number, we must give the new rule this number by
1457 inserting the new rule before it. */
1459 /* Make a dummy nonterminal, a gensym. */
1460 bucket
*sdummy
= gensym ();
1462 /* Make a new rule, whose body is empty, before the
1463 current one, so that the action just read can
1467 p
= symbol_list_new (sdummy
);
1468 /* Attach its lineno to that of the host rule. */
1469 p
->line
= crule
->line
;
1474 /* End of the rule. */
1475 crule1
= symbol_list_new (NULL
);
1476 crule1
->next
= crule
;
1480 /* Insert the dummy generated by that rule into this
1483 p
= symbol_list_new (sdummy
);
1490 if (t
== tok_identifier
)
1493 p
= symbol_list_new (symval
);
1497 else /* handle an action. */
1499 copy_action (crule
, rulelength
);
1501 xactions
++; /* JF */
1504 } /* end of read rhs of rule */
1506 /* Put an empty link in the list to mark the end of this rule */
1507 p
= symbol_list_new (NULL
);
1513 complain (_("two @prec's in a row"));
1515 crule
->ruleprec
= symval
;
1520 if (!semantic_parser
)
1521 complain (_("%%guard present but %%semantic_parser not specified"));
1523 copy_guard (crule
, rulelength
);
1526 else if (t
== tok_left_curly
)
1528 /* This case never occurs -wjh */
1530 complain (_("two actions at end of one rule"));
1531 copy_action (crule
, rulelength
);
1533 xactions
++; /* -wjh */
1536 /* If $$ is being set in default way, report if any type
1539 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1541 if (lhs
->type_name
== 0
1542 || first_rhs
->type_name
== 0
1543 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1544 complain (_("type clash (`%s' `%s') on default action"),
1545 lhs
->type_name
? lhs
->type_name
: "",
1546 first_rhs
->type_name
? first_rhs
->type_name
: "");
1548 /* Warn if there is no default for $$ but we need one. */
1549 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1550 complain (_("empty rule for typed nonterminal, and no action"));
1551 if (t
== tok_semicolon
)
1556 complain (_("invalid input: %s"), quote (token_buffer
));
1561 /* grammar has been read. Do some checking */
1563 if (nsyms
> MAXSHORT
)
1564 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1567 fatal (_("no rules in the input grammar"));
1569 /* Report any undefined symbols and consider them nonterminals. */
1571 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1572 if (bp
->class == unknown_sym
)
1575 ("symbol %s is used, but is not defined as a token and has no rules"),
1577 bp
->class = nterm_sym
;
1578 bp
->value
= nvars
++;
1581 ntokens
= nsyms
- nvars
;
1584 /* At the end of the grammar file, some C source code must
1585 be stored. It is going to be associated to the epilogue
1588 read_additionnal_code (void)
1591 struct obstack el_obstack
;
1593 obstack_init (&el_obstack
);
1597 obstack_fgrow2 (&el_obstack
, muscle_find ("linef"),
1598 lineno
, quotearg_style (c_quoting_style
,
1599 muscle_find("filename")));
1602 while ((c
= getc (finput
)) != EOF
)
1603 obstack_1grow (&el_obstack
, c
);
1605 obstack_1grow (&el_obstack
, 0);
1606 muscle_insert ("epilogue", obstack_finish (&el_obstack
));
1610 /*--------------------------------------------------------------.
1611 | For named tokens, but not literal ones, define the name. The |
1612 | value is the user token number. |
1613 `--------------------------------------------------------------*/
1616 output_token_defines (struct obstack
*oout
)
1622 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1624 symbol
= bp
->tag
; /* get symbol */
1626 if (bp
->value
>= ntokens
)
1628 if (bp
->user_token_number
== SALIAS
)
1630 if ('\'' == *symbol
)
1631 continue; /* skip literal character */
1633 continue; /* skip error token */
1634 if ('\"' == *symbol
)
1636 /* use literal string only if given a symbol with an alias */
1638 symbol
= bp
->alias
->tag
;
1643 /* Don't #define nonliteral tokens whose names contain periods. */
1645 while ((c
= *cp
++) && c
!= '.');
1649 obstack_fgrow2 (oout
, "# define\t%s\t%d\n",
1650 symbol
, bp
->user_token_number
);
1651 if (semantic_parser
)
1652 /* FIXME: This is certainly dead wrong, and should be just as
1654 obstack_fgrow2 (oout
, "# define\tT%s\t%d\n", symbol
, bp
->value
);
1659 /*------------------------------------------------------------------.
1660 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
1662 `------------------------------------------------------------------*/
1665 token_translations_init (void)
1670 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1672 /* Initialize all entries for literal tokens to 2, the internal
1673 token number for $undefined., which represents all invalid
1675 for (i
= 0; i
<= max_user_token_number
; i
++)
1676 token_translations
[i
] = 2;
1678 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1681 if (bp
->value
>= ntokens
)
1683 /* A token string alias? */
1684 if (bp
->user_token_number
== SALIAS
)
1687 assert (bp
->user_token_number
!= SUNDEF
);
1689 /* A token which translation has already been set? */
1690 if (token_translations
[bp
->user_token_number
] != 2)
1691 complain (_("tokens %s and %s both assigned number %d"),
1692 tags
[token_translations
[bp
->user_token_number
]],
1693 bp
->tag
, bp
->user_token_number
);
1694 token_translations
[bp
->user_token_number
] = bp
->value
;
1699 /*------------------------------------------------------------------.
1700 | Assign symbol numbers, and write definition of token names into |
1701 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1703 `------------------------------------------------------------------*/
1710 int last_user_token_number
;
1711 static char DOLLAR
[] = "$";
1713 tags
= XCALLOC (char *, nsyms
+ 1);
1714 user_toknums
= XCALLOC (short, nsyms
+ 1);
1716 sprec
= XCALLOC (short, nsyms
);
1717 sassoc
= XCALLOC (short, nsyms
);
1719 /* The EOF token. */
1721 user_toknums
[0] = 0;
1723 max_user_token_number
= 256;
1724 last_user_token_number
= 256;
1726 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1728 if (bp
->class == nterm_sym
)
1730 bp
->value
+= ntokens
;
1734 /* this symbol and its alias are a single token defn.
1735 allocate a tokno, and assign to both check agreement of
1736 ->prec and ->assoc fields and make both the same */
1738 bp
->value
= bp
->alias
->value
= tokno
++;
1740 if (bp
->prec
!= bp
->alias
->prec
)
1742 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1743 && bp
->user_token_number
== SALIAS
)
1744 complain (_("conflicting precedences for %s and %s"),
1745 bp
->tag
, bp
->alias
->tag
);
1747 bp
->alias
->prec
= bp
->prec
;
1749 bp
->prec
= bp
->alias
->prec
;
1752 if (bp
->assoc
!= bp
->alias
->assoc
)
1754 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1755 && bp
->user_token_number
== SALIAS
)
1756 complain (_("conflicting assoc values for %s and %s"),
1757 bp
->tag
, bp
->alias
->tag
);
1759 bp
->alias
->assoc
= bp
->assoc
;
1761 bp
->assoc
= bp
->alias
->assoc
;
1764 if (bp
->user_token_number
== SALIAS
)
1765 continue; /* do not do processing below for SALIASs */
1768 else /* bp->class == token_sym */
1770 bp
->value
= tokno
++;
1773 if (bp
->class == token_sym
)
1775 if (bp
->user_token_number
== SUNDEF
)
1776 bp
->user_token_number
= ++last_user_token_number
;
1777 if (bp
->user_token_number
> max_user_token_number
)
1778 max_user_token_number
= bp
->user_token_number
;
1781 tags
[bp
->value
] = bp
->tag
;
1782 user_toknums
[bp
->value
] = bp
->user_token_number
;
1783 sprec
[bp
->value
] = bp
->prec
;
1784 sassoc
[bp
->value
] = bp
->assoc
;
1787 token_translations_init ();
1789 error_token_number
= errtoken
->value
;
1791 if (startval
->class == unknown_sym
)
1792 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1793 else if (startval
->class == token_sym
)
1794 fatal (_("the start symbol %s is a token"), startval
->tag
);
1796 start_symbol
= startval
->value
;
1800 /*-----------------------------------.
1801 | Output definition of token names. |
1802 `-----------------------------------*/
1805 symbols_output (void)
1808 struct obstack tokendefs
;
1809 obstack_init (&tokendefs
);
1810 output_token_defines (&tokendefs
);
1811 obstack_1grow (&tokendefs
, 0);
1812 muscle_insert ("tokendef", xstrdup (obstack_finish (&tokendefs
)));
1813 obstack_free (&tokendefs
, NULL
);
1818 output_token_defines (&defines_obstack
);
1821 obstack_fgrow1 (&defines_obstack
, "\nextern YYSTYPE %slval;\n",
1823 if (semantic_parser
)
1827 for (i
= ntokens
; i
< nsyms
; i
++)
1829 /* don't make these for dummy nonterminals made by gensym. */
1830 if (*tags
[i
] != '@')
1831 obstack_fgrow2 (&defines_obstack
,
1832 "# define\tNT%s\t%d\n", tags
[i
], i
);
1835 /* `fdefines' is now a temporary file, so we need to copy its
1836 contents in `done', so we can't close it here. */
1845 /*---------------------------------------------------------------.
1846 | Convert the rules into the representation using RRHS, RLHS and |
1848 `---------------------------------------------------------------*/
1857 ritem
= XCALLOC (short, nitems
+ 1);
1858 rule_table
= XCALLOC (rule_t
, nrules
) - 1;
1866 bucket
*ruleprec
= p
->ruleprec
;
1867 rule_table
[ruleno
].lhs
= p
->sym
->value
;
1868 rule_table
[ruleno
].rhs
= itemno
;
1869 rule_table
[ruleno
].line
= p
->line
;
1870 rule_table
[ruleno
].useful
= TRUE
;
1871 rule_table
[ruleno
].action
= p
->action
;
1872 rule_table
[ruleno
].action_line
= p
->action_line
;
1877 ritem
[itemno
++] = p
->sym
->value
;
1878 /* A rule gets by default the precedence and associativity
1879 of the last token in it. */
1880 if (p
->sym
->class == token_sym
)
1882 rule_table
[ruleno
].prec
= p
->sym
->prec
;
1883 rule_table
[ruleno
].assoc
= p
->sym
->assoc
;
1889 /* If this rule has a %prec,
1890 the specified symbol's precedence replaces the default. */
1893 rule_table
[ruleno
].prec
= ruleprec
->prec
;
1894 rule_table
[ruleno
].assoc
= ruleprec
->assoc
;
1895 rule_table
[ruleno
].precsym
= ruleprec
->value
;
1898 ritem
[itemno
++] = -ruleno
;
1908 ritem_print (stderr
);
1911 /*-------------------------------------------------------------------.
1912 | Read in the grammar specification and record it in the format |
1913 | described in gram.h. All guards are copied into the GUARD_OBSTACK |
1914 | and all actions into ACTION_OBSTACK, in each case forming the body |
1915 | of a C function (YYGUARD or YYACTION) which contains a switch |
1916 | statement to decide which guard or action to execute. |
1917 `-------------------------------------------------------------------*/
1923 startval
= NULL
; /* start symbol not specified yet. */
1933 semantic_parser
= 0;
1941 /* Initialize the muscle obstack. */
1942 obstack_init (&muscle_obstack
);
1944 /* Initialize the symbol table. */
1947 /* Construct the error token */
1948 errtoken
= getsym ("error");
1949 errtoken
->class = token_sym
;
1950 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1952 /* Construct a token that represents all undefined literal tokens.
1953 It is always token number 2. */
1954 undeftoken
= getsym ("$undefined.");
1955 undeftoken
->class = token_sym
;
1956 undeftoken
->user_token_number
= 2;
1958 /* Read the declaration section. Copy %{ ... %} groups to
1959 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1960 etc. found there. */
1961 read_declarations ();
1962 /* Read in the grammar, build grammar in list form. Write out
1963 guards and actions. */
1965 /* Some C code is given at the end of the grammar file. */
1966 read_additionnal_code ();
1968 /* Now we know whether we need the line-number stack. If we do,
1969 write its type into the .tab.h file.
1970 This is no longer need with header skeleton. */
1972 /* Assign the symbols their symbol numbers. Write #defines for the
1973 token symbols into FDEFINES if requested. */
1975 /* Convert the grammar into the format described in gram.h. */
1977 /* Output the headers. */