Re: Saving comments in an abstract syntax tree

jsgray@ix.netcom.com (Jan Gray)
Fri, 31 Mar 1995 07:01:24 GMT

          From comp.compilers

Related articles
Saving comments in an abstract syntax tree gautier@irisa.fr (1995-03-22)
Re: Saving comments in an abstract syntax tree steve@cegelecproj.co.uk (1995-03-30)
Re: Saving comments in an abstract syntax tree jsgray@ix.netcom.com (1995-03-31)
| List of all articles for this month |

Newsgroups: comp.compilers
From: jsgray@ix.netcom.com (Jan Gray)
Keywords: parse
Organization: Netcom
References: 95-03-131
Date: Fri, 31 Mar 1995 07:01:24 GMT

gautier@irisa.fr (Thierry Gautier) writes:
>Has anybody some experience in attaching comments, during parsing,
>as attributes of an abstract syntax tree


I imagine this is routine in toolsets, such as syntax directed editors and
language to language translators, which represent programs as something other
than source code.


In 1984, in "Alice Pascal" (a DOS hosted syntax directed editor/interpreter
designed by Brad Templeton), we wrote a Pascal parser to import source code
into our abstract syntax tree program representation. (We kept no source; as
usual, selective AST pretty-printing provided a program display and "Save As
Text"). Here it was important to import comments as AST attributes, especially
since Turbo Pascal directives were comments.


In this system, any comments were simply gathered up and buffered by our
lexical analyzer. "Line" productions in our grammar, such as declaration and
statement, were modified to attach the "comment buffer" contents as just
another attribute of the current AST node. Only certain node types could have
comments, and so sometimes there could be a loss in fidelity. Given:
t := t + a[i,j] { across } * b[j,k] { down }; { inner prod }
we would capture comments on the "assignment statement" production to produce
t := t + a[i,j] * b[j,k]; { across down inner prod }
or perhaps
t := t + a[i,j] * b[j,k]; { across down }
{ inner prod }
Unfortunate, but adequate for our needs.


In principle one could capture comments on a token by token basis, and attach
the comment attribute to each node, directly, or through an associative mapping
(usually there many fewer comments than tokens). But since most comments are
either to the right of a line of code, or stand alone on their own line, a
simple approach may well be adequate for you too.


Jan Gray
Redmond, WA
--


Post a followup to this message

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