如何在AST中使用Boost Spirit自动规则? [英] How to use Boost Spirit auto rules with AST?
问题描述
我扩展了sehe的示例,以在我要在另一条规则上使用它时显示该问题: http://liveworkspace.org/code/22lxL7 $ 17
I expanded sehe's example to show the problem when I want to use it on another rule: http://liveworkspace.org/code/22lxL7$17
我正在尝试提高Boost Spirit解析器的性能,并且看到自C ++ 11起,就可以使用这样的自动规则:
I'm trying to improve the performances of my Boost Spirit parser and I saw that since C++11, it was possible to use auto-rules like that:
auto comment = "/*" >> *(char_ - "*/") >> "*/";
(或使用BOOST_AUTO或BOOST_SPIRIT_AUTO).
(or with BOOST_AUTO or BOOST_SPIRIT_AUTO).
我有一个这样的规则声明者:
I have a rule declarer like that:
qi::rule<lexer::Iterator, ast::SimpleType()> simple_type;
并定义如下:
simple_type %=
const_
>> lexer.identifier;
如果我用auto声明它,它将编译,但不能在其他规则中用作AST.
If I declare it with auto, it compiles, but it cannot be used as AST in other rules.
是否可以定义使用自动规则创建AST的规则?我也对在Boost Spirit中加快AST创建的其他方式感兴趣.
Is it possible to define rules creating AST with auto rules ? I'm also interested in other ways to speedup AST creation in Boost Spirit.
推荐答案
首先,我尝试了一个简单的示例,并使用简单的适应结构对我有用":
First of all, I tried a simple example and "it works for me" with a simple adapted struct:
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;
};
这很高兴地解析了输入: http://liveworkspace.org/code/22lxL7$1
This happily parses the inputs: http://liveworkspace.org/code/22lxL7$1
我想您可能是说要求属性兼容的地方,并且
I think you might mean where attribute compatibility is required, and
-
attr_cast
(doc)
应该能够帮上大忙.
有关attr_cast(以及一般的属性兼容性)的更多详细信息,请参见以下答案:
See this answer for more details on attr_cast (and attribute compatibility in general): String parser with boost variant recursive wrapper
这篇关于如何在AST中使用Boost Spirit自动规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!