caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jon Harrop <jon@ffconsultancy.com>
To: caml-list@yquem.inria.fr
Subject: Parsing
Date: Fri, 22 Jun 2007 00:14:39 +0100	[thread overview]
Message-ID: <200706220014.39510.jon@ffconsultancy.com> (raw)


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:

  http://www.ffconsultancy.com/ocaml/benefits/parsing.html

# let rec parse_atom = parser
    | [< 'Int n >] -> Num n
    | [< 'Kwd "("; e=parse_expr; 'Kwd ")" >] -> e

  and parse_factor = parser
    | [< e1=parse_atom; stream >] ->
        (parser
         | [< 'Kwd "*"; e2=parse_factor >] -> e1 *: e2
         | [< >] -> e1) stream

  and parse_expr = parser
    | [< e1=parse_factor; stream >] ->
        (parser
         | [< 'Kwd "+"; e2=parse_expr >] -> e1 +: e2
         | [< 'Kwd "-"; e2=parse_expr >] -> e1 -: e2
         | [< >] -> e1) stream;;
val parse_atom : Genlex.token Stream.t -> expr = <fun>
val parse_factor : Genlex.token Stream.t -> expr = <fun>
val parse_expr : Genlex.token Stream.t -> expr = <fun>

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? Could the camlp4 macro 
not factor the pattern match for me?

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

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
The OCaml Journal
http://www.ffconsultancy.com/products/ocaml_journal/?e


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

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-06-21 23:14 Jon Harrop [this message]
2007-06-21 23:26 ` [Caml-list] Parsing Jonathan Bryant
2007-06-21 23:45 ` Christian Stork
2007-06-22  4:03 ` skaller
2007-06-22 10:14 ` Bruno De Fraine
2007-06-24 17:17   ` Jon Harrop

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=200706220014.39510.jon@ffconsultancy.com \
    --to=jon@ffconsultancy.com \
    --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).