caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Parsing
@ 2007-06-21 23:14 Jon Harrop
  2007-06-21 23:26 ` [Caml-list] Parsing Jonathan Bryant
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Jon Harrop @ 2007-06-21 23:14 UTC (permalink / raw)
  To: caml-list


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


^ permalink raw reply	[flat|nested] 9+ messages in thread
* [Caml-list] Parsing
@ 2012-02-11  2:07 malc
  2012-02-11  2:25 ` Ashish Agarwal
  2012-02-11 17:41 ` Esther Baruk
  0 siblings, 2 replies; 9+ messages in thread
From: malc @ 2012-02-11  2:07 UTC (permalink / raw)
  To: caml-list

Was the fact that:

        Objective Caml version 3.12.1

# if 0=0then 1else 0;;
- : int = 1

runs at all intended?

-- 
mailto:av1474@comtv.ru

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2012-02-11 17:43 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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).