A mini JavaScript parser using Lex & Yacc. This parser is not production ready version. So it might have potential syntax issue.
It's not full featured, but it is a light version of JavaScript. It supports most used keywords in original JavaScript.
Here is some major keywords the lexer can recognize.
category | keywords |
---|---|
variable declaration | var , let , const |
function declaration | function , => |
loop statements | for , while , do |
conditional statement | if , else , switch , case |
literal | true , false , null , undefined , NaN , Infinity and more |
modern javascript keyword | "use strict"; , async , await |
logical operator | == , === , != , !== , < , > , && , || and more |
comment | // |
You can find the entire keywords in this file.
see this yacc file.
When the parser task ends, it prints debug messages and parse tree as the result.
Here is the sample output
test.js (click to show / hide source)
"use strict";
var a = 1;
const b = 123;
let c;
const str = "Hello"
const template_str = `hello`;
function add() {
return
}
output
This project does not provide compiled version of binary. So you should manually compile the parser to work on your system. The following steps describe how to compile the parser.
Before getting started, a few tools must be installed on your system.
- flex
- bison
- make
- gcc
macOS Installation
# Install bison and flex
brew install bison flex
# Install gcc and make
xcode-select --install
- Clone this repository
git clone https://github.com/Jaewoook/mini-javascript.git
- Run make command
make
- Done!
also, lex and yacc standalone compile script is available.
# compile lex standalone
./lex.sh
# compile yacc standalone
./yacc.sh
⚠️ Note: if you can't run .sh file, run following command:
chmod +x *.sh
./javascript [FILE]
You can test the parser by executing following script. The test script automatically runs all .js
files in the sample directory.
./test.sh
Your contributions are always welcome! 😍 🎉