Python2 visitor behaviour

22 views
Skip to first unread message

Athanasios Anastasiou

unread,
Jul 18, 2015, 8:01:12 AM7/18/15
to antlr-di...@googlegroups.com
Hello everyone

This is just to confirm the Visitor behaviour on a Python2 target as I am not sure how (or when) are the children of a node parsed.

Here is the fragment:

literal:number|sstring;
number:NUMBER+;
sstring:SYM_QUOTE .*? SYM_QUOTE;
slist:SYM_SQ_OPEN (literal (SYM_DELIM literal)*) SYM_SQ_CLOSE;

number and sstring are leaf nodes, so I have a very simple return int(ctx.getText()), return ctx.getText() respectively.

But within the slist visiting function, I have a U = self.visitChildren(ctx) in order to process U before I return it as the result of visiting the list node. I expected that U was going to be a list mirroring the contents of ctx.children but now with whatever content is returned from visitng functions called deeper in the tree. Instead, U is empty (None) :(

Is there something I am getting or doing wrong here?


Looking forward to hearing from you
AA

Athanasios Anastasiou

unread,
Jul 21, 2015, 4:14:08 AM7/21/15
to antlr-di...@googlegroups.com
Hello everyone

Any ideas regarding the attached?

Eric Vergnaud

unread,
Jul 21, 2015, 5:19:16 AM7/21/15
to antlr-di...@googlegroups.com
Hi,

visitors and listeners run after parsing, not during parsing.

Eric

Athanasios Anastasiou

unread,
Jul 24, 2015, 9:51:25 AM7/24/15
to antlr-discussion, eric.v...@wanadoo.fr
Hello

Thank you for your response. I appreciate that the visitors and the listeners are running after the parsing, maybe bad use of terminology on mypart but what I am trying to understand is the operation of the existing functions.

At the moment, I am using the following code to visit children prior to processing a node's data:

        U=[]
       
for aChild in ctx.getChildren():
           
if not type(aChild) is antlr4.tree.Tree.TerminalNodeImpl:            
                U
.append(self.visit(aChild))
       
return U        


My assumption though was that this is exactly what visitChildren does. But it doesn't work this way.

So my question is, shall I continue visiting nodes in this way or is there a "proper" use of the visitChildren function?
Reply all
Reply to author
Forward
0 new messages