c++17 - 将 BOOST Spirit X3 与自定义词法分析器结合使用

标签 c++17 boost-spirit boost-spirit-x3

X3 解析器如何使用已生成的标记向量。 如何定义规则,例如

enum class token { aa, bb, cc};    
auto rule = token::aa >> token::bb >> -token::cc;
std::vector<token> tokens{token::aa, token:bb, token:cc};
auto ok = parse(tokens.cbegin(), tokens.cend(), rule);

我有兴趣验证输入。这个想法是为了避免遵循 C++ 零开销原则的任何词法分析(x3::lit、x3::char_、x3::lexeme、x3::alpha 等)。

最佳答案

X3 不支持 Lex,而且可能永远不会支持。

从这里开始:

  • using Lex makes most of the sweet-spot disappear since all "highlevel" parsers (like real_parser, [u]int_parser) are out the window. The Spirit devs are on record they prefer not to use Lex. Moreover, Spirit X3 doesn't have Lex support anymore

我在这里快速搜索了原始引用:https://sourceforge.net/p/spirit/mailman/spirit-general/thread/CACBJYpn4YvXpKoU68cWZD4PmGG0R4kfe%3DVDu5PuTkA7S79QAoA%40mail.gmail.com/#msg34551953

There are no replacements for the lexer and I doubt X3 will ever have one, unless someone contributes some time and effort. I personally don't really use them, instead preferring on pure Qi. If you do it right, A pure Qi parser can be at par with or even outperform one with a lexer (anecdotal evidence only, not fully substantiated).

The real advantage of X3 over QI is 1) in compile time and 2) in AST building. Parsing should be more or less the same. But, again in my experience, the most time consuming operations are in AST building, not parsing and not lexing.

Apologies in advance to the onslaught of questions and thanks in advance for any insights. I'd love to migrate our parser to X3 as it seems to provide many benefits over Qi.

再次请原谅延迟回复。

问候,
--
乔尔·德·古兹曼

当被问及时,Joel 在其他地方建议如果您的应用程序确实从中受益,则创建您自己的简单 token 流,例如在这里https://sourceforge.net/p/spirit/mailman/spirit-general/thread/2A5FC1FD75EA0346A44CF4BD559664CD10CCEC%40SHEX-MB-09.ad.local/#msg34855244

关于c++17 - 将 BOOST Spirit X3 与自定义词法分析器结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77245558/

相关文章:

c++ - 一条规则使用 BOOST_FUSION_ADAPT_STRUCT 时出现精神业力语法问题

boost - Boost::Spirit 解析规则中的复合属性生成

c++ - Boost.Spirit.x3 避免将相同类型的两个连续属性 fold 成一个 vector

c++ - 在 C++ 中生成随机字符串的最有效方法是什么?

c++ - std::variant 和 std::visit: 错误:没有名为 'valueless_by_exception' 的成员

c++ - 如何在 Boost Spirit Qi 中返回堆分配值

c++ - 如何在 Spirit x3 中执行 no_case

c++ - 使用spirit x3 解析字符串列表,后跟字符串列表

c++ - 继承和友元函数,从基类访问 protected 成员

C++17 复制构造函数,std::unordered_map 上的深度复制