Git Product home page Git Product logo

parserdemo's Introduction

Parser Demo

A simple(ish) lexer, parser, and interpreter for Scheme style prefix notation maths expressions. (e.g. (+ 1 2 (- 3 4) )).

The parser is written using a simple top-down recursive descent algorithm with backtracking, based on a context-free grammar described below. I have also implemented a visualisation tool to show the step by step process of building the parse tree as a gif (including backtracking).

This was written mainly to help understand the parsing and grammar sections of CS2001 - hence, I have not implemented optimisations like look ahead, as these are not covered in this module.

Roadmap

  • Add negative number and integer division support.
  • Implement more complex grammars.
  • Write a bottom-up table based parser (once it is covered more in lectures).

Visualisation

An example visualisation of the parsing of tokens into an AST:

An example visualisation

Grammar

I defined the grammar as follows:

(Capitalised names are terminal tokens, generated by my lexer).

expression -> OPEN_BRACKET function args CLOSED_BRACKET

function -> ADD | SUBTRACT | MULTIPLY

args -> expression args' | INTEGER args'

args' -> INTEGER args' | expression args' | NIL 

Usage

Dependencies

  • For running the interpreter, no dependencies are needed.
  • For generating visualisations, imagemagick and graphviz are required (available on all good package managers).

Visualizer

To generate a visualization (on Mac and Linux), the run.sh script can be used. This takes in an expression to visualise as the first argument.

For example:

./run.sh '(+ 1 2 3 (* 4 5))'

This will generate graph.gif in the current directory.

Interpreter

The interpreter can be built and ran using Maven.

Note that this project uses Java preview features - the --enable-preview flag is needed when running any code from this project.

./mvnw clean package
java --enable-preview -cp target/parser-demo-1.0-SNAPSHOT.jar com.dewally.niklas.parserdemo.prefixCalc.Interpreter

Bibliography

Some books I found useful when trying to understand this:

  • Johnsonbaugh, Richard - Discrete Mathematics

  • Aho, Kam, Sethi, Ullman - Compilers: Principles, Techniques, and Tools. (chapters 2 and 4)

parserdemo's People

Contributors

niklasdewally avatar

Watchers

 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.