The Wayback Machine - https://web.archive.org/web/20240803051437/https://www.geeksforgeeks.org/abstract-syntax-tree-ast-in-java/
Open In App

Abstract Syntax Tree (AST) in Java

Last Updated : 12 Aug, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

Abstract Syntax Tree is a kind of tree representation of the abstract syntactic structure of source code written in a programming language. Each node of the tree denotes a construct occurring in the source code.

There is numerous importance of AST with application in compilers as abstract syntax trees are data structures widely used in compilers to represent the structure of program code. An AST is usually the result of the syntax analysis phase of a compiler. It often serves as an intermediate representation of the program through several stages that the compiler requires, and has a strong impact on the final output of the compiler.

Let us do discuss the use of AST before proceeding further to the implementation part. AST’s are mainly used in compilers to check code for their accuracy. If the generated tree has errors, the compiler prints an error message. Abstract Syntax Tree (AST) is used because some constructs cannot be represented in context-free grammar, such as implicit typing. They are highly specific to programming languages, but research is underway on universal syntax trees.

Flow Chart:  

id + id * id would have the following syntax tree which is as follows:

Abstract syntax tree will be as follows:

Implementation:

Here we will be writing custom java source codes corresponding to which we will be providing the AST for the same java source code as in implementation.

Example 1(A) Java source code

Java




// Java Custom Source Code
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Print statement
        System.out.println("Hello World!");
    }
}


Example 1(B) AST of above source code

Java




CLASS_DEF -> CLASS_DEF [1:0]
|--MODIFIERS -> MODIFIERS [1:0]
|   `--LITERAL_PUBLIC -> public [1:0]
|--LITERAL_CLASS -> class [1:7]
|--IDENT -> GFG [1:13]
`--OBJBLOCK -> OBJBLOCK [1:17]
    |--LCURLY -> { [1:17]
    |--METHOD_DEF -> METHOD_DEF [2:4]
    |   |--MODIFIERS -> MODIFIERS [2:4]
    |   |   |--LITERAL_PUBLIC -> public [2:4]
    |   |   `--LITERAL_STATIC -> static [2:11]
    |   |--TYPE -> TYPE [2:18]
    |   |   `--LITERAL_VOID -> void [2:18]
    |   |--IDENT -> main [2:23]
    |   |--LPAREN -> ( [2:27]
    |   |--PARAMETERS -> PARAMETERS [2:34]
    |   |   `--PARAMETER_DEF -> PARAMETER_DEF [2:34]
    |   |       |--MODIFIERS -> MODIFIERS [2:34]
    |   |       |--TYPE -> TYPE [2:34]
    |   |       |   `--ARRAY_DECLARATOR -> [ [2:34]
    |   |       |       |--IDENT -> String [2:28]
    |   |       |       `--RBRACK -> ] [2:35]
    |   |       `--IDENT -> args [2:37]
    |   |--RPAREN -> ) [2:41]
    |   `--SLIST -> { [2:43]
    |       |--EXPR -> EXPR [3:26]
    |       |   `--METHOD_CALL -> ( [3:26]
    |       |       |--DOT -> . [3:18]
    |       |       |   |--DOT -> . [3:14]
    |       |       |   |   |--IDENT -> System [3:8]
    |       |       |   |   `--IDENT -> out [3:15]
    |       |       |   `--IDENT -> println [3:19]
    |       |       |--ELIST -> ELIST [3:27]
    |       |       |   `--EXPR -> EXPR [3:27]
    |       |       |       `--STRING_LITERAL -> "Hello World!" [3:27]
    |       |       `--RPAREN -> ) [3:41]
    |       |--SEMI -> ; [3:42]
    |       `--RCURLY -> } [4:4]
    `--RCURLY -> } [5:0]


Now you must be wondering how to make an AST or how the above code is generated for that geek follow the simple steps as listed in the sequential order. 

 checkstyle-8.43-all.jar 
  • Audit the Program with the help of Checkstyle in your Terminal:
java -jar checkstyle-8.43-all.jar -c /google_checks.xml YourFile.java
  • After Audit, Run this command in your terminal to get the AST of your preferred  Code: java -jar checkstyle-8.43-all.jar -t YourFile.java
  • AST is now ready. But wait geeks,

Note: This is not an Updated AST

Remember: To update the AST, we have to do the following two steps

Step 1: We should replace 

">" with "&gt;" and "<" with "&lt;"

Step 2: Remove the code lines

Example 1(C) Updated AST Examples of the above code is as follows:

Java




CLASS_DEF -> CLASS_DEF 
|--MODIFIERS -> MODIFIERS 
|   `--LITERAL_PUBLIC -> public 
|--LITERAL_CLASS -> class 
|--IDENT -> GFG 
`--OBJBLOCK -> OBJBLOCK 
    |--LCURLY -> { 
    |--METHOD_DEF -> METHOD_DEF 
    |   |--MODIFIERS -> MODIFIERS 
    |   |   |--LITERAL_PUBLIC -> public 
    |   |   `--LITERAL_STATIC -> static 
    |   |--TYPE -> TYPE 
    |   |   `--LITERAL_VOID -> void 
    |   |--IDENT -> main 
    |   |--LPAREN -> ( 
    |   |--PARAMETERS -> PARAMETERS 
    |   |   `--PARAMETER_DEF -> PARAMETER_DEF 
    |   |       |--MODIFIERS -> MODIFIERS 
    |   |       |--TYPE -> TYPE 
    |   |       |   `--ARRAY_DECLARATOR -> [ 
    |   |       |       |--IDENT -> String 
    |   |       |       `--RBRACK -> ] 
    |   |       `--IDENT -> args 
    |   |--RPAREN -> ) 
    |   `--SLIST -> { 
    |       |--EXPR -> EXPR 
    |       |   `--METHOD_CALL -> ( 
    |       |       |--DOT -> . 
    |       |       |   |--DOT -> . 
    |       |       |   |   |--IDENT -> System 
    |       |       |   |   `--IDENT -> out 
    |       |       |   `--IDENT -> println
    |       |       |--ELIST -> ELIST 
    |       |       |   `--EXPR -> EXPR 
    |       |       |       `--STRING_LITERAL -> "Hello World!" 
    |       |       `--RPAREN -> ) 
    |       |--SEMI -> ; 
    |       `--RCURLY -> }
    `--RCURLY -> } 


Example 2: Representing 1 + 2 can be represented in AST 

Java




+ BinaryExpression
 - type: +
 - left_value: 
  LiteralExpr:
   value: 1
 - right_vaue:
  LiteralExpr:
   value: 2




Similar Reads

Abstract Syntax Tree vs Parse Tree
Abstract Syntax Trees (ASTs) and Parse Trees (PTs) are two fundamental concepts in computer science and software engineering. Understanding the differences between them is essential for writing efficient and robust code. This blog post will look at both ASTs and PTs and compare their main features. What is an Abstract Syntax Tree? An Abstract Synta
3 min read
Difference Between Abstract Class and Abstract Method in Java
Abstract is the modifier applicable only for methods and classes but not for variables. Even though we don't have implementation still we can declare a method with an abstract modifier. That is abstract methods have only declaration but not implementation. Hence, abstract method declaration should compulsory ends with semicolons. Illustration: publ
3 min read
Parse Tree and Syntax Tree
A parse tree is a visual representation of the syntactic structure of a piece of source code, as produced by a parser. It shows the hierarchy of the elements in the code and the relationships between them. In compiler design, a parse tree is generated by the parser, which is a component of the compiler that processes the source code and checks it f
4 min read
Compiler Design - Variants of Syntax Tree
A syntax tree is a tree in which each leaf node represents an operand, while each inside node represents an operator. The Parse Tree is abbreviated as the syntax tree. The syntax tree is usually used when representing a program in a tree structure. Rules of Constructing a Syntax Tree A syntax tree's nodes can all be performed as data with numerous
6 min read
Difference between Abstract Class and Concrete Class in Java
Abstract Class: An abstract class is a type of class in Java that is declared by the abstract keyword. An abstract class cannot be instantiated directly, i.e. the object of such class cannot be created directly using the new keyword. An abstract class can be instantiated either by a concrete subclass or by defining all the abstract method along wit
5 min read
Why can't static methods be abstract in Java?
In Java, a static method cannot be abstract. Doing so will cause compilation errors.Example: Java Code // Java program to demonstrate // abstract static method import java.io.*; // super-class A abstract class A { // abstract static method func // it has no body abstract static void func(); } // subclass class B class B extends A { // class B must
3 min read
Are All Methods in a Java Interface are Abstract?
In Java, the interface is called a blueprint of a class and it is used to achieve abstraction in java. By using interfaces only we can achieve multiple inheritances in java. Let's understand the concept of blueprint through an example like a blueprint of the building will consist of properties and behaviors of the building but for every building, i
8 min read
Difference between Final and Abstract in Java
In this article, the difference between the abstract class and the final class is discussed. Before getting into the differences, lets first understand what each of this means. Final Class: A class which is declared with the "Final" keyword is known as the final class. The final keyword is used to finalize the implementations of the classes, the me
4 min read
Why Java Interfaces Cannot Have Constructor But Abstract Classes Can Have?
Prerequisite: Interface and Abstract class in Java. A Constructor is a special member function used to initialize the newly created object. It is automatically called when an object of a class is created. Why interfaces can not have the constructor? An Interface is a complete abstraction of class. All data members present in the interface are by de
4 min read
Why a Constructor can not be final, static or abstract in Java?
Prerequisite: Inheritance in Java Constructor in java is a special type of method which is different from normal java methods/ordinary methods. Constructors are used to initialize an object. Automatically a constructor is called when an object of a class is created. It is syntactically similar to a method but it has the same name as its class and a
6 min read
Practice Tags :
three90RightbarBannerImg