Antlr4 Win/Ubuntu 在 org.antlr.v4.gui.TestRig.main(TestRig.java:119)
Antlr4 Win/Ubuntu at org.antlr.v4.gui.TestRig.main(TestRig.java:119)
- 我使用的版本是 4.9.2 ,在 Windows10 和 Ubuntu16.04
- 我使用了g4
中提供的java8
- 这是我的测试代码
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");
}
}
- 我可以保证这是正确的代码并且可以运行。
- 我先做这个
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
- 然后我编译它们
javac -cp ".;D:\lib\antlr\antlr-4.9.2-complete.jar" Java*.java
- None 以上错误
- 我想要一棵树,所以我
javac Test2.java
java -cp ".;D:\lib\antlr\antlr-4.9.2-complete.jar" org.antlr.v4.gui.TestRig Test2 r -tree
- 但无论我怎么尝试,我得到的结果是这样
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)
- 网上试了各种方法都没有solution.I也在Ubuntu16.04上试过,结果一样。
- 请大家帮我TT
这里有几处错误:org.antlr.v4.gui.TestRig
的参数必须是解析器名称(没有单词 Parser
)和开始的规则。您有 Test2
和 r
作为参数,但这应该是 Java8
和 compilationUnit
。 Test2.java
也应该完整提供。
试试这个:
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
打印以下内容:
(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>)
- 我使用的版本是 4.9.2 ,在 Windows10 和 Ubuntu16.04
- 我使用了g4 中提供的java8
- 这是我的测试代码
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");
}
}
- 我可以保证这是正确的代码并且可以运行。
- 我先做这个
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
- 然后我编译它们
javac -cp ".;D:\lib\antlr\antlr-4.9.2-complete.jar" Java*.java
- None 以上错误
- 我想要一棵树,所以我
javac Test2.java
java -cp ".;D:\lib\antlr\antlr-4.9.2-complete.jar" org.antlr.v4.gui.TestRig Test2 r -tree
- 但无论我怎么尝试,我得到的结果是这样
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)
- 网上试了各种方法都没有solution.I也在Ubuntu16.04上试过,结果一样。
- 请大家帮我TT
这里有几处错误:org.antlr.v4.gui.TestRig
的参数必须是解析器名称(没有单词 Parser
)和开始的规则。您有 Test2
和 r
作为参数,但这应该是 Java8
和 compilationUnit
。 Test2.java
也应该完整提供。
试试这个:
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
打印以下内容:
(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>)