[antlr-interest] Recommend a Document for TreeAdaptors?
Mark Wright
markwright at internode.on.net
Sat Jan 26 02:47:26 PST 2008
On Thu, 24 Jan 2008 14:57:10 -0600
"Dejas Ninethousand" <dejas9000 at gmail.com> wrote:
> I have my first parse tree (default) horray! Of course now I want to
> build parse trees
Hello Dejas,
I guess you mean Abstract Syntax Trees.
> using classes of my own design instead of relying
> on the default ITree implementation. Poking around a bit I get the
> notion that this is done by providing a custom TreeAdaptor
> implementation; however, I am having trouble finding clear and
> expressive documentation on antlr.org related to custom tree
> construction. Could anyone recommend a url that is more instructive
> on this topic? Thanks.
>
> -- Dejas
I guess I should ask which target are you using.
With the Java target an alternative approach is to ask
ANTLR to build tokens of your own class:
options {
output=AST;
TokenLabelType=MyToken;
ASTLabelType=CommonTree;
}
// ...
@lexer::members {
// ANTLR 3.0.1
// public Token emit() {
// MyToken t = new MyToken(input, type, channel, tokenStartCharIndex, getCharIndex()-1);
// t.setLine(tokenStartLine);
// t.setText(text);
// t.setCharPositionInLine(tokenStartCharPositionInLine);
// emit(t);
// return t;
// }
// ANTLR 3.1 beta
public Token emit() {
Token t = new MyToken(input, state.type, state.channel, state.tokenStartCharIndex, getCharIndex()-1);
t.setLine(state.tokenStartLine);
t.setText(state.text);
t.setCharPositionInLine(state.tokenStartCharPositionInLine);
emit(t);
return t;
}
}
Then you can add your own field that can contain a pointer to
a class in your symbol table heirarchy:
public class MyToken extends CommonToken {
protected Symbol symbol;
public MyToken(CharStream input, int type, int channel, int start, int stop) {
super(input, type, channel, start, stop);
symbol = (Symbol)null;
}
public MyToken(int type, String text) {
super(type, text);
symbol = (Symbol)null;
}
public final Symbol getSymbol() {
return symbol;
}
public void setSymbol(Symbol value) {
symbol = value;
}
}
With the C target Jim explained in an email a while ago
that there is a void* field already in the token struct
for this purpose.
Thanks, Mark
--
More information about the antlr-interest
mailing list