Exemplo n.º 1
0
        internal SymbolicRegex <T> Transform <T>(SymbolicRegex <S> sr, SymbolicRegexBuilder <T> builderT, Func <S, T> predicateTransformer)
        {
            switch (sr.kind)
            {
            case SymbolicRegexKind.StartAnchor:
                return(builderT.startAnchor);

            case SymbolicRegexKind.EndAnchor:
                return(builderT.endAnchor);

            case SymbolicRegexKind.Epsilon:
                return(builderT.epsilon);

            case SymbolicRegexKind.Singleton:
                return(builderT.MkSingleton(predicateTransformer(sr.set)));

            case SymbolicRegexKind.Loop:
                return(builderT.MkLoop(Transform(sr.left, builderT, predicateTransformer), sr.lower, sr.upper));

            case SymbolicRegexKind.Or:
                return(builderT.MkOr(sr.alts.Transform(builderT, predicateTransformer)));

            case SymbolicRegexKind.Concat:
                return(builderT.MkConcat(Transform(sr.left, builderT, predicateTransformer),
                                         Transform(sr.right, builderT, predicateTransformer)));

            default:     //ITE
                return
                    (builderT.MkIfThenElse(Transform(sr.IteCond, builderT, predicateTransformer),
                                           Transform(sr.left, builderT, predicateTransformer),
                                           Transform(sr.right, builderT, predicateTransformer)));
            }
        }
Exemplo n.º 2
0
 public SymbolicRegexSampler(SymbolicRegex <S> sr, int maxUnroll, int cornerCaseProb = 5, int maxSamplingIter = 3)
 {
     this.cornerCaseProb  = cornerCaseProb;
     this.maxSamplingIter = maxSamplingIter;
     this.maxUnroll       = maxUnroll;
     this.sr      = sr;
     this.builder = sr.builder;
     rand         = new Random();
 }
Exemplo n.º 3
0
        //public SymbolicRegexBuilder<S> SRBuilder
        //{
        //    get
        //    {
        //        return srBuilder;
        //    }
        //}

        /// <summary>
        /// Constructs a regex to symbolic finite automata converter
        /// </summary>
        /// <param name="solver">solver for character constraints</param>
        /// <param name="categorizer">maps unicode categories to corresponding character conditions</param>
        internal RegexToAutomatonConverter(ICharAlgebra <S> solver, IUnicodeCategoryTheory <S> categorizer = null)
        {
            this.solver      = solver;
            this.categorizer = (categorizer == null ? new UnicodeCategoryTheory <S>(solver) : categorizer);
            description.Add(solver.True, ".");
            //"[]" does not unfortunately parse as a valid regex
            //description.Add(solver.False, "[0-[0]]");
            description.Add(solver.False, "[]");
            this.automBuilder = new RegexToAutomatonBuilder <RegexNode, S>(solver, ConvertNode);
            this.srBuilder    = new SymbolicRegexBuilder <S>((ICharAlgebra <S>)solver);
            //this.converterHelper.Callback = (node, start, end) => ConvertNode(node, start, end);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        ///// <summary>
        ///// node is assumed to be in left-assoc form if it is a concatenation
        ///// </summary>
        //Sequence<CounterOperation> GetNullabilityCondition_of_left_assoc(SymbolicRegexNode<S> node)
        //{
        //    switch (node.kind)
        //    {
        //        case SymbolicRegexKind.StartAnchor:
        //        case SymbolicRegexKind.EndAnchor:
        //        case SymbolicRegexKind.Epsilon:
        //            {
        //                return Sequence<CounterOperation>.Empty;
        //            }
        //        case SymbolicRegexKind.Singleton:
        //            {
        //                return null;
        //            }
        //        case SymbolicRegexKind.Or:
        //            {
        //                if (node.isNullable)
        //                    return Sequence<CounterOperation>.Empty;
        //                else
        //                    return null;
        //            }
        //        case SymbolicRegexKind.Loop:
        //            {
        //                if (node.isNullable)
        //                    return Sequence<CounterOperation>.Empty;
        //                else if (IsCountingLoop(node))
        //                    return new Sequence<CounterOperation>(new CounterOperation(node, CounterOp.EXIT));
        //                else
        //                    return null;
        //            }
        //        case SymbolicRegexKind.Concat:
        //            {
        //                var reset1 = GetNullabilityCondition_of_left_assoc(node.left);
        //                if (reset1 == null)
        //                    return null;
        //                else
        //                {
        //                    //we know that right is not a concat
        //                    var reset2 = GetNullabilityCondition_of_left_assoc(node.right);
        //                    if (reset2 == null)
        //                        return null;
        //                    else
        //                    {
        //                        //TBD: this optimization needs to be verified
        //                        //if reset2 is nonempty it can only be a singleton
        //                        if (reset1.IsEmpty || reset2.IsEmpty ||
        //                            reset1.TrueForAll(x => reset2[0].Counter.ContainsSubCounter(x.Counter)))
        //                            return reset1.Append(reset2);
        //                        else if (reset2[0].Counter.LowerBound == 0)
        //                        {
        //                            return reset1;
        //                        }
        //                        else
        //                        {
        //                            return null;
        //                        }
        //                    }
        //                }
        //            }
        //        default:
        //            {
        //                throw new NotSupportedException("GetNullabilityCondition not supported for " + node.kind);
        //            }
        //    }
        //}


        internal SymbolicRegexNode <T> Transform <T>(SymbolicRegexNode <S> sr, SymbolicRegexBuilder <T> builderT, Func <S, T> predicateTransformer)
        {
            switch (sr.kind)
            {
            case SymbolicRegexKind.StartAnchor:
                return(builderT.startAnchor);

            case SymbolicRegexKind.EndAnchor:
                return(builderT.endAnchor);

            case SymbolicRegexKind.WatchDog:
                return(builderT.MkWatchDog(sr.lower));

            case SymbolicRegexKind.Epsilon:
                return(builderT.epsilon);

            case SymbolicRegexKind.Singleton:
                return(builderT.MkSingleton(predicateTransformer(sr.set)));

            //case SymbolicRegexKind.Sequence:
            //    return builderT.MkSequence(new Sequence<T>(Array.ConvertAll<S,T>(sr.sequence.ToArray(), x => predicateTransformer(x))));
            case SymbolicRegexKind.Loop:
                return(builderT.MkLoop(Transform(sr.left, builderT, predicateTransformer), sr.isLazyLoop, sr.lower, sr.upper));

            case SymbolicRegexKind.Or:
                return(builderT.MkOr(sr.alts.Transform(builderT, predicateTransformer)));

            case SymbolicRegexKind.And:
                return(builderT.MkAnd(sr.alts.Transform(builderT, predicateTransformer)));

            case SymbolicRegexKind.Concat:
            {
                var sr_elems            = sr.ToArray();
                var sr_elems_trasformed = Array.ConvertAll(sr_elems, x => Transform(x, builderT, predicateTransformer));
                return(builderT.MkConcat(sr_elems_trasformed, false));
            }

            default:     //ITE
                return
                    (builderT.MkIfThenElse(Transform(sr.IteCond, builderT, predicateTransformer),
                                           Transform(sr.left, builderT, predicateTransformer),
                                           Transform(sr.right, builderT, predicateTransformer)));
            }
        }
Exemplo n.º 6
0
        string GenerateRandomMember(SymbolicRegexBuilder <S> builder, SymbolicRegexNode <S> root)
        {
            // TODO: ITE, And: are currently not supported.
            string sample = "";
            Stack <SymbolicRegexNode <S> > nodeQueue = new Stack <SymbolicRegexNode <S> >();
            SymbolicRegexNode <S>          curNode   = null;

            nodeQueue.Push(UnrollRE(root));
            while (nodeQueue.Count > 0 || curNode != null)
            {
                if (curNode == null)
                {
                    curNode = nodeQueue.Pop();
                }
                switch (curNode.Kind)
                {
                case SymbolicRegexKind.Singleton:
                    if (!builder.solver.IsSatisfiable(curNode.Set))
                    {
                        throw new AutomataException(AutomataExceptionKind.SetIsEmpty);
                    }

                    sample += builder.solver.ChooseUniformly(curNode.Set);
                    curNode = null;
                    break;

                case SymbolicRegexKind.Loop:
                    curNode = curNode.Left;
                    break;

                case SymbolicRegexKind.Epsilon:
                    curNode = null;
                    break;

                case SymbolicRegexKind.Concat:
                    nodeQueue.Push(curNode.Right);
                    curNode = curNode.Left;
                    break;

                case SymbolicRegexKind.Or:
                    int choice = rand.Next(curNode.OrCount);
                    int i      = 0;
                    foreach (var elem in curNode.alts)
                    {
                        if (i == choice)
                        {
                            curNode = elem;
                            break;
                        }
                        else
                        {
                            i += 1;
                        }
                    }
                    break;

                case SymbolicRegexKind.EndAnchor:
                case SymbolicRegexKind.StartAnchor:
                case SymbolicRegexKind.WatchDog:
                    curNode = null;
                    break;

                default:
                    throw new NotImplementedException(curNode.Kind.ToString());
                }
            }
            return(sample);
        }
Exemplo n.º 7
0
 public CABA(SymbolicRegexBuilder <S> builder)
 {
     this.builder = builder;
 }
Exemplo n.º 8
0
        //public SymbolicRegexBuilder<S> SRBuilder
        //{
        //    get
        //    {
        //        return srBuilder;
        //    }
        //}

        /// <summary>
        /// Constructs a regex to symbolic finite automata converter
        /// </summary>
        /// <param name="solver">solver for character constraints</param>
        /// <param name="categorizer">maps unicode categories to corresponding character conditions</param>
        public RegexToAutomatonConverter(ICharAlgebra <S> solver, IUnicodeCategoryTheory <S> categorizer = null)
        {
            this.solver      = solver;
            this.categorizer = (categorizer == null ? new UnicodeCategoryTheory <S>(solver) : categorizer);
            this.srBuilder   = new SymbolicRegexBuilder <S>((ICharAlgebra <S>)solver);
        }