Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Haskell-cafe] concurrent definitions with parsec

2 views
Skip to first unread message

Zsolt SZALAI

unread,
Mar 10, 2008, 9:42:06 AM3/10/08
to haskel...@haskell.org
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

Brandon S. Allbery KF8NH

unread,
Mar 10, 2008, 9:45:46 AM3/10/08
to Zsolt SZALAI, Haskell Cafe

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] all...@kf8nh.com
system administrator [openafs,heimdal,too many hats] all...@ece.cmu.edu
electrical and computer engineering, carnegie mellon university KF8NH


_______________________________________________
Haskell-Cafe mailing list
Haskel...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Zsolt SZALAI

unread,
Mar 10, 2008, 9:55:44 AM3/10/08
to Brandon S. Allbery KF8NH, Haskell Cafe
>
>
> 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] all...@kf8nh.com
> system administrator [openafs,heimdal,too many hats] all...@ece.cmu.edu
> electrical and computer engineering, carnegie mellon university KF8NH
>
>
>


--
Szalai Zsolt

Brandon S. Allbery KF8NH

unread,
Mar 10, 2008, 10:04:03 AM3/10/08
to Zsolt SZALAI, Haskell Cafe

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] all...@kf8nh.com
system administrator [openafs,heimdal,too many hats] all...@ece.cmu.edu
electrical and computer engineering, carnegie mellon university KF8NH

0 new messages