Fix for dangling else problem

143 views
Skip to first unread message

Aaron Michaux

unread,
Aug 10, 2017, 11:38:15 AM8/10/17
to antlr-discussion
Hi,

Cannot find any principled way to suppress warnings RE the dangling else problem in ANLTR4. In fact, neither "language implementation patterns" nor "The Definitive ANTLR4 Reference" even mention it! Previously posted solutions are no longer supported in the latest version of ANTLR.

I'm curious: can someone knowledgeable give some background for why ANTLR4 doesn't seem to address this issue. I'm sure there is a good reason. If there is a principled way to approach the issue, where one can choose what ANTLR does, and suppress any warnings, because ANTLR knows that you've told it to do the right thing?

Sam Harwell

unread,
Aug 11, 2017, 12:38:41 PM8/11/17
to antlr-di...@googlegroups.com

The Java 8 grammar in one of my branches uses the grammar to handle this case:

https://github.com/sharwell/antlr4/blob/6059bda3c1fc54796b5620ee32b099599331dc79/tool/test/org/antlr/v4/test/Java.g4#L751-L753

 

Then, in order for this grammar to produce a usable parse tree, I implemented the “baseContext” option to allow multiple rules in the grammar to share a single parse tree representation. For example, the ifThenStatement and ifThenElseStatement both produce a IfThenElseStatementContext parse tree node, but only the latter actually allows an else statement to appear. The feature is part of a pull request to ANTLR 4 (antlr/antlr4#697) but not yet accepted. If you need it in the meantime you can try using my fork of the project which includes it.

 

Sam

--
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.

Reply all
Reply to author
Forward
0 new messages