Skip to content

yusiangeng/recursive-descent-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

test workflow test workflow

Recursive Descent Parser

Parser for a JavaScript-like language. Outputs a JSON abstract syntax tree (AST).

Example

./parse -e 'let x = 2 + 3;'

Output:

{
  "type": "Program",
  "body": [
    {
      "type": "VariableStatement",
      "declarations": [
        {
          "type": "VariableDeclaration",
          "id": {
            "type": "Identifier",
            "name": "x"
          },
          "init": {
            "type": "BinaryExpression",
            "left": {
              "type": "NumericLiteral",
              "value": 2
            },
            "operator": "+",
            "right": {
              "type": "NumericLiteral",
              "value": 3
            }
          }
        }
      ]
    }
  ]
}

Usage

# Enter your program inline
./parse -e '<your program>'

# Read your program from a file
./parse -f path/to/file

Language Features

  • Math

    4 * (5 + 6) / 2;
  • Variable declarations with no explicit typing

    let x, y = 10, z = "hello";
  • Supported types: integer (123), string ("hello"), boolean (true/false), null

  • Assignment

    x = 10;
    y += 2;
  • Block scopes

    let x = 2;
    {
      let y = 3;
    }
  • If-else statements

    if (x > 3) {
      y = 1;
    } else {
      y = 2;
    }
  • Loops

    while (x < 3) {
      x += 1;
    }
    
    for (let i = 0; i < 3; i += 1) {
      x += i;
    }

How to Build

This project uses CMake.

mkdir build && cd build && cmake .. && make

The executable created is build/parse.

Run automated tests

Run build/run-tests. Tests are defined in tests folder.

Tests are also run on GitHub Actions runners using ubuntu-22.04 and macos-14.

Third Party Libraries used

About

Parser for a JavaScript-like language

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published