I had the same issues as in this issue: https://github.com/antlr/antlr4/issues/147 and the fix also worked for me.
I then tried to separate the lexer from the parser and I get an exception when I try to run the TestRig.
file HelloL.g4:
// Define a grammar called Hello
lexer grammar HelloL;
Hello : 'hello' ;
ID : [a-z]+ ; // match lower-case identifiers
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines
file HelloP.g4:
// Define a grammar called Hello
parser grammar HelloP;
options { tokenVocab=HelloL; }
r : Hello ID ; // match keyword hello followed by an identifier
I then run these commands:
java -cp .;antlr-4.1-complete.jar org.antlr.v4.Tool HelloL.g4
java -cp .;antlr-4.1-complete.jar org.antlr.v4.Tool HelloP.g4
javac -cp .;antlr-4.1-complete.jar Hello*.java
java -cp .;antlr-4.1-complete.jar org.antlr.v4.runtime.misc.TestRig HelloP r -tree sample.txt
I then get this exception:
Exception in thread "main" java.lang.ClassCastException: class HelloP
at java.lang.Class.asSubclass(Unknown Source)
at org.antlr.v4.runtime.misc.TestRig.process(TestRig.java:159)
at org.antlr.v4.runtime.misc.TestRig.main(TestRig.java:143)
Contents of sample.txt
hello parrt
These are the contents of the folder I work in (the batch files contain basically the commands above - but I also tested it directly in the shell):
Try
java -cp .;antlr-4.1-complete.jar org.antlr.v4.runtime.misc.TestRig Hello r -tree sample.txt
w/o the P.
Ter
I'm afraid it didn't work:
Can't load Hello as lexer or parser
Since I've split the file "Hello.g4" into "HelloL.g4" (lexer) "HelloP.g4" (parser)
there is no combined grammer "Hello" anymore, only a lexer (HelloL) and a parser (HelloP).
oh right. make it HelloLexer and HelloParser as the names of your items, including the filenames. please don't post questions here.