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

后端 未结 5 1086
星月不相逢
星月不相逢 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 09:56

    You represent programs using mutually recursive algebraic data types, and to parse programs you use parsing combinators. There are a million flavors; you will find three helpful tutorial papers on the schedule for my class for Monday, March 23, 2009. They are

    • Graham Hutton and Erik Meijer, Functional Pearl: Monadic parsing in Haskell (1998)
    • Graham Hutton, Higher-order Functions for Parsing (1992)
    • Jeroen Fokker, Functional Parsers (1995)

    The Hutton and Meijer paper is the shortest and simplest, but it uses monads, which are not obvious to the amateur. However they have a very nice grammar of and parser for expressions. If you don't grok monads yet, Fokker's tutorial is the one.

提交回复
热议问题