From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 11DC6BC6B for ; Fri, 22 Jun 2007 01:45:41 +0200 (CEST) Received: from stirner.roentgeninstitut.de (stirner.roentgeninstitut.de [212.110.109.210]) by concorde.inria.fr (8.13.6/8.13.6) with SMTP id l5LNjeRj016342 for ; Fri, 22 Jun 2007 01:45:40 +0200 Received: (qmail 1481 invoked by uid 1000); 21 Jun 2007 16:45:38 -0700 Date: Thu, 21 Jun 2007 16:45:37 -0700 From: Christian Stork To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Parsing Message-ID: <20070621234537.GA1420@stirner.roentgeninstitut.de> References: <200706220014.39510.jon@ffconsultancy.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200706220014.39510.jon@ffconsultancy.com> X-Archive: encrypt User-Agent: Mutt/1.5.13 (2006-08-11) X-Miltered: at concorde with ID 467B0DA4.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; parsing:01 0100,:01 parser:01 camlp:01 parsing:01 syntax:01 parsers:01 grammars:01 expr:01 parser:01 expr:01 npeek:01 camlp:01 grammars:01 806:98 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