]>
git.saurik.com Git - bison.git/blob - src/reader.c
d7d6e86db16482b9d45f314c34d99c1793be4d34
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. */
35 #include "conflicts.h"
36 #include "muscle_tab.h"
38 typedef struct symbol_list
40 struct symbol_list
*next
;
43 /* The action is attached to the LHS of a rule. */
52 static symbol_list
*grammar
;
53 static int start_flag
;
54 static bucket
*startval
;
56 /* Nonzero if components of semantic values are used, implying
57 they must be unions. */
58 static int value_components_used
;
60 /* Nonzero if %union has been seen. */
63 /* Incremented for each %left, %right or %nonassoc seen */
66 static bucket
*errtoken
;
67 static bucket
*undeftoken
;
71 symbol_list_new (bucket
*sym
)
73 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
);
237 /*------------------------------------------------------------------.
238 | Dump the wannabee comment from IN to OOUT. In fact we just saw a |
239 | `/', which might or might not be a comment. In any case, copy |
241 `------------------------------------------------------------------*/
244 copy_comment (FILE *fin
, struct obstack
*oout
)
250 /* We read a `/', output it. */
251 obstack_1grow (oout
, '/');
253 switch ((c
= getc (fin
)))
266 obstack_1grow (oout
, c
);
272 if (!cplus_comment
&& c
== '*')
276 obstack_1grow (oout
, c
);
282 obstack_1grow (oout
, c
);
289 obstack_1grow (oout
, c
);
296 fatal (_("unterminated comment"));
299 obstack_1grow (oout
, c
);
306 /*-----------------------------------------------------------------.
307 | FIN is pointing to a location (i.e., a `@'). Output to OOUT a |
308 | reference to this location. STACK_OFFSET is the number of values |
309 | in the current rule so far, which says where to find `$0' with |
310 | respect to the top of the stack. |
311 `-----------------------------------------------------------------*/
314 copy_at (FILE *fin
, struct obstack
*oout
, int stack_offset
)
321 obstack_sgrow (oout
, "yyloc");
324 else if (isdigit (c
) || c
== '-')
329 n
= read_signed_integer (fin
);
331 obstack_fgrow1 (oout
, "yylsp[%d]", n
- stack_offset
);
338 complain (_("%s is invalid"), quote (buf
));
343 /*-------------------------------------------------------------------.
344 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
346 | Possible inputs: $[<TYPENAME>]($|integer) |
348 | Output to OOUT a reference to this semantic value. STACK_OFFSET is |
349 | the number of values in the current rule so far, which says where |
350 | to find `$0' with respect to the top of the stack. |
351 `-------------------------------------------------------------------*/
354 copy_dollar (FILE *fin
, struct obstack
*oout
,
355 symbol_list
*rule
, int stack_offset
)
358 const char *type_name
= NULL
;
360 /* Get the type name if explicit. */
363 read_type_name (fin
);
364 type_name
= token_buffer
;
365 value_components_used
= 1;
371 obstack_sgrow (oout
, "yyval");
374 type_name
= get_type_name (0, rule
);
376 obstack_fgrow1 (oout
, ".%s", type_name
);
377 if (!type_name
&& typed
)
378 complain (_("$$ of `%s' has no declared type"),
381 else if (isdigit (c
) || c
== '-')
385 n
= read_signed_integer (fin
);
387 if (!type_name
&& n
> 0)
388 type_name
= get_type_name (n
, rule
);
390 obstack_fgrow1 (oout
, "yyvsp[%d]", n
- stack_offset
);
393 obstack_fgrow1 (oout
, ".%s", type_name
);
394 if (!type_name
&& typed
)
395 complain (_("$%d of `%s' has no declared type"),
402 complain (_("%s is invalid"), quote (buf
));
406 /*-------------------------------------------------------------------.
407 | Copy the contents of a `%{ ... %}' into the definitions file. The |
408 | `%{' has already been read. Return after reading the `%}'. |
409 `-------------------------------------------------------------------*/
412 copy_definition (void)
415 /* -1 while reading a character if prev char was %. */
420 obstack_fgrow2 (&attrs_obstack
, muscle_find ("linef"),
421 lineno
, quotearg_style (c_quoting_style
,
422 muscle_find("filename")));
434 obstack_1grow (&attrs_obstack
, c
);
444 copy_string (finput
, &attrs_obstack
, c
);
448 copy_comment (finput
, &attrs_obstack
);
452 fatal ("%s", _("unterminated `%{' definition"));
455 obstack_1grow (&attrs_obstack
, c
);
464 obstack_1grow (&attrs_obstack
, '%');
471 /*-------------------------------------------------------------------.
472 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
473 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
475 `-------------------------------------------------------------------*/
478 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
480 token_t token
= tok_undef
;
481 char *typename
= NULL
;
483 /* The symbol being defined. */
484 struct bucket
*symbol
= NULL
;
486 /* After `%token' and `%nterm', any number of symbols maybe be
490 int tmp_char
= ungetc (skip_white_space (), finput
);
492 /* `%' (for instance from `%token', or from `%%' etc.) is the
493 only valid means to end this declaration. */
497 fatal (_("Premature EOF after %s"), token_buffer
);
500 if (token
== tok_comma
)
505 if (token
== tok_typename
)
507 typename
= xstrdup (token_buffer
);
508 value_components_used
= 1;
511 else if (token
== tok_identifier
&& *symval
->tag
== '\"' && symbol
)
514 warn (_("symbol `%s' used more than once as a literal string"),
516 else if (symbol
->alias
)
517 warn (_("symbol `%s' given more than one literal string"),
521 symval
->class = token_sym
;
522 symval
->type_name
= typename
;
523 symval
->user_token_number
= symbol
->user_token_number
;
524 symbol
->user_token_number
= SALIAS
;
525 symval
->alias
= symbol
;
526 symbol
->alias
= symval
;
527 /* symbol and symval combined are only one symbol */
532 else if (token
== tok_identifier
)
534 int oldclass
= symval
->class;
537 if (symbol
->class == what_is_not
)
538 complain (_("symbol %s redefined"), symbol
->tag
);
539 symbol
->class = what_is
;
540 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
541 symbol
->value
= nvars
++;
545 if (symbol
->type_name
== NULL
)
546 symbol
->type_name
= typename
;
547 else if (strcmp (typename
, symbol
->type_name
) != 0)
548 complain (_("type redeclaration for %s"), symbol
->tag
);
551 else if (symbol
&& token
== tok_number
)
553 symbol
->user_token_number
= numval
;
557 complain (_("`%s' is invalid in %s"),
559 (what_is
== token_sym
) ? "%token" : "%nterm");
567 /*------------------------------.
568 | Parse what comes after %start |
569 `------------------------------*/
572 parse_start_decl (void)
575 complain (_("multiple %s declarations"), "%start");
576 if (lex () != tok_identifier
)
577 complain (_("invalid %s declaration"), "%start");
585 /*-----------------------------------------------------------.
586 | read in a %type declaration and record its information for |
587 | get_type_name to access |
588 `-----------------------------------------------------------*/
591 parse_type_decl (void)
595 if (lex () != tok_typename
)
597 complain ("%s", _("%type declaration has no <typename>"));
602 name
= xstrdup (token_buffer
);
607 int tmp_char
= ungetc (skip_white_space (), finput
);
612 fatal (_("Premature EOF after %s"), token_buffer
);
624 if (symval
->type_name
== NULL
)
625 symval
->type_name
= name
;
626 else if (strcmp (name
, symval
->type_name
) != 0)
627 complain (_("type redeclaration for %s"), symval
->tag
);
632 complain (_("invalid %%type declaration due to item: %s"),
641 /*----------------------------------------------------------------.
642 | Read in a %left, %right or %nonassoc declaration and record its |
644 `----------------------------------------------------------------*/
647 parse_assoc_decl (associativity assoc
)
652 lastprec
++; /* Assign a new precedence level, never 0. */
657 int tmp_char
= ungetc (skip_white_space (), finput
);
662 fatal (_("Premature EOF after %s"), token_buffer
);
669 name
= xstrdup (token_buffer
);
676 if (symval
->prec
!= 0)
677 complain (_("redefining precedence of %s"), symval
->tag
);
678 symval
->prec
= lastprec
;
679 symval
->assoc
= assoc
;
680 if (symval
->class == nterm_sym
)
681 complain (_("symbol %s redefined"), symval
->tag
);
682 symval
->class = token_sym
;
684 { /* record the type, if one is specified */
685 if (symval
->type_name
== NULL
)
686 symval
->type_name
= name
;
687 else if (strcmp (name
, symval
->type_name
) != 0)
688 complain (_("type redeclaration for %s"), symval
->tag
);
693 if (prev
== tok_identifier
)
695 symval
->user_token_number
= numval
;
700 ("invalid text (%s) - number should be after identifier"),
710 complain (_("unexpected item: %s"), token_buffer
);
720 /*--------------------------------------------------------------.
721 | Copy the union declaration into the stype muscle |
722 | (and fdefines), where it is made into the definition of |
723 | YYSTYPE, the type of elements of the parser value stack. |
724 `--------------------------------------------------------------*/
727 parse_union_decl (void)
732 struct obstack union_obstack
;
734 complain (_("multiple %s declarations"), "%union");
738 obstack_init (&union_obstack
);
739 obstack_sgrow (&union_obstack
, "union");
745 /* If C contains '/', it is output by copy_comment (). */
747 obstack_1grow (&union_obstack
, c
);
756 copy_comment (finput
, &union_obstack
);
764 /* FIXME: Errr. How could this happen???. --akim */
766 complain (_("unmatched %s"), "`}'");
774 /* JF don't choke on trailing semi */
775 c
= skip_white_space ();
778 obstack_1grow (&union_obstack
, 0);
779 muscle_insert ("stype", obstack_finish (&union_obstack
));
783 /*-------------------------------------------------------.
784 | Parse the declaration %expect N which says to expect N |
785 | shift-reduce conflicts. |
786 `-------------------------------------------------------*/
789 parse_expect_decl (void)
791 int c
= skip_white_space ();
795 complain (_("argument of %%expect is not an integer"));
797 expected_conflicts
= read_signed_integer (finput
);
801 /*-------------------------------------------------------------------.
802 | Parse what comes after %thong. the full syntax is |
804 | %thong <type> token number literal |
806 | the <type> or number may be omitted. The number specifies the |
807 | user_token_number. |
809 | Two symbols are entered in the table, one for the token symbol and |
810 | one for the literal. Both are given the <type>, if any, from the |
811 | declaration. The ->user_token_number of the first is SALIAS and |
812 | the ->user_token_number of the second is set to the number, if |
813 | any, from the declaration. The two symbols are linked via |
814 | pointers in their ->alias fields. |
816 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
817 | only the literal string is retained it is the literal string that |
818 | is output to yytname |
819 `-------------------------------------------------------------------*/
822 parse_thong_decl (void)
825 struct bucket
*symbol
;
827 int usrtoknum
= SUNDEF
;
829 token
= lex (); /* fetch typename or first token */
830 if (token
== tok_typename
)
832 typename
= xstrdup (token_buffer
);
833 value_components_used
= 1;
834 token
= lex (); /* fetch first token */
837 /* process first token */
839 if (token
!= tok_identifier
)
841 complain (_("unrecognized item %s, expected an identifier"),
846 symval
->class = token_sym
;
847 symval
->type_name
= typename
;
848 symval
->user_token_number
= SALIAS
;
851 token
= lex (); /* get number or literal string */
853 if (token
== tok_number
)
856 token
= lex (); /* okay, did number, now get literal */
859 /* process literal string token */
861 if (token
!= tok_identifier
|| *symval
->tag
!= '\"')
863 complain (_("expected string constant instead of %s"), token_buffer
);
867 symval
->class = token_sym
;
868 symval
->type_name
= typename
;
869 symval
->user_token_number
= usrtoknum
;
871 symval
->alias
= symbol
;
872 symbol
->alias
= symval
;
874 /* symbol and symval combined are only one symbol. */
879 parse_muscle_decl (void)
881 int ch
= ungetc (skip_white_space (), finput
);
886 if (!isalpha (ch
) && ch
!= '_')
888 complain (_("invalid %s declaration"), "%define");
892 copy_identifier (finput
, &muscle_obstack
);
893 obstack_1grow (&muscle_obstack
, 0);
894 muscle_key
= obstack_finish (&muscle_obstack
);
897 ch
= skip_white_space ();
903 complain (_("invalid %s declaration"), "%define");
908 fatal (_("Premature EOF after %s"), "\"");
910 copy_string2 (finput
, &muscle_obstack
, '"', 0);
911 obstack_1grow (&muscle_obstack
, 0);
912 muscle_value
= obstack_finish (&muscle_obstack
);
914 /* Store the (key, value) pair in the environment. */
915 muscle_insert (muscle_key
, muscle_value
);
920 /*---------------------------------.
921 | Parse a double quoted parameter. |
922 `---------------------------------*/
925 parse_dquoted_param (const char *from
)
927 struct obstack param_obstack
;
928 const char *param
= NULL
;
931 obstack_init (¶m_obstack
);
932 c
= skip_white_space ();
936 complain (_("invalid %s declaration"), from
);
942 while ((c
= literalchar ()) != '"')
943 obstack_1grow (¶m_obstack
, c
);
945 obstack_1grow (¶m_obstack
, '\0');
946 param
= obstack_finish (¶m_obstack
);
948 if (c
!= '"' || strlen (param
) == 0)
950 complain (_("invalid %s declaration"), from
);
960 /*----------------------------------.
961 | Parse what comes after %skeleton. |
962 `----------------------------------*/
965 parse_skel_decl (void)
967 skeleton
= parse_dquoted_param ("%skeleton");
970 /*----------------------------------------------------------------.
971 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
972 | any `%' declarations, and copy the contents of any `%{ ... %}' |
973 | groups to ATTRS_OBSTACK. |
974 `----------------------------------------------------------------*/
977 read_declarations (void)
981 int c
= skip_white_space ();
985 token_t tok
= parse_percent_token ();
989 case tok_two_percents
:
992 case tok_percent_left_curly
:
997 parse_token_decl (token_sym
, nterm_sym
);
1001 parse_token_decl (nterm_sym
, token_sym
);
1009 parse_start_decl ();
1013 parse_union_decl ();
1017 parse_expect_decl ();
1021 parse_thong_decl ();
1025 parse_assoc_decl (left_assoc
);
1029 parse_assoc_decl (right_assoc
);
1033 parse_assoc_decl (non_assoc
);
1037 parse_muscle_decl ();
1055 complain (_("unrecognized: %s"), token_buffer
);
1060 fatal (_("no input grammar"));
1065 complain (_("unknown character: %s"), quote (buf
));
1071 /*-------------------------------------------------------------------.
1072 | Assuming that a `{' has just been seen, copy everything up to the |
1073 | matching `}' into the actions file. STACK_OFFSET is the number of |
1074 | values in the current rule so far, which says where to find `$0' |
1075 | with respect to the top of the stack. |
1076 `-------------------------------------------------------------------*/
1079 copy_action (symbol_list
*rule
, int stack_offset
)
1084 /* offset is always 0 if parser has already popped the stack pointer */
1085 if (semantic_parser
)
1098 obstack_1grow (&action_obstack
, c
);
1103 obstack_1grow (&action_obstack
, c
);
1109 copy_string (finput
, &action_obstack
, c
);
1113 copy_comment (finput
, &action_obstack
);
1117 copy_dollar (finput
, &action_obstack
,
1118 rule
, stack_offset
);
1122 copy_at (finput
, &action_obstack
,
1127 fatal (_("unmatched %s"), "`{'");
1130 obstack_1grow (&action_obstack
, c
);
1136 /* above loop exits when c is '}' */
1140 obstack_1grow (&action_obstack
, c
);
1145 obstack_1grow (&action_obstack
, '\0');
1146 rule
->action
= obstack_finish (&action_obstack
);
1147 rule
->action_line
= lineno
;
1150 /*-------------------------------------------------------------------.
1151 | After `%guard' is seen in the input file, copy the actual guard |
1152 | into the guards file. If the guard is followed by an action, copy |
1153 | that into the actions file. STACK_OFFSET is the number of values |
1154 | in the current rule so far, which says where to find `$0' with |
1155 | respect to the top of the stack, for the simple parser in which |
1156 | the stack is not popped until after the guard is run. |
1157 `-------------------------------------------------------------------*/
1160 copy_guard (symbol_list
*rule
, int stack_offset
)
1166 /* offset is always 0 if parser has already popped the stack pointer */
1167 if (semantic_parser
)
1170 obstack_fgrow1 (&guard_obstack
, "\ncase %d:\n", nrules
);
1172 obstack_fgrow2 (&guard_obstack
, muscle_find ("linef"),
1173 lineno
, quotearg_style (c_quoting_style
,
1174 muscle_find ("filename")));
1175 obstack_1grow (&guard_obstack
, '{');
1180 while (brace_flag
? (count
> 0) : (c
!= ';'))
1185 obstack_1grow (&guard_obstack
, c
);
1190 obstack_1grow (&guard_obstack
, c
);
1196 obstack_1grow (&guard_obstack
, c
);
1201 complain (_("unmatched %s"), "`}'");
1202 c
= getc (finput
); /* skip it */
1208 copy_string (finput
, &guard_obstack
, c
);
1212 copy_comment (finput
, &guard_obstack
);
1216 copy_dollar (finput
, &guard_obstack
, rule
, stack_offset
);
1220 copy_at (finput
, &guard_obstack
, stack_offset
);
1224 fatal ("%s", _("unterminated %guard clause"));
1227 obstack_1grow (&guard_obstack
, c
);
1230 if (c
!= '}' || count
!= 0)
1234 c
= skip_white_space ();
1236 obstack_sgrow (&guard_obstack
, ";\n break;}");
1238 copy_action (rule
, stack_offset
);
1241 c
= getc (finput
); /* why not skip_white_space -wjh */
1243 copy_action (rule
, stack_offset
);
1250 /*-------------------------------------------------------------------.
1251 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1252 | with the user's names. |
1253 `-------------------------------------------------------------------*/
1258 /* Incremented for each generated symbol */
1259 static int gensym_count
= 0;
1260 static char buf
[256];
1264 sprintf (buf
, "@%d", ++gensym_count
);
1266 sym
= getsym (token_buffer
);
1267 sym
->class = nterm_sym
;
1268 sym
->value
= nvars
++;
1272 /*-------------------------------------------------------------------.
1273 | Parse the input grammar into a one symbol_list structure. Each |
1274 | rule is represented by a sequence of symbols: the left hand side |
1275 | followed by the contents of the right hand side, followed by a |
1276 | null pointer instead of a symbol to terminate the rule. The next |
1277 | symbol is the lhs of the following rule. |
1279 | All guards and actions are copied out to the appropriate files, |
1280 | labelled by the rule number they apply to. |
1282 | Bison used to allow some %directives in the rules sections, but |
1283 | this is no longer consider appropriate: (i) the documented grammar |
1284 | doesn't claim it, (ii), it would promote bad style, (iii), error |
1285 | recovery for %directives consists in skipping the junk until a `%' |
1286 | is seen and helrp synchronizing. This scheme is definitely wrong |
1287 | in the rules section. |
1288 `-------------------------------------------------------------------*/
1295 symbol_list
*p
= NULL
;
1296 symbol_list
*p1
= NULL
;
1299 /* Points to first symbol_list of current rule. its symbol is the
1301 symbol_list
*crule
= NULL
;
1302 /* Points to the symbol_list preceding crule. */
1303 symbol_list
*crule1
= NULL
;
1307 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1308 if (t
== tok_identifier
|| t
== tok_bar
)
1310 int action_flag
= 0;
1311 /* Number of symbols in rhs of this rule so far */
1313 int xactions
= 0; /* JF for error checking */
1314 bucket
*first_rhs
= 0;
1316 if (t
== tok_identifier
)
1329 complain (_("ill-formed rule: initial symbol not followed by colon"));
1334 if (nrules
== 0 && t
== tok_bar
)
1336 complain (_("grammar starts with vertical bar"));
1337 lhs
= symval
; /* BOGUS: use a random symval */
1339 /* start a new rule and record its lhs. */
1344 p
= symbol_list_new (lhs
);
1355 /* mark the rule's lhs as a nonterminal if not already so. */
1357 if (lhs
->class == unknown_sym
)
1359 lhs
->class = nterm_sym
;
1363 else if (lhs
->class == token_sym
)
1364 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1366 /* read the rhs of the rule. */
1374 crule
->ruleprec
= symval
;
1378 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1381 /* If next token is an identifier, see if a colon follows it.
1382 If one does, exit this rule now. */
1383 if (t
== tok_identifier
)
1392 if (t1
== tok_colon
)
1395 if (!first_rhs
) /* JF */
1397 /* Not followed by colon =>
1398 process as part of this rule's rhs. */
1401 /* If we just passed an action, that action was in the middle
1402 of a rule, so make a dummy rule to reduce it to a
1406 /* Since the action was written out with this rule's
1407 number, we must give the new rule this number by
1408 inserting the new rule before it. */
1410 /* Make a dummy nonterminal, a gensym. */
1411 bucket
*sdummy
= gensym ();
1413 /* Make a new rule, whose body is empty, before the
1414 current one, so that the action just read can
1418 p
= symbol_list_new (sdummy
);
1419 /* Attach its lineno to that of the host rule. */
1420 p
->line
= crule
->line
;
1425 /* End of the rule. */
1426 crule1
= symbol_list_new (NULL
);
1427 crule1
->next
= crule
;
1431 /* Insert the dummy generated by that rule into this
1434 p
= symbol_list_new (sdummy
);
1441 if (t
== tok_identifier
)
1444 p
= symbol_list_new (symval
);
1448 else /* handle an action. */
1450 copy_action (crule
, rulelength
);
1452 xactions
++; /* JF */
1455 } /* end of read rhs of rule */
1457 /* Put an empty link in the list to mark the end of this rule */
1458 p
= symbol_list_new (NULL
);
1464 complain (_("two @prec's in a row"));
1466 crule
->ruleprec
= symval
;
1471 if (!semantic_parser
)
1472 complain (_("%%guard present but %%semantic_parser not specified"));
1474 copy_guard (crule
, rulelength
);
1477 else if (t
== tok_left_curly
)
1479 /* This case never occurs -wjh */
1481 complain (_("two actions at end of one rule"));
1482 copy_action (crule
, rulelength
);
1484 xactions
++; /* -wjh */
1487 /* If $$ is being set in default way, report if any type
1490 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1492 if (lhs
->type_name
== 0
1493 || first_rhs
->type_name
== 0
1494 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1495 complain (_("type clash (`%s' `%s') on default action"),
1496 lhs
->type_name
? lhs
->type_name
: "",
1497 first_rhs
->type_name
? first_rhs
->type_name
: "");
1499 /* Warn if there is no default for $$ but we need one. */
1500 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1501 complain (_("empty rule for typed nonterminal, and no action"));
1502 if (t
== tok_semicolon
)
1507 complain (_("invalid input: %s"), quote (token_buffer
));
1512 /* grammar has been read. Do some checking */
1514 if (nsyms
> MAXSHORT
)
1515 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1518 fatal (_("no rules in the input grammar"));
1520 /* Report any undefined symbols and consider them nonterminals. */
1522 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1523 if (bp
->class == unknown_sym
)
1526 ("symbol %s is used, but is not defined as a token and has no rules"),
1528 bp
->class = nterm_sym
;
1529 bp
->value
= nvars
++;
1532 ntokens
= nsyms
- nvars
;
1535 /* At the end of the grammar file, some C source code must
1536 be stored. It is going to be associated to the epilogue
1539 read_additionnal_code (void)
1542 struct obstack el_obstack
;
1544 obstack_init (&el_obstack
);
1548 obstack_fgrow2 (&el_obstack
, muscle_find ("linef"),
1549 lineno
, quotearg_style (c_quoting_style
,
1550 muscle_find("filename")));
1553 while ((c
= getc (finput
)) != EOF
)
1554 obstack_1grow (&el_obstack
, c
);
1556 obstack_1grow (&el_obstack
, 0);
1557 muscle_insert ("epilogue", obstack_finish (&el_obstack
));
1561 /*------------------------------------------------------------------.
1562 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
1564 `------------------------------------------------------------------*/
1567 token_translations_init (void)
1572 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1574 /* Initialize all entries for literal tokens to 2, the internal
1575 token number for $undefined., which represents all invalid
1577 for (i
= 0; i
<= max_user_token_number
; i
++)
1578 token_translations
[i
] = 2;
1580 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1583 if (bp
->value
>= ntokens
)
1585 /* A token string alias? */
1586 if (bp
->user_token_number
== SALIAS
)
1589 assert (bp
->user_token_number
!= SUNDEF
);
1591 /* A token which translation has already been set? */
1592 if (token_translations
[bp
->user_token_number
] != 2)
1593 complain (_("tokens %s and %s both assigned number %d"),
1594 tags
[token_translations
[bp
->user_token_number
]],
1595 bp
->tag
, bp
->user_token_number
);
1596 token_translations
[bp
->user_token_number
] = bp
->value
;
1601 /*------------------------------------------------------------------.
1602 | Assign symbol numbers, and write definition of token names into |
1603 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1605 `------------------------------------------------------------------*/
1612 int last_user_token_number
;
1613 static char DOLLAR
[] = "$";
1615 tags
= XCALLOC (char *, nsyms
+ 1);
1616 user_toknums
= XCALLOC (short, nsyms
+ 1);
1618 sprec
= XCALLOC (short, nsyms
);
1619 sassoc
= XCALLOC (short, nsyms
);
1621 /* The EOF token. */
1623 user_toknums
[0] = 0;
1625 max_user_token_number
= 256;
1626 last_user_token_number
= 256;
1628 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1630 if (bp
->class == nterm_sym
)
1632 bp
->value
+= ntokens
;
1636 /* this symbol and its alias are a single token defn.
1637 allocate a tokno, and assign to both check agreement of
1638 ->prec and ->assoc fields and make both the same */
1640 bp
->value
= bp
->alias
->value
= tokno
++;
1642 if (bp
->prec
!= bp
->alias
->prec
)
1644 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1645 && bp
->user_token_number
== SALIAS
)
1646 complain (_("conflicting precedences for %s and %s"),
1647 bp
->tag
, bp
->alias
->tag
);
1649 bp
->alias
->prec
= bp
->prec
;
1651 bp
->prec
= bp
->alias
->prec
;
1654 if (bp
->assoc
!= bp
->alias
->assoc
)
1656 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1657 && bp
->user_token_number
== SALIAS
)
1658 complain (_("conflicting assoc values for %s and %s"),
1659 bp
->tag
, bp
->alias
->tag
);
1661 bp
->alias
->assoc
= bp
->assoc
;
1663 bp
->assoc
= bp
->alias
->assoc
;
1666 if (bp
->user_token_number
== SALIAS
)
1667 continue; /* do not do processing below for SALIASs */
1670 else /* bp->class == token_sym */
1672 bp
->value
= tokno
++;
1675 if (bp
->class == token_sym
)
1677 if (bp
->user_token_number
== SUNDEF
)
1678 bp
->user_token_number
= ++last_user_token_number
;
1679 if (bp
->user_token_number
> max_user_token_number
)
1680 max_user_token_number
= bp
->user_token_number
;
1683 tags
[bp
->value
] = bp
->tag
;
1684 user_toknums
[bp
->value
] = bp
->user_token_number
;
1685 sprec
[bp
->value
] = bp
->prec
;
1686 sassoc
[bp
->value
] = bp
->assoc
;
1689 token_translations_init ();
1691 error_token_number
= errtoken
->value
;
1693 if (startval
->class == unknown_sym
)
1694 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1695 else if (startval
->class == token_sym
)
1696 fatal (_("the start symbol %s is a token"), startval
->tag
);
1698 start_symbol
= startval
->value
;
1702 /*---------------------------------------------------------------.
1703 | Save the definition of token names in the `TOKENDEFS' muscle. |
1704 `---------------------------------------------------------------*/
1709 struct obstack tokendefs
;
1713 obstack_init (&tokendefs
);
1715 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1717 symbol
= bp
->tag
; /* get symbol */
1719 if (bp
->value
>= ntokens
)
1721 if (bp
->user_token_number
== SALIAS
)
1723 if ('\'' == *symbol
)
1724 continue; /* skip literal character */
1726 continue; /* skip error token */
1727 if ('\"' == *symbol
)
1729 /* use literal string only if given a symbol with an alias */
1731 symbol
= bp
->alias
->tag
;
1736 /* Don't #define nonliteral tokens whose names contain periods. */
1738 while ((c
= *cp
++) && c
!= '.');
1742 obstack_fgrow2 (&tokendefs
, "# define %s\t%d\n",
1743 symbol
, bp
->user_token_number
);
1744 if (semantic_parser
)
1745 /* FIXME: This is probably wrong, and should be just as
1747 obstack_fgrow2 (&tokendefs
, "# define T%s\t%d\n", symbol
, bp
->value
);
1750 obstack_1grow (&tokendefs
, 0);
1751 muscle_insert ("tokendef", xstrdup (obstack_finish (&tokendefs
)));
1752 obstack_free (&tokendefs
, NULL
);
1756 /*---------------------------------------------------------------.
1757 | Convert the rules into the representation using RRHS, RLHS and |
1759 `---------------------------------------------------------------*/
1768 ritem
= XCALLOC (short, nitems
+ 1);
1769 rule_table
= XCALLOC (rule_t
, nrules
) - 1;
1777 bucket
*ruleprec
= p
->ruleprec
;
1778 rule_table
[ruleno
].lhs
= p
->sym
->value
;
1779 rule_table
[ruleno
].rhs
= itemno
;
1780 rule_table
[ruleno
].line
= p
->line
;
1781 rule_table
[ruleno
].useful
= TRUE
;
1782 rule_table
[ruleno
].action
= p
->action
;
1783 rule_table
[ruleno
].action_line
= p
->action_line
;
1788 ritem
[itemno
++] = p
->sym
->value
;
1789 /* A rule gets by default the precedence and associativity
1790 of the last token in it. */
1791 if (p
->sym
->class == token_sym
)
1793 rule_table
[ruleno
].prec
= p
->sym
->prec
;
1794 rule_table
[ruleno
].assoc
= p
->sym
->assoc
;
1800 /* If this rule has a %prec,
1801 the specified symbol's precedence replaces the default. */
1804 rule_table
[ruleno
].prec
= ruleprec
->prec
;
1805 rule_table
[ruleno
].assoc
= ruleprec
->assoc
;
1806 rule_table
[ruleno
].precsym
= ruleprec
->value
;
1809 ritem
[itemno
++] = -ruleno
;
1819 ritem_print (stderr
);
1822 /*-------------------------------------------------------------------.
1823 | Read in the grammar specification and record it in the format |
1824 | described in gram.h. All guards are copied into the GUARD_OBSTACK |
1825 | and all actions into ACTION_OBSTACK, in each case forming the body |
1826 | of a C function (YYGUARD or YYACTION) which contains a switch |
1827 | statement to decide which guard or action to execute. |
1828 `-------------------------------------------------------------------*/
1834 startval
= NULL
; /* start symbol not specified yet. */
1844 semantic_parser
= 0;
1852 /* Initialize the muscle obstack. */
1853 obstack_init (&muscle_obstack
);
1855 /* Initialize the symbol table. */
1858 /* Construct the error token */
1859 errtoken
= getsym ("error");
1860 errtoken
->class = token_sym
;
1861 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1863 /* Construct a token that represents all undefined literal tokens.
1864 It is always token number 2. */
1865 undeftoken
= getsym ("$undefined.");
1866 undeftoken
->class = token_sym
;
1867 undeftoken
->user_token_number
= 2;
1869 /* Initialize the obstacks. */
1870 obstack_init (&action_obstack
);
1871 obstack_init (&attrs_obstack
);
1872 obstack_init (&guard_obstack
);
1873 obstack_init (&output_obstack
);
1875 finput
= xfopen (infile
, "r");
1877 /* Read the declaration section. Copy %{ ... %} groups to
1878 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1879 etc. found there. */
1880 read_declarations ();
1881 /* Read in the grammar, build grammar in list form. Write out
1882 guards and actions. */
1884 /* Some C code is given at the end of the grammar file. */
1885 read_additionnal_code ();
1890 /* Assign the symbols their symbol numbers. Write #defines for the
1891 token symbols into FDEFINES if requested. */
1896 /* Convert the grammar into the format described in gram.h. */