我需要编写具有语法分析功能的词法分析器,例如x(t-1),u(t),u(t-4),a0,a1 ...,并且此词素的属性应为“无符号”(如令牌x(t-2)的示例属性值应为2)。我可以通过正则表达式定义所有这些标记,但是我不知道如何从匹配的字符串中提取属性值。

附言该词法分析器将用于增强气韵语法。

那么,有人知道我该怎么做吗?

最佳答案

#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;
    }
};

关于c++ - 如何从匹配的字符串中提取无符号值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14732982/

10-11 17:59