Ejemplo n.º 1
0
 public void TestSTbFromRegexLoop()
 {
     Z3Provider solver = new Z3Provider(BitWidth.BV8);
     STbFromRegexBuilder <FuncDecl, Expr, Sort> builder = new STbFromRegexBuilder <FuncDecl, Expr, Sort>(solver);
     var res = builder.Mk(@"(\s*(?<i>\d{2,3})\s(?<b>\w{4,5}),(?<le>\d+)(?<la>[a-z]{2,})[\s-[\n]]*\n)*", "i", "int", "b", "bool", "le", "length", "la", "last");
     //res.ToST().ShowGraph();
 }
Ejemplo n.º 2
0
        STb <FuncDecl, Expr, Sort> Generate()
        {
            var name = DeclarationType.ContainingNamespace.Name + "." +
                       (DeclarationType.ContainingType == null ? "" : DeclarationType.ContainingType.Name + ".") + DeclarationType.Name;

            Console.WriteLine("Regex " + name);

            var builder = new STbFromRegexBuilder <FuncDecl, Expr, Sort>(_automataCtx);
            var stb     = builder.Mk(_regex, "value", _type);

            if ((stb.OutputSort is TupleSort || _automataCtx.IsTupleSort(stb.OutputSort)) && _automataCtx.GetTupleConstructor(stb.OutputSort).Arity == 1)
            {
                var fieldDecl = _automataCtx.GetTupleField(stb.OutputSort, 0);
                var reg       = _automataCtx.MkTuple();
                var projector = new STb <FuncDecl, Expr, Sort>(_automataCtx, "Project", stb.OutputSort, fieldDecl.Range, reg.Sort, reg, 0);
                projector.AssignRule(0, new BaseRule <Expr>(new Sequence <Expr>(fieldDecl.Apply(projector.InputVar)), reg, 0));
                projector.AssignFinalRule(0, new BaseRule <Expr>(Sequence <Expr> .Empty, reg, 0));
                stb = stb.Compose(projector).Flatten();
            }

            if (ShowGraphStages.Count > 0)
            {
                stb.ToST().ShowGraph();
            }
            return(stb);
        }
Ejemplo n.º 3
0
 public void TestSTbFromRegex2()
 {
     Z3Provider solver = new Z3Provider(BitWidth.BV8);
     STbFromRegexBuilder <FuncDecl, Expr, Sort> builder = new STbFromRegexBuilder <FuncDecl, Expr, Sort>(solver);
     var res = builder.Mk(@"([^,]*,){2}(?<int>\d+),(?<bool>(true|false))", "int", "int", "bool", "bool");
     //res.ToST().ShowGraph();
 }
Ejemplo n.º 4
0
        public void TestSTb_ParseCommaSeparatedIntegers()
        {
            Z3Provider solver = new Z3Provider(BitWidth.BV8);
            STbFromRegexBuilder <FuncDecl, Expr, Sort> builder = new STbFromRegexBuilder <FuncDecl, Expr, Sort>(solver);
            var res = builder.Mk(@"((?<i>\d+),)*", "i", "int");

            res.Name = "ParseCommaSeparatedIntegers";
            //res.ToST().ShowGraph();
        }
Ejemplo n.º 5
0
        public void TestSTb_ParseCommaSeparatedIntegers()
        {
            Z3Provider solver = new Z3Provider(BitWidth.BV8);
            STbFromRegexBuilder <FuncDecl, Expr, Sort> builder = new STbFromRegexBuilder <FuncDecl, Expr, Sort>(solver);
            var res = builder.Mk(@"(?<i>\d+),(?<w>\w+),(?<single>\w)", "i", "int", "w", "last", "single", "last");

            res.Name = "ParseCommaSeparatedIntegers";
            //res.ShowGraph();
            var st = res.ToST();

            st.ShowGraph();
        }
Ejemplo n.º 6
0
        public void TestSTb_UTf8DecodeAndParse()
        {
            string utf8decode_bek = @"
function fuse(r,c) = ((r << 6) | (c & 0x3F));
function one(c) = ((0 <= c) && (c <= 0x7F));
function C2_DF(c) = ((0xC2 <= c) && (c <= 0xDF));
function E1_EF(c) = ((0xE1 <= c) && (c <= 0xEF));
function A0_BF(c) = ((0xA0 <= c) && (c <= 0xBF));
function x80_BF(c) = ((0x80 <= c) && (c <= 0xBF));
function x80_9F(c) = ((0x80 <= c) && (c <= 0x9F));
program utf8decode(input){
  return iter(c in input)[q := 0; r := 0;] 
  {
    case (q == 0):
      if (one(c))                  {yield (c);}
      else if (C2_DF(c))           {q := 2; r := (c & 0x1F);}    // ------ 2 bytes --------
      else if (c == 0xE0)          {q := 4; r := (c & 0x0F);}    // ------ 3 bytes --------
      else if (c == 0xED)          {q := 5; r := (c & 0x0F);}    // ------ 3 bytes --------
      else if (E1_EF(c))           {q := 3; r := (c & 0x0F);}    // ------ 3 bytes --------
      else {raise InvalidInput;}

    case (q == 2): 
      if (x80_BF(c))                 {q := 0; yield(fuse(r,c)); r := 0;}
      else {raise InvalidInput;}

    case (q == 3): 
      if (x80_BF(c))                 {q := 2; r := fuse(r,c);}
      else {raise InvalidInput;}

    case (q == 4): 
      if (A0_BF(c))                  {q := 2; r := fuse(r,c);}
      else {raise InvalidInput;}

    case (q == 5): 
      if (x80_9F(c))                 {q := 2; r := fuse(r,c);}
      else {raise InvalidInput;}

    end case (!(q == 0)):
      raise InvalidInput;
  }; 
}
";

            Z3Provider solver     = new Z3Provider(BitWidth.BV16);
            var        dec        = BekConverter.BekToSTb(solver, utf8decode_bek);
            var        utf8decode = dec.ExploreBools();

            STbFromRegexBuilder <FuncDecl, Expr, Sort> builder = new STbFromRegexBuilder <FuncDecl, Expr, Sort>(solver);
            var parse = builder.Mk(@"((?<i>[1-9][0-9]*),)*", "i", "int");

            var comp = utf8decode.Compose(parse);

            //utf8decode.ToST().ShowGraph();

            //parse.ToST().ShowGraph();

            //comp.ToST().ShowGraph();

            var simpl = comp.Simplify();

            //simpl.ToST().ShowGraph();
        }
Ejemplo n.º 7
0
        public void TestSTb_UTf8DecodeAndParse()
        {
            string utf8decode_bek = @"
            function fuse(r,c) = ((r << 6) | (c & 0x3F));
            function one(c) = ((0 <= c) && (c <= 0x7F));
            function C2_DF(c) = ((0xC2 <= c) && (c <= 0xDF));
            function E1_EF(c) = ((0xE1 <= c) && (c <= 0xEF));
            function A0_BF(c) = ((0xA0 <= c) && (c <= 0xBF));
            function x80_BF(c) = ((0x80 <= c) && (c <= 0xBF));
            function x80_9F(c) = ((0x80 <= c) && (c <= 0x9F));
            program utf8decode(input){
              return iter(c in input)[q := 0; r := 0;]
              {
            case (q == 0):
              if (one(c))                  {yield (c);}
              else if (C2_DF(c))           {q := 2; r := (c & 0x1F);}    // ------ 2 bytes --------
              else if (c == 0xE0)          {q := 4; r := (c & 0x0F);}    // ------ 3 bytes --------
              else if (c == 0xED)          {q := 5; r := (c & 0x0F);}    // ------ 3 bytes --------
              else if (E1_EF(c))           {q := 3; r := (c & 0x0F);}    // ------ 3 bytes --------
              else {raise InvalidInput;}

            case (q == 2):
              if (x80_BF(c))                 {q := 0; yield(fuse(r,c)); r := 0;}
              else {raise InvalidInput;}

            case (q == 3):
              if (x80_BF(c))                 {q := 2; r := fuse(r,c);}
              else {raise InvalidInput;}

            case (q == 4):
              if (A0_BF(c))                  {q := 2; r := fuse(r,c);}
              else {raise InvalidInput;}

            case (q == 5):
              if (x80_9F(c))                 {q := 2; r := fuse(r,c);}
              else {raise InvalidInput;}

            end case (!(q == 0)):
              raise InvalidInput;
              };
            }
            ";

            Z3Provider solver = new Z3Provider(BitWidth.BV16);
            var dec = BekConverter.BekToSTb(solver, utf8decode_bek);
            var utf8decode = dec.ExploreBools();

            STbFromRegexBuilder<FuncDecl, Expr, Sort> builder = new STbFromRegexBuilder<FuncDecl, Expr, Sort>(solver);
            var parse = builder.Mk(@"((?<i>[1-9][0-9]*),)*", "i", "int");

            var comp = utf8decode.Compose(parse);

            //utf8decode.ToST().ShowGraph();

            //parse.ToST().ShowGraph();

            //comp.ToST().ShowGraph();

            var simpl = comp.Simplify();

            //simpl.ToST().ShowGraph();
        }
Ejemplo n.º 8
0
 public void TestSTb_ParseIntegers()
 {
     Z3Provider solver = new Z3Provider(BitWidth.BV8);
     STbFromRegexBuilder<FuncDecl, Expr, Sort> builder = new STbFromRegexBuilder<FuncDecl, Expr, Sort>(solver);
     var res = builder.Mk(@"((?<i>\d+),)*", "i", "int");
     res.Name = "ParseIntegers";
     //res.ToST().ShowGraph();
 }
Ejemplo n.º 9
0
 public void TestSTbFromRegexLoop()
 {
     Z3Provider solver = new Z3Provider(BitWidth.BV8);
     STbFromRegexBuilder<FuncDecl, Expr, Sort> builder = new STbFromRegexBuilder<FuncDecl, Expr, Sort>(solver);
     var res = builder.Mk(@"(\s*(?<i>\d{2,3})\s(?<b>\w{4,5}),(?<le>\d+)(?<la>[a-z]{2,})[\s-[\n]]*\n)*", "i", "int", "b", "bool", "le", "length", "la", "last");
     //res.ToST().ShowGraph();
 }
Ejemplo n.º 10
0
 public void TestSTbFromRegex2()
 {
     Z3Provider solver = new Z3Provider(BitWidth.BV8);
     STbFromRegexBuilder<FuncDecl, Expr, Sort> builder = new STbFromRegexBuilder<FuncDecl, Expr, Sort>(solver);
     var res = builder.Mk(@"([^,]*,){2}(?<int>\d+),(?<bool>(true|false))", "int", "int", "bool", "bool");
     //res.ToST().ShowGraph();
 }