简体   繁体   English

Antrl4中的ExitRule和EnterRule有什么区别?

[英]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.我一直在"the definitive antrl4 reference"一书中寻找ListenerExitRuleEnterRule的区别,但我仍然不明白其中的区别。 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.简单地说,这些是由 Antrl 创建的自动生成的事件,用于跟踪步行者,可以这么说。

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.要跟踪您已经访问过的门,您可以用X和后面的递增数字标记它们。

enterRule == You opens the door of a room. enterRule == 你打开一个房间的门。

you get in and search你进入并搜索

exitRule == leave the room and paint the X and the next number on the door. exitRule == 离开房间并在门上涂上X和下一个数字。

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. Antrl 为定义的每个规则创建一个进入退出方法。 These Methods, or also known as callbacks , being used by the walker to walk the given tree.这些方法,也称为callbacks ,被 walker 用来遍历给定的树。

Using a * ParseTreeListener you provide an entry point which indicates the beginning of the tree.使用 * ParseTreeListener提供一个入口点,指示树的开头。 For example enterAssign .例如enterAssign The Walker looks for this event and triggers it. Walker 查找此事件并触发它。 It then looks for a sub enterRule to trigger this event, and so on...然后它寻找一个子enterRule来触发这个事件,依此类推……

It keeps walking for as long as it find further enter rules or triggers exitAssign and the walker stops its walk.只要它发现进一步的输入规则或触发exitAssign并且步行者停止步行,它就会一直步行。

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.另一方面, ParseTreeVisitor不会生成进入/退出规则来遍历树。 It will generate visitRule instead.它将生成visitRule 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-Doc --> 解析树监听器

Antrl-Mega-Tutorial --> many Information, short and precise Antrl-Mega-Tutorial --> 大量信息,简短准确

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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