3 ms·
For a good use of operator overloading, check out Boost.Spirit, which implements EBNF grammars directly in C++: http://spirit.sourceforge.net/ http://spirit.sou
by scott_s 18y ago
For a good use of operator overloading, check out Boost.Spirit, which implements EBNF grammars directly in C++: http://spirit.sourceforge.net/ http://spirit.sourceforge.net/
An example, from the user's guide:
struct calculator : public grammar<calculator> {
template <typename ScannerT>
struct definition {
definition(calculator const& self)
{
group = '(' >> expression >> ')';
factor = integer | group;
term = factor >> *(('*' >> factor) | ('/' >> factor));
expression = term >> *(('+' >> term) | ('-' >> term));
}
rule<ScannerT> expression, term, factor, group;
rule<ScannerT> const& start() const
{
return expression;
}
};
};
This is nowhere near as concise as it would be in, say, OCaml, but if you're already using C++, it's useful.