if (alternative == null) {
throw CompilerException.invalidReference(identifier);
}
Alternative grammarAlternative = alternative
.getGrammarAlternative();
int size = grammarAlternative.getElements().size();
if (size == 0) {
throw CompilerException.alternativeNotRecursive(identifier);
}
String prodName = this.currentProduction.getNameToken().getText();
// is it left recursive?
Element element = grammarAlternative.getElement(0);
if (element instanceof ProductionElement
&& ((ProductionElement) element).getProduction().getName()
.equals(prodName)) {
// check that recursion is followed by token
if (grammarAlternative.getElements().size() < 2
|| !(grammarAlternative.getElement(1) instanceof TokenElement)) {
throw CompilerException
.recursionNotFollowedByToken(identifier);
}
}
else {
// not left recursive => check that it is right recursive
element = grammarAlternative.getElement(size - 1);
if (!(element instanceof ProductionElement)
|| !((ProductionElement) element).getProduction()
.getName().equals(prodName)) {
throw CompilerException.alternativeNotRecursive(identifier);
}
}
this.currentPriorityLevel.addAlternative(grammarAlternative);
grammarAlternative.setPriorityLevel(this.currentPriorityLevel,
identifier);
}
}