Haskell - How to best to represent a programming language's grammar?

后端 未结 5 1081
星月不相逢
星月不相逢 2021-01-30 09:52

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 (pa

5条回答
  •  长发绾君心
    2021-01-30 10:13

    I can't tell from the tone of your question whether this is the first time you're attempting to write a compiler, or if you've written compilers before and are looking for advice specific to Haskell. If you're already a compiler guru, what little advice I have to offer isn't going to help. :)

    Programming language grammars are commonly represented in BNF form, which can be used by tools like Yacc or Bison to parse source code. I don't know if this counts as a Haskell-ian way to do it, but it's the only way that I've heard of. With some digging around you can probably dig up a tool to generate Haskell code from a BNF grammar; I found this tool which claims to be able to do that.

    A quick Google search turned up this BNF grammar for Haskell, and there are probably others out there, in case you want to write a compiler for Haskell (maybe you'd like to write a Haskell compiler in Haskell?) BNF grammars for C and Java seem to be popular.

    Finally, if you're looking for a book about compiler design, the classic text is "The Dragon Book".

提交回复
热议问题