是递归的升压精神语法允许吗? [英] Are recursive boost-spirit grammars allowed?
问题描述
我要写一个解析器数学一类的语言,已经发现了,这将是很好的,有时调用我的精神的语法为前pression的子部分进行解析。
I am about to write a parser for a mathematica-like language and have found out, that it would be nice to sometimes invoke my spirit grammar for subparts of the expression to parse.
即。如果我要解析
a+b*c+d
这将方便调用解析()
上的B * C'部分,而查询+号。
it would be handy to call parse()
on the 'b*c' part while querying the '+' sign.
这是可以做到在使用的相同我的语法的实例吗? (语法参数是'*这个')
Is this possible to do while using the same instance of my grammar? (The grammar parameter would be '*this')
虽然我还没有完全相信这是否是完成这一特定任务的最佳方式,我觉得这个问题非常有趣的,因为我无法找到的文档任何东西。
Though I am not yet fully convinced whether this is the best way to accomplish this particular task, I find the question rather interesting, since I could not find anything in the docs.
Obvously我不应该依赖于类的本地或全局变量,如果我用这个技术。不过,我想知道,如果它主要是由精神的设计允许的。
Obvously I should not depend on class-local or global variables if I used this technique. But I would like to know if it is principally allowed by the design of spirit.
修改
我的语法实例如下所示:
My grammar instances look as follows:
class MyGrammar : public boost::spirit::qi::grammar<...>
{
/* a few rules. Some with local and/or inherited attributes */
MyGrammar( void )
{
/* assign all the rules, use a few 'on_error' statements */
// In one or two rules I would like to invoke parse(...,*this,...)
// on a subrange of the expression
}
}
谢谢!
推荐答案
当然,您可以:
// In one or two rules I would like to invoke parse(...,*this,...)
// on a subrange of the expression
^这不是规则是如何在声明语法组成。你似乎在程序上想的(这可能表明你可以有previous的写作经验递归下降解析器?)。
^ That is not how rules are composed in a declarative grammar. You seem to think of this in procedural terms (which may indicate you could have previous experience writing recursive-descent parsers?).
我就放心了顶部的精神简单的前pression语法可能看起来像这样:
Off the top of my mind a simple expression grammar in spirit could look like this:
literal = +qi::int_;
variable = lexeme [ qi::alpha >> *qi::alnum ];
term = literal
| variable
| (qi::lit('(') > expression >> ')');
factor = term >> *(qi::char_("+-") >> term);
expression = factor >> *(qi::char_("*/%") >> term);
请注意在词
的最后一个分支递归:它解析器括号前pressions
Note the recursion in the last branch of term
: it parsers parenthesized expressions.
这个简单的示例实际上并不会导致解析树,反映了运营商precedence。但在精神库中的样本和测试包含 多的的例子做到这一点。
This simplistic sample won't actually result in a parse tree that reflects operator precedence. But the samples and tests in the Spirit library contain many examples that do.
请参阅说明其工作原理更详细(全样本)我的也有其他的答案:
See also other answers of mine that show how this works in more detail (with full samples):
-
<一个href=\"http://stackoverflow.com/questions/8464969/boostspirit-ex$p$pssion-parser/8468822#8468822\">Boost::Spirit防爆pression分析器
的由提问者链接文档样本和原来的code改进解释的睡例如的
<一个href=\"http://stackoverflow.com/questions/8706356/boolean-ex$p$pssion-grammar-parser-in-c/8707598#8707598\">Boolean前pression(语法)分析器在C ++中
<一个href=\"http://stackoverflow.com/questions/9453712/compilation-error-with-a-boostspirit-parser/9458029#9458029\">Compilation误差一个boost ::精神解析器的另一种方法
希望帮助
这篇关于是递归的升压精神语法允许吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!