From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id KAA20709; Wed, 17 Sep 2003 10:48:35 +0200 (MET DST) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id KAA06028 for ; Wed, 17 Sep 2003 10:48:34 +0200 (MET DST) Received: from indyio.rz.uni-saarland.de (indyio.rz.uni-saarland.de [134.96.7.3]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id h8H8mXT15017 for ; Wed, 17 Sep 2003 10:48:33 +0200 (MET DST) Received: from uni-sb.de (uni-sb.de [134.96.7.230]) by indyio.rz.uni-saarland.de (8.12.9/8.12.9) with ESMTP id h8H8mVnS20141334 for ; Wed, 17 Sep 2003 10:48:31 +0200 (CST) Received: from cs.uni-sb.de (cs.uni-sb.de [134.96.252.31]) by uni-sb.de (8.12.9/2003073000) with ESMTP id h8H8mMEH011584; Wed, 17 Sep 2003 10:48:30 +0200 (CEST) Received: from mail.cs.uni-sb.de (mail.cs.uni-sb.de [134.96.254.200]) by cs.uni-sb.de (8.12.9/2003091100) with ESMTP id h8H8lwk9027499; Wed, 17 Sep 2003 10:48:21 +0200 (CEST) Received: from mail.st.cs.uni-sb.de (goscinny.cs.uni-sb.de [134.96.235.32]) by mail.cs.uni-sb.de (8.12.9/2003073000) with ESMTP id h8H8invc015231; Wed, 17 Sep 2003 10:44:50 +0200 (CEST) X-Authentication-Warning: email: Host goscinny.cs.uni-sb.de [134.96.235.32] claimed to be mail.st.cs.uni-sb.de Received: from jacobs.cs.uni-sb.de (jacobs.cs.uni-sb.de [134.96.235.135]) by mail.st.cs.uni-sb.de (Postfix) with ESMTP id 2C71A34B24C; Wed, 17 Sep 2003 10:44:49 +0200 (CEST) Received: by jacobs.cs.uni-sb.de (Postfix, from userid 7528) id 497AF97BA2; Wed, 17 Sep 2003 10:44:48 +0200 (CEST) Date: Wed, 17 Sep 2003 10:44:48 +0200 From: Christian Lindig To: "Rafael 'Dido' Sevilla" Cc: Caml Mailing List Subject: Re: [Caml-list] line number information in abstract syntax trees Message-ID: <20030917084448.GD8771@st> Mail-Followup-To: Christian Lindig , Rafael 'Dido' Sevilla , Caml Mailing List References: <20030915075339.GA20719@imperium.ph> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20030915075339.GA20719@imperium.ph> X-No-Archive: yes X-URL: http://www.st.cs.uni-sb.de/~lindig/ User-Agent: Mutt/1.5.4i X-Loop: caml-list@inria.fr X-Spam: no; 0.00; lindig:01 lindig:01 uni-sb:01 caml-list:01 rafael:01 'dido':01 datatype:01 datatype:01 ramsey:01 expr:01 expr:01 char:01 unop:01 primop:01 uni-sb:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Mon, Sep 15, 2003 at 03:53:39PM +0800, Rafael 'Dido' Sevilla wrote: > and datatype = > Byte of int > | Int of int > | Big of int > | Real of int > | String of int > | Tuple of (datatype list * int) > | Array of (datatype * int) > | List of (datatype * int) > | Chan of (datatype * int) > > This works just fine, but it just seems to me like such a grossly ugly > hack into what is otherwise an elegant-looking data structure. Anyone > have style guidelines I like to suggest a technique (that I learned from Norman Ramsey) that makes line numbers optional in the abstract syntax tree (AST). The suggested style leads to a separation of concerns. and expr = ExprAt of (expr * region) (* <--- *) | Int of (StdPrims.std_string * ty option) | Float of (StdPrims.std_string * ty option) | Char of (StdPrims.std_int * ty option) | Fetch of (name_or_mem) | BinOp of (expr * op * expr) | UnOp of (op * expr) | PrimOp of (name * actual list) An expression is an Int or a Float or something else and by itself does not contain a line number. But it may be wrapped in an ExprAt tag that contains the expression and the line number (here of type region). When you build the abstract syntax tree in a parser, you usually create ExprAt nodes that contain the line number (but you don't have to). You can easily implement a policy that attaches line numbers only at the outer level on an expression, if you want to save space. This representation comes in handy when you create an AST not by parsing, but directly. In this case you don't have to invent line numbers because you simply never would generate ExprAt nodes. -- Christian -- Christian Lindig http://www.st.cs.uni-sb.de/~lindig/ ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners