Пример #1
0
        public static void testRegularExpressionThompson2()
        {
            //This is the regular expression ( (a|b|c|d)|(ab|ad|bc) )+ (aab) (c|cad|cb)*
            RegularExpression exp1, exp2, exp3, exp4, a, b, c, d, ab, ad, bc, abb, cad, cb,all;
            a = new RegularExpression("a");
            b = new RegularExpression("b");
            c = new RegularExpression("c");
            d = new RegularExpression("d");
            ab = new RegularExpression("ab");
            ad = new RegularExpression("ad");
            bc = new RegularExpression("bc");
            abb = new RegularExpression("abb");
            cad = new RegularExpression("cad");
            cb = new RegularExpression("cb");

            // (a|b|c|d)
            exp1 = (a.or(b).or(c).or(d));
            // (ab|ad|bc)
            exp2 = (ab.or(ad).or(bc));
            // (c|cad|cb)*
            exp3 = (c.or(cad).or(cb).star());
            // ( (a|b|c|d) | (ab|ad|bc) )+
            exp4 = (exp1.or(exp2).plus());
            // Merge
            all = exp4.dot(abb.dot(exp3));

            Automata<String> auto = new Automata<String>();
            int num = 0;
            all.regexToNDFA(ref num, ref auto);
            generateAutomataImage(auto);
            return;
        }
Пример #2
0
 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;
 }