bison/yacc 语法消歧
我有以下野牛语法(作为更复杂语法的一部分): <代码>
expression:
IDENTIFIER
| CONST
| LAMBDA match_block
;
match_block:
pattern '=' expression
| match_block '|' pattern '=' expression
;
pattern:
IDENTIFIER
| CONST
;
which describe expressions containing identifiers, constants and lambda-functions with pattern matching, which looks like this:lambda 0 = 1
| 1 = 2
| x = x
Problem is 1 shift/reduce conflict caused by ambiguity with nested matches, as in following example: lambda 0 = 1
| x = lambda 1 = 2
| y = 4
Rule is that match block relate to closest function, as shown with indentation in example above.我的问题是 - 如何重写这个语法来消除这种歧义(不使用 %left %right yacc 指令)?
I have following bison grammar (as part of more complex grammar):
expression: IDENTIFIER | CONST | LAMBDA match_block ; match_block: pattern '=' expression | match_block '|' pattern '=' expression ; pattern: IDENTIFIER | CONST ;
which describe expressions containing identifiers, constants and lambda-functions with pattern matching, which looks like this:
lambda 0 = 1 | 1 = 2 | x = x
Problem is 1 shift/reduce conflict caused by ambiguity with nested matches, as in following example:
lambda 0 = 1 | x = lambda 1 = 2 | y = 4
Rule is that match block relate to closest function, as shown with indentation in example above.
My questions is - how can I rewrite this grammar to eliminate this ambiguity (without using %left %right yacc directives)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您始终希望
|
绑定到最近的LAMBDA
,那基本上意味着您只能在 LAST中拥有
子句:LAMBDA
match_block
的 >|基本上,您将
expression
和match_block
分成两个版本 - 一个允许 lambda,另一个允许 lambda不是这样的——并在每个地方使用适当的一个以避免歧义。If you always want the
|
to bind to the closestLAMBDA
, that basically just means that you can only have aLAMBDA
in the LAST|
clause of amatch_block
:Basically, you split
expression
andmatch_block
into two versions -- one that allows lambdas and one that does not -- and use the appropriate one in each spot to avoid the ambiguity.