Re: java.lang.StackOverflowError
thanks for your answers mate. well, before redisigning my code (that
could take really a lot of time for me, because i really can't find
another algorithm to make this) i wanna try to resize the memory of the
stack as Bla says, but can't find how to do that.
however, here is some code, mind that all is working fine for
stringToCheck that has 11 or less characters, then for 12 or more i get
the StackOverflowError:
[CODE]
public boolean check(int scanIndex)
{
productionSet = givenGrammar.getProductionSet();
startSymbol = givenGrammar.getStartSymbol();
if(sententialFormStack.isEmpty())
{
for (i = scanIndex; i < productionSet.size(); i++)
{
currentLeftSide = productionSet.elementAt(i).getLeftSide();
currentRightSide = productionSet.elementAt(i).getRightSide();
if (currentLeftSide.equals(startSymbol) &&
currentRightSide.length() <= stringToCheck.length())
{
if(currentLeftSide.equals(stringToCheck))
{
return true;
}
sententialFormStack.push(new Ancestor(startSymbol, i));
return check(0);
}
}
return false;
}
else
{
currentSententialForm =
sententialFormStack.peek().getSententialForm();
currentScanIndex =
sententialFormStack.peek().getProductionIndexUsedToUnfold();
for(i = scanIndex; i < productionSet.size(); i++)
{
currentLeftSide = productionSet.elementAt(i).getLeftSide();
currentRightSide = productionSet.elementAt(i).getRightSide();
totalLength = currentSententialForm.length() +
currentRightSide.length() - currentLeftSide.length();
if(currentSententialForm.contains(currentLeftSide) && totalLength
<= stringToCheck.length())
{
newSententialForm =
currentSententialForm.replaceFirst(currentLeftSide, currentRightSide);
if(newSententialForm.equals(stringToCheck))
{
return true;
}
sententialFormStack.peek().setProductionIndexUsedToUnfold(i);
sententialFormStack.push(new Ancestor(newSententialForm, -1));
return check(0);
}
}
if(currentSententialForm.equals(startSymbol))
{
return false;
}
sententialFormStack.removeElementAt(sententialFormStack.size()-1);
currentScanIndex =
sententialFormStack.peek().getProductionIndexUsedToUnfold();
sententialFormStack.peek().setProductionIndexUsedToUnfold(-1);
return check(currentScanIndex+1);
}
}
[/CODE]