From: skaller <skaller@ozemail.com.au>
To: "Rafael 'Dido' Sevilla" <dido@imperium.ph>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] line number information in abstract syntax trees
Date: 17 Sep 2003 06:07:51 +1000 [thread overview]
Message-ID: <1063742870.14799.18.camel@pelican> (raw)
In-Reply-To: <20030915075339.GA20719@imperium.ph>
On Mon, 2003-09-15 at 17:53, Rafael 'Dido' Sevilla wrote:
> As some of you have suggested earlier, I have foregone doing some
> preliminary semantic analysis for my compiler in my ocamlyacc grammar,
> and instead am using the grammar solely to do syntactic analysis. Which
> then brings me to another problem. I've created an abstract syntax tree
> data type, but now I need to somehow embed line number information
> obtained from the syntactic analysis phase so that I can later do error
> reporting. I can't think of a clean way to do this. So far, I have a
> syntax tree data type that kind of looks like:
>
> type program = { impmodule: string ; tdecls: topdecl list; plineno:int}
> and topdecl =
> Declaration of decl * int
> and decl = { idents: string list ; dtype: xtype ; dlineno:int}
> and xtype =
> Data of datatype * int
> | Func of fntype * int
> | Alias of xtype * int
> 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)
>
> Note that all the record types have additional fields that look like
> 'plineno:int' and every variant type has an int tacked on somewhere.
> That int is supposed to contain the line number.
>
> 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
In Felix, every single node of the Abstract Syntax Tree contains
a source reference (except type expressions). Whilst it is
painful to construct this information, it is worthwhile.
My nodes look like:
| AST_name (sr,name)
| AST_apply(sr,f1,e1)
. | AST_literal (sr,9999)
...
where sr is the source reference.
An alternative for expressions is a dummy expression
combinator:
| AST_srcref (sr,e)
which can be put where needed. When the source
is just a 'span' of two nodes it can be elided,
and you use a function
let rec src x = match x with
| AST_apply(f,e) -> range (src f) (src e)
to compute the source location. My ranged source
references have the type
type range_srcref =
string * (* filename *)
int * (* start line number *)
int * (* start column *)
int * (* end line number *)
int (* end column *)
Even token lexed contains the filename,
line number, and start and end columns
of the lexeme the token was derived from.
The pain of carrying the source references
around is lessened when you consider that
in any production quality compiler 70%
of all the code is error reporting anyhow :-)
Here's an error diagnostic (this one is actually
a compiler bug)
CLIENT ERROR
[bind_exe] LHS[t](List::list[<T1128>]) of initialisation must have same
type as
RHS(List::list[<T1104>]) unfolded LHS = List::list[<T1128>]
In lpsrc/flx_lib.ipk: line 504, cols 19 to 20
503: | Empty => Empty
504: | Cons (?h, ?t) => Cons (h, rev t)
**
505: endmatch
-------------------
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
next prev parent reply other threads:[~2003-09-16 20:08 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-09-15 7:53 Rafael 'Dido' Sevilla
2003-09-15 11:14 ` Michal Moskal
2003-09-16 20:07 ` skaller [this message]
2003-09-17 8:44 ` Christian Lindig
2003-09-18 19:01 ` skaller
2003-09-19 6:50 ` Christian Lindig
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=1063742870.14799.18.camel@pelican \
--to=skaller@ozemail.com.au \
--cc=caml-list@inria.fr \
--cc=dido@imperium.ph \
/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