Commit 2a8891d3 authored by Michael Schwarz's avatar Michael Schwarz

show example for cycle if precedence is cyclic

parent 5627ee50
......@@ -574,7 +574,7 @@ class ConflictPanel extends Composite {
else {
if((resolutionOptions.getSelectionIndex() == 1 && cupConflictsView.currentPrecs.isResolvedInFavorOfShift(srdetails))
|| (resolutionOptions.getSelectionIndex() == 2 && cupConflictsView.currentPrecs.isResolvedInFavorOfReduce(srdetails))) {
//Current action is desired -> Do nothing
// Current action is desired -> Do nothing
return;
}
......@@ -605,11 +605,12 @@ class ConflictPanel extends Composite {
};
private void showCyclicMessageAndResetDropdown(PrecedenceCyclicException e1){
private void showCyclicMessageAndResetDropdown(PrecedenceCyclicException e){
Shell shell = new Shell(Display.getCurrent());
MessageBox box = new MessageBox(shell,SWT.ICON_WARNING | SWT.OK);
box.setText("Information");
box.setMessage("These precedences are inconsistent because they contain a cycle.\nThe precedence that was inserted last is undone.");
box.setMessage("These precedences are inconsistent because they contain a cycle.\n" + e.cycleToString() +"\n"
+ "The precedence that was inserted last is undone.");
box.open();
markIfAffected(cupConflictsView.currentPrecs);
......
package de.tum.in.www2.cupplugin.views;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.Stack;
import java.util.Map.Entry;
......@@ -41,12 +43,34 @@ class PrecedenceToInsert {
* Every Exception is expected to have one of those
*/
private static final long serialVersionUID = -3346362877378712162L;
private List<terminal> cycle;
public PrecedenceCyclicException(String s){
public PrecedenceCyclicException(String s,List<terminal> cycle){
super(s);
this.cycle = cycle;
}
public String cycleToString(){
//Works because the cycle is guaranteed to have at least 3 elements
StringBuilder sb = new StringBuilder();
sb.append(cycle.get(0).name());
sb.append(" ->");
for(int i=1;i < cycle.size()-1;i++){
sb.append(" ");
sb.append(cycle.get(i).name());
sb.append(" ->");
}
sb.append(" ");
sb.append(cycle.get(cycle.size()-1).name());
return sb.toString();
}
}
private LinkedList<terminal> cycle;
private LinkedList<Pair<terminal,Precedence.Type>> result;
/**
......@@ -169,7 +193,7 @@ class PrecedenceToInsert {
precs.remove(lower);
}
throw new PrecedenceCyclicException("");
throw new PrecedenceCyclicException("",cycle);
}
}
......@@ -186,6 +210,8 @@ class PrecedenceToInsert {
tarjanNodeInfos.put(t, tni);
}
cycle = new LinkedList<>();
for(Entry<terminal, TarjanNodeInfo> e : tarjanNodeInfos.entrySet()){
if(e.getValue().index == -1){
if(strongConnect(e.getKey())){
......@@ -223,6 +249,18 @@ class PrecedenceToInsert {
tarjanNodeInfos.get(w).onStack = false;
if(!w.equals(t)){
// Construct cycle
cycle.add(t);
cycle.add(w);
do {
w = tarjanStack.pop();
cycle.add(w);
}
while(!w.equals(t));
//Cycle is in reverse order of what we want -> Reverse it
Collections.reverse(cycle);
return true;
}
}
......@@ -276,7 +314,7 @@ class PrecedenceToInsert {
*/
public String precsToInsert() throws PrecedenceCyclicException{
if(!isAcyclic()){
throw new PrecedenceCyclicException("");
throw new PrecedenceCyclicException("",cycle);
}
topSort();
......
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