Skip to content

Instantly share code, notes, and snippets.

@pooya-raz
Created August 13, 2013 14:32
Show Gist options
  • Save pooya-raz/6221672 to your computer and use it in GitHub Desktop.
Save pooya-raz/6221672 to your computer and use it in GitHub Desktop.
An example of parsing a simple sexp in instaparse
(ns parse.core
(:require [instaparse.core :as insta]))
(def parser
(insta/parser
"sexp = lparen operation rparen
<lparen> = <'('>
<rparen> = <')'>
operation = operator + args
operator = '+'
args = snumber+ ssexp*
<ssexp> = space sexp
<snumber> = space number
<space> = <#'[ ]*'>
number = #'[0-9]+'
"))
(defn choose-op [op]
(case op
"+" +))
(def transform-options
{:number read-string
:args vector
:operator choose-op
:operation apply
:sexp identity
})
(defn lisp [input]
(->> (parser input) (insta/transform transform-options)))
(lisp "(+ 1 2 (+ 3 4))") ;; => 10
@senothe
Copy link

senothe commented Feb 9, 2014

Anyone can tell me why we put a "+" in line9? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment