Analysis of preconditions and recursion with Boost :: Spirit

I am trying to parse a PDDL file using Boost :: Spirit and have some problems parsing the preconditions in the structure. I am struggling to understand Boost's guide on how to get a condition in my structure and with recursion.

I give a code snippet below that should illustrate the problem well. A line that looks like this should be parsed:

:precondition
(and
  (at-pos ?r ?pos)
  (not (has-pos ?m ?pos))
)

My code so far looks like this, but I'm pretty sure I did not understand how at_c works without having experience with Boost :: Phoenix.

predi_param = '?' >> name_type;
predi = '(' 
    >> name_type
    >> +predi_param
    >> ')';
literal = ( 
    ( '(' >> lit("not") >>
      predi       [at_c<0>(_val) = false]
      >> ')'
    )
    | predi       [at_c<0>(_val) = true]
  )
  >> ')';
pred_list = ( '(' >> lit("and") >> (*pred_list) >> ')')
  | literal;
preconditions = lit(":precondition") >> pred_list;

qi::rule<Iterator, std::string(), ascii::space_type> predi_param;
qi::rule<Iterator, Predicate(), ascii::space_type> predi;
qi::rule<Iterator, Literal(), ascii::space_type> literal;
qi::rule<Iterator, std::vector<Literal>(), ascii::space_type> preconditions, pred_list;

My AST is as follows:

struct Predicate
{
  std::string name;
  std::vector<std::string> predicate_params;
};  
struct Literal
{
  bool condition;
  Predicate predicate;
};

BOOST_FUSION_ADAPT_STRUCT(
    pddl_parser::Literal,
    (bool, condition)
    (pddl_parser::Predicate, predicate)
)

BOOST_FUSION_ADAPT_STRUCT(
    pddl_parser::Predicate,
    (std::string, name)
    (std::vector<std::string>, predicate_params)
)

Compiling the results leads to a compilation error:

parser.cpp:67:17:   required from ‘pddl_parser::domain_parser<Iterator>::domain_parser() [with Iterator = __gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >]’
parser.cpp:136:10:   required from here
/usr/include/boost/spirit/home/qi/detail/assign_to.hpp:153:20: error: no matching function for call to ‘pddl_parser::Literal::Literal(const std::vector<pddl_parser::Literal>&)’
             attr = static_cast<Attribute>(val);
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from parser.cpp:11:0:
./pddlast.h:23:10: note: candidate: pddl_parser::Literal::Literal()
   struct Literal
          ^~~~~~~
./pddlast.h:23:10: note:   candidate expects 0 arguments, 1 provided
./pddlast.h:23:10: note: candidate: pddl_parser::Literal::Literal(const pddl_parser::Literal&)
./pddlast.h:23:10: note:   no known conversion for argument 1 from ‘const std::vector<pddl_parser::Literal>’ to ‘const pddl_parser::Literal&’
./pddlast.h:23:10: note: candidate: pddl_parser::Literal::Literal(pddl_parser::Literal&&)
./pddlast.h:23:10: note:   no known conversion for argument 1 from ‘const std::vector<pddl_parser::Literal>’ to ‘pddl_parser::Literal&&’

pred_list pred_list = ( '(' >> *literal) >> ')');, , , (and ). , - , . Boost:: Spirit.

+4
1

, ,

pred_list = ( '(' >> *literal) >> ')');

, :

pred_list = ( '(' >> lit("and") >> (*pred_list) >> ')') | literal;

, . pred_list std::vector<Literal>, , (*literal) , .

. ('(', "and", ')'), ... *pred_list. pred_list std::vector<Literal>, , , *pred_list std::vector<std::vector<Literal> >. , "" | literal variant<vector<vector<Literal>>, Literal>.

. . , .

, , , . , /then/, AST.

Interlude

literal. (. qi:: rule ):

literal = 
    '(' >> matches("not") >> predi >> ')'
    | qi::attr(false) >> predi
    ;

PS ,

    >> ')';

?

, , -

(function_name arguments)

. , , .

, :

namespace AST {
    using Atom = std::string;

    struct Application;

    using Expression = boost::variant<Atom, Application>;

    struct Application {
        Atom function;
        std::vector<Expression> arguments;
    };
}

, . , :

template <typename Iterator>
struct Precondition : qi::grammar<Iterator, AST::Expression()> {
    Precondition() : Precondition::base_type(precondition) {
        using namespace qi;

        atom         = +(graph - '(' - ')');
        application  = '(' >> atom >> *expression >> ')';
        expression   = atom | application;

        precondition = skip(ascii::space) [":precondition" >> expression];

        BOOST_SPIRIT_DEBUG_NODES((precondition)(expression)(application)(atom))
    }

  private:
    using Skipper = qi::ascii::space_type;
    qi::rule<Iterator, AST::Application(), Skipper> application;
    qi::rule<Iterator, AST::Expression(), Skipper>  expression;

    // lexemes
    qi::rule<Iterator, AST::Expression()> precondition;
    qi::rule<Iterator, AST::Atom()> atom;
};

, AST node. precondition .

: Live On Coliru

Parsed (and (at-pos ?r ?pos) (not (has-pos ?m ?pos)))

BOOST_SPIRIT_DEBUG:

<precondition>
  <try>:precondition\n      </try>
  <expression>
    <try>\n                   </try>
    <atom>
      <try>(and\n               </try>
      <fail/>
    </atom>
    <application>
      <try>(and\n               </try>
      <atom>
        <try>and\n                </try>
        <success>\n                   </success>
        <attributes>[[a, n, d]]</attributes>
      </atom>
      <expression>
        <try>\n                   </try>
        <atom>
          <try>(at-pos ?r ?pos)\n   </try>
          <fail/>
        </atom>
        <application>
          <try>(at-pos ?r ?pos)\n   </try>
          <atom>
            <try>at-pos ?r ?pos)\n    </try>
            <success> ?r ?pos)\n          </success>
            <attributes>[[a, t, -, p, o, s]]</attributes>
          </atom>
          <expression>
            <try> ?r ?pos)\n          </try>
            <atom>
              <try>?r ?pos)\n           </try>
              <success> ?pos)\n             </success>
              <attributes>[[?, r]]</attributes>
            </atom>
            <success> ?pos)\n             </success>
            <attributes>[[?, r]]</attributes>
          </expression>
          <expression>
            <try> ?pos)\n             </try>
            <atom>
              <try>?pos)\n              </try>
              <success>)\n                  </success>
              <attributes>[[?, p, o, s]]</attributes>
            </atom>
            <success>)\n                  </success>
            <attributes>[[?, p, o, s]]</attributes>
          </expression>
          <expression>
            <try>)\n                  </try>
            <atom>
              <try>)\n                  </try>
              <fail/>
            </atom>
            <application>
              <try>)\n                  </try>
              <fail/>
            </application>
            <fail/>
          </expression>
          <success>\n                   </success>
          <attributes>[[[a, t, -, p, o, s], [[?, r], [?, p, o, s]]]]</attributes>
        </application>
        <success>\n                   </success>
        <attributes>[[[a, t, -, p, o, s], [[?, r], [?, p, o, s]]]]</attributes>
      </expression>
      <expression>
        <try>\n                   </try>
        <atom>
          <try>(not (has-pos ?m ?po</try>
          <fail/>
        </atom>
        <application>
          <try>(not (has-pos ?m ?po</try>
          <atom>
            <try>not (has-pos ?m ?pos</try>
            <success> (has-pos ?m ?pos))\n</success>
            <attributes>[[n, o, t]]</attributes>
          </atom>
          <expression>
            <try> (has-pos ?m ?pos))\n</try>
            <atom>
              <try>(has-pos ?m ?pos))\n </try>
              <fail/>
            </atom>
            <application>
              <try>(has-pos ?m ?pos))\n </try>
              <atom>
                <try>has-pos ?m ?pos))\n  </try>
                <success> ?m ?pos))\n         </success>
                <attributes>[[h, a, s, -, p, o, s]]</attributes>
              </atom>
              <expression>
                <try> ?m ?pos))\n         </try>
                <atom>
                  <try>?m ?pos))\n          </try>
                  <success> ?pos))\n            </success>
                  <attributes>[[?, m]]</attributes>
                </atom>
                <success> ?pos))\n            </success>
                <attributes>[[?, m]]</attributes>
              </expression>
              <expression>
                <try> ?pos))\n            </try>
                <atom>
                  <try>?pos))\n             </try>
                  <success>))\n                 </success>
                  <attributes>[[?, p, o, s]]</attributes>
                </atom>
                <success>))\n                 </success>
                <attributes>[[?, p, o, s]]</attributes>
              </expression>
              <expression>
                <try>))\n                 </try>
                <atom>
                  <try>))\n                 </try>
                  <fail/>
                </atom>
                <application>
                  <try>))\n                 </try>
                  <fail/>
                </application>
                <fail/>
              </expression>
              <success>)\n                  </success>
              <attributes>[[[h, a, s, -, p, o, s], [[?, m], [?, p, o, s]]]]</attributes>
            </application>
            <success>)\n                  </success>
            <attributes>[[[h, a, s, -, p, o, s], [[?, m], [?, p, o, s]]]]</attributes>
          </expression>
          <expression>
            <try>)\n                  </try>
            <atom>
              <try>)\n                  </try>
              <fail/>
            </atom>
            <application>
              <try>)\n                  </try>
              <fail/>
            </application>
            <fail/>
          </expression>
          <success>\n                   </success>
          <attributes>[[[n, o, t], [[[h, a, s, -, p, o, s], [[?, m], [?, p, o, s]]]]]]</attributes>
        </application>
        <success>\n                   </success>
        <attributes>[[[n, o, t], [[[h, a, s, -, p, o, s], [[?, m], [?, p, o, s]]]]]]</attributes>
      </expression>
      <expression>
        <try>\n                   </try>
        <atom>
          <try>)</try>
          <fail/>
        </atom>
        <application>
          <try>)</try>
          <fail/>
        </application>
        <fail/>
      </expression>
      <success></success>
      <attributes>[[[a, n, d], [[[a, t, -, p, o, s], [[?, r], [?, p, o, s]]], [[n, o, t], [[[h, a, s, -, p, o, s], [[?, m], [?, p, o, s]]]]]]]]</attributes>
    </application>
    <success></success>
    <attributes>[[[a, n, d], [[[a, t, -, p, o, s], [[?, r], [?, p, o, s]]], [[n, o, t], [[[h, a, s, -, p, o, s], [[?, m], [?, p, o, s]]]]]]]]</attributes>
  </expression>
  <success></success>
  <attributes>[[[a, n, d], [[[a, t, -, p, o, s], [[?, r], [?, p, o, s]]], [[n, o, t], [[[h, a, s, -, p, o, s], [[?, m], [?, p, o, s]]]]]]]]</attributes>
</precondition>

:

  • AST
  • (. )

, , - AST PDDL. , , , .

Live On Coliru

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

namespace AST {
    using Atom = std::string;

    struct Application;

    using Expression = boost::variant<Atom, Application>;

    struct Application {
        Atom function;
        std::vector<Expression> arguments;

        friend std::ostream& operator<<(std::ostream& os, Application const& a) {
            os << "(" << a.function;
            for (auto& arg : a.arguments)
                os << " " << arg;
            return os << ")";
        }
    };
}

BOOST_FUSION_ADAPT_STRUCT(AST::Application, function, arguments)

namespace pddl_parser {

    namespace qi    = boost::spirit::qi;

    template <typename Iterator>
    struct Precondition : qi::grammar<Iterator, AST::Expression()> {
        Precondition() : Precondition::base_type(precondition) {
            using namespace qi;

            atom         = +(graph - '(' - ')');
            application  = '(' >> atom >> *expression >> ')';
            expression   = atom | application;

            precondition = skip(ascii::space) [":precondition" >> expression];

            BOOST_SPIRIT_DEBUG_NODES((precondition)(expression)(application)(atom))
        }

      private:
        using Skipper = qi::ascii::space_type;
        qi::rule<Iterator, AST::Application(), Skipper> application;
        qi::rule<Iterator, AST::Expression(), Skipper>  expression;

        // lexemes
        qi::rule<Iterator, AST::Expression()> precondition;
        qi::rule<Iterator, AST::Atom()> atom;
    };
}

int main() {
    using It = std::string::const_iterator;

    for (std::string const& input : {
            R"--(:precondition
                    (and
                      (at-pos ?r ?pos)
                      (not (has-pos ?m ?pos))
                    ))--"
            })
    {
        std::cout << "-----\n";
        It f = input.begin(), l = input.end();

        AST::Expression precondition;
        bool ok = parse(f, l, pddl_parser::Precondition<It>{}, precondition);

        if (ok) {
            std::cout << "Parsed " << precondition << "\n";
        } else {
            std::cout << "Parse Failed\n";
        }

        if (f != l) {
            std::cout << "Remaining unparsed input: '" << std::string(f, l) << "'\n";
        }
    }
}
+3

Source: https://habr.com/ru/post/1677372/


All Articles