I'm trying to bootstrap (a subset of) C from scratch, without using extra dependencies (parser generators, libraries, etc.). Also I want to make use of the idea of parser combinators, which is a fantastic technique in functional programming. I would like to borrow this idea from the functional world to procedural C, in a concise and practical way.
I tried to implement some necessary parser combinators for the following toy grammar, which is also an example from the book, Implementing Functional Languages - a tutorial, of Simon Peyton Jones.
greeting -> hg person "!"
hg -> "hello"
| "goodbye"
where person
is any token beginning with a letter. For example, the token list
["goodbye", "James", "!"]
is parsed into
[(("goodbye", "James"), ["!"])]
(The book uses Haskell, and it's hard to make it language-agnostic, but you get the idea :-)
I implemented this in C, and you can view the code here: https://gist.github.com/4451478
This implementation costs 200+ lines of C code, which is far more than the ~20 lines of Haskell as written in the book. So I'm not sure whether I'm on the right track of doing parser combinators in C, and if there's any possible improvements. Any suggestions are welcomed. Thanks in advance.