Commit 88e54b1b authored by Michael Schwarz's avatar Michael Schwarz

Prepare for connected resolve Reduce/Reduce

parent 1ec32a0f
package de.tum.in.www2.cupplugin.views;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
public class GraphHelper {
public static <X> boolean isReachable(HashMap<X,Set<X>> edges,X from,X to){
HashSet<X> toCheck = new HashSet<>();
if(edges.get(from).size() == 0){
// if the node has no outgoing node, than nothing is reachable
return false;
}
toCheck.addAll(edges.get(from));
while(toCheck.size() > 0){
if(toCheck.contains(to)){
return true;
}
else {
HashSet<X> next = new HashSet<>();
for(X x : toCheck){
next.addAll(edges.get(x));
}
toCheck = next;
}
}
return false;
}
}
\ No newline at end of file
......@@ -359,32 +359,9 @@ class PrecedenceToInsert {
}
// Hereafter we know that both are in there
// This works because we have guarantee that the graph is a DAG
HashSet<terminal> toCheck = new HashSet<>();
if(edges.get(over).size() == 0){
// if the node has no outgoing node, than nothing has precedence over it
return false;
}
toCheck.addAll(edges.get(over));
while(toCheck.size() > 0){
if(toCheck.contains(t)){
return true;
}
else {
HashSet<terminal> next = new HashSet<>();
for(terminal x : toCheck){
next.addAll(edges.get(x));
}
toCheck = next;
}
}
return false;
// t reachable from over -> t has higher precedence
return GraphHelper.isReachable(edges, over, t);
}
/**
......
......@@ -7,11 +7,11 @@ import java.util.List;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import de.in.tum.www2.cup.ast.NonTerminal;
import de.in.tum.www2.cup.ast.ParserResult;
import de.in.tum.www2.cup.ast.Production;
import de.in.tum.www2.cup.ast.ProductionRight;
import de.in.tum.www2.cup.internal.lalr_item;
import de.in.tum.www2.cup.internal.lr_item_core;
import de.tum.in.www2.cupplugin.model.Model;
public class ReduceReduceReorder {
......@@ -49,7 +49,7 @@ public class ReduceReduceReorder {
* the apply function is called
* @param item lalr_item which is to be moved
*/
public void moveItemToVeryEnd(lalr_item item){
public void moveItemToVeryEnd(lr_item_core item){
Production p =(Production)item.the_production().getAstNode().getParent();
String toInsert = "";
......
package de.tum.in.www2.cupplugin.views;
import java.util.HashMap;
import java.util.Set;
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.lr_item_core;
public class ReordersToDo {
private HashMap<lr_item_core,Set<lr_item_core>> edges = new HashMap<>();
// These crazy many overriden versions are necessary because otherwise the lalr_items will
// be put into the set, and their equals checks the lookahead set but we don't want that
// (one of the reasons to favor composition over inheritance)
public boolean isResolvedInFavorOf1(lr_item_core item1,lr_item_core item2){
return isHigher(item1,item2);
}
public boolean isResolvedInFavorOf1(lalr_item item1,lalr_item item2){
try {
return isResolvedInFavorOf1(new lr_item_core(item1.the_production(),item1.dot_pos()),
new lr_item_core(item2.the_production(),item2.dot_pos()));
} catch (internal_error e) {
return false;
}
}
public boolean isResolvedInFavorOf2(lr_item_core item1,lr_item_core item2){
return isHigher(item2,item1);
}
public boolean isResolvedInFavorOf2(lalr_item item1,lalr_item item2){
try {
return isResolvedInFavorOf2(new lr_item_core(item1.the_production(),item1.dot_pos()),
new lr_item_core(item2.the_production(),item2.dot_pos()));
} catch (internal_error e) {
return false;
}
}
public boolean isAffected(lr_item_core item1,lr_item_core item2){
return (edges.containsKey(item1) || edges.containsKey(item2));
}
public boolean isAffected(lalr_item item1,lalr_item item2){
try {
return isAffected(new lr_item_core(item1.the_production(),item1.dot_pos()),
new lr_item_core(item2.the_production(),item2.dot_pos()));
} catch (internal_error e) {
return false;
}
}
private boolean isHigher(lr_item_core high,lr_item_core low){
if(!edges.containsKey(high) || !edges.containsKey(low)){
return false;
}
return GraphHelper.isReachable(edges, low, high);
}
}
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