如何构建解析树?

发布于 2024-12-07 05:09:47 字数 256 浏览 0 评论 0原文

已经找到了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不如归去 2024-12-14 05:09:47

条件语句具有简单的递归结构。相应的递归下降解析器具有类似的简单递归结构。抽象地,内部条件解析如下:

<cond> -> if <expression> then <statement> [else <statement>]

cond :
  a = parse expression
  b = parse statement
  if is_else(token)
    then c = parse statement
         return conditional(a,b,c)
    else return conditional(a,b)

在您的示例中,条件语句包含条件块,其中最后一个包含 else 子句。假设标记化的输入序列具有这种形式,并且在词法分析期间检测到语法错误,则外部条件解析如下:

<conditional> -> selection_statement: {<cond>} <cond>

conditional :
  b = new block
  while (iscond(next))
    s = parse cond
    b = insert(s,b)
  return b

当然,实际的实现会更加详细和繁琐。然而,前面概述了从标记化输入序列构建具有所需形式的条件语句的解析树。

我刚刚意识到你在谈论评估抽象语法树。评估条件语句的函数的结构与解析条件语句的函数类似。抽象地说,

cond(input) :
  a = evaluate(if_part(input))
  if is_true(a)
    then evaluate(then_part(input))
    else if(is_else(input))
           then evaluate(else_part(input))
           else return

为了确定要评估条件的哪一部分,必须首先将条件的“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:

<cond> -> if <expression> then <statement> [else <statement>]

cond :
  a = parse expression
  b = parse statement
  if is_else(token)
    then c = parse statement
         return conditional(a,b,c)
    else return conditional(a,b)

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:

<conditional> -> selection_statement: {<cond>} <cond>

conditional :
  b = new block
  while (iscond(next))
    s = parse cond
    b = insert(s,b)
  return b

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,

cond(input) :
  a = evaluate(if_part(input))
  if is_true(a)
    then evaluate(then_part(input))
    else if(is_else(input))
           then evaluate(else_part(input))
           else return

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.

情深如许 2024-12-14 05:09:47

首先,您需要区分编译器的常用过程:

  1. 词法分析,即识别单词并删除注释;
  2. 解析,即将线性输入流构建为抽象语法树;以及
  3. 评估或代码生成。

先做第一件事,然后你就会明白其余的事情。查看 boost::spirit 的前两个步骤。

First of all, you need to distinguish between the usual passes of a compiler:

  1. The lexing, that is recognizing words and removing comments,
  2. the parsing, that is structuring of the linear input stream into an abstract syntax tree, and
  3. the evaluation or code generation.

Do the first things first, then you'll understand the rest. Look at boost::spirit for the first two steps.

凉城凉梦凉人心 2024-12-14 05:09:47

有多种程序采用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文