문제

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 "("
도움이 되었습니까?

해결책

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)
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top