IntegerList productions = grammar.getProductionList(symbol);
SymbolSet examined = new SymbolSet(); // List of all examined symbols
for (int i = 0; i<productions.getCount(); i++)
{
SymbolList productiondefinition = grammar.getProduction(productions.get(i)).getDefinition();
if (productiondefinition.getSymbolCount()==0)
// Symbol for a empty firstset added
firstset.addSymbol(EMPTYLIST);
else
{
// for every symbol in the production
int j = 0;
Symbol newsymbol;
boolean foundEmptyList;
do
{
foundEmptyList = true;
newsymbol = productiondefinition.getSymbol(j);
if (newsymbol instanceof Terminal)
// if a terminal symbol
firstset.addSymbol(newsymbol);
else if (!newsymbol.equals(symbol))
{
// and if a non terminal symbol
if (!examined.contains(newsymbol))
{
SymbolSet newfirstset = first(newsymbol, visited);
foundEmptyList = newfirstset.contains(EMPTYLIST);
for (int k = 0; k<newfirstset.getSymbolCount(); k++)
if (!newfirstset.getSymbol(k).equals(EMPTYLIST))
firstset.addSymbol(newfirstset.getSymbol(k));
examined.addSymbol(newsymbol);
}
}
j++;
}
while ((!(newsymbol instanceof Terminal)) && (foundEmptyList) &&
(j<productiondefinition.getSymbolCount()) &&
(!productiondefinition.getSymbol(j-1).equals(symbol)));
}
}
return firstset;
}