Coder Social home page Coder Social logo

ramble's Introduction

Ramble

status CRAN_Status_Badge Travis-CI Build Status Coverage Status

This project is just an example to examine the functional components of R.

Installation

Ramble is now on CRAN:

install.packages("Ramble")

The development version can be installed from github:

# install.packages("devtools")
devtools::install_github("chappers/Ramble")

Goals

Create a parser combinator written in pure R. This is mostly a proof of concept, but could be useful or helpful to someone.

This is inspired by Programming in Haskell by Graham Hutton, and also the JavaScript port, and Python's recursive descent parsing library.

References:

Contributing

You can contribute by opening issues on Github or implementing things yourself and making a pull request.

Please ensure that package passes all checks with --as-cran flag (i.e. via devtools::check(args = c('--as-cran'))) before submitting a pull request.

How it Works

To understand the differences between Ramble and other combinatory parsers please read Ramble: A Parser Combinator in R.

Example

You may view examples for:

  • Parsing xml file
  • Creating a simple calculator
  • Reading a number given in words, and converting it to the appropriate numeric value

Within the examples/* folder. Below is the calculator example.

#' expr :: = term + term | term - term | term
#' term :: = factor * factor | factor / factor | factor
#' factor :: = (expr) | digit+

expr <- ((term %then% 
            symbol("+") %then%
            expr %using% function(x) {
              print(unlist(c(x)))
              return(sum(as.numeric(unlist(c(x))[c(1,3)])))
            }) %alt% 
           (term %then% 
              symbol("-") %then%
              expr %using% function(x) {
                print(unlist(c(x)))
                return(Reduce("-", as.numeric(unlist(c(x))[c(1,3)])))
              }) %alt% term)


term <- ((factor %then% 
             symbol("*") %then%
             term %using% function(x) {
               print(unlist(c(x)))
               return(prod(as.numeric(unlist(c(x))[c(1,3)])))
             }) %alt% 
           (factor %then% 
              symbol("/") %then%
              term %using% function(x) {
                print(unlist(c(x)))
                return(Reduce("/", as.numeric(unlist(c(x))[c(1,3)])))
              }) %alt% factor)

factor <- ((
    symbol("(") %then%
      expr %then%
      symbol(")") %using% 
      function(x){
        print(unlist(c(x)))
        return(as.numeric(unlist(c(x))[2]))
        })
    %alt% natural())

Output:

> expr("(1+1)*2")
[1] "1" "+" "1"
[1] "(" "2" ")"
[1] "2" "*" "2"
[1] "1" "+" "1"
[1] "(" "2" ")"
[1] "2" "*" "2"
[1] "1" "+" "1"
[1] "(" "2" ")"
[1] "2" "*" "2"
$result
[1] 4

$leftover
[1] ""

> expr("(1+2)*3")
[1] "1" "+" "2"
[1] "(" "3" ")"
[1] "3" "*" "3"
[1] "1" "+" "2"
[1] "(" "3" ")"
[1] "3" "*" "3"
[1] "1" "+" "2"
[1] "(" "3" ")"
[1] "3" "*" "3"
$result
[1] 9

$leftover
[1] ""

> expr("1*(2+3)*4*5")
[1] "2" "+" "3"
[1] "(" "5" ")"
[1] "4" "*" "5"
[1] "5"  "*"  "20"
[1] "1"   "*"   "100"
[1] "2" "+" "3"
[1] "(" "5" ")"
[1] "4" "*" "5"
[1] "5"  "*"  "20"
[1] "1"   "*"   "100"
[1] "2" "+" "3"
[1] "(" "5" ")"
[1] "4" "*" "5"
[1] "5"  "*"  "20"
[1] "1"   "*"   "100"
$result
[1] 100

$leftover
[1] ""

> expr("(4-2)+3")
[1] "4" "-" "2"
[1] "(" "2" ")"
[1] "4" "-" "2"
[1] "(" "2" ")"
[1] "4" "-" "2"
[1] "(" "2" ")"
[1] "2" "+" "3"
$result
[1] 5

$leftover
[1] ""

ramble's People

Contributors

8bit-pixies avatar billdenney avatar

Stargazers

Ed Lee avatar

Watchers

James Cloos avatar Ed Lee 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.