[Haskell-beginners] Simple lookahead with Parsec

Brandon S. Allbery KF8NH allbery at ece.cmu.edu
Fri Mar 19 11:21:53 EDT 2010


On Mar 19, 2010, at 11:14 , Derek Thurn wrote:
> Here's a simplified version of my grammar:
>
> myType = try (primitiveType) <|> arrayType
> primitiveType = do {reserved "int"; return "primitive"}
> arrayType = do { primitiveType; symbol "["; symbol "]"; return  
> "array"}

What you're doing here is matching and succeeding on primitiveType and  
never even looking for the bracket; your parser then exits and the  
framework fails expecting the end of the token stream but finding the  
unmatched left bracket.

You need to rearrange your cases:

 > myType = try arrayType <|> primitiveType

so that your parser actually checks for the brackets before concluding  
that it has a primitiveType already.

Even better would be to refactor the grammar since both types start  
with a primitiveType, but I'll leave that up to you since it does  
complicate returning the shape of the resulting type.

-- 
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH


-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/beginners/attachments/20100319/9db80647/PGP.bin


More information about the Beginners mailing list