[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: getting line numbers
On Wed, Nov 13, 2002 at 05:50:18PM +0000, ivan moore wrote:
> I am using the Java1.1 parser included in the
> sablecc examples - I've found it very simple
> to use for what I've needed so far.
> (I'm working on a code duplication spotting
> tool - http://condenser.sf.net)
Sounds interesting. You might find a search of the literature concerning the
"same fringe" problem helpful for this kind of tree matching problem. Then
again, it might be as easy as just using two lexers side by side as the
iterators of each fringe.
> How do I access the line numbers/character positions
> that a node, e.g. an instance of
> represents? I'd really like to be able to find out
> the start and end positions of such a node in the
> original source file. (If line numbers are available,
> I would also want the character positions on the lines
You can only get the line positions and numbers of subclasses of the Token
classes (the TFoo classes). These are the objects generated by the lexer.
The getLine() and getPos() methods are the ones that give you the start of
the receiving token.
You can only find the starting positions of productions (the PBar abstract
class and its ASpecificBar subclasses) by finding the first token in the
depth first search of nodes generated by the parser. A subclass of analysis
DepthFirstAdapter class can be used to implement this fairly easily.
Jon Shapcott <firstname.lastname@example.org>