Boost Spirit X3 Skip Parser Implementation?

Boost Spirit X3 Skip Parser Implementation?

本文关键字:Implementation Parser Skip Spirit X3 Boost      更新时间:2023-10-16

对于我正在使用X3解析的当前语法,空格和Perl风格的注释将被忽略。

在我看来,X3 中的跳过解析器只是一个普通的解析器,它消耗的任何输入都被视为"跳过"。我想出了这个:

namespace x3 = boost::spirit::x3;
auto const blank_comment = 
   x3::blank | x3::lexeme[ '#' >> *(x3::char_ - x3::eol) >> x3::eol ];

在解析一个非常基本的输入(几个注释行和一个引用的字符串行(时,这似乎效果很好。(住在科利鲁(

但是,由于我找不到有关此事的任何文档,并且当前跳过解析器的详细信息隐藏在一个复杂的模板系统中,我希望得到一些输入。

  1. 这是定义"跳过解析器"的正确方法吗?有标准方法吗?
  2. 这样的实现是否存在性能问题?如何改进?

我之前搜索了SO以获取详细信息,并使用Qi(带有Boost::Spirit的自定义跳过解析器(找到了答案。由于我从未学过气,所以很多细节都很难理解。我上面描述的方法似乎更直观。

是的,这很好。

船长似乎很理想。您可以通过重新排序和使用字符集否定 ( operator~ 来优化quoted_string规则:

住在科里鲁

#include <boost/spirit/home/x3.hpp>
namespace parser {
    namespace x3 = boost::spirit::x3;
    auto const quoted_string = x3::lexeme [ '"' >>  *('' >> x3::char_ | ~x3::char_(""n")) >> '"' ];
    auto const space_comment = x3::space | x3::lexeme[ '#' >> *(x3::char_ - x3::eol) >> x3::eol];
}
#include <iostream>
int main() {
    std::string result, s1 = "# foonn#barn   t"This is a simple string, containing \"escaped quotes\""";
    phrase_parse(s1.begin(), s1.end(), parser::quoted_string, parser::space_comment, result);
    std::cout << "Original: `" << s1 << "`nResult: `" << result << "`n";
}

指纹

Original: `# foo
#bar
    "This is a simple string, containing "escaped quotes""`
Result: `This is a simple string, containing "escaped quotes"`