Super-simple recursive-descent parser in Clojure?

351 views
Skip to first unread message

markjszy

unread,
Nov 19, 2013, 4:05:43 PM11/19/13
to clo...@googlegroups.com
Hi everyone,

I have been learning Clojure for a little bit now and had thought about using the language to try to explore compilation/language parsing. I had a lot of trouble getting a recursive-descent parser implementation implemented in the language, and was hoping someone might be able to shed some light on what I'm missing.

So, from what I understand, the simplest of the recursive descent implementations just has you define a bunch of functions, each of which is capable of yielding a production for the left side of a simple (say, LL1 grammar). And then you kick off the parsing process by calling one of the functions, which ends up having all the others called like a domino effect until parsing is complete.

Here's the crux of my problem: in an immutable world, is there any simple manageable way to get that model to work? It seems to me that you can't get around the need for a global, mutable stream that every function is capable of popping items off of. I have this impression because I don't see how else the recursion would work - if my top-level function, A, says "grab the first element and compile program", I expect that by the time it gets execution control back, it should not see anything really left in the stream of parsable tokens - the stream should have been consumed by the cascade of other functions that were called.

I gave up on using refs/atoms, and ended up looking at parser combinators like Parsatron, but in all honesty, they are a little over my head at the moment.

Thanks for any advice on this matter, or any other insight about getting started with topics in compilation!

James Reeves

unread,
Nov 19, 2013, 5:17:07 PM11/19/13
to clo...@googlegroups.com
If anything, parsing is easier to do with immutable structures, as backtracing is trivial.

You don't need a mutable stream of symbols, you just need to have parsing functions with a type signature like:

    tokens -> [ast tokens]

Rather than the function parsing a stream of tokens and returning just the resulting AST, you also return the remaining tokens that were not parsed.

It's actually a lot easier to use this than a mutable structure, as you don't have to worry about pushing tokens back onto the stream if parsing of a group fails.

- James


--
--
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
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages