Logo Search packages:      
Sourcecode: jflex version File versions

IntPair JFlex::NFA::insertNFA ( RegExp  regExp  )  [inline]

Constructs an NFA for regExp such that the NFA has

exactly one start state, exactly one end state, no transitions leading out of the end state no transitions leading into the start state

Parameters:
regExp the regular expression to construct the NFA for
Returns:
a pair of integers denoting the index of start and end state of the NFA.

Definition at line 788 of file NFA.java.

References complement(), JFlex::IntPair::end, JFlex::Macros::getDefinition(), JFlex::RegExp2::r1, JFlex::RegExp2::r2, JFlex::IntPair::start, and JFlex::RegExp::type.

                                          {
    
    IntPair nfa1, nfa2;
    int start, end;
    RegExp2 r;
    
    if (Out.DEBUG)
      Out.debug("Inserting RegExp : "+regExp);
    
    switch (regExp.type) {
      
    case sym.BAR:
      
      r = (RegExp2) regExp;
      
      nfa1 = insertNFA(r.r1);
      nfa2 = insertNFA(r.r2);
      
      start = nfa2.end+1;
      end   = nfa2.end+2;
      
      addEpsilonTransition(start, nfa1.start);
      addEpsilonTransition(start, nfa2.start);
      addEpsilonTransition(nfa1.end, end);
      addEpsilonTransition(nfa2.end, end);
      
      return new IntPair(start, end);
      
    case sym.CONCAT:
        
      r = (RegExp2) regExp;
        
      nfa1 = insertNFA(r.r1);
      nfa2 = insertNFA(r.r2);
      
      addEpsilonTransition(nfa1.end, nfa2.start);
      
      return new IntPair(nfa1.start, nfa2.end);
      
    case sym.STAR:
      nfa1 = insertNFA( (RegExp) ((RegExp1) regExp).content );
      
      start = nfa1.end+1;
      end   = nfa1.end+2;               
      
      addEpsilonTransition(nfa1.end, end);     
      addEpsilonTransition(start, nfa1.start);
      
      addEpsilonTransition(start, end);
      addEpsilonTransition(nfa1.end, nfa1.start);
      
      return new IntPair(start, end);
      
    case sym.PLUS:
      nfa1 = insertNFA( (RegExp) ((RegExp1) regExp).content );
      
      start = nfa1.end+1;
      end   = nfa1.end+2;               
      
      addEpsilonTransition(nfa1.end, end);     
      addEpsilonTransition(start, nfa1.start);
      
      addEpsilonTransition(nfa1.end, nfa1.start);
      
      return new IntPair(start, end);
      
    case sym.QUESTION:
      nfa1 = insertNFA( (RegExp) ((RegExp1) regExp).content );
      
      addEpsilonTransition(nfa1.start, nfa1.end);
      
      return new IntPair(nfa1.start, nfa1.end);
      
    case sym.BANG:
      return complement(insertNFA((RegExp) ((RegExp1) regExp).content));

    case sym.TILDE:
      nfa1 = insertNFA((RegExp) ((RegExp1) regExp).content);
                 
      start  = nfa1.end+1;
      int s1 = start+1;
      int s2 = s1+1;
      end    = s2+1;

      for (int i = 0; i < numInput; i++) {
        addTransition(s1,i,s1);
        addTransition(s2,i,s2);
      }

      addEpsilonTransition(start, s1);
      addEpsilonTransition(s1, nfa1.start);
      addEpsilonTransition(nfa1.end, s2);
      addEpsilonTransition(s2, end);

      nfa1 = complement(new IntPair(start,end));
      nfa2 = insertNFA((RegExp) ((RegExp1) regExp).content);
      
      addEpsilonTransition(nfa1.end, nfa2.start);

      return new IntPair(nfa1.start, nfa2.end);
      
    case sym.CCLASS:
      return insertClassNFA( (Vector) ((RegExp1) regExp).content );
      
    case sym.CCLASSNOT:
      return insertNotClassNFA( (Vector) ((RegExp1) regExp).content );
      
    case sym.CHAR:
      return insertLetterNFA( ((Character) ((RegExp1) regExp).content).charValue() );
      
    case sym.STRING:
      return insertStringNFA( (String) ((RegExp1) regExp).content );
      
    case sym.MACROUSE:
      return insertNFA(macros.getDefinition((String) ((RegExp1) regExp).content));
    }
    
    return null;
  }


Generated by  Doxygen 1.6.0   Back to index