Closed arcaartem closed 9 years ago
Try grun Some layout On Apr 20, 2015, at 2:56 PM, Arca Artem notifications@github.com 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.
This bites me on the butt every 10 months when I drop back to grun to add to my language. We need a "Hey did you mean XXX instead of XXXParser" to prevent a class exception. It's crystal clear when you rub your nose in it. I have a CSVLexer.g4 and CSVParser.g4, and keep trying grun on CSVParser !DOH! mean to do CSV not CSVParser.
Sorry. yeah, maybe not the clearest design on my part.
My first thought, is ... gee what did I do before ANTLR4. REGEXP hacks in perl and occasional deep dives int lexx and yacc and deciding my regexp hacks were still the way to go. Thanks for a great tool!
Thanks for the info, I was doing the same mistake, putting SomeGrammarParser, because I used two files. Luckly this thread exists and someone had the exact same problem as mine. Maybe, as ross62 said a different error message could help, like: Hey did you mean XXX instead of XXXParser
I have a relatively simple split parser and lexer grammars which is causing a ClassCastException.
Parser: https://gist.github.com/arcaartem/da1a61a94fda2de9d2ad Lexer: https://gist.github.com/arcaartem/59c294694a6f0015c3e0