如何在 Listener 和 Visitor 之间做出选择?

How to decide between Listener and Visitor?

我使用的是 Visual Studio 下的 Antlr4,C# 版本。效果很好。

据我所知(但我可能错了)听众和访客用于类似的任务。

那么,如何取舍呢?或者我可以同时使用两者吗?请注意,我发现的所有示例都使用 Listener 或 Visitor,但不是两者都使用...

在我看来 he visitor 是一个不错的选择,因为你可以完全控制遍历

这是书中的引述"The Definitive ANTLR 4 Reference"

The biggest difference between the listener and visitor mechanisms is that listener methods are called by the ANTLR-provided walker object, whereas visitor methods must walk their children with explicit visit calls. Forgetting to invoke visit() on a node’s children means those subtrees don’t get visited

如果您将输入翻译成较低级别,例如虚拟机指令,这两种模式都可能有用。