]> git.saurik.com Git - bison.git/blobdiff - src/reader.c
* src/gram.h (associativity): New enum type which replaces the
[bison.git] / src / reader.c
index ded07a708ef7fd97b5fcb97a06cf4f4af190d8a2..bd13e044e70baf9c8bee222c1a64760932edbd1a 100644 (file)
@@ -23,7 +23,7 @@
 #include "system.h"
 #include "getargs.h"
 #include "files.h"
 #include "system.h"
 #include "getargs.h"
 #include "files.h"
-#include "alloc.h"
+#include "xalloc.h"
 #include "symtab.h"
 #include "lex.h"
 #include "gram.h"
 #include "symtab.h"
 #include "lex.h"
 #include "gram.h"
 
 extern char *printable_version PARAMS ((int));
 
 
 extern char *printable_version PARAMS ((int));
 
-#define        LTYPESTR        "\
-\n\
-#ifndef YYLTYPE\n\
-typedef\n\
-  struct yyltype\n\
-\
-    {\n\
-      int timestamp;\n\
-      int first_line;\n\
-      int first_column;\
-\n\
-      int last_line;\n\
-      int last_column;\n\
-      char *text;\n\
-   }\n\
-\
-  yyltype;\n\
-\n\
-#define YYLTYPE yyltype\n\
-#endif\n\
-\n"
-
 /* Number of slots allocated (but not necessarily used yet) in `rline'  */
 static int rline_allocated;
 
 /* Number of slots allocated (but not necessarily used yet) in `rline'  */
 static int rline_allocated;
 
@@ -78,11 +56,14 @@ static bucket *startval;
    they must be unions.  */
 static int value_components_used;
 
    they must be unions.  */
 static int value_components_used;
 
-static int typed;              /* nonzero if %union has been seen.  */
+/* Nonzero if %union has been seen.  */
+static int typed;
 
 
-static int lastprec;           /* incremented for each %left, %right or %nonassoc seen */
+/* Incremented for each %left, %right or %nonassoc seen */
+static int lastprec;
 
 
-static int gensym_count;       /* incremented for each generated symbol */
+/* Incremented for each generated symbol */
+static int gensym_count;
 
 static bucket *errtoken;
 static bucket *undeftoken;
 
 static bucket *errtoken;
 static bucket *undeftoken;
@@ -358,14 +339,14 @@ copy_definition (void)
 }
 
 
 }
 
 
-/*-----------------------------------------------------------------.
-| Parse what comes after %token or %nterm.  For %token, what_is is |
-| STOKEN and what_is_not is SNTERM.  For %nterm, the arguments are |
-| reversed.                                                        |
-`-----------------------------------------------------------------*/
+/*-------------------------------------------------------------------.
+| Parse what comes after %token or %nterm.  For %token, WHAT_IS is   |
+| token_sym and WHAT_IS_NOT is nterm_sym.  For %nterm, the arguments |
+| are reversed.                                                      |
+`-------------------------------------------------------------------*/
 
 static void
 
 static void
-parse_token_decl (int what_is, int what_is_not)
+parse_token_decl (symbol_class what_is, symbol_class what_is_not)
 {
   int token = 0;
   char *typename = 0;
 {
   int token = 0;
   char *typename = 0;
@@ -390,7 +371,7 @@ parse_token_decl (int what_is, int what_is_not)
       if (token == TYPENAME)
        {
          k = strlen (token_buffer);
       if (token == TYPENAME)
        {
          k = strlen (token_buffer);
-         typename = NEW2 (k + 1, char);
+         typename = XCALLOC (char, k + 1);
          strcpy (typename, token_buffer);
          value_components_used = 1;
          symbol = NULL;
          strcpy (typename, token_buffer);
          value_components_used = 1;
          symbol = NULL;
@@ -398,7 +379,7 @@ parse_token_decl (int what_is, int what_is_not)
       else if (token == IDENTIFIER && *symval->tag == '\"' && symbol)
        {
          translations = 1;
       else if (token == IDENTIFIER && *symval->tag == '\"' && symbol)
        {
          translations = 1;
-         symval->class = STOKEN;
+         symval->class = token_sym;
          symval->type_name = typename;
          symval->user_token_number = symbol->user_token_number;
          symbol->user_token_number = SALIAS;
          symval->type_name = typename;
          symval->user_token_number = symbol->user_token_number;
          symbol->user_token_number = SALIAS;
@@ -417,7 +398,7 @@ parse_token_decl (int what_is, int what_is_not)
          if (symbol->class == what_is_not)
            complain (_("symbol %s redefined"), symbol->tag);
          symbol->class = what_is;
          if (symbol->class == what_is_not)
            complain (_("symbol %s redefined"), symbol->tag);
          symbol->class = what_is;
-         if (what_is == SNTERM && oldclass != SNTERM)
+         if (what_is == nterm_sym && oldclass != nterm_sym)
            symbol->value = nvars++;
 
          if (typename)
            symbol->value = nvars++;
 
          if (typename)
@@ -436,7 +417,7 @@ parse_token_decl (int what_is, int what_is_not)
       else
        {
          complain (_("`%s' is invalid in %s"),
       else
        {
          complain (_("`%s' is invalid in %s"),
-                   token_buffer, (what_is == STOKEN) ? "%token" : "%nterm");
+                   token_buffer, (what_is == token_sym) ? "%token" : "%nterm");
          skip_to_char ('%');
        }
     }
          skip_to_char ('%');
        }
     }
@@ -444,7 +425,9 @@ parse_token_decl (int what_is, int what_is_not)
 }
 
 
 }
 
 
-/* Parse what comes after %start */
+/*------------------------------.
+| Parse what comes after %start |
+`------------------------------*/
 
 static void
 parse_start_decl (void)
 
 static void
 parse_start_decl (void)
@@ -516,7 +499,7 @@ parse_type_decl (void)
     }
 
   k = strlen (token_buffer);
     }
 
   k = strlen (token_buffer);
-  name = NEW2 (k + 1, char);
+  name = XCALLOC (char, k + 1);
   strcpy (name, token_buffer);
 
   for (;;)
   strcpy (name, token_buffer);
 
   for (;;)
@@ -556,11 +539,13 @@ parse_type_decl (void)
 
 
 
 
 
 
-/* read in a %left, %right or %nonassoc declaration and record its information.  */
-/* assoc is either LEFT_ASSOC, RIGHT_ASSOC or NON_ASSOC.  */
+/*----------------------------------------------------------------.
+| Read in a %left, %right or %nonassoc declaration and record its |
+| information.                                                    |
+`----------------------------------------------------------------*/
 
 static void
 
 static void
-parse_assoc_decl (int assoc)
+parse_assoc_decl (associativity assoc)
 {
   int k;
   char *name = NULL;
 {
   int k;
   char *name = NULL;
@@ -585,7 +570,7 @@ parse_assoc_decl (int assoc)
 
        case TYPENAME:
          k = strlen (token_buffer);
 
        case TYPENAME:
          k = strlen (token_buffer);
-         name = NEW2 (k + 1, char);
+         name = XCALLOC (char, k + 1);
          strcpy (name, token_buffer);
          break;
 
          strcpy (name, token_buffer);
          break;
 
@@ -597,9 +582,9 @@ parse_assoc_decl (int assoc)
            complain (_("redefining precedence of %s"), symval->tag);
          symval->prec = lastprec;
          symval->assoc = assoc;
            complain (_("redefining precedence of %s"), symval->tag);
          symval->prec = lastprec;
          symval->assoc = assoc;
-         if (symval->class == SNTERM)
+         if (symval->class == nterm_sym)
            complain (_("symbol %s redefined"), symval->tag);
            complain (_("symbol %s redefined"), symval->tag);
-         symval->class = STOKEN;
+         symval->class = token_sym;
          if (name)
            {                   /* record the type, if one is specified */
              if (symval->type_name == NULL)
          if (name)
            {                   /* record the type, if one is specified */
              if (symval->type_name == NULL)
@@ -639,9 +624,11 @@ token_buffer);
 
 
 
 
 
 
-/* copy the union declaration into fattrs (and fdefines),
-   where it is made into the
-   definition of YYSTYPE, the type of elements of the parser value stack.  */
+/*-------------------------------------------------------------------.
+| Copy the union declaration into fattrs (and fdefines), where it is |
+| made into the definition of YYSTYPE, the type of elements of the   |
+| parser value stack.                                                |
+`-------------------------------------------------------------------*/
 
 static void
 parse_union_decl (void)
 
 static void
 parse_union_decl (void)
@@ -710,8 +697,11 @@ parse_union_decl (void)
     }
 }
 
     }
 }
 
-/* parse the declaration %expect N which says to expect N
-   shift-reduce conflicts.  */
+
+/*-------------------------------------------------------.
+| Parse the declaration %expect N which says to expect N |
+| shift-reduce conflicts.                                |
+`-------------------------------------------------------*/
 
 static void
 parse_expect_decl (void)
 
 static void
 parse_expect_decl (void)
@@ -774,7 +764,7 @@ parse_thong_decl (void)
   if (token == TYPENAME)
     {
       k = strlen (token_buffer);
   if (token == TYPENAME)
     {
       k = strlen (token_buffer);
-      typename = NEW2 (k + 1, char);
+      typename = XCALLOC (char, k + 1);
       strcpy (typename, token_buffer);
       value_components_used = 1;
       token = lex ();          /* fetch first token */
       strcpy (typename, token_buffer);
       value_components_used = 1;
       token = lex ();          /* fetch first token */
@@ -789,7 +779,7 @@ parse_thong_decl (void)
       skip_to_char ('%');
       return;
     }
       skip_to_char ('%');
       return;
     }
-  symval->class = STOKEN;
+  symval->class = token_sym;
   symval->type_name = typename;
   symval->user_token_number = SALIAS;
   symbol = symval;
   symval->type_name = typename;
   symval->user_token_number = SALIAS;
   symbol = symval;
@@ -812,7 +802,7 @@ parse_thong_decl (void)
       skip_to_char ('%');
       return;
     }
       skip_to_char ('%');
       return;
     }
-  symval->class = STOKEN;
+  symval->class = token_sym;
   symval->type_name = typename;
   symval->user_token_number = usrtoknum;
 
   symval->type_name = typename;
   symval->user_token_number = usrtoknum;
 
@@ -822,6 +812,7 @@ parse_thong_decl (void)
   nsyms--;                     /* symbol and symval combined are only one symbol */
 }
 
   nsyms--;                     /* symbol and symval combined are only one symbol */
 }
 
+
 /*----------------------------------------------------------------.
 | Read from finput until `%%' is seen.  Discard the `%%'.  Handle |
 | any `%' declarations, and copy the contents of any `%{ ... %}'  |
 /*----------------------------------------------------------------.
 | Read from finput until `%%' is seen.  Discard the `%%'.  Handle |
 | any `%' declarations, and copy the contents of any `%{ ... %}'  |
@@ -852,11 +843,11 @@ read_declarations (void)
              break;
 
            case TOKEN:
              break;
 
            case TOKEN:
-             parse_token_decl (STOKEN, SNTERM);
+             parse_token_decl (token_sym, nterm_sym);
              break;
 
            case NTERM:
              break;
 
            case NTERM:
-             parse_token_decl (SNTERM, STOKEN);
+             parse_token_decl (nterm_sym, token_sym);
              break;
 
            case TYPE:
              break;
 
            case TYPE:
@@ -877,16 +868,17 @@ read_declarations (void)
            case THONG:
              parse_thong_decl ();
              break;
            case THONG:
              parse_thong_decl ();
              break;
+
            case LEFT:
            case LEFT:
-             parse_assoc_decl (LEFT_ASSOC);
+             parse_assoc_decl (left_assoc);
              break;
 
            case RIGHT:
              break;
 
            case RIGHT:
-             parse_assoc_decl (RIGHT_ASSOC);
+             parse_assoc_decl (right_assoc);
              break;
 
            case NONASSOC:
              break;
 
            case NONASSOC:
-             parse_assoc_decl (NON_ASSOC);
+             parse_assoc_decl (non_assoc);
              break;
 
            case SEMANTIC_PARSER:
              break;
 
            case SEMANTIC_PARSER:
@@ -1216,8 +1208,7 @@ record_rule_line (void)
   if (nrules >= rline_allocated)
     {
       rline_allocated = nrules * 2;
   if (nrules >= rline_allocated)
     {
       rline_allocated = nrules * 2;
-      rline = (short *) xrealloc ((char *) rline,
-                                 rline_allocated * sizeof (short));
+      rline = XREALLOC (rline, short, rline_allocated);
     }
   rline[nrules] = lineno;
 }
     }
   rline[nrules] = lineno;
 }
@@ -1235,7 +1226,7 @@ gensym (void)
 
   sprintf (token_buffer, "@%d", ++gensym_count);
   sym = getsym (token_buffer);
 
   sprintf (token_buffer, "@%d", ++gensym_count);
   sym = getsym (token_buffer);
-  sym->class = SNTERM;
+  sym->class = nterm_sym;
   sym->value = nvars++;
   return sym;
 }
   sym->value = nvars++;
   return sym;
 }
@@ -1263,7 +1254,7 @@ get_type (void)
     }
 
   k = strlen (token_buffer);
     }
 
   k = strlen (token_buffer);
-  name = NEW2 (k + 1, char);
+  name = XCALLOC (char, k + 1);
   strcpy (name, token_buffer);
 
   for (;;)
   strcpy (name, token_buffer);
 
   for (;;)
@@ -1361,7 +1352,7 @@ readgram (void)
 
          record_rule_line ();
 
 
          record_rule_line ();
 
-         p = NEW (symbol_list);
+         p = XCALLOC (symbol_list, 1);
          p->sym = lhs;
 
          crule1 = p1;
          p->sym = lhs;
 
          crule1 = p1;
@@ -1375,13 +1366,13 @@ readgram (void)
 
          /* mark the rule's lhs as a nonterminal if not already so.  */
 
 
          /* mark the rule's lhs as a nonterminal if not already so.  */
 
-         if (lhs->class == SUNKNOWN)
+         if (lhs->class == unknown_sym)
            {
            {
-             lhs->class = SNTERM;
+             lhs->class = nterm_sym;
              lhs->value = nvars;
              nvars++;
            }
              lhs->value = nvars;
              nvars++;
            }
-         else if (lhs->class == STOKEN)
+         else if (lhs->class == token_sym)
            complain (_("rule given for %s, which is a token"), lhs->tag);
 
          /* read the rhs of the rule.  */
            complain (_("rule given for %s, which is a token"), lhs->tag);
 
          /* read the rhs of the rule.  */
@@ -1439,19 +1430,19 @@ readgram (void)
                  nrules++;
                  nitems++;
                  record_rule_line ();
                  nrules++;
                  nitems++;
                  record_rule_line ();
-                 p = NEW (symbol_list);
+                 p = XCALLOC (symbol_list, 1);
                  if (crule1)
                    crule1->next = p;
                  else
                    grammar = p;
                  p->sym = sdummy;
                  if (crule1)
                    crule1->next = p;
                  else
                    grammar = p;
                  p->sym = sdummy;
-                 crule1 = NEW (symbol_list);
+                 crule1 = XCALLOC (symbol_list, 1);
                  p->next = crule1;
                  crule1->next = crule;
 
                  /* insert the dummy generated by that rule into this rule.  */
                  nitems++;
                  p->next = crule1;
                  crule1->next = crule;
 
                  /* insert the dummy generated by that rule into this rule.  */
                  nitems++;
-                 p = NEW (symbol_list);
+                 p = XCALLOC (symbol_list, 1);
                  p->sym = sdummy;
                  p1->next = p;
                  p1 = p;
                  p->sym = sdummy;
                  p1->next = p;
                  p1 = p;
@@ -1462,7 +1453,7 @@ readgram (void)
              if (t == IDENTIFIER)
                {
                  nitems++;
              if (t == IDENTIFIER)
                {
                  nitems++;
-                 p = NEW (symbol_list);
+                 p = XCALLOC (symbol_list, 1);
                  p->sym = symval;
                  p1->next = p;
                  p1 = p;
                  p->sym = symval;
                  p1->next = p;
                  p1 = p;
@@ -1477,7 +1468,7 @@ readgram (void)
            }                   /* end of  read rhs of rule */
 
          /* Put an empty link in the list to mark the end of this rule  */
            }                   /* end of  read rhs of rule */
 
          /* Put an empty link in the list to mark the end of this rule  */
-         p = NEW (symbol_list);
+         p = XCALLOC (symbol_list, 1);
          p1->next = p;
          p1 = p;
 
          p1->next = p;
          p1 = p;
 
@@ -1535,12 +1526,12 @@ readgram (void)
 */
       else if (t == TOKEN)
        {
 */
       else if (t == TOKEN)
        {
-         parse_token_decl (STOKEN, SNTERM);
+         parse_token_decl (token_sym, nterm_sym);
          t = lex ();
        }
       else if (t == NTERM)
        {
          t = lex ();
        }
       else if (t == NTERM)
        {
-         parse_token_decl (SNTERM, STOKEN);
+         parse_token_decl (nterm_sym, token_sym);
          t = lex ();
        }
       else if (t == TYPE)
          t = lex ();
        }
       else if (t == TYPE)
@@ -1593,12 +1584,12 @@ readgram (void)
   /* Report any undefined symbols and consider them nonterminals.  */
 
   for (bp = firstsymbol; bp; bp = bp->next)
   /* Report any undefined symbols and consider them nonterminals.  */
 
   for (bp = firstsymbol; bp; bp = bp->next)
-    if (bp->class == SUNKNOWN)
+    if (bp->class == unknown_sym)
       {
        complain (_
                  ("symbol %s is used, but is not defined as a token and has no rules"),
 bp->tag);
       {
        complain (_
                  ("symbol %s is used, but is not defined as a token and has no rules"),
 bp->tag);
-       bp->class = SNTERM;
+       bp->class = nterm_sym;
        bp->value = nvars++;
       }
 
        bp->value = nvars++;
       }
 
@@ -1672,20 +1663,20 @@ packsymbols (void)
 
   /* int lossage = 0; JF set but not used */
 
 
   /* int lossage = 0; JF set but not used */
 
-  tags = NEW2 (nsyms + 1, char *);
+  tags = XCALLOC (char *, nsyms + 1);
   tags[0] = DOLLAR;
   tags[0] = DOLLAR;
-  user_toknums = NEW2 (nsyms + 1, short);
+  user_toknums = XCALLOC (short, nsyms + 1);
   user_toknums[0] = 0;
 
   user_toknums[0] = 0;
 
-  sprec = NEW2 (nsyms, short);
-  sassoc = NEW2 (nsyms, short);
+  sprec = XCALLOC (short, nsyms);
+  sassoc = XCALLOC (short, nsyms);
 
   max_user_token_number = 256;
   last_user_token_number = 256;
 
   for (bp = firstsymbol; bp; bp = bp->next)
     {
 
   max_user_token_number = 256;
   last_user_token_number = 256;
 
   for (bp = firstsymbol; bp; bp = bp->next)
     {
-      if (bp->class == SNTERM)
+      if (bp->class == nterm_sym)
        {
          bp->value += ntokens;
        }
        {
          bp->value += ntokens;
        }
@@ -1725,12 +1716,12 @@ packsymbols (void)
            continue;           /* do not do processing below for SALIASs */
 
        }
            continue;           /* do not do processing below for SALIASs */
 
        }
-      else                     /* bp->class == STOKEN */
+      else                     /* bp->class == token_sym */
        {
          bp->value = tokno++;
        }
 
        {
          bp->value = tokno++;
        }
 
-      if (bp->class == STOKEN)
+      if (bp->class == token_sym)
        {
          if (translations && !(bp->user_token_number))
            bp->user_token_number = ++last_user_token_number;
        {
          if (translations && !(bp->user_token_number))
            bp->user_token_number = ++last_user_token_number;
@@ -1749,7 +1740,7 @@ packsymbols (void)
     {
       int j;
 
     {
       int j;
 
-      token_translations = NEW2 (max_user_token_number + 1, short);
+      token_translations = XCALLOC (short, max_user_token_number + 1);
 
       /* initialize all entries for literal tokens to 2, the internal
          token number for $undefined., which represents all invalid
 
       /* initialize all entries for literal tokens to 2, the internal
          token number for $undefined., which represents all invalid
@@ -1776,9 +1767,9 @@ packsymbols (void)
   if (!noparserflag)
     output_token_defines (ftable);
 
   if (!noparserflag)
     output_token_defines (ftable);
 
-  if (startval->class == SUNKNOWN)
+  if (startval->class == unknown_sym)
     fatal (_("the start symbol %s is undefined"), startval->tag);
     fatal (_("the start symbol %s is undefined"), startval->tag);
-  else if (startval->class == STOKEN)
+  else if (startval->class == token_sym)
     fatal (_("the start symbol %s is a token"), startval->tag);
 
   start_symbol = startval->value;
     fatal (_("the start symbol %s is a token"), startval->tag);
 
   start_symbol = startval->value;
@@ -1827,12 +1818,12 @@ packgram (void)
 
   bucket *ruleprec;
 
 
   bucket *ruleprec;
 
-  ritem = NEW2 (nitems + 1, short);
-  rlhs = NEW2 (nrules, short) - 1;
-  rrhs = NEW2 (nrules, short) - 1;
-  rprec = NEW2 (nrules, short) - 1;
-  rprecsym = NEW2 (nrules, short) - 1;
-  rassoc = NEW2 (nrules, short) - 1;
+  ritem = XCALLOC (short, nitems + 1);
+  rlhs = XCALLOC (short, nrules) - 1;
+  rrhs = XCALLOC (short, nrules) - 1;
+  rprec = XCALLOC (short, nrules) - 1;
+  rprecsym = XCALLOC (short, nrules) - 1;
+  rassoc = XCALLOC (short, nrules) - 1;
 
   itemno = 0;
   ruleno = 1;
 
   itemno = 0;
   ruleno = 1;
@@ -1850,7 +1841,7 @@ packgram (void)
          ritem[itemno++] = p->sym->value;
          /* A rule gets by default the precedence and associativity
             of the last token in it.  */
          ritem[itemno++] = p->sym->value;
          /* A rule gets by default the precedence and associativity
             of the last token in it.  */
-         if (p->sym->class == STOKEN)
+         if (p->sym->class == token_sym)
            {
              rprec[ruleno] = p->sym->prec;
              rassoc[ruleno] = p->sym->assoc;
            {
              rprec[ruleno] = p->sym->prec;
              rassoc[ruleno] = p->sym->assoc;
@@ -1906,7 +1897,7 @@ reader (void)
   nrules = 0;
   nitems = 0;
   rline_allocated = 10;
   nrules = 0;
   nitems = 0;
   rline_allocated = 10;
-  rline = NEW2 (rline_allocated, short);
+  rline = XCALLOC (short, rline_allocated);
 
   typed = 0;
   lastprec = 0;
 
   typed = 0;
   lastprec = 0;
@@ -1926,12 +1917,12 @@ reader (void)
   tabinit ();
   /* Construct the error token */
   errtoken = getsym ("error");
   tabinit ();
   /* Construct the error token */
   errtoken = getsym ("error");
-  errtoken->class = STOKEN;
+  errtoken->class = token_sym;
   errtoken->user_token_number = 256;   /* Value specified by POSIX.  */
   /* Construct a token that represents all undefined literal tokens.
      It is always token number 2.  */
   undeftoken = getsym ("$undefined.");
   errtoken->user_token_number = 256;   /* Value specified by POSIX.  */
   /* Construct a token that represents all undefined literal tokens.
      It is always token number 2.  */
   undeftoken = getsym ("$undefined.");
-  undeftoken->class = STOKEN;
+  undeftoken->class = token_sym;
   undeftoken->user_token_number = 2;
 
   /* Read the declaration section.  Copy %{ ... %} groups to FTABLE
   undeftoken->user_token_number = 2;
 
   /* Read the declaration section.  Copy %{ ... %} groups to FTABLE
@@ -1968,9 +1959,31 @@ reader (void)
   free_symtab ();
 }
 
   free_symtab ();
 }
 
+
 void
 reader_output_yylsp (FILE *f)
 {
   if (yylsp_needed)
 void
 reader_output_yylsp (FILE *f)
 {
   if (yylsp_needed)
-    fprintf (f, LTYPESTR);
+    fputs ("\
+\n\
+#ifndef YYLTYPE\n\
+typedef\n\
+  struct yyltype\n\
+\
+    {\n\
+      int timestamp;\n\
+      int first_line;\n\
+      int first_column;\
+\n\
+      int last_line;\n\
+      int last_column;\n\
+      char *text;\n\
+   }\n\
+\
+  yyltype;\n\
+\n\
+#define YYLTYPE yyltype\n\
+#endif\n\
+\n",
+          f);
 }
 }