From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: alex@barettadeit.com
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Parser combinators
Date: Mon, 04 Apr 2005 09:40:49 +0900 (JST) [thread overview]
Message-ID: <20050404.094049.35417841.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <42501740.1040802@barettadeit.com>
From: Alex Baretta <alex@barettadeit.com>
> > Interestingly, your example corresponds exactly to the one in the
> > ocaml tutorial, where it is solved using stream parsers.
> > Stream parsers are a bit more involved than just writing yacc rules,
> > but they give you more control on how to combine rules (you can write
> > parser combinators.) And they are completely integrated in the
> > language using camlp4.
>
> Er.. Excuse me for sticking my nose into this, but I think I read
> something interesting. Parser combinators? What do you mean? I'm quite
> sure I have not seen any operators acting on stream parsers, at least if
> by stream parsers you mean the LL1 based on pa_op.cmo camlp4 module.
The point is that you can define them yourself.
For instance here is the star operator.
let rec star ?(acc=[]) p = parser
[< x = p ; s >] -> star ~acc:(x::acc) p s
| [< >] -> List.rev acc
More concretely, here is another version of expression parsing, using
functionals.
type expr =
Num of int
| Var of string
| Plus of expr * expr
| Mult of expr * expr
open Genlex
let rec accumulate parse accu = parser
| [< e = parse accu; s >] -> accumulate parse e s
| [< >] -> accu
(* val accumulate : ('a -> Genlex.token Stream.t -> 'a) ->
'a -> Genlex.token Stream.t -> 'a *)
let left_assoc parse op wrap =
let parse' accu =
parser [< 'Kwd k when k = op; s >] -> wrap accu (parse s) in
parser [< e1 = parse; e2 = accumulate parse' e1 >] -> e2
(* val left_assoc : (Genlex.token Stream.t -> 'a) ->
string -> ('a -> 'a -> 'a) -> Genlex.token Stream.t -> 'a *)
let rec parse_simple = parser
| [< 'Int n >] -> Num n
| [< 'Ident x >] -> Var x
| [< 'Kwd"("; e = parse_expr; 'Kwd")" >] -> e
and parse_mult s =
left_assoc parse_simple "*" (fun e1 e2 -> Mult(e1,e2)) s
and parse_expr s =
left_assoc parse_mult "+" (fun e1 e2 -> Plus(e1,e2)) s
(* val parse_simple : Genlex.token Stream.t -> expr
val parse_mult : Genlex.token Stream.t -> expr
val parse_expr : Genlex.token Stream.t -> expr *)
let lexer = Genlex.make_lexer ["+";"*";"(";")"]
let parse_string s =
match lexer (Stream.of_string s) with parser
[< e = parse_expr; _ = Stream.empty >] -> e
(* val parse_string : string -> expr *)
I leave as exercise how to extend it to handle "-" and "/" in a
generic way.
Jacques Garrigue
next prev parent reply other threads:[~2005-04-04 0:41 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-04-01 11:32 bug in "developing applications with objective caml" (english translation) Jack Andrews
2005-04-01 20:03 ` [Caml-list] " Ken Rose
2005-04-02 5:10 ` some comments on ocaml{lex,yacc} from a novice's POV Jack Andrews
2005-04-02 7:02 ` [Caml-list] " Erik de Castro Lopo
2005-04-02 7:38 ` Jacques Garrigue
2005-04-03 16:18 ` Parser combinators [was: some comments on ocaml{lex,yacc} from a novice's POV] Alex Baretta
2005-04-04 0:40 ` Jacques Garrigue [this message]
2005-04-05 16:06 ` [Caml-list] some comments on ocaml{lex,yacc} from a novice's POV Oliver Bandel
[not found] ` <50130.202.164.198.46.1112418605.squirrel@www.ivorykite.com>
2005-04-04 3:42 ` Jack Andrews
2005-04-04 5:44 ` [Caml-list] " Erik de Castro Lopo
2005-04-04 9:51 ` Jon Harrop
2005-04-05 12:00 ` Geoff Wozniak
2005-04-05 13:49 ` Jon Harrop
2005-04-05 14:26 ` Richard Jones
2005-04-05 16:13 ` Oliver Bandel
2005-04-06 4:52 ` Geoff Wozniak
2005-04-06 5:12 ` Kenneth Knowles
2005-04-06 6:15 ` some comments on ocaml{lex,yacc} from anovice's POV Jack Andrews
2005-04-04 10:29 ` [Caml-list] Re: some comments on ocaml{lex,yacc} from a novice's POV Daan Leijen
2005-04-04 17:39 ` Paul Snively
2005-04-04 18:16 ` skaller
2005-04-04 18:49 ` Paul Snively
-- strict thread matches above, loose matches on Subject: below --
2001-03-21 18:41 [Caml-list] recursive modules redux, & interface files Xavier Leroy
2001-03-30 10:27 ` [Caml-list] parser combinators Kevin Backhouse
2001-04-08 18:28 ` Daniel de Rauglaudre
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=20050404.094049.35417841.garrigue@math.nagoya-u.ac.jp \
--to=garrigue@math.nagoya-u.ac.jp \
--cc=alex@barettadeit.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