[antlr-interest] Re: Line number [sls]-bl.spamcop.net
tdknghi <nk.truong at student.qut.edu.au>
nk.truong at student.qut.edu.au
Tue Feb 11 22:40:28 PST 2003
Hi,
Many thanks for your quick and detail response. However, I tried it and
it doesn't work. It all compiled but when I run
java Main -showtree myprogram.java
the ASTFrame view doesn't appear at all. Do we have to modify the
lexer or parser?????
Thanks.
Regards,
MiMi
--- In antlr-interest at yahoogroups.com, "Morne Streicher"
<mornes at m...> wrote:
> Dear All
>
> Thanks to some help from this interest group, I found it easy to track
line numbers. Here is what I have done. Hope this concrete example
helps - and off course, it's only one way of doing it...
>
> Sure there are more elegant ways - but this works for me...
>
> Step 1 : Extend the CommonAST class:
> ----------------------------------------------------------
>
>
>
> public class OTS_AST extends CommonAST {
> int line_number = -1;
> int column_number = -1;
>
> public OTS_AST() {
> }
>
> public OTS_AST(Token tok) {
> initialize(tok);
> }
>
> public void initialize(Token tok) {
> setText(tok.getText());
> setType(tok.getType());
> this.line_number = tok.getLine();
> this.column_number = tok.getColumn();
> }
>
> public int getLine()
> {
> return this.line_number;
> }
>
> public int getColumn()
> {
> return this.column_number;
> }
> }
>
> Step 2 : Tell the parser which AST class to use:
> ---------------------------------------------------
> //....
> otsLexer lexer = new otsLexer(fis);
> lexer.setFilename(filename);
> otsParser parser = new otsParser
(lexer);
> parser.setFilename(filename);
> parser.setASTNodeType
("OTS_AST"); --NB!!!!!!!!
> parser.otscript();
> //....
>
> Step 3: Get your instance of the AST
> ----------------------------------------
>
> //.....
>
> OTS_AST ast = (OTS_AST) parser.getAST()
>
> int token1_line = ast.getLine();
>
> //...
>
> And that's it.
>
> M.
>
> -----Original Message-----
> From: tdknghi <nk.truong at s...>
> [mailto:nk.truong at s...]
> Sent: 12 February 2003 07:58
> To: antlr-interest at yahoogroups.com
> Subject: [antlr-interest] Re: Line number [sls]-bl.spamcop.net
> Importance: Low
>
>
> Hi,
>
> Sorry for being so stupid. Could you give me more detail instruction
> how to keep track the line number for Java programs (by AST view)?
> I read the track line number section in the ANTLR documentation.
> However, I haven't got a clear idea how to do it.
>
> Thanks.
>
> Regards,
> MiMi
>
>
> --- In antlr-interest at yahoogroups.com, "micheal_jor
<open.zone at v...>"
> <open.zone at v...> wrote:
> >
> > > Hi all,
> > >
> > > I have seen a lot of post regarding about display the line number
> > in the
> > > AST vew. However, there is no clear and example of how to do
it. I
> > am
> > > currently using antlr 2-7.2.
> > > Could you please let me know how to get it done. Thanks.
> > >
> > > Regards,
> > > MiMi
> >
> > If you are using Java or C# and, if by "AST view" you mean the
> > ASTFrame mechanism for viewing your ASTs then you can do the
> > following:
> >
> > 1. Create a subclass of the appropriate standard Common***AST
> class
> > (if you haven't already)
> > 2. Override toString()/ToString() to print line numbers as well
> > 3. Instruct your parser to use your new AST class with
> > parser.setASTNodeType(string) or by using the "ASTLableType"
> option
> > in your grammar.
> >
> > Run your app as usual ;-)
> >
> > Micheal
>
>
>
>
> Your use of Yahoo! Groups is subject to
http://docs.yahoo.com/info/terms/
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list