[antlr-interest] line number counter reset
tdknghi <nk.truong at student.qut.edu.au>
nk.truong at student.qut.edu.au
Fri Feb 14 23:40:22 PST 2003
Hi all,
I would like tracking the line number in my program when parse Java
program. I wrote a class to extend antlr.CommonAST then instruct the
parser to use my custom AST type rather than the default AST node
type. I have everything work beautifully except one problem. I
attach all the files together with the description so that you could
get a better idea of what goes wrong.
---------------------------------
1. Here is my custom AST node type:
public class LineAST extends antlr.CommonAST
{
int line_number = 0;
int column_number = -1;
private static boolean verboseStringConversion = false;
private static String[] tokenNames = null;
public LineAST()
{
}
public LineAST(antlr.Token tok)
{
initialize(tok);
}
public void initialize(antlr.Token tok)
{
setText(tok.getText());
setType(tok.getType());
this.line_number = tok.getLine();
this.column_number = tok.getColumn();
}
public int getLine()
{
return line_number;
}
public int getColumn()
{
return column_number;
}
public static void setVerboseStringConversion(boolean
verbose, String[] names)
{
verboseStringConversion = verbose;
tokenNames = names;
}
public String toString()
{
StringBuffer b = new StringBuffer();
// if verbose and type name not same as text
(keyword probably)
if (verboseStringConversion &&
!getText().equalsIgnoreCase(tokenNames
[getType()]))
{
b.append("[l# " + getLine() + " c# " +
getColumn() + " ");
b.append(getText());
b.append(",<");
b.append(tokenNames[getType()]);
b.append(">]");
return b.toString();
}
return getText();
}
}
-----------------------------------------
2. Instruct the parser to use my custom AST node..(it's done
correctly).
--------------------------------------------------
3. When a parser the following program --- PROBLEM START HERE!!!!!!
1 import TerminalIO.*;
2
3 public class Minutes
4 {
5 ScreenWriter writer = new ScreenWriter(); ****
6
7 public void run()
8 {
9 int i = 5; ***
10 System.out.println(i);
11 }
12
13 public static void main(String[] args)
14 {
15 System.out.println("main method");
16 }
17 }
a. The line number of the "ScreenWriter token" (type one) suddently
reset to 0 (the correct line number should be 5), but the line
number of the token "writer" is correct which is 5. The rest of
tokens in that line is correct.
b. Similarity with the line int i = 5; the line number of the
token "int" is reset to 0 but the line number of toke "i", "=", "5"
is correct.
I have done several tests. the line number only reset for the first
token of the variable declaration line only.
Do you have any idea what goes wrong? Many thanks for all your help.
Regards,
MM
Note: I am using ANTLR 2.7.2
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list