Parsec: intelligent parsing
I only have a few skills with haskell and need help implementing intelligent parsing (LL *) with parsec.
I have a context free grammar:
<a> ::= identifier | identifier '(' <args> ')'
Based on http://research.microsoft.com/en-us/um/people/daan/download/parsec/parsec.pdf (parsing section 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 this code to try to match '(', and if the parser does not continue and match only id. This code only works for id match '(' args ')'.
When called only by the identifier "a", it produces:
parse error at (line 1, column 2):
unexpected end of input
expecting letter or digit or "("
+3
312k1t
source
to share
1 answer
the whole 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)
}
+6
CapelliC
source
to share