Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsec: Predictive parsing

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 "("
like image 440
312k1t Avatar asked Dec 27 '25 16:12

312k1t


1 Answers

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)
    }
like image 156
CapelliC Avatar answered Dec 30 '25 07:12

CapelliC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!