gpt4 book ai didi

smalltalk - 使用 PetitParser 定义左关联解析器

转载 作者:行者123 更新时间:2023-12-03 01:16:30 24 4
gpt4 key购买 nike

http://pharobooks.gforge.inria.fr/PharoByExampleTwo-Eng/latest/ ,定义了一个ExpressionGrammar。然而,它是右关联的

parser parse: '1 + 2 + 6'.    ======> #(1 $+ #(2 $+ 6))

如何使其左关联,以便

parser parse: '1 + 2 + 6'.

结果

#(#(1 $+ 2) $+ 6)

最佳答案

对于左联想语法,使用:

term := (prod sepratedBy: $+ asParser trim) foldLeft: [ :a :op :b |

...]

对于右联想语法,请使用:

raise := (prod sepratedBy: $^ asParser trim) foldRight: [ :a :op :b |

...]

或者,您可能想查看PPExpressionParser,它会自动为您处理所有详细信息。您只需告诉它哪些运算符是左关联、右关联、前缀或后缀运算符。查看类评论以进行深入讨论。

关于smalltalk - 使用 PetitParser 定义左关联解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14563188/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com