caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jonathan Bryant <jtbryant@valdosta.edu>
To: Jon Harrop <jon@ffconsultancy.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Parsing
Date: Thu, 21 Jun 2007 19:26:37 -0400	[thread overview]
Message-ID: <569CF4E8-052D-4DA7-B75E-2F8DB24469C1@valdosta.edu> (raw)
In-Reply-To: <200706220014.39510.jon@ffconsultancy.com>

It's because with Streams you're doing recursive descent parsing  
which is LL, so rules can't contain left recursion.  It's one of the  
disadvantages of using LL over LALR, such as OCamlYacc (not saying  
that LL doesn't also have it's advantages).

--Jonathan

On Jun 21, 2007, at 7:14 PM, 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:
>
>   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
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs


  reply	other threads:[~2007-06-21 23:26 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 ` Jonathan Bryant [this message]
2007-06-21 23:45 ` [Caml-list] Parsing Christian Stork
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=569CF4E8-052D-4DA7-B75E-2F8DB24469C1@valdosta.edu \
    --to=jtbryant@valdosta.edu \
    --cc=caml-list@yquem.inria.fr \
    --cc=jon@ffconsultancy.com \
    /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).