Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] line number information in abstract syntax trees
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-09-17 (08:48)
From: Christian Lindig <lindig@c...>
Subject: Re: [Caml-list] line number information in abstract syntax trees
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

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: