Jin Hc
Jin Hc

Reputation: 25

Antlr4 Win/Ubuntu at org.antlr.v4.gui.TestRig.main(TestRig.java:119)

  1. The version I use is 4.9.2 , on Windows10 and Ubuntu16.04
  2. I used java8 provided in g4
  3. This is my test code Test2.java:
class mytest {
    public void hello() {
        System.out.println("hello1");
    }

    public void hello2() {
        System.out.println("hello2");
        hello();
    }
}

public class Test2 {
    public static void main(final String[] args) throws Exception {
        mytest a = new mytest();
        a.hello();
        a.hello2();
        System.out.println("hello3");
    }
}
  1. I can guarantee that this is the correct code and it can run.
  2. I do this first
java -jar D:\lib\antlr\antlr-4.9.2-complete.jar Java8Lexer.g4
java -jar D:\lib\antlr\antlr-4.9.2-complete.jar Java8Parser.g4
  1. Then I compile them
javac -cp ".;D:\lib\antlr\antlr-4.9.2-complete.jar" Java*.java
  1. None of the above errors
  2. I want to get a tree,so I
javac Test2.java
java -cp ".;D:\lib\antlr\antlr-4.9.2-complete.jar" org.antlr.v4.gui.TestRig Test2 r -tree
  1. But no matter how I try, I get this result
Exception in thread "main" java.lang.ClassCastException: class Test2
        at java.lang.Class.asSubclass(Class.java:3404)
        at org.antlr.v4.gui.TestRig.process(TestRig.java:135)
        at org.antlr.v4.gui.TestRig.main(TestRig.java:119)
  1. I have tried various methods on the Internet but no solution.I also tried it on Ubuntu 16.04, and the same result.
  2. plz help me TT

Upvotes: 1

Views: 311

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170178

Couple of things that are going wrong here: the parameters of org.antlr.v4.gui.TestRig must be the parser name (without the word Parser) and the rule to start with. You have Test2 and r as parameters, but this should be Java8 and compilationUnit. The Test2.java should also be provided entirely.

Try this:

Mac/Linux

java -cp .:antlr-4.9.2-complete.jar org.antlr.v4.gui.TestRig Java8 compilationUnit Test2.java -tree

Windows

java -cp .;antlr-4.9.2-complete.jar org.antlr.v4.gui.TestRig Java8 compilationUnit Test2.java -tree

which prints the following:

(compilationUnit (typeDeclaration (classDeclaration (normalClassDeclaration class mytest (classBody { (classBodyDeclaration (classMemberDeclaration (methodDeclaration (methodModifier public) (methodHeader (result void) (methodDeclarator hello ( ))) (methodBody (block { (blockStatements (blockStatement (statement (statementWithoutTrailingSubstatement (expressionStatement (statementExpression (methodInvocation (typeName (packageOrTypeName System) . out) . println ( (argumentList (expression (assignmentExpression (conditionalExpression (conditionalOrExpression (conditionalAndExpression (inclusiveOrExpression (exclusiveOrExpression (andExpression (equalityExpression (relationalExpression (shiftExpression (additiveExpression (multiplicativeExpression (unaryExpression (unaryExpressionNotPlusMinus (postfixExpression (primary (primaryNoNewArray_lfno_primary (literal "hello1")))))))))))))))))))) ))) ;))))) }))))) (classBodyDeclaration (classMemberDeclaration (methodDeclaration (methodModifier public) (methodHeader (result void) (methodDeclarator hello2 ( ))) (methodBody (block { (blockStatements (blockStatement (statement (statementWithoutTrailingSubstatement (expressionStatement (statementExpression (methodInvocation (typeName (packageOrTypeName System) . out) . println ( (argumentList (expression (assignmentExpression (conditionalExpression (conditionalOrExpression (conditionalAndExpression (inclusiveOrExpression (exclusiveOrExpression (andExpression (equalityExpression (relationalExpression (shiftExpression (additiveExpression (multiplicativeExpression (unaryExpression (unaryExpressionNotPlusMinus (postfixExpression (primary (primaryNoNewArray_lfno_primary (literal "hello2")))))))))))))))))))) ))) ;)))) (blockStatement (statement (statementWithoutTrailingSubstatement (expressionStatement (statementExpression (methodInvocation (methodName hello) ( ))) ;))))) }))))) })))) (typeDeclaration (classDeclaration (normalClassDeclaration (classModifier public) class Test2 (classBody { (classBodyDeclaration (classMemberDeclaration (methodDeclaration (methodModifier public) (methodModifier static) (methodHeader (result void) (methodDeclarator main ( (formalParameterList (lastFormalParameter (formalParameter (variableModifier final) (unannType (unannReferenceType (unannArrayType (unannClassOrInterfaceType (unannClassType_lfno_unannClassOrInterfaceType String)) (dims [ ])))) (variableDeclaratorId args)))) )) (throws_ throws (exceptionTypeList (exceptionType (classType Exception))))) (methodBody (block { (blockStatements (blockStatement (localVariableDeclarationStatement (localVariableDeclaration (unannType (unannReferenceType (unannClassOrInterfaceType (unannClassType_lfno_unannClassOrInterfaceType mytest)))) (variableDeclaratorList (variableDeclarator (variableDeclaratorId a) = (variableInitializer (expression (assignmentExpression (conditionalExpression (conditionalOrExpression (conditionalAndExpression (inclusiveOrExpression (exclusiveOrExpression (andExpression (equalityExpression (relationalExpression (shiftExpression (additiveExpression (multiplicativeExpression (unaryExpression (unaryExpressionNotPlusMinus (postfixExpression (primary (primaryNoNewArray_lfno_primary (classInstanceCreationExpression_lfno_primary new mytest ( )))))))))))))))))))))))) ;)) (blockStatement (statement (statementWithoutTrailingSubstatement (expressionStatement (statementExpression (methodInvocation (typeName a) . hello ( ))) ;)))) (blockStatement (statement (statementWithoutTrailingSubstatement (expressionStatement (statementExpression (methodInvocation (typeName a) . hello2 ( ))) ;)))) (blockStatement (statement (statementWithoutTrailingSubstatement (expressionStatement (statementExpression (methodInvocation (typeName (packageOrTypeName System) . out) . println ( (argumentList (expression (assignmentExpression (conditionalExpression (conditionalOrExpression (conditionalAndExpression (inclusiveOrExpression (exclusiveOrExpression (andExpression (equalityExpression (relationalExpression (shiftExpression (additiveExpression (multiplicativeExpression (unaryExpression (unaryExpressionNotPlusMinus (postfixExpression (primary (primaryNoNewArray_lfno_primary (literal "hello3")))))))))))))))))))) ))) ;))))) }))))) })))) <EOF>)

Upvotes: 1

Related Questions