A Java mini-compiler for JavaScript do...while loops, featuring lexical and syntax analysis using automata-based methods.
This project was developed for the Compilation module at Abderrahmane Mira University of Béjaia, as part of the final year Bachelor’s in Computer Science.
It demonstrates the front-end stages of a compiler:
- Lexical Analysis (Lexer)
- Syntax Analysis (Parser)
Specifically targets JavaScript do...while loops.
- Character-by-character scanning using a finite automaton
- Breaks code into lexemes and classifies tokens:
keyword,identifier,number,string,operator,delimiter
- Recognizes major JavaScript keywords:
do,while,if,else,for, etc. - Supports custom keywords:
Rezouali,Imane - Ignores comments and whitespace
- Detects and reports lexical errors with line numbers
Validates do...while loops:
do {
<statements>
} while (<condition>);- Checks proper use of braces {}, parentheses (), and semicolons ;
- Validates declarations, assignments, expressions, and operators
- Detects and reports syntax errors with line numbers
- Implemented using a recursive descent parser with finite automata and regular expressions
Simplified grammar for supported JavaScript constructs:
Program -> StatementList EOF
StatementList -> Statement*
Statement -> DoWhile | Declaration | Assignment | OtherStatement
DoWhile -> 'do' Block 'while' '(' Expression ')' ';'
Block -> '{' StatementList '}'
Declaration -> ('var' | 'let' | 'const') Identifier ('=' Expression)? ';'
Assignment -> Identifier '=' Expression ';'
Expression -> Value ((Operator) Value)*
Value -> Identifier | Number | String
Operator -> ArithmeticOperator | ComparisonOperator
Arithmetic operators: +, -, *, /, %
Comparison operators: ==, !=, <, >, <=, >=
Anyone wanting to test this from another machine should follow these steps:
-
Download the project repository to your computer or clone the repo.
-
Ensure Java is installed (Java JDK 8 or higher).
-
Run the executable:
- Option 1: Run via terminal (if you have Java 23 or higher):
java -jar B3_Rezouali_Imane.jar-
Option 2: Building a New JAR (Java 8–22)
- Open a terminal in the project directory.
- Delete any previous
outdirectory (safe):
rm -rf out- Create the output directory:
mkdir -p out- Delete any previous
B3_Rezouali_Imane.jarfile (safe):
rm -f B3_Rezouali_Imane.jar- Compile the source files. The repository places sources under
src/— compile all.javafiles and place class files intoout:
javac -d out src/*.java- Create the JAR file.
jar cfe B3_Rezouali_Imane.jar Main -C out . && java -jar B3_Rezouali_Imane.jarthis project has a Main class in the default package, so use Main:
jar cfe B3_Rezouali_Imane.jar Main -C out .- Run the JAR file:
java -jar B3_Rezouali_Imane.jar- Using the IDE:
- Write, compile, and test JavaScript
do...whileloops. - Click Compile to run lexical and syntax analysis.
- Click Show Tokens to display the tokens generated by the lexer.