Re: Get line numbers with Antlr parser



On 2006-05-16, Ulrich Hobelmann <u.hobelmann@xxxxxx> wrote:
Hi, I'm using the Java1.5 parser + tree parser by Mike Studman.

My problem: I need to extract the line numbers from the parser.
You will have to build an AST class to be used by antlr for that,
replacing the standard one

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).
This is not surprising: theline and colum information is not stored in
the tree being built

What can / do I have to do, to get the line numbers to the high level,
where I need it?
You can define for instance something like the following

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
.



Relevant Pages

  • Re: Formelparser
    ... >> Abstract Syntax Tree (AST) und Expression Parsing. ... Der Begriff Token ist leider auch im Compilerbau relativ überladen. ... >> wird nahezu immer Parser gennant. ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: Generating AST using javacc and jjtree
    ... It supports not only Java but C++ and C#. Written in Java v.1.1 and can be ... Parser (which can build AST) and AST Grammar Walker (with ... At ANTLR site ...
    (comp.lang.java.softwaretools)
  • Re: c++ XML processor class?
    ... Do you imply xerces-c++ doesn't have a DOM parser? ... rather I don't see a easy way of traversing a tree with it and ... the key pairs I want to deal with not the whole tree structure. ...
    (comp.text.xml)
  • Re: Highspeed parsen - aber wie?
    ... AST) erzeugen sollte. ... Vergleiche das am besten mit SAX und DOM. ... der Parser Elemente und Attribute findet, ...
    (de.comp.lang.java)
  • Re: Creating scripting language with JavaCC
    ... I want to create a simple scripting language - something like VBScript ... How do I create an AST for this, ... with their own tree. ... identifier() identifier ...
    (comp.compilers.tools.javacc)