I’ve been looking at Haskell and I’d quite like to write a compiler in it (as a learning exercise), since a lot of its innate features can be readily applied to a compiler (particularly a recursive descent compiler).
What I can’t quite get my head around is how to represent a language’s grammar in a Haskell-ian way. My first thought was to use recursive data type definitions, but I can’t see how I use them to match against keywords in the language (‘if’) for example.
Thoughts and suggestions greatly appreciated,
Pete
A recursive data type is fine for this. For example, given the language:
an example expression in this language would be:
Your Haskell data type would look something like this:
Your parser then takes care to translate, e.g.,
xintoVar 'x', andtrueintoLit True, etc. I.e.:For writing parsers you can roll your own using the techniques mentioned in Norman’s answer, or using Parsec or use parser generators like Happy.