language=Python2 and the visitor interface

304 views
Skip to first unread message

Athanasios Anastasiou

unread,
Aug 14, 2014, 12:09:28 PM8/14/14
to antlr-di...@googlegroups.com
Hello everyone

First of all, a very big thank you for the Python2 target and runtime.

The basic listener interface works fine but I am having some trouble with the visitor and I am wondering if some things operate differently in this target, please see below:

1) I thought that the default behaviour for each visitor's rules is to visit its children (?) The way the abstract [grammar]Visitor is set-up none of its descendants will be called automatically. (There is simply a pass) (?)

2) A look at the super of my visitor's class does not seem to contain anything hinting at visitChildren or visit([sometree]). Should i be carrying out the checks on which rule method to call next myself?

3) I thought about substituting the "pass" of my abstract visitor's methods with calls to ParseTreeVisitor.someMethod (anything hinting towards visit or visitChildren) but ParseTreeVisitor does not seem to have any relevant methods :/


My test case is a very simple grammar that parses nested lists like:
"[1,2,3,4,5,[11,12,13,14,15,[21,22,23,24,25],17],7]"

Ultimately, I would like to have the visitor return a Python object of the same structure rather than having a listener create objects but at the moment, all I want to do is retrieve the numbers (i.e. override just 1 of the two rules in my grammar and have the other one call its standard implementation).

Any ideas?

Looking forward to hearing from you
AA

Jasper Timmer

unread,
Aug 18, 2014, 2:41:53 PM8/18/14
to antlr-di...@googlegroups.com
I just bumped into this same problem on the Python3 target (http://stackoverflow.com/questions/25353601/antlr4-python3-target-visitor-not-usable)

I want to try to modify the string template, but I don't know if I'm doing it right. There are no build instructions in the Python3 repo.
  1. I cloned the antlr4-Python3 repo and the antlr4 repo
  2. modified the python string template in the Python3 repo
  3. copied the tool folder from Python3 over the tool folder in the antlr4 repo
  4. ran mvn package.
Shouldn't that do it?

If i succeed in generating a visit I will create a pull request or post here for discussion.

Op donderdag 14 augustus 2014 18:09:28 UTC+2 schreef Athanasios Anastasiou:

Terence Parr

unread,
Aug 18, 2014, 3:07:05 PM8/18/14
to antlr-di...@googlegroups.com

On Aug 18, 2014, at 11:41 AM, Jasper Timmer <jjwt...@gmail.com> wrote:

> I just bumped into this same problem on the Python3 target (http://stackoverflow.com/questions/25353601/antlr4-python3-target-visitor-not-usable)
>
> I want to try to modify the string template, but I don't know if I'm doing it right. There are no build instructions in the Python3 repo.
> • I cloned the antlr4-Python3 repo and the antlr4 repo
> • modified the python string template in the Python3 repo
> • copied the tool folder from Python3 over the tool folder in the antlr4 repo
> • ran mvn package.
> Shouldn't that do it?

What I do is clone the targets and the antlr4 repository and then type “./bild.py” inside the main directory of antlr4, which polls then all of the targets. that script is just Python.

If it’s just a matter of altering templates, then all you have to do is make a copy of the Python3.stg template and place it in a directory specified first in the class path. that will override the original template.

T

Jasper Timmer

unread,
Aug 18, 2014, 4:52:14 PM8/18/14
to antlr-di...@googlegroups.com
I tried the classpath option but I seem to be doing something wrong, when I run like this:

java -cp /home/jasper/IdeaProjects/Expr/classpath/Python3.stg:.:$CLASSPATH -jar /usr/local/lib/antlr-4.4-complete.jar -visitor -no-listener -o gen -Dlanguage=Python3 Expr.g4

It doesn't look like it's picking up the Python3.stg file. Am I doing it wrong again? :p

Thanks!

Op maandag 18 augustus 2014 21:07:05 UTC+2 schreef the_antlr_guy:

Terence Parr

unread,
Aug 18, 2014, 5:00:25 PM8/18/14
to antlr-di...@googlegroups.com
hi. it needs to be in the proper package/directory structure too :) check out the existing structure and then mirror it somewhere in your path.
Ter
> --
> You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Jasper Timmer

unread,
Aug 18, 2014, 5:20:39 PM8/18/14
to antlr-di...@googlegroups.com
Got it prof, time to hack. Oh no, sleep.

Op maandag 18 augustus 2014 23:00:25 UTC+2 schreef the_antlr_guy:

Athanasios Anastasiou

unread,
Aug 19, 2014, 4:36:47 AM8/19/14
to antlr-di...@googlegroups.com
Hello Jasper

Can I ask what sort of modifications are required to the String Template or are you just experimenting?

I wonder if there is any other way of getting the visitor to work as expected short of handling the dispatching manually (?)

All the best
AA


--
You received this message because you are subscribed to a topic in the Google Groups "antlr-discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/antlr-discussion/Z-5wQqvoST8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to antlr-discussi...@googlegroups.com.

Jasper Timmer

unread,
Aug 20, 2014, 1:33:06 PM8/20/14
to antlr-di...@googlegroups.com
Hi Athanasios,

It seems to me that the visitor isn't implemented in the python implementation, so I'm trying to fix that. I'm not yet succeeding, but I'm fun and learning in the mean while.

Jasper

Op dinsdag 19 augustus 2014 10:36:47 UTC+2 schreef Athanasios Anastasiou:

Jasper Timmer

unread,
Aug 20, 2014, 2:15:43 PM8/20/14
to antlr-di...@googlegroups.com
I now have the calculator example working in Python3. My changes are in a pull request: https://github.com/antlr/antlr4-python3/pull/6

Hope it is a good start to create a good Python(2/3) implementation. I'm open for discussion.

Jasper

Op woensdag 20 augustus 2014 19:33:06 UTC+2 schreef Jasper Timmer:
Reply all
Reply to author
Forward
0 new messages