This repository contains a collection of mini-parsers for different JavaScript syntax constructs, implemented using the ply
library in Python. PLY is a Pythonic re-implementation of the popular compiler construction tools lex and yacc.
Parsing is a crucial step in compilers and interpreters where a stream of tokens (produced by a lexer) is analyzed to determine its grammatical structure according to a formal grammar.
This project breaks down the complexity of JavaScript parsing into smaller, manageable components, each focusing on a specific syntax feature.
- 🧩 Modular Design – Each JavaScript construct is handled by a separate
ply
parser. - 🏷️ Lexical Analysis – Uses
ply.lex
to tokenize JavaScript input (e.g.,ID
,VAR
,SEMICOLON
). - 🧠 Syntax Analysis – Defines grammar rules via
ply.yacc
to validate and parse constructs. - 🖥️ Interactive Input – Each script prompts for input, enabling real-time testing of JavaScript snippets.
git clone https://github.com/your_username/javascript-ply-parsers.git
cd javascript-ply-parsers
pip install ply
Each .py
file is a standalone parser for a JavaScript feature. To run:
python <parser_filename>.py
You will be prompted to enter JavaScript code.
Handles basic var
, let
, const
declarations and assignments.
var x;
let y;
const z;
var a = 10;
let b = myVar;
const c = 20;
var x; x = 5;
python var_declaration_assignment.py
var myVariable = 123;
let anotherOne;
const PI = 3.14;
var test; test = 100;
Focuses on literal and constructor-based array declarations.
const myArray = [1, 2, "hello"];
let emptyArray = [];
var newArray = new Array();
let prefilledArray = new Array(1, "test", 3);
python array.py
const numbers = [1, 2, 3];
let names = ["Alice", "Bob"];
var data = new Array();
let mixed = new Array("apple", 123, "banana");
Parses setTimeout
function calls with a callback and delay.
setTimeout(myFunction, 1000);
setTimeout(anotherFunc, 500);
python setTimeout.py
setTimeout(callbackFunction, 2000);
setTimeout(animate, 500);
📌 This parser prints lexed tokens before parsing.
Handles basic for
loops with optional console.log
in the body.
for (let i = 0; i < 10; i++) { console.log("Looping"); }
for (var x = 5; x > 0; x--) { console.log("Count"); }
python forloop.py
for (let i = 0; i < 5; i++) { console.log("Iteration"); }
for (var j = 10; j > 0; j--) { console.log("Countdown"); }