Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Number of rule entries in listener while parsing does not match number while walking #18

Closed
DJCordhose opened this issue Feb 17, 2012 · 8 comments

Comments

@DJCordhose
Copy link

This is illustrated by this test

http://code.google.com/p/antlr4-ast-visitor-sandbox/source/browse/trunk/test/EnterAndExitEveryRuleTest.java

@sharwell
Copy link
Member

That is correct. For some contexts created by a left-recursive rule, no call to enter occurs during the parse phase.

Listeners during the parse phase should be reserved for advanced users who fully understand the nuances of the ANTLR runtime's internals and have a specific requirement to use a listener at that time, as there are many limitations to this method. All other users and for nearly all purposes a listener should only be used with a walker (generally the ParseTreeWalker.DEFAULT) after the parsing is complete.

@sharwell
Copy link
Member

I'm closing this issue since the behavior is known and currently by design - a separate issue could be filed if there are specific requests to change the current behavior.

@parrt parrt reopened this Feb 18, 2012
@parrt
Copy link
Member

parrt commented Feb 18, 2012

I think i'm forced to make a ParserListener that has NO enter events for left-recursive rules, only exits. Might even remove arg from enterX(). Ollie is right. This behavior should/can be captured in the interface.

@sharwell
Copy link
Member

Better to just make Parser.addParseListener protected. There's almost no reason to ever use a parser listener - making the method protected is just leaving the hook in place should it be necessary in some particular (and obviously advanced) application.

It'd even be better to remove this capability completely than add a new interface for what is essentially a crippled version of a feature that works perfectly well when run independently of the parse.

parrt added a commit that referenced this issue Feb 19, 2012
added -parse-listener option, interfaces; fixes #18
@parrt
Copy link
Member

parrt commented Feb 19, 2012

Apparently "fixing" in pull request does NOT close.

@parrt parrt closed this as completed Feb 19, 2012
@DJCordhose
Copy link
Author

Using a parse listener might make sense at least when you need to store information and later use it to direct parse decisions. Like when using a symbol table to decide if something is an id or function or whatever.

@sharwell
Copy link
Member

Writing a parser which manipulates state during the parse process in ways that affect prediction (including the example of a symbol table which is used by predicates) requires a deep understanding of the way ANTLR 4 makes decisions. In particular, ANTLR 4 doesn't perform the same backtracking mechanism as ANTLR 3 and only executes a limited set of traversed predicates during the prediction process for a particular decision. Changing the interface doesn't make understanding this process any easier, but does add to the overall size of generated code and makes it very difficult for someone who does understand how to write a parser listener to share such a listener with the walk phase (java doesn't have multiple inheritance, so assuming you extend FooBaseListener you have to explicitly implement the entire FooParserListener interface on such an object).

@DJCordhose
Copy link
Author

Talking about semantic predicates. Anyway, parse listeners are in place now and things rock :)

parrt pushed a commit that referenced this issue Jun 30, 2015
Added `function` to javascript keywords
parrt pushed a commit that referenced this issue Jun 30, 2015
Support visitor pattern, no impact on existing code I believe
parrt pushed a commit that referenced this issue Jun 30, 2015
parrt pushed a commit that referenced this issue Nov 7, 2016
vector::erase invalidates any iterators at/after the erase position
@ericvergnaud ericvergnaud mentioned this issue Nov 8, 2016
KvanTTT pushed a commit to KvanTTT/antlr4 that referenced this issue Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants