表达优先和消歧

时间:2017-04-11 14:43:19

标签: expression operator-precedence rascal disambiguation

我目前正致力于使用Rascal进行Rust-lang解析。最初使用的语法是为Bison制作的,因此我将其翻译为可用。我达到的绊脚石是表达方式。 Rust在其Bison文件中有四种类型的表达式,分别是nonblock_exprexprnonparen_exprexpr_nostruct。我现有的翻译充满了含糊之处,但即使在阅读了关于Rascal和Java的主题和可用语法的Rascal文档之后,我也不确定如何解决这个问题。我想解决这些含糊之处,并更好地了解如何进一步解决这个问题。

以下是我的翻译示例:

syntax Expression
    = Literal
    > Path_expression
    | "self"
    | Macro_expression
    | Path_expression "{" Structure_expression_fields "}"
    | Expression "." Path_generic_args_with_colons
    //> left Expression "." Literal_integer
    | Expression "[" Expression? "]"
    | Expression "(" (Expressions ","?)? ")"
    | "(" (Expressions ","?)? ")"
    | "[" Vector_expression "]"
    | "continue"
    | "continue" Identifier
    | "return"
    | "return" Expression
    | "break"
    | "break" Identifier
    > left  ( Expression "*" Expression
            | Expression "/" Expression
            | Expression "%" Expression
            )
    > left  ( Expression "+" Expression
            | Expression "-" Expression
            > Expression "\<\<" Expression
            | Expression "\>\>" Expression
            > Expression "&" Expression
            > Expression "^" Expression
            > Expression "|" Expression
            > Expression "\<" Expression
            | Expression "\>" Expression
            | Expression "\<=" Expression
            | Expression "\>=" Expression
            > Expression "==" Expression
            | Expression "!=" Expression
            > Expression "||" Expression
            > Expression "&&" Expression
            )
    > right Expression "\<-" Expression
    > right ( Expression "=" Expression
            | Expression "\<\<=" Expression
            | Expression "\>\>=" Expression
            | Expression "-=" Expression
            | Expression "&=" Expression
            | Expression "|=" Expression
            | Expression "+=" Expression
            | Expression "*=" Expression
            | Expression "/=" Expression
            | Expression "^=" Expression
            | Expression "%=" Expression
            )
    | Expression ".."
    | Expression ".." Expression
    | ".." Expression
    | ".."
    | Expression "as" Type
    | "box" Nonparen_expression
    > "box" "(" Expression? ")" Expression
    | Expression_qualified_path
    | Block_expression
    | Block
    | Nonblock_prefix_expression
    ;

来源:

Rust Bison文件:Github Rust

Oxidize Rascal文件:Github Oxidize

添加歧义示例 这是一个输入文件的示例(只是文件的模糊部分在解析参数中)。

fn main() {
    let mut config = Config::parse(&flags.build, flags.config.clone());
}

下面看到的输出解析树似乎对前缀表达式(&amp;)的构造感到困惑。这种歧义无法在Nonblock_prefix_expressionExpression "." Path_generic_args_with_colons之间做出决定。我希望它能够解析Nonblock_prefix_expressionenter image description here

1 个答案:

答案 0 :(得分:1)

  • 查看http://www.meta-environment.org/doc/books/syntax/sdf-disambiguation/sdf-disambiguation.pdf有关消除歧义的一般概念以及如何逐步解决歧义问题。本手册是为SDF2编写的,它与Rascal有很多共同之处。不过要注意差异,但是{prefer}{avoid}在Rascal中不存在,但可以使用库进行模拟(不建议)。
  • DrAmbiguity工具可以帮助自动诊断歧义:import analysis::grammars::Ambiguity;diagnose(yourAmbiguousTree),它以非常原始的形式解释了同一子串的不同替代解析树之间的差异和共性。差异是歧义的原因:规则应用的规则和顺序,在一个替代中使用但在另一个中不使用。可以通过应用消除歧义过滤器来消除原因,这些过滤器只删除两个备选方案中的一个(因此它们专注于差异)
  • 如果您对示例输入句子和输出树(尽可能小)有明确的含糊之处,我们可以在stackoverflow.com上提供帮助。