How to use automatic Boost Spirit rules with AST?

EDIT: I extended the example to show the problem when I want to use it according to a different rule: http://liveworkspace.org/code/22lxL7 $ 17

I am trying to improve the performance of my Boost Spirit parser, and I saw that with C ++ 11 you could use these automatic rules:

auto comment = "/*" >> *(char_ - "*/") >> "*/"; 

(or with BOOST_AUTO or BOOST_SPIRIT_AUTO).

I have an ad declaration like:

 qi::rule<lexer::Iterator, ast::SimpleType()> simple_type; 

and is defined as follows:

 simple_type %= const_ >> lexer.identifier; 

If I declare it with auto, it compiles, but it cannot be used as an AST in other rules.

Is it possible to define rules that create an AST with automatic rules? I'm also interested in other ways to speed up the creation of AST in Boost Spirit.

+4
source share
2 answers

First of all, I tried a simple example and โ€œit works for meโ€ with a simple adapted structure:

 struct fixed { int integral; unsigned fractional; }; BOOST_FUSION_ADAPT_STRUCT(fixed, (int, integral)(unsigned, fractional)); template <typename It, typename Skipper = qi::space_type> struct parser : qi::grammar<It, std::vector<fixed>(), Skipper> { parser() : parser::base_type(start) { using namespace qi; BOOST_SPIRIT_AUTO(qi, fixed_rule, lexeme [ int_ >> -('.' >> uint_ | attr(0u)) ]); start = *fixed_rule; BOOST_SPIRIT_DEBUG_NODE(start); } private: qi::rule<It, std::vector<fixed>(), Skipper> start; }; 

It happily parses the input: http://liveworkspace.org/code/22lxL7$1

I think you could understand where attribute compatibility is required, and

should be able to help just fine in these cases.

See this answer for more information on attr_cast (and attribute compatibility in general): String parser with recursive wrapper version boost

+2
source

There is no such thing as an "auto rule". When you automatically record a similar expression, you use all the default settings to create the rule. Thus, the attribute of this "automatic rule" will be exactly and only the attribute of the expression without conversion of the attribute.

If you need to create special attribute data (that is: you need to convert the incoming attribute type to your own data), you must either use a rule or a semantic action.

0
source

All Articles