3

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 "("
312k1t
  • 269
  • 1
  • 4
  • 10
  • 1
    I don't believe you need `try` or LL(*) for this. `identifier` is a common prefix of both productions, so parse an `identifier` and optionally parse parens and the function arguments. Return an identifier if the function suffix fails. – stephen tetley Mar 08 '12 at 16:30

1 Answers1

6

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)
    }
dave4420
  • 46,404
  • 6
  • 118
  • 152
CapelliC
  • 59,646
  • 5
  • 47
  • 90