c++ - 转换为 token_def 的值类型

标签 c++ boost boost-spirit boost-spirit-qi boost-spirit-lex

当我在词法分析器中定义时

typedef boost::mpl::vector<std::string, unsigned int, bool>
            token_value_types;
lex::token_def<unsigned int> lit_uint("[0-9]+", token_ids::lit_uint);

然后在某些语法中使用它

primary_expr =
        lexer.lit_uint
    |   lexer.true_or_false
    |   identifier
    |   '(' > expr > ')'
    ;

那么如何将字符串转换为正确的 token 值类型的值(在本例中为unsigned int)?如果您指定自定义类型或浮点类型作为 token 值类型,会发生什么?转换例程在哪里(我认为类似于 boost::iterator_rangedouble 转换)?

最佳答案

完成你想要的事情的方法是专业assign_to_attribute_from_iterators 。您可以找到自定义类型 here 的示例。如果您使用 double 作为标记定义中的属性,spirit 会在内部使用 qi::double_ 来解析该值。 (您可以找到 here double 和其余基本类型的特化)。

愚蠢的示例,我将 real 标记定义为 ; 之外的任何内容,以显示 的解析双s。

#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/lex_lexertl.hpp>
#include <boost/spirit/include/qi.hpp>

namespace lex = boost::spirit::lex;
namespace qi = boost::spirit::qi;
namespace mpl = boost::mpl;


template <typename Lexer>
struct my_lexer : lex::lexer<Lexer>
{
    my_lexer()
    {
        real = "[^,;]*"; //anything that is not a , or ; is a real number

        this->self=lex::token_def<lex::omit>(',')| ';';

        this->self.add(real);
    }
    lex::token_def<double> real;
};


int main()
{
    // the token type needs to know the iterator type of the underlying
    // input and the set of used token value types
    typedef lex::lexertl::token<std::string::iterator,
        mpl::vector<double> > token_type;

    // use actor_lexer<> here if your token definitions have semantic
    // actions
    typedef lex::lexertl::lexer<token_type> lexer_type;

    // this is the iterator exposed by the lexer, we use this for parsing
    typedef lexer_type::iterator_type iterator_type;

    // create a lexer instance
    std::string input("3.4,2,.4,4.,infinity,NaN,-3.8,1e2,1.5E3;");
    std::string::iterator s = input.begin();

    my_lexer<lexer_type> lex;
    iterator_type b = lex.begin(s, input.end());

    // use the embedded token_def as a parser, it exposes its token value type
    // as its parser attribute type
    std::vector<double> result;
    qi::rule<iterator_type,double()> number= lex.real;
    qi::rule<iterator_type,std::vector<double>()> sequence= number >> *(',' >> number) >> ';';
    BOOST_SPIRIT_DEBUG_NODE(number);
    BOOST_SPIRIT_DEBUG_NODE(sequence);
    if (!qi::parse(b, lex.end(), sequence, result))
    {
        std::cerr << "Parsing failed!" << std::endl;
        return -1;
    }

    std::cout << "Parsing succeeded:"  << std::endl;
    for(auto& n : result)
        std::cout << n << std::endl;
    return 0;
}

编辑:我对正则表达式的经验很少,但我相信标记定义相当于注释中链接的语法(我认为应该有 fractional_constant >> -exponent_part 而不是 fractional_constant >> !exponent_part) 将是:

template <typename Lexer>
struct my_lexer : lex::lexer<Lexer>
{
    my_lexer()
    {
        this->self.add_pattern("SIGN","[\\+\\-]");
        this->self.add_pattern("NAN","(1\\.0#)?(?i:nan)(\\([^\\)]\\))?");
        this->self.add_pattern("INF","(?i:inf(inity)?)");
        this->self.add_pattern("DIGIT","[0-9]");
        this->self.add_pattern("FRACT_CONST","{DIGIT}*\\.{DIGIT}+|{DIGIT}+\\.?");
        this->self.add_pattern("EXP","[eE]{SIGN}?{DIGIT}+");

        real = "{SIGN}?({NAN}|{INF}|{FRACT_CONST}{EXP}?|{DIGIT}+{EXP})";

        this->self=lex::token_def<lex::omit>(',')| ';';

        this->self.add(real);
    }
    lex::token_def<double> real;
};

关于c++ - 转换为 token_def 的值类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14291865/

相关文章:

c# - 管道未正确读取数据

c++ - Boost spirit解析器如何打印符号表匹配的变量?

c++ - 是否可以自定义 Visual Studio 自动套用格式?

boost - 交叉编译 boost 的问题

c++ - 使用 boost strand 和 std::mutex

boost - 在哪里可以找到 bcp for boost?

c++ - boost 灵气 : binding to struct with vector of tuples

c++ - Boost.Spirit、Nabialek 技巧和错误处理

c++ - "u8"未在此范围内声明

c++ - 从带空格的 std::stringstream 中提取流