Commit dd218ed2 authored by Michael Schwarz's avatar Michael Schwarz

Report if other conflicts are affected by doing Reduce/Reduce resolution

parent dc29a25d
......@@ -35,6 +35,7 @@ import de.in.tum.www2.cup.ast.ProductionSymbolRef;
import de.in.tum.www2.cup.internal.internal_error;
import de.in.tum.www2.cup.internal.lalr_item;
import de.in.tum.www2.cup.internal.lalr_state;
import de.in.tum.www2.cup.internal.lr_item_core;
import de.in.tum.www2.cup.internal.terminal;
import de.tum.in.www2.cupplugin.Colors;
import de.tum.in.www2.cupplugin.Pair;
......@@ -396,8 +397,23 @@ class ConflictPanel extends Composite {
resolutionOptions.removeAll();
resolutionOptions.add("-- Choose an option --");
resolutionOptions.select(0);
resolutionOptions.add("Reduce with production 1");
resolutionOptions.add("Reduce with production 2");
String affects = "";
try {
ReduceReduceConflict rrc = (ReduceReduceConflict) conflict;
lr_item_core item1 = new lr_item_core(rrc.getConflictItem1().the_production(),rrc.getConflictItem1().dot_pos());
lr_item_core item2 = new lr_item_core(rrc.getConflictItem2().the_production(),rrc.getConflictItem2().dot_pos());
if(cupConflictsView.settingOrderAffetsOtherConflicts(item1, item2)){
affects= "Affects others: ";
}
} catch (internal_error e) {
e.printStackTrace();
}
resolutionOptions.add(affects + "Reduce with production 1");
resolutionOptions.add(affects + "Reduce with production 2");
}
/**
......
......@@ -40,6 +40,8 @@ import de.in.tum.www2.cup.ast.ProductionRight;
import de.in.tum.www2.cup.ast.ProductionSymbolRef;
import de.in.tum.www2.cup.ast.SymbolDeclaration;
import de.in.tum.www2.cup.ast.Terminal;
import de.in.tum.www2.cup.internal.internal_error;
import de.in.tum.www2.cup.internal.lr_item_core;
import de.in.tum.www2.cup.internal.terminal;
import de.tum.in.www2.cupplugin.Colors;
import de.tum.in.www2.cupplugin.controller.Controller.JobsToDo;
......@@ -71,6 +73,7 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
private Composite list;
private HashMap<terminal,Integer> terminalsAffectConflicts;
private HashMap<lr_item_core,Integer> ordersAffectConflicts;
PrecedenceToInsert currentPrecs;
private int ignoreRRAfterLine;
......@@ -199,11 +202,13 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
List<Conflict> conflicts = new ArrayList<>();
terminalsAffectConflicts = new HashMap<>();
ordersAffectConflicts = new HashMap<>();
for (int i = 0; i < crm.getAllConflicts().size(); i++) {
Conflict c = crm.getAllConflicts().get(i);
if(!isIgnored(c)){
conflicts.add(c);
addToTerminalSet(crm,c);
addToOrderSet(c);
}
}
int size = conflicts.size();
......@@ -256,26 +261,29 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
}
}
if(terminalsAffectConflicts.containsKey(sourceOfShiftPrec)){
terminalsAffectConflicts.put(sourceOfShiftPrec, terminalsAffectConflicts.get(sourceOfShiftPrec)+1);
}
else{
terminalsAffectConflicts.put(sourceOfShiftPrec, 1);
}
incrementOrSetTo1(terminalsAffectConflicts,sourceOfShiftPrec);
// If both precs come from the same terminal, we only need to increment once (every conflict affects itself)
if(sourceOfReducePrec != null && !sourceOfReducePrec.equals(sourceOfShiftPrec)){
incrementOrSetTo1(terminalsAffectConflicts,sourceOfReducePrec);
}
}
}
private void addToOrderSet(Conflict conflict){
if(conflict instanceof ReduceReduceConflict){
ReduceReduceConflict rrc = (ReduceReduceConflict) conflict;
try {
//We need this workaround because someone didn't favor composition over inheritance
lr_item_core item1 = new lr_item_core(rrc.getConflictItem1().the_production(),rrc.getConflictItem1().dot_pos());
lr_item_core item2 = new lr_item_core(rrc.getConflictItem2().the_production(),rrc.getConflictItem2().dot_pos());
if(sourceOfReducePrec.toString().equals(sourceOfShiftPrec.toString())){
throw new IllegalStateException();
}
if(terminalsAffectConflicts.containsKey(sourceOfReducePrec)){
terminalsAffectConflicts.put(sourceOfReducePrec, terminalsAffectConflicts.get(sourceOfReducePrec)+1);
}
else{
terminalsAffectConflicts.put(sourceOfReducePrec, 1);
}
incrementOrSetTo1(ordersAffectConflicts,item1);
incrementOrSetTo1(ordersAffectConflicts,item2);
} catch (internal_error e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
......@@ -299,6 +307,14 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
this.countConflictsLabel.setText("Detected " + number + " conflicts:");
}
private <X> void incrementOrSetTo1(HashMap<X,Integer> table,X key){
if(table.containsKey(key)){
table.put(key, table.get(key)+1);
}else{
table.put(key,1);
}
}
private void refreshScrolledLayout() {
scrolled.layout(true, true);
scrolled.setMinSize(list.computeSize(SWT.DEFAULT, SWT.DEFAULT));
......@@ -527,6 +543,22 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
return affected != 1;
}
}
boolean settingOrderAffetsOtherConflicts(lr_item_core item1,lr_item_core item2){
Integer affected = ordersAffectConflicts.get(item1);
if(affected != null && affected > 1){
return true;
}
affected = ordersAffectConflicts.get(item2);
if(affected==null){
return false;
}
else{
return affected != 1;
}
}
private int computeIgnoreRRAfter(){
IDocument document = editor.getDocument();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment