0

x(t-1)、u(t)、u(t-4)、a0、a1 などのトークンを解析する機能を備えた語彙アナライザーを作成する必要があり、この語彙素の属性は「符号なし」である必要があります (たとえば、トークン x(t-2) の属性値は 2 にする必要があります)。このすべてのトークンを正規表現で定義できますが、一致した文字列から属性値を抽出する方法がわかりません。

PS この字句解析器は、ブースト精神気文法で使用されます。

それで、私がこれを行う方法を知っている人はいますか?

4

1 に答える 1

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;
    }
};
于 2013-02-22T10:30:26.323 に答える