Professional Documents
Culture Documents
Xpressive
Xpressive
Xpressive
Mini-languages, everywhere!
– GNU Make
– Backus Naur Form
– Regular expressions
Syntax, constructs and abstractions for
working efficiently in some narrow domain
Not general purpose!
A DSL in a library
All the wholesome goodness of a DSL in the
host language of your choice!
C++ is a good host language
– operator overloading
– low abstraction penalty
group =
::=
'('
'('>>expr
expr')'
>> ')';
fact =::=
integer
integer| group;
| group
term =::=
fact
fact>>(('*'
*(('*'fact)
>> fact)
| ('/'
| fact))*
('/' >>
fact));
expr ::= term (('+' term) | ('-' term))*
expr = term >> *(('+' >> term) | ('-' >>
term));
Example:
SQLCommand c = "SELECT * from Employees";
Advantages:
– Unconstrained syntax
– Statements can be specified at runtime
Disadvantages:
– Syntax errors discovered at runtime
– Performance costs of interpretation
Example:
double d = (matrix * vector)(3, 4);
Advantages:
– Syntax errors checked at compile-time
– Aggressive inlining, domain-specific codegen
Disadvantages:
– Constrained by rules for legal C++ expressions
– Cannot accept new statements at runtime
Static
DSELs...
Can’t I have it
both ways?
Dynamic
DSELs...
"\\w" _w
"\\w+" +_w
"a\\w" 'a' >> _w
"a|b" as_xpr('a') | 'b'
"(\\w)\\1" (s1= _w) >> s1
"[^a-z]" ~range('a', 'z')
"(?=foo)" before("foo")
inline cons<Car,Cdr>
make_cons(Car const & a, Cdr const & d)
{ return cons(a, d); }
Copyright 2005 Eric Niebler
Simple Fusion-esque List, cont.
template<typename F>
void for_each(nil, F) {}
std::pair<int, std::pair<int,
??? *> ???*> *>
std::pair<int, ???*> *> *>
std::pair<int,
std::pair<int,
std::pair<int,
std::pair<int,
std::pair<int, Copyright 2005 Eric Niebler
Dual-Mode DSEL Design Strategy
recursive algorithms
one modular core
two binding policies: static and dynamic
acyclic data structures
xpressive:
– http://boost-sandbox.sf.net/libs/xpressive
Spirit and Fusion
– by Joel de Guzman
– http://spirit.sf.net