Reputation: 901
For grammar:
grammar qwe;
query
: COLUMN OPERATOR value EOF
;
COLUMN
: [a-z_]+
;
OPERATOR
: ('='|'>'|'<')
;
SCALAR
: [a-z_]+
;
value
: SCALAR
;
WS : [ \t\r\n]+ -> skip ;
there are identical rules COLUMN
and SCALAR
. Here I was advised to use grun
aliases.
I installed it for my Ubuntu. And for folders structure:
ran this from project learning_antlr4
level:
grun qwe tokens -tokens < qwe/qwe.tokens
The output was empty.
What do I wrong? Where that aliases are saved?
Upvotes: 1
Views: 2879
Reputation: 41
This Code works for Java Maven antlr4
import java.io.IOException;
import org.antlr.v4.gui.Trees;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
public class GrunGui {
public static void main(String[] args) throws IOException {
// input source (can be different in your case)
String inputfilepath = "<your file path>";
// your input type can be different, adjust code according to your input source
CharStream charStreamInput = CharStreams.fromFileName(inputfilepath);
// replace ExampleLexer and ExampleParser with your own lexer and parser
ExampleLexer lexer = new ExampleLexer(charStreamInput);
CommonTokenStream tokens = new CommonTokenStream(lexer);
ExampleParser parser = new ExampleParser(tokens);
// replace YourContext and .yourStartRuleMethod() method according to your own grammar file
ExampleParser.YourContext ctx = parser.yourStartRuleMethod();
Trees.inspect(ctx, parser);
}
}
This code will open grun GUI.
Upvotes: 0
Reputation: 6785
Assuming you have the grun alias set up (if not, see the QuickStart at the tops of this page https://www.antlr.org):
What you want is to view the token stream produced by the Lexer processing your input (not your qwe.tokens
file)
qwe.txt:
total_sales>qwe
ANTLR on master [✘+?]
➜ antlr4 qwe.g4
ANTLR on master [✘+?]
➜ javac *.java
ANTLR on master [✘+?]
➜ grun qwe tokens -tokens < qwe.txt
[@0,0:10='total_sales',<COLUMN>,1:0]
[@1,11:11='>',<OPERATOR>,1:11]
[@2,12:14='qwe',<COLUMN>,1:12]
[@3,15:14='<EOF>',<EOF>,1:15]
AS you can see... both total_sales
and qwe
are recognized as COLUMN
tokens,
Upvotes: 1