caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Christian Stork <caml-list@cstork.org>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Parsing
Date: Thu, 21 Jun 2007 16:45:37 -0700	[thread overview]
Message-ID: <20070621234537.GA1420@stirner.roentgeninstitut.de> (raw)
In-Reply-To: <200706220014.39510.jon@ffconsultancy.com>

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


  parent reply	other threads:[~2007-06-21 23:45 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-06-21 23:14 Parsing Jon Harrop
2007-06-21 23:26 ` [Caml-list] Parsing Jonathan Bryant
2007-06-21 23:45 ` Christian Stork [this message]
2007-06-22  4:03 ` skaller
2007-06-22 10:14 ` Bruno De Fraine
2007-06-24 17:17   ` Jon Harrop
2012-02-11  2:07 malc
2012-02-11  2:25 ` Ashish Agarwal
2012-02-11 17:41 ` Esther Baruk

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20070621234537.GA1420@stirner.roentgeninstitut.de \
    --to=caml-list@cstork.org \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).