Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
637 views
in Technique[技术] by (71.8m points)

c++ - Boost.Spirit X3 parser "no type named type in(...)"


I was toying with Boost.Spirit X3 calculator example when I encountered an error I couldn't get my head around.
I minimized the program to reduce complexity still throwing the same error. Say I want to parse an input as a list of statements (strings) followed by a delimiter (';').

This is my structure:


namespace client { namespace ast
 {    
     struct program
    {
        std::list<std::string> stmts;
    };
 }}

BOOST_FUSION_ADAPT_STRUCT(client::ast::program,
            (std::list<std::string>, stmts)
)

The grammar is as follows:

namespace client
{ 
    namespace grammar
    {

   x3::rule<class program, ast::program> const program("program");

    auto const program_def =
            *((*char_) > ';')
            ;

   BOOST_SPIRIT_DEFINE(
       program
    );
    auto calculator = program;
}

using grammar::calculator;

}

Invoked


    int
    main()
    {
    std::cout <<"///////////////////////////////////////////

";
    std::cout << "Expression parser...

";
    std::cout << //////////////////////////////////////////////////

";
    std::cout << "Type an expression...or [q or Q] to quit

";

    typedef std::string::const_iterator iterator_type;
    typedef client::ast::program ast_program;

    std::string str;
    while (std::getline(std::cin, str))
    {
        if (str.empty() || str[0] == 'q' || str[0] == 'Q')
            break;

        auto& calc = client::calculator;    // Our grammar
        ast_program program;                // Our program (AST)

        iterator_type iter = str.begin();
        iterator_type end = str.end();
        boost::spirit::x3::ascii::space_type space;
        bool r = phrase_parse(iter, end, calc, space, program);

        if (r && iter == end)
        {
            std::cout << "-------------------------
";
            std::cout << "Parsing succeeded
";
            std::cout<< '
';
            std::cout << "-------------------------
";
        }
        else
        {
            std::cout << "-------------------------
";
            std::cout << "Parsing failed
";
            std::cout << "-------------------------
";
        }
    }

    std::cout << "Bye... :-) 

";
    return 0;
}

Error I get is

/opt/boost_1_66_0/boost/spirit/home/x3/support/traits/container_traits.hpp: In instantiation of ‘struct boost::spirit::x3::traits::container_value<client::ast::program, void>’:
.
.
.

/opt/boost_1_66_0/boost/spirit/home/x3/support/traits/container_traits.hpp:76:12: error: no type named ‘value_type’ in ‘struct client::ast::program’
         struct container_value
/opt/boost_1_66_0/boost/spirit/home/x3/operator/detail/sequence.hpp:497:72: error: no type named ‘type’ in ‘struct boost::spirit::x3::traits::container_value<client::ast::program, void>’
          , typename traits::is_substitute<attribute_type, value_type>::type());
                                                                        ^~~~~~

Things I tried:


Following Getting boost::spirit::qi to use stl containers
Even though it uses Qi I nonetheless tried:

namespace boost{namespace spirit{ namespace traits{
template<>
struct container_value<client::ast::program> 
//also with struct container<client::ast::program, void>
{
      typedef std::list<std::string> type;
};
}}}

You see I'm kinda in the dark, so expectably to no avail.

parser2.cpp:41:8: error: ‘container_value’ is not a class template
 struct container_value<client::ast::program>
        ^~~~~~~~~~~~~~~

In the same SO question I author says
"There is one known limitation though, when you try to use a struct that has a single element that is also a container compilation fails unless you add qi::eps >> ... to your rule."

I did try adding a dummy eps also without success.

Please, help me decipher what that error means.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yup. This looks like another limitation with automatic propagation of attributes when single-element sequences are involved.

I'd probably bite the bullet and change the rule definition from what it is (and what you'd expect to work) to:

x3::rule<class program_, std::vector<std::string> >

That removes the root of the confusion.

Other notes:

  • you had char_ which also eats ';' so the grammar would never succeed because no ';' would follow a "statement".

  • your statements aren't lexeme, so whitespace is discarded (is this what you meant? See Boost spirit skipper issues)

  • your statement could be empty, which meant parsing would ALWAYS fail at the end of input (where it would always read an empty state, and then discover that the expected ';' was missing). Fix it by requiring at least 1 character before accepting a statement.

With some simplifications/style changes:

Live On Coliru

#include <boost/fusion/adapted.hpp>
#include <boost/spirit/home/x3.hpp>
#include <list>

namespace x3 = boost::spirit::x3;

namespace ast {
    using statement = std::string;

    struct program {
        std::list<statement> stmts;
    };
} 

BOOST_FUSION_ADAPT_STRUCT(ast::program, stmts)

namespace grammar {
    auto statement 
        = x3::rule<class statement_, ast::statement> {"statement"}
        = +~x3::char_(';');
    auto program 
        = x3::rule<class program_, std::list<ast::statement> > {"program"}
        = *(statement >> ';');
} 

#include <iostream>
#include <iomanip>

int main() {
    std::cout << "Type an expression...or [q or Q] to quit

";

    using It = std::string::const_iterator;

    for (std::string str; std::getline(std::cin, str);) {
        if (str.empty() || str[0] == 'q' || str[0] == 'Q')
            break;

        auto &parser = grammar::program;
        ast::program program; // Our program (AST)

        It iter = str.begin(), end = str.end();
        if (phrase_parse(iter, end, parser, x3::space, program)) {
            std::cout << "Parsing succeeded
";
            for (auto& s : program.stmts) {
                std::cout << "Statement: " << std::quoted(s, ''') << "
";
            }
        }
        else
            std::cout << "Parsing failed
";

        if (iter != end)
            std::cout << "Remaining unparsed: " << std::quoted(std::string(iter, end), ''') << "
";
    }
}

Which for input "a;b;c;d;" prints:

Parsing succeeded
Statement: 'a'
Statement: 'b'
Statement: 'c'
Statement: 'd'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...