[Haskell-cafe] Parsec - separating Parsing from Lexing

Fernando Henrique Sanches fernandohsanches at gmail.com
Tue Nov 10 14:23:29 EST 2009


Hello.

I'm currently implementing a MicroJava compiler for a college assignment
(the implemented language was defined, the implementation language was of
free choice).

I've sucessfully implemented the lexer using Parsec. It has the type String
-> Parser [MJVal], where MJVal are all the possible tokens.

However, I don't know how to implement the parser, or at least how to do it
keeping it distinguished from the lexer.

For example, the MicroJava grammar specifies:

Program = "program" ident {ConstDecl | VarDecl | ClassDecl}
          "{" {MethodDecl} "}".

The natural solution (for me) would be:

program = do
  string "program"
  programName <- identifier
  ...

However, I can't do this because the file is already tokenized, what I have
is something like:
[Program_, identifier_ "testProgram", lBrace_, ...]
for the example program:

program testProgram {
...

How should I implement the parser separated from the lexer? That is, how
should I parse Tokens instead of Strings in the "Haskell way"?

Fernando Henrique Sanches
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20091110/7bdc8155/attachment.html


More information about the Haskell-Cafe mailing list