正确设置 expectation_failure in boost::spirit 的跨度

Correctly set the span of an expectation_failure in boost::spirit

我正在尝试向我的解析器添加错误报告,但我不知道如何针对特定规则正确执行此操作。

相关规则匹配 mag(12.5)sqrt(5.38) 形式的函数调用。有一个固定的函数名称列表,每个函数都可以以不同于其他函数的方式解析其参数(例如,time(4) 只接受 int 值)。我的语法生成一个 AST,其中每个函数都有自己的节点类型(MagSqrtTime)。

我的第一个实现很简单:我支持的每个功能都有一个规则。

fn %= mag | sqrt | time;
mag %= (lit("mag") >> lit('(') > double_ > lit(')'));
sqrt %= (lit("sqrt") >> lit('(') > double_ > lit(')'));
time %= (lit("time") >> lit('(') > int_ > lit(')'));

这有效,但如果输入包含不受支持的函数名称 (hello(12)),规则将失败且不会出现错误。我想要的是 expectation_failure(或类似)失败的规则,即 "Expected mag, sqrt or time, got 'hello'".

下面是我尝试生成一个错误。它读取后跟左括号的任何标识(使用期望运算符),然后使用 eps 中的谓词做两件事:根据函数名称生成正确的节点,如果名称未知则失败,从而生成 expectation_failure。问题是 expectation_failure 的位置不是我想要的。它产生:

Expected <function parameters>
Got 12)

而不是

Expected <mag, sqrt or time>
Got hello

有没有办法控制expectation_failure::first::last的值?或者除了我应该使用的 expectation_failure 之外,还有另一种报告错误的方法吗?另外,我不明白为什么我的 expectation_failure 在这种情况下指向“12)”而不仅仅是“12”。

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_function.hpp>
#include <iostream>
#include <string>

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace spirit = boost::spirit;

struct Mag { double val; };
struct Sqrt { double val; };
struct Time { int val; };
using Fn = boost::variant<Mag, Sqrt, Time>;

std::ostream& operator<<(std::ostream& os, const Mag& v) {
  os << "mag(" << v.val << ")";
  return os;
}

std::ostream& operator<<(std::ostream& os, const Sqrt& v) {
  os << "sqrt(" << v.val << ")";
  return os;
}

std::ostream& operator<<(std::ostream& os, const Time& v) {
  os << "time(" << v.val << ")";
  return os;
}


BOOST_FUSION_ADAPT_STRUCT(Mag, (double, val))
BOOST_FUSION_ADAPT_STRUCT(Sqrt, (double, val))
BOOST_FUSION_ADAPT_STRUCT(Time, (int, val))


void makeMag_(Fn& fn, double val) {
  Mag s;
  s.val = val;
  fn.swap(Fn(s));
}

void makeSqrt_(Fn& fn, double val) {
  Sqrt s;
  s.val = val;
  fn.swap(Fn(s));
}

void makeTime_(Fn& fn, int val) {
  Time s;
  s.val = val;
  fn.swap(Fn(s));
}

BOOST_PHOENIX_ADAPT_FUNCTION(void, makeMag, makeMag_, 2)
BOOST_PHOENIX_ADAPT_FUNCTION(void, makeSqrt, makeSqrt_, 2)
BOOST_PHOENIX_ADAPT_FUNCTION(void, makeTime, makeTime_, 2)

template <typename Iterator>
struct FnParser : qi::grammar<Iterator, qi::locals<std::string>, ascii::space_type, Fn()>
{
  FnParser() : FnParser::base_type(fn)
  {
    using qi::double_;
    using qi::int_;
    using qi::_val;
    using qi::_1;
    using qi::_a;
    using qi::_r1;
    using qi::eps;
    using qi::lit;
    using qi::lexeme;
    using qi::alpha;

    ident %= lexeme[+alpha];

    fnParams =
          (eps(_r1 == "mag")  >> double_)  [makeMag(_val, _1)]
        | (eps(_r1 == "sqrt") >> double_)  [makeSqrt(_val, _1)]
        | (eps(_r1 == "time") >> int_)     [makeTime(_val, _1)]
        ;

    fn =  ident        [_a = _1]
        > lit('(')
        > fnParams(_a) [_val = _1]
        > lit(')');

    ident.name("identifier");
    fnParams.name("function parameters");
    fn.name("function");
  }

  qi::rule<Iterator, qi::locals<std::string>, ascii::space_type, Fn()> fn;
  qi::rule<Iterator, ascii::space_type, Fn(std::string)> fnParams;
  qi::rule<Iterator, ascii::space_type, std::string()> ident;
};

int main() {

      using Iter = std::string::const_iterator;
      using boost::spirit::ascii::space;

      FnParser <Iter> parser;

      std::string str;

      while (std::getline(std::cin, str)) {

        if (str.empty() || str[0] == 'q' || str[0] == 'Q')
          break;

        Iter iter = str.begin();
        Iter end = str.end();
        Fn fn;

        try {
          bool r = phrase_parse(iter, end, parser, space, fn);

          if (r && iter == end) {
            std::cout << "Ok\n";
          } else {
            std::string rest(iter, end);
            std::cout << "Failed\n"
                      << "Stopped at \"" << rest << "\"\n";
          }
        } catch(qi::expectation_failure<Iter> e) {
          std::string got(e.first, e.last);
          std::cout << "Expected " << e.what_ << "\n"
                    << "Got " << std::string(e.first, e.last) << "\n";
        }
      }
    }

编辑

我没有给出完整的语法,因此可能缺少一些上下文。除了函数调用之外,完整的语法还有算术运算符和变量。区分函数调用和变量的唯一方法是后面是否存在左括号。两者都可以在相同的上下文中使用,我使用有序的替代方案 fn | var 来优先考虑函数调用。这就是为什么我将期望点放在括号之后,而不是之前。

你已经控制了期望失败的位置。

例如

mag %= (lit("mag") >> lit('(') > double_ > lit(')'));

期望点是> double_。要将其移动到参数列表的开头,请说:

mag %= lit("mag") > (lit('(') >> double_ > lit(')'));

顺便说一句,你可以这样写:

mag = "mag" > ('(' >> double_ >> ')'));

Also, I don't understand why my expectation_failure points to "12)" and not just "12" in this case.

我认为它只是打印到输入序列的末尾。在输入迭代器 (qi::istream_iterator) 的情况下,它可能会打印到输入的最后一部分 seen,但这是猜测。

附带说明一下,您可以使用 on_error 获得更多控制权,记录在此处:https://www.boost.org/doc/libs/1_67_0/libs/spirit/doc/html/spirit/qi/tutorials/mini_xml___error_handling.html 和编译器示例中。


更新

进行编辑

The only way to tell apart a function call from a variable is the presence of an opening parenthesis afterwards. Both can be used in the same context and I use an ordered alternative fn | var to give priority to the function call. This is why I put the expectation point after the parenthesis, and not before.

你还可以拥有它:

mag = "mag" >> &lit('(') > ('(' >> double_ >> ')'));

这使用前瞻&lit('(')进入分支,然后从期望点开始。所以,没有 '(' 只是一个不匹配,但是期望点 "fires" 在参数列表 none-the-less.

!lit('(')用于负先行断言。 (文档 here and here)。

其他想法

您说:

The only way to tell apart a function call from a variable is the presence of an opening parenthesis afterwards

这当然取决于您对符号表和语义分析的选择。请参阅这些示例,其中我确实进行了动态功能检测:

有点相关:

  • Boost::spirit::qi defining a calculator for nullaries
  • Boost spirit parse rule is not applied