PetitParser中的规则是否分配?
接下来有规则:
integerLiteral --> hexIntegerLiteral / octalIntegerLiteral / decimalIntegerLiteral
hexIntegerLiteral --> hexNumeral , (integerTypeSuffix optional)
octalIntegerLiteral --> octalNumeral , (integerTypeSuffix optional)
decimalIntegerLiteral --> decimalNumeral , (integerTypeSuffix optional)
如果我将它们改为:
integerLiteral --> (hexIntegerLiteral / octalIntegerLiteral / decimalIntegerLiteral) , (integerTypeSuffix optional)
hexIntegerLiteral --> hexNumeral
octalIntegerLiteral --> octalNumeral
decimalIntegerLiteral --> decimalNumeral
然后0777L
不再被解析了。它应与octalNumeral , (integerTypeSuffix optional)
或新版本octalIntegerLiteral , (integerTypeSuffix optional)
匹配,但这不会发生。
答案 0 :(得分:3)
是的,PetitParser中的有序选择是分配式的。在你的例子中有一些上下文缺失,所以我不知道它为什么不适合你。
PetitParser优化器会自动执行您建议的更改。重写规则(稍微更一般的形式)定义为:
PPOptimizer>>#postfixChoice
<optimize>
| before prefix body1 body2 postfix after |
before := PPListPattern any.
prefix := PPPattern any.
body1 := PPListPattern any.
body2 := PPListPattern any.
postfix := PPPattern any.
after := PPListPattern any.
rewriter
replace: before / (prefix , body1) / (prefix , body2) / after
with: before / (prefix , (body1 / body2)) / after.
rewriter
replace: before / (body1 , postfix) / (body2 , postfix) / after
with: before / ((body1 / body2) , postfix) / after