[英]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.