This repository contains a JavaScript compiler implemented using ANTLRv4 and Java. The compiler performs lexical analysis, parsing, semantic analysis, and generates both a parse tree and an abstract syntax tree (AST) for JavaScript code.
- ANTLR Grammar: Utilizes the
hello.g4
file in thesrc
directory to define the JavaScript grammar. - ANTLR Recognizer Generation: Generates ANTLR recognizer classes using the ANTLR tool.
- Lexer and Parser: Utilizes ANTLR-generated lexer and parser for JavaScript grammar.
- Semantic Analysis: Performs semantic analysis to ensure correctness and coherence of the input code.
- Parse Tree and AST Generation: Generates a parse tree and an abstract syntax tree (AST) representing the structure of the JavaScript code.
- Error Handling: Implements robust error handling mechanisms to provide informative messages for syntax and semantic errors.
- Optimizations: (Optional) Includes any optimizations or enhancements applied to the generated AST or code.
- Java Development Kit (JDK): Download and install JDK
- ANTLRv4: Download and install ANTLR
-
Install ANTLR:
# On Unix-based systems (Linux/Mac) cd /usr/local/lib sudo curl -O https://www.antlr.org/download/antlr-4.x-complete.jar export CLASSPATH=".:/usr/local/lib/antlr-4.x-complete.jar:$CLASSPATH" alias antlr4='java -jar /usr/local/lib/antlr-4.x-complete.jar' alias grun='java org.antlr.v4.gui.TestRig'
# On Windows # Download the ANTLR JAR from https://www.antlr.org/download.html # Set up CLASSPATH and alias commands accordingly
-
Generate ANTLR Recognizer:
antlr4 src/hello.g4
-
Compile the generated files:
javac -cp .:antlr-4.x-complete.jar src/*.java
-
Run the compiler:
# Replace 'YourMainClass' with the actual main class of your compiler java -cp .:antlr-4.x-complete.jar src/YourMainClass input.js
# Replace 'YourCompilerExecutable' with the actual name of your compiled executable
./YourCompilerExecutable input.js