[antlr-interest] treeparser to copy input to output
SainTiss
saintiss at arklinux.org
Sun Feb 1 11:49:40 PST 2004
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,
I need to do some simple transformations on java source code.
Basically, what I'd like to do is turn declarations like this:
int myInt = 78;
into assignment statements like this:
myInt = 78;
so I took a look at the java grammar, which will be useful to parse the java
source code. For the second part I thought it would be easiest to start with
some code which just prints all nodes to System.out. I thought this would
result in the input java source again, but instead I get something like the
following:
CLASS_DEF
MODIFIERS
A
EXTENDS_CLAUSE
IMPLEMENTS_CLAUSE
OBJBLOCK
...
final
strictfp
H
EXTENDS_CLAUSE
IMPLEMENTS_CLAUSE
OBJBLOCK
The code I used was the following:
public static void printTree(AST t) {
System.out.println(t.getText());
if (t.getNumberOfChildren() != 0) {
AST child = t.getFirstChild();
printTree(child);
for (int i = 1; i < t.getNumberOfChildren(); i++) {
child = child.getNextSibling();
printTree(child);
}
}
}
So it seems I got something wrong here... What do I have to do to just get the
input back? Or isn't this the right approach for this kind of simple
transformations?
Thanks in advance,
Hans Schippers
- --
Ark Linux - Linux for the Masses (http://arklinux.org)
Capitalism:
You don't have any cows. The bank will not lend you money to buy cows,
because you don't have any cows to put up as collateral.
Representative Democracy:
You have two cows. Your neighbors pick someone to tell you who gets the milk.
In a world without walls and fences, who needs windows and gates?
Hans Schippers
2LIC INF
UA 2003-2004
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)
iD8DBQFAHVhUXlnUYIbmLOQRAllNAJ9hasTwhdIPZTvXfx+IvWli7CJA4gCfbxWa
o3Dvfua1Hliu5h//AszaZ9I=
=SRPz
-----END PGP SIGNATURE-----
Yahoo! Groups Links
To visit your group on the web, go to:
http://groups.yahoo.com/group/antlr-interest/
To unsubscribe from this group, send an email to:
antlr-interest-unsubscribe at yahoogroups.com
Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list