Re: Saving comments in an abstract syntax tree (Steve_Kilbane)
Thu, 30 Mar 1995 12:25:00 GMT

          From comp.compilers

Related articles
Saving comments in an abstract syntax tree (1995-03-22)
Re: Saving comments in an abstract syntax tree (1995-03-30)
Re: Saving comments in an abstract syntax tree (1995-03-31)
| List of all articles for this month |

Newsgroups: comp.compilers
From: (Steve_Kilbane)
Keywords: parse
Organization: DSDIC Team, Cegelec Projects Ltd.
References: 95-03-131
Date: Thu, 30 Mar 1995 12:25:00 GMT (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.


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.