0

I need to write lexical analyzer with ability to parse tokens like x(t-1), u(t), u(t-4), a0, a1,... and attributes of this lexemes should be "unsigned" (as example attribute value for token x(t-2) should be 2). I can define all this tokens via regular expressions, but i don't know how i can extract attribute value from matched string.

P.S. This lexer will be used in boost spirit qi grammar.

So, does anybody know way how i can do this?

Anton
  • 575
  • 2
  • 7
  • 27
  • Yes. Somebody knows how to do it. If you could please be a little less vague we might even show you how. This is not a code-writing site, as you know. Perhaps a [SSCCE](http://meta.stackexchange.com/questions/22754/sscce-how-to-provide-examples-for-programming-questions/22762#22762) will help. As well as a good reason to use Lex here. – sehe Feb 07 '13 at 00:34

1 Answers1

1
#define BOOST_SPIRIT_USE_PHOENIX_V3

#include <boost/phoenix.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/lex_lexertl.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
...
namespace qi = ::boost::spirit::qi;
namespace mpl = ::boost::mpl;
namespace lex = ::boost::spirit::lex;
...
struct extract_func
{
    template <typename Iterator> struct result
    {
        typedef unsigned type;
    };

    template <typename Iterator> typename result<Iterator>::type operator()(Iterator& begin, Iterator& end) const
    {
        ::std::string n(begin, end);
        ::boost::trim_if(n, !::boost::is_digit());
        return n.empty()
                ? 0U
                : ::boost::lexical_cast<unsigned>(n);
    }
};

const ::boost::phoenix::function<extract_func> EXTRACT;

template <typename L>
struct DynamicExpressionLexer : lex::lexer<L>
{
    lex::token_def<unsigned> OBJECT_USAGE;
    ...

    lex::token_def<lex::omit> WS;

    DynamicExpressionLexer() :
        OBJECT_USAGE("x\\ *\\(\\ *t\\ *-\\ *[0-9]+\\ *\\)"),
        ...
        WS("[ \\t]+")
    {
        this->self
                = OBJECT_USAGE[lex::_val = EXTRACT(lex::_start, lex::_end)]
                | ...;

        this->self("WS") = WS;
    }
};
Anton
  • 575
  • 2
  • 7
  • 27