Git Product home page Git Product logo

ast-parser's Introduction

Simple Boolean Expression Parser and UI

This project includes a simple recursive descent parser for boolean expressions, and a React-based user interface to demonstrate the parser in action. The parser is written in JavaScript and parses expressions like 'hello AND world', and the user interface allows you to enter boolean expressions, see the resulting tokens and view the generated abstract syntax tree (AST).

Table of Contents

  1. Introduction
  2. Features
  3. Getting Started
  4. Usage
  5. Demonstration UI
  6. Project Structure
  7. Contributing
  8. License

Introduction

The purpose of this project is to research and explore the theory of parsing, abstract syntax trees and compiler theory. The parser uses a recursive descent parsing algorithm and handles single-line boolean expressions. The user interface is built with React and Material-UI, and visualizes the parsing process in a user-friendly way.

Features

  • Lexical Analysis (Tokenization)
  • Syntax Analysis (Parsing)
  • AST generation
  • Data filtering based on boolean expressions
  • Interactive UI to visualize tokens and AST

Getting Started

To use this project, you'll need to have Node.js and npm installed on your computer. Once you've got that, you can clone the repository and install the dependencies:

git clone https://github.com/lkubicek1/ast-parser.git
cd ast-parser
npm install

Usage

To start the React app, use the start script:

npm start

Parsing

Here's a simple example of how to use the parser:

const { Parser } = require('./Parser');

let parser = new Parser();
let ast = parser.translate('hello AND world');
console.log(ast);

This will output the abstract syntax tree (AST) of the expression:

{
  "type": "program",
  "body": {
    "type": "expr",
    "expression": {
      "type": "boolean_expr",
      "operator": {
        "type": "and_operator",
        "value": "AND"
      },
      "left": {
        "type": "operand",
        "value": "hello"
      },
      "right": {
        "type": "operand",
        "value": "world"
      }
    }
  }
}

Data Filtering

const { SearchInterpreter } = require('./SearchUtil');
const interpreter = new SearchInterpreter();
const data = [
    {"name": 'hello', "age": 1},
    {"name": 'world', "age": 2},
    {"name": 'goodbye', "age": 3},
    {"name": 'goodnight', "age": 4}
];
const filter = interpreter.compile('hello OR world');
const filteredData = data.filter(filter);

This will compile the input query to a filter function and execute the filter:

[
    {"name": 'hello', "age": 1},
    {"name": 'world', "age": 2}
]

Project Structure

The main parts of the project are found in the src/services directory:

  • Tokenizer.js: This file includes the Tokenizer class, which is responsible for breaking the input into tokens.

  • Parser.js: This file includes the Parser class, which is responsible for parsing the tokens into an abstract syntax tree.

  • SearchInterpreter.js: This file includes the SearchInterpreter class, which is responsible for parsing an input query into an AST and interpreting the AST to generate a filter function.

Testing

This project uses Jest for testing. Run npm test to execute the test suite.

Demonstration UI

The React application provides an interactive interface for the parser. The user can input a boolean expression, which is parsed in real-time. The resulting tokens and AST are displayed side-by-side.

You can view a live demo of the application at https://lkubicek1.github.io/ast-parser

Contributing

Contributions to this project are welcome. Please feel free to fork the repository and submit pull requests. If you find any issues, please report them on the GitHub issue tracker.

License

This project is licensed under the Apache 2.0 License - see the LICENSE.md file for details.

ast-parser's People

Contributors

lkubicek1 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.