]>
git.saurik.com Git - bison.git/blob - src/reader.c
6ec2c0d2c5dfc8505cc4fab6ee4ee5466d09a360
   1 /* Input parser for bison 
   2    Copyright (C) 1984, 86, 89, 92, 98, 2000 Free Software Foundation, Inc. 
   4    This file is part of Bison, the GNU Compiler Compiler. 
   6    Bison is free software; you can redistribute it and/or modify 
   7    it under the terms of the GNU General Public License as published by 
   8    the Free Software Foundation; either version 2, or (at your option) 
  11    Bison is distributed in the hope that it will be useful, 
  12    but WITHOUT ANY WARRANTY; without even the implied warranty of 
  13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  14    GNU General Public License for more details. 
  16    You should have received a copy of the GNU General Public License 
  17    along with Bison; see the file COPYING.  If not, write to 
  18    the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 
  19    Boston, MA 02111-1307, USA.  */ 
  22 /* Read in the grammar specification and record it in the format 
  23    described in gram.h.  All guards are copied into the fguard file 
  24    and all actions into faction, in each case forming the body of a C 
  25    function (yyguard or yyaction) which contains a switch statement to 
  26    decide which guard or action to execute. 
  28    The entry point is reader ().  */ 
  57 #define YYLTYPE yyltype\n\ 
  61 /* Number of slots allocated (but not necessarily used yet) in `rline'  */ 
  64 extern char *program_name
; 
  65 extern int definesflag
; 
  66 extern int nolinesflag
; 
  67 extern int noparserflag
; 
  68 extern int rawtoknumflag
; 
  69 extern bucket 
*symval
; 
  71 extern int expected_conflicts
; 
  72 extern char *token_buffer
; 
  75 extern void init_lex 
PARAMS((void)); 
  76 extern char *grow_token_buffer 
PARAMS((char *)); 
  77 extern void tabinit 
PARAMS((void)); 
  78 extern void output_headers 
PARAMS((void)); 
  79 extern void output_trailers 
PARAMS((void)); 
  80 extern void free_symtab 
PARAMS((void)); 
  81 extern void open_extra_files 
PARAMS((void)); 
  82 extern char *int_to_string 
PARAMS((int)); 
  83 extern char *printable_version 
PARAMS((int)); 
  84 extern void fatal 
PARAMS((char *)); 
  85 extern void fatals 
PARAMS((char *, char *)); 
  86 extern void warn 
PARAMS((char *)); 
  87 extern void warni 
PARAMS((char *, int)); 
  88 extern void warns 
PARAMS((char *, char *)); 
  89 extern void warnss 
PARAMS((char *, char *, char *)); 
  90 extern void warnsss 
PARAMS((char *, char *, char *, char *)); 
  91 extern void unlex 
PARAMS((int)); 
  92 extern void done 
PARAMS((int)); 
  94 extern int skip_white_space 
PARAMS((void)); 
  95 extern int parse_percent_token 
PARAMS((void)); 
  96 extern int lex 
PARAMS((void)); 
 101       struct symbol_list 
*next
; 
 108 void reader 
PARAMS((void)); 
 109 void reader_output_yylsp 
PARAMS((FILE *)); 
 110 void read_declarations 
PARAMS((void)); 
 111 void copy_definition 
PARAMS((void)); 
 112 void parse_token_decl 
PARAMS((int, int)); 
 113 void parse_start_decl 
PARAMS((void)); 
 114 void parse_type_decl 
PARAMS((void)); 
 115 void parse_assoc_decl 
PARAMS((int)); 
 116 void parse_union_decl 
PARAMS((void)); 
 117 void parse_expect_decl 
PARAMS((void)); 
 118 char *get_type_name 
PARAMS((int, symbol_list 
*)); 
 119 void copy_guard 
PARAMS((symbol_list 
*, int)); 
 120 void parse_thong_decl 
PARAMS((void)); 
 121 void copy_action 
PARAMS((symbol_list 
*, int)); 
 122 bucket 
*gensym 
PARAMS((void)); 
 123 void readgram 
PARAMS((void)); 
 124 void record_rule_line 
PARAMS((void)); 
 125 void packsymbols 
PARAMS((void)); 
 126 void output_token_defines 
PARAMS((FILE *)); 
 127 void packgram 
PARAMS((void)); 
 128 int read_signed_integer 
PARAMS((FILE *)); 
 131 static int get_type 
PARAMS((void)); 
 135 symbol_list 
*grammar
; 
 141 /* Nonzero if components of semantic values are used, implying 
 142    they must be unions.  */ 
 143 static int value_components_used
; 
 145 static int typed
;  /* nonzero if %union has been seen.  */ 
 147 static int lastprec
;  /* incremented for each %left, %right or %nonassoc seen */ 
 149 static int gensym_count
;  /* incremented for each generated symbol */ 
 151 static bucket 
*errtoken
; 
 152 static bucket 
*undeftoken
; 
 154 /* Nonzero if any action or guard uses the @n construct.  */ 
 155 static int yylsp_needed
; 
 159 skip_to_char (int target
) 
 163     warn(_("   Skipping to next \\n")); 
 165     warni(_("   Skipping to next %c"), target
); 
 168     c 
= skip_white_space(); 
 169   while (c 
!= target 
&& c 
!= EOF
); 
 175 /* Dump the string from FINPUT to FOUTPUT.  MATCH is the delimiter of 
 176    the string (either ' or ").  */ 
 179 copy_string (FILE *finput
, FILE *foutput
, int match
) 
 183   putc (match
, foutput
); 
 189         fatal (_("unterminated string at end of file")); 
 192           warn (_("unterminated string")); 
 194           c 
= match
;            /* invent terminator */ 
 204             fatal (_("unterminated string at end of file")); 
 217 /* Dump the comment from FINPUT to FOUTPUT.  C is either `*' or `/', 
 218    depending upon the type of comments used.  */ 
 221 copy_comment (FILE *finput
, FILE *foutput
, int c
) 
 227   cplus_comment 
= (c 
== '/'); 
 234       if (!cplus_comment 
&& c 
== '*') 
 258         fatal (_("unterminated comment")); 
 272   startval 
= NULL
;  /* start symbol not specified yet. */ 
 275   translations 
= 0;  /* initially assume token number translation not needed.  */ 
 277   /* Nowadays translations is always set to 1, 
 278      since we give `error' a user-token-number 
 279      to satisfy the Posix demand for YYERRCODE==256.  */ 
 286   rline_allocated 
= 10; 
 287   rline 
= NEW2(rline_allocated
, short); 
 303   /* initialize the symbol table.  */ 
 305   /* construct the error token */ 
 306   errtoken 
= getsym("error"); 
 307   errtoken
->class = STOKEN
; 
 308   errtoken
->user_token_number 
= 256; /* Value specified by posix.  */ 
 309   /* construct a token that represents all undefined literal tokens. */ 
 310   /* it is always token number 2.  */ 
 311   undeftoken 
= getsym("$undefined."); 
 312   undeftoken
->class = STOKEN
; 
 313   undeftoken
->user_token_number 
= 2; 
 314   /* Read the declaration section.  Copy %{ ... %} groups to ftable and fdefines file. 
 315      Also notice any %token, %left, etc. found there.  */ 
 317     fprintf(ftable
, "\n/*  Bison-generated parse tables, made from %s\n", 
 320     fprintf(ftable
, "\n/*  A Bison parser, made from %s\n", infile
); 
 321   fprintf(ftable
, "    by %s  */\n\n", VERSION_STRING
); 
 322   fprintf(ftable
, "#define YYBISON 1  /* Identify Bison output.  */\n\n"); 
 324   /* start writing the guard and action files, if they are needed.  */ 
 326   /* read in the grammar, build grammar in list form.  write out guards and actions.  */ 
 328   /* Now we know whether we need the line-number stack. 
 329      If we do, write its type into the .tab.h file.  */ 
 331     reader_output_yylsp(fdefines
); 
 332   /* write closing delimiters for actions and guards.  */ 
 335     fprintf(ftable
, "#define YYLSP_NEEDED\n\n"); 
 336   /* assign the symbols their symbol numbers. 
 337      Write #defines for the token symbols into fdefines if requested.  */ 
 339   /* convert the grammar into the format described in gram.h.  */ 
 341   /* free the symbol table data structure 
 342      since symbols are now all referred to by symbol number.  */ 
 347 reader_output_yylsp (FILE *f
) 
 350     fprintf(f
, LTYPESTR
); 
 353 /* Read from finput until `%%' is seen.  Discard the `%%'.  Handle any 
 354    `%' declarations, and copy the contents of any `%{ ... %}' groups 
 358 read_declarations (void) 
 365       c 
= skip_white_space(); 
 369           tok 
= parse_percent_token(); 
 376             case PERCENT_LEFT_CURLY
: 
 381               parse_token_decl (STOKEN
, SNTERM
); 
 385               parse_token_decl (SNTERM
, STOKEN
); 
 407               parse_assoc_decl(LEFT_ASSOC
); 
 411               parse_assoc_decl(RIGHT_ASSOC
); 
 415               parse_assoc_decl(NON_ASSOC
); 
 418             case SEMANTIC_PARSER
: 
 419               if (semantic_parser 
== 0) 
 434               warns(_("unrecognized: %s"), token_buffer
); 
 439         fatal(_("no input grammar")); 
 442           warns (_("unknown character: %s"), printable_version(c
)); 
 449 /* Copy the contents of a `%{ ... %}' into the definitions file.  The 
 450    `%{' has already been read.  Return after reading the `%}'.  */ 
 453 copy_definition (void) 
 456   /* -1 while reading a character if prev char was %. */ 
 457   register int after_percent
; 
 460     fprintf(fattrs
, "#line %d \"%s\"\n", lineno
, infile
); 
 481           copy_string (finput
, fattrs
, c
); 
 487           if (c 
!= '*' && c 
!= '/') 
 489           copy_comment (finput
, fattrs
, c
); 
 493           fatal(_("unterminated `%{' definition")); 
 515 /* parse what comes after %token or %nterm. 
 516 For %token, what_is is STOKEN and what_is_not is SNTERM. 
 517 For %nterm, the arguments are reversed.  */ 
 520 parse_token_decl (int what_is
, int what_is_not
) 
 522   register int token 
= 0; 
 523   register char *typename 
= 0; 
 524   register struct bucket 
*symbol 
= NULL
;  /* pts to symbol being defined */ 
 529       int tmp_char 
= ungetc (skip_white_space (), finput
); 
 534         fatals ("Premature EOF after %s", token_buffer
); 
 542       if (token 
== TYPENAME
) 
 544           k 
= strlen(token_buffer
); 
 545           typename 
= NEW2(k 
+ 1, char); 
 546           strcpy(typename
, token_buffer
); 
 547           value_components_used 
= 1; 
 550       else if (token 
== IDENTIFIER 
&& *symval
->tag 
== '\"' 
 554           symval
->class = STOKEN
; 
 555           symval
->type_name 
= typename
; 
 556           symval
->user_token_number 
= symbol
->user_token_number
; 
 557           symbol
->user_token_number 
= SALIAS
; 
 559           symval
->alias 
= symbol
; 
 560           symbol
->alias 
= symval
; 
 563           nsyms
--;   /* symbol and symval combined are only one symbol */ 
 565       else if (token 
== IDENTIFIER
) 
 567           int oldclass 
= symval
->class; 
 570           if (symbol
->class == what_is_not
) 
 571             warns(_("symbol %s redefined"), symbol
->tag
); 
 572           symbol
->class = what_is
; 
 573           if (what_is 
== SNTERM 
&& oldclass 
!= SNTERM
) 
 574             symbol
->value 
= nvars
++; 
 578               if (symbol
->type_name 
== NULL
) 
 579                 symbol
->type_name 
= typename
; 
 580               else if (strcmp(typename
, symbol
->type_name
) != 0) 
 581                 warns(_("type redeclaration for %s"), symbol
->tag
); 
 584       else if (symbol 
&& token 
== NUMBER
) 
 586           symbol
->user_token_number 
= numval
; 
 591           warnss(_("`%s' is invalid in %s"), 
 593                 (what_is 
== STOKEN
) ? "%token" : "%nterm"); 
 600 /* parse what comes after %thong 
 602                 %thong <type> token number literal 
 603  the <type> or number may be omitted.  The number specifies the 
 606  Two symbols are entered in the table, one for the token symbol and 
 607  one for the literal.  Both are given the <type>, if any, from the declaration. 
 608  The ->user_token_number of the first is SALIAS and the ->user_token_number 
 609  of the second is set to the number, if any, from the declaration. 
 610  The two symbols are linked via pointers in their ->alias fields. 
 612  during output_defines_table, the symbol is reported 
 613  thereafter, only the literal string is retained 
 614  it is the literal string that is output to yytname 
 618 parse_thong_decl (void) 
 621   register struct bucket 
*symbol
; 
 622   register char *typename 
= 0; 
 626   token 
= lex();                /* fetch typename or first token */ 
 627   if (token 
== TYPENAME
) { 
 628     k 
= strlen(token_buffer
); 
 629     typename 
= NEW2(k 
+ 1, char); 
 630     strcpy(typename
, token_buffer
); 
 631     value_components_used 
= 1; 
 632     token 
= lex();              /* fetch first token */ 
 635   /* process first token */ 
 637   if (token 
!= IDENTIFIER
) 
 639       warns(_("unrecognized item %s, expected an identifier"), 
 644   symval
->class = STOKEN
; 
 645   symval
->type_name 
= typename
; 
 646   symval
->user_token_number 
= SALIAS
; 
 649   token 
= lex();                /* get number or literal string */ 
 651   if (token 
== NUMBER
) { 
 653     token 
= lex();              /* okay, did number, now get literal */ 
 657   /* process literal string token */ 
 659   if (token 
!= IDENTIFIER 
|| *symval
->tag 
!= '\"') 
 661       warns(_("expected string constant instead of %s"), 
 666   symval
->class = STOKEN
; 
 667   symval
->type_name 
= typename
; 
 668   symval
->user_token_number 
= usrtoknum
; 
 670   symval
->alias 
= symbol
; 
 671   symbol
->alias 
= symval
; 
 673   nsyms
--;                      /* symbol and symval combined are only one symbol */ 
 677 /* parse what comes after %start */ 
 680 parse_start_decl (void) 
 683     warn(_("multiple %start declarations")); 
 684   if (lex() != IDENTIFIER
) 
 685     warn(_("invalid %start declaration")); 
 695 /* read in a %type declaration and record its information for get_type_name to access */ 
 698 parse_type_decl (void) 
 703   if (lex() != TYPENAME
) 
 705       warn(_("%type declaration has no <typename>")); 
 710   k 
= strlen(token_buffer
); 
 711   name 
= NEW2(k 
+ 1, char); 
 712   strcpy(name
, token_buffer
); 
 717       int tmp_char 
= ungetc (skip_white_space (), finput
); 
 722         fatals ("Premature EOF after %s", token_buffer
); 
 734           if (symval
->type_name 
== NULL
) 
 735             symval
->type_name 
= name
; 
 736           else if (strcmp(name
, symval
->type_name
) != 0) 
 737             warns(_("type redeclaration for %s"), symval
->tag
); 
 742           warns(_("invalid %%type declaration due to item: `%s'"), token_buffer
); 
 750 /* read in a %left, %right or %nonassoc declaration and record its information.  */ 
 751 /* assoc is either LEFT_ASSOC, RIGHT_ASSOC or NON_ASSOC.  */ 
 754 parse_assoc_decl (int assoc
) 
 757   register char *name 
= NULL
; 
 758   register int prev 
= 0; 
 760   lastprec
++;  /* Assign a new precedence level, never 0.  */ 
 765       int tmp_char 
= ungetc (skip_white_space (), finput
); 
 770         fatals ("Premature EOF after %s", token_buffer
); 
 778           k 
= strlen(token_buffer
); 
 779           name 
= NEW2(k 
+ 1, char); 
 780           strcpy(name
, token_buffer
); 
 787           if (symval
->prec 
!= 0) 
 788             warns(_("redefining precedence of %s"), symval
->tag
); 
 789           symval
->prec 
= lastprec
; 
 790           symval
->assoc 
= assoc
; 
 791           if (symval
->class == SNTERM
) 
 792             warns(_("symbol %s redefined"), symval
->tag
); 
 793           symval
->class = STOKEN
; 
 795             { /* record the type, if one is specified */ 
 796               if (symval
->type_name 
== NULL
) 
 797                 symval
->type_name 
= name
; 
 798               else if (strcmp(name
, symval
->type_name
) != 0) 
 799                 warns(_("type redeclaration for %s"), symval
->tag
); 
 804           if (prev 
== IDENTIFIER
) 
 806               symval
->user_token_number 
= numval
; 
 811               warns(_("invalid text (%s) - number should be after identifier"), 
 821           warns(_("unexpected item: %s"), token_buffer
); 
 832 /* copy the union declaration into fattrs (and fdefines), 
 833    where it is made into the 
 834    definition of YYSTYPE, the type of elements of the parser value stack.  */ 
 837 parse_union_decl (void) 
 841   register int in_comment
; 
 845     warn(_("multiple %union declarations")); 
 850     fprintf(fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
); 
 852     fprintf(fattrs
, "\n"); 
 854   fprintf(fattrs
, "typedef union"); 
 856     fprintf(fdefines
, "typedef union"); 
 877           if (c 
!= '*' && c 
!= '/') 
 884               cplus_comment 
= (c 
== '/'); 
 903                     fatal(_("unterminated comment at end of file")); 
 905                   if (!cplus_comment 
&& c 
== '*') 
 929             warn (_("unmatched close-brace (`}')")); 
 933               fprintf(fattrs
, " YYSTYPE;\n"); 
 935                 fprintf(fdefines
, " YYSTYPE;\n"); 
 936               /* JF don't choke on trailing semi */ 
 937               c
=skip_white_space(); 
 938               if(c
!=';') ungetc(c
,finput
); 
 947 /* parse the declaration %expect N which says to expect N 
 948    shift-reduce conflicts.  */ 
 951 parse_expect_decl (void) 
 958   while (c 
== ' ' || c 
== '\t') 
 962   while (c 
>= '0' && c 
<= '9') 
 972   if (count 
<= 0 || count 
> 10) 
 973         warn(_("argument of %expect is not an integer")); 
 974   expected_conflicts 
= atoi (buffer
); 
 977 /* that's all of parsing the declaration section */ 
 979 /* Get the data type (alternative in the union) of the value for symbol n in rule rule.  */ 
 982 get_type_name (int n
, symbol_list 
*rule
) 
 984   static char *msg 
= N_("invalid $ value"); 
 987   register symbol_list 
*rp
; 
1001       if (rp 
== NULL 
|| rp
->sym 
== NULL
) 
1009   return (rp
->sym
->type_name
); 
1014 /* After `%guard' is seen in the input file, copy the actual guard 
1015    into the guards file.  If the guard is followed by an action, copy 
1016    that into the actions file.  STACK_OFFSET is the number of values 
1017    in the current rule so far, which says where to find `$0' with 
1018    respect to the top of the stack, for the simple parser in which the 
1019    stack is not popped until after the guard is run.  */ 
1022 copy_guard (symbol_list 
*rule
, int stack_offset
) 
1027   register char *type_name
; 
1030   /* offset is always 0 if parser has already popped the stack pointer */ 
1031   if (semantic_parser
) stack_offset 
= 0; 
1033   fprintf(fguard
, "\ncase %d:\n", nrules
); 
1035     fprintf (fguard
, "#line %d \"%s\"\n", lineno
, infile
); 
1041   while (brace_flag 
? (count 
> 0) : (c 
!= ';')) 
1062               warn(_("unmatched right brace (`}')")); 
1063               c 
= getc(finput
); /* skip it */ 
1069           copy_string (finput
, fguard
, c
); 
1075           if (c 
!= '*' && c 
!= '/') 
1077           copy_comment (finput
, fguard
, c
); 
1086               register char *cp 
= token_buffer
; 
1088               while ((c 
= getc(finput
)) != '>' && c 
> 0) 
1090                   if (cp 
== token_buffer 
+ maxtoken
) 
1091                     cp 
= grow_token_buffer(cp
); 
1096               type_name 
= token_buffer
; 
1103               fprintf(fguard
, "yyval"); 
1105                 type_name 
= rule
->sym
->type_name
; 
1107                 fprintf(fguard
, ".%s", type_name
); 
1108               if(!type_name 
&& typed
) 
1109                 warns(_("$$ of `%s' has no declared type"), rule
->sym
->tag
); 
1111           else if (isdigit(c
) || c 
== '-') 
1114               n 
= read_signed_integer (finput
); 
1117               if (!type_name 
&& n 
> 0) 
1118                 type_name 
= get_type_name(n
, rule
); 
1120               fprintf(fguard
, "yyvsp[%d]", n 
- stack_offset
); 
1122                 fprintf(fguard
, ".%s", type_name
); 
1123               if (!type_name 
&& typed
) 
1124                 warnss (_("$%s of `%s' has no declared type"), 
1125                         int_to_string(n
), rule
->sym
->tag
); 
1129             warns(_("$%s is invalid"), printable_version(c
)); 
1136               fprintf (fguard
, "yyloc"); 
1139           else if (isdigit(c
) || c 
== '-') 
1142               n 
= read_signed_integer (finput
); 
1144               fprintf (fguard
, "yylsp[%d]", n 
- stack_offset
); 
1150               warns (_("@%s is invalid"), printable_version (c
)); 
1156           fatal (_("unterminated %%guard clause")); 
1162       if (c 
!= '}' || count 
!= 0) 
1166   c 
= skip_white_space(); 
1168   fprintf(fguard
, ";\n    break;}"); 
1170     copy_action(rule
, stack_offset
); 
1173       c 
= getc(finput
); /* why not skip_white_space -wjh */ 
1175         copy_action(rule
, stack_offset
); 
1183 /* Assuming that a `{' has just been seen, copy everything up to the 
1184    matching `}' into the actions file.  STACK_OFFSET is the number of 
1185    values in the current rule so far, which says where to find `$0' 
1186    with respect to the top of the stack.  */ 
1189 copy_action (symbol_list 
*rule
, int stack_offset
) 
1194   register char *type_name
; 
1196   /* offset is always 0 if parser has already popped the stack pointer */ 
1197   if (semantic_parser
) 
1200   fprintf (faction
, "\ncase %d:\n", nrules
); 
1202     fprintf (faction
, "#line %d \"%s\"\n", lineno
, infile
); 
1203   putc ('{', faction
); 
1226               copy_string (finput
, faction
, c
); 
1232               if (c 
!= '*' && c 
!= '/') 
1234               copy_comment (finput
, faction
, c
); 
1243                   register char *cp 
= token_buffer
; 
1245                   while ((c 
= getc(finput
)) != '>' && c 
> 0) 
1247                       if (cp 
== token_buffer 
+ maxtoken
) 
1248                         cp 
= grow_token_buffer(cp
); 
1253                   type_name 
= token_buffer
; 
1254                   value_components_used 
= 1; 
1260                   fprintf(faction
, "yyval"); 
1262                     type_name 
= get_type_name(0, rule
); 
1264                     fprintf(faction
, ".%s", type_name
); 
1265                   if(!type_name 
&& typed
) 
1266                     warns(_("$$ of `%s' has no declared type"), 
1269               else if (isdigit(c
) || c 
== '-') 
1272                   n 
= read_signed_integer(finput
); 
1275                   if (!type_name 
&& n 
> 0) 
1276                     type_name 
= get_type_name(n
, rule
); 
1278                   fprintf(faction
, "yyvsp[%d]", n 
- stack_offset
); 
1280                     fprintf(faction
, ".%s", type_name
); 
1281                   if(!type_name 
&& typed
) 
1282                     warnss(_("$%s of `%s' has no declared type"), 
1283                                 int_to_string(n
), rule
->sym
->tag
); 
1287                 warns(_("$%s is invalid"), printable_version(c
)); 
1295                   fprintf (faction
, "yyloc"); 
1298               else if (isdigit(c
) || c 
== '-') 
1301                   n 
= read_signed_integer (finput
); 
1303                   fprintf (faction
, "yylsp[%d]", n 
- stack_offset
); 
1309                   warns (_("@%s is invalid"), printable_version (c
)); 
1315               fatal(_("unmatched `{'")); 
1324       /* above loop exits when c is '}' */ 
1333   fprintf(faction
, ";\n    break;}"); 
1338 /* generate a dummy symbol, a nonterminal, 
1339 whose name cannot conflict with the user's names. */ 
1344   register bucket 
*sym
; 
1346   sprintf (token_buffer
, "@%d", ++gensym_count
); 
1347   sym 
= getsym(token_buffer
); 
1348   sym
->class = SNTERM
; 
1349   sym
->value 
= nvars
++; 
1353 /* Parse the input grammar into a one symbol_list structure. 
1354 Each rule is represented by a sequence of symbols: the left hand side 
1355 followed by the contents of the right hand side, followed by a null pointer 
1356 instead of a symbol to terminate the rule. 
1357 The next symbol is the lhs of the following rule. 
1359 All guards and actions are copied out to the appropriate files, 
1360 labelled by the rule number they apply to.  */ 
1366   register bucket 
*lhs 
= NULL
; 
1367   register symbol_list 
*p
; 
1368   register symbol_list 
*p1
; 
1369   register bucket 
*bp
; 
1371   symbol_list 
*crule
;   /* points to first symbol_list of current rule.  */ 
1372                         /* its symbol is the lhs of the rule.   */ 
1373   symbol_list 
*crule1
;  /* points to the symbol_list preceding crule.  */ 
1379   while (t 
!= TWO_PERCENTS 
&& t 
!= ENDFILE
) 
1381       if (t 
== IDENTIFIER 
|| t 
== BAR
) 
1383           register int actionflag 
= 0; 
1384           int rulelength 
= 0;  /* number of symbols in rhs of this rule so far  */ 
1385           int xactions 
= 0;     /* JF for error checking */ 
1386           bucket 
*first_rhs 
= 0; 
1388           if (t 
== IDENTIFIER
) 
1401                   warn(_("ill-formed rule: initial symbol not followed by colon")); 
1406           if (nrules 
== 0 && t 
== BAR
) 
1408               warn(_("grammar starts with vertical bar")); 
1409               lhs 
= symval
;     /* BOGUS: use a random symval */ 
1411           /* start a new rule and record its lhs.  */ 
1416           record_rule_line (); 
1418           p 
= NEW(symbol_list
); 
1430           /* mark the rule's lhs as a nonterminal if not already so.  */ 
1432           if (lhs
->class == SUNKNOWN
) 
1434               lhs
->class = SNTERM
; 
1438           else if (lhs
->class == STOKEN
) 
1439             warns(_("rule given for %s, which is a token"), lhs
->tag
); 
1441           /* read the rhs of the rule.  */ 
1449                   crule
->ruleprec 
= symval
; 
1453               if (! (t 
== IDENTIFIER 
|| t 
== LEFT_CURLY
)) break; 
1455               /* If next token is an identifier, see if a colon follows it. 
1456                  If one does, exit this rule now.  */ 
1457               if (t 
== IDENTIFIER
) 
1459                   register bucket 
*ssave
; 
1466                   if (t1 
== COLON
) break; 
1468                   if(!first_rhs
)        /* JF */ 
1470                   /* Not followed by colon => 
1471                      process as part of this rule's rhs.  */ 
1474               /* If we just passed an action, that action was in the middle 
1475                  of a rule, so make a dummy rule to reduce it to a 
1479                   register bucket 
*sdummy
; 
1481                   /* Since the action was written out with this rule's */ 
1482                   /* number, we must give the new rule this number */ 
1483                   /* by inserting the new rule before it.  */ 
1485                   /* Make a dummy nonterminal, a gensym.  */ 
1488                   /* Make a new rule, whose body is empty, 
1489                      before the current one, so that the action 
1490                      just read can belong to it.  */ 
1493                   record_rule_line (); 
1494                   p 
= NEW(symbol_list
); 
1499                   crule1 
= NEW(symbol_list
); 
1501                   crule1
->next 
= crule
; 
1503                   /* insert the dummy generated by that rule into this rule.  */ 
1505                   p 
= NEW(symbol_list
); 
1513               if (t 
== IDENTIFIER
) 
1516                   p 
= NEW(symbol_list
); 
1521               else /* handle an action.  */ 
1523                   copy_action(crule
, rulelength
); 
1525                   xactions
++;   /* JF */ 
1528             }   /* end of  read rhs of rule */ 
1530           /* Put an empty link in the list to mark the end of this rule  */ 
1531           p 
= NEW(symbol_list
); 
1537               warn(_("two @prec's in a row")); 
1539               crule
->ruleprec 
= symval
; 
1544               if (! semantic_parser
) 
1545                 warn(_("%%guard present but %%semantic_parser not specified")); 
1547               copy_guard(crule
, rulelength
); 
1550           else if (t 
== LEFT_CURLY
) 
1552                 /* This case never occurs -wjh */ 
1554                 warn(_("two actions at end of one rule")); 
1555               copy_action(crule
, rulelength
); 
1557               xactions
++;       /* -wjh */ 
1560           /* If $$ is being set in default way, warn if any type 
1564                    && lhs
->type_name 
!= first_rhs
->type_name
) 
1566               if (lhs
->type_name 
== 0 
1567                   || first_rhs
->type_name 
== 0 
1568                   || strcmp(lhs
->type_name
,first_rhs
->type_name
)) 
1569                 warnss(_("type clash (`%s' `%s') on default action"), 
1570                        lhs
->type_name 
? lhs
->type_name 
: "", 
1571                        first_rhs
->type_name 
? first_rhs
->type_name 
: ""); 
1573           /* Warn if there is no default for $$ but we need one.  */ 
1574           else if (!xactions 
&& !first_rhs 
&& lhs
->type_name 
!= 0) 
1575             warn(_("empty rule for typed nonterminal, and no action")); 
1580   /* these things can appear as alternatives to rules.  */ 
1582         a) none of the documentation allows them 
1583         b) most of them scan forward until finding a next % 
1584                 thus they may swallow lots of intervening rules 
1586       else if (t 
== TOKEN
) 
1588           parse_token_decl(STOKEN
, SNTERM
); 
1591       else if (t 
== NTERM
) 
1593           parse_token_decl(SNTERM
, STOKEN
); 
1600       else if (t 
== UNION
) 
1605       else if (t 
== EXPECT
) 
1607           parse_expect_decl(); 
1610       else if (t 
== START
) 
1619           warns(_("invalid input: %s"), token_buffer
); 
1624   /* grammar has been read.  Do some checking */ 
1626   if (nsyms 
> MAXSHORT
) 
1627     fatals(_("too many symbols (tokens plus nonterminals); maximum %s"), 
1628            int_to_string(MAXSHORT
)); 
1630     fatal(_("no rules in the input grammar")); 
1632   if (typed 
== 0        /* JF put out same default YYSTYPE as YACC does */ 
1633       && !value_components_used
) 
1635       /* We used to use `unsigned long' as YYSTYPE on MSDOS, 
1636          but it seems better to be consistent. 
1637          Most programs should declare their own type anyway.  */ 
1638       fprintf(fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n"); 
1640         fprintf(fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n"); 
1643   /* Report any undefined symbols and consider them nonterminals.  */ 
1645   for (bp 
= firstsymbol
; bp
; bp 
= bp
->next
) 
1646     if (bp
->class == SUNKNOWN
) 
1648         warns(_("symbol %s is used, but is not defined as a token and has no rules"), 
1651         bp
->value 
= nvars
++; 
1654   ntokens 
= nsyms 
- nvars
; 
1659 record_rule_line (void) 
1661   /* Record each rule's source line number in rline table.  */ 
1663   if (nrules 
>= rline_allocated
) 
1665       rline_allocated 
= nrules 
* 2; 
1666       rline 
= (short *) xrealloc ((char *) rline
, 
1667                                   rline_allocated 
* sizeof (short)); 
1669   rline
[nrules
] = lineno
; 
1674 /* read in a %type declaration and record its information for get_type_name to access */ 
1675 /* this is unused.  it is only called from the #if 0 part of readgram */ 
1681   register char *name
; 
1687       warn(_("ill-formed %type declaration")); 
1691   k 
= strlen(token_buffer
); 
1692   name 
= NEW2(k 
+ 1, char); 
1693   strcpy(name
, token_buffer
); 
1708           if (symval
->type_name 
== NULL
) 
1709             symval
->type_name 
= name
; 
1710           else if (strcmp(name
, symval
->type_name
) != 0) 
1711             warns(_("type redeclaration for %s"), symval
->tag
); 
1723 /* Assign symbol numbers, and write definition of token names into 
1724    fdefines.  Set up vectors tags and sprec of names and precedences 
1730   register bucket 
*bp
; 
1731   register int tokno 
= 1; 
1733   register int last_user_token_number
; 
1735   /* int lossage = 0; JF set but not used */ 
1737   tags 
= NEW2(nsyms 
+ 1, char *); 
1739   user_toknums 
= NEW2(nsyms 
+ 1, int); 
1740   user_toknums
[0] = 0; 
1742   sprec 
= NEW2(nsyms
, short); 
1743   sassoc 
= NEW2(nsyms
, short); 
1745   max_user_token_number 
= 256; 
1746   last_user_token_number 
= 256; 
1748   for (bp 
= firstsymbol
; bp
; bp 
= bp
->next
) 
1750       if (bp
->class == SNTERM
) 
1752           bp
->value 
+= ntokens
; 
1756           /* this symbol and its alias are a single token defn. 
1757              allocate a tokno, and assign to both check agreement of 
1758              ->prec and ->assoc fields and make both the same */ 
1760             bp
->value 
= bp
->alias
->value 
= tokno
++; 
1762           if (bp
->prec 
!= bp
->alias
->prec
) 
1764               if (bp
->prec 
!= 0 && bp
->alias
->prec 
!= 0 
1765                   && bp
->user_token_number 
== SALIAS
) 
1766                 warnss(_("conflicting precedences for %s and %s"), 
1767                        bp
->tag
, bp
->alias
->tag
); 
1769                 bp
->alias
->prec 
= bp
->prec
; 
1771                 bp
->prec 
= bp
->alias
->prec
; 
1774           if (bp
->assoc 
!= bp
->alias
->assoc
) 
1776             if (bp
->assoc 
!= 0 && bp
->alias
->assoc 
!= 0 
1777                 && bp
->user_token_number 
== SALIAS
) 
1778               warnss(_("conflicting assoc values for %s and %s"), 
1779                      bp
->tag
, bp
->alias
->tag
); 
1781               bp
->alias
->assoc 
= bp
->assoc
; 
1783               bp
->assoc 
= bp
->alias
->assoc
; 
1786           if (bp
->user_token_number 
== SALIAS
) 
1787             continue;  /* do not do processing below for SALIASs */ 
1790       else  /* bp->class == STOKEN */ 
1792           bp
->value 
= tokno
++; 
1795       if (bp
->class == STOKEN
) 
1797           if (translations 
&& !(bp
->user_token_number
)) 
1798             bp
->user_token_number 
= ++last_user_token_number
; 
1799           if (bp
->user_token_number 
> max_user_token_number
) 
1800             max_user_token_number 
= bp
->user_token_number
; 
1803       tags
[bp
->value
] = bp
->tag
; 
1804       user_toknums
[bp
->value
] = bp
->user_token_number
; 
1805       sprec
[bp
->value
] = bp
->prec
; 
1806       sassoc
[bp
->value
] = bp
->assoc
; 
1814       token_translations 
= NEW2(max_user_token_number
+1, short); 
1816       /* initialize all entries for literal tokens to 2, the internal 
1817          token number for $undefined., which represents all invalid 
1819       for (i 
= 0; i 
<= max_user_token_number
; i
++) 
1820         token_translations
[i
] = 2; 
1822       for (bp 
= firstsymbol
; bp
; bp 
= bp
->next
) 
1824           if (bp
->value 
>= ntokens
) 
1825             continue;             /* non-terminal */ 
1826           if (bp
->user_token_number 
== SALIAS
) 
1828           if (token_translations
[bp
->user_token_number
] != 2) 
1829             warnsss(_("tokens %s and %s both assigned number %s"), 
1830                     tags
[token_translations
[bp
->user_token_number
]], 
1832                     int_to_string(bp
->user_token_number
)); 
1833           token_translations
[bp
->user_token_number
] = bp
->value
; 
1837   error_token_number 
= errtoken
->value
; 
1840     output_token_defines(ftable
); 
1842   if (startval
->class == SUNKNOWN
) 
1843     fatals(_("the start symbol %s is undefined"), startval
->tag
); 
1844   else if (startval
->class == STOKEN
) 
1845     fatals(_("the start symbol %s is a token"), startval
->tag
); 
1847   start_symbol 
= startval
->value
; 
1851       output_token_defines(fdefines
); 
1855           if (spec_name_prefix
) 
1856             fprintf(fdefines
, "\nextern YYSTYPE %slval;\n", spec_name_prefix
); 
1858             fprintf(fdefines
, "\nextern YYSTYPE yylval;\n"); 
1861       if (semantic_parser
) 
1862         for (i 
= ntokens
; i 
< nsyms
; i
++) 
1864             /* don't make these for dummy nonterminals made by gensym.  */ 
1865             if (*tags
[i
] != '@') 
1866               fprintf(fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
); 
1869       /* `fdefines' is now a temporary file, so we need to copy its 
1870          contents in `done', so we can't close it here.  */ 
1877 /* For named tokens, but not literal ones, define the name.  The value 
1878    is the user token number.  */ 
1881 output_token_defines (FILE *file
) 
1884   register char *cp
, *symbol
; 
1887   for (bp 
= firstsymbol
; bp
; bp 
= bp
->next
) 
1889       symbol 
= bp
->tag
;                         /* get symbol */ 
1891       if (bp
->value 
>= ntokens
) continue; 
1892       if (bp
->user_token_number 
== SALIAS
) continue; 
1893       if ('\'' == *symbol
) continue;    /* skip literal character */ 
1894       if (bp 
== errtoken
) continue;     /* skip error token */ 
1895       if ('\"' == *symbol
) 
1897                 /* use literal string only if given a symbol with an alias */ 
1899                         symbol 
= bp
->alias
->tag
; 
1904       /* Don't #define nonliteral tokens whose names contain periods.  */ 
1906       while ((c 
= *cp
++) && c 
!= '.'); 
1907       if (c 
!= '\0')  continue; 
1909       fprintf(file
, "#define\t%s\t%d\n", symbol
, 
1910                 ((translations 
&& ! rawtoknumflag
) 
1911                         ? bp
->user_token_number
 
1913       if (semantic_parser
) 
1914         fprintf(file
, "#define\tT%s\t%d\n", symbol
, bp
->value
); 
1922 /* convert the rules into the representation using rrhs, rlhs and ritems.  */ 
1927   register int itemno
; 
1928   register int ruleno
; 
1929   register symbol_list 
*p
; 
1930 /*  register bucket *bp; JF unused */ 
1934   ritem 
= NEW2(nitems 
+ 1, short); 
1935   rlhs 
= NEW2(nrules
, short) - 1; 
1936   rrhs 
= NEW2(nrules
, short) - 1; 
1937   rprec 
= NEW2(nrules
, short) - 1; 
1938   rprecsym 
= NEW2(nrules
, short) - 1; 
1939   rassoc 
= NEW2(nrules
, short) - 1; 
1947       rlhs
[ruleno
] = p
->sym
->value
; 
1948       rrhs
[ruleno
] = itemno
; 
1949       ruleprec 
= p
->ruleprec
; 
1954           ritem
[itemno
++] = p
->sym
->value
; 
1955           /* A rule gets by default the precedence and associativity 
1956              of the last token in it.  */ 
1957           if (p
->sym
->class == STOKEN
) 
1959               rprec
[ruleno
] = p
->sym
->prec
; 
1960               rassoc
[ruleno
] = p
->sym
->assoc
; 
1965       /* If this rule has a %prec, 
1966          the specified symbol's precedence replaces the default.  */ 
1969           rprec
[ruleno
] = ruleprec
->prec
; 
1970           rassoc
[ruleno
] = ruleprec
->assoc
; 
1971           rprecsym
[ruleno
] = ruleprec
->value
; 
1974       ritem
[itemno
++] = -ruleno
; 
1983 /* Read a signed integer from STREAM and return its value.  */ 
1986 read_signed_integer (FILE *stream
) 
1988   register int c 
= getc(stream
); 
1989   register int sign 
= 1; 
2000       n 
= 10*n 
+ (c 
- '0');