A basic semantic analyzer for a simple programming language, implemented in JavaScript. This project demonstrates the semantic analysis phase of a compiler, focusing on variable declaration and usage checks, using a sample Abstract Syntax Tree (AST).
- Detects redeclaration of variables (declaring a variable more than once)
- Detects usage of undeclared variables
- Modular and easy-to-read code
- Designed for educational and demonstration purposes
semantic-analyzer/
├── analyzer.js
├── abstract-syntax-tree.js
├── main.js
├── package.json
analyzer.js
: Contains the semantic analyzer logic.abstract-syntax-tree.js
: Provides a sample AST for testing.main.js
: Entry point for running the analyzer and displaying results.
- Node.js (version 14+ recommended)
Clone the repository or download the source code:
git clone https://github.com/robinsonur/semantic-analyzer-js.git
cd semantic-analyzer-js
Install dependencies (if you add any in the future):
npm install
You can run the analyzer using the included start script:
npm start
Or directly with Node:
node main.js
If there are semantic errors in the AST, you will see a list like:
Semantic analysis errors:
Error: Variable "x" is already declared.
Error: Variable "a" is used before declaration.
If no errors are found:
Semantic analysis successful: No errors found.
-
AST Construction: The sample AST (
abstract-syntax-tree.js
) represents a program after parsing, using plain JavaScript objects for statements and expressions. -
Semantic Analysis: The analyzer (
analyzer.js
) checks for:- Variable redeclaration within the same scope
- Usage of variables before they are declared
-
Execution: The entry point (
main.js
) runs the analyzer on the AST and prints out semantic errors (if any) or a success message.
- To experiment, modify the AST in
abstract-syntax-tree.js
and rerun the project. - You can extend the analyzer to support more semantic checks, type checking, or more complex language features.
Robinson U. Rodríguez GitHub