public override NFAModel ConvertAlternation(AlternationExpression exp) { var nfa1 = Convert(exp.Expression1); var nfa2 = Convert(exp.Expression2); NFAState head = new NFAState(); NFAState tail = new NFAState(); //build edges head.AddEdge(nfa1.EntryEdge); head.AddEdge(nfa2.EntryEdge); nfa1.TailState.AddEmptyEdgeTo(tail); nfa2.TailState.AddEmptyEdgeTo(tail); NFAModel alternationNfa = new NFAModel(); alternationNfa.AddState(head); alternationNfa.AddStates(nfa1.States); alternationNfa.AddStates(nfa2.States); alternationNfa.AddState(tail); //add an empty entry edge alternationNfa.EntryEdge = new NFAEdge(head); alternationNfa.TailState = tail; return(alternationNfa); }
public override NFAModel ConvertStringLiteral(StringLiteralExpression exp) { NFAModel literalNfa = new NFAModel(); NFAState lastState = null; foreach (var symbol in exp.Literal) { var symbolState = new NFAState(); int cclass = CompactCharSetManager.GetCompactClass(symbol); var symbolEdge = new NFAEdge(cclass, symbolState); if (lastState != null) { lastState.AddEdge(symbolEdge); } else { literalNfa.EntryEdge = symbolEdge; } lastState = symbolState; literalNfa.AddState(symbolState); } literalNfa.TailState = lastState; return(literalNfa); }
public override NFAModel ConvertAlternationCharSet(AlternationCharSetExpression exp) { NFAState head = new NFAState(); NFAState tail = new NFAState(); //build edges NFAModel charSetNfa = new NFAModel(); charSetNfa.AddState(head); HashSet <int> classesSet = new HashSet <int>(); foreach (var symbol in exp.CharSet) { int cclass = CompactCharSetManager.GetCompactClass(symbol); if (classesSet.Add(cclass)) { var symbolEdge = new NFAEdge(cclass, tail); head.AddEdge(symbolEdge); } } charSetNfa.AddState(tail); //add an empty entry edge charSetNfa.EntryEdge = new NFAEdge(head); charSetNfa.TailState = tail; return(charSetNfa); }
public override NFAModel ConvertKleeneStar(KleeneStarExpression exp) { var innerNFA = Convert(exp.InnerExpression); var newTail = new NFAState(); var entry = new NFAEdge(newTail); innerNFA.TailState.AddEmptyEdgeTo(newTail); newTail.AddEdge(innerNFA.EntryEdge); var kleenStarNFA = new NFAModel(); kleenStarNFA.AddStates(innerNFA.States); kleenStarNFA.AddState(newTail); kleenStarNFA.EntryEdge = entry; kleenStarNFA.TailState = newTail; return(kleenStarNFA); }
private void ConvertLexcionToNFA() { //Compact transition char set CompactCharSetManager = m_lexicon.CreateCompactCharSetManager(); NFAConverter converter = new NFAConverter(CompactCharSetManager); NFAState entryState = new NFAState(); NFAModel lexerNFA = new NFAModel(); lexerNFA.AddState(entryState); foreach (var token in m_lexicon.GetTokens()) { NFAModel tokenNFA = token.CreateFiniteAutomatonModel(converter); entryState.AddEdge(tokenNFA.EntryEdge); lexerNFA.AddStates(tokenNFA.States); } lexerNFA.EntryEdge = new NFAEdge(entryState); m_nfa = lexerNFA; }