如何更改规则的解析器 [英] how to change the parser of a rule

查看:97
本文介绍了如何更改规则的解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在运行时修改规则的解析器?我能够动态创建解析器(派生自parser_base的类),但是我不知道如何将新的解析器分配给现有规则.

基本上,我的问题是我想为由数字(如在我的所有解析器输入中不变的意义上称它们为常量)和符号(我想要的变体)这样的元素组成的行定义一个解析器使用动态解析器​​方法捕获).由于这些符号基于运行时的当前环境,因此我认为我需要一个动态解析器​​.

最小的问题,我想要符号和行尾:

namespace parser
{
    x3::rule<class line, ast::line> line = "line";
    auto const line_def = variants_def >> lineend_def;
    BOOST_SPIRIT_DEFINE(line);
}

using line_type = boost::spirit::x3::rule<class client::parser::line, ast::line>;
line_type line(boost::spirit::x3::plus<boost::spirit::x3::symbols_parser<boost::spirit::char_encoding::standard, client::ast::command, boost::spirit::x3::tst<boost::spirit::char_encoding::standard::char_type, client::ast::command>>> symbols_parser)
{
    auto line_end = lineend();
    auto const line_def2 = symbols_parser >> line_end;
    return parser::line; // <-- how can I change the line_type to use line_def2?
}

此代码可能远非最少,但不幸的是,我还不太熟悉C ++.

解决方案

您不能.

实际上,我撒谎了,您可以在动态库中拥有具有相同id的不同解析器,并通过来回切换动态库来更改规则的解析器,但是请不要这样做.

Is it possible to modify the parser of a rule at runtime? I am able to dynamically create parsers (classes that are derived from parser_base), but I dont know how I can assign the new parser to an existing rule.

Basically my problem is that I want to define a parser for a line that consists of elements like numbers (lets call them constants in the sense that they are invariant over all my parser input) and symbols (the variants that I want to capture with a dynamic parser approach). Since the symbols are based on the current environment at runtime I think I need a dynamic parser.

minimalistic issue, I want symbols and a line-end:

namespace parser
{
    x3::rule<class line, ast::line> line = "line";
    auto const line_def = variants_def >> lineend_def;
    BOOST_SPIRIT_DEFINE(line);
}

using line_type = boost::spirit::x3::rule<class client::parser::line, ast::line>;
line_type line(boost::spirit::x3::plus<boost::spirit::x3::symbols_parser<boost::spirit::char_encoding::standard, client::ast::command, boost::spirit::x3::tst<boost::spirit::char_encoding::standard::char_type, client::ast::command>>> symbols_parser)
{
    auto line_end = lineend();
    auto const line_def2 = symbols_parser >> line_end;
    return parser::line; // <-- how can I change the line_type to use line_def2?
}

This code might be far from minimal, but unfortunately Im not so familiar with C++ yet.

解决方案

You cannot.

Actually, I lied, you can have different parsers with the same id in dynamic libraries and change the rule's parser by switching dynamic libraries back and forth, but please, do not do this.

这篇关于如何更改规则的解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆