Commit 94502fbd authored by Johannes Roith's avatar Johannes Roith

Initial work on Conflict API.

parent b170f2b5
package de.in.tum.www2.cup;
import de.in.tum.www2.cup.internal.lalr_state;
public abstract class Conflict
{
private lalr_state state;
public Conflict(lalr_state state) {
this.state = state;
}
}
package de.in.tum.www2.cup;
import java.util.ArrayList;
import java.util.List;
public class ConflictManager
{
private CupContext context;
private List<Conflict> conflicts;
public List<Conflict> getConflicts() {
return conflicts;
}
public ConflictManager(CupContext context) {
this.context = context;
this.conflicts = new ArrayList<Conflict>();
}
public void addConflict(Conflict conflict) {
this.conflicts.add(conflict);
}
}
......@@ -4,6 +4,7 @@ package de.in.tum.www2.cup;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import de.in.tum.www2.cup.internal.*;
public class CupContext
......@@ -14,6 +15,7 @@ public class CupContext
private Boolean showWarnings = true;
private ErrorManager errorManager;
private ConflictManager conflictManager;
private HashMap<String, Object> singletons = new HashMap<String, Object>();
public <T> T getForContext(Class<T> cls)
......@@ -78,9 +80,14 @@ public class CupContext
public ErrorManager getErrorManager() {
return errorManager;
}
public ConflictManager getConflictManager() {
return conflictManager;
}
public CupContext (IErrorReporter er) {
this.errorManager = new ErrorManager(er);
this.conflictManager = new ConflictManager(this);
}
}
......
......@@ -15,7 +15,7 @@ class Demo
{
public static void main(String[] blah) throws Exception {
IErrorReporter er = new DefaultErrorReporter();
IErrorReporter er = new NoopErrorReporter();
FileInputStream input = new FileInputStream("/Users/jroith/testcup/testspeed.cup");
......@@ -41,10 +41,15 @@ class Demo
System.out.println("QUESTION is a declared terminal! :-)");
// System.out.println("\nComputing tables ...");
// LALRResult lalrResult = LALRResult.Compute(context, context.start_production);
System.out.println("\nComputing tables ...");
LALRResult lalrResult = LALRResult.Compute(context, context.start_production);
for (Conflict conflict : context.getConflictManager().getConflicts()) {
System.out.println(conflict);
}
// System.out.println(lalrResult.getActionTable());
......
package de.in.tum.www2.cup;
import de.in.tum.www2.cup.internal.lalr_state;
public class ReduceReduceConflict extends Conflict
{
public ReduceReduceConflict(lalr_state state) {
super(state);
}
// state (index)
// conflict between:
// item1 ( toString ; has corresponding the_production -> link to Production if in AST)
// item2 ( toString ; has corresponding the_production -> link to Production if in AST)
// symbols (lookahead)
// resolved in favour of item1 / item2 (the_production respectively)
}
package de.in.tum.www2.cup;
import de.in.tum.www2.cup.internal.lalr_state;
public class ShiftReduceConflict extends Conflict
{
public ShiftReduceConflict(lalr_state state) {
super(state);
}
// state (index)
// conflict between:
// item which could be reduced ; toSimpleString has corresponding the_production -> link to Production if in AST)
// item(s) which could be shifted (have corresponding the_production -> link to Production if in AST)
// symbol
// resolved in favour of shifting
}
\ No newline at end of file
......@@ -5,8 +5,12 @@ import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Stack;
import de.in.tum.www2.cup.Conflict;
import de.in.tum.www2.cup.ConflictManager;
import de.in.tum.www2.cup.CupContext;
import de.in.tum.www2.cup.ErrorSource;
import de.in.tum.www2.cup.ReduceReduceConflict;
import de.in.tum.www2.cup.ShiftReduceConflict;
import de.in.tum.www2.cup.internal.production.production_shared;
/** This class represents a state in the LALR viable prefix recognition machine.
......@@ -835,6 +839,11 @@ public class lalr_state {
else
message+="the second production.\n";
Conflict conflict = new ReduceReduceConflict(this);
ConflictManager cm = context.getConflictManager();
cm.addConflict(conflict);
/* count the conflict */
context.num_conflicts++;
context.getErrorManager().Warning(ErrorSource.LALR, message);
......@@ -880,6 +889,11 @@ public class lalr_state {
message += " under symbol "+ shared.find(conflict_sym).name() + "\n"+
" Resolved in favor of shifting.\n";
Conflict conflict = new ShiftReduceConflict(this);
ConflictManager cm = context.getConflictManager();
cm.addConflict(conflict);
/* count the conflict */
context.num_conflicts++;
context.getErrorManager().Warning(ErrorSource.LALR, message);
......
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