public static void testRegExpThompson() { RegularExpression exp1, exp2, exp3, exp4, exp5, a, b, c, all, why; a = new RegularExpression("a"); b = new RegularExpression("b"); c = new RegularExpression("c"); //exp 1 (baa) exp1 = new RegularExpression("baa"); //exp 2 (ac) exp2 = new RegularExpression("ac"); //exp 4 (baa)+ exp4 = exp1.plus(); //exp 5 (ac)* exp5 = exp2.star(); //exp 3 (a|b|c)* exp3 = (a.or(b).or(c)).star(); //all (a|b|c)* (baa)+ (ca)* all = exp3.dot(exp4.dot(exp5)); why = new RegularExpression("baa").plus().star().plus(); foreach (String o in all.getLanguage(5)) { Console.Write(o + " "); } Automata<String> auto = new Automata<String>(); int num = 0; why.regexToNDFA(ref num,ref auto); return; }
public static RegularExpression generateRandomRegex(char[] alfabet, int depth=5) { bool wasmulti = false; RegularExpression begin = new RegularExpression("a"); for (int c = 0; c < depth; c++) { int type; if (wasmulti) type = r.Next(1, 2); else type = r.Next(1, 4); wasmulti = false; switch (type) { case 1: //ONE & DOT string terminal = ""; for (int i = 0; i < r.Next(1, 5); i++) { terminal = terminal + alfabet[r.Next(0, alfabet.Count())]; } begin = begin.dot(new RegularExpression(terminal)); break; case 2: //ONE & OR terminal = ""; for (int i = 0; i < r.Next(1, 5); i++) { terminal = terminal + alfabet[r.Next(0, alfabet.Count())]; } begin = begin.or(new RegularExpression(terminal)); break; case 3: //PLUS begin = begin.plus(); wasmulti = true; break; case 4: //STAR begin = begin.star(); wasmulti = true; break; } } return begin; }