Parker Browne
Parker Browne

Reputation: 19

How do I run antlr4 on a Java file in the commandline?

I'm trying to use antlr to get the AST of a given java file. I've run antlr4 on both the JavaLexer.g4 and the JavaParser.g4 to generate everything but I'm trying to use grun to see the tree from a java file. I've tried commands like this .\grun JavaLexer tokens HelloWorld.java -gui but get nothing back.

I'm confused on if I need to be calling the JavaLexer, the JavaParser or if I completely have the command wrong in every way. I'm using PowerShell if that matters.

(edit) the correct powershell command is: .\grun Java compilationUnit HelloWorld.java -gui Thanks for the help!

Upvotes: 1

Views: 743

Answers (1)

Mike Cargal
Mike Cargal

Reputation: 6785

Try

.\grun Java compilationUnit -gui < HellowWorld.java 

(not positive re: the syntax of piping HellowWorld.java to lysin in PowerShell)

grun wants the grammarName (Java in your case), then the start rule (compilationUnit for the Java Parser if you want to parse a *.java source file)

Referencing the Lexer grammar name and using tokens as the "start rule" is a special case that only works with the -tokens options to tokenize the input stream and dump the tokens.

Upvotes: 2

Related Questions