Version française
Home     About     Download     Resources     Contact us    
Browse thread
Parsing
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Christian Stork <caml-list@c...>
Subject: Re: [Caml-list] Parsing
On Fri, Jun 22, 2007 at 12:14:39AM +0100, Jon Harrop wrote:
> 
> I'm not a parser junkie, so please bear with me if this is silly...
> 
> The camlp4 stream parsing syntax extension is a very cool way to write 
> efficient parsers for simple grammars:
... 
> However, you must factor heads in the pattern matches. So instead of:
> 
>   and parse_expr = parser
>     | [< e1=parse_factor; 'Kwd "+"; e2=parse_expr >] -> e1 +: e2
>     | [< e1=parse_factor; 'Kwd "-"; e2=parse_expr >] -> e1 -: e2
>     | [< e1=parse_factor  >] -> e1
> 
> you must write:
> 
>   and parse_expr = parser
>     | [< e1=parse_factor; stream >] ->
>         (parser
>          | [< 'Kwd "+"; e2=parse_expr >] -> e1 +: e2
>          | [< 'Kwd "-"; e2=parse_expr >] -> e1 -: e2
>          | [< >] -> e1) stream
> 
> Why is this? Is it because the streams are destructive? 

Yes.  If you want to look forward non-destructively use Stream.peek or
Stream.npeek.

> Could the camlp4 macro 
> not factor the pattern match for me?
 
Yes, and camlp4 grammars do indeed perform this kind of
left-factorization.

> Are there other parser tools that integrate into the language and do a better 
> job?

Yes, camlp4 grammars.

-- 
Chris Stork   <>  Support eff.org!  <>   http://www.ics.uci.edu/~cstork/
OpenPGP fingerprint:  B08B 602C C806 C492 D069  021E 41F3 8C8D 50F9 CA2F