This commit was manufactured by cvs2svn to create tag 'FreeBSD-release/1.0'.
[unix-history] / gnu / usr.bin / grep / dfa.c
index 08b383d..fc649af 100644 (file)
@@ -1,4 +1,4 @@
-/* dfa.c - determinisitic extended regexp routines for GNU
+/* dfa.c - deterministic extended regexp routines for GNU
    Copyright (C) 1988 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    Copyright (C) 1988 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
 
 /* Written June, 1988 by Mike Haertel
    Modified July, 1988 by Arthur David Olson to assist BMG speedups  */
 
 /* Written June, 1988 by Mike Haertel
    Modified July, 1988 by Arthur David Olson to assist BMG speedups  */
-\f
-#include <stdio.h>
+
 #include <assert.h>
 #include <assert.h>
+#include <ctype.h>
+#include <stdio.h>
+
+#ifdef STDC_HEADERS
+#include <stdlib.h>
+#else
+#include <sys/types.h>
+extern char *calloc(), *malloc(), *realloc();
+extern void free();
+#endif
 
 
-#if defined(USG) || defined(STDC_HEADERS)
+#if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
 #include <string.h>
 #include <string.h>
-#ifndef index
+#undef index
 #define index strchr
 #define index strchr
-#endif
 #else
 #include <strings.h>
 #endif
 
 #else
 #include <strings.h>
 #endif
 
+#ifndef isgraph
+#define isgraph(C) (isprint(C) && !isspace(C))
+#endif
+
+#ifdef isascii
+#define ISALPHA(C) (isascii(C) && isalpha(C))
+#define ISUPPER(C) (isascii(C) && isupper(C))
+#define ISLOWER(C) (isascii(C) && islower(C))
+#define ISDIGIT(C) (isascii(C) && isdigit(C))
+#define ISXDIGIT(C) (isascii(C) && isxdigit(C))
+#define ISSPACE(C) (isascii(C) && isspace(C))
+#define ISPUNCT(C) (isascii(C) && ispunct(C))
+#define ISALNUM(C) (isascii(C) && isalnum(C))
+#define ISPRINT(C) (isascii(C) && isprint(C))
+#define ISGRAPH(C) (isascii(C) && isgraph(C))
+#define ISCNTRL(C) (isascii(C) && iscntrl(C))
+#else
+#define ISALPHA(C) isalpha(C)
+#define ISUPPER(C) isupper(C)
+#define ISLOWER(C) islower(C)
+#define ISDIGIT(C) isdigit(C)
+#define ISXDIGIT(C) isxdigit(C)
+#define ISSPACE(C) isspace(C)
+#define ISPUNCT(C) ispunct(C)
+#define ISALNUM(C) isalnum(C)
+#define ISPRINT(C) isprint(C)
+#define ISGRAPH(C) isgraph(C)
+#define ISCNTRL(C) iscntrl(C)
+#endif
+
 #include "dfa.h"
 #include "dfa.h"
+#include "regex.h"
 
 #if __STDC__
 typedef void *ptr_t;
 
 #if __STDC__
 typedef void *ptr_t;
@@ -38,7 +77,7 @@ typedef void *ptr_t;
 typedef char *ptr_t;
 #endif
 
 typedef char *ptr_t;
 #endif
 
-static void    regmust();
+static void    dfamust();
 
 static ptr_t
 xcalloc(n, s)
 
 static ptr_t
 xcalloc(n, s)
@@ -48,11 +87,11 @@ xcalloc(n, s)
   ptr_t r = calloc(n, s);
 
   if (!r)
   ptr_t r = calloc(n, s);
 
   if (!r)
-    regerror("Memory exhausted");
+    dfaerror("Memory exhausted");
   return r;
 }
 
   return r;
 }
 
-ptr_t                          /* Not static, so alloca.o can use it.  */
+static ptr_t
 xmalloc(n)
      size_t n;
 {
 xmalloc(n)
      size_t n;
 {
@@ -60,7 +99,7 @@ xmalloc(n)
 
   assert(n != 0);
   if (!r)
 
   assert(n != 0);
   if (!r)
-    regerror("Memory exhausted");
+    dfaerror("Memory exhausted");
   return r;
 }
 
   return r;
 }
 
@@ -73,7 +112,7 @@ xrealloc(p, n)
 
   assert(n != 0);
   if (!r)
 
   assert(n != 0);
   if (!r)
-    regerror("Memory exhausted");
+    dfaerror("Memory exhausted");
   return r;
 }
 
   return r;
 }
 
@@ -89,54 +128,52 @@ xrealloc(p, n)
        (nalloc) *= 2;                            \
       REALLOC(p, t, nalloc);                     \
     }
        (nalloc) *= 2;                            \
       REALLOC(p, t, nalloc);                     \
     }
-\f
+
 #ifdef DEBUG
 #ifdef DEBUG
-#include <stdio.h>
 
 static void
 prtok(t)
 
 static void
 prtok(t)
-     _token t;
+     token t;
 {
   char *s;
 
   if (t < 0)
     fprintf(stderr, "END");
 {
   char *s;
 
   if (t < 0)
     fprintf(stderr, "END");
-  else if (t < _NOTCHAR)
+  else if (t < NOTCHAR)
     fprintf(stderr, "%c", t);
   else
     {
       switch (t)
        {
     fprintf(stderr, "%c", t);
   else
     {
       switch (t)
        {
-       case _EMPTY: s = "EMPTY"; break;
-       case _BACKREF: s = "BACKREF"; break;
-       case _BEGLINE: s = "BEGLINE"; break;
-       case _ALLBEGLINE: s = "ALLBEGLINE"; break;
-       case _ENDLINE: s = "ENDLINE"; break;
-       case _ALLENDLINE: s = "ALLENDLINE"; break;
-       case _BEGWORD: s = "BEGWORD"; break;
-       case _ENDWORD: s = "ENDWORD"; break;
-       case _LIMWORD: s = "LIMWORD"; break;
-       case _NOTLIMWORD: s = "NOTLIMWORD"; break;
-       case _QMARK: s = "QMARK"; break;
-       case _STAR: s = "STAR"; break;
-       case _PLUS: s = "PLUS"; break;
-       case _CAT: s = "CAT"; break;
-       case _OR: s = "OR"; break;
-       case _LPAREN: s = "LPAREN"; break;
-       case _RPAREN: s = "RPAREN"; break;
-       default: s = "SET"; break;
+       case EMPTY: s = "EMPTY"; break;
+       case BACKREF: s = "BACKREF"; break;
+       case BEGLINE: s = "BEGLINE"; break;
+       case ENDLINE: s = "ENDLINE"; break;
+       case BEGWORD: s = "BEGWORD"; break;
+       case ENDWORD: s = "ENDWORD"; break;
+       case LIMWORD: s = "LIMWORD"; break;
+       case NOTLIMWORD: s = "NOTLIMWORD"; break;
+       case QMARK: s = "QMARK"; break;
+       case STAR: s = "STAR"; break;
+       case PLUS: s = "PLUS"; break;
+       case CAT: s = "CAT"; break;
+       case OR: s = "OR"; break;
+       case ORTOP: s = "ORTOP"; break;
+       case LPAREN: s = "LPAREN"; break;
+       case RPAREN: s = "RPAREN"; break;
+       default: s = "CSET"; break;
        }
       fprintf(stderr, "%s", s);
     }
 }
 #endif /* DEBUG */
 
        }
       fprintf(stderr, "%s", s);
     }
 }
 #endif /* DEBUG */
 
-/* Stuff pertaining to charsets. */
+/* Stuff pertaining to charclasses. */
 
 static int
 tstbit(b, c)
      int b;
 
 static int
 tstbit(b, c)
      int b;
-     _charset c;
+     charclass c;
 {
   return c[b / INTBITS] & 1 << b % INTBITS;
 }
 {
   return c[b / INTBITS] & 1 << b % INTBITS;
 }
@@ -144,7 +181,7 @@ tstbit(b, c)
 static void
 setbit(b, c)
      int b;
 static void
 setbit(b, c)
      int b;
-     _charset c;
+     charclass c;
 {
   c[b / INTBITS] |= 1 << b % INTBITS;
 }
 {
   c[b / INTBITS] |= 1 << b % INTBITS;
 }
@@ -152,83 +189,83 @@ setbit(b, c)
 static void
 clrbit(b, c)
      int b;
 static void
 clrbit(b, c)
      int b;
-     _charset c;
+     charclass c;
 {
   c[b / INTBITS] &= ~(1 << b % INTBITS);
 }
 
 static void
 copyset(src, dst)
 {
   c[b / INTBITS] &= ~(1 << b % INTBITS);
 }
 
 static void
 copyset(src, dst)
-     const _charset src;
-     _charset dst;
+     charclass src;
+     charclass dst;
 {
   int i;
 
 {
   int i;
 
-  for (i = 0; i < _CHARSET_INTS; ++i)
+  for (i = 0; i < CHARCLASS_INTS; ++i)
     dst[i] = src[i];
 }
 
 static void
 zeroset(s)
     dst[i] = src[i];
 }
 
 static void
 zeroset(s)
-     _charset s;
+     charclass s;
 {
   int i;
 
 {
   int i;
 
-  for (i = 0; i < _CHARSET_INTS; ++i)
+  for (i = 0; i < CHARCLASS_INTS; ++i)
     s[i] = 0;
 }
 
 static void
 notset(s)
     s[i] = 0;
 }
 
 static void
 notset(s)
-     _charset s;
+     charclass s;
 {
   int i;
 
 {
   int i;
 
-  for (i = 0; i < _CHARSET_INTS; ++i)
+  for (i = 0; i < CHARCLASS_INTS; ++i)
     s[i] = ~s[i];
 }
 
 static int
 equal(s1, s2)
     s[i] = ~s[i];
 }
 
 static int
 equal(s1, s2)
-     const _charset s1;
-     const _charset s2;
+     charclass s1;
+     charclass s2;
 {
   int i;
 
 {
   int i;
 
-  for (i = 0; i < _CHARSET_INTS; ++i)
+  for (i = 0; i < CHARCLASS_INTS; ++i)
     if (s1[i] != s2[i])
       return 0;
   return 1;
 }
     if (s1[i] != s2[i])
       return 0;
   return 1;
 }
-\f
-/* A pointer to the current regexp is kept here during parsing. */
-static struct regexp *reg;
 
 
-/* Find the index of charset s in reg->charsets, or allocate a new charset. */
+/* A pointer to the current dfa is kept here during parsing. */
+static struct dfa *dfa;
+
+/* Find the index of charclass s in dfa->charclasses, or allocate a new charclass. */
 static int
 static int
-charset_index(s)
-     const _charset s;
+charclass_index(s)
+     charclass s;
 {
   int i;
 
 {
   int i;
 
-  for (i = 0; i < reg->cindex; ++i)
-    if (equal(s, reg->charsets[i]))
+  for (i = 0; i < dfa->cindex; ++i)
+    if (equal(s, dfa->charclasses[i]))
       return i;
       return i;
-  REALLOC_IF_NECESSARY(reg->charsets, _charset, reg->calloc, reg->cindex);
-  ++reg->cindex;
-  copyset(s, reg->charsets[i]);
+  REALLOC_IF_NECESSARY(dfa->charclasses, charclass, dfa->calloc, dfa->cindex);
+  ++dfa->cindex;
+  copyset(s, dfa->charclasses[i]);
   return i;
 }
 
 /* Syntax bits controlling the behavior of the lexical analyzer. */
   return i;
 }
 
 /* Syntax bits controlling the behavior of the lexical analyzer. */
-static syntax_bits, syntax_bits_set;
+static int syntax_bits, syntax_bits_set;
 
 /* Flag for case-folding letters into sets. */
 
 /* Flag for case-folding letters into sets. */
-static case_fold;
+static int case_fold;
 
 /* Entry point to set syntax options. */
 void
 
 /* Entry point to set syntax options. */
 void
-regsyntax(bits, fold)
+dfasyntax(bits, fold)
      int bits;
      int fold;
 {
      int bits;
      int fold;
 {
@@ -237,63 +274,132 @@ regsyntax(bits, fold)
   case_fold = fold;
 }
 
   case_fold = fold;
 }
 
-/* Lexical analyzer. */
-static const char *lexstart;   /* Pointer to beginning of input string. */
-static const char *lexptr;     /* Pointer to next input character. */
+/* Lexical analyzer.  All the dross that deals with the obnoxious
+   GNU Regex syntax bits is located here.  The poor, suffering
+   reader is referred to the GNU Regex documentation for the
+   meaning of the @#%!@#%^!@ syntax bits. */
+
+static char *lexstart;         /* Pointer to beginning of input string. */
+static char *lexptr;           /* Pointer to next input character. */
 static lexleft;                        /* Number of characters remaining. */
 static lexleft;                        /* Number of characters remaining. */
-static caret_allowed;          /* True if backward context allows ^
-                                  (meaningful only if RE_CONTEXT_INDEP_OPS
-                                  is turned off). */
-static closure_allowed;                /* True if backward context allows closures
-                                  (meaningful only if RE_CONTEXT_INDEP_OPS
-                                  is turned off). */
+static token lasttok;          /* Previous token returned; initially END. */
+static int laststart;          /* True if we're separated from beginning or (, |
+                                  only by zero-width characters. */
+static int parens;             /* Count of outstanding left parens. */
+static int minrep, maxrep;     /* Repeat counts for {m,n}. */
 
 /* Note that characters become unsigned here. */
 #define FETCH(c, eoferr)             \
   {                                  \
     if (! lexleft)                   \
 
 /* Note that characters become unsigned here. */
 #define FETCH(c, eoferr)             \
   {                                  \
     if (! lexleft)                   \
-      if (eoferr)                    \
-       regerror(eoferr);             \
+      if (eoferr != 0)               \
+       dfaerror(eoferr);             \
       else                           \
       else                           \
-       return _END;                  \
+       return END;                   \
     (c) = (unsigned char) *lexptr++;  \
     --lexleft;                       \
   }
 
     (c) = (unsigned char) *lexptr++;  \
     --lexleft;                       \
   }
 
-static _token
+#define FUNC(F, P) static int F(c) int c; { return P(c); }
+
+FUNC(is_alpha, ISALPHA)
+FUNC(is_upper, ISUPPER)
+FUNC(is_lower, ISLOWER)
+FUNC(is_digit, ISDIGIT)
+FUNC(is_xdigit, ISXDIGIT)
+FUNC(is_space, ISSPACE)
+FUNC(is_punct, ISPUNCT)
+FUNC(is_alnum, ISALNUM)
+FUNC(is_print, ISPRINT)
+FUNC(is_graph, ISGRAPH)
+FUNC(is_cntrl, ISCNTRL)
+
+/* The following list maps the names of the Posix named character classes
+   to predicate functions that determine whether a given character is in
+   the class.  The leading [ has already been eaten by the lexical analyzer. */
+static struct {
+  char *name;
+  int (*pred)();
+} prednames[] = {
+  ":alpha:]", is_alpha,
+  ":upper:]", is_upper,
+  ":lower:]", is_lower,
+  ":digit:]", is_digit,
+  ":xdigit:]", is_xdigit,
+  ":space:]", is_space,
+  ":punct:]", is_punct,
+  ":alnum:]", is_alnum,
+  ":print:]", is_print,
+  ":graph:]", is_graph,
+  ":cntrl:]", is_cntrl,
+  0
+};
+
+static int
+looking_at(s)
+     char *s;
+{
+  int len;
+
+  len = strlen(s);
+  if (lexleft < len)
+    return 0;
+  return strncmp(s, lexptr, len) == 0;
+}
+
+static token
 lex()
 {
 lex()
 {
-  _token c, c2;
-  int invert;
-  _charset cset;
+  token c, c1, c2;
+  int backslash = 0, invert;
+  charclass ccl;
+  int i;
 
 
-  FETCH(c, (char *) 0);
-  switch (c)
+  /* Basic plan: We fetch a character.  If it's a backslash,
+     we set the backslash flag and go through the loop again.
+     On the plus side, this avoids having a duplicate of the
+     main switch inside the backslash case.  On the minus side,
+     it means that just about every case begins with
+     "if (backslash) ...".  */
+  for (i = 0; i < 2; ++i)
     {
     {
-    case '^':
-      if (! (syntax_bits & RE_CONTEXT_INDEP_OPS)
-         && (!caret_allowed ||
-             (syntax_bits & RE_TIGHT_VBAR) && lexptr - 1 != lexstart))
-       goto normal_char;
-      caret_allowed = 0;
-      return syntax_bits & RE_TIGHT_VBAR ? _ALLBEGLINE : _BEGLINE;
-
-    case '$':
-      if (syntax_bits & RE_CONTEXT_INDEP_OPS || !lexleft
-         || (! (syntax_bits & RE_TIGHT_VBAR)
-             && ((syntax_bits & RE_NO_BK_PARENS
-                  ? lexleft > 0 && *lexptr == ')'
-                  : lexleft > 1 && *lexptr == '\\' && lexptr[1] == ')')
-                 || (syntax_bits & RE_NO_BK_VBAR
-                     ? lexleft > 0 && *lexptr == '|'
-                     : lexleft > 1 && *lexptr == '\\' && lexptr[1] == '|'))))
-       return syntax_bits & RE_TIGHT_VBAR ? _ALLENDLINE : _ENDLINE;
-      goto normal_char;
-
-    case '\\':
-      FETCH(c, "Unfinished \\ quote");
+      FETCH(c, 0);
       switch (c)
        {
       switch (c)
        {
+       case '\\':
+         if (backslash)
+           goto normal_char;
+         if (lexleft == 0)
+           dfaerror("Unfinished \\ escape");
+         backslash = 1;
+         break;
+
+       case '^':
+         if (backslash)
+           goto normal_char;
+         if (syntax_bits & RE_CONTEXT_INDEP_ANCHORS
+             || lasttok == END
+             || lasttok == LPAREN
+             || lasttok == OR)
+           return lasttok = BEGLINE;
+         goto normal_char;
+
+       case '$':
+         if (backslash)
+           goto normal_char;
+         if (syntax_bits & RE_CONTEXT_INDEP_ANCHORS
+             || lexleft == 0
+             || (syntax_bits & RE_NO_BK_PARENS
+                 ? lexleft > 0 && *lexptr == ')'
+                 : lexleft > 1 && lexptr[0] == '\\' && lexptr[1] == ')')
+             || (syntax_bits & RE_NO_BK_VBAR
+                 ? lexleft > 0 && *lexptr == '|'
+                 : lexleft > 1 && lexptr[0] == '\\' && lexptr[1] == '|')
+             || ((syntax_bits & RE_NEWLINE_ALT)
+                 && lexleft > 0 && *lexptr == '\n'))
+           return lasttok = ENDLINE;
+         goto normal_char;
+
        case '1':
        case '2':
        case '3':
        case '1':
        case '2':
        case '3':
@@ -303,238 +409,307 @@ lex()
        case '7':
        case '8':
        case '9':
        case '7':
        case '8':
        case '9':
-         caret_allowed = 0;
-         closure_allowed = 1;
-         return _BACKREF;
+         if (backslash && !(syntax_bits & RE_NO_BK_REFS))
+           {
+             laststart = 0;
+             return lasttok = BACKREF;
+           }
+         goto normal_char;
 
        case '<':
 
        case '<':
-         caret_allowed = 0;
-         return _BEGWORD;
+         if (backslash)
+           return lasttok = BEGWORD;
+         goto normal_char;
 
        case '>':
 
        case '>':
-         caret_allowed = 0;
-         return _ENDWORD;
+         if (backslash)
+           return lasttok = ENDWORD;
+         goto normal_char;
 
        case 'b':
 
        case 'b':
-         caret_allowed = 0;
-         return _LIMWORD;
+         if (backslash)
+           return lasttok = LIMWORD;
+         goto normal_char;
 
        case 'B':
 
        case 'B':
-         caret_allowed = 0;
-         return _NOTLIMWORD;
-
-       case 'w':
-       case 'W':
-         zeroset(cset);
-         for (c2 = 0; c2 < _NOTCHAR; ++c2)
-           if (ISALNUM(c2))
-             setbit(c2, cset);
-         if (c == 'W')
-           notset(cset);
-         caret_allowed = 0;
-         closure_allowed = 1;
-         return _SET + charset_index(cset);
+         if (backslash)
+           return lasttok = NOTLIMWORD;
+         goto normal_char;
 
        case '?':
 
        case '?':
-         if (syntax_bits & RE_BK_PLUS_QM)
-           goto qmark;
-         goto normal_char;
+         if (syntax_bits & RE_LIMITED_OPS)
+           goto normal_char;
+         if (backslash != ((syntax_bits & RE_BK_PLUS_QM) != 0))
+           goto normal_char;
+         if (!(syntax_bits & RE_CONTEXT_INDEP_OPS) && laststart)
+           goto normal_char;
+         return lasttok = QMARK;
+
+       case '*':
+         if (backslash)
+           goto normal_char;
+         if (!(syntax_bits & RE_CONTEXT_INDEP_OPS) && laststart)
+           goto normal_char;
+         return lasttok = STAR;
 
        case '+':
 
        case '+':
-         if (syntax_bits & RE_BK_PLUS_QM)
-           goto plus;
-         goto normal_char;
+         if (syntax_bits & RE_LIMITED_OPS)
+           goto normal_char;
+         if (backslash != ((syntax_bits & RE_BK_PLUS_QM) != 0))
+           goto normal_char;
+         if (!(syntax_bits & RE_CONTEXT_INDEP_OPS) && laststart)
+           goto normal_char;
+         return lasttok = PLUS;
+
+       case '{':
+         if (!(syntax_bits & RE_INTERVALS))
+           goto normal_char;
+         if (backslash != ((syntax_bits & RE_NO_BK_BRACES) == 0))
+           goto normal_char;
+         minrep = maxrep = 0;
+         /* Cases:
+            {M} - exact count
+            {M,} - minimum count, maximum is infinity
+            {,M} - 0 through M
+            {M,N} - M through N */
+         FETCH(c, "unfinished repeat count");
+         if (ISDIGIT(c))
+           {
+             minrep = c - '0';
+             for (;;)
+               {
+                 FETCH(c, "unfinished repeat count");
+                 if (!ISDIGIT(c))
+                   break;
+                 minrep = 10 * minrep + c - '0';
+               }
+           }
+         else if (c != ',')
+           dfaerror("malformed repeat count");
+         if (c == ',')
+           for (;;)
+             {
+               FETCH(c, "unfinished repeat count");
+               if (!ISDIGIT(c))
+                 break;
+               maxrep = 10 * maxrep + c - '0';
+             }
+         else
+           maxrep = minrep;
+         if (!(syntax_bits & RE_NO_BK_BRACES))
+           {
+             if (c != '\\')
+               dfaerror("malformed repeat count");
+             FETCH(c, "unfinished repeat count");
+           }
+         if (c != '}')
+           dfaerror("malformed repeat count");
+         laststart = 0;
+         return lasttok = REPMN;
 
        case '|':
 
        case '|':
-         if (! (syntax_bits & RE_NO_BK_VBAR))
-           goto or;
-         goto normal_char;
+         if (syntax_bits & RE_LIMITED_OPS)
+           goto normal_char;
+         if (backslash != ((syntax_bits & RE_NO_BK_VBAR) == 0))
+           goto normal_char;
+         laststart = 1;
+         return lasttok = OR;
+
+       case '\n':
+         if (syntax_bits & RE_LIMITED_OPS
+             || backslash
+             || !(syntax_bits & RE_NEWLINE_ALT))
+           goto normal_char;
+         laststart = 1;
+         return lasttok = OR;
 
        case '(':
 
        case '(':
-         if (! (syntax_bits & RE_NO_BK_PARENS))
-           goto lparen;
-         goto normal_char;
+         if (backslash != ((syntax_bits & RE_NO_BK_PARENS) == 0))
+           goto normal_char;
+         ++parens;
+         laststart = 1;
+         return lasttok = LPAREN;
 
        case ')':
 
        case ')':
-         if (! (syntax_bits & RE_NO_BK_PARENS))
-           goto rparen;
-         goto normal_char;
-
-       default:
-         goto normal_char;
-       }
+         if (backslash != ((syntax_bits & RE_NO_BK_PARENS) == 0))
+           goto normal_char;
+         if (parens == 0 && syntax_bits & RE_UNMATCHED_RIGHT_PAREN_ORD)
+           goto normal_char;
+         --parens;
+         laststart = 0;
+         return lasttok = RPAREN;
+
+       case '.':
+         if (backslash)
+           goto normal_char;
+         zeroset(ccl);
+         notset(ccl);
+         if (!(syntax_bits & RE_DOT_NEWLINE))
+           clrbit('\n', ccl);
+         if (syntax_bits & RE_DOT_NOT_NULL)
+           clrbit('\0', ccl);
+         laststart = 0;
+         return lasttok = CSET + charclass_index(ccl);
 
 
-    case '?':
-      if (syntax_bits & RE_BK_PLUS_QM)
-       goto normal_char;
-    qmark:
-      if (! (syntax_bits & RE_CONTEXT_INDEP_OPS) && !closure_allowed)
-       goto normal_char;
-      return _QMARK;
-
-    case '*':
-      if (! (syntax_bits & RE_CONTEXT_INDEP_OPS) && !closure_allowed)
-       goto normal_char;
-      return _STAR;
-
-    case '+':
-      if (syntax_bits & RE_BK_PLUS_QM)
-       goto normal_char;
-    plus:
-      if (! (syntax_bits & RE_CONTEXT_INDEP_OPS) && !closure_allowed)
-       goto normal_char;
-      return _PLUS;
-
-    case '|':
-      if (! (syntax_bits & RE_NO_BK_VBAR))
-       goto normal_char;
-    or:
-      caret_allowed = 1;
-      closure_allowed = 0;
-      return _OR;
-
-    case '\n':
-      if (! (syntax_bits & RE_NEWLINE_OR))
-       goto normal_char;
-      goto or;
-
-    case '(':
-      if (! (syntax_bits & RE_NO_BK_PARENS))
-       goto normal_char;
-    lparen:
-      caret_allowed = 1;
-      closure_allowed = 0;
-      return _LPAREN;
-
-    case ')':
-      if (! (syntax_bits & RE_NO_BK_PARENS))
-       goto normal_char;
-    rparen:
-      caret_allowed = 0;
-      closure_allowed = 1;
-      return _RPAREN;
-
-    case '.':
-      zeroset(cset);
-      notset(cset);
-      clrbit('\n', cset);
-      caret_allowed = 0;
-      closure_allowed = 1;
-      return _SET + charset_index(cset);
-
-    case '[':
-      zeroset(cset);
-      FETCH(c, "Unbalanced [");
-      if (c == '^')
-       {
+       case 'w':
+       case 'W':
+         if (!backslash)
+           goto normal_char;
+         zeroset(ccl);
+         for (c2 = 0; c2 < NOTCHAR; ++c2)
+           if (ISALNUM(c2))
+             setbit(c2, ccl);
+         if (c == 'W')
+           notset(ccl);
+         laststart = 0;
+         return lasttok = CSET + charclass_index(ccl);
+       
+       case '[':
+         if (backslash)
+           goto normal_char;
+         zeroset(ccl);
          FETCH(c, "Unbalanced [");
          FETCH(c, "Unbalanced [");
-         invert = 1;
-       }
-      else
-       invert = 0;
-      do
-       {
-         FETCH(c2, "Unbalanced [");
-         if (c2 == '-')
+         if (c == '^')
            {
            {
-             FETCH(c2, "Unbalanced [");
+             FETCH(c, "Unbalanced [");
+             invert = 1;
+           }
+         else
+           invert = 0;
+         do
+           {
+             /* Nobody ever said this had to be fast. :-)
+                Note that if we're looking at some other [:...:]
+                construct, we just treat it as a bunch of ordinary
+                characters.  We can do this because we assume
+                regex has checked for syntax errors before
+                dfa is ever called. */
+             if (c == '[' && (syntax_bits & RE_CHAR_CLASSES))
+               for (c1 = 0; prednames[c1].name; ++c1)
+                 if (looking_at(prednames[c1].name))
+                   {
+                     for (c2 = 0; c2 < NOTCHAR; ++c2)
+                       if ((*prednames[c1].pred)(c2))
+                         setbit(c2, ccl);
+                     lexptr += strlen(prednames[c1].name);
+                     lexleft -= strlen(prednames[c1].name);
+                     FETCH(c1, "Unbalanced [");
+                     goto skip;
+                   }
+             if (c == '\\' && (syntax_bits & RE_BACKSLASH_ESCAPE_IN_LISTS))
+               FETCH(c, "Unbalanced [");
+             FETCH(c1, "Unbalanced [");
+             if (c1 == '-')
+               {
+                 FETCH(c2, "Unbalanced [");
+                 if (c2 == ']')
+                   {
+                     /* In the case [x-], the - is an ordinary hyphen,
+                        which is left in c1, the lookahead character. */
+                     --lexptr;
+                     ++lexleft;
+                     c2 = c;
+                   }
+                 else
+                   {
+                     if (c2 == '\\'
+                         && (syntax_bits & RE_BACKSLASH_ESCAPE_IN_LISTS))
+                       FETCH(c2, "Unbalanced [");
+                     FETCH(c1, "Unbalanced [");
+                   }
+               }
+             else
+               c2 = c;
              while (c <= c2)
                {
              while (c <= c2)
                {
-                 setbit(c, cset);
+                 setbit(c, ccl);
                  if (case_fold)
                    if (ISUPPER(c))
                  if (case_fold)
                    if (ISUPPER(c))
-                     setbit(tolower(c), cset);
+                     setbit(tolower(c), ccl);
                    else if (ISLOWER(c))
                    else if (ISLOWER(c))
-                     setbit(toupper(c), cset);
+                     setbit(toupper(c), ccl);
                  ++c;
                }
                  ++c;
                }
-             FETCH(c, "Unbalanced [");
+           skip:
+             ;
            }
            }
-         else
+         while ((c = c1) != ']');
+         if (invert)
            {
            {
-             setbit(c, cset);
-             if (case_fold)
-               if (ISUPPER(c))
-                 setbit(tolower(c), cset);
-               else if (ISLOWER(c))
-                 setbit(toupper(c), cset);
-             c = c2;
+             notset(ccl);
+             if (syntax_bits & RE_HAT_LISTS_NOT_NEWLINE)
+               clrbit('\n', ccl);
            }
            }
-       }
-      while (c != ']');
-      if (invert)
-       notset(cset);
-      caret_allowed = 0;
-      closure_allowed = 1;
-      return _SET + charset_index(cset);
+         laststart = 0;
+         return lasttok = CSET + charclass_index(ccl);
 
 
-    default:
-    normal_char:
-      caret_allowed = 0;
-      closure_allowed = 1;
-      if (case_fold && ISALPHA(c))
-       {
-         zeroset(cset);
-         if (isupper(c))
-           c = tolower(c);
-         setbit(c, cset);
-         setbit(toupper(c), cset);
-         return _SET + charset_index(cset);
+       default:
+       normal_char:
+         laststart = 0;
+         if (case_fold && ISALPHA(c))
+           {
+             zeroset(ccl);
+             setbit(c, ccl);
+             if (isupper(c))
+               setbit(tolower(c), ccl);
+             else
+               setbit(toupper(c), ccl);
+             return lasttok = CSET + charclass_index(ccl);
+           }
+         return c;
        }
        }
-      return c;
     }
     }
+
+  /* The above loop should consume at most a backslash
+     and some other character. */
+  abort();
 }
 }
-\f
+
 /* Recursive descent parser for regular expressions. */
 
 /* Recursive descent parser for regular expressions. */
 
-static _token tok;             /* Lookahead token. */
+static token tok;              /* Lookahead token. */
 static depth;                  /* Current depth of a hypothetical stack
                                   holding deferred productions.  This is
                                   used to determine the depth that will be
                                   required of the real stack later on in
 static depth;                  /* Current depth of a hypothetical stack
                                   holding deferred productions.  This is
                                   used to determine the depth that will be
                                   required of the real stack later on in
-                                  reganalyze(). */
+                                  dfaanalyze(). */
 
 /* Add the given token to the parse tree, maintaining the depth count and
    updating the maximum depth if necessary. */
 static void
 addtok(t)
 
 /* Add the given token to the parse tree, maintaining the depth count and
    updating the maximum depth if necessary. */
 static void
 addtok(t)
-     _token t;
+     token t;
 {
 {
-  REALLOC_IF_NECESSARY(reg->tokens, _token, reg->talloc, reg->tindex);
-  reg->tokens[reg->tindex++] = t;
+  REALLOC_IF_NECESSARY(dfa->tokens, token, dfa->talloc, dfa->tindex);
+  dfa->tokens[dfa->tindex++] = t;
 
   switch (t)
     {
 
   switch (t)
     {
-    case _QMARK:
-    case _STAR:
-    case _PLUS:
+    case QMARK:
+    case STAR:
+    case PLUS:
       break;
 
       break;
 
-    case _CAT:
-    case _OR:
+    case CAT:
+    case OR:
+    case ORTOP:
       --depth;
       break;
 
     default:
       --depth;
       break;
 
     default:
-      ++reg->nleaves;
-    case _EMPTY:
+      ++dfa->nleaves;
+    case EMPTY:
       ++depth;
       break;
     }
       ++depth;
       break;
     }
-  if (depth > reg->depth)
-    reg->depth = depth;
+  if (depth > dfa->depth)
+    dfa->depth = depth;
 }
 
 /* The grammar understood by the parser is as follows.
 
 }
 
 /* The grammar understood by the parser is as follows.
 
-   start:
-     regexp
-     _ALLBEGLINE regexp
-     regexp _ALLENDLINE
-     _ALLBEGLINE regexp _ALLENDLINE
-
    regexp:
    regexp:
-     regexp _OR branch
+     regexp OR branch
      branch
 
    branch:
      branch
 
    branch:
@@ -542,27 +717,27 @@ addtok(t)
      closure
 
    closure:
      closure
 
    closure:
-     closure _QMARK
-     closure _STAR
-     closure _PLUS
+     closure QMARK
+     closure STAR
+     closure PLUS
      atom
 
    atom:
      <normal character>
      atom
 
    atom:
      <normal character>
-     _SET
-     _BACKREF
-     _BEGLINE
-     _ENDLINE
-     _BEGWORD
-     _ENDWORD
-     _LIMWORD
-     _NOTLIMWORD
+     CSET
+     BACKREF
+     BEGLINE
+     ENDLINE
+     BEGWORD
+     ENDWORD
+     LIMWORD
+     NOTLIMWORD
      <empty>
 
    The parser builds a parse tree in postfix form in an array of tokens. */
 
 #if __STDC__
      <empty>
 
    The parser builds a parse tree in postfix form in an array of tokens. */
 
 #if __STDC__
-static void regexp(void);
+static void regexp(int);
 #else
 static void regexp();
 #endif
 #else
 static void regexp();
 #endif
@@ -570,116 +745,164 @@ static void regexp();
 static void
 atom()
 {
 static void
 atom()
 {
-  if (tok >= 0 && tok < _NOTCHAR || tok >= _SET || tok == _BACKREF
-      || tok == _BEGLINE || tok == _ENDLINE || tok == _BEGWORD
-      || tok == _ENDWORD || tok == _LIMWORD || tok == _NOTLIMWORD)
+  if ((tok >= 0 && tok < NOTCHAR) || tok >= CSET || tok == BACKREF
+      || tok == BEGLINE || tok == ENDLINE || tok == BEGWORD
+      || tok == ENDWORD || tok == LIMWORD || tok == NOTLIMWORD)
     {
       addtok(tok);
       tok = lex();
     }
     {
       addtok(tok);
       tok = lex();
     }
-  else if (tok == _LPAREN)
+  else if (tok == LPAREN)
     {
       tok = lex();
     {
       tok = lex();
-      regexp();
-      if (tok != _RPAREN)
-       regerror("Unbalanced (");
+      regexp(0);
+      if (tok != RPAREN)
+       dfaerror("Unbalanced (");
       tok = lex();
     }
   else
       tok = lex();
     }
   else
-    addtok(_EMPTY);
+    addtok(EMPTY);
+}
+
+/* Return the number of tokens in the given subexpression. */
+static int
+nsubtoks(tindex)
+{
+  int ntoks1;
+
+  switch (dfa->tokens[tindex - 1])
+    {
+    default:
+      return 1;
+    case QMARK:
+    case STAR:
+    case PLUS:
+      return 1 + nsubtoks(tindex - 1);
+    case CAT:
+    case OR:
+    case ORTOP:
+      ntoks1 = nsubtoks(tindex - 1);
+      return 1 + ntoks1 + nsubtoks(tindex - 1 - ntoks1);
+    }
+}
+
+/* Copy the given subexpression to the top of the tree. */
+static void
+copytoks(tindex, ntokens)
+     int tindex, ntokens;
+{
+  int i;
+
+  for (i = 0; i < ntokens; ++i)
+    addtok(dfa->tokens[tindex + i]);
 }
 
 static void
 closure()
 {
 }
 
 static void
 closure()
 {
+  int tindex, ntokens, i;
+
   atom();
   atom();
-  while (tok == _QMARK || tok == _STAR || tok == _PLUS)
-    {
-      addtok(tok);
-      tok = lex();
-    }
+  while (tok == QMARK || tok == STAR || tok == PLUS || tok == REPMN)
+    if (tok == REPMN)
+      {
+       ntokens = nsubtoks(dfa->tindex);
+       tindex = dfa->tindex - ntokens;
+       if (maxrep == 0)
+         addtok(PLUS);
+       if (minrep == 0)
+         addtok(QMARK);
+       for (i = 1; i < minrep; ++i)
+         {
+           copytoks(tindex, ntokens);
+           addtok(CAT);
+         }
+       for (; i < maxrep; ++i)
+         {
+           copytoks(tindex, ntokens);
+           addtok(QMARK);
+           addtok(CAT);
+         }
+       tok = lex();
+      }
+    else
+      {
+       addtok(tok);
+       tok = lex();
+      }
 }
 
 static void
 branch()
 {
   closure();
 }
 
 static void
 branch()
 {
   closure();
-  while (tok != _RPAREN && tok != _OR && tok != _ALLENDLINE && tok >= 0)
+  while (tok != RPAREN && tok != OR && tok >= 0)
     {
       closure();
     {
       closure();
-      addtok(_CAT);
+      addtok(CAT);
     }
 }
 
 static void
     }
 }
 
 static void
-regexp()
+regexp(toplevel)
+     int toplevel;
 {
   branch();
 {
   branch();
-  while (tok == _OR)
+  while (tok == OR)
     {
       tok = lex();
       branch();
     {
       tok = lex();
       branch();
-      addtok(_OR);
+      if (toplevel)
+       addtok(ORTOP);
+      else
+       addtok(OR);
     }
 }
 
 /* Main entry point for the parser.  S is a string to be parsed, len is the
     }
 }
 
 /* Main entry point for the parser.  S is a string to be parsed, len is the
-   length of the string, so s can include NUL characters.  R is a pointer to
-   the struct regexp to parse into. */
+   length of the string, so s can include NUL characters.  D is a pointer to
+   the struct dfa to parse into. */
 void
 void
-regparse(s, len, r)
-     const char *s;
+dfaparse(s, len, d)
+     char *s;
      size_t len;
      size_t len;
-     struct regexp *r;
+     struct dfa *d;
+
 {
 {
-  reg = r;
+  dfa = d;
   lexstart = lexptr = s;
   lexleft = len;
   lexstart = lexptr = s;
   lexleft = len;
-  caret_allowed = 1;
-  closure_allowed = 0;
+  lasttok = END;
+  laststart = 1;
+  parens = 0;
 
   if (! syntax_bits_set)
 
   if (! syntax_bits_set)
-    regerror("No syntax specified");
+    dfaerror("No syntax specified");
 
   tok = lex();
 
   tok = lex();
-  depth = r->depth;
-
-  if (tok == _ALLBEGLINE)
-    {
-      addtok(_BEGLINE);
-      tok = lex();
-      regexp();
-      addtok(_CAT);
-    }
-  else
-    regexp();
+  depth = d->depth;
 
 
-  if (tok == _ALLENDLINE)
-    {
-      addtok(_ENDLINE);
-      addtok(_CAT);
-      tok = lex();
-    }
+  regexp(1);
 
 
-  if (tok != _END)
-    regerror("Unbalanced )");
+  if (tok != END)
+    dfaerror("Unbalanced )");
 
 
-  addtok(_END - r->nregexps);
-  addtok(_CAT);
+  addtok(END - d->nregexps);
+  addtok(CAT);
 
 
-  if (r->nregexps)
-    addtok(_OR);
+  if (d->nregexps)
+    addtok(ORTOP);
 
 
-  ++r->nregexps;
+  ++d->nregexps;
 }
 }
-\f
+
 /* Some primitives for operating on sets of positions. */
 
 /* Copy one set to another; the destination must be large enough. */
 static void
 copy(src, dst)
 /* Some primitives for operating on sets of positions. */
 
 /* Copy one set to another; the destination must be large enough. */
 static void
 copy(src, dst)
-     const _position_set *src;
-     _position_set *dst;
+     position_set *src;
+     position_set *dst;
 {
   int i;
 
 {
   int i;
 
@@ -694,11 +917,11 @@ copy(src, dst)
    S->elems must point to an array large enough to hold the resulting set. */
 static void
 insert(p, s)
    S->elems must point to an array large enough to hold the resulting set. */
 static void
 insert(p, s)
-     _position p;
-     _position_set *s;
+     position p;
+     position_set *s;
 {
   int i;
 {
   int i;
-  _position t1, t2;
+  position t1, t2;
 
   for (i = 0; i < s->nelem && p.index < s->elems[i].index; ++i)
     ;
 
   for (i = 0; i < s->nelem && p.index < s->elems[i].index; ++i)
     ;
@@ -721,9 +944,9 @@ insert(p, s)
    the positions of both sets were inserted into an initially empty set. */
 static void
 merge(s1, s2, m)
    the positions of both sets were inserted into an initially empty set. */
 static void
 merge(s1, s2, m)
-     _position_set *s1;
-     _position_set *s2;
-     _position_set *m;
+     position_set *s1;
+     position_set *s2;
+     position_set *m;
 {
   int i = 0, j = 0;
 
 {
   int i = 0, j = 0;
 
@@ -747,8 +970,8 @@ merge(s1, s2, m)
 /* Delete a position from a set. */
 static void
 delete(p, s)
 /* Delete a position from a set. */
 static void
 delete(p, s)
-     _position p;
-     _position_set *s;
+     position p;
+     position_set *s;
 {
   int i;
 
 {
   int i;
 
@@ -759,15 +982,15 @@ delete(p, s)
     for (--s->nelem; i < s->nelem; ++i)
       s->elems[i] = s->elems[i + 1];
 }
     for (--s->nelem; i < s->nelem; ++i)
       s->elems[i] = s->elems[i + 1];
 }
-\f
+
 /* Find the index of the state corresponding to the given position set with
    the given preceding context, or create a new state if there is no such
    state.  Newline and letter tell whether we got here on a newline or
    letter, respectively. */
 static int
 /* Find the index of the state corresponding to the given position set with
    the given preceding context, or create a new state if there is no such
    state.  Newline and letter tell whether we got here on a newline or
    letter, respectively. */
 static int
-state_index(r, s, newline, letter)
-     struct regexp *r;
-     _position_set *s;
+state_index(d, s, newline, letter)
+     struct dfa *d;
+     position_set *s;
      int newline;
      int letter;
 {
      int newline;
      int letter;
 {
@@ -782,75 +1005,75 @@ state_index(r, s, newline, letter)
     hash ^= s->elems[i].index + s->elems[i].constraint;
 
   /* Try to find a state that exactly matches the proposed one. */
     hash ^= s->elems[i].index + s->elems[i].constraint;
 
   /* Try to find a state that exactly matches the proposed one. */
-  for (i = 0; i < r->sindex; ++i)
+  for (i = 0; i < d->sindex; ++i)
     {
     {
-      if (hash != r->states[i].hash || s->nelem != r->states[i].elems.nelem
-         || newline != r->states[i].newline || letter != r->states[i].letter)
+      if (hash != d->states[i].hash || s->nelem != d->states[i].elems.nelem
+         || newline != d->states[i].newline || letter != d->states[i].letter)
        continue;
       for (j = 0; j < s->nelem; ++j)
        if (s->elems[j].constraint
        continue;
       for (j = 0; j < s->nelem; ++j)
        if (s->elems[j].constraint
-           != r->states[i].elems.elems[j].constraint
-           || s->elems[j].index != r->states[i].elems.elems[j].index)
+           != d->states[i].elems.elems[j].constraint
+           || s->elems[j].index != d->states[i].elems.elems[j].index)
          break;
       if (j == s->nelem)
        return i;
     }
 
   /* We'll have to create a new state. */
          break;
       if (j == s->nelem)
        return i;
     }
 
   /* We'll have to create a new state. */
-  REALLOC_IF_NECESSARY(r->states, _dfa_state, r->salloc, r->sindex);
-  r->states[i].hash = hash;
-  MALLOC(r->states[i].elems.elems, _position, s->nelem);
-  copy(s, &r->states[i].elems);
-  r->states[i].newline = newline;
-  r->states[i].letter = letter;
-  r->states[i].backref = 0;
-  r->states[i].constraint = 0;
-  r->states[i].first_end = 0;
+  REALLOC_IF_NECESSARY(d->states, dfa_state, d->salloc, d->sindex);
+  d->states[i].hash = hash;
+  MALLOC(d->states[i].elems.elems, position, s->nelem);
+  copy(s, &d->states[i].elems);
+  d->states[i].newline = newline;
+  d->states[i].letter = letter;
+  d->states[i].backref = 0;
+  d->states[i].constraint = 0;
+  d->states[i].first_end = 0;
   for (j = 0; j < s->nelem; ++j)
   for (j = 0; j < s->nelem; ++j)
-    if (r->tokens[s->elems[j].index] < 0)
+    if (d->tokens[s->elems[j].index] < 0)
       {
        constraint = s->elems[j].constraint;
       {
        constraint = s->elems[j].constraint;
-       if (_SUCCEEDS_IN_CONTEXT(constraint, newline, 0, letter, 0)
-           || _SUCCEEDS_IN_CONTEXT(constraint, newline, 0, letter, 1)
-           || _SUCCEEDS_IN_CONTEXT(constraint, newline, 1, letter, 0)
-           || _SUCCEEDS_IN_CONTEXT(constraint, newline, 1, letter, 1))
-         r->states[i].constraint |= constraint;
-       if (! r->states[i].first_end)
-         r->states[i].first_end = r->tokens[s->elems[j].index];
+       if (SUCCEEDS_IN_CONTEXT(constraint, newline, 0, letter, 0)
+           || SUCCEEDS_IN_CONTEXT(constraint, newline, 0, letter, 1)
+           || SUCCEEDS_IN_CONTEXT(constraint, newline, 1, letter, 0)
+           || SUCCEEDS_IN_CONTEXT(constraint, newline, 1, letter, 1))
+         d->states[i].constraint |= constraint;
+       if (! d->states[i].first_end)
+         d->states[i].first_end = d->tokens[s->elems[j].index];
       }
       }
-    else if (r->tokens[s->elems[j].index] == _BACKREF)
+    else if (d->tokens[s->elems[j].index] == BACKREF)
       {
       {
-       r->states[i].constraint = _NO_CONSTRAINT;
-       r->states[i].backref = 1;
+       d->states[i].constraint = NO_CONSTRAINT;
+       d->states[i].backref = 1;
       }
 
       }
 
-  ++r->sindex;
+  ++d->sindex;
 
   return i;
 }
 
   return i;
 }
-\f
+
 /* Find the epsilon closure of a set of positions.  If any position of the set
    contains a symbol that matches the empty string in some context, replace
    that position with the elements of its follow labeled with an appropriate
    constraint.  Repeat exhaustively until no funny positions are left.
    S->elems must be large enough to hold the result. */
 void
 /* Find the epsilon closure of a set of positions.  If any position of the set
    contains a symbol that matches the empty string in some context, replace
    that position with the elements of its follow labeled with an appropriate
    constraint.  Repeat exhaustively until no funny positions are left.
    S->elems must be large enough to hold the result. */
 void
-epsclosure(s, r)
-     _position_set *s;
-     struct regexp *r;
+epsclosure(s, d)
+     position_set *s;
+     struct dfa *d;
 {
   int i, j;
   int *visited;
 {
   int i, j;
   int *visited;
-  _position p, old;
+  position p, old;
 
 
-  MALLOC(visited, int, r->tindex);
-  for (i = 0; i < r->tindex; ++i)
+  MALLOC(visited, int, d->tindex);
+  for (i = 0; i < d->tindex; ++i)
     visited[i] = 0;
 
   for (i = 0; i < s->nelem; ++i)
     visited[i] = 0;
 
   for (i = 0; i < s->nelem; ++i)
-    if (r->tokens[s->elems[i].index] >= _NOTCHAR
-       && r->tokens[s->elems[i].index] != _BACKREF
-       && r->tokens[s->elems[i].index] < _SET)
+    if (d->tokens[s->elems[i].index] >= NOTCHAR
+       && d->tokens[s->elems[i].index] != BACKREF
+       && d->tokens[s->elems[i].index] < CSET)
       {
        old = s->elems[i];
        p.constraint = old.constraint;
       {
        old = s->elems[i];
        p.constraint = old.constraint;
@@ -861,32 +1084,32 @@ epsclosure(s, r)
            continue;
          }
        visited[old.index] = 1;
            continue;
          }
        visited[old.index] = 1;
-       switch (r->tokens[old.index])
+       switch (d->tokens[old.index])
          {
          {
-         case _BEGLINE:
-           p.constraint &= _BEGLINE_CONSTRAINT;
+         case BEGLINE:
+           p.constraint &= BEGLINE_CONSTRAINT;
            break;
            break;
-         case _ENDLINE:
-           p.constraint &= _ENDLINE_CONSTRAINT;
+         case ENDLINE:
+           p.constraint &= ENDLINE_CONSTRAINT;
            break;
            break;
-         case _BEGWORD:
-           p.constraint &= _BEGWORD_CONSTRAINT;
+         case BEGWORD:
+           p.constraint &= BEGWORD_CONSTRAINT;
            break;
            break;
-         case _ENDWORD:
-           p.constraint &= _ENDWORD_CONSTRAINT;
+         case ENDWORD:
+           p.constraint &= ENDWORD_CONSTRAINT;
            break;
            break;
-         case _LIMWORD:
-           p.constraint &= _LIMWORD_CONSTRAINT;
+         case LIMWORD:
+           p.constraint &= LIMWORD_CONSTRAINT;
            break;
            break;
-         case _NOTLIMWORD:
-           p.constraint &= _NOTLIMWORD_CONSTRAINT;
+         case NOTLIMWORD:
+           p.constraint &= NOTLIMWORD_CONSTRAINT;
            break;
          default:
            break;
          }
            break;
          default:
            break;
          }
-       for (j = 0; j < r->follows[old.index].nelem; ++j)
+       for (j = 0; j < d->follows[old.index].nelem; ++j)
          {
          {
-           p.index = r->follows[old.index].elems[j].index;
+           p.index = d->follows[old.index].elems[j].index;
            insert(p, s);
          }
        /* Force rescan to start at the beginning. */
            insert(p, s);
          }
        /* Force rescan to start at the beginning. */
@@ -895,41 +1118,41 @@ epsclosure(s, r)
 
   free(visited);
 }
 
   free(visited);
 }
-\f
+
 /* Perform bottom-up analysis on the parse tree, computing various functions.
    Note that at this point, we're pretending constructs like \< are real
    characters rather than constraints on what can follow them.
 
    Nullable:  A node is nullable if it is at the root of a regexp that can
    match the empty string.
 /* Perform bottom-up analysis on the parse tree, computing various functions.
    Note that at this point, we're pretending constructs like \< are real
    characters rather than constraints on what can follow them.
 
    Nullable:  A node is nullable if it is at the root of a regexp that can
    match the empty string.
-   *  _EMPTY leaves are nullable.
+   *  EMPTY leaves are nullable.
    * No other leaf is nullable.
    * No other leaf is nullable.
-   * A _QMARK or _STAR node is nullable.
-   * A _PLUS node is nullable if its argument is nullable.
-   * A _CAT node is nullable if both its arguments are nullable.
-   * An _OR node is nullable if either argument is nullable.
+   * A QMARK or STAR node is nullable.
+   * A PLUS node is nullable if its argument is nullable.
+   * A CAT node is nullable if both its arguments are nullable.
+   * An OR node is nullable if either argument is nullable.
 
    Firstpos:  The firstpos of a node is the set of positions (nonempty leaves)
    that could correspond to the first character of a string matching the
    regexp rooted at the given node.
 
    Firstpos:  The firstpos of a node is the set of positions (nonempty leaves)
    that could correspond to the first character of a string matching the
    regexp rooted at the given node.
-   * _EMPTY leaves have empty firstpos.
+   * EMPTY leaves have empty firstpos.
    * The firstpos of a nonempty leaf is that leaf itself.
    * The firstpos of a nonempty leaf is that leaf itself.
-   * The firstpos of a _QMARK, _STAR, or _PLUS node is the firstpos of its
+   * The firstpos of a QMARK, STAR, or PLUS node is the firstpos of its
      argument.
      argument.
-   * The firstpos of a _CAT node is the firstpos of the left argument, union
+   * The firstpos of a CAT node is the firstpos of the left argument, union
      the firstpos of the right if the left argument is nullable.
      the firstpos of the right if the left argument is nullable.
-   * The firstpos of an _OR node is the union of firstpos of each argument.
+   * The firstpos of an OR node is the union of firstpos of each argument.
 
    Lastpos:  The lastpos of a node is the set of positions that could
    correspond to the last character of a string matching the regexp at
    the given node.
 
    Lastpos:  The lastpos of a node is the set of positions that could
    correspond to the last character of a string matching the regexp at
    the given node.
-   * _EMPTY leaves have empty lastpos.
+   * EMPTY leaves have empty lastpos.
    * The lastpos of a nonempty leaf is that leaf itself.
    * The lastpos of a nonempty leaf is that leaf itself.
-   * The lastpos of a _QMARK, _STAR, or _PLUS node is the lastpos of its
+   * The lastpos of a QMARK, STAR, or PLUS node is the lastpos of its
      argument.
      argument.
-   * The lastpos of a _CAT node is the lastpos of its right argument, union
+   * The lastpos of a CAT node is the lastpos of its right argument, union
      the lastpos of the left if the right argument is nullable.
      the lastpos of the left if the right argument is nullable.
-   * The lastpos of an _OR node is the union of the lastpos of each argument.
+   * The lastpos of an OR node is the union of the lastpos of each argument.
 
    Follow:  The follow of a position is the set of positions that could
    correspond to the character following a character matching the node in
 
    Follow:  The follow of a position is the set of positions that could
    correspond to the character following a character matching the node in
@@ -938,9 +1161,9 @@ epsclosure(s, r)
    Later, if we find that a special symbol is in a follow set, we will
    replace it with the elements of its follow, labeled with an appropriate
    constraint.
    Later, if we find that a special symbol is in a follow set, we will
    replace it with the elements of its follow, labeled with an appropriate
    constraint.
-   * Every node in the firstpos of the argument of a _STAR or _PLUS node is in
+   * Every node in the firstpos of the argument of a STAR or PLUS node is in
      the follow of every node in the lastpos.
      the follow of every node in the lastpos.
-   * Every node in the firstpos of the second argument of a _CAT node is in
+   * Every node in the firstpos of the second argument of a CAT node is in
      the follow of every node in the lastpos of the first argument.
 
    Because of the postfix representation of the parse tree, the depth-first
      the follow of every node in the lastpos of the first argument.
 
    Because of the postfix representation of the parse tree, the depth-first
@@ -949,61 +1172,61 @@ epsclosure(s, r)
    scheme; the number of elements in each set deeper in the stack can be
    used to determine the address of a particular set's array. */
 void
    scheme; the number of elements in each set deeper in the stack can be
    used to determine the address of a particular set's array. */
 void
-reganalyze(r, searchflag)
-     struct regexp *r;
+dfaanalyze(d, searchflag)
+     struct dfa *d;
      int searchflag;
 {
   int *nullable;               /* Nullable stack. */
   int *nfirstpos;              /* Element count stack for firstpos sets. */
      int searchflag;
 {
   int *nullable;               /* Nullable stack. */
   int *nfirstpos;              /* Element count stack for firstpos sets. */
-  _position *firstpos;         /* Array where firstpos elements are stored. */
+  position *firstpos;          /* Array where firstpos elements are stored. */
   int *nlastpos;               /* Element count stack for lastpos sets. */
   int *nlastpos;               /* Element count stack for lastpos sets. */
-  _position *lastpos;          /* Array where lastpos elements are stored. */
+  position *lastpos;           /* Array where lastpos elements are stored. */
   int *nalloc;                 /* Sizes of arrays allocated to follow sets. */
   int *nalloc;                 /* Sizes of arrays allocated to follow sets. */
-  _position_set tmp;           /* Temporary set for merging sets. */
-  _position_set merged;                /* Result of merging sets. */
+  position_set tmp;            /* Temporary set for merging sets. */
+  position_set merged;         /* Result of merging sets. */
   int wants_newline;           /* True if some position wants newline info. */
   int *o_nullable;
   int *o_nfirst, *o_nlast;
   int wants_newline;           /* True if some position wants newline info. */
   int *o_nullable;
   int *o_nfirst, *o_nlast;
-  _position *o_firstpos, *o_lastpos;
+  position *o_firstpos, *o_lastpos;
   int i, j;
   int i, j;
-  _position *pos;
+  position *pos;
 
 #ifdef DEBUG
 
 #ifdef DEBUG
-  fprintf(stderr, "reganalyze:\n");
-  for (i = 0; i < r->tindex; ++i)
+  fprintf(stderr, "dfaanalyze:\n");
+  for (i = 0; i < d->tindex; ++i)
     {
       fprintf(stderr, " %d:", i);
     {
       fprintf(stderr, " %d:", i);
-      prtok(r->tokens[i]);
+      prtok(d->tokens[i]);
     }
   putc('\n', stderr);
 #endif
 
     }
   putc('\n', stderr);
 #endif
 
-  r->searchflag = searchflag;
+  d->searchflag = searchflag;
 
 
-  MALLOC(nullable, int, r->depth);
+  MALLOC(nullable, int, d->depth);
   o_nullable = nullable;
   o_nullable = nullable;
-  MALLOC(nfirstpos, int, r->depth);
+  MALLOC(nfirstpos, int, d->depth);
   o_nfirst = nfirstpos;
   o_nfirst = nfirstpos;
-  MALLOC(firstpos, _position, r->nleaves);
-  o_firstpos = firstpos, firstpos += r->nleaves;
-  MALLOC(nlastpos, int, r->depth);
+  MALLOC(firstpos, position, d->nleaves);
+  o_firstpos = firstpos, firstpos += d->nleaves;
+  MALLOC(nlastpos, int, d->depth);
   o_nlast = nlastpos;
   o_nlast = nlastpos;
-  MALLOC(lastpos, _position, r->nleaves);
-  o_lastpos = lastpos, lastpos += r->nleaves;
-  MALLOC(nalloc, int, r->tindex);
-  for (i = 0; i < r->tindex; ++i)
+  MALLOC(lastpos, position, d->nleaves);
+  o_lastpos = lastpos, lastpos += d->nleaves;
+  MALLOC(nalloc, int, d->tindex);
+  for (i = 0; i < d->tindex; ++i)
     nalloc[i] = 0;
     nalloc[i] = 0;
-  MALLOC(merged.elems, _position, r->nleaves);
+  MALLOC(merged.elems, position, d->nleaves);
 
 
-  CALLOC(r->follows, _position_set, r->tindex);
+  CALLOC(d->follows, position_set, d->tindex);
 
 
-  for (i = 0; i < r->tindex; ++i)
+  for (i = 0; i < d->tindex; ++i)
 #ifdef DEBUG
     {                          /* Nonsyntactic #ifdef goo... */
 #endif
 #ifdef DEBUG
     {                          /* Nonsyntactic #ifdef goo... */
 #endif
-    switch (r->tokens[i])
+    switch (d->tokens[i])
       {
       {
-      case _EMPTY:
+      case EMPTY:
        /* The empty set is nullable. */
        *nullable++ = 1;
 
        /* The empty set is nullable. */
        *nullable++ = 1;
 
@@ -1011,8 +1234,8 @@ reganalyze(r, searchflag)
        *nfirstpos++ = *nlastpos++ = 0;
        break;
 
        *nfirstpos++ = *nlastpos++ = 0;
        break;
 
-      case _STAR:
-      case _PLUS:
+      case STAR:
+      case PLUS:
        /* Every element in the firstpos of the argument is in the follow
           of every element in the lastpos. */
        tmp.nelem = nfirstpos[-1];
        /* Every element in the firstpos of the argument is in the follow
           of every element in the lastpos. */
        tmp.nelem = nfirstpos[-1];
@@ -1020,19 +1243,19 @@ reganalyze(r, searchflag)
        pos = lastpos;
        for (j = 0; j < nlastpos[-1]; ++j)
          {
        pos = lastpos;
        for (j = 0; j < nlastpos[-1]; ++j)
          {
-           merge(&tmp, &r->follows[pos[j].index], &merged);
-           REALLOC_IF_NECESSARY(r->follows[pos[j].index].elems, _position,
+           merge(&tmp, &d->follows[pos[j].index], &merged);
+           REALLOC_IF_NECESSARY(d->follows[pos[j].index].elems, position,
                                 nalloc[pos[j].index], merged.nelem - 1);
                                 nalloc[pos[j].index], merged.nelem - 1);
-           copy(&merged, &r->follows[pos[j].index]);
+           copy(&merged, &d->follows[pos[j].index]);
          }
 
          }
 
-      case _QMARK:
-       /* A _QMARK or _STAR node is automatically nullable. */
-       if (r->tokens[i] != _PLUS)
+      case QMARK:
+       /* A QMARK or STAR node is automatically nullable. */
+       if (d->tokens[i] != PLUS)
          nullable[-1] = 1;
        break;
 
          nullable[-1] = 1;
        break;
 
-      case _CAT:
+      case CAT:
        /* Every element in the firstpos of the second argument is in the
           follow of every element in the lastpos of the first argument. */
        tmp.nelem = nfirstpos[-1];
        /* Every element in the firstpos of the second argument is in the
           follow of every element in the lastpos of the first argument. */
        tmp.nelem = nfirstpos[-1];
@@ -1040,13 +1263,13 @@ reganalyze(r, searchflag)
        pos = lastpos + nlastpos[-1];
        for (j = 0; j < nlastpos[-2]; ++j)
          {
        pos = lastpos + nlastpos[-1];
        for (j = 0; j < nlastpos[-2]; ++j)
          {
-           merge(&tmp, &r->follows[pos[j].index], &merged);
-           REALLOC_IF_NECESSARY(r->follows[pos[j].index].elems, _position,
+           merge(&tmp, &d->follows[pos[j].index], &merged);
+           REALLOC_IF_NECESSARY(d->follows[pos[j].index].elems, position,
                                 nalloc[pos[j].index], merged.nelem - 1);
                                 nalloc[pos[j].index], merged.nelem - 1);
-           copy(&merged, &r->follows[pos[j].index]);
+           copy(&merged, &d->follows[pos[j].index]);
          }
 
          }
 
-       /* The firstpos of a _CAT node is the firstpos of the first argument,
+       /* The firstpos of a CAT node is the firstpos of the first argument,
           union that of the second argument if the first is nullable. */
        if (nullable[-2])
          nfirstpos[-2] += nfirstpos[-1];
           union that of the second argument if the first is nullable. */
        if (nullable[-2])
          nfirstpos[-2] += nfirstpos[-1];
@@ -1054,7 +1277,7 @@ reganalyze(r, searchflag)
          firstpos += nfirstpos[-1];
        --nfirstpos;
 
          firstpos += nfirstpos[-1];
        --nfirstpos;
 
-       /* The lastpos of a _CAT node is the lastpos of the second argument,
+       /* The lastpos of a CAT node is the lastpos of the second argument,
           union that of the first argument if the second is nullable. */
        if (nullable[-1])
          nlastpos[-2] += nlastpos[-1];
           union that of the first argument if the second is nullable. */
        if (nullable[-1])
          nlastpos[-2] += nlastpos[-1];
@@ -1068,12 +1291,13 @@ reganalyze(r, searchflag)
          }
        --nlastpos;
 
          }
        --nlastpos;
 
-       /* A _CAT node is nullable if both arguments are nullable. */
+       /* A CAT node is nullable if both arguments are nullable. */
        nullable[-2] = nullable[-1] && nullable[-2];
        --nullable;
        break;
 
        nullable[-2] = nullable[-1] && nullable[-2];
        --nullable;
        break;
 
-      case _OR:
+      case OR:
+      case ORTOP:
        /* The firstpos is the union of the firstpos of each argument. */
        nfirstpos[-2] += nfirstpos[-1];
        --nfirstpos;
        /* The firstpos is the union of the firstpos of each argument. */
        nfirstpos[-2] += nfirstpos[-1];
        --nfirstpos;
@@ -1082,7 +1306,7 @@ reganalyze(r, searchflag)
        nlastpos[-2] += nlastpos[-1];
        --nlastpos;
 
        nlastpos[-2] += nlastpos[-1];
        --nlastpos;
 
-       /* An _OR node is nullable if either argument is nullable. */
+       /* An OR node is nullable if either argument is nullable. */
        nullable[-2] = nullable[-1] || nullable[-2];
        --nullable;
        break;
        nullable[-2] = nullable[-1] || nullable[-2];
        --nullable;
        break;
@@ -1093,36 +1317,36 @@ reganalyze(r, searchflag)
           an "epsilon closure" effectively makes them nullable later.
           Backreferences have to get a real position so we can detect
           transitions on them later.  But they are nullable. */
           an "epsilon closure" effectively makes them nullable later.
           Backreferences have to get a real position so we can detect
           transitions on them later.  But they are nullable. */
-       *nullable++ = r->tokens[i] == _BACKREF;
+       *nullable++ = d->tokens[i] == BACKREF;
 
        /* This position is in its own firstpos and lastpos. */
        *nfirstpos++ = *nlastpos++ = 1;
        --firstpos, --lastpos;
        firstpos->index = lastpos->index = i;
 
        /* This position is in its own firstpos and lastpos. */
        *nfirstpos++ = *nlastpos++ = 1;
        --firstpos, --lastpos;
        firstpos->index = lastpos->index = i;
-       firstpos->constraint = lastpos->constraint = _NO_CONSTRAINT;
+       firstpos->constraint = lastpos->constraint = NO_CONSTRAINT;
 
        /* Allocate the follow set for this position. */
        nalloc[i] = 1;
 
        /* Allocate the follow set for this position. */
        nalloc[i] = 1;
-       MALLOC(r->follows[i].elems, _position, nalloc[i]);
+       MALLOC(d->follows[i].elems, position, nalloc[i]);
        break;
       }
 #ifdef DEBUG
     /* ... balance the above nonsyntactic #ifdef goo... */
       fprintf(stderr, "node %d:", i);
        break;
       }
 #ifdef DEBUG
     /* ... balance the above nonsyntactic #ifdef goo... */
       fprintf(stderr, "node %d:", i);
-      prtok(r->tokens[i]);
+      prtok(d->tokens[i]);
       putc('\n', stderr);
       fprintf(stderr, nullable[-1] ? " nullable: yes\n" : " nullable: no\n");
       fprintf(stderr, " firstpos:");
       for (j = nfirstpos[-1] - 1; j >= 0; --j)
        {
          fprintf(stderr, " %d:", firstpos[j].index);
       putc('\n', stderr);
       fprintf(stderr, nullable[-1] ? " nullable: yes\n" : " nullable: no\n");
       fprintf(stderr, " firstpos:");
       for (j = nfirstpos[-1] - 1; j >= 0; --j)
        {
          fprintf(stderr, " %d:", firstpos[j].index);
-         prtok(r->tokens[firstpos[j].index]);
+         prtok(d->tokens[firstpos[j].index]);
        }
       fprintf(stderr, "\n lastpos:");
       for (j = nlastpos[-1] - 1; j >= 0; --j)
        {
          fprintf(stderr, " %d:", lastpos[j].index);
        }
       fprintf(stderr, "\n lastpos:");
       for (j = nlastpos[-1] - 1; j >= 0; --j)
        {
          fprintf(stderr, " %d:", lastpos[j].index);
-         prtok(r->tokens[lastpos[j].index]);
+         prtok(d->tokens[lastpos[j].index]);
        }
       putc('\n', stderr);
     }
        }
       putc('\n', stderr);
     }
@@ -1130,26 +1354,26 @@ reganalyze(r, searchflag)
 
   /* For each follow set that is the follow set of a real position, replace
      it with its epsilon closure. */
 
   /* For each follow set that is the follow set of a real position, replace
      it with its epsilon closure. */
-  for (i = 0; i < r->tindex; ++i)
-    if (r->tokens[i] < _NOTCHAR || r->tokens[i] == _BACKREF
-       || r->tokens[i] >= _SET)
+  for (i = 0; i < d->tindex; ++i)
+    if (d->tokens[i] < NOTCHAR || d->tokens[i] == BACKREF
+       || d->tokens[i] >= CSET)
       {
 #ifdef DEBUG
        fprintf(stderr, "follows(%d:", i);
       {
 #ifdef DEBUG
        fprintf(stderr, "follows(%d:", i);
-       prtok(r->tokens[i]);
+       prtok(d->tokens[i]);
        fprintf(stderr, "):");
        fprintf(stderr, "):");
-       for (j = r->follows[i].nelem - 1; j >= 0; --j)
+       for (j = d->follows[i].nelem - 1; j >= 0; --j)
          {
          {
-           fprintf(stderr, " %d:", r->follows[i].elems[j].index);
-           prtok(r->tokens[r->follows[i].elems[j].index]);
+           fprintf(stderr, " %d:", d->follows[i].elems[j].index);
+           prtok(d->tokens[d->follows[i].elems[j].index]);
          }
        putc('\n', stderr);
 #endif
          }
        putc('\n', stderr);
 #endif
-       copy(&r->follows[i], &merged);
-       epsclosure(&merged, r);
-       if (r->follows[i].nelem < merged.nelem)
-         REALLOC(r->follows[i].elems, _position, merged.nelem);
-       copy(&merged, &r->follows[i]);
+       copy(&d->follows[i], &merged);
+       epsclosure(&merged, d);
+       if (d->follows[i].nelem < merged.nelem)
+         REALLOC(d->follows[i].elems, position, merged.nelem);
+       copy(&merged, &d->follows[i]);
       }
 
   /* Get the epsilon closure of the firstpos of the regexp.  The result will
       }
 
   /* Get the epsilon closure of the firstpos of the regexp.  The result will
@@ -1157,19 +1381,19 @@ reganalyze(r, searchflag)
   merged.nelem = 0;
   for (i = 0; i < nfirstpos[-1]; ++i)
     insert(firstpos[i], &merged);
   merged.nelem = 0;
   for (i = 0; i < nfirstpos[-1]; ++i)
     insert(firstpos[i], &merged);
-  epsclosure(&merged, r);
+  epsclosure(&merged, d);
 
   /* Check if any of the positions of state 0 will want newline context. */
   wants_newline = 0;
   for (i = 0; i < merged.nelem; ++i)
 
   /* Check if any of the positions of state 0 will want newline context. */
   wants_newline = 0;
   for (i = 0; i < merged.nelem; ++i)
-    if (_PREV_NEWLINE_DEPENDENT(merged.elems[i].constraint))
+    if (PREV_NEWLINE_DEPENDENT(merged.elems[i].constraint))
       wants_newline = 1;
 
   /* Build the initial state. */
       wants_newline = 1;
 
   /* Build the initial state. */
-  r->salloc = 1;
-  r->sindex = 0;
-  MALLOC(r->states, _dfa_state, r->salloc);
-  state_index(r, &merged, wants_newline, 0);
+  d->salloc = 1;
+  d->sindex = 0;
+  MALLOC(d->states, dfa_state, d->salloc);
+  state_index(d, &merged, wants_newline, 0);
 
   free(o_nullable);
   free(o_nfirst);
 
   free(o_nullable);
   free(o_nfirst);
@@ -1179,8 +1403,8 @@ reganalyze(r, searchflag)
   free(nalloc);
   free(merged.elems);
 }
   free(nalloc);
   free(merged.elems);
 }
-\f
-/* Find, for each character, the transition out of state s of r, and store
+
+/* Find, for each character, the transition out of state s of d, and store
    it in the appropriate slot of trans.
 
    We divide the positions of s into groups (positions can appear in more
    it in the appropriate slot of trans.
 
    We divide the positions of s into groups (positions can appear in more
@@ -1211,25 +1435,25 @@ reganalyze(r, searchflag)
    create a new group labeled with the characters of C and insert this
    position in that group. */
 void
    create a new group labeled with the characters of C and insert this
    position in that group. */
 void
-regstate(s, r, trans)
+dfastate(s, d, trans)
      int s;
      int s;
-     struct regexp *r;
+     struct dfa *d;
      int trans[];
 {
      int trans[];
 {
-  _position_set grps[_NOTCHAR];        /* As many as will ever be needed. */
-  _charset labels[_NOTCHAR];   /* Labels corresponding to the groups. */
+  position_set grps[NOTCHAR];  /* As many as will ever be needed. */
+  charclass labels[NOTCHAR];   /* Labels corresponding to the groups. */
   int ngrps = 0;               /* Number of groups actually used. */
   int ngrps = 0;               /* Number of groups actually used. */
-  _position pos;               /* Current position being considered. */
-  _charset matches;            /* Set of matching characters. */
+  position pos;                        /* Current position being considered. */
+  charclass matches;           /* Set of matching characters. */
   int matchesf;                        /* True if matches is nonempty. */
   int matchesf;                        /* True if matches is nonempty. */
-  _charset intersect;          /* Intersection with some label set. */
+  charclass intersect;         /* Intersection with some label set. */
   int intersectf;              /* True if intersect is nonempty. */
   int intersectf;              /* True if intersect is nonempty. */
-  _charset leftovers;          /* Stuff in the label that didn't match. */
+  charclass leftovers;         /* Stuff in the label that didn't match. */
   int leftoversf;              /* True if leftovers is nonempty. */
   int leftoversf;              /* True if leftovers is nonempty. */
-  static _charset letters;     /* Set of characters considered letters. */
-  static _charset newline;     /* Set of characters that aren't newline. */
-  _position_set follows;       /* Union of the follows of some group. */
-  _position_set tmp;           /* Temporary space for merging sets. */
+  static charclass letters;    /* Set of characters considered letters. */
+  static charclass newline;    /* Set of characters that aren't newline. */
+  position_set follows;                /* Union of the follows of some group. */
+  position_set tmp;            /* Temporary space for merging sets. */
   int state;                   /* New state. */
   int wants_newline;           /* New state wants to know newline context. */
   int state_newline;           /* New state on a newline transition. */
   int state;                   /* New state. */
   int wants_newline;           /* New state wants to know newline context. */
   int state_newline;           /* New state on a newline transition. */
@@ -1242,7 +1466,7 @@ regstate(s, r, trans)
   if (! initialized)
     {
       initialized = 1;
   if (! initialized)
     {
       initialized = 1;
-      for (i = 0; i < _NOTCHAR; ++i)
+      for (i = 0; i < NOTCHAR; ++i)
        if (ISALNUM(i))
          setbit(i, letters);
       setbit('\n', newline);
        if (ISALNUM(i))
          setbit(i, letters);
       setbit('\n', newline);
@@ -1250,13 +1474,13 @@ regstate(s, r, trans)
 
   zeroset(matches);
 
 
   zeroset(matches);
 
-  for (i = 0; i < r->states[s].elems.nelem; ++i)
+  for (i = 0; i < d->states[s].elems.nelem; ++i)
     {
     {
-      pos = r->states[s].elems.elems[i];
-      if (r->tokens[pos.index] >= 0 && r->tokens[pos.index] < _NOTCHAR)
-       setbit(r->tokens[pos.index], matches);
-      else if (r->tokens[pos.index] >= _SET)
-       copyset(r->charsets[r->tokens[pos.index] - _SET], matches);
+      pos = d->states[s].elems.elems[i];
+      if (d->tokens[pos.index] >= 0 && d->tokens[pos.index] < NOTCHAR)
+       setbit(d->tokens[pos.index], matches);
+      else if (d->tokens[pos.index] >= CSET)
+       copyset(d->charclasses[d->tokens[pos.index] - CSET], matches);
       else
        continue;
 
       else
        continue;
 
@@ -1264,26 +1488,26 @@ regstate(s, r, trans)
         they fail in the current context. */
       if (pos.constraint != 0xFF)
        {
         they fail in the current context. */
       if (pos.constraint != 0xFF)
        {
-         if (! _MATCHES_NEWLINE_CONTEXT(pos.constraint,
-                                        r->states[s].newline, 1))
+         if (! MATCHES_NEWLINE_CONTEXT(pos.constraint,
+                                        d->states[s].newline, 1))
            clrbit('\n', matches);
            clrbit('\n', matches);
-         if (! _MATCHES_NEWLINE_CONTEXT(pos.constraint,
-                                        r->states[s].newline, 0))
-           for (j = 0; j < _CHARSET_INTS; ++j)
+         if (! MATCHES_NEWLINE_CONTEXT(pos.constraint,
+                                        d->states[s].newline, 0))
+           for (j = 0; j < CHARCLASS_INTS; ++j)
              matches[j] &= newline[j];
              matches[j] &= newline[j];
-         if (! _MATCHES_LETTER_CONTEXT(pos.constraint,
-                                       r->states[s].letter, 1))
-           for (j = 0; j < _CHARSET_INTS; ++j)
+         if (! MATCHES_LETTER_CONTEXT(pos.constraint,
+                                       d->states[s].letter, 1))
+           for (j = 0; j < CHARCLASS_INTS; ++j)
              matches[j] &= ~letters[j];
              matches[j] &= ~letters[j];
-         if (! _MATCHES_LETTER_CONTEXT(pos.constraint,
-                                       r->states[s].letter, 0))
-           for (j = 0; j < _CHARSET_INTS; ++j)
+         if (! MATCHES_LETTER_CONTEXT(pos.constraint,
+                                       d->states[s].letter, 0))
+           for (j = 0; j < CHARCLASS_INTS; ++j)
              matches[j] &= letters[j];
 
          /* If there are no characters left, there's no point in going on. */
              matches[j] &= letters[j];
 
          /* If there are no characters left, there's no point in going on. */
-         for (j = 0; j < _CHARSET_INTS && !matches[j]; ++j)
+         for (j = 0; j < CHARCLASS_INTS && !matches[j]; ++j)
            ;
            ;
-         if (j == _CHARSET_INTS)
+         if (j == CHARCLASS_INTS)
            continue;
        }
 
            continue;
        }
 
@@ -1292,21 +1516,21 @@ regstate(s, r, trans)
          /* If matches contains a single character only, and the current
             group's label doesn't contain that character, go on to the
             next group. */
          /* If matches contains a single character only, and the current
             group's label doesn't contain that character, go on to the
             next group. */
-         if (r->tokens[pos.index] >= 0 && r->tokens[pos.index] < _NOTCHAR
-             && !tstbit(r->tokens[pos.index], labels[j]))
+         if (d->tokens[pos.index] >= 0 && d->tokens[pos.index] < NOTCHAR
+             && !tstbit(d->tokens[pos.index], labels[j]))
            continue;
 
          /* Check if this group's label has a nonempty intersection with
             matches. */
          intersectf = 0;
            continue;
 
          /* Check if this group's label has a nonempty intersection with
             matches. */
          intersectf = 0;
-         for (k = 0; k < _CHARSET_INTS; ++k)
+         for (k = 0; k < CHARCLASS_INTS; ++k)
            (intersect[k] = matches[k] & labels[j][k]) ? intersectf = 1 : 0;
          if (! intersectf)
            continue;
 
          /* It does; now find the set differences both ways. */
          leftoversf = matchesf = 0;
            (intersect[k] = matches[k] & labels[j][k]) ? intersectf = 1 : 0;
          if (! intersectf)
            continue;
 
          /* It does; now find the set differences both ways. */
          leftoversf = matchesf = 0;
-         for (k = 0; k < _CHARSET_INTS; ++k)
+         for (k = 0; k < CHARCLASS_INTS; ++k)
            {
              /* Even an optimizing compiler can't know this for sure. */
              int match = matches[k], label = labels[j][k];
            {
              /* Even an optimizing compiler can't know this for sure. */
              int match = matches[k], label = labels[j][k];
@@ -1320,7 +1544,7 @@ regstate(s, r, trans)
            {
              copyset(leftovers, labels[ngrps]);
              copyset(intersect, labels[j]);
            {
              copyset(leftovers, labels[ngrps]);
              copyset(intersect, labels[j]);
-             MALLOC(grps[ngrps].elems, _position, r->nleaves);
+             MALLOC(grps[ngrps].elems, position, d->nleaves);
              copy(&grps[j], &grps[ngrps]);
              ++ngrps;
            }
              copy(&grps[j], &grps[ngrps]);
              ++ngrps;
            }
@@ -1341,41 +1565,41 @@ regstate(s, r, trans)
        {
          copyset(matches, labels[ngrps]);
          zeroset(matches);
        {
          copyset(matches, labels[ngrps]);
          zeroset(matches);
-         MALLOC(grps[ngrps].elems, _position, r->nleaves);
+         MALLOC(grps[ngrps].elems, position, d->nleaves);
          grps[ngrps].nelem = 1;
          grps[ngrps].elems[0] = pos;
          ++ngrps;
        }
     }
 
          grps[ngrps].nelem = 1;
          grps[ngrps].elems[0] = pos;
          ++ngrps;
        }
     }
 
-  MALLOC(follows.elems, _position, r->nleaves);
-  MALLOC(tmp.elems, _position, r->nleaves);
+  MALLOC(follows.elems, position, d->nleaves);
+  MALLOC(tmp.elems, position, d->nleaves);
 
   /* If we are a searching matcher, the default transition is to a state
      containing the positions of state 0, otherwise the default transition
      is to fail miserably. */
 
   /* If we are a searching matcher, the default transition is to a state
      containing the positions of state 0, otherwise the default transition
      is to fail miserably. */
-  if (r->searchflag)
+  if (d->searchflag)
     {
       wants_newline = 0;
       wants_letter = 0;
     {
       wants_newline = 0;
       wants_letter = 0;
-      for (i = 0; i < r->states[0].elems.nelem; ++i)
+      for (i = 0; i < d->states[0].elems.nelem; ++i)
        {
        {
-         if (_PREV_NEWLINE_DEPENDENT(r->states[0].elems.elems[i].constraint))
+         if (PREV_NEWLINE_DEPENDENT(d->states[0].elems.elems[i].constraint))
            wants_newline = 1;
            wants_newline = 1;
-         if (_PREV_LETTER_DEPENDENT(r->states[0].elems.elems[i].constraint))
+         if (PREV_LETTER_DEPENDENT(d->states[0].elems.elems[i].constraint))
            wants_letter = 1;
        }
            wants_letter = 1;
        }
-      copy(&r->states[0].elems, &follows);
-      state = state_index(r, &follows, 0, 0);
+      copy(&d->states[0].elems, &follows);
+      state = state_index(d, &follows, 0, 0);
       if (wants_newline)
       if (wants_newline)
-       state_newline = state_index(r, &follows, 1, 0);
+       state_newline = state_index(d, &follows, 1, 0);
       else
        state_newline = state;
       if (wants_letter)
       else
        state_newline = state;
       if (wants_letter)
-       state_letter = state_index(r, &follows, 0, 1);
+       state_letter = state_index(d, &follows, 0, 1);
       else
        state_letter = state;
       else
        state_letter = state;
-      for (i = 0; i < _NOTCHAR; ++i)
+      for (i = 0; i < NOTCHAR; ++i)
        if (i == '\n')
          trans[i] = state_newline;
        else if (ISALNUM(i))
        if (i == '\n')
          trans[i] = state_newline;
        else if (ISALNUM(i))
@@ -1384,7 +1608,7 @@ regstate(s, r, trans)
          trans[i] = state;
     }
   else
          trans[i] = state;
     }
   else
-    for (i = 0; i < _NOTCHAR; ++i)
+    for (i = 0; i < NOTCHAR; ++i)
       trans[i] = -1;
 
   for (i = 0; i < ngrps; ++i)
       trans[i] = -1;
 
   for (i = 0; i < ngrps; ++i)
@@ -1394,44 +1618,44 @@ regstate(s, r, trans)
       /* Find the union of the follows of the positions of the group.
         This is a hideously inefficient loop.  Fix it someday. */
       for (j = 0; j < grps[i].nelem; ++j)
       /* Find the union of the follows of the positions of the group.
         This is a hideously inefficient loop.  Fix it someday. */
       for (j = 0; j < grps[i].nelem; ++j)
-       for (k = 0; k < r->follows[grps[i].elems[j].index].nelem; ++k)
-         insert(r->follows[grps[i].elems[j].index].elems[k], &follows);
+       for (k = 0; k < d->follows[grps[i].elems[j].index].nelem; ++k)
+         insert(d->follows[grps[i].elems[j].index].elems[k], &follows);
 
       /* If we are building a searching matcher, throw in the positions
         of state 0 as well. */
 
       /* If we are building a searching matcher, throw in the positions
         of state 0 as well. */
-      if (r->searchflag)
-       for (j = 0; j < r->states[0].elems.nelem; ++j)
-         insert(r->states[0].elems.elems[j], &follows);
+      if (d->searchflag)
+       for (j = 0; j < d->states[0].elems.nelem; ++j)
+         insert(d->states[0].elems.elems[j], &follows);
 
       /* Find out if the new state will want any context information. */
       wants_newline = 0;
       if (tstbit('\n', labels[i]))
        for (j = 0; j < follows.nelem; ++j)
 
       /* Find out if the new state will want any context information. */
       wants_newline = 0;
       if (tstbit('\n', labels[i]))
        for (j = 0; j < follows.nelem; ++j)
-         if (_PREV_NEWLINE_DEPENDENT(follows.elems[j].constraint))
+         if (PREV_NEWLINE_DEPENDENT(follows.elems[j].constraint))
            wants_newline = 1;
 
       wants_letter = 0;
            wants_newline = 1;
 
       wants_letter = 0;
-      for (j = 0; j < _CHARSET_INTS; ++j)
+      for (j = 0; j < CHARCLASS_INTS; ++j)
        if (labels[i][j] & letters[j])
          break;
        if (labels[i][j] & letters[j])
          break;
-      if (j < _CHARSET_INTS)
+      if (j < CHARCLASS_INTS)
        for (j = 0; j < follows.nelem; ++j)
        for (j = 0; j < follows.nelem; ++j)
-         if (_PREV_LETTER_DEPENDENT(follows.elems[j].constraint))
+         if (PREV_LETTER_DEPENDENT(follows.elems[j].constraint))
            wants_letter = 1;
 
       /* Find the state(s) corresponding to the union of the follows. */
            wants_letter = 1;
 
       /* Find the state(s) corresponding to the union of the follows. */
-      state = state_index(r, &follows, 0, 0);
+      state = state_index(d, &follows, 0, 0);
       if (wants_newline)
       if (wants_newline)
-       state_newline = state_index(r, &follows, 1, 0);
+       state_newline = state_index(d, &follows, 1, 0);
       else
        state_newline = state;
       if (wants_letter)
       else
        state_newline = state;
       if (wants_letter)
-       state_letter = state_index(r, &follows, 0, 1);
+       state_letter = state_index(d, &follows, 0, 1);
       else
        state_letter = state;
 
       /* Set the transitions for each character in the current label. */
       else
        state_letter = state;
 
       /* Set the transitions for each character in the current label. */
-      for (j = 0; j < _CHARSET_INTS; ++j)
+      for (j = 0; j < CHARCLASS_INTS; ++j)
        for (k = 0; k < INTBITS; ++k)
          if (labels[i][j] & 1 << k)
            {
        for (k = 0; k < INTBITS; ++k)
          if (labels[i][j] & 1 << k)
            {
@@ -1441,7 +1665,7 @@ regstate(s, r, trans)
                trans[c] = state_newline;
              else if (ISALNUM(c))
                trans[c] = state_letter;
                trans[c] = state_newline;
              else if (ISALNUM(c))
                trans[c] = state_letter;
-             else if (c < _NOTCHAR)
+             else if (c < NOTCHAR)
                trans[c] = state;
            }
     }
                trans[c] = state;
            }
     }
@@ -1451,18 +1675,18 @@ regstate(s, r, trans)
   free(follows.elems);
   free(tmp.elems);
 }
   free(follows.elems);
   free(tmp.elems);
 }
-\f
-/* Some routines for manipulating a compiled regexp's transition tables.
+
+/* Some routines for manipulating a compiled dfa's transition tables.
    Each state may or may not have a transition table; if it does, and it
    Each state may or may not have a transition table; if it does, and it
-   is a non-accepting state, then r->trans[state] points to its table.
-   If it is an accepting state then r->fails[state] points to its table.
-   If it has no table at all, then r->trans[state] is NULL.
+   is a non-accepting state, then d->trans[state] points to its table.
+   If it is an accepting state then d->fails[state] points to its table.
+   If it has no table at all, then d->trans[state] is NULL.
    TODO: Improve this comment, get rid of the unnecessary redundancy. */
 
 static void
    TODO: Improve this comment, get rid of the unnecessary redundancy. */
 
 static void
-build_state(s, r)
+build_state(s, d)
      int s;
      int s;
-     struct regexp *r;
+     struct dfa *d;
 {
   int *trans;                  /* The new transition table. */
   int i;
 {
   int *trans;                  /* The new transition table. */
   int i;
@@ -1471,87 +1695,87 @@ build_state(s, r)
      exist at once.  1024 is arbitrary.  The idea is that the frequently
      used transition tables will be quickly rebuilt, whereas the ones that
      were only needed once or twice will be cleared away. */
      exist at once.  1024 is arbitrary.  The idea is that the frequently
      used transition tables will be quickly rebuilt, whereas the ones that
      were only needed once or twice will be cleared away. */
-  if (r->trcount >= 1024)
+  if (d->trcount >= 1024)
     {
     {
-      for (i = 0; i < r->tralloc; ++i)
-       if (r->trans[i])
+      for (i = 0; i < d->tralloc; ++i)
+       if (d->trans[i])
          {
          {
-           free((ptr_t) r->trans[i]);
-           r->trans[i] = NULL;
+           free((ptr_t) d->trans[i]);
+           d->trans[i] = NULL;
          }
          }
-       else if (r->fails[i])
+       else if (d->fails[i])
          {
          {
-           free((ptr_t) r->fails[i]);
-           r->fails[i] = NULL;
+           free((ptr_t) d->fails[i]);
+           d->fails[i] = NULL;
          }
          }
-      r->trcount = 0;
+      d->trcount = 0;
     }
 
     }
 
-  ++r->trcount;
+  ++d->trcount;
 
   /* Set up the success bits for this state. */
 
   /* Set up the success bits for this state. */
-  r->success[s] = 0;
-  if (ACCEPTS_IN_CONTEXT(r->states[s].newline, 1, r->states[s].letter, 0,
-      s, *r))
-    r->success[s] |= 4;
-  if (ACCEPTS_IN_CONTEXT(r->states[s].newline, 0, r->states[s].letter, 1,
-      s, *r))
-    r->success[s] |= 2;
-  if (ACCEPTS_IN_CONTEXT(r->states[s].newline, 0, r->states[s].letter, 0,
-      s, *r))
-    r->success[s] |= 1;
-
-  MALLOC(trans, int, _NOTCHAR);
-  regstate(s, r, trans);
+  d->success[s] = 0;
+  if (ACCEPTS_IN_CONTEXT(d->states[s].newline, 1, d->states[s].letter, 0,
+      s, *d))
+    d->success[s] |= 4;
+  if (ACCEPTS_IN_CONTEXT(d->states[s].newline, 0, d->states[s].letter, 1,
+      s, *d))
+    d->success[s] |= 2;
+  if (ACCEPTS_IN_CONTEXT(d->states[s].newline, 0, d->states[s].letter, 0,
+      s, *d))
+    d->success[s] |= 1;
+
+  MALLOC(trans, int, NOTCHAR);
+  dfastate(s, d, trans);
 
   /* Now go through the new transition table, and make sure that the trans
      and fail arrays are allocated large enough to hold a pointer for the
      largest state mentioned in the table. */
 
   /* Now go through the new transition table, and make sure that the trans
      and fail arrays are allocated large enough to hold a pointer for the
      largest state mentioned in the table. */
-  for (i = 0; i < _NOTCHAR; ++i)
-    if (trans[i] >= r->tralloc)
+  for (i = 0; i < NOTCHAR; ++i)
+    if (trans[i] >= d->tralloc)
       {
       {
-       int oldalloc = r->tralloc;
-
-       while (trans[i] >= r->tralloc)
-         r->tralloc *= 2;
-       REALLOC(r->realtrans, int *, r->tralloc + 1);
-       r->trans = r->realtrans + 1;
-       REALLOC(r->fails, int *, r->tralloc);
-       REALLOC(r->success, int, r->tralloc);
-       REALLOC(r->newlines, int, r->tralloc);
-       while (oldalloc < r->tralloc)
+       int oldalloc = d->tralloc;
+
+       while (trans[i] >= d->tralloc)
+         d->tralloc *= 2;
+       REALLOC(d->realtrans, int *, d->tralloc + 1);
+       d->trans = d->realtrans + 1;
+       REALLOC(d->fails, int *, d->tralloc);
+       REALLOC(d->success, int, d->tralloc);
+       REALLOC(d->newlines, int, d->tralloc);
+       while (oldalloc < d->tralloc)
          {
          {
-           r->trans[oldalloc] = NULL;
-           r->fails[oldalloc++] = NULL;
+           d->trans[oldalloc] = NULL;
+           d->fails[oldalloc++] = NULL;
          }
       }
 
   /* Keep the newline transition in a special place so we can use it as
      a sentinel. */
          }
       }
 
   /* Keep the newline transition in a special place so we can use it as
      a sentinel. */
-  r->newlines[s] = trans['\n'];
+  d->newlines[s] = trans['\n'];
   trans['\n'] = -1;
 
   trans['\n'] = -1;
 
-  if (ACCEPTING(s, *r))
-    r->fails[s] = trans;
+  if (ACCEPTING(s, *d))
+    d->fails[s] = trans;
   else
   else
-    r->trans[s] = trans;
+    d->trans[s] = trans;
 }
 
 static void
 }
 
 static void
-build_state_zero(r)
-     struct regexp *r;
+build_state_zero(d)
+     struct dfa *d;
 {
 {
-  r->tralloc = 1;
-  r->trcount = 0;
-  CALLOC(r->realtrans, int *, r->tralloc + 1);
-  r->trans = r->realtrans + 1;
-  CALLOC(r->fails, int *, r->tralloc);
-  MALLOC(r->success, int, r->tralloc);
-  MALLOC(r->newlines, int, r->tralloc);
-  build_state(0, r);
+  d->tralloc = 1;
+  d->trcount = 0;
+  CALLOC(d->realtrans, int *, d->tralloc + 1);
+  d->trans = d->realtrans + 1;
+  CALLOC(d->fails, int *, d->tralloc);
+  MALLOC(d->success, int, d->tralloc);
+  MALLOC(d->newlines, int, d->tralloc);
+  build_state(0, d);
 }
 }
-\f
-/* Search through a buffer looking for a match to the given struct regexp.
+
+/* Search through a buffer looking for a match to the given struct dfa.
    Find the first occurrence of a string matching the regexp in the buffer,
    and the shortest possible version thereof.  Return a pointer to the first
    character after the match, or NULL if none is found.  Begin points to
    Find the first occurrence of a string matching the regexp in the buffer,
    and the shortest possible version thereof.  Return a pointer to the first
    character after the match, or NULL if none is found.  Begin points to
@@ -1565,8 +1789,8 @@ build_state_zero(r)
    match needs to be verified by a backtracking matcher.  Otherwise
    we store a 0 in *backref. */
 char *
    match needs to be verified by a backtracking matcher.  Otherwise
    we store a 0 in *backref. */
 char *
-regexecute(r, begin, end, newline, count, backref)
-     struct regexp *r;
+dfaexec(d, begin, end, newline, count, backref)
+     struct dfa *d;
      char *begin;
      char *end;
      int newline;
      char *begin;
      char *end;
      int newline;
@@ -1575,9 +1799,9 @@ regexecute(r, begin, end, newline, count, backref)
 {
   register s, s1, tmp;         /* Current state. */
   register unsigned char *p;   /* Current input character. */
 {
   register s, s1, tmp;         /* Current state. */
   register unsigned char *p;   /* Current input character. */
-  register **trans, *t;                /* Copy of r->trans so it can be optimized
+  register **trans, *t;                /* Copy of d->trans so it can be optimized
                                   into a register. */
                                   into a register. */
-  static sbit[_NOTCHAR];       /* Table for anding with r->success. */
+  static sbit[NOTCHAR];        /* Table for anding with d->success. */
   static sbit_init;
 
   if (! sbit_init)
   static sbit_init;
 
   if (! sbit_init)
@@ -1585,7 +1809,7 @@ regexecute(r, begin, end, newline, count, backref)
       int i;
 
       sbit_init = 1;
       int i;
 
       sbit_init = 1;
-      for (i = 0; i < _NOTCHAR; ++i)
+      for (i = 0; i < NOTCHAR; ++i)
        if (i == '\n')
          sbit[i] = 4;
        else if (ISALNUM(i))
        if (i == '\n')
          sbit[i] = 4;
        else if (ISALNUM(i))
@@ -1594,18 +1818,18 @@ regexecute(r, begin, end, newline, count, backref)
          sbit[i] = 1;
     }
 
          sbit[i] = 1;
     }
 
-  if (! r->tralloc)
-    build_state_zero(r);
+  if (! d->tralloc)
+    build_state_zero(d);
 
 
-  s = 0;
+  s = s1 = 0;
   p = (unsigned char *) begin;
   p = (unsigned char *) begin;
-  trans = r->trans;
+  trans = d->trans;
   *end = '\n';
 
   for (;;)
     {
       /* The dreaded inner loop. */
   *end = '\n';
 
   for (;;)
     {
       /* The dreaded inner loop. */
-      if (t = trans[s])
+      if ((t = trans[s]) != 0)
        do
          {
            s1 = t[*p++];
        do
          {
            s1 = t[*p++];
@@ -1613,18 +1837,18 @@ regexecute(r, begin, end, newline, count, backref)
              goto last_was_s;
            s = t[*p++];
          }
              goto last_was_s;
            s = t[*p++];
          }
-        while (t = trans[s]);
+        while ((t = trans[s]) != 0);
       goto last_was_s1;
     last_was_s:
       tmp = s, s = s1, s1 = tmp;
     last_was_s1:
 
       goto last_was_s1;
     last_was_s:
       tmp = s, s = s1, s1 = tmp;
     last_was_s1:
 
-      if (s >= 0 && p <= (unsigned char *) end && r->fails[s])
+      if (s >= 0 && p <= (unsigned char *) end && d->fails[s])
        {
        {
-         if (r->success[s] & sbit[*p])
+         if (d->success[s] & sbit[*p])
            {
              if (backref)
            {
              if (backref)
-               if (r->states[s].backref)
+               if (d->states[s].backref)
                  *backref = 1;
                else
                  *backref = 0;
                  *backref = 1;
                else
                  *backref = 0;
@@ -1632,7 +1856,7 @@ regexecute(r, begin, end, newline, count, backref)
            }
 
          s1 = s;
            }
 
          s1 = s;
-         s = r->fails[s][*p++];
+         s = d->fails[s][*p++];
          continue;
        }
 
          continue;
        }
 
@@ -1641,63 +1865,64 @@ regexecute(r, begin, end, newline, count, backref)
        ++*count;
 
       /* Check if we've run off the end of the buffer. */
        ++*count;
 
       /* Check if we've run off the end of the buffer. */
-      if ((char *) p >= end)
+      if ((char *) p > end)
        return NULL;
 
       if (s >= 0)
        {
        return NULL;
 
       if (s >= 0)
        {
-         build_state(s, r);
-         trans = r->trans;
+         build_state(s, d);
+         trans = d->trans;
          continue;
        }
 
       if (p[-1] == '\n' && newline)
        {
          continue;
        }
 
       if (p[-1] == '\n' && newline)
        {
-         s = r->newlines[s1];
+         s = d->newlines[s1];
          continue;
        }
 
       s = 0;
     }
 }
          continue;
        }
 
       s = 0;
     }
 }
-\f
-/* Initialize the components of a regexp that the other routines don't
+
+/* Initialize the components of a dfa that the other routines don't
    initialize for themselves. */
 void
    initialize for themselves. */
 void
-reginit(r)
-     struct regexp *r;
+dfainit(d)
+     struct dfa *d;
 {
 {
-  r->calloc = 1;
-  MALLOC(r->charsets, _charset, r->calloc);
-  r->cindex = 0;
+  d->calloc = 1;
+  MALLOC(d->charclasses, charclass, d->calloc);
+  d->cindex = 0;
 
 
-  r->talloc = 1;
-  MALLOC(r->tokens, _token, r->talloc);
-  r->tindex = r->depth = r->nleaves = r->nregexps = 0;
+  d->talloc = 1;
+  MALLOC(d->tokens, token, d->talloc);
+  d->tindex = d->depth = d->nleaves = d->nregexps = 0;
 
 
-  r->searchflag = 0;
-  r->tralloc = 0;
+  d->searchflag = 0;
+  d->tralloc = 0;
+
+  d->musts = 0;
 }
 
 /* Parse and analyze a single string of the given length. */
 void
 }
 
 /* Parse and analyze a single string of the given length. */
 void
-regcompile(s, len, r, searchflag)
-     const char *s;
+dfacomp(s, len, d, searchflag)
+     char *s;
      size_t len;
      size_t len;
-     struct regexp *r;
+     struct dfa *d;
      int searchflag;
 {
      int searchflag;
 {
-  if (case_fold)       /* dummy folding in service of regmust() */
+  if (case_fold)       /* dummy folding in service of dfamust() */
     {
       char *copy;
       int i;
 
       copy = malloc(len);
       if (!copy)
     {
       char *copy;
       int i;
 
       copy = malloc(len);
       if (!copy)
-       regerror("out of memory");
+       dfaerror("out of memory");
       
       
-      /* This is a complete kludge and could potentially break
-        \<letter> escapes . . . */
+      /* This is a kludge. */
       case_fold = 0;
       for (i = 0; i < len; ++i)
        if (ISUPPER(s[i]))
       case_fold = 0;
       for (i = 0; i < len; ++i)
        if (ISUPPER(s[i]))
@@ -1705,572 +1930,596 @@ regcompile(s, len, r, searchflag)
        else
          copy[i] = s[i];
 
        else
          copy[i] = s[i];
 
-      reginit(r);
-      r->mustn = 0;
-      r->must[0] = '\0';
-      regparse(copy, len, r);
+      dfainit(d);
+      dfaparse(copy, len, d);
       free(copy);
       free(copy);
-      regmust(r);
-      reganalyze(r, searchflag);
+      dfamust(d);
+      d->cindex = d->tindex = d->depth = d->nleaves = d->nregexps = 0;
       case_fold = 1;
       case_fold = 1;
-      reginit(r);
-      regparse(s, len, r);
-      reganalyze(r, searchflag);
+      dfaparse(s, len, d);
+      dfaanalyze(d, searchflag);
     }
   else
     {
     }
   else
     {
-        reginit(r);
-        regparse(s, len, r);
-        regmust(r);
-        reganalyze(r, searchflag);
+        dfainit(d);
+        dfaparse(s, len, d);
+       dfamust(d);
+        dfaanalyze(d, searchflag);
     }
 }
 
     }
 }
 
-/* Free the storage held by the components of a regexp. */
+/* Free the storage held by the components of a dfa. */
 void
 void
-regfree(r)
-     struct regexp *r;
+dfafree(d)
+     struct dfa *d;
 {
   int i;
 {
   int i;
-
-  free((ptr_t) r->charsets);
-  free((ptr_t) r->tokens);
-  for (i = 0; i < r->sindex; ++i)
-    free((ptr_t) r->states[i].elems.elems);
-  free((ptr_t) r->states);
-  for (i = 0; i < r->tindex; ++i)
-    if (r->follows[i].elems)
-      free((ptr_t) r->follows[i].elems);
-  free((ptr_t) r->follows);
-  for (i = 0; i < r->tralloc; ++i)
-    if (r->trans[i])
-      free((ptr_t) r->trans[i]);
-    else if (r->fails[i])
-      free((ptr_t) r->fails[i]);
-  free((ptr_t) r->realtrans);
-  free((ptr_t) r->fails);
-  free((ptr_t) r->newlines);
+  struct dfamust *dm, *ndm;
+
+  free((ptr_t) d->charclasses);
+  free((ptr_t) d->tokens);
+  for (i = 0; i < d->sindex; ++i)
+    free((ptr_t) d->states[i].elems.elems);
+  free((ptr_t) d->states);
+  for (i = 0; i < d->tindex; ++i)
+    if (d->follows[i].elems)
+      free((ptr_t) d->follows[i].elems);
+  free((ptr_t) d->follows);
+  for (i = 0; i < d->tralloc; ++i)
+    if (d->trans[i])
+      free((ptr_t) d->trans[i]);
+    else if (d->fails[i])
+      free((ptr_t) d->fails[i]);
+  free((ptr_t) d->realtrans);
+  free((ptr_t) d->fails);
+  free((ptr_t) d->newlines);
+  for (dm = d->musts; dm; dm = ndm)
+    {
+      ndm = dm->next;
+      free(dm->must);
+      free((ptr_t) dm);
+    }
 }
 
 }
 
-/*
-Having found the postfix representation of the regular expression,
-try to find a long sequence of characters that must appear in any line
-containing the r.e.
-Finding a "longest" sequence is beyond the scope here;
-we take an easy way out and hope for the best.
-(Take "(ab|a)b"--please.)
-
-We do a bottom-up calculation of sequences of characters that must appear
-in matches of r.e.'s represented by trees rooted at the nodes of the postfix
-representation:
+/* Having found the postfix representation of the regular expression,
+   try to find a long sequence of characters that must appear in any line
+   containing the r.e.
+   Finding a "longest" sequence is beyond the scope here;
+   we take an easy way out and hope for the best.
+   (Take "(ab|a)b"--please.)
+
+   We do a bottom-up calculation of sequences of characters that must appear
+   in matches of r.e.'s represented by trees rooted at the nodes of the postfix
+   representation:
        sequences that must appear at the left of the match ("left")
        sequences that must appear at the right of the match ("right")
        lists of sequences that must appear somewhere in the match ("in")
        sequences that must constitute the match ("is")
        sequences that must appear at the left of the match ("left")
        sequences that must appear at the right of the match ("right")
        lists of sequences that must appear somewhere in the match ("in")
        sequences that must constitute the match ("is")
-When we get to the root of the tree, we use one of the longest of its
-calculated "in" sequences as our answer.  The sequence we find is returned in
-r->must (where "r" is the single argument passed to "regmust");
-the length of the sequence is returned in r->mustn.
-
-The sequences calculated for the various types of node (in pseudo ANSI c)
-are shown below.  "p" is the operand of unary operators (and the left-hand
-operand of binary operators); "q" is the right-hand operand of binary operators
-.
-"ZERO" means "a zero-length sequence" below.
-
-Type   left            right           is              in
-----   ----            -----           --              --
-char c # c             # c             # c             # c
-
-SET    ZERO            ZERO            ZERO            ZERO
-
-STAR   ZERO            ZERO            ZERO            ZERO
-
-QMARK  ZERO            ZERO            ZERO            ZERO
-
-PLUS   p->left         p->right        ZERO            p->in
-
-CAT    (p->is==ZERO)?  (q->is==ZERO)?  (p->is!=ZERO && p->in plus
-       p->left :       q->right :      q->is!=ZERO) ?  q->in plus
-       p->is##q->left  p->right##q->is p->is##q->is :  p->right##q->left
-                                       ZERO
-
-OR     longest common  longest common  (do p->is and   substrings common to
-       leading         trailing        q->is have same p->in and q->in
-       (sub)sequence   (sub)sequence   length and      
-       of p->left      of p->right     content) ?      
-       and q->left     and q->right    p->is : NULL    
-
-If there's anything else we recognize in the tree, all four sequences get set
-to zero-length sequences.  If there's something we don't recognize in the tree,
-we just return a zero-length sequence.
 
 
-Break ties in favor of infrequent letters (choosing 'zzz' in preference to
-'aaa')?
-
-And. . .is it here or someplace that we might ponder "optimizations" such as
+   When we get to the root of the tree, we use one of the longest of its
+   calculated "in" sequences as our answer.  The sequence we find is returned in
+   d->must (where "d" is the single argument passed to "dfamust");
+   the length of the sequence is returned in d->mustn.
+
+   The sequences calculated for the various types of node (in pseudo ANSI c)
+   are shown below.  "p" is the operand of unary operators (and the left-hand
+   operand of binary operators); "q" is the right-hand operand of binary
+   operators.
+
+   "ZERO" means "a zero-length sequence" below.
+
+       Type    left            right           is              in
+       ----    ----            -----           --              --
+       char c  # c             # c             # c             # c
+       
+       CSET    ZERO            ZERO            ZERO            ZERO
+       
+       STAR    ZERO            ZERO            ZERO            ZERO
+
+       QMARK   ZERO            ZERO            ZERO            ZERO
+
+       PLUS    p->left         p->right        ZERO            p->in
+
+       CAT     (p->is==ZERO)?  (q->is==ZERO)?  (p->is!=ZERO && p->in plus
+               p->left :       q->right :      q->is!=ZERO) ?  q->in plus
+               p->is##q->left  p->right##q->is p->is##q->is :  p->right##q->left
+                                               ZERO
+                                       
+       OR      longest common  longest common  (do p->is and   substrings common to
+               leading         trailing        q->is have same p->in and q->in
+               (sub)sequence   (sub)sequence   length and      
+               of p->left      of p->right     content) ?      
+               and q->left     and q->right    p->is : NULL    
+
+   If there's anything else we recognize in the tree, all four sequences get set
+   to zero-length sequences.  If there's something we don't recognize in the tree,
+   we just return a zero-length sequence.
+
+   Break ties in favor of infrequent letters (choosing 'zzz' in preference to
+   'aaa')?
+
+   And. . .is it here or someplace that we might ponder "optimizations" such as
        egrep 'psi|epsilon'     ->      egrep 'psi'
        egrep 'pepsi|epsilon'   ->      egrep 'epsi'
                                        (Yes, we now find "epsi" as a "string
                                        that must occur", but we might also
        egrep 'psi|epsilon'     ->      egrep 'psi'
        egrep 'pepsi|epsilon'   ->      egrep 'epsi'
                                        (Yes, we now find "epsi" as a "string
                                        that must occur", but we might also
-                                       simplify the *entire* r.e. being sought
-)
+                                       simplify the *entire* r.e. being sought)
        grep '[c]'              ->      grep 'c'
        grep '(ab|a)b'          ->      grep 'ab'
        grep 'ab*'              ->      grep 'a'
        grep 'a*b'              ->      grep 'b'
        grep '[c]'              ->      grep 'c'
        grep '(ab|a)b'          ->      grep 'ab'
        grep 'ab*'              ->      grep 'a'
        grep 'a*b'              ->      grep 'b'
-There are several issues:
-       Is optimization easy (enough)?
 
 
-       Does optimization actually accomplish anything,
-       or is the automaton you get from "psi|epsilon" (for example)
-       the same as the one you get from "psi" (for example)?
+   There are several issues:
+
+   Is optimization easy (enough)?
 
 
-       Are optimizable r.e.'s likely to be used in real-life situations
-       (something like 'ab*' is probably unlikely; something like is
-       'psi|epsilon' is likelier)?
-*/
+   Does optimization actually accomplish anything,
+   or is the automaton you get from "psi|epsilon" (for example)
+   the same as the one you get from "psi" (for example)?
+  
+   Are optimizable r.e.'s likely to be used in real-life situations
+   (something like 'ab*' is probably unlikely; something like is
+   'psi|epsilon' is likelier)? */
 
 static char *
 icatalloc(old, new)
 
 static char *
 icatalloc(old, new)
-char * old;
-char * new;
+     char *old;
+     char *new;
 {
 {
-       register char * result;
-       register int    oldsize, newsize;
-
-       newsize = (new == NULL) ? 0 : strlen(new);
-       if (old == NULL)
-               oldsize = 0;
-       else if (newsize == 0)
-               return old;
-       else    oldsize = strlen(old);
-       if (old == NULL)
-               result = (char *) malloc(newsize + 1);
-       else    result = (char *) realloc((void *) old, oldsize + newsize + 1);
-       if (result != NULL && new != NULL)
-               (void) strcpy(result + oldsize, new);
-       return result;
+  char *result;
+  int oldsize, newsize;
+
+  newsize = (new == NULL) ? 0 : strlen(new);
+  if (old == NULL)
+    oldsize = 0;
+  else if (newsize == 0)
+    return old;
+  else oldsize = strlen(old);
+  if (old == NULL)
+    result = (char *) malloc(newsize + 1);
+  else
+    result = (char *) realloc((void *) old, oldsize + newsize + 1);
+  if (result != NULL && new != NULL)
+    (void) strcpy(result + oldsize, new);
+  return result;
 }
 
 static char *
 icpyalloc(string)
 }
 
 static char *
 icpyalloc(string)
-const char *   string;
+     char *string;
 {
 {
-       return icatalloc((char *) NULL, string);
+  return icatalloc((char *) NULL, string);
 }
 
 static char *
 istrstr(lookin, lookfor)
 }
 
 static char *
 istrstr(lookin, lookfor)
-char *         lookin;
-register char *        lookfor;
+     char *lookin;
+     char *lookfor;
 {
 {
-       register char * cp;
-       register int    len;
-
-       len = strlen(lookfor);
-       for (cp = lookin; *cp != '\0'; ++cp)
-               if (strncmp(cp, lookfor, len) == 0)
-                       return cp;
-       return NULL;
+  char *cp;
+  int len;
+
+  len = strlen(lookfor);
+  for (cp = lookin; *cp != '\0'; ++cp)
+    if (strncmp(cp, lookfor, len) == 0)
+      return cp;
+  return NULL;
 }
 
 static void
 ifree(cp)
 }
 
 static void
 ifree(cp)
-char * cp;
+     char *cp;
 {
 {
-       if (cp != NULL)
-               free(cp);
+  if (cp != NULL)
+    free(cp);
 }
 
 static void
 freelist(cpp)
 }
 
 static void
 freelist(cpp)
-register char **       cpp;
+     char **cpp;
 {
 {
-       register int    i;
+  int i;
 
 
-       if (cpp == NULL)
-               return;
-       for (i = 0; cpp[i] != NULL; ++i) {
-               free(cpp[i]);
-               cpp[i] = NULL;
-       }
+  if (cpp == NULL)
+    return;
+  for (i = 0; cpp[i] != NULL; ++i)
+    {
+      free(cpp[i]);
+      cpp[i] = NULL;
+    }
 }
 
 static char **
 enlist(cpp, new, len)
 }
 
 static char **
 enlist(cpp, new, len)
-register char **       cpp;
-register char *                new;
-int len;
+     char **cpp;
+     char *new;
+     int len;
 {
 {
-       register int    i, j;
+  int i, j;
 
 
-       if (cpp == NULL)
-               return NULL;
-       if ((new = icpyalloc(new)) == NULL) {
-               freelist(cpp);
-               return NULL;
-       }
-       new[len] = '\0';
-       /*
-       ** Is there already something in the list that's new (or longer)?
-       */
-       for (i = 0; cpp[i] != NULL; ++i)
-               if (istrstr(cpp[i], new) != NULL) {
-                       free(new);
-                       return cpp;
-               }
-       /*
-       ** Eliminate any obsoleted strings.
-       */
-       j = 0;
-       while (cpp[j] != NULL)
-               if (istrstr(new, cpp[j]) == NULL)
-                       ++j;
-               else {
-                       free(cpp[j]);
-                       if (--i == j)
-                               break;
-                       cpp[j] = cpp[i];
-                       cpp[i] = 0;
-               }
-       /*
-       ** Add the new string.
-       */
-       cpp = (char **) realloc((char *) cpp, (i + 2) * sizeof *cpp);
-       if (cpp == NULL)
-               return NULL;
-       cpp[i] = new;
-       cpp[i + 1] = NULL;
+  if (cpp == NULL)
+    return NULL;
+  if ((new = icpyalloc(new)) == NULL)
+    {
+      freelist(cpp);
+      return NULL;
+    }
+  new[len] = '\0';
+  /* Is there already something in the list that's new (or longer)? */
+  for (i = 0; cpp[i] != NULL; ++i)
+    if (istrstr(cpp[i], new) != NULL)
+      {
+       free(new);
        return cpp;
        return cpp;
+      }
+  /* Eliminate any obsoleted strings. */
+  j = 0;
+  while (cpp[j] != NULL)
+    if (istrstr(new, cpp[j]) == NULL)
+      ++j;
+    else
+      {
+       free(cpp[j]);
+       if (--i == j)
+         break;
+       cpp[j] = cpp[i];
+       cpp[i] = NULL;
+      }
+  /* Add the new string. */
+  cpp = (char **) realloc((char *) cpp, (i + 2) * sizeof *cpp);
+  if (cpp == NULL)
+    return NULL;
+  cpp[i] = new;
+  cpp[i + 1] = NULL;
+  return cpp;
 }
 
 }
 
-/*
-** Given pointers to two strings,
-** return a pointer to an allocated list of their distinct common substrings.
-** Return NULL if something seems wild.
-*/
-
+/* Given pointers to two strings, return a pointer to an allocated
+   list of their distinct common substrings. Return NULL if something
+   seems wild. */
 static char **
 comsubs(left, right)
 static char **
 comsubs(left, right)
-char * left;
-char * right;
+     char *left;
+     char *right;
 {
 {
-       register char **        cpp;
-       register char *         lcp;
-       register char *         rcp;
-       register int            i, len;
-
-       if (left == NULL || right == NULL)
-               return NULL;
-       cpp = (char **) malloc(sizeof *cpp);
-       if (cpp == NULL)
-               return NULL;
-       cpp[0] = NULL;
-       for (lcp = left; *lcp != '\0'; ++lcp) {
-               len = 0;
-               rcp = index(right, *lcp);
-               while (rcp != NULL) {
-                       for (i = 1; lcp[i] != '\0' && lcp[i] == rcp[i]; ++i)
-                               ;
-                       if (i > len)
-                               len = i;
-                       rcp = index(rcp + 1, *lcp);
-               }
-               if (len == 0)
-                       continue;
-               if ((cpp = enlist(cpp, lcp, len)) == NULL)
-                       break;
+  char **cpp;
+  char *lcp;
+  char *rcp;
+  int i, len;
+
+  if (left == NULL || right == NULL)
+    return NULL;
+  cpp = (char **) malloc(sizeof *cpp);
+  if (cpp == NULL)
+    return NULL;
+  cpp[0] = NULL;
+  for (lcp = left; *lcp != '\0'; ++lcp)
+    {
+      len = 0;
+      rcp = index(right, *lcp);
+      while (rcp != NULL)
+       {
+         for (i = 1; lcp[i] != '\0' && lcp[i] == rcp[i]; ++i)
+           ;
+         if (i > len)
+           len = i;
+         rcp = index(rcp + 1, *lcp);
        }
        }
-       return cpp;
+      if (len == 0)
+       continue;
+      if ((cpp = enlist(cpp, lcp, len)) == NULL)
+       break;
+    }
+  return cpp;
 }
 
 static char **
 addlists(old, new)
 }
 
 static char **
 addlists(old, new)
-char **        old;
-char **        new;
+char **old;
+char **new;
 {
 {
-       register int    i;
-
-       if (old == NULL || new == NULL)
-               return NULL;
-       for (i = 0; new[i] != NULL; ++i) {
-               old = enlist(old, new[i], strlen(new[i]));
-               if (old == NULL)
-                       break;
-       }
-       return old;
-}
+  int i;
 
 
-/*
-** Given two lists of substrings,
-** return a new list giving substrings common to both.
-*/
+  if (old == NULL || new == NULL)
+    return NULL;
+  for (i = 0; new[i] != NULL; ++i)
+    {
+      old = enlist(old, new[i], strlen(new[i]));
+      if (old == NULL)
+       break;
+    }
+  return old;
+}
 
 
+/* Given two lists of substrings, return a new list giving substrings
+   common to both. */
 static char **
 inboth(left, right)
 static char **
 inboth(left, right)
-char **        left;
-char **        right;
+     char **left;
+     char **right;
 {
 {
-       register char **        both;
-       register char **        temp;
-       register int            lnum, rnum;
-
-       if (left == NULL || right == NULL)
-               return NULL;
-       both = (char **) malloc(sizeof *both);
-       if (both == NULL)
-               return NULL;
-       both[0] = NULL;
-       for (lnum = 0; left[lnum] != NULL; ++lnum) {
-               for (rnum = 0; right[rnum] != NULL; ++rnum) {
-                       temp = comsubs(left[lnum], right[rnum]);
-                       if (temp == NULL) {
-                               freelist(both);
-                               return NULL;
-                       }
-                       both = addlists(both, temp);
-                       freelist(temp);
-                       if (both == NULL)
-                               return NULL;
-               }
+  char **both;
+  char **temp;
+  int lnum, rnum;
+
+  if (left == NULL || right == NULL)
+    return NULL;
+  both = (char **) malloc(sizeof *both);
+  if (both == NULL)
+    return NULL;
+  both[0] = NULL;
+  for (lnum = 0; left[lnum] != NULL; ++lnum)
+    {
+      for (rnum = 0; right[rnum] != NULL; ++rnum)
+       {
+         temp = comsubs(left[lnum], right[rnum]);
+         if (temp == NULL)
+           {
+             freelist(both);
+             return NULL;
+           }
+         both = addlists(both, temp);
+         freelist(temp);
+         if (both == NULL)
+           return NULL;
        }
        }
-       return both;
+    }
+  return both;
 }
 
 }
 
-typedef struct {
-       char ** in;
-       char *  left;
-       char *  right;
-       char *  is;
+typedef struct
+{
+  char **in;
+  char *left;
+  char *right;
+  char *is;
 } must;
 
 static void
 resetmust(mp)
 } must;
 
 static void
 resetmust(mp)
-register must *        mp;
+must *mp;
 {
 {
-       mp->left[0] = mp->right[0] = mp->is[0] = '\0';
-       freelist(mp->in);
+  mp->left[0] = mp->right[0] = mp->is[0] = '\0';
+  freelist(mp->in);
 }
 
 static void
 }
 
 static void
-regmust(reg)
-register struct regexp *       reg;
+dfamust(dfa)
+struct dfa *dfa;
 {
 {
-       register must *         musts;
-       register must *         mp;
-       register char *         result;
-       register int            ri;
-       register int            i;
-       register _token         t;
-       static must             must0;
-
-       reg->mustn = 0;
-       reg->must[0] = '\0';
-       musts = (must *) malloc((reg->tindex + 1) * sizeof *musts);
-       if (musts == NULL)
-               return;
-       mp = musts;
-       for (i = 0; i <= reg->tindex; ++i)
-               mp[i] = must0;
-       for (i = 0; i <= reg->tindex; ++i) {
-               mp[i].in = (char **) malloc(sizeof *mp[i].in);
-               mp[i].left = malloc(2);
-               mp[i].right = malloc(2);
-               mp[i].is = malloc(2);
-               if (mp[i].in == NULL || mp[i].left == NULL ||
-                       mp[i].right == NULL || mp[i].is == NULL)
-                               goto done;
-               mp[i].left[0] = mp[i].right[0] = mp[i].is[0] = '\0';
-               mp[i].in[0] = NULL;
-       }
-       result = "";
+  must *musts;
+  must *mp;
+  char *result;
+  int ri;
+  int i;
+  int exact;
+  token t;
+  static must must0;
+  struct dfamust *dm;
+
+  result = "";
+  exact = 0;
+  musts = (must *) malloc((dfa->tindex + 1) * sizeof *musts);
+  if (musts == NULL)
+    return;
+  mp = musts;
+  for (i = 0; i <= dfa->tindex; ++i)
+    mp[i] = must0;
+  for (i = 0; i <= dfa->tindex; ++i)
+    {
+      mp[i].in = (char **) malloc(sizeof *mp[i].in);
+      mp[i].left = malloc(2);
+      mp[i].right = malloc(2);
+      mp[i].is = malloc(2);
+      if (mp[i].in == NULL || mp[i].left == NULL ||
+         mp[i].right == NULL || mp[i].is == NULL)
+       goto done;
+      mp[i].left[0] = mp[i].right[0] = mp[i].is[0] = '\0';
+      mp[i].in[0] = NULL;
+    }
 #ifdef DEBUG
 #ifdef DEBUG
-       fprintf(stderr, "regmust:\n");
-       for (i = 0; i < reg->tindex; ++i) {
-               fprintf(stderr, " %d:", i);
-               prtok(reg->tokens[i]);
-       }
-       putc('\n', stderr);
+  fprintf(stderr, "dfamust:\n");
+  for (i = 0; i < dfa->tindex; ++i)
+    {
+      fprintf(stderr, " %d:", i);
+      prtok(dfa->tokens[i]);
+    }
+  putc('\n', stderr);
 #endif
 #endif
-       for (ri = 0; ri < reg->tindex; ++ri) {
-               switch (t = reg->tokens[ri]) {
-               case _ALLBEGLINE:
-               case _ALLENDLINE:
-               case _LPAREN:
-               case _RPAREN:
-                       goto done;              /* "cannot happen" */
-               case _EMPTY:
-               case _BEGLINE:
-               case _ENDLINE:
-               case _BEGWORD:
-               case _ENDWORD:
-               case _LIMWORD:
-               case _NOTLIMWORD:
-               case _BACKREF:
-                       resetmust(mp);
-                       break;
-               case _STAR:
-               case _QMARK:
-                       if (mp <= musts)
-                               goto done;      /* "cannot happen" */
-                       --mp;
-                       resetmust(mp);
-                       break;
-               case _OR:
-                       if (mp < &musts[2])
-                               goto done;      /* "cannot happen" */
-                       {
-                               register char **        new;
-                               register must *         lmp;
-                               register must *         rmp;
-                               register int            j, ln, rn, n;
-
-                               rmp = --mp;
-                               lmp = --mp;
-                               /* Guaranteed to be.  Unlikely, but. . . */
-                               if (strcmp(lmp->is, rmp->is) != 0)
-                                       lmp->is[0] = '\0';
-                               /* Left side--easy */
-                               i = 0;
-                               while (lmp->left[i] != '\0' &&
-                                       lmp->left[i] == rmp->left[i])
-                                               ++i;
-                               lmp->left[i] = '\0';
-                               /* Right side */
-                               ln = strlen(lmp->right);
-                               rn = strlen(rmp->right);
-                               n = ln;
-                               if (n > rn)
-                                       n = rn;
-                               for (i = 0; i < n; ++i)
-                                       if (lmp->right[ln - i - 1] !=
-                                           rmp->right[rn - i - 1])
-                                               break;
-                               for (j = 0; j < i; ++j)
-                                       lmp->right[j] =
-                                               lmp->right[(ln - i) + j];
-                               lmp->right[j] = '\0';
-                               new = inboth(lmp->in, rmp->in);
-                               if (new == NULL)
-                                       goto done;
-                               freelist(lmp->in);
-                               free((char *) lmp->in);
-                               lmp->in = new;
-                       }
-                       break;
-               case _PLUS:
-                       if (mp <= musts)
-                               goto done;      /* "cannot happen" */
-                       --mp;
-                       mp->is[0] = '\0';
-                       break;
-               case _END:
-                       if (mp != &musts[1])
-                               goto done;      /* "cannot happen" */
-                       for (i = 0; musts[0].in[i] != NULL; ++i)
-                               if (strlen(musts[0].in[i]) > strlen(result))
-                                       result = musts[0].in[i];
-                       goto done;
-               case _CAT:
-                       if (mp < &musts[2])
-                               goto done;      /* "cannot happen" */
-                       {
-                               register must * lmp;
-                               register must * rmp;
-
-                               rmp = --mp;
-                               lmp = --mp;
-                               /*
-                               ** In.  Everything in left, plus everything in
-                               ** right, plus catenation of
-                               ** left's right and right's left.
-                               */
-                               lmp->in = addlists(lmp->in, rmp->in);
-                               if (lmp->in == NULL)
-                                       goto done;
-                               if (lmp->right[0] != '\0' &&
-                                       rmp->left[0] != '\0') {
-                                               register char * tp;
-
-                                               tp = icpyalloc(lmp->right);
-                                               if (tp == NULL)
-                                                       goto done;
-                                               tp = icatalloc(tp, rmp->left);
-                                               if (tp == NULL)
-                                                       goto done;
-                                               lmp->in = enlist(lmp->in, tp,
-                                                       strlen(tp));
-                                               free(tp);
-                                               if (lmp->in == NULL)
-                                                       goto done;
-                               }
-                               /* Left-hand */
-                               if (lmp->is[0] != '\0') {
-                                       lmp->left = icatalloc(lmp->left,
-                                               rmp->left);
-                                       if (lmp->left == NULL)
-                                               goto done;
-                               }
-                               /* Right-hand */
-                               if (rmp->is[0] == '\0')
-                                       lmp->right[0] = '\0';
-                               lmp->right = icatalloc(lmp->right, rmp->right);
-                               if (lmp->right == NULL)
-                                       goto done;
-                               /* Guaranteed to be */
-                               if (lmp->is[0] != '\0' && rmp->is[0] != '\0') {
-                                       lmp->is = icatalloc(lmp->is, rmp->is);
-                                       if (lmp->is == NULL)
-                                               goto done;
-                               } else
-                                       lmp->is[0] = '\0';
-                       }
-                       break;
-               default:
-                       if (t < _END) {
-                               /* "cannot happen" */
-                               goto done;
-                       } else if (t == '\0') {
-                               /* not on *my* shift */
-                               goto done;
-                       } else if (t >= _SET) {
-                               /* easy enough */
-                               resetmust(mp);
-                       } else {
-                               /* plain character */
-                               resetmust(mp);
-                               mp->is[0] = mp->left[0] = mp->right[0] = t;
-                               mp->is[1] = mp->left[1] = mp->right[1] = '\0';
-                               mp->in = enlist(mp->in, mp->is, 1);
-                               if (mp->in == NULL)
-                                       goto done;
-                       }
-                       break;
-               }
+  for (ri = 0; ri < dfa->tindex; ++ri)
+    {
+      switch (t = dfa->tokens[ri])
+       {
+       case LPAREN:
+       case RPAREN:
+         goto done;            /* "cannot happen" */
+       case EMPTY:
+       case BEGLINE:
+       case ENDLINE:
+       case BEGWORD:
+       case ENDWORD:
+       case LIMWORD:
+       case NOTLIMWORD:
+       case BACKREF:
+         resetmust(mp);
+         break;
+       case STAR:
+       case QMARK:
+         if (mp <= musts)
+           goto done;          /* "cannot happen" */
+         --mp;
+         resetmust(mp);
+         break;
+       case OR:
+       case ORTOP:
+         if (mp < &musts[2])
+           goto done;          /* "cannot happen" */
+         {
+           char **new;
+           must *lmp;
+           must *rmp;
+           int j, ln, rn, n;
+
+           rmp = --mp;
+           lmp = --mp;
+           /* Guaranteed to be.  Unlikely, but. . . */
+           if (strcmp(lmp->is, rmp->is) != 0)
+             lmp->is[0] = '\0';
+           /* Left side--easy */
+           i = 0;
+           while (lmp->left[i] != '\0' && lmp->left[i] == rmp->left[i])
+             ++i;
+           lmp->left[i] = '\0';
+           /* Right side */
+           ln = strlen(lmp->right);
+           rn = strlen(rmp->right);
+           n = ln;
+           if (n > rn)
+             n = rn;
+           for (i = 0; i < n; ++i)
+             if (lmp->right[ln - i - 1] != rmp->right[rn - i - 1])
+               break;
+           for (j = 0; j < i; ++j)
+             lmp->right[j] = lmp->right[(ln - i) + j];
+           lmp->right[j] = '\0';
+           new = inboth(lmp->in, rmp->in);
+           if (new == NULL)
+             goto done;
+           freelist(lmp->in);
+           free((char *) lmp->in);
+           lmp->in = new;
+         }
+         break;
+       case PLUS:
+         if (mp <= musts)
+           goto done;          /* "cannot happen" */
+         --mp;
+         mp->is[0] = '\0';
+         break;
+       case END:
+         if (mp != &musts[1])
+           goto done;          /* "cannot happen" */
+         for (i = 0; musts[0].in[i] != NULL; ++i)
+           if (strlen(musts[0].in[i]) > strlen(result))
+             result = musts[0].in[i];
+         if (strcmp(result, musts[0].is) == 0)
+           exact = 1;
+         goto done;
+       case CAT:
+         if (mp < &musts[2])
+           goto done;          /* "cannot happen" */
+         {
+           must *lmp;
+           must *rmp;
+
+           rmp = --mp;
+           lmp = --mp;
+           /* In.  Everything in left, plus everything in
+              right, plus catenation of
+              left's right and right's left. */
+           lmp->in = addlists(lmp->in, rmp->in);
+           if (lmp->in == NULL)
+             goto done;
+           if (lmp->right[0] != '\0' &&
+               rmp->left[0] != '\0')
+             {
+               char *tp;
+
+               tp = icpyalloc(lmp->right);
+               if (tp == NULL)
+                 goto done;
+               tp = icatalloc(tp, rmp->left);
+               if (tp == NULL)
+                 goto done;
+               lmp->in = enlist(lmp->in, tp,
+                                strlen(tp));
+               free(tp);
+               if (lmp->in == NULL)
+                 goto done;
+             }
+           /* Left-hand */
+           if (lmp->is[0] != '\0')
+             {
+               lmp->left = icatalloc(lmp->left,
+                                     rmp->left);
+               if (lmp->left == NULL)
+                 goto done;
+             }
+           /* Right-hand */
+           if (rmp->is[0] == '\0')
+             lmp->right[0] = '\0';
+           lmp->right = icatalloc(lmp->right, rmp->right);
+           if (lmp->right == NULL)
+             goto done;
+           /* Guaranteed to be */
+           if (lmp->is[0] != '\0' && rmp->is[0] != '\0')
+             {
+               lmp->is = icatalloc(lmp->is, rmp->is);
+               if (lmp->is == NULL)
+                 goto done;
+             }
+           else
+             lmp->is[0] = '\0';
+         }
+         break;
+       default:
+         if (t < END)
+           {
+             /* "cannot happen" */
+             goto done;
+           }
+         else if (t == '\0')
+           {
+             /* not on *my* shift */
+             goto done;
+           }
+         else if (t >= CSET)
+           {
+             /* easy enough */
+             resetmust(mp);
+           }
+         else
+           {
+             /* plain character */
+             resetmust(mp);
+             mp->is[0] = mp->left[0] = mp->right[0] = t;
+             mp->is[1] = mp->left[1] = mp->right[1] = '\0';
+             mp->in = enlist(mp->in, mp->is, 1);
+             if (mp->in == NULL)
+               goto done;
+           }
+         break;
+       }
 #ifdef DEBUG
 #ifdef DEBUG
-               fprintf(stderr, " node: %d:", ri);
-               prtok(reg->tokens[ri]);
-               fprintf(stderr, "\n  in:");
-               for (i = 0; mp->in[i]; ++i)
-                       fprintf(stderr, " \"%s\"", mp->in[i]);
-               fprintf(stderr, "\n  is: \"%s\"\n", mp->is);
-               fprintf(stderr, "  left: \"%s\"\n", mp->left);
-               fprintf(stderr, "  right: \"%s\"\n", mp->right);
+      fprintf(stderr, " node: %d:", ri);
+      prtok(dfa->tokens[ri]);
+      fprintf(stderr, "\n  in:");
+      for (i = 0; mp->in[i]; ++i)
+       fprintf(stderr, " \"%s\"", mp->in[i]);
+      fprintf(stderr, "\n  is: \"%s\"\n", mp->is);
+      fprintf(stderr, "  left: \"%s\"\n", mp->left);
+      fprintf(stderr, "  right: \"%s\"\n", mp->right);
 #endif
 #endif
-               ++mp;
-       }
-done:
-       (void) strncpy(reg->must, result, MUST_MAX - 1);
-       reg->must[MUST_MAX - 1] = '\0';
-       reg->mustn = strlen(reg->must);
-       mp = musts;
-       for (i = 0; i <= reg->tindex; ++i) {
-               freelist(mp[i].in);
-               ifree((char *) mp[i].in);
-               ifree(mp[i].left);
-               ifree(mp[i].right);
-               ifree(mp[i].is);
-       }
-       free((char *) mp);
+      ++mp;
+    }
+ done:
+  if (strlen(result))
+    {
+      dm = (struct dfamust *) malloc(sizeof (struct dfamust));
+      dm->exact = exact;
+      dm->must = malloc(strlen(result) + 1);
+      strcpy(dm->must, result);
+      dm->next = dfa->musts;
+      dfa->musts = dm;
+    }
+  mp = musts;
+  for (i = 0; i <= dfa->tindex; ++i)
+    {
+      freelist(mp[i].in);
+      ifree((char *) mp[i].in);
+      ifree(mp[i].left);
+      ifree(mp[i].right);
+      ifree(mp[i].is);
+    }
+  free((char *) mp);
 }
 }