Re: Get line numbers with Antlr parser
- From: Antoine Reilles <reilles@xxxxxxxx>
- Date: 18 May 2006 23:51:18 -0400
On 2006-05-16, Ulrich Hobelmann <u.hobelmann@xxxxxx> wrote:
Hi, I'm using the Java1.5 parser + tree parser by Mike Studman.You will have to build an AST class to be used by antlr for that,
My problem: I need to extract the line numbers from the parser.
replacing the standard one
This is not surprising: theline and colum information is not stored in
Funny thing: in one part of the parser it works, but in the tree parser
it doesn't. In java15.g I added some printlns to print getLine() of
some nodes, and they printed the lines. The very same nodes are also
put into a tree structure to read by the java-tree parser (obviously).
the tree being built
You can define for instance something like the following
What can / do I have to do, to get the line numbers to the high level,
where I need it?
public class MyAST extends CommonAST {
private int col = 0,line = 0;
public void initialize(Token tok) {
super.initialize(tok);
line = tok.getLine();
col = tok.getColumn();
}
public void initialize (AST ast) {
super.initialize(ast);
if (ast instanceof MyAST){
col = ((MyAST)ast).getColumn();
line = ((MyAST)ast).getLine();
}
}
public int getLine() { return line; }
public int getColumn() { return col; }
}
and use it with
parser.setASTNodeClass("MyAST");
That way, antlr will build the tree using MyAST nodes, which store the
line and column information
Have fun,
antoine
.
- Prev by Date: Re: AMPC 1.4.3 released (C to Java Class Files compiler suite)
- Next by Date: How to compress parser table use the paper(Dencker)
- Previous by thread: Please add these C# compilers to the list of open source compilers
- Next by thread: How to compress parser table use the paper(Dencker)
- Index(es):
Relevant Pages
|