]>
git.saurik.com Git - bison.git/blob - src/reader.c
bebbc1539b494ff3ab31b817aa1a9b3d36c029c8
   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. */ 
  57 static symbol_list 
*grammar
; 
  58 static int start_flag
; 
  59 static bucket 
*startval
; 
  61 /* Nonzero if components of semantic values are used, implying 
  62    they must be unions.  */ 
  63 static int value_components_used
; 
  65 /* Nonzero if %union has been seen.  */ 
  68 /* Incremented for each %left, %right or %nonassoc seen */ 
  71 static bucket 
*errtoken 
= NULL
; 
  72 static bucket 
*undeftoken 
= NULL
; 
  73 static bucket 
*eoftoken 
= NULL
; 
  74 static bucket 
*axiom 
= NULL
; 
  77 symbol_list_new (bucket 
*sym
) 
  79   symbol_list 
*res 
= XMALLOC (symbol_list
, 1); 
  93 /*===================\ 
  95 \===================*/ 
  98 skip_to_char (int target
) 
 102     complain (_("   Skipping to next \\n")); 
 104     complain (_("   Skipping to next %c"), target
); 
 107     c 
= skip_white_space (); 
 108   while (c 
!= target 
&& c 
!= EOF
); 
 114 /*---------------------------------------------------------. 
 115 | Read a signed integer from STREAM and return its value.  | 
 116 `---------------------------------------------------------*/ 
 119 read_signed_integer (FILE *stream
) 
 121   int c 
= getc (stream
); 
 133       n 
= 10 * n 
+ (c 
- '0'); 
 142 /*--------------------------------------------------------------. 
 143 | Get the data type (alternative in the union) of the value for | 
 144 | symbol N in rule RULE.                                        | 
 145 `--------------------------------------------------------------*/ 
 148 get_type_name (int n
, symbol_list 
*rule
) 
 155       complain (_("invalid $ value")); 
 165       if (rp 
== NULL 
|| rp
->sym 
== NULL
) 
 167           complain (_("invalid $ value")); 
 173   return rp
->sym
->type_name
; 
 176 /*------------------------------------------------------------. 
 177 | Dump the string from FIN to OOUT if non null.  MATCH is the | 
 178 | delimiter of the string (either ' or ").                    | 
 179 `------------------------------------------------------------*/ 
 182 copy_string2 (FILE *fin
, struct obstack 
*oout
, int match
, int store
) 
 187     obstack_1grow (oout
, match
); 
 194         fatal (_("unterminated string at end of file")); 
 197           complain (_("unterminated string")); 
 199           c 
= match
;            /* invent terminator */ 
 203       obstack_1grow (oout
, c
); 
 209             fatal (_("unterminated string at end of file")); 
 210           obstack_1grow (oout
, c
); 
 220     obstack_1grow (oout
, c
); 
 226 copy_string (FILE *fin
, struct obstack 
*oout
, int match
) 
 228   copy_string2 (fin
, oout
, match
, 1); 
 234 copy_identifier (FILE *fin
, struct obstack 
*oout
) 
 238   while (isalnum (c 
= getc (fin
)) || c 
== '_') 
 239     obstack_1grow (oout
, c
); 
 245 /*------------------------------------------------------------------. 
 246 | Dump the wannabee comment from IN to OOUT.  In fact we just saw a | 
 247 | `/', which might or might not be a comment.  In any case, copy    | 
 249 `------------------------------------------------------------------*/ 
 252 copy_comment (FILE *fin
, struct obstack 
*oout
) 
 258   /* We read a `/', output it. */ 
 259   obstack_1grow (oout
, '/'); 
 261   switch ((c 
= getc (fin
))) 
 274   obstack_1grow (oout
, c
); 
 280       if (!cplus_comment 
&& c 
== '*') 
 284               obstack_1grow (oout
, c
); 
 290               obstack_1grow (oout
, c
); 
 297           obstack_1grow (oout
, c
); 
 304         fatal (_("unterminated comment")); 
 307           obstack_1grow (oout
, c
); 
 314 /*-----------------------------------------------------------------. 
 315 | FIN is pointing to a location (i.e., a `@').  Output to OOUT a   | 
 316 | reference to this location. STACK_OFFSET is the number of values | 
 317 | in the current rule so far, which says where to find `$0' with   | 
 318 | respect to the top of the stack.                                 | 
 319 `-----------------------------------------------------------------*/ 
 322 copy_at (FILE *fin
, struct obstack 
*oout
, int stack_offset
) 
 329       obstack_sgrow (oout
, "yyloc"); 
 332   else if (isdigit (c
) || c 
== '-') 
 337       n 
= read_signed_integer (fin
); 
 338       if (n 
> stack_offset
) 
 339         complain (_("invalid value: %s%d"), "@", n
); 
 342           /* Offset is always 0 if parser has already popped the stack 
 344           obstack_fgrow1 (oout
, "yylsp[%d]", 
 345                           n 
- (semantic_parser 
? 0 : stack_offset
)); 
 353       complain (_("%s is invalid"), quote (buf
)); 
 358 /*-------------------------------------------------------------------. 
 359 | FIN is pointing to a wannabee semantic value (i.e., a `$').        | 
 361 | Possible inputs: $[<TYPENAME>]($|integer)                          | 
 363 | Output to OOUT a reference to this semantic value. STACK_OFFSET is | 
 364 | the number of values in the current rule so far, which says where  | 
 365 | to find `$0' with respect to the top of the stack.                 | 
 366 `-------------------------------------------------------------------*/ 
 369 copy_dollar (FILE *fin
, struct obstack 
*oout
, 
 370              symbol_list 
*rule
, int stack_offset
) 
 373   const char *type_name 
= NULL
; 
 375   /* Get the type name if explicit. */ 
 378       read_type_name (fin
); 
 379       type_name 
= token_buffer
; 
 380       value_components_used 
= 1; 
 386       obstack_sgrow (oout
, "yyval"); 
 389         type_name 
= get_type_name (0, rule
); 
 391         obstack_fgrow1 (oout
, ".%s", type_name
); 
 392       if (!type_name 
&& typed
) 
 393         complain (_("$$ of `%s' has no declared type"), 
 396   else if (isdigit (c
) || c 
== '-') 
 400       n 
= read_signed_integer (fin
); 
 402       if (n 
> stack_offset
) 
 403         complain (_("invalid value: %s%d"), "$", n
); 
 406           if (!type_name 
&& n 
> 0) 
 407             type_name 
= get_type_name (n
, rule
); 
 409           /* Offset is always 0 if parser has already popped the stack 
 411           obstack_fgrow1 (oout
, "yyvsp[%d]", 
 412                           n 
- (semantic_parser 
? 0 : stack_offset
)); 
 415             obstack_fgrow1 (oout
, ".%s", type_name
); 
 416           if (!type_name 
&& typed
) 
 417             complain (_("$%d of `%s' has no declared type"), 
 425       complain (_("%s is invalid"), quote (buf
)); 
 429 /*-------------------------------------------------------------------. 
 430 | Copy the contents of a `%{ ... %}' into the definitions file.  The | 
 431 | `%{' has already been read.  Return after reading the `%}'.        | 
 432 `-------------------------------------------------------------------*/ 
 435 copy_definition (void) 
 438   /* -1 while reading a character if prev char was %. */ 
 443       obstack_fgrow2 (&attrs_obstack
, muscle_find ("linef"), 
 444                       lineno
, quotearg_style (c_quoting_style
, 
 445                                               muscle_find ("filename"))); 
 457           obstack_1grow (&attrs_obstack
, c
); 
 467           copy_string (finput
, &attrs_obstack
, c
); 
 471           copy_comment (finput
, &attrs_obstack
); 
 475           fatal ("%s", _("unterminated `%{' definition")); 
 478           obstack_1grow (&attrs_obstack
, c
); 
 487           obstack_1grow (&attrs_obstack
, '%'); 
 494 /*-------------------------------------------------------------------. 
 495 | Parse what comes after %token or %nterm.  For %token, WHAT_IS is   | 
 496 | token_sym and WHAT_IS_NOT is nterm_sym.  For %nterm, the arguments | 
 498 `-------------------------------------------------------------------*/ 
 501 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
) 
 503   token_t token 
= tok_undef
; 
 504   char *typename 
= NULL
; 
 506   /* The symbol being defined.  */ 
 507   struct bucket 
*symbol 
= NULL
; 
 509   /* After `%token' and `%nterm', any number of symbols maybe be 
 513       int tmp_char 
= ungetc (skip_white_space (), finput
); 
 515       /* `%' (for instance from `%token', or from `%%' etc.) is the 
 516          only valid means to end this declaration.  */ 
 520         fatal (_("Premature EOF after %s"), token_buffer
); 
 523       if (token 
== tok_comma
) 
 528       if (token 
== tok_typename
) 
 530           typename 
= xstrdup (token_buffer
); 
 531           value_components_used 
= 1; 
 534       else if (token 
== tok_identifier 
&& *symval
->tag 
== '\"' && symbol
) 
 537             warn (_("symbol `%s' used more than once as a literal string"), 
 539           else if (symbol
->alias
) 
 540             warn (_("symbol `%s' given more than one literal string"), 
 544               symval
->class = token_sym
; 
 545               symval
->type_name 
= typename
; 
 546               symval
->user_token_number 
= symbol
->user_token_number
; 
 547               symbol
->user_token_number 
= SALIAS
; 
 548               symval
->alias 
= symbol
; 
 549               symbol
->alias 
= symval
; 
 550               /* symbol and symval combined are only one symbol */ 
 555       else if (token 
== tok_identifier
) 
 557           int oldclass 
= symval
->class; 
 560           if (symbol
->class == what_is_not
) 
 561             complain (_("symbol %s redefined"), symbol
->tag
); 
 562           symbol
->class = what_is
; 
 563           if (what_is 
== nterm_sym 
&& oldclass 
!= nterm_sym
) 
 564             symbol
->value 
= nvars
++; 
 568               if (symbol
->type_name 
== NULL
) 
 569                 symbol
->type_name 
= typename
; 
 570               else if (strcmp (typename
, symbol
->type_name
) != 0) 
 571                 complain (_("type redeclaration for %s"), symbol
->tag
); 
 574       else if (symbol 
&& token 
== tok_number
) 
 576           symbol
->user_token_number 
= numval
; 
 577           /* User defined EOF token? */ 
 583           complain (_("`%s' is invalid in %s"), 
 585                     (what_is 
== token_sym
) ? "%token" : "%nterm"); 
 593 /*------------------------------. 
 594 | Parse what comes after %start | 
 595 `------------------------------*/ 
 598 parse_start_decl (void) 
 601     complain (_("multiple %s declarations"), "%start"); 
 602   if (lex () != tok_identifier
) 
 603     complain (_("invalid %s declaration"), "%start"); 
 611 /*-----------------------------------------------------------. 
 612 | read in a %type declaration and record its information for | 
 613 | get_type_name to access                                    | 
 614 `-----------------------------------------------------------*/ 
 617 parse_type_decl (void) 
 621   if (lex () != tok_typename
) 
 623       complain ("%s", _("%type declaration has no <typename>")); 
 628   name 
= xstrdup (token_buffer
); 
 633       int tmp_char 
= ungetc (skip_white_space (), finput
); 
 638         fatal (_("Premature EOF after %s"), token_buffer
); 
 650           if (symval
->type_name 
== NULL
) 
 651             symval
->type_name 
= name
; 
 652           else if (strcmp (name
, symval
->type_name
) != 0) 
 653             complain (_("type redeclaration for %s"), symval
->tag
); 
 658           complain (_("invalid %%type declaration due to item: %s"), 
 667 /*----------------------------------------------------------------. 
 668 | Read in a %left, %right or %nonassoc declaration and record its | 
 670 `----------------------------------------------------------------*/ 
 673 parse_assoc_decl (associativity assoc
) 
 678   lastprec
++;                   /* Assign a new precedence level, never 0.  */ 
 683       int tmp_char 
= ungetc (skip_white_space (), finput
); 
 688         fatal (_("Premature EOF after %s"), token_buffer
); 
 695           name 
= xstrdup (token_buffer
); 
 702           if (symval
->prec 
!= 0) 
 703             complain (_("redefining precedence of %s"), symval
->tag
); 
 704           symval
->prec 
= lastprec
; 
 705           symval
->assoc 
= assoc
; 
 706           if (symval
->class == nterm_sym
) 
 707             complain (_("symbol %s redefined"), symval
->tag
); 
 708           symval
->class = token_sym
; 
 710             {                   /* record the type, if one is specified */ 
 711               if (symval
->type_name 
== NULL
) 
 712                 symval
->type_name 
= name
; 
 713               else if (strcmp (name
, symval
->type_name
) != 0) 
 714                 complain (_("type redeclaration for %s"), symval
->tag
); 
 719           if (prev 
== tok_identifier
) 
 721               symval
->user_token_number 
= numval
; 
 726                         ("invalid text (%s) - number should be after identifier"), 
 736           complain (_("unexpected item: %s"), token_buffer
); 
 746 /*--------------------------------------------------------------. 
 747 | Copy the union declaration into the stype muscle              | 
 748 | (and fdefines),  where it is made into the definition of      | 
 749 | YYSTYPE, the type of elements of the parser value stack.      | 
 750 `--------------------------------------------------------------*/ 
 753 parse_union_decl (void) 
 758   struct obstack union_obstack
; 
 760     complain (_("multiple %s declarations"), "%union"); 
 764   obstack_init (&union_obstack
); 
 765   obstack_sgrow (&union_obstack
, "union"); 
 771       /* If C contains '/', it is output by copy_comment ().  */ 
 773         obstack_1grow (&union_obstack
, c
); 
 782           copy_comment (finput
, &union_obstack
); 
 790           /* FIXME: Errr.  How could this happen???. --akim */ 
 792             complain (_("unmatched %s"), "`}'"); 
 800   /* JF don't choke on trailing semi */ 
 801   c 
= skip_white_space (); 
 804   obstack_1grow (&union_obstack
, 0); 
 805   muscle_insert ("stype", obstack_finish (&union_obstack
)); 
 809 /*-------------------------------------------------------. 
 810 | Parse the declaration %expect N which says to expect N | 
 811 | shift-reduce conflicts.                                | 
 812 `-------------------------------------------------------*/ 
 815 parse_expect_decl (void) 
 817   int c 
= skip_white_space (); 
 821     complain (_("argument of %%expect is not an integer")); 
 823     expected_conflicts 
= read_signed_integer (finput
); 
 827 /*-------------------------------------------------------------------. 
 828 | Parse what comes after %thong.  the full syntax is                 | 
 830 |                %thong <type> token number literal                  | 
 832 | the <type> or number may be omitted.  The number specifies the     | 
 833 | user_token_number.                                                 | 
 835 | Two symbols are entered in the table, one for the token symbol and | 
 836 | one for the literal.  Both are given the <type>, if any, from the  | 
 837 | declaration.  The ->user_token_number of the first is SALIAS and   | 
 838 | the ->user_token_number of the second is set to the number, if     | 
 839 | any, from the declaration.  The two symbols are linked via         | 
 840 | pointers in their ->alias fields.                                  | 
 842 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter,    | 
 843 | only the literal string is retained it is the literal string that  | 
 844 | is output to yytname                                               | 
 845 `-------------------------------------------------------------------*/ 
 848 parse_thong_decl (void) 
 851   struct bucket 
*symbol
; 
 853   int usrtoknum 
= SUNDEF
; 
 855   token 
= lex ();               /* fetch typename or first token */ 
 856   if (token 
== tok_typename
) 
 858       typename 
= xstrdup (token_buffer
); 
 859       value_components_used 
= 1; 
 860       token 
= lex ();           /* fetch first token */ 
 863   /* process first token */ 
 865   if (token 
!= tok_identifier
) 
 867       complain (_("unrecognized item %s, expected an identifier"), 
 872   symval
->class = token_sym
; 
 873   symval
->type_name 
= typename
; 
 874   symval
->user_token_number 
= SALIAS
; 
 877   token 
= lex ();               /* get number or literal string */ 
 879   if (token 
== tok_number
) 
 882       token 
= lex ();           /* okay, did number, now get literal */ 
 885   /* process literal string token */ 
 887   if (token 
!= tok_identifier 
|| *symval
->tag 
!= '\"') 
 889       complain (_("expected string constant instead of %s"), token_buffer
); 
 893   symval
->class = token_sym
; 
 894   symval
->type_name 
= typename
; 
 895   symval
->user_token_number 
= usrtoknum
; 
 897   symval
->alias 
= symbol
; 
 898   symbol
->alias 
= symval
; 
 900   /* symbol and symval combined are only one symbol.  */ 
 906 parse_muscle_decl (void) 
 908   int ch 
= ungetc (skip_white_space (), finput
); 
 913   if (!isalpha (ch
) && ch 
!= '_') 
 915       complain (_("invalid %s declaration"), "%define"); 
 919   copy_identifier (finput
, &muscle_obstack
); 
 920   obstack_1grow (&muscle_obstack
, 0); 
 921   muscle_key 
= obstack_finish (&muscle_obstack
); 
 924   ch 
= skip_white_space (); 
 930           complain (_("invalid %s declaration"), "%define"); 
 935         fatal (_("Premature EOF after %s"), "\""); 
 937   copy_string2 (finput
, &muscle_obstack
, '"', 0); 
 938   obstack_1grow (&muscle_obstack
, 0); 
 939   muscle_value 
= obstack_finish (&muscle_obstack
); 
 941   /* Store the (key, value) pair in the environment. */ 
 942   muscle_insert (muscle_key
, muscle_value
); 
 947 /*---------------------------------. 
 948 | Parse a double quoted parameter. | 
 949 `---------------------------------*/ 
 952 parse_dquoted_param (const char *from
) 
 954   struct obstack param_obstack
; 
 955   const char *param 
= NULL
; 
 958   obstack_init (¶m_obstack
); 
 959   c 
= skip_white_space (); 
 963       complain (_("invalid %s declaration"), from
); 
 969   while ((c 
= literalchar ()) != '"') 
 970     obstack_1grow (¶m_obstack
, c
); 
 972   obstack_1grow (¶m_obstack
, '\0'); 
 973   param 
= obstack_finish (¶m_obstack
); 
 975   if (c 
!= '"' || strlen (param
) == 0) 
 977       complain (_("invalid %s declaration"), from
); 
 987 /*----------------------------------. 
 988 | Parse what comes after %skeleton. | 
 989 `----------------------------------*/ 
 992 parse_skel_decl (void) 
 994   skeleton 
= parse_dquoted_param ("%skeleton"); 
 997 /*----------------------------------------------------------------. 
 998 | Read from finput until `%%' is seen.  Discard the `%%'.  Handle | 
 999 | any `%' declarations, and copy the contents of any `%{ ... %}'  | 
1000 | groups to ATTRS_OBSTACK.                                        | 
1001 `----------------------------------------------------------------*/ 
1004 read_declarations (void) 
1008       int c 
= skip_white_space (); 
1012           token_t tok 
= parse_percent_token (); 
1016             case tok_two_percents
: 
1019             case tok_percent_left_curly
: 
1024               parse_token_decl (token_sym
, nterm_sym
); 
1028               parse_token_decl (nterm_sym
, token_sym
); 
1036               parse_start_decl (); 
1040               parse_union_decl (); 
1044               parse_expect_decl (); 
1048               parse_thong_decl (); 
1052               parse_assoc_decl (left_assoc
); 
1056               parse_assoc_decl (right_assoc
); 
1060               parse_assoc_decl (non_assoc
); 
1064               parse_muscle_decl (); 
1082               complain (_("unrecognized: %s"), token_buffer
); 
1087         fatal (_("no input grammar")); 
1092           complain (_("unknown character: %s"), quote (buf
)); 
1098 /*-------------------------------------------------------------------. 
1099 | Assuming that a `{' has just been seen, copy everything up to the  | 
1100 | matching `}' into the actions file.  STACK_OFFSET is the number of | 
1101 | values in the current rule so far, which says where to find `$0'   | 
1102 | with respect to the top of the stack.                              | 
1104 | This routine is used both for actions and guards.  Only            | 
1105 | ACTION_OBSTACK is used, but this is fine, since we use only        | 
1106 | pointers to relevant portions inside this obstack.                 | 
1107 `-------------------------------------------------------------------*/ 
1110 parse_braces (symbol_list 
*rule
, int stack_offset
) 
1118       while ((c 
= getc (finput
)) != '}') 
1122             obstack_1grow (&action_obstack
, c
); 
1127             obstack_1grow (&action_obstack
, c
); 
1133             copy_string (finput
, &action_obstack
, c
); 
1137             copy_comment (finput
, &action_obstack
); 
1141             copy_dollar (finput
, &action_obstack
, 
1142                          rule
, stack_offset
); 
1146             copy_at (finput
, &action_obstack
, 
1151             fatal (_("unmatched %s"), "`{'"); 
1154             obstack_1grow (&action_obstack
, c
); 
1157       /* Above loop exits when C is '}'.  */ 
1160           obstack_1grow (&action_obstack
, c
); 
1165   obstack_1grow (&action_obstack
, '\0'); 
1170 parse_action (symbol_list 
*rule
, int stack_offset
) 
1172   rule
->action_line 
= lineno
; 
1173   parse_braces (rule
, stack_offset
); 
1174   rule
->action 
= obstack_finish (&action_obstack
); 
1179 parse_guard (symbol_list 
*rule
, int stack_offset
) 
1182   if (t 
!= tok_left_curly
) 
1183     complain (_("invalid %s declaration"), "%guard"); 
1184   rule
->guard_line 
= lineno
; 
1185   parse_braces (rule
, stack_offset
); 
1186   rule
->guard 
= obstack_finish (&action_obstack
); 
1191 /*-------------------------------------------------------------------. 
1192 | Generate a dummy symbol, a nonterminal, whose name cannot conflict | 
1193 | with the user's names.                                             | 
1194 `-------------------------------------------------------------------*/ 
1199   /* Incremented for each generated symbol */ 
1200   static int gensym_count 
= 0; 
1201   static char buf
[256]; 
1205   sprintf (buf
, "@%d", ++gensym_count
); 
1207   sym 
= getsym (token_buffer
); 
1208   sym
->class = nterm_sym
; 
1209   sym
->value 
= nvars
++; 
1213 /*-------------------------------------------------------------------. 
1214 | Parse the input grammar into a one symbol_list structure.  Each    | 
1215 | rule is represented by a sequence of symbols: the left hand side   | 
1216 | followed by the contents of the right hand side, followed by a     | 
1217 | null pointer instead of a symbol to terminate the rule.  The next  | 
1218 | symbol is the lhs of the following rule.                           | 
1220 | All guards and actions are copied out to the appropriate files,    | 
1221 | labelled by the rule number they apply to.                         | 
1223 | Bison used to allow some %directives in the rules sections, but    | 
1224 | this is no longer consider appropriate: (i) the documented grammar | 
1225 | doesn't claim it, (ii), it would promote bad style, (iii), error   | 
1226 | recovery for %directives consists in skipping the junk until a `%' | 
1227 | is seen and helrp synchronizing.  This scheme is definitely wrong  | 
1228 | in the rules section.                                              | 
1229 `-------------------------------------------------------------------*/ 
1236   symbol_list 
*p 
= NULL
; 
1237   symbol_list 
*p1 
= NULL
; 
1240   /* Points to first symbol_list of current rule. its symbol is the 
1242   symbol_list 
*crule 
= NULL
; 
1243   /* Points to the symbol_list preceding crule.  */ 
1244   symbol_list 
*crule1 
= NULL
; 
1248   while (t 
!= tok_two_percents 
&& t 
!= tok_eof
) 
1249     if (t 
== tok_identifier 
|| t 
== tok_bar
) 
1251         int action_flag 
= 0; 
1252         /* Number of symbols in rhs of this rule so far */ 
1254         int xactions 
= 0;       /* JF for error checking */ 
1255         bucket 
*first_rhs 
= 0; 
1257         if (t 
== tok_identifier
) 
1270                 complain (_("ill-formed rule: initial symbol not followed by colon")); 
1275         if (nrules 
== 0 && t 
== tok_bar
) 
1277             complain (_("grammar starts with vertical bar")); 
1278             lhs 
= symval
;       /* BOGUS: use a random symval */ 
1280         /* start a new rule and record its lhs.  */ 
1285         p 
= symbol_list_new (lhs
); 
1296         /* mark the rule's lhs as a nonterminal if not already so.  */ 
1298         if (lhs
->class == unknown_sym
) 
1300             lhs
->class = nterm_sym
; 
1304         else if (lhs
->class == token_sym
) 
1305           complain (_("rule given for %s, which is a token"), lhs
->tag
); 
1307         /* read the rhs of the rule.  */ 
1315                 crule
->ruleprec 
= symval
; 
1319             if (!(t 
== tok_identifier 
|| t 
== tok_left_curly
)) 
1322             /* If next token is an identifier, see if a colon follows it. 
1323                If one does, exit this rule now.  */ 
1324             if (t 
== tok_identifier
) 
1333                 if (t1 
== tok_colon
) 
1336                 if (!first_rhs
) /* JF */ 
1338                 /* Not followed by colon => 
1339                    process as part of this rule's rhs.  */ 
1342             /* If we just passed an action, that action was in the middle 
1343                of a rule, so make a dummy rule to reduce it to a 
1347                 /* Since the action was written out with this rule's 
1348                    number, we must give the new rule this number by 
1349                    inserting the new rule before it.  */ 
1351                 /* Make a dummy nonterminal, a gensym.  */ 
1352                 bucket 
*sdummy 
= gensym (); 
1354                 /* Make a new rule, whose body is empty, before the 
1355                    current one, so that the action just read can 
1359                 p 
= symbol_list_new (sdummy
); 
1360                 /* Attach its lineno to that of the host rule. */ 
1361                 p
->line 
= crule
->line
; 
1366                 /* End of the rule. */ 
1367                 crule1 
= symbol_list_new (NULL
); 
1368                 crule1
->next 
= crule
; 
1372                 /* Insert the dummy generated by that rule into this 
1375                 p 
= symbol_list_new (sdummy
); 
1382             if (t 
== tok_identifier
) 
1385                 p 
= symbol_list_new (symval
); 
1389             else                /* handle an action.  */ 
1391                 parse_action (crule
, rulelength
); 
1393                 xactions
++;     /* JF */ 
1396           }                     /* end of  read rhs of rule */ 
1398         /* Put an empty link in the list to mark the end of this rule  */ 
1399         p 
= symbol_list_new (NULL
); 
1405             complain (_("two @prec's in a row")); 
1407             crule
->ruleprec 
= symval
; 
1413             if (!semantic_parser
) 
1414               complain (_("%%guard present but %%semantic_parser not specified")); 
1416             parse_guard (crule
, rulelength
); 
1420         if (t 
== tok_left_curly
) 
1422             /* This case never occurs -wjh */ 
1424               complain (_("two actions at end of one rule")); 
1425             parse_action (crule
, rulelength
); 
1427             xactions
++; /* -wjh */ 
1430         /* If $$ is being set in default way, report if any type 
1433                  && first_rhs 
&& lhs
->type_name 
!= first_rhs
->type_name
) 
1435             if (lhs
->type_name 
== 0 
1436                 || first_rhs
->type_name 
== 0 
1437                 || strcmp (lhs
->type_name
, first_rhs
->type_name
)) 
1438               complain (_("type clash (`%s' `%s') on default action"), 
1439                         lhs
->type_name 
? lhs
->type_name 
: "", 
1440                         first_rhs
->type_name 
? first_rhs
->type_name 
: ""); 
1442         /* Warn if there is no default for $$ but we need one.  */ 
1443         else if (!xactions 
&& !first_rhs 
&& lhs
->type_name 
!= 0) 
1444           complain (_("empty rule for typed nonterminal, and no action")); 
1445         if (t 
== tok_semicolon
) 
1450         complain (_("invalid input: %s"), quote (token_buffer
)); 
1454   /* grammar has been read.  Do some checking */ 
1457     fatal (_("no rules in the input grammar")); 
1459   /* Report any undefined symbols and consider them nonterminals.  */ 
1461   for (bp 
= firstsymbol
; bp
; bp 
= bp
->next
) 
1462     if (bp
->class == unknown_sym
) 
1465                   ("symbol %s is used, but is not defined as a token and has no rules"), 
1467         bp
->class = nterm_sym
; 
1468         bp
->value 
= nvars
++; 
1471   /* Insert the initial rule, which line is that of the first rule 
1472      (not that of the start symbol): 
1474      axiom: %start EOF.  */ 
1475   p 
= symbol_list_new (axiom
); 
1476   p
->line 
= grammar
->line
; 
1477   p
->next 
= symbol_list_new (startval
); 
1478   p
->next
->next 
= symbol_list_new (eoftoken
); 
1479   p
->next
->next
->next 
= symbol_list_new (NULL
); 
1480   p
->next
->next
->next
->next 
= grammar
; 
1486   if (nsyms 
> MAXSHORT
) 
1487     fatal (_("too many symbols (tokens plus nonterminals); maximum %d"), 
1490   ntokens 
= nsyms 
- nvars
; 
1493 /* At the end of the grammar file, some C source code must 
1494    be stored. It is going to be associated to the epilogue 
1497 read_additionnal_code (void) 
1500   struct obstack el_obstack
; 
1502   obstack_init (&el_obstack
); 
1506       obstack_fgrow2 (&el_obstack
, muscle_find ("linef"), 
1507                       lineno
, quotearg_style (c_quoting_style
, 
1508                                               muscle_find ("filename"))); 
1511   while ((c 
= getc (finput
)) != EOF
) 
1512     obstack_1grow (&el_obstack
, c
); 
1514   obstack_1grow (&el_obstack
, 0); 
1515   muscle_insert ("epilogue", obstack_finish (&el_obstack
)); 
1519 /*------------------------------------------------------------------. 
1520 | Set TOKEN_TRANSLATIONS.  Check that no two symbols share the same | 
1522 `------------------------------------------------------------------*/ 
1525 token_translations_init (void) 
1530   token_translations 
= XCALLOC (short, max_user_token_number 
+ 1); 
1532   /* Initialize all entries for literal tokens to 2, the internal 
1533      token number for $undefined., which represents all invalid 
1535   for (i 
= 0; i 
<= max_user_token_number
; i
++) 
1536     token_translations
[i
] = 2; 
1538   for (bp 
= firstsymbol
; bp
; bp 
= bp
->next
) 
1541       if (bp
->value 
>= ntokens
) 
1543       /* A token string alias? */ 
1544       if (bp
->user_token_number 
== SALIAS
) 
1547       assert (bp
->user_token_number 
!= SUNDEF
); 
1549       /* A token which translation has already been set? */ 
1550       if (token_translations
[bp
->user_token_number
] != 2) 
1551         complain (_("tokens %s and %s both assigned number %d"), 
1552                   tags
[token_translations
[bp
->user_token_number
]], 
1553                   bp
->tag
, bp
->user_token_number
); 
1554       token_translations
[bp
->user_token_number
] = bp
->value
; 
1559 /*------------------------------------------------------------------. 
1560 | Assign symbol numbers, and write definition of token names into   | 
1561 | FDEFINES.  Set up vectors TAGS and SPREC of names and precedences | 
1563 `------------------------------------------------------------------*/ 
1570   int last_user_token_number
; 
1572   tags 
= XCALLOC (char *, nsyms 
+ 1); 
1573   user_toknums 
= XCALLOC (short, nsyms 
+ 1); 
1575   sprec 
= XCALLOC (short, nsyms
); 
1576   sassoc 
= XCALLOC (short, nsyms
); 
1578   max_user_token_number 
= 256; 
1579   last_user_token_number 
= 256; 
1581   for (bp 
= firstsymbol
; bp
; bp 
= bp
->next
) 
1583       if (bp
->class == nterm_sym
) 
1585           bp
->value 
+= ntokens
; 
1589           /* This symbol and its alias are a single token defn. 
1590              Allocate a tokno, and assign to both check agreement of 
1591              prec and assoc fields and make both the same */ 
1592           if (bp
->value 
== -1) 
1594               if (bp 
== eoftoken 
|| bp
->alias 
== eoftoken
) 
1595                 bp
->value 
= bp
->alias
->value 
= 0; 
1598                   bp
->value 
= bp
->alias
->value 
= tokno
++; 
1602           if (bp
->prec 
!= bp
->alias
->prec
) 
1604               if (bp
->prec 
!= 0 && bp
->alias
->prec 
!= 0 
1605                   && bp
->user_token_number 
== SALIAS
) 
1606                 complain (_("conflicting precedences for %s and %s"), 
1607                           bp
->tag
, bp
->alias
->tag
); 
1609                 bp
->alias
->prec 
= bp
->prec
; 
1611                 bp
->prec 
= bp
->alias
->prec
; 
1614           if (bp
->assoc 
!= bp
->alias
->assoc
) 
1616               if (bp
->assoc 
!= 0 && bp
->alias
->assoc 
!= 0 
1617                   && bp
->user_token_number 
== SALIAS
) 
1618                 complain (_("conflicting assoc values for %s and %s"), 
1619                           bp
->tag
, bp
->alias
->tag
); 
1621                 bp
->alias
->assoc 
= bp
->assoc
; 
1623                 bp
->assoc 
= bp
->alias
->assoc
; 
1626           /* Do not do processing below for SALIASs.  */ 
1627           if (bp
->user_token_number 
== SALIAS
) 
1631       else /* bp->class == token_sym */ 
1636             bp
->value 
= tokno
++; 
1639       if (bp
->class == token_sym
) 
1641           if (bp
->user_token_number 
== SUNDEF
) 
1642             bp
->user_token_number 
= ++last_user_token_number
; 
1643           if (bp
->user_token_number 
> max_user_token_number
) 
1644             max_user_token_number 
= bp
->user_token_number
; 
1647       tags
[bp
->value
] = bp
->tag
; 
1648       user_toknums
[bp
->value
] = bp
->user_token_number
; 
1649       sprec
[bp
->value
] = bp
->prec
; 
1650       sassoc
[bp
->value
] = bp
->assoc
; 
1653   token_translations_init (); 
1655   error_token_number 
= errtoken
->value
; 
1657   if (startval
->class == unknown_sym
) 
1658     fatal (_("the start symbol %s is undefined"), startval
->tag
); 
1659   else if (startval
->class == token_sym
) 
1660     fatal (_("the start symbol %s is a token"), startval
->tag
); 
1662   start_symbol 
= startval
->value
; 
1666 /*---------------------------------------------------------------. 
1667 | Save the definition of token names in the `TOKENDEFS' muscle.  | 
1668 `---------------------------------------------------------------*/ 
1673   struct obstack tokendefs
; 
1675   obstack_init (&tokendefs
); 
1677   for (bp 
= firstsymbol
; bp
; bp 
= bp
->next
) 
1679       char *symbol 
= bp
->tag
;                /* get symbol */ 
1681       if (bp
->value 
>= ntokens
) 
1683       if (bp
->user_token_number 
== SALIAS
) 
1685       if ('\'' == *symbol
) 
1686         continue;               /* skip literal character */ 
1688         continue;               /* skip error token */ 
1689       if ('\"' == *symbol
) 
1691           /* use literal string only if given a symbol with an alias */ 
1693             symbol 
= bp
->alias
->tag
; 
1698       /* Don't #define nonliteral tokens whose names contain periods.  */ 
1699       if (strchr (symbol
, '.')) 
1702       obstack_fgrow2 (&tokendefs
, "# define %s\t%d\n", 
1703                       symbol
, bp
->user_token_number
); 
1704       if (semantic_parser
) 
1705         /* FIXME: This is probably wrong, and should be just as 
1707         obstack_fgrow2 (&tokendefs
, "# define T%s\t%d\n", symbol
, bp
->value
); 
1710   obstack_1grow (&tokendefs
, 0); 
1711   muscle_insert ("tokendef", xstrdup (obstack_finish (&tokendefs
))); 
1712   obstack_free (&tokendefs
, NULL
); 
1716 /*---------------------------------------------------------------. 
1717 | Convert the rules into the representation using RRHS, RLHS and | 
1719 `---------------------------------------------------------------*/ 
1728   /* We use short to index items.  */ 
1729   if (nitems 
>= MAXSHORT
) 
1730     fatal (_("too many items (max %d)"), MAXSHORT
); 
1732   ritem 
= XCALLOC (short, nitems 
+ 1); 
1733   rule_table 
= XCALLOC (rule_t
, nrules
) - 1; 
1741       bucket 
*ruleprec 
= p
->ruleprec
; 
1742       rule_table
[ruleno
].lhs 
= p
->sym
->value
; 
1743       rule_table
[ruleno
].rhs 
= itemno
; 
1744       rule_table
[ruleno
].line 
= p
->line
; 
1745       rule_table
[ruleno
].useful 
= TRUE
; 
1746       rule_table
[ruleno
].action 
= p
->action
; 
1747       rule_table
[ruleno
].action_line 
= p
->action_line
; 
1748       rule_table
[ruleno
].guard 
= p
->guard
; 
1749       rule_table
[ruleno
].guard_line 
= p
->guard_line
; 
1754           ritem
[itemno
++] = p
->sym
->value
; 
1755           /* A rule gets by default the precedence and associativity 
1756              of the last token in it.  */ 
1757           if (p
->sym
->class == token_sym
) 
1759               rule_table
[ruleno
].prec 
= p
->sym
->prec
; 
1760               rule_table
[ruleno
].assoc 
= p
->sym
->assoc
; 
1766       /* If this rule has a %prec, 
1767          the specified symbol's precedence replaces the default.  */ 
1770           rule_table
[ruleno
].prec 
= ruleprec
->prec
; 
1771           rule_table
[ruleno
].assoc 
= ruleprec
->assoc
; 
1772           rule_table
[ruleno
].precsym 
= ruleprec
->value
; 
1775       ritem
[itemno
++] = -ruleno
; 
1784   assert (nritems 
== nitems
); 
1787     ritem_print (stderr
); 
1790 /*-------------------------------------------------------------------. 
1791 | Read in the grammar specification and record it in the format      | 
1792 | described in gram.h.  All guards are copied into the GUARD_OBSTACK | 
1793 | and all actions into ACTION_OBSTACK, in each case forming the body | 
1794 | of a C function (YYGUARD or YYACTION) which contains a switch      | 
1795 | statement to decide which guard or action to execute.              | 
1796 `-------------------------------------------------------------------*/ 
1802   startval 
= NULL
;              /* start symbol not specified yet. */ 
1812   semantic_parser 
= 0; 
1820   /* Initialize the muscle obstack.  */ 
1821   obstack_init (&muscle_obstack
); 
1823   /* Initialize the symbol table.  */ 
1826   /* Construct the axiom symbol. */ 
1827   axiom 
= getsym ("$axiom"); 
1828   axiom
->class = nterm_sym
; 
1829   axiom
->value 
= nvars
++; 
1831   /* Construct the error token */ 
1832   errtoken 
= getsym ("error"); 
1833   errtoken
->class = token_sym
; 
1834   errtoken
->user_token_number 
= 256;    /* Value specified by POSIX.  */ 
1836   /* Construct a token that represents all undefined literal tokens. 
1837      It is always token number 2.  */ 
1838   undeftoken 
= getsym ("$undefined."); 
1839   undeftoken
->class = token_sym
; 
1840   undeftoken
->user_token_number 
= 2; 
1842   /* Initialize the obstacks. */ 
1843   obstack_init (&action_obstack
); 
1844   obstack_init (&attrs_obstack
); 
1845   obstack_init (&output_obstack
); 
1847   finput 
= xfopen (infile
, "r"); 
1849   /* Read the declaration section.  Copy %{ ... %} groups to 
1850      TABLE_OBSTACK and FDEFINES file.  Also notice any %token, %left, 
1851      etc. found there.  */ 
1852   read_declarations (); 
1854   /* If the user did not define her EOFTOKEN, do it now. */ 
1857       eoftoken 
= getsym ("$"); 
1858       eoftoken
->class = token_sym
; 
1859       /* Value specified by POSIX.  */ 
1860       eoftoken
->user_token_number 
= 0; 
1863   /* Read in the grammar, build grammar in list form.  Write out 
1864      guards and actions.  */ 
1866   /* Some C code is given at the end of the grammar file. */ 
1867   read_additionnal_code (); 
1872   /* Assign the symbols their symbol numbers.  Write #defines for the 
1873      token symbols into FDEFINES if requested.  */ 
1878   /* Convert the grammar into the format described in gram.h.  */