我目前正致力于使用Rascal进行Rust-lang解析。最初使用的语法是为Bison制作的,因此我将其翻译为可用。我达到的绊脚石是表达方式。 Rust在其Bison文件中有四种类型的表达式,分别是nonblock_expr
,expr
,nonparen_expr
和expr_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_expression
和Expression "." Path_generic_args_with_colons
之间做出决定。我希望它能够解析Nonblock_prefix_expression
。
答案 0 :(得分:1)
{prefer}
和{avoid}
在Rascal中不存在,但可以使用库进行模拟(不建议)。 import analysis::grammars::Ambiguity;
和diagnose(yourAmbiguousTree)
,它以非常原始的形式解释了同一子串的不同替代解析树之间的差异和共性。差异是歧义的原因:规则应用的规则和顺序,在一个替代中使用但在另一个中不使用。可以通过应用消除歧义过滤器来消除原因,这些过滤器只删除两个备选方案中的一个(因此它们专注于差异)