Antlr - Parse Tree (AST)

Card Puncher Data Processing

Antlr - Parse Tree (AST)

About

The tree parser is an AST that is created by the parser from a text input.

Management

Print

Grun

with grun, see the -tree of -gui option of

Example:

grun grammar startRule -tree
grun grammar startRule -gui

Type your text and end with a End of File character (Ctrl+Z or Ctrl+D)

Idea

With the idea plugin:

  • right click on the line of the start rule
  • start to type a text to see the tree

Antlr Idea Plugin

Java

Build

Example from the getting started with an Hello.g4 grammar file that was generated in:

  • a lexer file called HelloLexer.java
  • a parser file called HelloParser.java

The grammar below will parse every hello [a-z]+ text.

grammar Hello;
r  : 'hello' ID ;   // Top parser rule of the tree
ID : [a-z]+ ;      
WS : [ \t\r\n]+ -> skip ;

To build a tree, you need to:

ANTLRInputStream input = new ANTLRInputStream("hello nico");
  • Build the lexer from the input
HelloLexer lexer = new HelloLexer(input);
  • Wrap the lexer into a stream of tokens CommonTokenStream (The parser can then read this stream back and forth)
CommonTokenStream tokens = new CommonTokenStream(lexer);
  • Create a parser that will build the tree
HelloParser parser = new HelloParser(tokens);
  • Finally, you can build the tree (a ParseTree) from the parser rule called r (present in the grammar file and generated in the parser as a function)
ParseTree tree = parser.r();    
System.out.println(tree.toStringTree(parser)); // print LISP-style tree





Discover More
Card Puncher Data Processing
Antlr - (Grammar|Lexicon) (g4)

Grammar in the context of Antlr. The grammar definition of Antlr is called a antlr/antlr4/blob/master/doc/lexicon.mdLexicon because the grammar is used by the lexer (hence the lexer grammar) See: ...
Card Puncher Data Processing
Antlr - (Lexical) Rule

in Antlr. Antlr has two types of rule: Name Case Type Description Example from the getting started uppercase letter lexer rule (known as Token name, they defines the token that the lexer...
Card Puncher Data Processing
Antlr - Generated class

From the grammar The lexer rules will create the lexer class The parser rules will create the parser class The classes generated will contain a method for each rule in the grammar. See from...
Idea Antlr Right Click Options
Antlr - Getting Started (Hello World)

A getting started page that brings you in the world of Antlr. antlr/antlr4/blob/master/doc/getting-started.mdantlr4 getting-started Create a grammar file called Hello.g4 and define the grammar...
Idea Antlr Right Click Options
Antlr - Idea Plugin

The Idea plugin is a plugin for Idea that install the Antlr tool. Create a grammar file with the extension g4 and Idea should propose you to install the Antlr...
Card Puncher Data Processing
Antlr - Parse Tree Listener

The parse tree listener (or listener) is a class that implements callback methods that are called by the parser when it creates the parse tree. You can overwrite this class to get information when the...
Card Puncher Data Processing
Antlr - Parse Tree Visitor

The in Antlr that will visit the parse tree in a depth-first search order In a visitor, you can: control the order of visite return data from the function Use mainly when the code is spread around...
Card Puncher Data Processing
Antlr - Parser

in Antlr. The parser is a generated class that is build from the grammar (specifically from the parser rule of the grammar). The job of the parser is to create a parse tree from: a parser start rule...
Card Puncher Data Processing
Antlr - Parser Rule

in Antlr. Parser rule is the second type of rule for Antlr. They begin with a lowercase letter. The lexer rules specify the tokens whereas the parser rules specify the tree. URL URI See ...
Card Puncher Data Processing
Antlr - Start Rule

A start rule is the rule engaged first by the parser to create the tree. Any rule in the grammar can act as a start rule. ...



Share this page:
Follow us:
Task Runner