0
我正在Antlr4中編寫語言分析器。我已經用它相當精通,但我不想陷入一個陷阱(再一次),所以在這裏它是:Antlr4:同時處理優先級和左側遞歸
expression
| gate=expression QUESTION
(ifTrue=expression)? COLON
(ifFalse=expression)? # TernaryExpression
| Identifier # IdentifierExpression
| literal # LiteralExpression
| expression logicalComparator expression # LogicalComparisonExpression
| expression logicalOperator expression # LogicalOperationExpression
;
和輸入:
user.field == 'STRING' ? user.field + user.otherField : user.somethingElse
樹我獲得是:
(expression
(expression
(expression user) . (expression field)
)
(logicalComparator = =)
(expression
(expression (literal 'STRING'))
? (expression
(expression
(expression user) . (expression field)
)
(binaryOperator +)
(expression
(expression user) . (expression otherField)
)
)
: (expression
(expression user) . (expression somethingElse)
)
)
)
(邏輯比較,其中左手側是user.field的表達,比較器是==和右手側是三元運算符。)
實際結果應該是三元運算符,其中門表達式是邏輯比較。
我該如何解決?我確信,我將TernaryExpression放在LogicalComparisonExpression上面的事實就足夠了,但顯然它沒有。
一些額外的研究,我發現後: https://github.com/antlr/antlr4/issues/303 https://github.com/antlr/antlr4/issues/268 這似乎表明,該問題出在Antlr上... – Gerino