[antlr-interest] Line number help
Micheal J
open.zone at virgin.net
Fri Nov 10 11:18:29 PST 2006
Hi,
> > However, if I have a rule such as:
> >
> > expression
> > : #(EXPR expr)
> > ;
> >
> > the line number and column number within the AST are always
> zero. I
> > suspect this has to do with the way they are constructed in the
> > parser. As an interim measure, I have developed routines which walk
> > the expression structure to find the starting and ending
> line numbers,
> > but is there any easy way of appending this information
> right into the
> > EXPR node? That is, any way short of modifying each and every rule
> > where I create such a tree structure when parsing the code?
>
> I've had to fix the same problem with my grammar today. I
> ended up defining a method like this:
>
> private void setLineCol(AST t, LineColAST child) {
> ((LineColAST)t).setLine(child.getLine());
> ((LineColAST)t).setCol(child.getCol());
> }
>
> This is for C#. Then, in my grammar, I replace rules like this one:
>
> expression
> : start:exp
> {## = #([EXPR,"EXPR"], #expression); }
> ;
>
> by this
>
> expression
> : start:exp
> {## = #([EXPR,"EXPR"], #expression); setLineCol(##, #start); }
> ;
>
> I noticed this is how it is done in the grammar for SDL 2000,
> available at the antlr website, so I'm doing the same.
KCSParse/csharp_v1 uses a similar scheme: ##.CopyPositionFrom( #some_node );
One difference is that CopyPositionFrom(node) is a method on a custom AST
class.
Micheal
-----------------------
The best way to contact me is via the list/forum. My time is very limited.
More information about the antlr-interest
mailing list