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=2.3 required=5.0 tests=AWL,DNS_FROM_RFC_POST, SPF_NEUTRAL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id F3316BBAF for ; Wed, 7 Oct 2009 22:16:38 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApwCAKOTzErRVdnaimdsb2JhbACRPYkBPwEBAQoJDAcRBapXgTuPPwEDAwWEJQSBVmWHcw X-IronPort-AV: E=Sophos;i="4.44,521,1249250400"; d="scan'208";a="48173955" Received: from mail-gx0-f218.google.com ([209.85.217.218]) by mail4-smtp-sop.national.inria.fr with ESMTP; 07 Oct 2009 22:16:38 +0200 Received: by gxk10 with SMTP id 10so6304059gxk.3 for ; Wed, 07 Oct 2009 13:16:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=wyttIhVzYK0qqzi3l21ypgC9JwWat/O6tLpkv20tS9M=; b=ATAEpozOKaXw8VsRlN4KXnY0JbbWWUKnYgY+3BLUIR0MwucGCnrVQgV4UzTCFSB7EF vsncgQf7BZ2IcWJVAbIbXxjIUGmOt6Wt+0IKgTfEgnWXtRfhJXs7olpQWHS9Vou193X0 unOv7pWyQ+WlLz2oeubhhP+EuOII3QXT5Qhg4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=tX8FVlfOCXzZf9beO8dGTuTUnvJpmg10B6gC21tnQ6xrp2w8360bz1igbvfX37KSEZ fe2vNVbA+V2nf1i0UOVv6l109ynE6cYcsSGLCXc5UymusA4r8kjCDD94YLXw9vXlqipd y9eWXvNAC5r7aVOo/63DK47C4CiSIdhz37HL8= MIME-Version: 1.0 Received: by 10.101.47.6 with SMTP id z6mr369638anj.140.1254946597149; Wed, 07 Oct 2009 13:16:37 -0700 (PDT) In-Reply-To: References: Date: Wed, 7 Oct 2009 22:16:34 +0200 Message-ID: <527cf6bc0910071316y233ecd35va02bfca2aa17d9c9@mail.gmail.com> Subject: Re: [Caml-list] camlp5/revised syntax questions From: blue storm To: Aaron Bohannon Cc: caml-list@yquem.inria.fr Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Spam: no; 0.00; camlp:01 syntax:01 camlp:01 ocaml:01 syntax:01 infix:01 notations:01 grammars:01 expr:01 ocaml:01 explicitely:01 infix:01 syntaxes:01 expr:01 val:01 Disclaimer : I have learned camlp4 from the ocaml distribution >=3D 3.10, wich is different from camlp5 : take my words with a grain of salt. On Wed, Oct 7, 2009 at 6:20 PM, Aaron Bohannon wro= te: > >From reading the camlp5 documentation, I've managed to write a syntax > extension that adds a new expression starting with a distinct keyword, > and it seems to work fine. =A0However, if I want to experiment with > infix notations, things get a little trickier. =A0I need to specify it's > precedence and associativity, of course. > > So, there is a list of syntactic structures on this page: > http://pauillac.inria.fr/~ddr/camlp5/doc/htmlc/ast_transi.html > > 1) Where can I find the "level names" for each of these syntactic > constructs, for use with BEFORE, LIKE, etc? =A0Is that what the > "Comment" column is for? The different "level names" are not absolute among all camlp4 grammars : they're a property of each grammar rule of each grammar. If you want to "modify" a specific grammar (that is, EXTEND it), you must check the different levels available in the definition. See the files meta/pa_r.ml and etc/pa_o.ml in the camlp5 source tree for the definition of the revised and classical syntax, respectively. Luckily, the expr rules (the one defining ocaml expressions, wich you seem interested in) of both syntax mostly share the same levels (the revised syntax has an additional "where" level for example, but they're otherwise mostly the same). There is no explicitely "precedence" in camlp4 parlance, you must use the levels instead : each precedence level of infix operators has it own level, usually named after the most representative infix operator of the level : ':=3D', '||', '&&', '<'... > 2) I am confused by the fact that this is a list is for the revised > syntax. =A0I think most people (including me) want to modify the > original syntax. =A0e.g., imagine that I want to modify the record > update operator "<-" in the original syntax---I need to refer to it > somehow, but it doesn't even appear in the list. While concrete syntaxes for revised and classical syntax are different, the abstract syntax tree is the same. Camlp4 quotations works by replacing (using camlp4) the quotation you wrote by the concrete ocaml AST representation. A nice side-effet of this is that you can use quotations "in the revised syntax" (the code inside quotations use the revised syntax) when writing an extension for the classical syntax. Eg. if you parse "for i =3D 0 to 10 step 2 do ... done" and you want to generate the OCaml AST corresponding to "for i =3D 0 to 10/2 do let i =3D i * 2 in ... done", you can write <:expr< for i =3D 0 to 10 / 2 do { let i =3D i * 2 in ... } >> , it will generate the corresponding AST and be printed (after processing the source using the extenion) in whatever syntax the user of your extension is using (probably the classical one). This way (using revised syntax inside the quotation of your extension), your can stay consistent with the camlp5 documentation (wich describes the quotation in the revised syntax). camlp4 >=3D 3.10 also has quotations in the classical syntax, but I wouldn't recommend using them : revised syntax is a less ambiguous syntax wich makes those things easier. In your specific case, you can parse whatever syntax you want using the "<-" operator, then output the corresponding AST using a quotation in the revised syntax, that is <:expr< a :=3D b >> (instead of "a <- b"). For a reference, see the related rules in etc/pa_o.ml : | ":=3D" NONA [ e1 =3D SELF; ":=3D"; e2 =3D expr LEVEL "expr1" -> <:expr< $e1$.val :=3D $e2$ >> | e1 =3D SELF; "<-"; e2 =3D expr LEVEL "expr1" -> <:expr< $e1$ :=3D $e2$ >> ]