Пример #1
0
  private void convertToDFA(NFA nfa) {
    DFAState curr, temp;
    List<String> tokens = new ArrayList<String>();
    LinkedList<DFAState> queue = new LinkedList<DFAState>();
    findStart(nfa);
    queue.add(start);

    MAINWHILE:
    while (!queue.isEmpty()) {
      curr = queue.removeFirst();
      for (DFAState sta : checked) {
        if (curr.equals(sta)) {
          //            		System.out.println("NOPE");
          continue MAINWHILE;
        }
      }
      checked.add(curr);
      // Pull out all keys
      for (State s : curr.getStates()) {
        for (String k : s.getTransitionTable().keySet()) {
          tokens.add(k);
          // System.out.println(tokens);
        }
      }
      // Iterate over all possible iterations and link states together
      for (String j : tokens) {
        if (!j.equals("")) {
          temp = goTo(curr, j);
          queue.add(temp);
        }
      }
    }
  }
Пример #2
0
  // Calculates epsilon transition states
  private DFAState closure(DFAState s) {
    HashSet<State> ret = new HashSet<State>();
    HashSet<State> toAdd;
    boolean isAccept = false;
    String name = "";
    DFAState output;

    // Add all input states to output
    for (State i : s.getStates()) {
      ret.add(i);
    }

    while (true) {
      toAdd = new HashSet<State>();

      // Iterate over all states currently in ret
      for (State r : ret) {
        if (r.getTransitionTable().containsKey("")) {
          for (State t : r.getTransitionTable().get("")) {
            if (!ret.contains(t)) {
              toAdd.add(t);
            }
          }
        }
      }
      if (toAdd.isEmpty()) {
        break;
      }
      for (State a : toAdd) {
        ret.add(a);
      }
    }
    // Determine if new state is accept
    for (State t : ret) {
      if (t.isAccept()) {
        isAccept = true;
        name = t.getName();
        break;
      }
    }
    // Create new DFAState
    output = new DFAState(name, isAccept, ret, new HashMap<String, DFAState>());
    for (DFAState sta : states) {
      if (output.equals(sta)) {
        output = sta;
        break;
      }
    }
    //        if(states.contains(output)){
    //        	System.out.println("NOOO");
    //        }
    states.add(output);
    if (output.isAccept()) {
      accept.add(output);
    }
    return output;
  }