Javatpoint Logo
Javatpoint Logo

Abstract Syntax Tree (AST) in Java

Abstract Syntax Tree is a computer language's abstract syntactic structure is represented by a type of tree called a tree. A construct that is present in the source code is indicated by each node of the tree.

Typically, an AST is the output of a compiler's syntax analysis stage. It has a significant influence on the compiler's output in the end and frequently acts as an intermediate representation of the program through several phases that the compiler needs.

In compilers, ASTs are mostly used to verify the accuracy of code. A compiler error message is printed if there are errors in the produced tree. Certain constructions, such implicit typing, cannot be represented in context-free language; for this reason, the Abstract Syntax Tree (AST) is used. The study of universal syntax trees is ongoing, though they are extremely specific to programming languages. Usually, the Java Model's ICompilationUnit provides the base for the creation of the AST.

Example:

Generally, the Flow chart for id + id * id would result in the syntax tree that is shown below.

Abstract Syntax Tree (AST) in Java

The following will be the abstract syntax tree:

Abstract Syntax Tree (AST) in Java

Implementation:

FileName: AST.java

In this case, we will be creating unique Java source codes that correspond to the AST for the appropriate Java source code in the implementation.

AST of the above source code:

You must be puzzled now how to create an AST or how the code above is generated for that purpose. To find out, simply follow the easy steps that are stated in the correct order.

  • Launch the source code within your local desktop.
  • Get the Checkstyle Command line utility.

With the use of Checkstyle in your terminal, audit the program:

Run the following command on your terminal after the audit to obtain the AST of the code that you want: Java -jar -t YourFile.java checkstyle-8.43-all.jar

AST is now ready.


Next TopicAliasing in Java





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA