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

后端 未结 5 1075
星月不相逢
星月不相逢 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:15

    A recursive data type is fine for this. For example, given the language:

    expr ::= var
          |  "true"
          |  "false"
          |  "if" expr "then" expr "else" expr
          |  "(" expr ")"
    

    an example expression in this language would be:

    if true then x else (if false then y else true)
    

    Your Haskell data type would look something like this:

    data Expr = Var String
              | Lit Bool
              | If Expr Expr Expr
    

    Your parser then takes care to translate, e.g., x into Var "x", and true into Lit True, etc. I.e.:

    parse "if x then false else true" 
      ==  If (Var "x") (Lit False) (Lit True)
    

    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.

提交回复
热议问题