]>
git.saurik.com Git - bison.git/blob - src/reader.c
45b3eb73afd93e032ae07d78e1bb98b660756422
   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
; 
  44   /* The action is attached to the LHS of a rule. */ 
  48   /* The guard is attached to the LHS of a rule. */ 
  56 static symbol_list 
*grammar
; 
  57 static int start_flag
; 
  58 static bucket 
*startval
; 
  60 /* Nonzero if components of semantic values are used, implying 
  61    they must be unions.  */ 
  62 static int value_components_used
; 
  64 /* Nonzero if %union has been seen.  */ 
  67 /* Incremented for each %left, %right or %nonassoc seen */ 
  70 static bucket 
*errtoken 
= NULL
; 
  71 static bucket 
*undeftoken 
= NULL
; 
  72 static bucket 
*eoftoken 
= NULL
; 
  73 static bucket 
*axiom 
= NULL
; 
  76 symbol_list_new (bucket 
*sym
) 
  78   symbol_list 
*res 
= XMALLOC (symbol_list
, 1); 
  92 /*===================\ 
  94 \===================*/ 
  97 skip_to_char (int target
) 
 101     complain (_("   Skipping to next \\n")); 
 103     complain (_("   Skipping to next %c"), target
); 
 106     c 
= skip_white_space (); 
 107   while (c 
!= target 
&& c 
!= EOF
); 
 113 /*---------------------------------------------------------. 
 114 | Read a signed integer from STREAM and return its value.  | 
 115 `---------------------------------------------------------*/ 
 118 read_signed_integer (FILE *stream
) 
 120   int c 
= getc (stream
); 
 132       n 
= 10 * n 
+ (c 
- '0'); 
 141 /*--------------------------------------------------------------. 
 142 | Get the data type (alternative in the union) of the value for | 
 143 | symbol N in rule RULE.                                        | 
 144 `--------------------------------------------------------------*/ 
 147 get_type_name (int n
, symbol_list 
*rule
) 
 154       complain (_("invalid $ value")); 
 164       if (rp 
== NULL 
|| rp
->sym 
== NULL
) 
 166           complain (_("invalid $ value")); 
 172   return rp
->sym
->type_name
; 
 175 /*------------------------------------------------------------. 
 176 | Dump the string from FIN to OOUT if non null.  MATCH is the | 
 177 | delimiter of the string (either ' or ").                    | 
 178 `------------------------------------------------------------*/ 
 181 copy_string2 (FILE *fin
, struct obstack 
*oout
, int match
, int store
) 
 186     obstack_1grow (oout
, match
); 
 193         fatal (_("unterminated string at end of file")); 
 196           complain (_("unterminated string")); 
 198           c 
= match
;            /* invent terminator */ 
 202       obstack_1grow (oout
, c
); 
 208             fatal (_("unterminated string at end of file")); 
 209           obstack_1grow (oout
, c
); 
 219     obstack_1grow (oout
, c
); 
 225 copy_string (FILE *fin
, struct obstack 
*oout
, int match
) 
 227   copy_string2 (fin
, oout
, match
, 1); 
 233 copy_identifier (FILE *fin
, struct obstack 
*oout
) 
 237   while (isalnum (c 
= getc (fin
)) || c 
== '_') 
 238     obstack_1grow (oout
, c
); 
 244 /*------------------------------------------------------------------. 
 245 | Dump the wannabee comment from IN to OOUT.  In fact we just saw a | 
 246 | `/', which might or might not be a comment.  In any case, copy    | 
 248 `------------------------------------------------------------------*/ 
 251 copy_comment (FILE *fin
, struct obstack 
*oout
) 
 257   /* We read a `/', output it. */ 
 258   obstack_1grow (oout
, '/'); 
 260   switch ((c 
= getc (fin
))) 
 273   obstack_1grow (oout
, c
); 
 279       if (!cplus_comment 
&& c 
== '*') 
 283               obstack_1grow (oout
, c
); 
 289               obstack_1grow (oout
, c
); 
 296           obstack_1grow (oout
, c
); 
 303         fatal (_("unterminated comment")); 
 306           obstack_1grow (oout
, c
); 
 313 /*-----------------------------------------------------------------. 
 314 | FIN is pointing to a location (i.e., a `@').  Output to OOUT a   | 
 315 | reference to this location. STACK_OFFSET is the number of values | 
 316 | in the current rule so far, which says where to find `$0' with   | 
 317 | respect to the top of the stack.                                 | 
 318 `-----------------------------------------------------------------*/ 
 321 copy_at (FILE *fin
, struct obstack 
*oout
, int stack_offset
) 
 328       obstack_sgrow (oout
, "yyloc"); 
 331   else if (isdigit (c
) || c 
== '-') 
 336       n 
= read_signed_integer (fin
); 
 337       if (n 
> stack_offset
) 
 338         complain (_("invalid value: %s%d"), "@", n
); 
 341           /* Offset is always 0 if parser has already popped the stack 
 343           obstack_fgrow1 (oout
, "yylsp[%d]", 
 344                           n 
- (semantic_parser 
? 0 : stack_offset
)); 
 352       complain (_("%s is invalid"), quote (buf
)); 
 357 /*-------------------------------------------------------------------. 
 358 | FIN is pointing to a wannabee semantic value (i.e., a `$').        | 
 360 | Possible inputs: $[<TYPENAME>]($|integer)                          | 
 362 | Output to OOUT a reference to this semantic value. STACK_OFFSET is | 
 363 | the number of values in the current rule so far, which says where  | 
 364 | to find `$0' with respect to the top of the stack.                 | 
 365 `-------------------------------------------------------------------*/ 
 368 copy_dollar (FILE *fin
, struct obstack 
*oout
, 
 369              symbol_list 
*rule
, int stack_offset
) 
 372   const char *type_name 
= NULL
; 
 374   /* Get the type name if explicit. */ 
 377       read_type_name (fin
); 
 378       type_name 
= token_buffer
; 
 379       value_components_used 
= 1; 
 385       obstack_sgrow (oout
, "yyval"); 
 388         type_name 
= get_type_name (0, rule
); 
 390         obstack_fgrow1 (oout
, ".%s", type_name
); 
 391       if (!type_name 
&& typed
) 
 392         complain (_("$$ of `%s' has no declared type"), 
 395   else if (isdigit (c
) || c 
== '-') 
 399       n 
= read_signed_integer (fin
); 
 401       if (n 
> stack_offset
) 
 402         complain (_("invalid value: %s%d"), "$", n
); 
 405           if (!type_name 
&& n 
> 0) 
 406             type_name 
= get_type_name (n
, rule
); 
 408           /* Offset is always 0 if parser has already popped the stack 
 410           obstack_fgrow1 (oout
, "yyvsp[%d]", 
 411                           n 
- (semantic_parser 
? 0 : stack_offset
)); 
 414             obstack_fgrow1 (oout
, ".%s", type_name
); 
 415           if (!type_name 
&& typed
) 
 416             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 %. */ 
 442       obstack_fgrow2 (&attrs_obstack
, muscle_find ("linef"), 
 443                       lineno
, quotearg_style (c_quoting_style
, 
 444                                               muscle_find ("filename"))); 
 456           obstack_1grow (&attrs_obstack
, c
); 
 466           copy_string (finput
, &attrs_obstack
, c
); 
 470           copy_comment (finput
, &attrs_obstack
); 
 474           fatal ("%s", _("unterminated `%{' definition")); 
 477           obstack_1grow (&attrs_obstack
, c
); 
 486           obstack_1grow (&attrs_obstack
, '%'); 
 493 /*-------------------------------------------------------------------. 
 494 | Parse what comes after %token or %nterm.  For %token, WHAT_IS is   | 
 495 | token_sym and WHAT_IS_NOT is nterm_sym.  For %nterm, the arguments | 
 497 `-------------------------------------------------------------------*/ 
 500 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
) 
 502   token_t token 
= tok_undef
; 
 503   char *typename 
= NULL
; 
 505   /* The symbol being defined.  */ 
 506   struct bucket 
*symbol 
= NULL
; 
 508   /* After `%token' and `%nterm', any number of symbols maybe be 
 512       int tmp_char 
= ungetc (skip_white_space (), finput
); 
 514       /* `%' (for instance from `%token', or from `%%' etc.) is the 
 515          only valid means to end this declaration.  */ 
 519         fatal (_("Premature EOF after %s"), token_buffer
); 
 522       if (token 
== tok_comma
) 
 527       if (token 
== tok_typename
) 
 529           typename 
= xstrdup (token_buffer
); 
 530           value_components_used 
= 1; 
 533       else if (token 
== tok_identifier 
&& *symval
->tag 
== '\"' && symbol
) 
 536             warn (_("symbol `%s' used more than once as a literal string"), 
 538           else if (symbol
->alias
) 
 539             warn (_("symbol `%s' given more than one literal string"), 
 543               symval
->class = token_sym
; 
 544               symval
->type_name 
= typename
; 
 545               symval
->user_token_number 
= symbol
->user_token_number
; 
 546               symbol
->user_token_number 
= SALIAS
; 
 547               symval
->alias 
= symbol
; 
 548               symbol
->alias 
= symval
; 
 549               /* symbol and symval combined are only one symbol */ 
 554       else if (token 
== tok_identifier
) 
 556           int oldclass 
= symval
->class; 
 559           if (symbol
->class == what_is_not
) 
 560             complain (_("symbol %s redefined"), symbol
->tag
); 
 561           symbol
->class = what_is
; 
 562           if (what_is 
== nterm_sym 
&& oldclass 
!= nterm_sym
) 
 563             symbol
->value 
= nvars
++; 
 567               if (symbol
->type_name 
== NULL
) 
 568                 symbol
->type_name 
= typename
; 
 569               else if (strcmp (typename
, symbol
->type_name
) != 0) 
 570                 complain (_("type redeclaration for %s"), symbol
->tag
); 
 573       else if (symbol 
&& token 
== tok_number
) 
 575           symbol
->user_token_number 
= numval
; 
 576           /* User defined EOF token? */ 
 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) 
 757   struct obstack union_obstack
; 
 759     complain (_("multiple %s declarations"), "%union"); 
 763   obstack_init (&union_obstack
); 
 764   obstack_sgrow (&union_obstack
, "union"); 
 770       /* If C contains '/', it is output by copy_comment ().  */ 
 772         obstack_1grow (&union_obstack
, c
); 
 781           copy_comment (finput
, &union_obstack
); 
 789           /* FIXME: Errr.  How could this happen???. --akim */ 
 791             complain (_("unmatched %s"), "`}'"); 
 799   /* JF don't choke on trailing semi */ 
 800   c 
= skip_white_space (); 
 803   obstack_1grow (&union_obstack
, 0); 
 804   muscle_insert ("stype", obstack_finish (&union_obstack
)); 
 808 /*-------------------------------------------------------. 
 809 | Parse the declaration %expect N which says to expect N | 
 810 | shift-reduce conflicts.                                | 
 811 `-------------------------------------------------------*/ 
 814 parse_expect_decl (void) 
 816   int c 
= skip_white_space (); 
 820     complain (_("argument of %%expect is not an integer")); 
 822     expected_conflicts 
= read_signed_integer (finput
); 
 826 /*-------------------------------------------------------------------. 
 827 | Parse what comes after %thong.  the full syntax is                 | 
 829 |                %thong <type> token number literal                  | 
 831 | the <type> or number may be omitted.  The number specifies the     | 
 832 | user_token_number.                                                 | 
 834 | Two symbols are entered in the table, one for the token symbol and | 
 835 | one for the literal.  Both are given the <type>, if any, from the  | 
 836 | declaration.  The ->user_token_number of the first is SALIAS and   | 
 837 | the ->user_token_number of the second is set to the number, if     | 
 838 | any, from the declaration.  The two symbols are linked via         | 
 839 | pointers in their ->alias fields.                                  | 
 841 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter,    | 
 842 | only the literal string is retained it is the literal string that  | 
 843 | is output to yytname                                               | 
 844 `-------------------------------------------------------------------*/ 
 847 parse_thong_decl (void) 
 850   struct bucket 
*symbol
; 
 852   int usrtoknum 
= SUNDEF
; 
 854   token 
= lex ();               /* fetch typename or first token */ 
 855   if (token 
== tok_typename
) 
 857       typename 
= xstrdup (token_buffer
); 
 858       value_components_used 
= 1; 
 859       token 
= lex ();           /* fetch first token */ 
 862   /* process first token */ 
 864   if (token 
!= tok_identifier
) 
 866       complain (_("unrecognized item %s, expected an identifier"), 
 871   symval
->class = token_sym
; 
 872   symval
->type_name 
= typename
; 
 873   symval
->user_token_number 
= SALIAS
; 
 876   token 
= lex ();               /* get number or literal string */ 
 878   if (token 
== tok_number
) 
 881       token 
= lex ();           /* okay, did number, now get literal */ 
 884   /* process literal string token */ 
 886   if (token 
!= tok_identifier 
|| *symval
->tag 
!= '\"') 
 888       complain (_("expected string constant instead of %s"), token_buffer
); 
 892   symval
->class = token_sym
; 
 893   symval
->type_name 
= typename
; 
 894   symval
->user_token_number 
= usrtoknum
; 
 896   symval
->alias 
= symbol
; 
 897   symbol
->alias 
= symval
; 
 899   /* symbol and symval combined are only one symbol.  */ 
 905 parse_muscle_decl (void) 
 907   int ch 
= ungetc (skip_white_space (), finput
); 
 912   if (!isalpha (ch
) && ch 
!= '_') 
 914       complain (_("invalid %s declaration"), "%define"); 
 918   copy_identifier (finput
, &muscle_obstack
); 
 919   obstack_1grow (&muscle_obstack
, 0); 
 920   muscle_key 
= obstack_finish (&muscle_obstack
); 
 923   ch 
= skip_white_space (); 
 929           complain (_("invalid %s declaration"), "%define"); 
 934         fatal (_("Premature EOF after %s"), "\""); 
 936   copy_string2 (finput
, &muscle_obstack
, '"', 0); 
 937   obstack_1grow (&muscle_obstack
, 0); 
 938   muscle_value 
= obstack_finish (&muscle_obstack
); 
 940   /* Store the (key, value) pair in the environment. */ 
 941   muscle_insert (muscle_key
, muscle_value
); 
 946 /*---------------------------------. 
 947 | Parse a double quoted parameter. | 
 948 `---------------------------------*/ 
 951 parse_dquoted_param (const char *from
) 
 953   struct obstack param_obstack
; 
 954   const char *param 
= NULL
; 
 957   obstack_init (¶m_obstack
); 
 958   c 
= skip_white_space (); 
 962       complain (_("invalid %s declaration"), from
); 
 968   while ((c 
= literalchar ()) != '"') 
 969     obstack_1grow (¶m_obstack
, c
); 
 971   obstack_1grow (¶m_obstack
, '\0'); 
 972   param 
= obstack_finish (¶m_obstack
); 
 974   if (c 
!= '"' || strlen (param
) == 0) 
 976       complain (_("invalid %s declaration"), from
); 
 986 /*----------------------------------. 
 987 | Parse what comes after %skeleton. | 
 988 `----------------------------------*/ 
 991 parse_skel_decl (void) 
 993   skeleton 
= parse_dquoted_param ("%skeleton"); 
 996 /*----------------------------------------------------------------. 
 997 | Read from finput until `%%' is seen.  Discard the `%%'.  Handle | 
 998 | any `%' declarations, and copy the contents of any `%{ ... %}'  | 
 999 | groups to ATTRS_OBSTACK.                                        | 
1000 `----------------------------------------------------------------*/ 
1003 read_declarations (void) 
1007       int c 
= skip_white_space (); 
1011           token_t tok 
= parse_percent_token (); 
1015             case tok_two_percents
: 
1018             case tok_percent_left_curly
: 
1023               parse_token_decl (token_sym
, nterm_sym
); 
1027               parse_token_decl (nterm_sym
, token_sym
); 
1035               parse_start_decl (); 
1039               parse_union_decl (); 
1043               parse_expect_decl (); 
1047               parse_thong_decl (); 
1051               parse_assoc_decl (left_assoc
); 
1055               parse_assoc_decl (right_assoc
); 
1059               parse_assoc_decl (non_assoc
); 
1063               parse_muscle_decl (); 
1081               complain (_("unrecognized: %s"), token_buffer
); 
1086         fatal (_("no input grammar")); 
1091           complain (_("unknown character: %s"), quote (buf
)); 
1097 /*-------------------------------------------------------------------. 
1098 | Assuming that a `{' has just been seen, copy everything up to the  | 
1099 | matching `}' into the actions file.  STACK_OFFSET is the number of | 
1100 | values in the current rule so far, which says where to find `$0'   | 
1101 | with respect to the top of the stack.                              | 
1103 | This routine is used both for actions and guards.  Only            | 
1104 | ACTION_OBSTACK is used, but this is fine, since we use only        | 
1105 | pointers to relevant portions inside this obstack.                 | 
1106 `-------------------------------------------------------------------*/ 
1109 parse_braces (symbol_list 
*rule
, int stack_offset
) 
1117       while ((c 
= getc (finput
)) != '}') 
1121             obstack_1grow (&action_obstack
, c
); 
1126             obstack_1grow (&action_obstack
, c
); 
1132             copy_string (finput
, &action_obstack
, c
); 
1136             copy_comment (finput
, &action_obstack
); 
1140             copy_dollar (finput
, &action_obstack
, 
1141                          rule
, stack_offset
); 
1145             copy_at (finput
, &action_obstack
, 
1150             fatal (_("unmatched %s"), "`{'"); 
1153             obstack_1grow (&action_obstack
, c
); 
1156       /* Above loop exits when C is '}'.  */ 
1159           obstack_1grow (&action_obstack
, c
); 
1164   obstack_1grow (&action_obstack
, '\0'); 
1169 parse_action (symbol_list 
*rule
, int stack_offset
) 
1171   rule
->action_line 
= lineno
; 
1172   parse_braces (rule
, stack_offset
); 
1173   rule
->action 
= obstack_finish (&action_obstack
); 
1178 parse_guard (symbol_list 
*rule
, int stack_offset
) 
1181   if (t 
!= tok_left_curly
) 
1182     complain (_("invalid %s declaration"), "%guard"); 
1183   rule
->guard_line 
= lineno
; 
1184   parse_braces (rule
, stack_offset
); 
1185   rule
->guard 
= obstack_finish (&action_obstack
); 
1190 /*-------------------------------------------------------------------. 
1191 | Generate a dummy symbol, a nonterminal, whose name cannot conflict | 
1192 | with the user's names.                                             | 
1193 `-------------------------------------------------------------------*/ 
1198   /* Incremented for each generated symbol */ 
1199   static int gensym_count 
= 0; 
1200   static char buf
[256]; 
1204   sprintf (buf
, "@%d", ++gensym_count
); 
1206   sym 
= getsym (token_buffer
); 
1207   sym
->class = nterm_sym
; 
1208   sym
->value 
= nvars
++; 
1212 /*-------------------------------------------------------------------. 
1213 | Parse the input grammar into a one symbol_list structure.  Each    | 
1214 | rule is represented by a sequence of symbols: the left hand side   | 
1215 | followed by the contents of the right hand side, followed by a     | 
1216 | null pointer instead of a symbol to terminate the rule.  The next  | 
1217 | symbol is the lhs of the following rule.                           | 
1219 | All guards and actions are copied out to the appropriate files,    | 
1220 | labelled by the rule number they apply to.                         | 
1222 | Bison used to allow some %directives in the rules sections, but    | 
1223 | this is no longer consider appropriate: (i) the documented grammar | 
1224 | doesn't claim it, (ii), it would promote bad style, (iii), error   | 
1225 | recovery for %directives consists in skipping the junk until a `%' | 
1226 | is seen and helrp synchronizing.  This scheme is definitely wrong  | 
1227 | in the rules section.                                              | 
1228 `-------------------------------------------------------------------*/ 
1235   symbol_list 
*p 
= NULL
; 
1236   symbol_list 
*p1 
= NULL
; 
1239   /* Points to first symbol_list of current rule. its symbol is the 
1241   symbol_list 
*crule 
= NULL
; 
1242   /* Points to the symbol_list preceding crule.  */ 
1243   symbol_list 
*crule1 
= NULL
; 
1247   while (t 
!= tok_two_percents 
&& t 
!= tok_eof
) 
1248     if (t 
== tok_identifier 
|| t 
== tok_bar
) 
1250         int action_flag 
= 0; 
1251         /* Number of symbols in rhs of this rule so far */ 
1253         int xactions 
= 0;       /* JF for error checking */ 
1254         bucket 
*first_rhs 
= 0; 
1256         if (t 
== tok_identifier
) 
1269                 complain (_("ill-formed rule: initial symbol not followed by colon")); 
1274         if (nrules 
== 0 && t 
== tok_bar
) 
1276             complain (_("grammar starts with vertical bar")); 
1277             lhs 
= symval
;       /* BOGUS: use a random symval */ 
1279         /* start a new rule and record its lhs.  */ 
1284         p 
= symbol_list_new (lhs
); 
1295         /* mark the rule's lhs as a nonterminal if not already so.  */ 
1297         if (lhs
->class == unknown_sym
) 
1299             lhs
->class = nterm_sym
; 
1303         else if (lhs
->class == token_sym
) 
1304           complain (_("rule given for %s, which is a token"), lhs
->tag
); 
1306         /* read the rhs of the rule.  */ 
1314                 crule
->ruleprec 
= symval
; 
1318             if (!(t 
== tok_identifier 
|| t 
== tok_left_curly
)) 
1321             /* If next token is an identifier, see if a colon follows it. 
1322                If one does, exit this rule now.  */ 
1323             if (t 
== tok_identifier
) 
1332                 if (t1 
== tok_colon
) 
1335                 if (!first_rhs
) /* JF */ 
1337                 /* Not followed by colon => 
1338                    process as part of this rule's rhs.  */ 
1341             /* If we just passed an action, that action was in the middle 
1342                of a rule, so make a dummy rule to reduce it to a 
1346                 /* Since the action was written out with this rule's 
1347                    number, we must give the new rule this number by 
1348                    inserting the new rule before it.  */ 
1350                 /* Make a dummy nonterminal, a gensym.  */ 
1351                 bucket 
*sdummy 
= gensym (); 
1353                 /* Make a new rule, whose body is empty, before the 
1354                    current one, so that the action just read can 
1358                 p 
= symbol_list_new (sdummy
); 
1359                 /* Attach its lineno to that of the host rule. */ 
1360                 p
->line 
= crule
->line
; 
1365                 /* End of the rule. */ 
1366                 crule1 
= symbol_list_new (NULL
); 
1367                 crule1
->next 
= crule
; 
1371                 /* Insert the dummy generated by that rule into this 
1374                 p 
= symbol_list_new (sdummy
); 
1381             if (t 
== tok_identifier
) 
1384                 p 
= symbol_list_new (symval
); 
1388             else                /* handle an action.  */ 
1390                 parse_action (crule
, rulelength
); 
1392                 xactions
++;     /* JF */ 
1395           }                     /* end of  read rhs of rule */ 
1397         /* Put an empty link in the list to mark the end of this rule  */ 
1398         p 
= symbol_list_new (NULL
); 
1404             complain (_("two @prec's in a row")); 
1406             crule
->ruleprec 
= symval
; 
1412             if (!semantic_parser
) 
1413               complain (_("%%guard present but %%semantic_parser not specified")); 
1415             parse_guard (crule
, rulelength
); 
1419         if (t 
== tok_left_curly
) 
1421             /* This case never occurs -wjh */ 
1423               complain (_("two actions at end of one rule")); 
1424             parse_action (crule
, rulelength
); 
1426             xactions
++; /* -wjh */ 
1429         /* If $$ is being set in default way, report if any type 
1432                  && first_rhs 
&& lhs
->type_name 
!= first_rhs
->type_name
) 
1434             if (lhs
->type_name 
== 0 
1435                 || first_rhs
->type_name 
== 0 
1436                 || strcmp (lhs
->type_name
, first_rhs
->type_name
)) 
1437               complain (_("type clash (`%s' `%s') on default action"), 
1438                         lhs
->type_name 
? lhs
->type_name 
: "", 
1439                         first_rhs
->type_name 
? first_rhs
->type_name 
: ""); 
1441         /* Warn if there is no default for $$ but we need one.  */ 
1442         else if (!xactions 
&& !first_rhs 
&& lhs
->type_name 
!= 0) 
1443           complain (_("empty rule for typed nonterminal, and no action")); 
1444         if (t 
== tok_semicolon
) 
1449         complain (_("invalid input: %s"), quote (token_buffer
)); 
1453   /* grammar has been read.  Do some checking */ 
1456     fatal (_("no rules in the input grammar")); 
1458   /* Report any undefined symbols and consider them nonterminals.  */ 
1460   for (bp 
= firstsymbol
; bp
; bp 
= bp
->next
) 
1461     if (bp
->class == unknown_sym
) 
1464                   ("symbol %s is used, but is not defined as a token and has no rules"), 
1466         bp
->class = nterm_sym
; 
1467         bp
->value 
= nvars
++; 
1470   /* Insert the initial rule, which line is that of the first rule 
1471      (not that of the start symbol): 
1473      axiom: %start EOF.  */ 
1474   p 
= symbol_list_new (axiom
); 
1475   p
->line 
= grammar
->line
; 
1476   p
->next 
= symbol_list_new (startval
); 
1477   p
->next
->next 
= symbol_list_new (eoftoken
); 
1478   p
->next
->next
->next 
= symbol_list_new (NULL
); 
1479   p
->next
->next
->next
->next 
= grammar
; 
1485   if (nsyms 
> MAXSHORT
) 
1486     fatal (_("too many symbols (tokens plus nonterminals); maximum %d"), 
1489   ntokens 
= nsyms 
- nvars
; 
1492 /* At the end of the grammar file, some C source code must 
1493    be stored. It is going to be associated to the epilogue 
1496 read_additionnal_code (void) 
1499   struct obstack el_obstack
; 
1501   obstack_init (&el_obstack
); 
1505       obstack_fgrow2 (&el_obstack
, muscle_find ("linef"), 
1506                       lineno
, quotearg_style (c_quoting_style
, 
1507                                               muscle_find ("filename"))); 
1510   while ((c 
= getc (finput
)) != EOF
) 
1511     obstack_1grow (&el_obstack
, c
); 
1513   obstack_1grow (&el_obstack
, 0); 
1514   muscle_insert ("epilogue", obstack_finish (&el_obstack
)); 
1518 /*------------------------------------------------------------------. 
1519 | Set TOKEN_TRANSLATIONS.  Check that no two symbols share the same | 
1521 `------------------------------------------------------------------*/ 
1524 token_translations_init (void) 
1529   token_translations 
= XCALLOC (short, max_user_token_number 
+ 1); 
1531   /* Initialize all entries for literal tokens to 2, the internal 
1532      token number for $undefined., which represents all invalid 
1534   for (i 
= 0; i 
<= max_user_token_number
; i
++) 
1535     token_translations
[i
] = 2; 
1537   for (bp 
= firstsymbol
; bp
; bp 
= bp
->next
) 
1540       if (bp
->value 
>= ntokens
) 
1542       /* A token string alias? */ 
1543       if (bp
->user_token_number 
== SALIAS
) 
1546       assert (bp
->user_token_number 
!= SUNDEF
); 
1548       /* A token which translation has already been set? */ 
1549       if (token_translations
[bp
->user_token_number
] != 2) 
1550         complain (_("tokens %s and %s both assigned number %d"), 
1551                   symbols
[token_translations
[bp
->user_token_number
]]->tag
, 
1552                   bp
->tag
, bp
->user_token_number
); 
1553       token_translations
[bp
->user_token_number
] = bp
->value
; 
1558 /*----------------------------------------------------------------. 
1559 | Assign symbol numbers, and write definition of token names into | 
1560 | FDEFINES.  Set up vectors SYMBOL_TABLE, TAGS of symbols.        | 
1561 `----------------------------------------------------------------*/ 
1568   int last_user_token_number
; 
1570   user_toknums 
= XCALLOC (short, nsyms 
+ 1); 
1571   symbols 
= XCALLOC (bucket 
*, nsyms
); 
1573   sassoc 
= XCALLOC (short, nsyms
); 
1575   max_user_token_number 
= 256; 
1576   last_user_token_number 
= 256; 
1578   for (bp 
= firstsymbol
; bp
; bp 
= bp
->next
) 
1580       if (bp
->class == nterm_sym
) 
1582           bp
->value 
+= ntokens
; 
1586           /* This symbol and its alias are a single token defn. 
1587              Allocate a tokno, and assign to both check agreement of 
1588              prec and assoc fields and make both the same */ 
1589           if (bp
->value 
== -1) 
1591               if (bp 
== eoftoken 
|| bp
->alias 
== eoftoken
) 
1592                 bp
->value 
= bp
->alias
->value 
= 0; 
1595                   bp
->value 
= bp
->alias
->value 
= tokno
++; 
1599           if (bp
->prec 
!= bp
->alias
->prec
) 
1601               if (bp
->prec 
!= 0 && bp
->alias
->prec 
!= 0 
1602                   && bp
->user_token_number 
== SALIAS
) 
1603                 complain (_("conflicting precedences for %s and %s"), 
1604                           bp
->tag
, bp
->alias
->tag
); 
1606                 bp
->alias
->prec 
= bp
->prec
; 
1608                 bp
->prec 
= bp
->alias
->prec
; 
1611           if (bp
->assoc 
!= bp
->alias
->assoc
) 
1613               if (bp
->assoc 
!= 0 && bp
->alias
->assoc 
!= 0 
1614                   && bp
->user_token_number 
== SALIAS
) 
1615                 complain (_("conflicting assoc values for %s and %s"), 
1616                           bp
->tag
, bp
->alias
->tag
); 
1618                 bp
->alias
->assoc 
= bp
->assoc
; 
1620                 bp
->assoc 
= bp
->alias
->assoc
; 
1623           /* Do not do processing below for SALIASs.  */ 
1624           if (bp
->user_token_number 
== SALIAS
) 
1628       else /* bp->class == token_sym */ 
1633             bp
->value 
= tokno
++; 
1636       if (bp
->class == token_sym
) 
1638           if (bp
->user_token_number 
== SUNDEF
) 
1639             bp
->user_token_number 
= ++last_user_token_number
; 
1640           if (bp
->user_token_number 
> max_user_token_number
) 
1641             max_user_token_number 
= bp
->user_token_number
; 
1644       symbols
[bp
->value
] = bp
; 
1645       user_toknums
[bp
->value
] = bp
->user_token_number
; 
1646       sassoc
[bp
->value
] = bp
->assoc
; 
1649   token_translations_init (); 
1651   error_token_number 
= errtoken
->value
; 
1653   if (startval
->class == unknown_sym
) 
1654     fatal (_("the start symbol %s is undefined"), startval
->tag
); 
1655   else if (startval
->class == token_sym
) 
1656     fatal (_("the start symbol %s is a token"), startval
->tag
); 
1658   start_symbol 
= startval
->value
; 
1662 /*---------------------------------------------------------------. 
1663 | Save the definition of token names in the `TOKENDEFS' muscle.  | 
1664 `---------------------------------------------------------------*/ 
1669   struct obstack tokendefs
; 
1671   obstack_init (&tokendefs
); 
1673   for (bp 
= firstsymbol
; bp
; bp 
= bp
->next
) 
1675       char *symbol 
= bp
->tag
;                /* get symbol */ 
1677       if (bp
->value 
>= ntokens
) 
1679       if (bp
->user_token_number 
== SALIAS
) 
1681       if ('\'' == *symbol
) 
1682         continue;               /* skip literal character */ 
1684         continue;               /* skip error token */ 
1685       if ('\"' == *symbol
) 
1687           /* use literal string only if given a symbol with an alias */ 
1689             symbol 
= bp
->alias
->tag
; 
1694       /* Don't #define nonliteral tokens whose names contain periods.  */ 
1695       if (strchr (symbol
, '.')) 
1698       obstack_fgrow2 (&tokendefs
, "# define %s\t%d\n", 
1699                       symbol
, bp
->user_token_number
); 
1700       if (semantic_parser
) 
1701         /* FIXME: This is probably wrong, and should be just as 
1703         obstack_fgrow2 (&tokendefs
, "# define T%s\t%d\n", symbol
, bp
->value
); 
1706   obstack_1grow (&tokendefs
, 0); 
1707   muscle_insert ("tokendef", xstrdup (obstack_finish (&tokendefs
))); 
1708   obstack_free (&tokendefs
, NULL
); 
1712 /*---------------------------------------------------------------. 
1713 | Convert the rules into the representation using RRHS, RLHS and | 
1715 `---------------------------------------------------------------*/ 
1724   /* We use short to index items.  */ 
1725   if (nitems 
>= MAXSHORT
) 
1726     fatal (_("too many items (max %d)"), MAXSHORT
); 
1728   ritem 
= XCALLOC (short, nitems 
+ 1); 
1729   rule_table 
= XCALLOC (rule_t
, nrules
) - 1; 
1737       bucket 
*ruleprec 
= p
->ruleprec
; 
1738       rule_table
[ruleno
].lhs 
= p
->sym
->value
; 
1739       rule_table
[ruleno
].rhs 
= itemno
; 
1740       rule_table
[ruleno
].line 
= p
->line
; 
1741       rule_table
[ruleno
].useful 
= TRUE
; 
1742       rule_table
[ruleno
].action 
= p
->action
; 
1743       rule_table
[ruleno
].action_line 
= p
->action_line
; 
1744       rule_table
[ruleno
].guard 
= p
->guard
; 
1745       rule_table
[ruleno
].guard_line 
= p
->guard_line
; 
1750           ritem
[itemno
++] = p
->sym
->value
; 
1751           /* A rule gets by default the precedence and associativity 
1752              of the last token in it.  */ 
1753           if (p
->sym
->class == token_sym
) 
1755               rule_table
[ruleno
].prec 
= p
->sym
->prec
; 
1756               rule_table
[ruleno
].assoc 
= p
->sym
->assoc
; 
1762       /* If this rule has a %prec, 
1763          the specified symbol's precedence replaces the default.  */ 
1766           rule_table
[ruleno
].prec 
= ruleprec
->prec
; 
1767           rule_table
[ruleno
].assoc 
= ruleprec
->assoc
; 
1768           rule_table
[ruleno
].precsym 
= ruleprec
->value
; 
1771       ritem
[itemno
++] = -ruleno
; 
1780   assert (nritems 
== nitems
); 
1783     ritem_print (stderr
); 
1786 /*-------------------------------------------------------------------. 
1787 | Read in the grammar specification and record it in the format      | 
1788 | described in gram.h.  All guards are copied into the GUARD_OBSTACK | 
1789 | and all actions into ACTION_OBSTACK, in each case forming the body | 
1790 | of a C function (YYGUARD or YYACTION) which contains a switch      | 
1791 | statement to decide which guard or action to execute.              | 
1792 `-------------------------------------------------------------------*/ 
1798   startval 
= NULL
;              /* start symbol not specified yet. */ 
1808   semantic_parser 
= 0; 
1816   /* Initialize the muscle obstack.  */ 
1817   obstack_init (&muscle_obstack
); 
1819   /* Initialize the symbol table.  */ 
1822   /* Construct the axiom symbol. */ 
1823   axiom 
= getsym ("$axiom"); 
1824   axiom
->class = nterm_sym
; 
1825   axiom
->value 
= nvars
++; 
1827   /* Construct the error token */ 
1828   errtoken 
= getsym ("error"); 
1829   errtoken
->class = token_sym
; 
1830   errtoken
->user_token_number 
= 256;    /* Value specified by POSIX.  */ 
1832   /* Construct a token that represents all undefined literal tokens. 
1833      It is always token number 2.  */ 
1834   undeftoken 
= getsym ("$undefined."); 
1835   undeftoken
->class = token_sym
; 
1836   undeftoken
->user_token_number 
= 2; 
1838   /* Initialize the obstacks. */ 
1839   obstack_init (&action_obstack
); 
1840   obstack_init (&attrs_obstack
); 
1841   obstack_init (&output_obstack
); 
1843   finput 
= xfopen (infile
, "r"); 
1845   /* Read the declaration section.  Copy %{ ... %} groups to 
1846      TABLE_OBSTACK and FDEFINES file.  Also notice any %token, %left, 
1847      etc. found there.  */ 
1848   read_declarations (); 
1850   /* If the user did not define her EOFTOKEN, do it now. */ 
1853       eoftoken 
= getsym ("$"); 
1854       eoftoken
->class = token_sym
; 
1855       /* Value specified by POSIX.  */ 
1856       eoftoken
->user_token_number 
= 0; 
1859   /* Read in the grammar, build grammar in list form.  Write out 
1860      guards and actions.  */ 
1862   /* Some C code is given at the end of the grammar file. */ 
1863   read_additionnal_code (); 
1868   /* Assign the symbols their symbol numbers.  Write #defines for the 
1869      token symbols into FDEFINES if requested.  */ 
1874   /* Convert the grammar into the format described in gram.h.  */