Why does this boost spirit qi rule fail to parse when a part of a composite rule but succeed when used on its own?

huangapple go评论72阅读模式
英文:

Why does this boost spirit qi rule fail to parse when a part of a composite rule but succeed when used on its own?

问题

This rule does not work because the PdRecordGrammar is not correctly set up to handle the input. In your composite PdRecordGrammar rule, you have two alternatives: canvasRule and arrayDataRule. When you try to parse the input string, it first attempts to match canvasRule, and if that fails, it tries to match arrayDataRule. However, the input #N canvas 0 0 400 300 moo 1; does not match either of these alternatives.

The canvasRule in the PdCanvasGrammar is designed to match a specific format, which your input doesn't follow. The arrayDataRule is intended for a different format as well.

To combine these rules properly, you need to modify your PdRecordGrammar to handle both formats. Here's an updated version of your PdRecordGrammar:

template <typename Iterator>
struct PdRecordGrammar : qi::grammar<Iterator, PdRecord(), ascii::space_type> {
    PdRecordGrammar() : PdRecordGrammar::base_type(recordRule) {
        using namespace qi;

        arrayDataRule = (lit("#A") >> float_ % ',' >> ';');
        canvasRule = PdCanvasGrammar<Iterator>();
        recordRule = canvasRule | arrayDataRule;
        BOOST_SPIRIT_DEBUG_NODES((arrayDataRule)(canvasRule)(recordRule))
    }

    qi::rule<Iterator, PdArrayData(), ascii::space_type> arrayDataRule;
    PdCanvasGrammar<Iterator> canvasRule;
    qi::rule<Iterator, PdRecord(), ascii::space_type> recordRule;
};

In this modified PdRecordGrammar, the arrayDataRule is updated to match the format of your #A rule, and canvasRule is still used to match the #N canvas format. The recordRule is updated to combine both canvasRule and arrayDataRule. This should now properly handle both formats when parsing.

With this change, your PdRecordGrammar should work as expected, and you can use it to parse both canvas and array data rules.

英文:

I am trying to combine multiple boost::spirit parse rules into bigger, composite rules, and have the following code:

#define BOOST_SPIRIT_DEBUG
#include &lt;boost/spirit/include/qi.hpp&gt;
#include &lt;boost/fusion/adapted/adt/adapt_adt.hpp&gt;
#include &lt;boost/fusion/include/adapt_adt.hpp&gt;
#include &lt;boost/variant.hpp&gt;

#include &lt;string&gt; 
#include &lt;vector&gt;
#include &lt;fstream&gt;


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

struct PdCanvas {
    int topX;
    int topY;
    int wX;
    int wY;
    std::string name;
    int openOnLoad; 
};


struct PdArrayData {
    std::vector&lt;float&gt; data; 
};


BOOST_FUSION_ADAPT_STRUCT(
    PdCanvas,
    (int, topX)
    (int, topY)
    (int, wX) 
    (int, wY)
    (std::string, name)
    (int, openOnLoad));


BOOST_FUSION_ADAPT_STRUCT(
    PdArrayData,
    (std::vector&lt;float&gt;, data)); 


using PdRecord = boost::variant&lt;
PdArrayData,
PdCanvas
&gt;;


template &lt;typename Iterator&gt; struct PdCanvasGrammar : qi::grammar&lt;Iterator, PdCanvas()&gt; {
    PdCanvasGrammar() : PdCanvasGrammar::base_type(start) {
        using namespace qi;
        start      = skip(space)[canvasRule &gt;&gt; eoi];
        name       = +((&#39;\\&#39; &gt;&gt; space) |graph);
        canvasRule = &quot;#N canvas&quot; &gt;&gt; int_ &gt;&gt; int_ &gt;&gt; int_ &gt;&gt; int_ &gt;&gt; name &gt;&gt; int_ &gt;&gt; &#39;;&#39;;

        BOOST_SPIRIT_DEBUG_NODES((start)(canvasRule)(name))
    }

  private:
    qi::rule&lt;Iterator, PdCanvas()&gt; start;
    qi::rule&lt;Iterator, PdCanvas(), qi::space_type&gt; canvasRule;
    qi::rule&lt;Iterator, std::string()&gt; name;
};


template &lt;typename Iterator&gt; 
struct PdRecordGrammar : qi::grammar&lt;Iterator, PdRecord(), ascii::space_type&gt; {
    PdRecordGrammar () : PdRecordGrammar::base_type(recordRule) {         
        using namespace qi;
        
        arrayDataRule = (lit(&quot;#A&quot;) &gt;&gt;  +(qi::float_) &gt;&gt; &quot;;&quot;);
        canvasRule = PdCanvasGrammar&lt;Iterator&gt;();
        recordRule = (canvasRule |  (arrayDataRule)  );
        BOOST_SPIRIT_DEBUG_NODES((arrayDataRule)(canvasRule)(recordRule))
    }

    
    qi::rule&lt;Iterator, PdArrayData(), ascii::space_type&gt; arrayDataRule; 
    qi::rule&lt;Iterator, PdCanvas()&gt; canvasRule;     
    qi::rule&lt;Iterator, PdRecord(), ascii::space_type&gt; recordRule;     
};



int main(int argc, char** argv)
{
  if(argc != 2)
    {
        std::cout &lt;&lt; &quot;Usage: &quot;  &lt;&lt;argv[0] &lt;&lt; &quot; &lt;PatchFile&gt;&quot; &lt;&lt; std::endl;
        exit(1); 
    }

    std::ifstream inputFile(argv[1]); 
    std::string inputString(std::istreambuf_iterator&lt;char&gt;(inputFile), {}); 

    PdRecord root;
    PdRecordGrammar&lt;std::string::iterator&gt; parser;
    std::cout &lt;&lt; &quot;Loaded file:\n &quot; &lt;&lt; inputString &lt;&lt; std::endl;


    PdCanvas canvObj;
    PdCanvasGrammar&lt;std::string::iterator&gt; canvParse;
    bool canvSuccess = qi::phrase_parse(inputString.begin(), inputString.end(), canvParse, boost::spirit::ascii::space, canvObj);
    std::cout &lt;&lt; &quot;Canvas success: &quot; &lt;&lt; canvSuccess &lt;&lt; std::endl; 

    bool success = qi::phrase_parse(inputString.begin(), inputString.end(), parser, boost::spirit::ascii::space, root); 
    std::cout &lt;&lt; &quot;Success: &quot; &lt;&lt; success &lt;&lt; std::endl;

    return 0; 

}

I then tested the code on the following string, which should parse:

#N canvas 0 0 400 300 moo 1;

Running the code gives the following output:

Loaded file:
 #N canvas 0 0 400 300 moo 1;

&lt;start&gt;
  &lt;try&gt;#N canvas 0 0 400 30&lt;/try&gt;
  &lt;canvasRule&gt;
    &lt;try&gt;#N canvas 0 0 400 30&lt;/try&gt;
    &lt;name&gt;
      &lt;try&gt;moo 1;\n&lt;/try&gt;
      &lt;success&gt; 1;\n&lt;/success&gt;
      &lt;attributes&gt;[[m, o, o]]&lt;/attributes&gt;
    &lt;/name&gt;
    &lt;success&gt;\n&lt;/success&gt;
    &lt;attributes&gt;[[0, 0, 400, 300, [m, o, o], 1]]&lt;/attributes&gt;
  &lt;/canvasRule&gt;
  &lt;success&gt;&lt;/success&gt;
  &lt;attributes&gt;[[0, 0, 400, 300, [m, o, o], 1]]&lt;/attributes&gt;
&lt;/start&gt;
Canvas success: 1
&lt;recordRule&gt;
  &lt;try&gt;#N canvas 0 0 400 30&lt;/try&gt;
  &lt;canvasRule&gt;
    &lt;try&gt;#N canvas 0 0 400 30&lt;/try&gt;
    &lt;fail/&gt;
  &lt;/canvasRule&gt;
  &lt;arrayDataRule&gt;
    &lt;try&gt;#N canvas 0 0 400 30&lt;/try&gt;
    &lt;fail/&gt;
  &lt;/arrayDataRule&gt;
  &lt;fail/&gt;
&lt;/recordRule&gt;
Success: 0

As one can see, the file is successfully parsed by the standalone PdCanvasGrammar rule, but fails to parse when I attempt to use the composite PdRecordGrammar rule.

I assume I'm doing something wrong in combining the rules together, but I don't know what.

As an aside, the arrayDataRule, which is defined as part of the PdRecordGrammar, successfully parses its input:

Loaded file:
 #A 1 2 3 4 5;
&lt;start&gt;
  &lt;try&gt;#A 1 2 3 4 5;&lt;/try&gt;
  &lt;canvasRule&gt;
    &lt;try&gt;#A 1 2 3 4 5;&lt;/try&gt;
    &lt;fail/&gt;
  &lt;/canvasRule&gt;
  &lt;fail/&gt;
&lt;/start&gt;
Canvas success: 0
&lt;recordRule&gt;
  &lt;try&gt;#A 1 2 3 4 5;&lt;/try&gt;
  &lt;canvasRule&gt;
    &lt;try&gt;#A 1 2 3 4 5;&lt;/try&gt;
    &lt;fail/&gt;
  &lt;/canvasRule&gt;
  &lt;arrayDataRule&gt;
    &lt;try&gt;#A 1 2 3 4 5;&lt;/try&gt;
    &lt;success&gt;&lt;/success&gt;
    &lt;attributes&gt;[[[1, 2, 3, 4, 5]]]&lt;/attributes&gt;
  &lt;/arrayDataRule&gt;
  &lt;success&gt;&lt;/success&gt;
  &lt;attributes&gt;[[[1, 2, 3, 4, 5]]]&lt;/attributes&gt;
&lt;/recordRule&gt;
Success: 1

So my assumption is that if I just took the PdCanvasGrammar rule and defined it intrinsically to the PdRecord rule, that it would work. But I'm trying to understand how to properly combine rules so that no single rule gets too large and unwieldy.

So my question is thus:
Why does this rule as defined not work, and what is the proper way to combine rules into a composite rule?

Versions:

Boost: 1.75.0

C++ standard: 11

GCC: gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)

答案1

得分: 2

以下是翻译的部分:

canvasRule = PdCanvasGrammar<Iterator>();

创建一个指向悬空临时对象的引用。修复它:

template <typename Iterator>
struct PdRecordGrammar : qi::grammar<Iterator, PdRecord(), qi::space_type> {
    PdRecordGrammar() : PdRecordGrammar::base_type(recordRule) {
        arrayDataRule = "#A" >> +qi::float_ >> ";";
        recordRule = canvasRule | arrayDataRule;

        BOOST_SPIRIT_DEBUG_NODES((arrayDataRule)(recordRule))
    }

  private:
    qi::rule<Iterator, PdArrayData(), qi::space_type> arrayDataRule;
    PdCanvasGrammar<Iterator> canvasRule;
    qi::rule<Iterator, PdRecord(), qi::space_type> recordRule;
};

简化:

  • 我已经在之前简化了一些部分before。没有理由撤销这些简化。
  • 你没有使用adapt_adt头文件(这是最好的方式)。为什么要包含它?
  • 为什么在Fusion ADAPT_XXXX宏的前C++11版本中使用phrase_parse,当语法没有skipper(或在内部覆盖它)时?请参考https://stackoverflow.com/questions/17072987/boost-spirit-skipper-issues/17073965#17073965以获取更多背景信息。
  • 在许多地方有多余的lit()、括号和分号。
  • 你使用using namespace qi,但仍然限定了qi::float_
  • 一个语法将规则声明为私有的,而另一个则没有。
  • 变量的作用域与使用不匹配(在main中)。这会导致生命周期/变量重用错误,或者只是难以维护的代码。
  • 你的输入可以使用const_iterator。应该使用。

Live Demo

// #define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/adapted.hpp>
#include <iomanip>

namespace qi = boost::spirit::qi;

struct PdCanvas { int topX, topY, wX, wY, openOnLoad; std::string name; };
struct PdArrayData { std::vector<float> data; };

BOOST_FUSION_ADAPT_STRUCT(PdCanvas, topX, topY, wX, wY, name, openOnLoad)
BOOST_FUSION_ADAPT_STRUCT(PdArrayData, data)

using PdRecord = boost::variant<PdArrayData, PdCanvas>;

template <typename Iterator> struct PdCanvasGrammar : qi::grammar<Iterator, PdCanvas()> {
    PdCanvasGrammar() : PdCanvasGrammar::base_type(start) {
        using namespace qi;
        start = skip(space)[canvasRule >> eoi];
        name = +(char_("\\") >> space | graph);
        canvasRule = "#N canvas" >> int_ >> int_ >> int_ >> int_ >> name >> int_ >> ';';

        BOOST_SPIRIT_DEBUG_NODES((start)(canvasRule)(name))
    }

  private:
    qi::rule<Iterator, PdCanvas()> start;
    qi::rule<Iterator, PdCanvas(), qi::space_type> canvasRule;
    qi::rule<Iterator, std::string()> name;
};

template <typename Iterator>
struct PdRecordGrammar : qi::grammar<Iterator, PdRecord(), qi::space_type> {
    PdRecordGrammar() : PdRecordGrammar::base_type(recordRule) {
        arrayDataRule = "#A" >> +qi::float_ >> ";";
        recordRule = canvasRule | arrayDataRule;

        BOOST_SPIRIT_DEBUG_NODES((arrayDataRule)(recordRule))
    }

  private:
    qi::rule<Iterator, PdArrayData(), qi::space_type> arrayDataRule;
    PdCanvasGrammar<Iterator> canvasRule;
    qi::rule<Iterator, PdRecord(), qi::space_type> recordRule;
};

int main() {
    std::string const input = "#N canvas 0 0 400 300 moo 1; ";
    std::cout << "Input:\n " << std::quoted(input) << std::endl;

    {
        PdCanvas obj;
        PdCanvasGrammar<std::string::const_iterator> const canvParse;
        std::cout << "Canvas success: " << parse(input.begin(), input.end(), canvParse, obj) << "\n";
    }

    {
        PdRecord obj;
        PdRecordGrammar<std::string::const_iterator> const parser;
        std::cout << "Success: " << phrase_parse(input.begin(), input.end(), parser, qi::space, obj) << "\n";
    }
}

输出:

Input:
"#N canvas 0 0 400 300 moo 1; "
Canvas success: 1
Success: 1
英文:

This:

canvasRule    = PdCanvasGrammar&lt;Iterator&gt;();

creates a reference to a dangling temporary. Fix it:

template &lt;typename Iterator&gt;
struct PdRecordGrammar : qi::grammar&lt;Iterator, PdRecord(), qi::space_type&gt; {
PdRecordGrammar() : PdRecordGrammar::base_type(recordRule) {
arrayDataRule = &quot;#A&quot; &gt;&gt; +qi::float_ &gt;&gt; &quot;;&quot;;
recordRule    = canvasRule | arrayDataRule;
BOOST_SPIRIT_DEBUG_NODES((arrayDataRule)(recordRule))
}
private:
qi::rule&lt;Iterator, PdArrayData(), qi::space_type&gt; arrayDataRule;
PdCanvasGrammar&lt;Iterator&gt;                         canvasRule;
qi::rule&lt;Iterator, PdRecord(), qi::space_type&gt;    recordRule;
};

Simplify

  • I've simplified some parts before. There's no reason to undo that.
  • You're not using the adapt_adt header (and that's for the best). Why include that?
  • Why use the pre-c++11 versions of the Fusion ADAPT_XXXX macros?
  • Why use phrase_parse when the grammar has no skipper (or overrides it internally). See https://stackoverflow.com/questions/17072987/boost-spirit-skipper-issues/17073965#17073965 again for context.
  • There's redundant lit(), parentheses and ; in quite a few places.
  • You have using namespace qi but still qualify qi::float_.
  • One grammar declares rules private, the other doesn't
  • The scopes of variables doesn't match the use (in main). This leads to lifetime/variable re-use bugs. Or just code that's hard to maintain.
  • Your input can use const_iterator. It should.

Live Demo

// #define BOOST_SPIRIT_DEBUG
#include &lt;boost/spirit/include/qi.hpp&gt;
#include &lt;boost/fusion/adapted.hpp&gt;
#include &lt;iomanip&gt;
namespace qi = boost::spirit::qi;
struct PdCanvas { int topX, topY, wX, wY, openOnLoad; std::string name; };
struct PdArrayData { std::vector&lt;float&gt; data; };
BOOST_FUSION_ADAPT_STRUCT(PdCanvas, , topX, topY, wX, wY, name, openOnLoad)
BOOST_FUSION_ADAPT_STRUCT(PdArrayData, data)
using PdRecord = boost::variant&lt;PdArrayData, PdCanvas&gt;;
template &lt;typename Iterator&gt; struct PdCanvasGrammar : qi::grammar&lt;Iterator, PdCanvas()&gt; {
PdCanvasGrammar() : PdCanvasGrammar::base_type(start) {
using namespace qi;
start      = skip(space)[canvasRule &gt;&gt; eoi];
name       = +(&#39;\\&#39; &gt;&gt; space | graph);
canvasRule = &quot;#N canvas&quot; &gt;&gt; int_ &gt;&gt; int_ &gt;&gt; int_ &gt;&gt; int_ &gt;&gt; name &gt;&gt; int_ &gt;&gt; &#39;;&#39;;
BOOST_SPIRIT_DEBUG_NODES((start)(canvasRule)(name))
}
private:
qi::rule&lt;Iterator, PdCanvas()&gt;                 start;
qi::rule&lt;Iterator, PdCanvas(), qi::space_type&gt; canvasRule;
qi::rule&lt;Iterator, std::string()&gt;              name;
};
template &lt;typename Iterator&gt;
struct PdRecordGrammar : qi::grammar&lt;Iterator, PdRecord(), qi::space_type&gt; {
PdRecordGrammar() : PdRecordGrammar::base_type(recordRule) {
arrayDataRule = &quot;#A&quot; &gt;&gt; +qi::float_ &gt;&gt; &quot;;&quot;;
recordRule    = canvasRule | arrayDataRule;
BOOST_SPIRIT_DEBUG_NODES((arrayDataRule)(recordRule))
}
private:
qi::rule&lt;Iterator, PdArrayData(), qi::space_type&gt; arrayDataRule;
PdCanvasGrammar&lt;Iterator&gt;                         canvasRule;
qi::rule&lt;Iterator, PdRecord(), qi::space_type&gt;    recordRule;
};
int main() {
std::string const input = &quot;#N canvas 0 0 400 300 moo 1; &quot;;
std::cout &lt;&lt; &quot;Input:\n &quot; &lt;&lt; quoted(input) &lt;&lt; std::endl;
{
PdCanvas obj;
PdCanvasGrammar&lt;std::string::const_iterator&gt; const canvParse;
std::cout &lt;&lt; &quot;Canvas success: &quot; &lt;&lt; parse(input.begin(), input.end(), canvParse, obj) &lt;&lt; &quot;\n&quot;;
}
{
PdRecord                                     obj;
PdRecordGrammar&lt;std::string::const_iterator&gt; const parser;
std::cout &lt;&lt; &quot;Success: &quot; &lt;&lt; phrase_parse(input.begin(), input.end(), parser, qi::space, obj) &lt;&lt; &quot;\n&quot;;
}
}

Output:

Input:
&quot;#N canvas 0 0 400 300 moo 1; &quot;
Canvas success: 1
Success: 1

huangapple
  • 本文由 发表于 2023年4月7日 01:51:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定