This repository contains tasks related to grammar implementation using ANTLR.
ANTLR (ANother Tool for Language Recognition) is a powerful parser generator that can be used for a wide range of language processing tasks, including building parsers, interpreters, compilers, and more. It facilitates the creation of grammar-based tools by generating parsers for input languages.
To set up ANTLR, follow these steps:
-
Download ANTLR:
- Download the ANTLR tool folder, named as
antler
in the repo.
- Download the ANTLR tool folder, named as
-
Place ANTLR Folder:
- Once downloaded, extract the contents of the ANTLR folder to a location of your choice. For instance, you might place it in the
D
drive (D:\antlr
).
- Once downloaded, extract the contents of the ANTLR folder to a location of your choice. For instance, you might place it in the
-
Set CLASSPATH System Environment Variable:
- Create a new System Environment Variable named
CLASSPATH
. - Set its value to include the ANTLR JAR file. For example,
.;D:\antlr\antlr-4.13.1-complete.jar
.- This value includes the current directory (
.
) and the path to the ANTLR JAR file.
- This value includes the current directory (
- Create a new System Environment Variable named
-
Update Path System Variable:
- Edit the
Path
System Variable to include:%CLASSPATH%
D:\antlr
These additions allow Java to locate ANTLR classes and resources necessary for executing ANTLR-related commands and programs.
- Edit the
-
Verify Installation:
- To verify that ANTLR is correctly installed, open a command prompt and type
antlr
orjava org.antlr.v4.Tool
. If the installation was successful, you'll see ANTLR's command-line help.
- To verify that ANTLR is correctly installed, open a command prompt and type
To execute Task 1 related to Expr.g4, follow these steps:
-
Navigate to the 'tour' directory:
cd tour
-
Generate the lexer and parser using ANTLR for Expr.g4:
antlr Expr.g4
-
Check the generated Java files for Expr:
dir Expr*.java
-
Compile the generated Java files for Expr:
javac Expr*.java
-
Run the program with the generated parser and a test input file t.expr:
grun Expr prog -gui t.expr
To generate parsers using ANTLR for LabeledExpr.g4, follow these steps:
-
Change directory to
calc
:cd calc
-
Generate parser without listener and with visitor:
antlr -no-listener -visitor LabeledExpr.g4
-
Check generated Java files:
dir LabeledExpr*.java
-
Compile generated files with Calc.java:
javac Calc.java LabeledExpr*.java
-
Execute the program with a test input file t.expr:
java Calc t.expr
Follow these sequential commands to generate parsers using ANTLR for LabeledExpr.g4.