|Saving comments in an abstract syntax tree email@example.com (1995-03-22)|
|Re: Saving comments in an abstract syntax tree firstname.lastname@example.org (1995-03-30)|
|Re: Saving comments in an abstract syntax tree email@example.com (1995-03-31)|
|Organization:||DSDIC Team, Cegelec Projects Ltd.|
|Date:||Thu, 30 Mar 1995 12:25:00 GMT|
firstname.lastname@example.org (Thierry Gautier) writes:
> Has anybody some experience in attaching comments, during parsing,
> as attributes of an abstract syntax tree
> (where and how attaching them)?
> The parser is built using bison (or yacc).
Yep, and I cheated. In the language I was processing, comments were
delimited with (* ... *), and as usual were normally stripped out
during the lexical analysis stage - the yacc parser never saw them.
This wasn't acceptable in the target environment, which used a
custom-built graphical editor - the source language was the form
in which the editor stored its programs. The result is that you
save the file, then when you reload (and therefore re-parse) the
file, all the comments have vanished.
I did play for a while with saving comments in lex, and then trying
to work out where to attach them in the tree, later on. Generally
speaking, it didn't work. Comments usually appeared slightly further
on in the tree, which is disastrous if you're annotating lists of
So I cheated. We defined certain points in the grammar where we
would allow syntactic comment constructs, delimited with (** ... **),
and altered the editor to only put comments there. If you wrote
source code by hand, such "supercomments" in any other places caused
a syntax error, and all normal comments still get stripped.
It's horrible, but it works.
Return to the
Search the comp.compilers archives again.