简体   繁体   English

在 tree-sitter 语法中,运算符优先级/关联性冲突是否可能导致运行时解析失败?

[英]In a tree-sitter grammar, is it possible for operator precedence/associativity conflict to cause a runtime parse failure?

Consider an infix operator like subset (⊂).考虑像子集(⊂)这样的中缀运算符。 The subset operator is not associative, because its result (a boolean) is not itself a set and so cannot be fed into one or another side of the subset operator.子集运算符不是关联的,因为它的结果(布尔值)本身不是一个集合,因此不能馈送到子集运算符的一侧或另一侧。 Consider:考虑:

S ⊂ T ⊂ M

Ideally this would be a parse failure, but tree-sitter does not seem to allow parse failures based on operator conflict;理想情况下,这将是解析失败,但 tree-sitter 似乎不允许基于运算符冲突的解析失败; instead, it requires you unambiguously resolve the conflict at parser generation time by specifying associativity or precedence.相反,它要求您在解析器生成时通过指定关联性或优先级来明确解决冲突。 Is there any way to indicate to tree-sitter this should be a parse conflict?有没有办法向树保姆表明这应该是解析冲突? Not only for non-associative operators of the same kind, but also between different operators with equivalent precedence which are not associative, like:不仅适用于同类的非关联运算符,还适用于具有相同优先级但不具有关联性的不同运算符之间,例如:

S ⊂ T ⊆ M

Or is the only solution to specify an unambiguous parse, then handle this at the semantic level?或者是唯一指定明确解析,然后在语义级别处理的解决方案?

You are correct this should be handled at the semantic level.您是对的,这应该在语义级别进行处理。 So ⊂ should be marked left-associative in the grammar for parsing purposes, even though it is not.因此 ⊂ 应该在语法中标记为左关联以进行解析,即使它不是。 For the string S ⊂ T ⊂ M it will then be parsed as:对于字符串S ⊂ T ⊂ M ,它将被解析为:

(op ⊂
  (op ⊂
    (id S)
    (id T)
  )
  (id M)
)

At the semantic level you can then add a rule checking whether has any child nodes which are also (or any other operator of equal precedence), which you can surface as an associativity/precedence conflict error.然后,在语义级别,您可以添加一条规则,检查是否有任何也是的子节点(或任何其他具有相同优先级的运算符),您可以将其显示为关联性/优先级冲突错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Atom 包中获取整个 tree-sitter 解析树 - Getting the entire tree-sitter parse tree inside an Atom package 在 tree-sitter 语法中,除了标识符中的保留关键字,我如何匹配字符串? - In a tree-sitter grammar, how do I match strings except for reserved keywords in identifiers? 语法优先与联想 - Grammar Precedence and associativity 在 tree-sitter 语法中,如何以非关联方式匹配非定界值列表? - In a tree-sitter grammar, how do I match a non-delimited list of values in a non-associative way? 不遵循运算符关联性和优先级规则的 BNF 文法是否可以被视为明确文法? - Can a BNF grammar that does not follow operator associativity and precedence rules be considered as an unambiguous grammar? 解析器中的运算符优先级和关联性(Haskell) - Operator precedence and associativity in a parser (Haskell) 语法和运算符关联性之间的关系 - Relation between grammar and operator associativity 通过运算符优先级简化语法 - Simplifying grammar via operator precedence 解析树和语法信息 - Parse tree and grammar information 为什么 1*2+3 被解析为 1*(2+3) 即使没有声明运算符优先级或结合性? - Why 1*2+3 is parsed as 1*(2+3) even though operator precedence or associativity is not declared?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM