[antlr-interest] Line numbers

Terence Parr parrt at jguru.com
Mon Jan 20 10:18:02 PST 2003


On Sunday, January 19, 2003, at 09:39 PM, Morne Streicher wrote:

> Dear All
>
> I'm pretty new to compiler theory and parser generators. I've managed 
> to successfully defined a language (and implemented the interpreter as 
> well) using ANTLR. :-)
>
> What I'm doing to interpret the source language is to walk the 
> Abstract Syntax Tree (AST) generated by ANTLR, and "evaluate" that.
>
> The AST generated by the parser however do not include the line and 
> column number for the token it represents. I need that information to 
> customize run-time error messages, i.e. include line numbers.
>
> Has anybody got an (simple) example of how I could do that - extend 
> the .g file to make it include line numbers in the AST class? I 
> believe I'll have to extend the AST class, which is fine.

Hi there.  I believe there was a quick thread previously where I 
decided to add getToken() method to the AST interface and then modify 
the AST factory method create(Token t) to just store the token not pull 
out the text/type and store it.  This way the tree will have a ref to 
the token and you can ask (and my tree parsers can ask) for the line 
number.

The problem will have to way for 2.7.3 as 2.7.2 will be out today I 
think.  The CPP and C# output would not have time to catch and I need 
to think about the problem a bit more.

I would do this yourself until all ANTLR does it. :)

Ter
--
Co-founder, http://www.jguru.com
Creator, ANTLR Parser Generator: http://www.antlr.org
Lecturer in Comp. Sci., University of San Francisco


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



More information about the antlr-interest mailing list