如何构建解析树?
已经找到了 C++ BNF 和下一行
selection-statement:
if ( condition ) statement
if ( condition ) statement else statement
现在正在尝试编写解析器。需要构建解析树。输入时我有 BNF 和源文件。但我陷入了如何向我的解析器指出如果条件评估为true,那么它需要执行第一个语句,否则else块会怎样?谢谢。
Have found C++ BNF and there next lines
selection-statement:
if ( condition ) statement
if ( condition ) statement else statement
Now trying to write parser. Need to build parse tree. On input i have BNF and source file. But i'm stucked in how i can point my parser what if condition evaluated to true, then it need to execute first statement otherwise else block? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
条件语句具有简单的递归结构。相应的递归下降解析器具有类似的简单递归结构。抽象地,内部条件解析如下:
在您的示例中,条件语句包含条件块,其中最后一个包含 else 子句。假设标记化的输入序列具有这种形式,并且在词法分析期间检测到语法错误,则外部条件解析如下:
当然,实际的实现会更加详细和繁琐。然而,前面概述了从标记化输入序列构建具有所需形式的条件语句的解析树。
我刚刚意识到你在谈论评估抽象语法树。评估条件语句的函数的结构与解析条件语句的函数类似。抽象地说,
为了确定要评估条件的哪一部分,必须首先将条件的“if”部分评估为布尔值。如果布尔值为“true”,则评估条件的“then”部分。如果布尔值为“false”,则评估条件的“else”部分。如果没有“其他”部分,就没有什么可评价的。当然,实现上会比上面更详细。
Conditional statements have a simple recursive structure. The corresponding recursive descent parser has a similarly simple recursive structure. Abstractly, the interior conditionals are parsed as follows:
In your example, conditional statements contain blocks of conditionals the last of which contains an else clause. Assuming that the tokenized input sequence has this form and syntactic errors were detected during lexical analysis, the outer conditional is parsed as follows:
Of course, the actual implementation will be significantly more detailed and tedious. However, the preceding describes in outline the construction of a parse tree of a conditional statement having the required form from a tokenized input sequence.
I just realized you were talking about evaluating the abstract syntax tree. The structure of the function that evaluations a conditional statement is similar to the function that parses a conditional statement. Abstractly,
In order to determine which portion of the conditional to evaluate, you must first evalute the "if" part of the conditional to a Boolean value. If the Boolean value is "true," the "then" part of the conditional is evaluated. If the Boolean value is "false," then the "else" part of the conditional is evaluated. If there is no "else" part, there is nothing to evaluate. Of course, the implementation will be more detailed than the above.
首先,您需要区分编译器的常用过程:
先做第一件事,然后你就会明白其余的事情。查看 boost::spirit 的前两个步骤。
First of all, you need to distinguish between the usual passes of a compiler:
Do the first things first, then you'll understand the rest. Look at boost::spirit for the first two steps.
有多种程序采用 BNF 语法并输出正确的解析器: http:// en.wikipedia.org/wiki/Backus-Naur_form#Software_using_BNF
如果您正在编写自己的解析器,则有一个 这里有精彩的在线概述。
There are a variety of programs that take a BNF grammar and output a proper parser: http://en.wikipedia.org/wiki/Backus-Naur_form#Software_using_BNF
If you are writing your own parser, there is an excellent overview online here.