Ejemplo n.º 1
0
 internal RegexAutomaton(BVAlgebra algebra, Regex regex, int alphabetSize, int stateCount, int[] delta, HashSet <int> isfinalstate, DecisionTree dt, int nonfinalSinkState = -1, int finalSinkState = -1)
 {
     this.algebra           = algebra;
     this.regex             = regex;
     this.SymbolCount       = alphabetSize;
     this.StateCount        = stateCount;
     this.delta             = delta;
     this.finalstates       = isfinalstate;
     this.NonfinalSinkState = nonfinalSinkState;
     this.FinalSinkState    = finalSinkState;
     this.dt = dt;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Copmiles a regex into a symbolic regex
        /// </summary>
        /// <param name="regex">given regex</param>
        /// <param name="css">given solver, if null a new one is created</param>
        /// <param name="simplify">if true then lower loop bounds are unwound (default is true)</param>
        /// <returns></returns>
        public static SymbolicRegex <BV> Compile(this Regex regex, CharSetSolver css = null, bool simplify = true)
        {
            if (css == null)
            {
                css = new CharSetSolver();
            }
            var       sr_bdd = css.RegexConverter.ConvertToSymbolicRegex(regex, true);
            BVAlgebra bva    = new BVAlgebra(css, sr_bdd.ComputeMinterms());
            SymbolicRegexBuilder <BV> builder = new SymbolicRegexBuilder <BV>(bva);
            var sr_bv = sr_bdd.builder.Transform <BV>(sr_bdd, builder, builder.solver.ConvertFromCharSet);

            if (simplify)
            {
                sr_bv = sr_bv.Simplify();
            }
            sr_bv.InitializeMatcher();
            return(sr_bv);
        }
Ejemplo n.º 3
0
        static internal RegexAutomaton Create(CharSetSolver solver, Regex regex)
        {
            int t0       = System.Environment.TickCount;
            var nfa      = solver.Convert(regex.ToString(), regex.Options);
            var minterms = GetMinterms(nfa);
            //create specialized BV algebra for this particular minterm partition
            var bvsolver = new BVAlgebra(solver, minterms);
            //convert the nfa to the specialized algebra
            var nfa_BV = nfa.ReplaceAlgebra <BV>(bvsolver.MapPredToBV, bvsolver);

            t0 = System.Environment.TickCount - t0;
            int t    = System.Environment.TickCount;
            var nfa1 = nfa.Minimize();//.RemoveEpsilons().Minimize();
            var dfa  = nfa1.Determinize().Minimize().Normalize();

            t = System.Environment.TickCount - t;
            int t_BV       = System.Environment.TickCount;
            var nfa1_BV    = nfa_BV;//.RemoveEpsilons().Minimize();
            var dfa_BV     = nfa1_BV.Minimize().Determinize();
            var dfa_BV_min = dfa_BV.Minimize().Normalize();

            t_BV = System.Environment.TickCount - t_BV;
            //number of states
            var N = dfa.StateCount;
            //number of symbols
            int t2                = System.Environment.TickCount;
            var K                 = minterms.Length;
            var isfinalstate      = new HashSet <int>();
            var nonfinalSinkstate = -1;
            var finalSinkstate    = -1;

            for (int q = 0; q < N; q++)
            {
                if (dfa.IsFinalState(q))
                {
                    isfinalstate.Add(q);
                }
                if (dfa.IsLoopState(q) && dfa.GetMovesCountFrom(q) == 1)
                {
                    //there can only be at most one of each because dfa is minimal
                    if (dfa.IsFinalState(q))
                    {
                        if (finalSinkstate != -1)
                        {
                            throw new AutomataException(AutomataExceptionKind.InternalError_RegexAutomaton);
                        }
                        finalSinkstate = q;
                    }
                    else
                    {
                        if (nonfinalSinkstate != -1)
                        {
                            throw new AutomataException(AutomataExceptionKind.InternalError_RegexAutomaton);
                        }
                        nonfinalSinkstate = q;
                    }
                }
            }
            var delta = new int[K * N];

            for (int q = 0; q < dfa.StateCount; q++)
            {
                int symbols_mapped = 0;
                foreach (var move in dfa.GetMovesFrom(q))
                {
                    for (int a = 0; a < K; a++)
                    {
                        var phi = solver.MkAnd(move.Label, minterms[a]);
                        if (!phi.IsEmpty)
                        {
                            delta[(move.SourceState * K) + a] = move.TargetState;
                            symbols_mapped += 1;
                        }
                    }
                }
                if (symbols_mapped != K)
                {
                    throw new AutomataException(AutomataExceptionKind.InternalError_RegexAutomaton);
                }
            }
            var dt = DecisionTree.Create(solver, minterms);
            var ra = new RegexAutomaton(bvsolver, regex, K, N, delta, isfinalstate, dt, nonfinalSinkstate, finalSinkstate);

            t2 = System.Environment.TickCount - t2;
            return(ra);
        }