I am trying to parse a string recursively with StringTokenizer
. The string represents a tree, in the form:
[(0,1),[(00,01,02),[()],[()]]]
where information of the node is stored inside the parenthesis, while the brackets are the children of a node, separated by commas. For instance, this string represents this tree:
If a node has something inside the parenthesis it is a normal node, if it has nothing it is a leaf.
I've written the code below to parse it, and it works fine but when the recursion ends it seems the tokenizer doesn't have any other token to analyze. The problem is that when it encounters the final brackets (]]]
) it jumps directly to the last one skipping the others.
import java.util.*;
public class ParseString
{
public void setParameters(String parameters) throws Exception {
setParameters(new StringTokenizer(parameters, "[(,)]", true));
}
public void setParameters(StringTokenizer tokenizer) throws Exception{
String buf;
try{
if (!(buf = tokenizer.nextToken()).equals("["))
throw new Exception("Malformed string, found " + buf + "instead of [");
boolean isLeaf = setWeights(tokenizer);
System.out.println("Leaf: " + isLeaf);
while (!(buf = tokenizer.nextToken()).equals("]")) {
do{
setParameters(tokenizer);
}while (!(tokenizer.nextToken().equals("]")));
if (!(buf = tokenizer.nextToken()).equals(","))
break;
}
}catch(Exception e){e.printStackTrace();}
}
public boolean setWeights(StringTokenizer tokenizer) throws
Exception{
String buf;
if(!(buf = tokenizer.nextToken()).equals("("))
throw new Exception("Malformed string, found "+ buf + "instead of (");
do{
buf = tokenizer.nextToken();
if(buf.equals(")")){
return true;
}
if(!buf.equals(","))
System.out.println(buf);
}while(!tokenizer.nextToken().equals(")"));
return false;
}
public static void main(String[] args)
{
ParseString ps = new ParseString();
try{
ps.setParameters("[(0,1),[(00,01,02),[()],[()]]]");
}catch(Exception e){e.printStackTrace();}
}
}
This is the output I have running it:
0
1
Leaf: false
00
01
02
Leaf: false
Leaf: true
Leaf: true
java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:349)
at ParseString.setParameters(ParseString.java:22)
at ParseString.setParameters(ParseString.java:7)
at ParseString.main(ParseString.java:51)
Another thing: the parser should be able to analyze any generic tree, not just this one. If someone can fix this, I'll be glad.