简体   繁体   中英

What's the difference between ExitRule and EnterRule in Antrl4?

I've been looking for the difference of ExitRule and EnterRule in Listener in "the definitive antrl4 reference" book but I still don't understand the difference. What is the difference between these two? And how does Listener travel the tree?

simply said, these are auto generated events created by Antrl to keep track of the walker, so to speak.

Imagine, you stand in the middle of a long floor with countless doors on either site. Which you have to open and something in the rooms. To keep track on which doors you've already visited, you mark them with a X and an incrementing number behind.

enterRule == You opens the door of a room.

you get in and search

exitRule == leave the room and paint the X and the next number on the door.

Now you are able to tell exactly which rooms you have already visited, but further more you are able to go to a specific room again for another search, without having to go all the way back and start all over again.

more technically spoken.

Antrl creates an enter and exit method for each Rule that is defined. These Methods, or also known as callbacks , being used by the walker to walk the given tree.

Using a * ParseTreeListener you provide an entry point which indicates the beginning of the tree. For example enterAssign . The Walker looks for this event and triggers it. It then looks for a sub enterRule to trigger this event, and so on...

It keeps walking for as long as it find further enter rules or triggers exitAssign and the walker stops its walk.

Keypoint here is the automated or indipendent walk behavior.

The ParseTreeVisitor on the other hand, will not generating enter/exit Rules to walk a tree. It will generate visitRule instead. The visit methods have to be called explicitly! That means, if you forget to invoke a visit all its children don't get visited at all.

Antrl-Doc --> Parse Tree Listeners

Antrl-Mega-Tutorial --> many Information, short and precise

Tree Walking

Walking starts from a Root-Node and goes down on it until it have found the very left-most nested item. Then goes back up until it reaches the first node and looks for a sub-tree on the right. It enters the right tree if one is found. Or, it gets futher up the chain to find the next node. ...

Until it reaches the Root-Node again. short Picture:

            parent
           |
          /  \
         /    \
     Child1  Child2
      /          
     /
Grandchild

chain of Calls:

enter parent
enter Child1
enter Grandchild
exit Grandchild
exit Child1
enter Child2
exit Child2
exit parent

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM