Question

I have only a few skills with haskell and I need help how to implement predictive parsing (LL*) with parsec.

I have context free grammar:

<a> ::= identifier | identifier '(' <args> ')'

Based on http://research.microsoft.com/en-us/um/people/daan/download/parsec/parsec.pdf (chapter predictive parsers) I wrote this code:

term =  do{ x <- m_identifier
    ; try( char '(' )
    ; b <- argsparser
    ; char ')'
    ; return (FncCall x b)
    }
<|> do { x <- m_identifier
    ; return (VarId x)
    }

I expected that this code try to match '(' and if not parser will continue and match only identifier. This code works only for matching identifier '(' args ')'.

With calling it only on identifier "a" it throws:

parse error at (line 1, column 2):
unexpected end of input
expecting letter or digit or "("
Was it helpful?

Solution

all the alternative part should be under try, I think:

term =  try( do{ x <- m_identifier
    ; char '('
    ; b <- argsparser
    ; char ')'
    ; return (FncCall x b)
    } )
<|> do { x <- m_identifier
    ; return (VarId x)
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top