Menu

📰
0

Writing more readable parsers in Haskell using keep and skip from elm/parser

DEV Community: haskell·Dwayne Crooks·about 1 month ago
#STya44iK
#dev#succeed#l271#class#code#expr
Reading 0:00
15s threshold

The CPS transformer from Chapter 6 of EOPL3 reuses the LETREC language from Chapter 3 with the addition of multiargument procedures and multideclaration letrec expressions. I already wrote a parser for it when I solved exercise 3.33 so I decided to start writing my implementation of the CPS transformer by basing my implementation on LETREC-3.33 . Before starting on the main task I decided to refactor its parser. I extracted a lexer, Lexer.hs , which simplified the parser alot but then I was left with parsing expressions that worked but felt a bit icky. For e.g. ifExpr :: Parser Expr ifExpr = If <$> ( rIf *> expr ) <*> ( rThen *> expr ) <*> ( rElse *> expr ) In particular, I didn't like the placement of the parentheses and how it grouped the terminals with the expression to the right of it. It didn't match how I thought of the parser doing a left-to-right parse of the string. This made me wonder if thinking in terms of keep and skip from elm/parser would improve the situation.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More