|
Boost : |
From: shiwei xu (xushiweizh_at_[hidden])
Date: 2008-05-28 21:43:22
TPL is short for "Text Processing Library". It is a spirit/xpressive like
library.
I used spirit/xpressive. But the codes based on spirit or xpressive was
compiled slowly.
Here are some hints of TPL:
* It is simple.
* It will always be compiled fast.
* It is based on Boost-Memory (still is a sandbox library).
Here is an example of TPL:
#include <stack>
#include <tpl/RegExp.h>
#include <tpl/ext/Calculator.h>
#include <cmath>
using namespace tpl;
void calculate2()
{
typedef SimpleImplementation impl;
// ---- define rules ----
impl::Allocator alloc;
std::stack<double> stk;
impl::Grammar::Var rFactor;
impl::Grammar rMul( alloc, '*' + rFactor/calc<std::multiplies>(stk) );
impl::Grammar rDiv( alloc, '/' + rFactor/calc<std::divides>(stk) );
impl::Grammar rTerm( alloc, rFactor + *(rMul | rDiv) );
impl::Grammar rAdd( alloc, '+' + rTerm/calc<std::plus>(stk) );
impl::Grammar rSub( alloc, '-' + rTerm/calc<std::minus>(stk) );
impl::Grammar rExpr( alloc, rTerm + *(rAdd | rSub) );
impl::Rule rFun( alloc, "sin"/calc(stk, sin) | "cos"/calc(stk, cos) |
"pow"/calc(stk, pow) );
rFactor.assign( alloc,
real()/assign(stk) |
'-' + rFactor/calc<std::negate>(stk) |
'(' + rExpr + ')' |
(gr(c_symbol()) + '(' + rExpr % ',' + ')')/(gr(rFun) + '(') |
'+' + rFactor );
// ---- do match ----
for (;;)
{
std::string strExp;
std::cout << "input an expression (q to quit): ";
if (!std::getline(std::cin, strExp) || strExp == "q") {
std::cout << '\n';
break;
}
try {
while ( !stk.empty() )
stk.pop();
if ( !impl::match(strExp.c_str(), rExpr + eos(), skipws(),
alloc) )
std::cout << ">>> ERROR: invalid expression!\n";
else
std::cout << stk.top() << '\n';
}
catch (const std::logic_error& e) {
std::cout << ">>> ERROR: " << e.what() << '\n';
}
}
}
// -------------------------------------------------------------------------
If you are interested in TPL, you can check out its source code from SVN:
svn checkout http://libtpl.googlecode.com/svn/trunk/
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk