Metaparse tutorial

Agenda 

DSL embedding in C++: current practice



Boost.Xpressive introduction



Template metaprogramming introduction



Embedding regular expressions

Lab 

Detailed tutorial



These slides



Lab environment



Solutions

https://github.com/sabel83/metaparse_tutorial

Mpllibs 

Template Metaprogramming libraries



http://abel.web.elte.hu/mpllibs 

Metaparse



Metamonad



Safe Printf



Xlxpressive

Mpllibs 

Ábel Sinkovics



Endre Sajó



Zoltán Porkoláb



István Siroki

Processing DSLs "a.*a"

developer

Processing DSLs main.cpp

int main() { string s; cin << s; sregex r = sregex::compile("a.*a"); smatch w; regex_search(s, w, r); // ... }

"a.*a"

developer

Processing DSLs main.cpp

int main() { string s; cin << s; sregex r = sregex::compile("a.*a"); smatch w; regex_search(s, w, r); // ... } Compilation

executable "a.*a"

"a.*a"

developer

Processing DSLs main.cpp

int main() { string s; cin << s; sregex r = sregex::compile("a.*a"); smatch w; regex_search(s, w, r); // ... } Compilation

Execution

executable "a.*a" "a.*a"

"a.*a"

developer

Processing DSLs main.cpp

int main() { string s; cin << s; sregex r = sregex::compile("a.*a"); smatch w; regex_search(s, w, r); // ... } Compilation

"a.*a"

developer

Execution

executable "a.*a" "a.*a"

Matching code

Processing DSLs main.cpp

int main() { string s; cin << s; sregex r = sregex::compile("a.*a"); smatch w; regex_search(s, w, r); // ... } Compilation

"a.*a"

developer

Execution matching...

executable "a.*a" "a.*a"

Matching code

Processing DSLs main.cpp

int main() { string s; cin << s; sregex r =as_xpr('a') >> *_ >> 'a'; smatch w; regex_search(s, w, r); // ... }

"a.*a"

developer

Processing DSLs main.cpp

int main() { string s; cin << s; sregex r =as_xpr('a') >> *_ >> 'a'; smatch w; regex_search(s, w, r); // ... } Compilation

executable Matching code

"a.*a"

developer

Processing DSLs main.cpp

int main() { string s; cin << s; sregex r =as_xpr('a') >> *_ >> 'a'; smatch w; regex_search(s, w, r); // ... } Compilation

"a.*a"

developer

Execution matching...

executable Matching code

Processing DSLs main.cpp

int main() { string s; cin << s; sregex r =as_xpr('a') >> *_ >> 'a'; smatch w; regex_search(s, w, r); // ... } Compilation

"a.*a"

developer

Execution matching...

executable Matching code

Processing DSLs main.cpp

int main() { string s; cin << s; sregex r = MPLLIBS_REGEX("a.*a"); smatch w; regex_search(s, w, r); // ... } Compilation

"a.*a"

developer

Execution matching...

executable Matching code

Processing DSLs main.cpp

int main() { string s; cin << s; sregex r = MPLLIBS_REGEX("a.*a"); smatch w; regex_search(s, w, r); // ... } Compilation Template metaprogram

executable Matching code

"a.*a"

developer

Execution matching...

Lab 0 Set up your working environment

git clone https://github.com/sabel83/metaparse_tutorial cd metaparse_tutorial/lab make

Boost.Xpressive 

Include the headers

#include

Create a matching object

sregex re = sregex::compile("ab*c"); 

Do some matching smatch what; regex_match("abbbbbbbc", what, re);

Lab 1  

Try Xpressive yourself Create a number of regular expressions using Xpressive "" "a" "abc" "b*" "ab*" "b*c" "ab*c" "a.*c" "a1*c" "(abc)*"

More Boost.Xpressive sregex re = sregex::compile("*")

More Boost.Xpressive sregex re = sregex::compile("*")

terminate called after throwing an instance of 'boost::exception_detail::clone_impl' what(): quantifier not expected

Boost.Xpressive 

Dynamic regex

sregex re = sregex::compile("ab*c");

Boost.Xpressive 

Dynamic regex

sregex re = sregex::compile("ab*c"); 

Static regex

sregex re =

Boost.Xpressive 

Dynamic regex

sregex re = sregex::compile("ab*c"); 

Static regex

sregex re = as_xpr('a')

Boost.Xpressive 

Dynamic regex

sregex re = sregex::compile("ab*c"); 

Static regex

sregex re = as_xpr('a')

as_xpr('b')

Boost.Xpressive 

Dynamic regex

sregex re = sregex::compile("ab*c"); 

Static regex

sregex re = as_xpr('a')

as_xpr('b')

as_xpr('c');

Boost.Xpressive 

Dynamic regex

sregex re = sregex::compile("ab*c"); 

Static regex

sregex re = as_xpr('a')

*as_xpr('b')

as_xpr('c');

Boost.Xpressive 

Dynamic regex

sregex re = sregex::compile("ab*c"); 

Static regex

sregex re = as_xpr('a') >> *as_xpr('b') >> as_xpr('c');

Lab 2 

Create the same regular expressions using static regexes of Xpressive

"" "a" "abc" "b*" "ab*" "b*c" "ab*c" "a.*c" "a1*c" "(abc)*"

Safe static regexes sregex re = REGEX("ab*c");

sregex re = as_xpr('a') >> *as_xpr('b') >> as_xpr('c');

Safe static regexes sregex re = REGEX("ab*c");

Magic happens here sregex re = as_xpr('a') >> *as_xpr('b') >> as_xpr('c');

Safe static regexes sregex re = REGEX("ab*c");

Template metaprogram

sregex re = as_xpr('a') >> *as_xpr('b') >> as_xpr('c');

Safe static regexes sregex re = REGEX("ab*c");

Template metaprogram

type

sregex re = as_xpr('a') >> *as_xpr('b') >> as_xpr('c');

Safe static regexes sregex re = REGEX("ab*c");

Template metaprogram

struct build_my_regex { static sregex run() { return /* … */; } }; sregex re = as_xpr('a') >> *as_xpr('b') >> as_xpr('c');

Safe static regexes sregex re = REGEX("ab*c");

Template metaprogram

struct build_my_regex { static sregex run() { return as_xpr('a') >> *as_xpr('b') >> as_xpr('c'); } }; sregex re = build_my_regex::run();

build_my_regex struct r_a

{

};

a

r_a

build_my_regex struct r_a

{

static

/* ... */

run() {

} };

a

r_a

build_my_regex struct r_a

{

static /* ... */ return as_xpr('a'); }

run() {

};

a

r_a

build_my_regex struct r_a

{

static decltype(as_xpr('a')) run() { return as_xpr('a'); } };

a

r_a

build_my_regex struct r_a

{

static decltype(as_xpr('a')) run() { return as_xpr('a'); } };

#define RUN(...) \ static decltype((__VA_ARGS__)) run() { \ return (__VA_ARGS__); \ }

a

r_a

build_my_regex struct r_a

{

RUN(as_xpr('a'

))

};

#define RUN(...) \ static decltype((__VA_ARGS__)) run() { \ return (__VA_ARGS__); \ }

a

r_a

build_my_regex struct r_a { typedef r_a RUN(as_xpr('a'

type; ))

};

#define RUN(...) \ static decltype((__VA_ARGS__)) run() { \ return (__VA_ARGS__); \ }

a

r_a

build_my_regex template struct r_char { typedef r_char type; RUN(as_xpr(C

))

};

#define RUN(...) \ static decltype((__VA_ARGS__)) run() { \ return (__VA_ARGS__); \ }

a

r_char<

'a' >

build_my_regex template struct r_char { typedef r_char type; RUN(as_xpr(C::value

))

};

#define RUN(...) \ static decltype((__VA_ARGS__)) run() { \ return (__VA_ARGS__); \ }

a

r_char>

build_my_regex template struct r_char { typedef r_char type; RUN(as_xpr(C::type::value)) };

#define RUN(...) \ static decltype((__VA_ARGS__)) run() { \ return (__VA_ARGS__); \ }

a

r_char>

build_my_regex template struct r_char { typedef r_char type; RUN(as_xpr(C::type::value)) };

ab a

r_char>

build_my_regex template struct r_char { typedef r_char type; RUN(as_xpr(C::type::value)) }; template struct r_concat { typedef r_concat type; };

ab a

r_char>

build_my_regex template struct r_char { typedef r_char type; RUN(as_xpr(C::type::value)) }; template struct r_concat { typedef r_concat type; };

ab

r_concat< r_char>, r_char> >

a

r_char>

build_my_regex template struct r_char { typedef r_char type; RUN(as_xpr(C::type::value)) }; template struct r_concat { typedef r_concat type; RUN( };

)

ab

r_concat< r_char>, r_char> >

a

r_char>

build_my_regex template struct r_char { typedef r_char type; RUN(as_xpr(C::type::value)) }; template struct r_concat { typedef r_concat type; RUN(A::run() };

)

ab

r_concat< r_char>, r_char> >

a

r_char>

build_my_regex template struct r_char { typedef r_char type; RUN(as_xpr(C::type::value)) }; template struct r_concat { typedef r_concat type; RUN(A::run() B::run() ) }; r_concat< r_char>, r_char> ab > a

r_char>

build_my_regex template struct r_char { typedef r_char type; RUN(as_xpr(C::type::value)) }; template struct r_concat { typedef r_concat type; RUN(A::run() >> B::run() ) }; r_concat< r_char>, r_char> ab > a

r_char>

build_my_regex template struct r_char { typedef r_char type; RUN(as_xpr(C::type::value)) }; template struct r_concat { typedef r_concat type; RUN(A::type::run() >> B::type::run()) }; r_concat< r_char>, r_char> ab > a

r_char>

build_my_regex template struct r_char { typedef r_char type; RUN(as_xpr(C::type::value)) }; template struct r_concat { typedef r_concat type; RUN(A::type::run() >> B::type::run()) abc }; r_concat< r_char>, r_char> ab > a

r_char>

build_my_regex template struct r_char { typedef r_char type; RUN(as_xpr(C::type::value)) r_concat< r_concat< r_char>, template r_char> struct r_concat { >, typedef r_concat type; r_char> RUN(A::type::run() >> B::type::run()) abc > }; };

ab

r_concat< r_char>, r_char> >

a

r_char>

build_my_regex r_concat::run()

ab

r_concat< r_char>, r_char> >

build_my_regex A::type::run()

r_concat::run() regex A

ab

B::type::run()

regex B

r_concat< r_char>, r_char> >

build_my_regex r_concat::run() regex A

regex B operator>>() regex AB

ab

r_concat< r_char>, r_char> >

build_my_regex r_concat::run() regex A

regex B operator>>() regex AB

ab

r_concat< r_char>, r_char> >

build_my_regex r_concat::run() regex A

regex B

regex AB

ab

r_concat< r_char>, r_char> >

build_my_regex r_concat::run() regex A

regex B regex AB deep_copy() regex A

regex B

regex AB

ab

r_concat< r_char>, r_char> >

build_my_regex r_concat::run()

regex A

regex B

regex AB

ab

r_concat< r_char>, r_char> >

build_my_regex template struct r_concat { typedef r_concat type; r_concat::run() RUN( A::type::run() >> B::type::run() ) };

regex A

regex B

regex AB

ab

r_concat< r_char>, r_char> >

build_my_regex template struct r_concat { typedef r_concat type; r_concat::run() RUN(deep_copy(A::type::run() >> B::type::run())) };

regex A

regex B

regex AB

ab

r_concat< r_char>, r_char> >

Lab 3 

Implement the types representing regular expressions 

r_empty: empty regular expression



r_dot: the . regular expression



r_star: the * regular expression



r_concat: the concatenation of two regular

expressions 

r_char: match one specific character

C++ template metafunction

Argument list

Name

Body

C++ template metafunction

template struct add_const { typedef const T type; };

Argument list

Name

Body

C++ template metafunction

template struct add_const { typedef const T type; };

Argument list

Name

Body

C++ template metafunction

template struct add_const { typedef const T type; };

Argument list

Name

Body

add_const::type

C++ template metafunction

template struct add_const { typedef const T type; };

Argument list

Name

Body

add_const::type

C++ template metafunction

template struct add_const { typedef const T type; };

template struct add_volatile { typedef volatile T type; };

C++ template metafunction

template struct add_const { typedef const T type; };

template struct add_volatile { typedef volatile T type; };

template struct add_cv {

};

C++ template metafunction

template struct add_const { typedef const T type; };

template struct add_volatile { typedef volatile T type; };

template struct add_cv { typedef typename add_const::type type; };

C++ template metafunction

template struct add_const { typedef const T type; };

template struct add_volatile { typedef volatile T type; };

template struct add_cv { typedef typename add_volatile< typename add_const::type >::type type; };

C++ template metafunction

template struct add_const { typedef const T type; };

template struct add_volatile { typedef volatile T type; };

template struct add_cv : add_volatile< typename add_const::type > { typedef type; };

C++ template metafunction

template struct add_const { typedef const T type; };

template struct add_volatile { typedef volatile T type; };

template struct add_cv : add_volatile< typename add_const::type > {};

Lab 4 

Write a template metafunction called beginning_and_end 







It has one argument (which is expected to be a string) Returns a pair of characters: the first and the last character of the string Eg. "Hello" → ('h', 'o')

Make use of Boost.MPL 

boost::mpl::pair



boost::mpl::front



boost::mpl::back

Higher order functions

template struct add_const { typedef const T type; };

add_const::type

Higher order functions struct add_const { template struct add_const { typedef const T type; }; };

add_const::add_const::type

Higher order functions struct add_const { template struct apply { typedef const T type; }; };

add_const::apply::type

Template metafunction class struct add_const { template struct apply { typedef const T type; }; };

Argument list

Name

Body

add_const::apply::type

Template metafunction class struct add_const { template struct apply { typedef const T type; }; };

Argument list

Name

Body

add_const::apply::type

Template metafunction class struct add_const { template struct apply { typedef const T type; }; typedef add_const type; };

add_const::apply::type

Argument list

Name

Body

Lab 5 

Turn beginning_and_end into a template metafunction class

The grammar 

We will support 

letters and numbers (eg. abc123)



.



*



brackets (eg. (abc)*)

The grammar 

We will support 

letters and numbers (eg. abc123)



.



*



brackets (eg. (abc)*) reg_exp unary_item item any bracket_exp char_ number letter

::= ::= ::= ::= ::= ::= ::= ::=

unary_item* item '*'? any | bracket_exp | char_ '.' '(' reg_exp ')' number | letter '0'..'9' 'a'..'z' | 'A'..'Z'

Parsing regular expressions

abc

regex parser

r_concat< r_concat< r_char>, r_char> >, r_char> >

Parsing regular expressions

abc

regex parser

*

regex parser

r_concat< r_concat< r_char>, r_char> >, r_char> >

error< invalid_regular_expression >

letter ::= 'a'..'z' | 'A'..'Z' abc

letter

letter

letter ::= 'a'..'z' | 'A'..'Z' abc

letter mpl::char_<'a'>

letter

letter ::= 'a'..'z' | 'A'..'Z' abc

bc

letter mpl::char_<'a'>

letter

number ::= '0'..'9' letter ::= 'a'..'z' | 'A'..'Z' abc

bc

letter mpl::char_<'a'>

mpl::char_<'1'>

123

digit

23

letter

digit

char_ ::= number | letter number ::= '0'..'9' letter ::= 'a'..'z' | 'A'..'Z' abc

bc

letter mpl::char_<'a'>

mpl::char_<'1'>

123

digit

23

one_of

one_of

char_ ::= number | letter number ::= '0'..'9' letter ::= 'a'..'z' | 'A'..'Z' abc

bc

letter mpl::char_<'a'>

mpl::char_<'1'>

123

digit

r_char< mpl::char_<'a'> > r_char< mpl::char_<'1'> > 23

one_of

one_of

char_ ::= number | letter number ::= '0'..'9' letter ::= 'a'..'z' | 'A'..'Z' abc

bc

letter

r_char< mpl::char_<'a'> >

mpl::char_<'a'> build_char_ mpl::char_<'1'>

123

digit

r_char< mpl::char_<'1'> > 23

one_of struct build_char_ { typedef build_char_ type; template struct apply : r_char {}; one_of };

char_ ::= number | letter number ::= '0'..'9' letter ::= 'a'..'z' | 'A'..'Z' abc

bc

letter

r_char< mpl::char_<'a'> >

mpl::char_<'a'> build_char_ mpl::char_<'1'>

123

digit

r_char< mpl::char_<'1'> > 23

one_of transform

transform, build_char_>

char_ ::= number | letter number ::= '0'..'9' letter ::= 'a'..'z' | 'A'..'Z' bc r_char< mpl::char_<'a'> >

abc

char_

123

r_char< mpl::char_<'1'> > 23

typedef transform, build_char_> char_;

The grammar 

We will support 

letters and numbers (eg. abc123)



.



*



brackets (eg. (abc)*) reg_exp unary_item item any bracket_exp char_ number letter

::= ::= ::= ::= ::= ::= ::= ::=

unary_item* item '*'? any | bracket_exp | char_ '.' '(' reg_exp ')' number | letter '0'..'9' 'a'..'z' | 'A'..'Z'

The grammar 

We will support 

letters and numbers (eg. abc123)



.



*



brackets (eg. (abc)*) reg_exp unary_item item any bracket_exp char_ number letter

::= ::= ::= ::= ::= ::= ::= ::=

unary_item* item '*'? any | bracket_exp | char_ '.' '(' reg_exp ')' number | letter '0'..'9' 'a'..'z' | 'A'..'Z'

The grammar 

We will support

typedef one_of item;



letters and numbers (eg. abc123)



.



*



brackets (eg. (abc)*) reg_exp unary_item item any bracket_exp char_ number letter

::= ::= ::= ::= ::= ::= ::= ::=

unary_item* item '*'? any | bracket_exp | char_ '.' '(' reg_exp ')' number | letter '0'..'9' 'a'..'z' | 'A'..'Z'

unary_item ::= item '*'? mpl::char_<'*'> *c

lit_c<'*'>

lit_c<'*'>

c

unary_item ::= item '*'? mpl::char_<'*'> *c

lit_c<'*'>

bc

lit_c<'*'>

c

unary_item ::= item '*'? mpl::char_<'*'> *c

lit_c<'*'>

c

bc

return_< mpl::char_<'x'> >

bc

lit_c<'*'>

return_>

unary_item ::= item '*'? mpl::char_<'*'> *c

lit_c<'*'>

c

bc

return_< mpl::char_<'x'> >

bc

mpl::char_<'x'>

lit_c<'*'>

return_>

unary_item ::= item '*'? mpl::char_<'*'> *c

bc

c

lit_c<'*'> return_< mpl::char_<'x'> > one_of

bc

mpl::char_<'x'>

one_of, return_>>

unary_item ::= item '*'? mpl::char_<'*'> b*c

abc

*c

bc

c

lit_c<'*'> return_< mpl::char_<'x'> > one_of

bc

mpl::char_<'x'>

one_of, return_>>

unary_item ::= item '*'? r_char> b*c

*c item bc

abc

c

lit_c<'*'> return_< mpl::char_<'x'> > one_of

r_char>

item

mpl::char_<'*'>

bc

mpl::char_<'x'>

one_of, return_>>

unary_item ::= item '*'? mpl::vector< r_char> , mpl::char_<'*'> > b*c

*c item

abc

bc

lit_c<'*'> return_< mpl::char_<'x'> > one_of

c

bc

sequence mpl::vector< r_char> , mpl::char_<'x'> >

sequence, return_>>>

unary_item ::= item '*'? r_star>> mpl::vector< r_char> , mpl::char_<'*'> > b*c

*c item

abc

bc

lit_c<'*'> return_< mpl::char_<'x'> > one_of

c

bc

sequence mpl::vector< r_char> , mpl::char_<'x'> > r_char> sequence, return_>>>

unary_item ::= item '*'? r_star>> mpl::vector< r_char> template , mpl::char_<'*'> > struct impl; b*c

*c item

abc

bc

lit_c<'*'> return_< mpl::char_<'x'> > one_of

c

bc

sequence mpl::vector< r_char> , mpl::char_<'x'> > r_char> sequence, return_>>>

unary_item ::= item '*'? r_star>> mpl::vector< r_char> template , mpl::char_<'*'> > struct impl; b*c template *c lit_c<'*'> struct impl : r_star {}; item return_< mpl::char_<'x'> bc > abc one_of

c

bc

sequence mpl::vector< r_char> , mpl::char_<'x'> > r_char> sequence, return_>>>

unary_item ::= item '*'? r_star>> mpl::vector< r_char> template , mpl::char_<'*'> > struct impl; b*c template *c lit_c<'*'> struct impl : r_star {}; item return_< template mpl::char_<'x'> bc 'x'> : >RegExp {}; abc struct impl
c

bc

sequence mpl::vector< r_char> , mpl::char_<'x'> > r_char> sequence, return_>>>

unary_item ::= item '*'? r_star>> mpl::vector< r_char> template , mpl::char_<'*'> > struct impl; b*c template *c c lit_c<'*'> struct impl : r_star {}; item return_< template mpl::char_<'x'> bc 'x'> : >RegExp {}; abc struct impl sequence struct apply : impl, back::type::value> {}; mpl::vector< r_char> , mpl::char_<'x'> > }; r_char> sequence, return_>>>

unary_item ::= item '*'? r_star>> mpl::vector< r_char> , mpl::char_<'*'> > b*c

*c item

abc

bc

lit_c<'*'> return_< mpl::char_<'x'> > one_of

c

bc

sequence mpl::vector< r_char> , mpl::char_<'x'> > transform r_char> transform< sequence, return_>>>, build_unary_item >

unary_item ::= item '*'? r_star>>

b*c

c

unary_item abc

bc

r_char> typedef transform< sequence, return_>>>, build_unary_item > unary_item;

The grammar 

We will support 

letters and numbers (eg. abc123)



.



*



brackets (eg. (abc)*) reg_exp unary_item item any bracket_exp char_ number letter

::= ::= ::= ::= ::= ::= ::= ::=

unary_item* item '*'? any | bracket_exp | char_ '.' '(' reg_exp ')' number | letter '0'..'9' 'a'..'z' | 'A'..'Z'

reg_exp ::= unary_item*

abc

reg_exp ::= unary_item*

r_char>

abc

unary_item

bc

unary_item

reg_exp ::= unary_item*

r_char> r_char>

abc

unary_item

bc

unary_item

unary_item

c

reg_exp ::= unary_item*

r_char> r_char> r_char>

abc

unary_item

bc

unary_item

unary_item

c

unary_item

reg_exp ::= unary_item* r_concat< r_concat< r_concat< r_empty, r_char> >, r_char> >, r_char> >

abc

unary_item

bc

unary_item

unary_item

c

unary_item

reg_exp ::= unary_item*

r_empty

abc foldl

foldl

reg_exp ::= unary_item*

r_empty r_char>

abc

unary_item

bc foldl

foldl

reg_exp ::= unary_item* r_concat< r_empty, r_char> >

abc

unary_item

bc foldl

foldl

reg_exp ::= unary_item* r_concat< r_empty, r_char> > r_char>

abc

unary_item

bc

unary_item

c

foldl

foldl

reg_exp ::= unary_item* r_concat< r_concat< r_empty, r_char> >, r_char> >

abc

unary_item

bc

unary_item

c

foldl

foldl

reg_exp ::= unary_item* r_concat< r_concat< r_empty, r_char> >, r_char> > r_char>

abc

unary_item

bc

unary_item

c

unary_item

foldl

foldl

reg_exp ::= unary_item* r_concat< r_concat< r_concat< r_empty, r_char> >, r_char> >, r_char> >

abc

unary_item

bc

unary_item

c

unary_item

foldl

foldl

reg_exp ::= unary_item* r_concat< r_concat< r_concat< r_empty, r_char> >, r_char> >, r_char> >

abc

reg_exp

typedef foldl reg_exp;

The grammar 

We will support 

letters and numbers (eg. abc123)



.



*



brackets (eg. (abc)*) reg_exp unary_item item any bracket_exp char_ number letter

::= ::= ::= ::= ::= ::= ::= ::=

unary_item* item '*'? any | bracket_exp | char_ '.' '(' reg_exp ')' number | letter '0'..'9' 'a'..'z' | 'A'..'Z'

The grammar 

We will support 

letters and numbers (eg. abc123)



.



*



brackets (eg. (abc)*) reg_exp unary_item item any bracket_exp char_ number letter

::= ::= ::= ::= ::= ::= ::= ::=

unary_item* item '*'? any | bracket_exp | char_ '.' '(' reg_exp ')' number | letter '0'..'9' 'a'..'z' | 'A'..'Z'

The grammar 

We will support 

letters and numbers (eg. abc123)



.



*



brackets (eg. (abc)*) reg_exp unary_item item any bracket_exp char_ number letter

::= ::= ::= ::= ::= ::= ::= ::=

unary_item* item '*'? any | bracket_exp | char_ '.' '(' reg_exp ')' number | letter '0'..'9' 'a'..'z' | 'A'..'Z'

The grammar 

We will support 

letters and numbers (eg. abc123)



.



*



brackets (eg. (abc)*) reg_exp unary_item item any bracket_exp char_ number letter

::= ::= ::= ::= ::= ::= ::= ::=

unary_item* item '*'? any | bracket_exp | char_ '.' '(' reg_exp ')' number | letter '0'..'9' 'a'..'z' | 'A'..'Z'

The grammar 

We will support 

letters and numbers (eg. abc123)



.



*



brackets (eg. (abc)*) reg_exp unary_item item any bracket_exp char_ number letter

::= ::= ::= ::= ::= ::= ::= ::=

unary_item* item '*'? any | bracket_exp | char_ '.' '(' reg_exp ')' number | letter '0'..'9' 'a'..'z' | 'A'..'Z'

bracket_exp ::= '(' reg_exp ')'

// … typedef bracket_exp; // … typedef foldl reg_exp;

bracket_exp ::= '(' reg_exp ')'

// … typedef bracket_exp; // … typedef foldl reg_exp;

struct reg_exp : foldl {};

bracket_exp ::= '(' reg_exp ')' struct reg_exp; // … typedef bracket_exp; // … typedef foldl reg_exp;

struct reg_exp : foldl {};

bracket_exp ::= '(' reg_exp ')' struct reg_exp; // … typedef

reg_exp

bracket_exp; // … typedef foldl reg_exp;

struct reg_exp : foldl {};

bracket_exp ::= '(' reg_exp ')' struct reg_exp; // … typedef

lit_c<'('> bracket_exp;

reg_exp

lit_c<')'>

// … typedef foldl reg_exp;

struct reg_exp : foldl {};

bracket_exp ::= '(' reg_exp ')' struct reg_exp; // … typedef middle_of, reg_exp, lit_c<')'>> bracket_exp; // … typedef foldl reg_exp;

struct reg_exp : foldl {};

The grammar 

We will support 

letters and numbers (eg. abc123)



.



*



brackets (eg. (abc)*) reg_exp unary_item item any bracket_exp char_ number letter

::= ::= ::= ::= ::= ::= ::= ::=

unary_item* item '*'? any | bracket_exp | char_ '.' '(' reg_exp ')' number | letter '0'..'9' 'a'..'z' | 'A'..'Z'

Parsing regular expressions

abc

*

reg_exp

reg_exp

r_concat< r_concat< r_char>, r_char> >, r_char> >

error< invalid_regular_expression > reg_exp

Parsing regular expressions

abc

*

reg_exp

reg_exp build_parser

r_concat< r_concat< r_char>, r_char> >, r_char> >

error< invalid_regular_expression >

typedef build_parser reg_exp_parser;

Using the parser sregex re = regex_parser::apply::type::run();

Using the parser sregex re = regex_parser::apply::type::run();

#define REGEX(s) \ (regex_parser::apply::type::run())

Using the parser sregex re = REGEX("abc");

#define REGEX(s) \ (regex_parser::apply::type::run())

Lab 6 

Build the regular expression parser

Summary 



DSL embedding into C++ 

Early validation and error reporting



Efficient implementation



Keeping the common syntax

Improving the interface of existing libraries

Q&A

Mpllibs.Metaparse http://abel.web.elte.hu/mpllibs https://github.com/sabel83/metaparse_tutorial

Metaparse tutorial - GitHub

"a.*a" int main(). { string s; cin << s; sregex r = sregex::compile("a.*a"); smatch w; regex_search(s, w, r);. // ... } main.cpp developer ...

1MB Sizes 9 Downloads 289 Views

Recommend Documents

DSQSS Tutorial 2015.12.01 - GitHub
Dec 1, 2015 - Step1 :Choose a site and an imaginary time point. Step2 :Put a worm pair. if no, go to Step4. Step3 :The worm-head moving. When the head ...

Epic Vim Tutorial - GitHub
Jan 19, 2012 - Move back to the start of the first email address in the file. 7. Press Ctrl+v to enter visual block mode. 8. Using the arrow keys, select all email ...

Tutorial OpenPIV - GitHub
Sep 6, 2012 - ... is an open source Particle Image Velocimetry (PIV) analysis software ... the visualization of the vectors but it has to be taken into account ...

Custom Skin Tutorial - GitHub
dashboard.html – defines all dashboard elements supported by the skin .... There are two ways to display numbers: as a string value or as an analog meter.

BamTools API Tutorial - GitHub
Mar 23, 2011 - https://github.com/pezmaster31/bamtools/wiki/BamTools-1x_PortingGuide.pdf ... adjust how your app locates the shared library at runtime.

GNU gdb Tutorial - GitHub
The apropos command can be used to find commands. 3. Basic Debugging .... exist in your program; they are assigned by GDB to give you a way of designating ...

Vulkan Tutorial - GitHub
cross-platform and allows you to develop for Windows, Linux and Android at ..... to be described explicitly, there is no default color blend state, for example. 10 ...

WiFiMCU Tutorial - GitHub
2, Quickly Start with WiFiMCU STUDIO ................................................................................. 3 .... 2 Breathing LED -use PWM module . .... Content-Type:text/html. 7.

Cryptography Tutorial Contents - GitHub
In Erlang to encode some data we might do something like: Bin1 = encrypt(Bin, SymKey), ...... Trying all the small strings on your hard disk to see if they are passwords. • Analyzing the swap .... http://cr.yp.to/highspeed/ · coolnacl-20120725.pdf.

Tutorial for Overture/VDM++ - GitHub
Sep 6, 2015 - Overture Technical Report Series. No. TR-004. September ... Year Version Version of Overture.exe. January. 2010. 0.1.5 ... Contents. 3 Overture Tool Support for VDM++. 1. 3.1 Introduction . .... classes and their usage in class diagrams

Vowpal Wabbit 7 Tutorial - GitHub
Weight 1 by default. – Label: use {-1,1} for classification, or any real value for regression. 1 | 1:0.43 5:2.1 10:0.1. -1 | I went to school. 10 | race=white sex=male ...

Normalized Online Learning Tutorial - GitHub
Normalized Online Learning Tutorial. Paul Mineiro joint work with Stephane Ross & John Langford. December 9th, 2013. Paul Mineiro. Normalized Online ...

D Templates: A Tutorial - GitHub
In the next chapters, you'll see how to define function, struct and class templates. ...... can find on the D Programming Language website, but they act in a natural ...... opDispatch is a sort of operator overloading (it's in the same place in the o

Problem Tutorial: “Apples” - GitHub
careful when finding x, cause the multiplication might not fit in the limits of long long. Also don't the forget the case when there's no answer. Page 1 of 1.

MeqTrees Batch Mode: A Short Tutorial - GitHub
tdlconf.profiles is where you save/load options using the buttons at ... Section is the profile name you supply ... around the Python interface (~170 lines of code).

Tutorial for Overture/VDM-SL - GitHub
2010 1. 0.2. February. 2011 2. 1.0.0. April. 2013 3. 2.0.0. September 2015 4 ..... Figure 3.13: The generated pdf file with test coverage information .... the Overture documentation at http://overturetool.org/documentation/manuals.html for the.

Notes and Tutorial on GDB - GitHub
CSM Linux Users Group ... GDB can make use of special symbols in your program to help you debug. ... exists and is up to date, and if so, call the debugger.

Problem Tutorial: “The queue” - GitHub
You need to optimize that solution even more using some data structure. In fact ... problem with 2 types of query (if you imagine that you have a very big array) :.

PIA – Protein Inference Algorithms Tutorial - GitHub
Sep 23, 2016 - install the "KNIME Analytics Platform + all free extensions", which comes with ... is used to pass spectrum data to PIA, which can later be used to ...

IAP 2013 Julia Tutorial Schedule - GitHub
Jan 15, 2013 - Punctuation Review. () Parentheses: Function Calls. Required! quit(),tic(),toc(),help() ... julia –machinefile file #hosts in file. • addprocs_local(5) ...

Tutorial for Overture/VDM-RT - GitHub
Tutorial to Overture/VDM-RT. Document history. Month. Year Version Version of Overture.exe. January. 2010. 0.1.5. March. 2010. 0.2. May. 2010 1. 0.2. February.

Tutorial introducing the R package TransPhylo - GitHub
Jan 16, 2017 - disease transmission using genomic data. The input is a dated phylogeny, ... In the second part we will analyse the dataset simulated in the first ...

Gmail - different record counts in tutorial #3 - GitHub
use all of the individuals in the sample for data analysis for personal research, ... Though we use different programs in the tutorials (some Excel, some SAS), your ...

A tutorial on clonal ordering and visualization using ClonEvol - GitHub
Aug 18, 2017 - It uses the clustering of heterozygous variants identified using other tools as input to infer consensus clonal evolution trees and estimate the cancer cell ... This results in a bootstrap estimate of the sampling distribution of the C