Added '?' operator
[oweals/gnunet.git] / src / regex / regex.c
index 596acb323c727f0774be80495eff1d1f3aa30405..a240fcd8023a466919a9bcc90584d4c2b5f25501 100644 (file)
  */
 struct GNUNET_REGEX_Context
 {
+  /**
+   * Unique state id.
+   */
   unsigned int state_id;
+
+  /**
+   * Unique transition id.
+   */
   unsigned int transition_id;
 
   /**
-   * DLL of GNUNET_REGEX_Automaton's used as a stack
+   * DLL of GNUNET_REGEX_Automaton's used as a stack.
    */
   struct GNUNET_REGEX_Automaton *stack_head;
+
+  /**
+   * DLL of GNUNET_REGEX_Automaton's used as a stack.
+   */
   struct GNUNET_REGEX_Automaton *stack_tail;
 };
 
+/**
+ * Type of an automaton.
+ */
 enum GNUNET_REGEX_automaton_type
 {
   NFA,
@@ -50,19 +64,51 @@ enum GNUNET_REGEX_automaton_type
 };
 
 /**
- * Automaton representation
+ * Automaton representation.
  */
 struct GNUNET_REGEX_Automaton
 {
+  /**
+   * This is a linked list.
+   */
   struct GNUNET_REGEX_Automaton *prev;
+
+  /**
+   * This is a linked list.
+   */
   struct GNUNET_REGEX_Automaton *next;
 
+  /**
+   * First state of the automaton. This is mainly
+   * used for constructing an NFA, where each NFA
+   * itself consists of one or more NFAs linked
+   * together.
+   */
   struct State *start;
+
+  /**
+   * End state of the automaton.
+   */
   struct State *end;
 
+  /**
+   * Number of states in the automaton.
+   */
+  unsigned int state_count;
+
+  /**
+   * DLL of states.
+   */
   struct State *states_head;
+
+  /**
+   * DLL of states
+   */
   struct State *states_tail;
 
+  /**
+   * Type of the automaton.
+   */
   enum GNUNET_REGEX_automaton_type type;
 };
 
@@ -71,17 +117,58 @@ struct GNUNET_REGEX_Automaton
  */
 struct State
 {
+  /**
+   * This is a linked list.
+   */
   struct State *prev;
+
+  /**
+   * This is a linked list.
+   */
   struct State *next;
 
+  /**
+   * Unique state id.
+   */
   unsigned int id;
+
+  /**
+   * If this is an accepting state or not.
+   */
   int accepting;
+
+  /**
+   * Marking of the state. This is used for marking all visited
+   * states when traversing all states of an automaton and for
+   * cases where the state id cannot be used (dfa minimization).
+   */
   int marked;
+
+  /**
+   * Human readable name of the automaton. Used for debugging
+   * and graph creation.
+   */
   char *name;
 
+  /**
+   * Number of transitions from this state to other states.
+   */
+  unsigned int transition_count;
+
+  /**
+   * DLL of transitions.
+   */
   struct Transition *transitions_head;
+
+  /**
+   * DLL of transitions.
+   */
   struct Transition *transitions_tail;
 
+  /**
+   * Set of states on which this state is based on. Used when
+   * creating a DFA out of several NFA states.
+   */
   struct StateSet *nfa_set;
 };
 
@@ -91,23 +178,46 @@ struct State
  */
 struct Transition
 {
+  /**
+   * This is a linked list.
+   */
   struct Transition *prev;
+
+  /**
+   * This is a linked list.
+   */
   struct Transition *next;
 
+  /**
+   * Unique id of this transition.
+   */
   unsigned int id;
+
+  /**
+   * Literal for this transition. This is basically the edge label for
+   * the graph.
+   */
   char literal;
+
+  /**
+   * State to which this transition leads.
+   */
   struct State *state;
 };
 
 /**
- * Set of states
+ * Set of states.
  */
 struct StateSet
 {
   /**
-   * Array of states
+   * Array of states.
    */
   struct State **states;
+
+  /**
+   * Length of the 'states' array.
+   */
   unsigned int len;
 };
 
@@ -116,7 +226,7 @@ struct StateSet
  *
  * @param ctx context
  */
-void
+static void
 GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
 {
   if (NULL == ctx)
@@ -130,7 +240,7 @@ GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
   ctx->stack_tail = NULL;
 }
 
-void
+static void
 debug_print_state (struct State *s)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -138,7 +248,7 @@ debug_print_state (struct State *s)
               s->marked, s->accepting);
 }
 
-void
+static void
 debug_print_states (struct StateSet *sset)
 {
   struct State *s;
@@ -151,7 +261,7 @@ debug_print_states (struct StateSet *sset)
   }
 }
 
-void
+static void
 debug_print_transitions (struct State *s)
 {
   struct Transition *t;
@@ -175,51 +285,58 @@ debug_print_transitions (struct State *s)
   }
 }
 
+/**
+ * Compare two states. Used for sorting.
+ *
+ * @param a first state
+ * @param b second state
+ *
+ * @return an integer less than, equal to, or greater than zero
+ *         if the first argument is considered to be respectively
+ *         less than, equal to, or greater than the second.
+ */
+static int
+state_compare (const void *a, const void *b)
+{
+  struct State **s1;
+  struct State **s2;
+
+  s1 = (struct State **) a;
+  s2 = (struct State **) b;
+
+  return (*s1)->id - (*s2)->id;
+}
+
 /**
  * Compare to state sets by comparing the id's of the states that are
- * contained in each set.
+ * contained in each set. Both sets are expected to be sorted by id!
  *
  * @param sset1 first state set
  * @param sset2 second state set
  *
- * @return 0 if they are equal, non 0 otherwise
+ * @return an integer less than, equal to, or greater than zero
+ *         if the first argument is considered to be respectively
+ *         less than, equal to, or greater than the second.
  */
-int
+static int
 state_set_compare (struct StateSet *sset1, struct StateSet *sset2)
 {
-  struct State *s1;
-  struct State *s2;
-  int i1;
-  int i2;
-  int contains;
-  int rslt;
+  int result;
+  int i;
 
-  if (sset1->len < 1 || sset2->len < 1)
-    return -1;
+  if (NULL == sset1 || NULL == sset2)
+    return 1;
 
-  rslt = 0;
+  result = sset1->len - sset2->len;
 
-  for (i1 = 0; i1 < sset1->len; i1++)
+  for (i = 0; i < sset1->len; i++)
   {
-    s1 = sset1->states[i1];
-    contains = 0;
-    for (i2 = 0; i2 < sset2->len; i2++)
-    {
-      s2 = sset2->states[i2];
-      if (s1->id == s2->id)
-      {
-        contains = 1;
-        break;
-      }
-    }
-
-    if (0 == contains)
-    {
-      rslt = 1;
+    if (0 != result)
       break;
-    }
+
+    result = state_compare (&sset1->states[i], &sset2->states[i]);
   }
-  return rslt;
+  return result;
 }
 
 /**
@@ -230,7 +347,7 @@ state_set_compare (struct StateSet *sset1, struct StateSet *sset2)
  *
  * @return GNUNET_YES if 'set' contains 'elem, GNUNET_NO otherwise
  */
-int
+static int
 state_set_contains (struct StateSet *set, struct State *elem)
 {
   struct State *s;
@@ -245,6 +362,22 @@ state_set_contains (struct StateSet *set, struct State *elem)
   return GNUNET_NO;
 }
 
+/**
+ * Clears the given StateSet 'set'
+ *
+ * @param set set to be cleared
+ */
+static void
+state_set_clear (struct StateSet *set)
+{
+  if (NULL != set)
+  {
+    if (NULL != set->states)
+      GNUNET_free (set->states);
+    GNUNET_free (set);
+  }
+}
+
 /**
  * Adds a transition from one state to another on 'literal'
  *
@@ -253,7 +386,7 @@ state_set_contains (struct StateSet *set, struct State *elem)
  * @param literal transition label
  * @param to_state state to where the transition should point to
  */
-void
+static void
 add_transition (struct GNUNET_REGEX_Context *ctx, struct State *from_state,
                 const char literal, struct State *to_state)
 {
@@ -281,13 +414,17 @@ add_transition (struct GNUNET_REGEX_Context *ctx, struct State *from_state,
  *
  * @param a automaton to be cleared
  */
-void
+static void
 automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
 {
+  if (NULL == a)
+    return;
+
   a->start = NULL;
   a->end = NULL;
   a->states_head = NULL;
   a->states_tail = NULL;
+  a->state_count = 0;
   GNUNET_free (a);
 }
 
@@ -296,12 +433,15 @@ automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
  *
  * @param s state that should be destroyed
  */
-void
+static void
 automaton_destroy_state (struct State *s)
 {
   struct Transition *t;
   struct Transition *next_t;
 
+  if (NULL == s)
+    return;
+
   if (NULL != s->name)
     GNUNET_free (s->name);
 
@@ -313,14 +453,123 @@ automaton_destroy_state (struct State *s)
     t = next_t;
   }
 
-  if (NULL != s->nfa_set)
+  state_set_clear (s->nfa_set);
+
+  GNUNET_free (s);
+}
+
+/**
+ * Remove a state from the given automaton 'a'. Always use this function
+ * when altering the states of an automaton. Will also remove all transitions
+ * leading to this state, before destroying it.
+ *
+ * @param a automaton
+ * @param s state to remove
+ */
+static void
+automaton_remove_state (struct GNUNET_REGEX_Automaton *a, struct State *s)
+{
+  struct State *ss;
+  struct State *s_check;
+  struct Transition *t_check;
+
+  if (NULL == a || NULL == s)
+    return;
+
+  // remove state
+  ss = s;
+  GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
+  a->state_count--;
+
+  // remove all transitions leading to this state
+  for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
   {
-    if (s->nfa_set->len > 0)
-      GNUNET_free (s->nfa_set->states);
-    GNUNET_free (s->nfa_set);
+    for (t_check = s_check->transitions_head; NULL != t_check;
+         t_check = t_check->next)
+    {
+      if (t_check->state == ss)
+      {
+        GNUNET_CONTAINER_DLL_remove (s_check->transitions_head,
+                                     s_check->transitions_tail, t_check);
+        s_check->transition_count--;
+      }
+    }
   }
 
-  GNUNET_free (s);
+  automaton_destroy_state (ss);
+}
+
+/**
+ * Merge two states into one. Will merge 's1' and 's2' into 's1' and destroy 's2'.
+ *
+ * @param ctx context
+ * @param a automaton
+ * @param s1 first state
+ * @param s2 second state, will be destroyed
+ */
+static void
+automaton_merge_states (struct GNUNET_REGEX_Context *ctx,
+                        struct GNUNET_REGEX_Automaton *a, struct State *s1,
+                        struct State *s2)
+{
+  struct State *s_check;
+  struct Transition *t_check;
+  struct Transition *t;
+  char *new_name;
+
+  GNUNET_assert (NULL != ctx && NULL != a && NULL != s1 && NULL != s2);
+
+  // 1. Make all transitions pointing to s2 point to s1
+  for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
+  {
+    for (t_check = s_check->transitions_head; NULL != t_check;
+         t_check = t_check->next)
+    {
+      if (s_check != s1 && s2 == t_check->state)
+        t_check->state = s1;
+    }
+  }
+
+  // 2. Add all transitions from s2 to sX to s1
+  for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next)
+  {
+    for (t = s1->transitions_head; NULL != t; t = t->next)
+    {
+      if (t_check->literal != t->literal && NULL != t_check->state &&
+          t_check->state != t->state && t_check->state != s2)
+      {
+        add_transition (ctx, s1, t_check->literal, t_check->state);
+      }
+    }
+  }
+
+  // 3. Rename s1 to {s1,s2}
+  new_name = GNUNET_malloc (strlen (s1->name) + strlen (s2->name) + 1);
+  strncat (new_name, s1->name, strlen (s1->name));
+  strncat (new_name, s2->name, strlen (s2->name));
+  if (NULL != s1->name)
+    GNUNET_free (s1->name);
+  s1->name = new_name;
+
+  // remove state
+  s_check = s2;
+  GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s_check);
+  a->state_count--;
+  automaton_destroy_state (s_check);
+}
+
+/**
+ * Add a state to the automaton 'a', always use this function to
+ * alter the states DLL of the automaton.
+ *
+ * @param a automaton to add the state to
+ * @param s state that should be added
+ */
+static void
+automaton_add_state (struct GNUNET_REGEX_Automaton *a, struct State *s)
+{
+  GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s);
+  a->state_count++;
 }
 
 /**
@@ -332,7 +581,7 @@ automaton_destroy_state (struct State *s)
  *
  * @return new DFA state
  */
-struct State *
+static struct State *
 dfa_state_create (struct GNUNET_REGEX_Context *ctx, struct StateSet *nfa_states)
 {
   struct State *s;
@@ -351,7 +600,10 @@ dfa_state_create (struct GNUNET_REGEX_Context *ctx, struct StateSet *nfa_states)
   s->name = NULL;
 
   if (NULL == nfa_states)
+  {
+    GNUNET_asprintf (&s->name, "s%i", s->id);
     return s;
+  }
 
   s->nfa_set = nfa_states;
 
@@ -408,7 +660,16 @@ dfa_state_create (struct GNUNET_REGEX_Context *ctx, struct StateSet *nfa_states)
   return s;
 }
 
-struct State *
+/**
+ * Move from the given state 's' to the next state on
+ * transition 'literal'
+ *
+ * @param s starting state
+ * @param literal edge label to follow
+ *
+ * @return new state or NULL, if transition on literal not possible
+ */
+static struct State *
 dfa_move (struct State *s, const char literal)
 {
   struct Transition *t;
@@ -431,6 +692,199 @@ dfa_move (struct State *s, const char literal)
   return new_s;
 }
 
+/**
+ * Remove all unreachable states from DFA 'a'. Unreachable states
+ * are those states that are not reachable from the starting state.
+ *
+ * @param a DFA automaton
+ */
+static void
+dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
+{
+  struct State *stack[a->state_count];
+  int stack_len;
+  struct State *s;
+  struct Transition *t;
+
+  stack_len = 0;
+
+  // 1. unmark all states
+  for (s = a->states_head; NULL != s; s = s->next)
+  {
+    s->marked = 0;
+  }
+
+  // 2. traverse dfa from start state and mark all visited states
+  stack[stack_len] = a->start;
+  stack_len++;
+  while (stack_len > 0)
+  {
+    s = stack[stack_len - 1];
+    stack_len--;
+    s->marked = 1;              // mark s as visited
+    for (t = s->transitions_head; NULL != t; t = t->next)
+    {
+      // add next states to stack
+      if (NULL != t->state && 0 == t->state->marked)
+        stack[++stack_len] = t->state;
+    }
+  }
+
+  // 3. delete all states that were not visited
+  for (s = a->states_head; NULL != s; s = s->next)
+  {
+    if (0 == s->marked)
+      automaton_remove_state (a, s);
+  }
+}
+
+/**
+ * Remove all dead states from the DFA 'a'. Dead states are those
+ * states that do not transition to any other state but themselfes.
+ *
+ * @param a DFA automaton
+ */
+static void
+dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
+{
+  struct State *s;
+  struct Transition *t;
+  int dead;
+
+  GNUNET_assert (DFA == a->type);
+
+  for (s = a->states_head; NULL != s; s = s->next)
+  {
+    if (s->accepting)
+      continue;
+
+    dead = 1;
+    for (t = s->transitions_head; NULL != t; t = t->next)
+    {
+      if (NULL != t->state && t->state != s)
+      {
+        dead = 0;
+        break;
+      }
+    }
+
+    if (0 == dead)
+      continue;
+
+    // state s is dead, remove it
+    automaton_remove_state (a, s);
+  }
+}
+
+/**
+ * Merge all non distinguishable states in the DFA 'a'
+ *
+ * @param ctx context
+ * @param a DFA automaton
+ */
+static void
+dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
+                                     struct GNUNET_REGEX_Automaton *a)
+{
+  int i;
+  int table[a->state_count][a->state_count];
+  struct State *s1;
+  struct State *s2;
+  struct Transition *t1;
+  struct Transition *t2;
+  int change;
+
+  change = 1;
+  for (i = 0, s1 = a->states_head; i < a->state_count && NULL != s1;
+       i++, s1 = s1->next)
+    s1->marked = i;
+
+  // Mark all pairs of accepting/!accepting states
+  for (s1 = a->states_head; NULL != s1; s1 = s1->next)
+  {
+    for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
+    {
+      if ((s1->accepting && !s2->accepting) ||
+          (!s1->accepting && s2->accepting))
+      {
+        table[s1->marked][s2->marked] = 1;
+      }
+      else
+        table[s1->marked][s2->marked] = 0;
+    }
+  }
+
+  while (0 != change)
+  {
+    change = 0;
+    for (s1 = a->states_head; NULL != s1; s1 = s1->next)
+    {
+      for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
+      {
+        if (0 != table[s1->marked][s2->marked])
+          continue;
+
+        for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
+        {
+          for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
+          {
+            if (t1->literal == t2->literal && t1->state == t2->state &&
+                (0 != table[t1->state->marked][t2->state->marked] ||
+                 0 != table[t2->state->marked][t1->state->marked]))
+            {
+              table[s1->marked][s2->marked] = t1->literal;
+              change = 1;
+            }
+            else if (t1->literal != t2->literal && t1->state != t2->state)
+            {
+              table[s1->marked][s2->marked] = -1;
+              change = 1;
+            }
+          }
+        }
+      }
+    }
+  }
+
+  struct State *s2_next;
+
+  for (i = 0, s1 = a->states_head; NULL != s1; s1 = s1->next)
+  {
+    for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
+    {
+      s2_next = s2->next;
+      if (s1 != s2 && table[s1->marked][s2->marked] == 0)
+        automaton_merge_states (ctx, a, s1, s2);
+    }
+  }
+}
+
+/**
+ * Minimize the given DFA 'a' by removing all unreachable states,
+ * removing all dead states and merging all non distinguishable states
+ *
+ * @param ctx context
+ * @param a DFA automaton
+ */
+static void
+dfa_minimize (struct GNUNET_REGEX_Context *ctx,
+              struct GNUNET_REGEX_Automaton *a)
+{
+  if (NULL == a)
+    return;
+
+  GNUNET_assert (DFA == a->type);
+
+  // 1. remove unreachable states
+  dfa_remove_unreachable_states (a);
+
+  // 2. remove dead states
+  dfa_remove_dead_states (a);
+
+  // 3. Merge nondistinguishable states
+  dfa_merge_nondistinguishable_states (ctx, a);
+}
+
 /**
  * Creates a new NFA fragment. Needs to be cleared using automaton_fragment_clear.
  *
@@ -439,7 +893,7 @@ dfa_move (struct State *s, const char literal)
  *
  * @return new NFA fragment
  */
-struct GNUNET_REGEX_Automaton *
+static struct GNUNET_REGEX_Automaton *
 nfa_fragment_create (struct State *start, struct State *end)
 {
   struct GNUNET_REGEX_Automaton *n;
@@ -453,67 +907,184 @@ nfa_fragment_create (struct State *start, struct State *end)
   if (NULL == start && NULL == end)
     return n;
 
-  GNUNET_CONTAINER_DLL_insert (n->states_head, n->states_tail, end);
-  GNUNET_CONTAINER_DLL_insert (n->states_head, n->states_tail, start);
+  automaton_add_state (n, end);
+  automaton_add_state (n, start);
+
+  n->start = start;
+  n->end = end;
+
+  return n;
+}
+
+/**
+ * Adds a list of states to the given automaton 'n'.
+ *
+ * @param n automaton to which the states should be added
+ * @param states_head head of the DLL of states
+ * @param states_tail tail of the DLL of states
+ */
+static void
+nfa_add_states (struct GNUNET_REGEX_Automaton *n, struct State *states_head,
+                struct State *states_tail)
+{
+  struct State *s;
+
+  if (NULL == n || NULL == states_head)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
+    return;
+  }
+
+  if (NULL == n->states_head)
+  {
+    n->states_head = states_head;
+    n->states_tail = states_tail;
+    return;
+  }
+
+  if (NULL != states_head)
+  {
+    n->states_tail->next = states_head;
+    n->states_tail = states_tail;
+  }
+
+  for (s = states_head; NULL != s; s = s->next)
+    n->state_count++;
+}
+
+/**
+ * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
+ *
+ * @param ctx context
+ * @param accepting is it an accepting state or not
+ *
+ * @return new NFA state
+ */
+static struct State *
+nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
+{
+  struct State *s;
+
+  s = GNUNET_malloc (sizeof (struct State));
+  s->id = ctx->state_id++;
+  s->accepting = accepting;
+  s->marked = 0;
+  s->name = NULL;
+  GNUNET_asprintf (&s->name, "s%i", s->id);
+
+  return s;
+}
+
+/**
+ * Calculates the NFA closure set for the given state.
+ *
+ * @param s starting point state
+ * @param literal transitioning literal on which to base the closure on,
+ *                pass 0 for epsilon transition
+ *
+ * @return sorted nfa closure on 'literal' (epsilon closure if 'literal' is 0)
+ */
+static struct StateSet *
+nfa_closure_create (struct State *s, const char literal)
+{
+  struct StateSet *cls;
+  struct StateSet *cls_check;
+  struct State *clsstate;
+  struct State *currentstate;
+  struct Transition *ctran;
+
+  if (NULL == s)
+    return NULL;
+
+  cls = GNUNET_malloc (sizeof (struct StateSet));
+  cls_check = GNUNET_malloc (sizeof (struct StateSet));
+
+  // Add start state to closure only for epsilon closure
+  if (0 == literal)
+    GNUNET_array_append (cls->states, cls->len, s);
+
+  GNUNET_array_append (cls_check->states, cls_check->len, s);
+  while (cls_check->len > 0)
+  {
+    currentstate = cls_check->states[cls_check->len - 1];
+    GNUNET_array_grow (cls_check->states, cls_check->len, cls_check->len - 1);
+
+    for (ctran = currentstate->transitions_head; NULL != ctran;
+         ctran = ctran->next)
+    {
+      if (NULL != ctran->state && literal == ctran->literal)
+      {
+        clsstate = ctran->state;
+
+        if (NULL != clsstate &&
+            GNUNET_YES != state_set_contains (cls, clsstate))
+        {
+          GNUNET_array_append (cls->states, cls->len, clsstate);
+          GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
+        }
+      }
+    }
+  }
+  GNUNET_assert (0 == cls_check->len);
+  GNUNET_free (cls_check);
 
-  n->start = start;
-  n->end = end;
+  if (cls->len > 1)
+    qsort (cls->states, cls->len, sizeof (struct State *), state_compare);
 
-  return n;
+  return cls;
 }
 
 /**
- * Adds a list of states to the given automaton 'n'.
+ * Calculates the closure set for the given set of states.
  *
- * @param n automaton to which the states should be added
- * @param states_head head of the DLL of states
- * @param states_tail tail of the DLL of states
+ * @param states list of states on which to base the closure on
+ * @param literal transitioning literal for which to base the closure on,
+ *                pass 0 for epsilon transition
+ *
+ * @return sorted nfa closure on 'literal' (epsilon closure if 'literal' is 0)
  */
-void
-nfa_add_states (struct GNUNET_REGEX_Automaton *n, struct State *states_head,
-                struct State *states_tail)
+static struct StateSet *
+nfa_closure_set_create (struct StateSet *states, const char literal)
 {
-  if (NULL == n || NULL == states_head)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
-    return;
-  }
+  struct State *s;
+  struct StateSet *sset;
+  struct StateSet *cls;
+  int i;
+  int j;
+  int k;
+  int contains;
 
-  if (NULL == n->states_head)
-  {
-    n->states_head = states_head;
-    n->states_tail = states_tail;
-    return;
-  }
+  if (NULL == states)
+    return NULL;
 
-  if (NULL != states_head)
+  cls = GNUNET_malloc (sizeof (struct StateSet));
+
+  for (i = 0; i < states->len; i++)
   {
-    n->states_tail->next = states_head;
-    n->states_tail = states_tail;
-  }
-}
+    s = states->states[i];
+    sset = nfa_closure_create (s, literal);
 
-/**
- * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
- *
- * @param ctx context
- * @param accepting is it an accepting state or not
- *
- * @return new NFA state
- */
-struct State *
-nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
-{
-  struct State *s;
+    for (j = 0; j < sset->len; j++)
+    {
+      contains = 0;
+      for (k = 0; k < cls->len; k++)
+      {
+        if (sset->states[j]->id == cls->states[k]->id)
+        {
+          contains = 1;
+          break;
+        }
+      }
+      if (!contains)
+        GNUNET_array_append (cls->states, cls->len, sset->states[j]);
+    }
+    state_set_clear (sset);
+  }
 
-  s = GNUNET_malloc (sizeof (struct State));
-  s->id = ctx->state_id++;
-  s->accepting = accepting;
-  s->marked = 0;
-  s->name = NULL;
-  GNUNET_asprintf (&s->name, "s%i", s->id);
+  if (cls->len > 1)
+    qsort (cls->states, cls->len, sizeof (struct State *), state_compare);
 
-  return s;
+  return cls;
 }
 
 /**
@@ -521,7 +1092,7 @@ nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
  *
  * @param ctx context
  */
-void
+static void
 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
 {
   struct GNUNET_REGEX_Automaton *a;
@@ -553,7 +1124,7 @@ nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
  *
  * @param ctx context
  */
-void
+static void
 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
 {
   struct GNUNET_REGEX_Automaton *a;
@@ -594,7 +1165,7 @@ nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
  *
  * @param ctx context
  */
-void
+static void
 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
 {
   struct GNUNET_REGEX_Automaton *a;
@@ -607,13 +1178,52 @@ nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
 }
 
+/**
+ * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
+ *
+ * @param ctx context
+ */
+static void
+nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
+{
+  struct GNUNET_REGEX_Automaton *a;
+  struct GNUNET_REGEX_Automaton *new;
+  struct State *start;
+  struct State *end;
+
+  a = ctx->stack_tail;
+  GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
+
+  if (NULL == a)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "nfa_add_question_op failed, because there was no element on the stack");
+    return;
+  }
+
+  start = nfa_state_create (ctx, 0);
+  end = nfa_state_create (ctx, 1);
+
+  add_transition (ctx, start, 0, a->start);
+  add_transition (ctx, start, 0, end);
+  add_transition (ctx, a->end, 0, end);
+
+  a->end->accepting = 0;
+
+  new = nfa_fragment_create (start, end);
+  nfa_add_states (new, a->states_head, a->states_tail);
+  automaton_fragment_clear (a);
+
+  GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
+}
+
 /**
  * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment
  * that alternates between a and b (a|b)
  *
  * @param ctx context
  */
-void
+static void
 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
 {
   struct GNUNET_REGEX_Automaton *a;
@@ -654,7 +1264,7 @@ nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
  * @param ctx context
  * @param lit literal for nfa transition
  */
-void
+static void
 nfa_add_literal (struct GNUNET_REGEX_Context *ctx, const char lit)
 {
   struct GNUNET_REGEX_Automaton *n;
@@ -671,104 +1281,6 @@ nfa_add_literal (struct GNUNET_REGEX_Context *ctx, const char lit)
   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
 }
 
-/**
- * Calculates the NFA closure set for the given state
- *
- * @param s starting point state
- * @param literal transitioning literal on which to base the closure on,
- *                pass 0 for epsilon transition
- *
- * @return nfa closure on 'literal' (epsilon closure if 'literal' is 0)
- */
-struct StateSet *
-nfa_closure_create (struct State *s, const char literal)
-{
-  struct StateSet *cls;
-  struct StateSet *cls_check;
-  struct State *clsstate;
-  struct State *currentstate;
-  struct Transition *ctran;
-
-  if (NULL == s)
-    return NULL;
-
-  cls = GNUNET_malloc (sizeof (struct StateSet));
-  cls_check = GNUNET_malloc (sizeof (struct StateSet));
-
-  // Add start state to closure only for epsilon closure
-  if (0 == literal)
-    GNUNET_array_append (cls->states, cls->len, s);
-
-  GNUNET_array_append (cls_check->states, cls_check->len, s);
-  while (cls_check->len > 0)
-  {
-    currentstate = cls_check->states[cls_check->len - 1];
-    GNUNET_array_grow (cls_check->states, cls_check->len, cls_check->len - 1);
-
-    for (ctran = currentstate->transitions_head; NULL != ctran;
-         ctran = ctran->next)
-    {
-      if (NULL != ctran->state && literal == ctran->literal)
-      {
-        clsstate = ctran->state;
-
-        if (NULL != clsstate &&
-            GNUNET_YES != state_set_contains (cls, clsstate))
-        {
-          GNUNET_array_append (cls->states, cls->len, clsstate);
-          GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
-        }
-      }
-    }
-  }
-  GNUNET_assert (0 == cls_check->len);
-  GNUNET_free (cls_check);
-
-  return cls;
-}
-
-/**
- * Calculates the closure set for the given set of states.
- *
- * @param states list of states on which to base the closure on
- * @param literal transitioning literal for which to base the closure on,
- *                pass 0 for epsilon transition
- *
- * @return nfa closure on 'literal' (epsilon closure if 'literal' is 0)
- */
-struct StateSet *
-nfa_closure_set_create (struct StateSet *states, const char literal)
-{
-  struct State *s;
-  struct StateSet *sset;
-  struct StateSet *cls;
-  int i;
-  int j;
-
-  if (NULL == states)
-    return NULL;
-
-  cls = GNUNET_malloc (sizeof (struct StateSet));
-
-  for (i = 0; i < states->len; i++)
-  {
-    s = states->states[i];
-    sset = nfa_closure_create (s, literal);
-
-    for (j = 0; j < sset->len; j++)
-      GNUNET_array_append (cls->states, cls->len, sset->states[j]);
-
-    if (NULL != sset)
-    {
-      if (sset->len > 0)
-        GNUNET_free (sset->states);
-      GNUNET_free (sset);
-    }
-  }
-
-  return cls;
-}
-
 /**
  * Construct an NFA by parsing the regex string of length 'len'.
  *
@@ -868,6 +1380,14 @@ GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
       }
       nfa_add_plus_op (&ctx);
       break;
+    case '?':
+      if (atomcount == 0)
+      {
+        error_msg = "Cannot append '?' to nothing";
+        goto error;
+      }
+      nfa_add_question_op (&ctx);
+      break;
     case 92:                   /* escape: \ */
       regexp++;
       count++;
@@ -905,17 +1425,14 @@ GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
     goto error;
   }
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Created NFA with %i States and a total of %i Transitions\n",
-              ctx.state_id, ctx.transition_id);
-
   return nfa;
 
 error:
   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex\n");
   if (NULL != error_msg)
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
-  GNUNET_free (p);
+  if (NULL != p)
+    GNUNET_free (p);
   while (NULL != ctx.stack_tail)
   {
     GNUNET_REGEX_automaton_destroy (ctx.stack_tail);
@@ -978,6 +1495,13 @@ GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
   // Create NFA
   nfa = GNUNET_REGEX_construct_nfa (regex, len);
 
+  if (NULL == nfa)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Could not create DFA, because NFA creation failed\n");
+    return NULL;
+  }
+
   dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
   dfa->type = DFA;
 
@@ -985,8 +1509,10 @@ GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
   dfa_stack = GNUNET_malloc (sizeof (struct StateSet));
   nfa_set = nfa_closure_create (nfa->start, 0);
   dfa->start = dfa_state_create (&ctx, nfa_set);
-  GNUNET_CONTAINER_DLL_insert (dfa->states_head, dfa->states_tail, dfa->start);
+  automaton_add_state (dfa, dfa->start);
   GNUNET_array_append (dfa_stack->states, dfa_stack->len, dfa->start);
+
+  // Create dfa states by combining nfa states
   while (dfa_stack->len > 0)
   {
     dfa_state = dfa_stack->states[dfa_stack->len - 1];
@@ -999,12 +1525,7 @@ GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
       {
         tmp = nfa_closure_set_create (dfa_state->nfa_set, ctran->literal);
         nfa_set = nfa_closure_set_create (tmp, 0);
-        if (NULL != tmp)
-        {
-          if (tmp->len > 0)
-            GNUNET_free (tmp->states);
-          GNUNET_free (tmp);
-        }
+        state_set_clear (tmp);
         new_dfa_state = dfa_state_create (&ctx, nfa_set);
         state_contains = NULL;
         for (state_iter = dfa->states_head; NULL != state_iter;
@@ -1017,8 +1538,7 @@ GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
 
         if (NULL == state_contains)
         {
-          GNUNET_CONTAINER_DLL_insert_tail (dfa->states_head, dfa->states_tail,
-                                            new_dfa_state);
+          automaton_add_state (dfa, new_dfa_state);
           GNUNET_array_append (dfa_stack->states, dfa_stack->len,
                                new_dfa_state);
           ctran->state = new_dfa_state;
@@ -1035,8 +1555,7 @@ GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
   GNUNET_free (dfa_stack);
   GNUNET_REGEX_automaton_destroy (nfa);
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created DFA with %i States\n",
-              ctx.state_id);
+  dfa_minimize (&ctx, dfa);
 
   return dfa;
 }
@@ -1125,13 +1644,26 @@ GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a,
   fclose (p);
 }
 
-int
+/**
+ * Evaluates the given string using the given DFA automaton
+ *
+ * @param a automaton, type must be DFA
+ * @param string string that should be evaluated
+ *
+ * @return 0 if string matches, non 0 otherwise
+ */
+static int
 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
 {
   const char *strp;
   struct State *s;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Evaluating '%s' using DFA\n", string);
+  if (DFA != a->type)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Tried to evaluate DFA, but NFA automaton given");
+    return -1;
+  }
 
   s = a->start;
 
@@ -1143,19 +1675,61 @@ evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
   }
 
   if (NULL != s && s->accepting)
-    return GNUNET_YES;
+    return 0;
 
-  return GNUNET_NO;
+  return 1;
 }
 
-int
+/**
+ * Evaluates the given string using the given NFA automaton
+ *
+ * @param a automaton, type must be NFA
+ * @param string string that should be evaluated
+ *
+ * @return 0 if string matches, non 0 otherwise
+ */
+static int
 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
 {
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Evaluating '%s' using NFA\n", string);
+  const char *strp;
+  struct State *s;
+  struct StateSet *sset;
+  struct StateSet *new_sset;
+  int i;
+  int result;
 
-  return GNUNET_YES;
-}
+  if (NFA != a->type)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Tried to evaluate NFA, but DFA automaton given");
+    return -1;
+  }
+
+  result = 1;
+  strp = string;
+  sset = nfa_closure_create (a->start, 0);
+
+  for (strp = string; NULL != strp && *strp; strp++)
+  {
+    new_sset = nfa_closure_set_create (sset, *strp);
+    state_set_clear (sset);
+    sset = nfa_closure_set_create (new_sset, 0);
+    state_set_clear (new_sset);
+  }
+
+  for (i = 0; i < sset->len; i++)
+  {
+    s = sset->states[i];
+    if (NULL != s && s->accepting)
+    {
+      result = 0;
+      break;
+    }
+  }
 
+  state_set_clear (sset);
+  return result;
+}
 
 /**
  * Evaluates the given 'string' against the given compiled regex
@@ -1163,27 +1737,27 @@ evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
  * @param a automaton
  * @param string string to check
  *
- * @return GNUNET_YES if 'a' matches 'string', GNUNET_NO otherwise
+ * @return 0 if string matches, non 0 otherwise
  */
 int
 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
 {
-  int eval;
+  int result;
 
   switch (a->type)
   {
   case DFA:
-    eval = evaluate_dfa (a, string);
+    result = evaluate_dfa (a, string);
     break;
   case NFA:
-    eval = evaluate_nfa (a, string);
+    result = evaluate_nfa (a, string);
     break;
   default:
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Evaluating regex failed, automaton has no type!\n");
-    eval = GNUNET_SYSERR;
+    result = GNUNET_SYSERR;
     break;
   }
 
-  return eval;
+  return result;
 }