Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Scalas/Haskells parser combinators sufficient?

I'm wondering if Scalas/Haskells parser combinators are sufficient for parsing a programming language. More specifically the language MiniJava. I'm currently reading compiller construction and jflex and java cup is quite painful to work with so I'm wondering if I could/should use parser combinators instead. The MiniJava syntax is very small. MiniJavas BNF: http://www.cambridge.org/us/features/052182060X/grammar.html

like image 735
Daniel O Avatar asked Jan 28 '09 22:01

Daniel O


2 Answers

I've never used Scala, but the existence of a definitive BNF makes this easy.

Trivially translated into Haskell's Text.ParserCombinators.Parsec:

goal = do c <- mainClass
          cs <- many classDeclaration
          eof
          return $ c:cs
mainClass = do token "class"
               name <- identifier
               ...

etc. The PArrows translation is pretty trivial too. You'll probably find it easier to have a distinct lexing phase before the parser, but you can do without too.

like image 69
ephemient Avatar answered Nov 06 '22 11:11

ephemient


I'm using Scala's parser combinators to parse PL/SQL code, it works like a charm.

like image 20
Germán Avatar answered Nov 06 '22 12:11

Germán