Antlr4: ClassCastException on split parser & lexer grammars.

Created on 20 Apr 2015  Â·  3Comments  Â·  Source: antlr/antlr4

I have a relatively simple split parser and lexer grammars which is causing a ClassCastException.

$ grun SomeParser layout
Exception in thread "main" java.lang.ClassCastException: class SomeParser
        at java.lang.Class.asSubclass(Class.java:3404)
        at org.antlr.v4.runtime.misc.TestRig.process(TestRig.java:159)
        at org.antlr.v4.runtime.misc.TestRig.main(TestRig.java:143)

Parser: https://gist.github.com/arcaartem/da1a61a94fda2de9d2ad
Lexer: https://gist.github.com/arcaartem/59c294694a6f0015c3e0

invalid

Most helpful comment

Try grun Some layout
On Apr 20, 2015, at 2:56 PM, Arca Artem [email protected] wrote:

I have a relatively simple split parser and lexer grammars which is causing a ClassCastException.

$ grun SomeParser layout
Exception in thread "main" java.lang.ClassCastException: class SomeParser
at java.lang.Class.asSubclass(Class.java:3404)
at org.antlr.v4.runtime.misc.TestRig.process(TestRig.java:159)
at org.antlr.v4.runtime.misc.TestRig.main(TestRig.java:143)

Parser: https://gist.github.com/arcaartem/da1a61a94fda2de9d2ad
Lexer: https://gist.github.com/arcaartem/59c294694a6f0015c3e0

�
Reply to this email directly or view it on GitHub.

All 3 comments

Try grun Some layout
On Apr 20, 2015, at 2:56 PM, Arca Artem [email protected] wrote:

I have a relatively simple split parser and lexer grammars which is causing a ClassCastException.

$ grun SomeParser layout
Exception in thread "main" java.lang.ClassCastException: class SomeParser
at java.lang.Class.asSubclass(Class.java:3404)
at org.antlr.v4.runtime.misc.TestRig.process(TestRig.java:159)
at org.antlr.v4.runtime.misc.TestRig.main(TestRig.java:143)

Parser: https://gist.github.com/arcaartem/da1a61a94fda2de9d2ad
Lexer: https://gist.github.com/arcaartem/59c294694a6f0015c3e0

�
Reply to this email directly or view it on GitHub.

Yup, that was it. Thanks.

If I may I would like to add a bit of documenting here since I run up against this issue each time I come back to my antlr project.

short version:

change

javac SomeParser*.java

to

javac Some*.java

more....

I know this may be obvious to others but it's not to me (not that it's anyone's fault but my own).

I think the confusion arises because in the book the split parser/lexer example uses import directives but that may be deprecated or at least not recommended based on Sam Harwell's comments. One of the issues is that with an import directive I was seeing a bunch of warnings having to do with "unrecognized constant value". This may or may not be an artifact from relatively new changes to how antlr works - I just don't know nor am I completely up to speed with understanding this warning or what it relates to.

OK, so if one changes to using the options directive this resolves the issue with unrecognized constant values but introduces a necessary change to how one needs to compile the java sources Some* vs SomeParser*.

Makes sense now but before it wasn't clear that there was a SomeLexer.java source file that needed to be compiled. I don't know - just wasn't really paying attn I guess - I'm not specifically executing any antlr on SomeLexer.g4 - I just assumed that the parser was using this file for rule definitions. I guess I now see that those same definitions need to exist somewhere in java.

In addition there is the added confusion (sometimes) of the naming requirements on the files names (and by extension the lexer/parser names) when splitting. Again the book illustrates this a different manner with the names not directly related (CommonLexerRules vs SomeLexer) - SomeLexer being related to the parser complement SomeParser.

This I would suggest might be something that should be changed. I mean I guess I might see why it's a strict requirement but maybe not really. I don't have enough insight on what's going on behind the scenes to know why the name rules are enforced like this. Seems arbitrary although I'm sure it's not - just seems that way.

Thanks for listening and apologies if this isn't the right forum for this note but I keep seeing this in google results so I assume others are as well. I know a project like this is a ton of work and I appreciate all the work that goes into antlr including keeping up with all the questions and comments.

Was this page helpful?
0 / 5 - 0 ratings