[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to obtain the line number of AST node?



Hi Antti,

There is no simple solution to the general problem of assigning line
numbers to intermediate AST nodes.  (Yes, there are simplistic
solutions, but they wouldn't achieve the desired result in many cases).

The normal solution that I usually recommend to my students (and which
works very well) is to keep enough tokens in the AST to give intuitive
error messages.  So, for example, I keep the "(" preceding the
conditional expression of "if" statements and the operator of binary
expressions in my ASTs.  This results in slightly bigger ASTs, but you
still get rid of many spurious tokens.

Etienne

Antti Virtanen wrote:
> Howdy!
> 
> I'm a bit puzzled as I could not find a way to get the line number of
> the AST nodes. The token classes have getLine() since they derive from
> Token, but after AST translation some AST nodes do not contain any
> tokens. AST node classes are derived from Node, which does not have
> getLine() so the line number seems to be forgotten in the translatation.
> What to do now? There are nodes which can span multiple lines, but it
> would be enough to get the starting line. This seems a bit strange since
> I'm not the only one with this need, so it's possible I have missed
> something.

-- 
Etienne M. Gagnon, Ph.D.            http://www.info2.uqam.ca/~egagnon/
SableVM:                                       http://www.sablevm.org/
SableCC:                                       http://www.sablecc.org/

Attachment: signature.asc
Description: OpenPGP digital signature