SymbolSet shiftsymbols = I.getShiftSymbols(); // Transition symbols for shift actions
SymbolSet reducesymbols = I.getReduceSymbols(); // Lookahead symbols for reduce actions
for (int symbol = 0; symbol<tsymbols.getSymbolCount(); symbol++)
{
IntegerSet reduceproductions = I.getReduceProductions(tsymbols.getSymbol(symbol));
int productionpriority = -1;
int highestproduction = -1;
for (int k = 0; k<reduceproductions.getCount(); k++)
{
ReduceReduceConflict reduceconflict = null;
if (k>0)
{
reduceconflict =
new ReduceReduceConflict(grammar, itemsets, state,
(Terminal)tsymbols.getSymbol(symbol),
reduceproductions.get(k-1), reduceproductions.get(k));
conflicts.addConflict(reduceconflict);
}
if (grammar.getPriority(grammar.getProduction(reduceproductions.get(k)))>productionpriority)
{
highestproduction = reduceproductions.get(k);
productionpriority = grammar.getPriority(grammar.getProduction(highestproduction));
if ((log!=null) && (reduceconflict!=null))
log.info(reduceconflict.toString());
}
else if (grammar.getPriority(grammar.getProduction(reduceproductions.get(k)))==productionpriority)
{
if (log!=null)
log.warn(reduceconflict.toString());
}
}
if (reduceproductions.getCount()>1)
if ((log!=null) && (log.isInfoEnabled()))
log.info("The parser will reduce the production "+
grammar.getProduction(highestproduction));
boolean errorreduce = reducesymbols.contains(Error.instance);
if (shiftsymbols.contains(tsymbols.getSymbol(symbol)))
{
if ((errorreduce) || (reducesymbols.contains(tsymbols.getSymbol(symbol))))
{
int tokenpriority = grammar.getPriority((Terminal)tsymbols.getSymbol(symbol));
ShiftReduceConflict shiftconflict =
new ShiftReduceConflict(grammar, itemsets, state,
(Terminal)tsymbols.getSymbol(symbol), highestproduction);
if (tokenpriority>productionpriority)
{
automaton.setShiftAction(state, symbol, I.getTransition(tsymbols.getSymbol(symbol)));
if (log!=null)
{
log.info(shiftconflict.toString());
log.info("The parser will shift");
}
}
else if (tokenpriority<productionpriority)
{
automaton.setReduceAction(state, symbol, highestproduction);
if (log!=null)
{
log.info(shiftconflict.toString());
log.info("The parser will reduce");
}
}
else
{
if (log!=null)
log.warn(shiftconflict.toString());
Associativity associativity =
grammar.getAssociativity((Terminal)tsymbols.getSymbol(symbol));
if (associativity.equals(Associativity.RIGHT))
{
// if the terminal has a right associativity
automaton.setShiftAction(state, symbol, I.getTransition(tsymbols.getSymbol(symbol)));
if (log!=null)
log.warn("The parser will shift");
}
else if (associativity.equals(Associativity.LEFT))
{
// if the terminal has a left associativity
automaton.setReduceAction(state, symbol, highestproduction);
if (log!=null)
log.warn("The parser will reduce");
}
else
{
// SHIFT should be always prefered
automaton.setShiftAction(state, symbol, I.getTransition(tsymbols.getSymbol(symbol)));
if (log!=null)
log.warn("The parser will shift");
}
}
}
else
automaton.setShiftAction(state, symbol, I.getTransition(tsymbols.getSymbol(symbol)));
}
else if ((errorreduce) || (reducesymbols.contains(tsymbols.getSymbol(symbol))))
automaton.setReduceAction(state, symbol, highestproduction);
else
for (int i = 0; i<shiftsymbols.getSymbolCount(); i++)
if (shiftsymbols.getSymbol(i) instanceof Error)
automaton.setErrorAction(state, symbol, I.getTransition(shiftsymbols.getSymbol(i)));
}
// create all actions for the end of file.
if (reducesymbols.contains(EOF))
{
IntegerSet reduceproductions = I.getReduceProductions(EOF);
int productionpriority = -1;
int highestproduction = -1;
for (int k = 0; k<reduceproductions.getCount(); k++)
{
if (grammar.getPriority(grammar.getProduction(reduceproductions.get(k)))>productionpriority)
{
highestproduction = reduceproductions.get(k);
productionpriority = grammar.getPriority(grammar.getProduction(highestproduction));
}
}
if ((grammar.getProduction(highestproduction).getSymbol().equals(grammar.getStartSymbol())))