Removed code from teacher
This commit is contained in:
@@ -272,16 +272,6 @@ public class Main {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// System.out.println("Here my code be");
|
|
||||||
// com.imegumii.codevandocent.Automata<String> t1 = TestAutomata.getExampleSlide8Lesson2();
|
|
||||||
// t1.printTransitions();
|
|
||||||
|
|
||||||
// TestRegExp testreg = new TestRegExp();
|
|
||||||
// testreg.testLanguage();
|
|
||||||
|
|
||||||
|
|
||||||
// System.out.println("Zo moet het dus, nu onze beurt.");
|
|
||||||
|
|
||||||
// Practicum1();
|
// Practicum1();
|
||||||
|
|
||||||
// Practicum2();
|
// Practicum2();
|
||||||
|
|||||||
@@ -1,105 +0,0 @@
|
|||||||
package com.imegumii.codevandocent;
|
|
||||||
/**
|
|
||||||
* The class Automata represents both DFA and Automata: some Automata's are also DFA
|
|
||||||
* Using the method isDFA we can check this
|
|
||||||
*
|
|
||||||
* We use '$' to denote the empty symbol epsilon
|
|
||||||
*
|
|
||||||
* @author Paul de Mast
|
|
||||||
* @version 1.0
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
public class Automata<T extends Comparable>
|
|
||||||
{
|
|
||||||
|
|
||||||
// Or use a Map structure
|
|
||||||
private Set<Transition <T>> transitions;
|
|
||||||
|
|
||||||
private SortedSet<T> states;
|
|
||||||
private SortedSet<T> startStates;
|
|
||||||
private SortedSet<T> finalStates;
|
|
||||||
private SortedSet<Character> symbols;
|
|
||||||
|
|
||||||
public Automata()
|
|
||||||
{
|
|
||||||
this(new TreeSet<Character>());
|
|
||||||
}
|
|
||||||
|
|
||||||
public Automata(Character [] s)
|
|
||||||
{
|
|
||||||
this(new TreeSet<Character>(Arrays.asList(s)) );
|
|
||||||
}
|
|
||||||
|
|
||||||
public Automata(SortedSet<Character> symbols)
|
|
||||||
{
|
|
||||||
transitions = new TreeSet<Transition<T>>();
|
|
||||||
states = new TreeSet<T>();
|
|
||||||
startStates = new TreeSet<T>();
|
|
||||||
finalStates = new TreeSet<T>();
|
|
||||||
this.setAlphabet(symbols);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAlphabet(Character [] s)
|
|
||||||
{
|
|
||||||
this.setAlphabet(new TreeSet<Character>(Arrays.asList(s)));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAlphabet(SortedSet<Character> symbols)
|
|
||||||
{
|
|
||||||
this.symbols = symbols;
|
|
||||||
}
|
|
||||||
|
|
||||||
public SortedSet<Character> getAlphabet()
|
|
||||||
{
|
|
||||||
return symbols;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addTransition(Transition<T> t)
|
|
||||||
{
|
|
||||||
transitions.add(t);
|
|
||||||
states.add(t.getFromState());
|
|
||||||
states.add(t.getToState());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void defineAsStartState(T t)
|
|
||||||
{
|
|
||||||
// if already in states no problem because a Set will remove duplicates.
|
|
||||||
states.add(t);
|
|
||||||
startStates.add(t);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void defineAsFinalState(T t)
|
|
||||||
{
|
|
||||||
// if already in states no problem because a Set will remove duplicates.
|
|
||||||
states.add(t);
|
|
||||||
finalStates.add(t);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void printTransitions()
|
|
||||||
{
|
|
||||||
|
|
||||||
for (Transition<T> t : transitions)
|
|
||||||
{
|
|
||||||
System.out.println (t);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isDFA()
|
|
||||||
{
|
|
||||||
boolean isDFA = true;
|
|
||||||
|
|
||||||
for (T from : states)
|
|
||||||
{
|
|
||||||
for (char symbol : symbols)
|
|
||||||
{
|
|
||||||
// isDFA = isDFA && getToStates(from, symbol).size() == 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return isDFA;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,140 +0,0 @@
|
|||||||
package com.imegumii.codevandocent;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Voorbeeld class voor het representeren van reguliere expressies
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class RegExp
|
|
||||||
{
|
|
||||||
Operator operator;
|
|
||||||
String terminals;
|
|
||||||
|
|
||||||
// De mogelijke operatoren voor een reguliere expressie (+, *, |, .)
|
|
||||||
// Daarnaast ook een operator definitie voor 1 keer repeteren (default)
|
|
||||||
public enum Operator { PLUS, STAR, OR, DOT, ONE}
|
|
||||||
|
|
||||||
RegExp left;
|
|
||||||
RegExp right;
|
|
||||||
|
|
||||||
static final Comparator<String> compareByLength
|
|
||||||
= new Comparator<String> ()
|
|
||||||
{
|
|
||||||
public int compare(String s1, String s2)
|
|
||||||
{
|
|
||||||
if (s1.length() == s2.length())
|
|
||||||
{return s1.compareTo(s2);}
|
|
||||||
else
|
|
||||||
{return s1.length() - s2.length();}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public RegExp()
|
|
||||||
{
|
|
||||||
operator = Operator.ONE;
|
|
||||||
terminals = "";
|
|
||||||
left = null;
|
|
||||||
right = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public RegExp(String p)
|
|
||||||
{
|
|
||||||
operator = Operator.ONE;
|
|
||||||
terminals = p;
|
|
||||||
left = null;
|
|
||||||
right = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public RegExp plus()
|
|
||||||
{
|
|
||||||
RegExp result = new RegExp();
|
|
||||||
result.operator = Operator.PLUS;
|
|
||||||
result.left = this;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public RegExp star()
|
|
||||||
{
|
|
||||||
RegExp result = new RegExp();
|
|
||||||
result.operator = Operator.STAR;
|
|
||||||
result.left = this;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public RegExp or(RegExp e2)
|
|
||||||
{
|
|
||||||
RegExp result = new RegExp();
|
|
||||||
result.operator = Operator.OR;
|
|
||||||
result.left = this;
|
|
||||||
result.right = e2;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public RegExp dot(RegExp e2)
|
|
||||||
{
|
|
||||||
RegExp result = new RegExp();
|
|
||||||
result.operator = Operator.DOT;
|
|
||||||
result.left = this;
|
|
||||||
result.right = e2;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public SortedSet <String> getLanguage(int maxSteps)
|
|
||||||
{
|
|
||||||
SortedSet<String> emptyLanguage = new TreeSet<String>(compareByLength);
|
|
||||||
SortedSet<String> languageResult = new TreeSet<String>(compareByLength);
|
|
||||||
|
|
||||||
SortedSet<String> languageLeft, languageRight;
|
|
||||||
|
|
||||||
if (maxSteps < 1) return emptyLanguage;
|
|
||||||
|
|
||||||
switch (this.operator) {
|
|
||||||
case ONE:
|
|
||||||
{languageResult.add(terminals);}
|
|
||||||
|
|
||||||
case OR:
|
|
||||||
languageLeft = left == null ? emptyLanguage : left.getLanguage(maxSteps - 1);
|
|
||||||
languageRight = right == null ? emptyLanguage : right.getLanguage(maxSteps - 1);
|
|
||||||
languageResult.addAll (languageLeft);
|
|
||||||
languageResult.addAll (languageRight);
|
|
||||||
break;
|
|
||||||
|
|
||||||
|
|
||||||
case DOT:
|
|
||||||
languageLeft = left == null ? emptyLanguage : left.getLanguage(maxSteps - 1);
|
|
||||||
languageRight = right == null ? emptyLanguage : right.getLanguage(maxSteps - 1);
|
|
||||||
for (String s1 : languageLeft)
|
|
||||||
for (String s2 : languageRight)
|
|
||||||
{languageResult.add (s1 + s2);}
|
|
||||||
break;
|
|
||||||
|
|
||||||
// STER(*) en PLUS(+) kunnen we bijna op dezelfde manier uitwerken:
|
|
||||||
case STAR:
|
|
||||||
case PLUS:
|
|
||||||
languageLeft = left == null ? emptyLanguage : left.getLanguage(maxSteps - 1);
|
|
||||||
languageResult.addAll(languageLeft);
|
|
||||||
for (int i = 1; i < maxSteps; i++)
|
|
||||||
{ HashSet<String> languageTemp = new HashSet<String>(languageResult);
|
|
||||||
for (String s1 : languageLeft)
|
|
||||||
{ for (String s2 : languageTemp)
|
|
||||||
{ languageResult.add (s1+s2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (this.operator == Operator.STAR)
|
|
||||||
{languageResult.add("");}
|
|
||||||
break;
|
|
||||||
|
|
||||||
|
|
||||||
default:
|
|
||||||
System.out.println ("getLanguage is nog niet gedefinieerd voor de operator: " + this.operator);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return languageResult;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,76 +0,0 @@
|
|||||||
package com.imegumii.codevandocent;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This file shows how to build up some example automata
|
|
||||||
*
|
|
||||||
* @author (your name)
|
|
||||||
* @version (a version number or a date)
|
|
||||||
*/
|
|
||||||
public class TestAutomata
|
|
||||||
{
|
|
||||||
|
|
||||||
static public Automata<String> getExampleSlide8Lesson2()
|
|
||||||
{
|
|
||||||
Character [] alphabet = {'a', 'b'};
|
|
||||||
Automata<String> m = new Automata<String>(alphabet);
|
|
||||||
|
|
||||||
m.addTransition( new Transition<String> ("q0", 'a', "q1") );
|
|
||||||
m.addTransition( new Transition<String> ("q0", 'b', "q4") );
|
|
||||||
|
|
||||||
m.addTransition( new Transition<String> ("q1", 'a', "q4") );
|
|
||||||
m.addTransition( new Transition<String> ("q1", 'b', "q2") );
|
|
||||||
|
|
||||||
m.addTransition( new Transition<String> ("q2", 'a', "q3") );
|
|
||||||
m.addTransition( new Transition<String> ("q2", 'b', "q4") );
|
|
||||||
|
|
||||||
m.addTransition( new Transition<String> ("q3", 'a', "q1") );
|
|
||||||
m.addTransition( new Transition<String> ("q3", 'b', "q2") );
|
|
||||||
|
|
||||||
// the error state, loops for a and b:
|
|
||||||
m.addTransition( new Transition<String> ("q4", 'a') );
|
|
||||||
m.addTransition( new Transition<String> ("q4", 'b') );
|
|
||||||
|
|
||||||
// only on start state in a dfa:
|
|
||||||
m.defineAsStartState("q0");
|
|
||||||
|
|
||||||
// two final states:
|
|
||||||
m.defineAsFinalState("q2");
|
|
||||||
m.defineAsFinalState("q3");
|
|
||||||
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static public Automata<String> getExampleSlide14Lesson2()
|
|
||||||
{
|
|
||||||
Character [] alphabet = {'a', 'b'};
|
|
||||||
Automata<String> m = new Automata<String>(alphabet);
|
|
||||||
|
|
||||||
m.addTransition( new Transition<String> ("A", 'a', "C") );
|
|
||||||
m.addTransition( new Transition<String> ("A", 'b', "B") );
|
|
||||||
m.addTransition( new Transition<String> ("A", 'b', "C") );
|
|
||||||
|
|
||||||
m.addTransition( new Transition<String> ("B", 'b', "C") );
|
|
||||||
m.addTransition( new Transition<String> ("B", "C") );
|
|
||||||
|
|
||||||
m.addTransition( new Transition<String> ("C", 'a', "D") );
|
|
||||||
m.addTransition( new Transition<String> ("C", 'a', "E") );
|
|
||||||
m.addTransition( new Transition<String> ("C", 'b', "D") );
|
|
||||||
|
|
||||||
m.addTransition( new Transition<String> ("D", 'a', "B") );
|
|
||||||
m.addTransition( new Transition<String> ("D", 'a', "C") );
|
|
||||||
|
|
||||||
m.addTransition( new Transition<String> ("E", 'a') );
|
|
||||||
m.addTransition( new Transition<String> ("E", "D") );
|
|
||||||
|
|
||||||
// only on start state in a dfa:
|
|
||||||
m.defineAsStartState("A");
|
|
||||||
|
|
||||||
// two final states:
|
|
||||||
m.defineAsFinalState("C");
|
|
||||||
m.defineAsFinalState("E");
|
|
||||||
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
package com.imegumii.codevandocent;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Write a description of class TestRegExp here.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class TestRegExp
|
|
||||||
{
|
|
||||||
private RegExp expr1, expr2, expr3, expr4, expr5, a, b, all;
|
|
||||||
|
|
||||||
public TestRegExp()
|
|
||||||
{
|
|
||||||
a = new RegExp("a");
|
|
||||||
b = new RegExp("b");
|
|
||||||
|
|
||||||
// expr1: "baa"
|
|
||||||
expr1 = new RegExp("baa");
|
|
||||||
// expr2: "bb"
|
|
||||||
expr2 = new RegExp("bb");
|
|
||||||
// expr3: "baa | baa"
|
|
||||||
expr3 = expr1.or(expr2);
|
|
||||||
|
|
||||||
// all: "(a|b)*"
|
|
||||||
all = (a.or(b)).star();
|
|
||||||
|
|
||||||
// expr4: "(baa | baa)+"
|
|
||||||
expr4 = expr3.plus();
|
|
||||||
// expr5: "(baa | baa)+ (a|b)*"
|
|
||||||
expr5 = expr4.dot(all);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testLanguage()
|
|
||||||
{
|
|
||||||
System.out.println("taal van (baa):\n" + expr1.getLanguage(5));
|
|
||||||
System.out.println("taal van (bb):\n" + expr2.getLanguage(5));
|
|
||||||
System.out.println("taal van (baa | bb):\n" + expr3.getLanguage(5));
|
|
||||||
|
|
||||||
System.out.println("taal van (a|b)*:\n" + all.getLanguage(5));
|
|
||||||
System.out.println("taal van (baa | bb)+:\n" + expr4.getLanguage(5));
|
|
||||||
System.out.println("taal van (baa | bb)+ (a|b)*:\n" + expr5.getLanguage(6));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,86 +0,0 @@
|
|||||||
package com.imegumii.codevandocent;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The class Automata represents both DFA and Automata: some Automata's are also DFA
|
|
||||||
* Using the method isDFA we can check this
|
|
||||||
*
|
|
||||||
* We use '$' to denote the empty symbol epsilon
|
|
||||||
*
|
|
||||||
* @author Paul de Mast
|
|
||||||
* @version 1.0
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class Transition<T extends Comparable> implements Comparable<Transition<T>>
|
|
||||||
{
|
|
||||||
|
|
||||||
public static final char EPSILON = '$';
|
|
||||||
|
|
||||||
private T fromState;
|
|
||||||
private char symbol;
|
|
||||||
private T toState;
|
|
||||||
|
|
||||||
// this constructor can be used to define loops:
|
|
||||||
public Transition(T fromOrTo, char s)
|
|
||||||
{
|
|
||||||
this (fromOrTo, s, fromOrTo);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Transition(T from, T to)
|
|
||||||
{
|
|
||||||
this(from, EPSILON, to);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public Transition(T from, char s, T to)
|
|
||||||
{
|
|
||||||
this.fromState = from;
|
|
||||||
this.symbol = s;
|
|
||||||
this.toState = to;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// overriding equals
|
|
||||||
public boolean equals ( Object other )
|
|
||||||
{
|
|
||||||
if ( other == null )
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
else if ( other instanceof Transition )
|
|
||||||
{
|
|
||||||
return this.fromState.equals (((Transition )other ).fromState) && this.toState.equals (((Transition )other ).toState) && this.symbol == (((Transition )other ).symbol);
|
|
||||||
}
|
|
||||||
else return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public int compareTo(Transition<T> t)
|
|
||||||
{
|
|
||||||
int fromCmp = fromState.compareTo(t.fromState);
|
|
||||||
int symbolCmp = new Character (symbol).compareTo(new Character (t.symbol));
|
|
||||||
int toCmp = toState.compareTo(t.toState);
|
|
||||||
|
|
||||||
return (fromCmp != 0 ? fromCmp : (symbolCmp != 0 ? symbolCmp : toCmp));
|
|
||||||
}
|
|
||||||
|
|
||||||
public T getFromState()
|
|
||||||
{
|
|
||||||
return fromState;
|
|
||||||
}
|
|
||||||
|
|
||||||
public T getToState()
|
|
||||||
{
|
|
||||||
return toState;
|
|
||||||
}
|
|
||||||
|
|
||||||
public char getSymbol()
|
|
||||||
{
|
|
||||||
return symbol;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String toString()
|
|
||||||
{
|
|
||||||
return "(" + this.getFromState() + ", " + this.getSymbol() + ")" + "-->" + this.getToState();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user