antlr / antlr4

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
http://antlr.org
BSD 3-Clause "New" or "Revised" License
16.95k stars 3.26k forks source link

lexer grammar, import names very particular #560

Open james-q-arnold opened 10 years ago

james-q-arnold commented 10 years ago

I split a grammer into a parser and a lexer. It appears the runtime package is very particular about the selected names. I had a lexer named 'SomeLex' and a parser 'SomeParser'.

$ grun SomeParser r
Exception in thread "main" java.lang.ClassCastException: class SomeParser
    at java.lang.Class.asSubclass(Class.java:3126)
    at org.antlr.v4.runtime.misc.TestRig.process(TestRig.java:159)
    at org.antlr.v4.runtime.misc.TestRig.main(TestRig.java:143)
$ grun Some r
Can't load Some as lexer or parser

Changing the lexer to 'SomeLexer' fixed the problem. By the way, I was using antlr4. The parser used tokenVocab to import the lexer. I would attach the files, but I don't see a way to do that in the github error report. The files were trivial: one rule (r) in the parser, three simple token rules in the lexer.

parrt commented 10 years ago

Hi. That is the intended functionality.

james-q-arnold commented 10 years ago

I suggest adding something to the documentation in this case.

parrt commented 10 years ago

Hi. It's buried on this page:

https://theantlrguy.atlassian.net/wiki/display/ANTLR4/ANTLR+Tool+Command+Line+Options

but in the book.

sharwell commented 10 years ago

Reopening to examine whether the error message could be improved, but no promises that anything will change.

mikecargal commented 1 year ago

This one seems to get me every time I'm away from running TestRig for a while. I end up typing grun BlahParser someRule < infile ( instead of grun Blah someRule < infile) and get this error message. Then I have to go pull up the source and realize that it adds the "Lexer" and "Parser" suffixes trying to find the class.

For split grammars, it's "non-obvious" at best (the source clearly says "BlahParser", I can see generated code for "BlahParser", but I get a ClassCastException for "BlahParser" 🤷).

Consider this another vote for either trying the input name without appending "Parser" or "Lexer", or at least a better error message.

I might even take a crack at it if everyone is open to the idea.