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
plyparser. - 🏷️ Lexical Analysis – Uses
ply.lexto tokenize JavaScript input (e.g.,ID,VAR,SEMICOLON). - 🧠 Syntax Analysis – Defines grammar rules via
ply.yaccto 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-parserspip install plyEach .py file is a standalone parser for a JavaScript feature. To run:
python <parser_filename>.pyYou 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.pyvar 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.pyconst 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.pysetTimeout(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.pyfor (let i = 0; i < 5; i++) { console.log("Iteration"); }
for (var j = 10; j > 0; j--) { console.log("Countdown"); }