Пример #1
0
 private static void AddState(RegexState state, ISet<RegexState> stateSet)
 {
     stateSet.Add(state);
     foreach (var eps in state.Epsilon) {
         AddState(eps, stateSet);
     }
 }
Пример #2
0
 private void HandleAlt(Token token)
 {
     var n2 = _regexStack.Pop();
     var n1 = _regexStack.Pop();
     var s0 = new RegexState();
     s0.Epsilon.Add(n1.Start);
     s0.Epsilon.Add(n2.Start);
     var s3 = new RegexState();
     n1.End.Epsilon.Add(s3);
     n2.End.Epsilon.Add(s3);
     n1.End.IsFinalStates = false;
     n2.End.IsFinalStates = false;
     var regex = new Regex(s0, s3);
     _regexStack.Push(regex);
 }
Пример #3
0
 private static void Draw(RegexState regexState, StringBuilder sb, HashSet<string> set)
 {
     if (set.Contains(regexState.ToString())) {
         return;
     }
     set.Add(regexState.ToString());
     if (!regexState.IsFinalStates) {
         sb.Append($"    {regexState} [shape=\"circle\"];\n");
     }
     foreach (var transition in regexState.Transitions) {
         sb.Append($"    {regexState} -> {transition.Value}[label=\"{transition.Key}\"];\n");
         Draw(transition.Value, sb, set);
     }
     foreach (var state in regexState.Epsilon) {
         sb.Append($"    {regexState} -> {state}[label=\"ε\"];\n");
         Draw(state, sb, set);
     }
 }
Пример #4
0
 private void HandleChar(Token token)
 {
     var s0 = new RegexState();
     var s1 = new RegexState();
     s0.Transitions.Add(token.Text.First(), s1);
     var regex = new Regex(s0, s1);
     _regexStack.Push(regex);
 }
Пример #5
0
 private void HandleRepeat(Token token)
 {
     var s0 = _regexStack.Pop();
     var s1 = new RegexState();
     var s2 = new RegexState();
     s1.Epsilon.Add(s0.Start);
     if (token.Text == "*") {
         s1.Epsilon.Add(s2);
     }
     s0.End.Epsilon.Add(s2);
     s0.End.Epsilon.Add(s0.Start);
     s0.End.IsFinalStates = false;
     var regex = new Regex(s1, s2);
     _regexStack.Push(regex);
 }
Пример #6
0
 public Regex(RegexState start, RegexState end)
 {
     Start = start;
     End = end;
     End.IsFinalStates = true;
 }