[Haskell-cafe] A very nontrivial parser

Malcolm Wallace Malcolm.Wallace at cs.york.ac.uk
Thu Jul 5 06:03:46 EDT 2007


Andrew Coppin <andrewcoppin at btinternet.com> wrote:

> My goal is to be able to stack multiple parsers one on top of the
> other  - but be able to *change* the stack half way through parsing if
> needed.
> 
> Essentially, I have the "stacked" function, where if I do
> 
>   x <- stacked foo parser1 bar parser2
>   y <- parser3
> 
> then it runs parser2, but it uses parser1 to transform the data first.

I can't help thinking that all you really want to do is parse the same
data twice, through an intermediate representation.  That only requires
you to feed the result of one parse into a top-level call to a different
parser.  For instance:

  this = do
    tmp <- parser1
    x   <- return (runParser parser2 bar tmp)
    y   <- parser3

  ... = runParser this foo input

In the example, 'parser2' takes a different state type, and a different
source token representation from 'parser1' and 'parser3'.  No fearsome
stack type is needed.  :-)

Regards,
    Malcolm


More information about the Haskell-Cafe mailing list