/// <summary> /// Traverses all left fields of the <see cref="RegExp"/>. /// </summary> /// <param name="right">The initial right part of the <see cref="RegExp"/>.</param> /// <returns>Returns the final string of everyting right of the original.</returns> private string TraverseRight(RegExp right) { string leftPart = ""; string rightPart = ""; char operatorAsChar = '$'; if (right.LeftRegExp != null) { leftPart = TraverseLeft(right.LeftRegExp); } if (right.op != Operator.ONE) { operatorAsChar = EnumToChar(right.op); } if (right.RightRegExp != null) { rightPart = TraverseRight(right.RightRegExp); } string rightString = right.terminals; if (operatorAsChar != '$') { rightString = rightString + operatorAsChar; } if (leftPart != string.Empty) { if (operatorAsChar != '|') { rightString = "(" + leftPart + ")" + rightString; } else { rightString = leftPart + rightString; } } if (rightPart != string.Empty) { if (operatorAsChar != '|') { rightString = rightString + "(" + rightPart + ")"; } else { rightString = rightString + rightPart; } } return(rightString); }
private static void TestRegExAndThompson() { RegExpTest ret = new RegExpTest(); ret.testLanguage(); ret.testToString(); RegExp regExp1 = new RegExp("baa"); RegExp regExp2 = new RegExp("aba"); RegExp regExp3 = new RegExp("bb"); RegExp oneOrTwo = regExp1.Or(regExp2); RegExp orStar = oneOrTwo.Star(); RegExp orStarDotThree = orStar.Dot(regExp3); RegExp orStarDotThreePlus = orStarDotThree.Plus(); Console.WriteLine("-----------------------------------------"); Console.WriteLine("And conversion by use of the thompson construction:"); Console.WriteLine("-----------------------------------------"); ThompsonConstruction thompson = new ThompsonConstruction(); Automata <string> thompsonNDFA = thompson.GenerateNDFA(orStarDotThreePlus); Console.WriteLine("thompsonNDFA is dfa? " + thompsonNDFA.IsDfa()); Console.WriteLine("-----------------------------------------"); thompsonNDFA.PrintTransitions(); Console.WriteLine("-----------------------------------------"); foreach (string state in thompsonNDFA.StartStates) { Console.WriteLine(state); } Console.WriteLine("-----------------------------------------"); foreach (string state in thompsonNDFA.FinalStates) { Console.WriteLine(state); } Console.WriteLine("-----------------------------------------"); Console.WriteLine("String {0} is a {1} string for Auto3.", THOMPSON_TEST_1, thompsonNDFA.IsStringAcceptable(THOMPSON_TEST_1) ? "valid" : "invalid"); Console.WriteLine("String {0} is a {1} string for Auto3.", THOMPSON_TEST_2, thompsonNDFA.IsStringAcceptable(THOMPSON_TEST_2) ? "valid" : "invalid"); Console.WriteLine("String {0} is a {1} string for Auto3.", THOMPSON_TEST_3, thompsonNDFA.IsStringAcceptable(THOMPSON_TEST_3) ? "valid" : "invalid"); Console.WriteLine("-----------------------------------------"); }
public RegExpTest() { a = new RegExp("a"); b = new RegExp("b"); // expr1: "baa" expr1 = new RegExp("baa"); // expr2: "bb" expr2 = new RegExp("bb"); // expr3: "baa | baa" expr3 = expr1.Or(expr2); // all: "(a|b)*" all = (a.Or(b)).Star(); // expr4: "(baa | baa)+" expr4 = expr3.Plus(); // expr5: "(baa | baa)+ (a|b)*" expr5 = expr4.Dot(all); }