Giter Club home page Giter Club logo

expression_parser's Introduction

Expression Parser

A toy example of a recursive descent parser and lexer for a small arithmetic expressions language written in Rust. The parser builds a syntax tree and features a simple evaluator that computes the expressions's value by traversing a syntax tree. Example expression: 0 + 8 * 15. I created this to satisfy my curiosity about the language.

Grammar

Tokens

The tokens that the lexer needs to recognize (modulo whitespace).

MulOp := '*' | '/'
AddOp := '+' | '-'
Num := ['0' - '9']+

Productions (left recursive)

This is the base grammar.

Start → Exp
Exp → Exp + Term
Exp → Exp – Term
Exp → Term
Term → Term ∗ Num
Term → Term / Num
Term → Num

Productions (normalized)

We cannot use left-recursive grammars to build a recursive descent parser. So we just can transform the grammar as follows:

Start → Exp
Exp → Term Exp′
Exp′ → '–' Term Exp′
Exp′ → '+' Term Exp′
Exp′ → 𝜖
Term → Num Term′
Term′ → '∗' Num Term′
Term′ → '/' Num Term′
Term′ → 𝜖 

This is the grammar that was used to create the toy example.

Usage

Install Rust and Cargo on your system as described here. Clone this repo, cd to the checkout directory and run cargo test. This should just compile the sources and run the accompanied unit tests.

Unlicense

For license information just read the license file.

expression_parser's People

Contributors

svenkarol 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.