Lexer and parser generator in Clojure

1,625 views
Skip to first unread message

Alexsandro Soares

unread,
May 18, 2012, 8:46:19 AM5/18/12
to clo...@googlegroups.com
Hi,

     I'm trying to build a compiler using Clojure. Is there any tools
like flex and bison generating Clojure code?
     I'm interested in a lexer/parser in pure Clojure because I think
 in use the same code with Javascript (via ClojureScript) and Java (via Clojure).
I already know isolated tools like Jflex, JavaCup and Bison generating Java and
Jison, JS/CC and friends for Javascript, but I am just interested in a pure Clojure solution.

Thanks in advance for any answer.

Cheers,
Alex

Matt Mitchell

unread,
May 19, 2012, 8:04:05 AM5/19/12
to clo...@googlegroups.com
Here's a parser: https://github.com/joshua-choi/fnparse

Doesn't look like it's active, but could be a starting point?

- Matt

John Szakmeister

unread,
May 19, 2012, 12:58:18 PM5/19/12
to clo...@googlegroups.com
You might want to look at Parsley:
<https://github.com/cgrand/parsley>

-John

Jason Jackson

unread,
May 19, 2012, 3:44:28 PM5/19/12
to clo...@googlegroups.com
I wrote a compiler in Clojure for a 4th year course.

For parsing I used this combinator parser library: https://github.com/jasonjckn/clarsec  which is modeled after haskell's parsec. 
However the performance was kind of bad, combinator parsers in general can be pretty slow. 

In retrospect, I would have tried https://github.com/cgrand/parsley afaik it has ~LR1 performance characteristics. 

Beyond the above options, there's many LR1/LL1 parsers written in Java that are perfectly fine options. If you're parsing with a grammar file, you don't gain anything if the parser was written in Clojure versus Java. 

For lexing, I just used clojure's regular expression implementation. Not sure what libraries exist. 

-Jason

Jason Jackson

unread,
May 19, 2012, 3:52:44 PM5/19/12
to clo...@googlegroups.com
I didn't read the part about clojure/clojurescript interop. 

Also, you could write a parser by hand in clojure[script], which takes a parse table as input. The parse table can be generated with from any language. 

Brent Millare

unread,
May 19, 2012, 4:11:01 PM5/19/12
to clo...@googlegroups.com
I wrote a library on parsing expression grammars (PEGs). Its not completely independent from clojurescript since I use java's regular expressions for the token component, but technically it works on sequences and its easy to rewrite this for js's regexps. Performance hasn't been measured but theres less invocations than a general parsec library. The code is heavily documented and should at least get you started.



On Friday, May 18, 2012 8:46:19 AM UTC-4, Alexsandro Soares wrote:

Christophe Grand

unread,
May 20, 2012, 4:26:47 AM5/20/12
to clo...@googlegroups.com
On Sat, May 19, 2012 at 9:44 PM, Jason Jackson <jaso...@gmail.com> wrote:
In retrospect, I would have tried https://github.com/cgrand/parsley afaik it has ~LR1 performance characteristics. 

Parsley is closer to LR(0) (but I'd like to make it LR(1) using Pager's lane tracing algorithm or IELR) however some ambiguities of LR(0) can be lifted in parsley thanks to the contextual tokenizer.

Christophe

Jason Jackson

unread,
May 20, 2012, 1:01:03 PM5/20/12
to clo...@googlegroups.com
This is a really great project. If you add LR1, you may want to retain ~LR0 as an option. My understanding is most grammars today are designed for LALR1. 

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Lukas Domagala

unread,
May 22, 2012, 9:23:33 AM5/22/12
to clo...@googlegroups.com
you could try https://github.com/Cyrik/clparsec . i´m still working on an alpha release, but it already has all the basic parsec operators
Reply all
Reply to author
Forward
0 new messages