Skip to content

Jaewoook/mini-javascript

Repository files navigation

mini-javascript

A mini JavaScript parser using Lex & Yacc. This parser is not production ready version. So it might have potential syntax issue.

What is mini-JavaScript?

It's not full featured, but it is a light version of JavaScript. It supports most used keywords in original JavaScript.

Supported Keywords

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.

Supported Syntax

see this yacc file.

Parse Result

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

parse output

Getting Started

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.

Prerequisites

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

Installation

  1. Clone this repository
git clone https://github.com/Jaewoook/mini-javascript.git
  1. Run make command
make
  1. 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

Usage

./javascript [FILE]

Test

You can test the parser by executing following script. The test script automatically runs all .js files in the sample directory.

./test.sh

Contributing

Your contributions are always welcome! 😍 🎉

Author

License

MIT License