Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ParseTreeVisitor or ParseTreeListener

Tags:

java

antlr

I am trying to write an AST interpreter / REPL. ANTLRv4 provides two very similar interfaces (ParseTreeVisitor and ParseTreeListener) to walk the parse tree. I cannot seem to find any major differences between them, and the documentation is rather sparse. Is one interface preferable to the other?

like image 692
Joshua Avatar asked Nov 11 '13 23:11

Joshua


1 Answers

The interfaces are used for different purposes. The primary differences are as follows:

ParseTreeListener

  • Provides separate enter/exit methods for before/after the children of a parse tree node are examined.
  • All methods return void. Any values collected for "return" by the listener must be held in fields or elsewhere.
  • Control of which tree nodes are examined is external (via ParseTreeWalker or a derived class).

ParseTreeVisitor

  • Provides one method which is responsible for all analysis/behavior for each parse tree node.
  • Each method returns generic type parameter T, which may be Void if the visitor methods do not return a value.
  • Control of which tree nodes are examined is internal (via visitChildren and/or calls to visit for specific children).
like image 134
Sam Harwell Avatar answered Oct 18 '22 14:10

Sam Harwell