GithubHelp home page GithubHelp logo

arithmetic-expressions's Introduction

Arithmetic Expression Parser

Simple arithmetic expression parser using Ocaml

Overview

This project uses Ocaml to parse and evaluate basic arithmetic expression. The operator and expression of this arithmetic expression can be defined as

type binop = Add | Sub | Mul | Pow

and

type expression =
    | Num of float
    | Var
    | Binop of binop * expression * expression
    | Neg of expression

where Num n represents the floating-point number n. Var represents the only variable, which is x. Binop (o, e1, e2) represents a binary operation o between expression e1 and expression e2 where o can be either Add, Sub, Mul, or Pow. And Neg e is the negation of expression e (in this parser, we will implement negation as ~ symbol to remove ambiguity in the language while parsing expression string).

For example, we could represent -3.0x^2 + 2.0 as

Binop (Add, Binop (Mul, Binop (Pow, Var, Num 2.0)), Num 2.0)

Parsing and evalutating only support functions with at most one variable x. Including different variables other than x would raise ParseError.

Installation

This project requires opam Ocaml package management and ocaml installed in your local machine. For more information, visit https://ocaml.org/learn/tutorials/up_and_running.html for instruction on how to install opam and ocaml.

Clone this repository to your local machine

git clone https://github.com/b72u68/arithmetic-expressions

Running

In your shell, run the following command

ocaml

to open the Ocaml shell installed in your local machine. In the Ocaml shell, run #use 'expressions.ml';; to import the file and use it.

arithmetic-expressions's People

Contributors

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