
Hi! I'm writing a parser for a language, with a BNF like this: Type = "type" Def Def = RecordDef | RecordOfDef ... RecordDef = "record" Body RecordOfDef = "record" "of" With a perser what uses parsec module it can be mapped to haskell easily: structuredTypeDef = recordDef <|> recordOfDef But this way, the recordOfDef case will never be parsed at a line like "type record <somethings>", because recordDef will win in the first place always. Is there an option or an other operation instead of <|> to do the trick? I cant see if the problem can be solved with parsecperm or not. It is a possibility to do something like swap "recordDef <|> recordOfDef" with "recordlikeDefs " where recordlikeDefs = do { reserved "record" ; others } but that would be the dirty way... Do you have any advise on the case? Thanks, -- Zsolt Szalai

On Mar 10, 2008, at 9:41 , Zsolt SZALAI wrote:
It is a possibility to do something like swap "recordDef <|> recordOfDef" with "recordlikeDefs " where recordlikeDefs = do { reserved "record" ; others } but that would be the dirty way...
Er? Refactoring the grammar like that is the clean and preferred way. But if you insist, use the try combinator. structuredTypeDef = try recordDef <|> recordOfDef -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

Er? Refactoring the grammar like that is the clean and preferred way. But if you insist, use the try combinator.
Oh, all right, i was trying to be loyal to the BNF in standard.
structuredTypeDef = try recordDef <|> recordOfDef
-- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH
-- Szalai Zsolt

On Mar 10, 2008, at 9:55 , Zsolt SZALAI wrote:
Er? Refactoring the grammar like that is the clean and preferred way. But if you insist, use the try combinator.
Oh, all right, i was trying to be loyal to the BNF in standard.
BNF doesn't necessarily apply cleanly to all types of parsers. Compare LALR(1) parsers (yacc, happy) to LL(1) (ideal Parsec grammars). You can use the try combinator to write such grammars, but it imposes a potentially large amount of overhead because it may have to hold on to a large parse tree that might end up being thrown out. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH
participants (2)
-
Brandon S. Allbery KF8NH
-
Zsolt SZALAI