A getting started page that brings you in the world of Antlr.
Antlr - Installation (Version 4)
The grammar file comes from antlr4 getting-started
Create a grammar file called Hello.g4 and define the grammar called Hello
grammar Hello; // The name of the grammar is Hello
r : 'hello' ID ; // The rule/production r match keyword `hello` followed by the rule `ID`
ID : [a-z]+ ; // The rule/production `ID` match all lower-case characters
WS : [ \t\r\n]+ -> skip ; // This WS rule (WhiteSpace) skip spaces, tabs, newlines
where:
with the antlr command line
cd /temp/hellog4
antlr4 Hello.g4 # Run the tool
Next to the grammar file, you got:
ls -A1
Hello.g4
Hello.interp
Hello.tokens
HelloBaseListener.java
HelloLexer.interp
HelloLexer.java
HelloLexer.tokens
HelloListener.java
HelloParser.java
javac -cp "C:\antlr\antlr-4.8-complete.jar" Hello*.java # Compile the generated class
ls -A1 *.class
HelloBaseListener.class
HelloLexer.class
HelloListener.class
'HelloParser$RContext.class'
HelloParser.class
The idea plugin can also run antlr with the Generate Recognizer command
Start grun
grun Hello r -tree
where:
Enter the text to parse and terminate with an End of File Character
hello Nico # Type hello + your name
^Z # End of file (Ctrl+Z)
(r hello nico)
With the minimal grun gui parse tree inspector
grun Hello r -gui
hello nicoooo # Type your text to parse
^Z # send an end of file ( Ctrl+Z - windows or Ctrl-Z linux)
With the idea plugin:
With all import from org.antlr.v4
public class Hello {
public static void main(String[] args) {
// The input
String input = "hello nico";
ANTLRInputStream inputStream = new ANTLRInputStream(input);
// Lexer
HelloLexer lexer = new HelloLexer(inputStream);
// Stream of token
CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
// Create the parser
HelloParser parser = new HelloParser(commonTokenStream);
// Create the tree from the r parser rule
ParseTree tree = parser.r();
// Print the tree
printNode(tree, 0, Arrays.asList(parser.getRuleNames()));
// Or with the utility function to print the LISP-style tree
// equivalent to `grun Hello r --tree`
System.out.println(tree.toStringTree(parser));
}
/**
* Recursive function to print a node
* @param node
* @param level
* @param ruleNames
*/
private static void printNode(ParseTree node, int level, List<String> ruleNames) {
String nodeText = Trees.getNodeText(node, ruleNames);
// Print
StringBuilder line = new StringBuilder();
IntStream.range(0, level).forEach(i -> line.append(" "));
line
.append("Level ")
.append(level)
.append(" - ")
.append(nodeText);
System.out.println(line.toString());
// Chilrdnre
if (node instanceof ParserRuleContext) {
ParserRuleContext parserRuleContext = (ParserRuleContext) node;
if (parserRuleContext.children != null) {
for (ParseTree child : parserRuleContext.children) {
printNode(child, level + 1, ruleNames);
}
}
}
}
}
Level 0 - r
Level 1 - hello
Level 1 - nico
(r hello nico)
when starting grun, you may get the following error:
Can't load Hello as lexer or parser
This is because the lexer and parser generated class file are not in the classpath.
For dos, be sure that the doskey has the current directory (ie .;) in the classpath. Example:
doskey grun=java -cp ".;C:\antlr\antlr-4.8-complete.jar" org.antlr.v4.runtime.misc.TestRig $*