remove dfa

This commit is contained in:
ChUrl
2020-12-12 16:02:04 +01:00
parent 232b631744
commit f9dcebc82a
10 changed files with 0 additions and 591 deletions

View File

@ -1,16 +0,0 @@
package parser;
import java.util.Set;
public interface ILL1ParsingTable {
String get(String nonterminal, String terminal);
String getStartSymbol();
Set<String> getNonterminals();
Set<String> getTerminals();
String getEpsilon();
}

View File

@ -1,49 +0,0 @@
package util.dfa;
import java.util.Collection;
import java.util.Set;
public class DFA {
private final Set<INode> nodes;
private final INode start;
public DFA(Set<INode> nodes, INode start, Collection<IEdge> edges) {
this.nodes = nodes;
this.start = start;
for (INode node : nodes) {
edges.stream()
.filter(edge -> edge.getStart() == node)
.forEach(node::addEdge);
}
}
public INode getStart() {
return this.start;
}
public Set<INode> getNodes() {
return this.nodes;
}
public String accept(String word) {
return this.accept(word, this.start);
}
private String accept(String input, INode current) {
if (input.isEmpty()) {
return current.isFinal()
? current.getName() + " akzeptieren"
: current.getName() + " ablehnen";
}
char c = input.charAt(0);
if (!current.hasNext(c)) {
return current.getName() + " ablehnen";
}
return current.getName() + this.accept(input.substring(1), current.getNext(c));
}
}

View File

@ -1,8 +0,0 @@
package util.dfa;
public class DFANoSuchEdgeException extends RuntimeException {
public DFANoSuchEdgeException(String message) {
super(message);
}
}

View File

@ -1,29 +0,0 @@
package util.dfa;
public class Edge implements IEdge {
private final INode start;
private final INode end;
private final char read;
public Edge(INode start, char read, INode end) {
this.start = start;
this.read = read;
this.end = end;
}
@Override
public INode getStart() {
return this.start;
}
@Override
public INode getEnd() {
return this.end;
}
@Override
public char getChar() {
return this.read;
}
}

View File

@ -1,10 +0,0 @@
package util.dfa;
public interface IEdge {
INode getStart();
INode getEnd();
char getChar();
}

View File

@ -1,18 +0,0 @@
package util.dfa;
import java.util.Collection;
public interface INode {
String getName();
boolean isFinal();
void addEdge(IEdge edge);
Collection<IEdge> getEdges();
INode getNext(char c);
boolean hasNext(char c);
}

View File

@ -1,51 +0,0 @@
package util.dfa;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
public class Node implements INode {
private final String name;
private final boolean fin;
private final Map<Character, IEdge> edges = new HashMap<>();
public Node(String name, boolean fin) {
this.name = name;
this.fin = fin;
}
@Override
public String getName() {
return this.name;
}
@Override
public boolean isFinal() {
return this.fin;
}
@Override
public void addEdge(IEdge edge) {
this.edges.put(edge.getChar(), edge);
}
@Override
public Collection<IEdge> getEdges() {
return this.edges.values();
}
@Override
public INode getNext(char c) {
return Optional.ofNullable(this.edges.get(c))
.map(IEdge::getEnd)
.orElseThrow(() -> new DFANoSuchEdgeException("Can't read " + c + " when in " + this.name));
}
@Override
public boolean hasNext(char c) {
return Optional.ofNullable(this.edges.get(c))
.isPresent();
}
}

View File

@ -1,62 +0,0 @@
package util.tools;
import util.dfa.DFA;
import util.dfa.IEdge;
import util.dfa.INode;
import java.util.HashMap;
import java.util.Map;
public final class DFAViewUtil {
static int i;
private DFAViewUtil() {}
public static String toDot(DFA automaton) {
StringBuilder sb = new StringBuilder();
sb.append("\n");
sb.append("digraph G {\n");
Map<INode, Integer> ids = new HashMap<>();
for (INode node : automaton.getNodes()) {
ids.put(node, i++);
}
for (INode node : automaton.getNodes()) {
sb.append("node");
sb.append(ids.get(node));
sb.append(" [label=\"");
sb.append(node.getName());
sb.append("\"");
if (node.isFinal()) {
sb.append(" shape=doublecircle");
}
sb.append("]\n");
if (automaton.getStart() == node) {
sb.append("start [style=invisible]\n");
sb.append("start -> node");
sb.append(ids.get(node));
sb.append("\n");
}
for (IEdge edge : node.getEdges()) {
sb.append("node");
sb.append(ids.get(node));
sb.append("->");
sb.append("node");
sb.append(ids.get(edge.getEnd()));
sb.append(" [label=\" ");
if (edge.getChar() == '\0') {
sb.append("&epsilon;");
} else {
sb.append(edge.getChar());
}
sb.append("\"]\n");
}
}
sb.append("}\n");
return sb.toString();
}
}

View File

@ -1,208 +0,0 @@
//package parser;
//
//import org.junit.jupiter.api.BeforeAll;
//import org.junit.jupiter.api.Disabled;
//import org.junit.jupiter.api.Test;
//import parser.grammar.Grammar;
//
//import java.io.IOException;
//import java.net.URISyntaxException;
//import java.nio.file.Path;
//import java.nio.file.Paths;
//import java.util.AbstractMap.SimpleEntry;
//import java.util.Arrays;
//import java.util.HashMap;
//import java.util.HashSet;
//import java.util.Map;
//import java.util.Set;
//
//import static org.assertj.core.api.Assertions.assertThat;
//import static org.assertj.core.api.Assertions.assertThatThrownBy;
//
//class LL1ParserTest {
//
// private static ILL1ParsingTable table0;
// private static ILL1ParsingTable table1;
//
// @BeforeAll
// static void setUp() {
// table0 = initTable0();
// table1 = initTable1();
// }
//
// private static ILL1ParsingTable initTable0() {
// /*
// S -> a
// S -> i E t S
// E -> b
// */
// Set<String> nonterminals;
// String[] narray = {"S", "E"};
// nonterminals = new HashSet<>(Arrays.asList(narray));
//
// Set<String> terminals;
// String[] tarray = {"a", "b", "e", "i", "t"};
// terminals = new HashSet<>(Arrays.asList(tarray));
//
// String startSymbol = "S";
// String epsilonSymbol = "epsilon";
//
// Map<Map.Entry<String, String>, String> map;
// map = new HashMap<>();
// String production0 = "a";
// map.put(new SimpleEntry<>("S", "a"), production0);
// String production1 = "i E t S";
// map.put(new SimpleEntry<>("S", "i"), production1);
// String production2 = "b";
// map.put(new SimpleEntry<>("E", "b"), production2);
//
// Grammar grammar = new Grammar(terminals, nonterminals,
// startSymbol, epsilonSymbol,
// null);
//
// return new LL1ParsingTable(grammar, map);
// }
//
// private static ILL1ParsingTable initTable1() {
// /*
// Folie 4b/32
// */
// Set<String> nonterminals;
// String[] narray = {"E", "T", "E2", "T2", "F"};
// nonterminals = new HashSet<>(Arrays.asList(narray));
//
// Set<String> terminals;
// String[] tarray = {"id", "+", "*", "(", ")"};
// terminals = new HashSet<>(Arrays.asList(tarray));
//
// String startSymbol = "E";
// String epsilonSymbol = "epsilon";
//
// Map<Map.Entry<String, String>, String> map;
// map = new HashMap<>();
// String production0 = "T E2";
// map.put(new SimpleEntry<>("E", "id"), production0);
// String production1 = "T E2";
// map.put(new SimpleEntry<>("E", "("), production1);
// String production2 = "+ T E2";
// map.put(new SimpleEntry<>("E2", "+"), production2);
// String production3 = "epsilon";
// map.put(new SimpleEntry<>("E2", ")"), production3);
// String production4 = "epsilon";
// map.put(new SimpleEntry<>("E2", "$"), production4);
// String production5 = "F T2";
// map.put(new SimpleEntry<>("T", "id"), production5);
// String production6 = "F T2";
// map.put(new SimpleEntry<>("T", "("), production6);
// String production7 = "epsilon";
// map.put(new SimpleEntry<>("T2", "+"), production7);
// String production8 = "* F T2";
// map.put(new SimpleEntry<>("T2", "*"), production8);
// String production9 = "epsilon";
// map.put(new SimpleEntry<>("T2", ")"), production9);
// String production10 = "epsilon";
// map.put(new SimpleEntry<>("T2", "$"), production10);
// String production11 = "id";
// map.put(new SimpleEntry<>("F", "id"), production11);
// String production12 = "( E )";
// map.put(new SimpleEntry<>("F", "("), production12);
//
// Grammar grammar = new Grammar(terminals, nonterminals,
// startSymbol, epsilonSymbol,
// null);
//
// return new LL1ParsingTable(grammar, map);
// }
//
// @Test
// void testIfThenElse() throws MyParseException {
// LL1Parser parser = new LL1Parser(table0);
//
// String[] token1 = {"i", "b", "t", "a"};
// String[] token2 = {"i", "b", "t", "i", "b", "t", "a"};
// String[] token3 = {"i", "b", "t", "i", "b", "t", "i", "b", "t", "a"};
//
// assertThat(parser.parse(Arrays.asList(token1))).isTrue();
// assertThat(parser.parse(Arrays.asList(token2))).isTrue();
// assertThat(parser.parse(Arrays.asList(token3))).isTrue();
// }
//
// @Test
// void testIfThenElseFromFile() throws MyParseException, IOException, URISyntaxException {
// Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/SimpleGrammar0.grammar").toURI());
// LL1Parser parser = LL1Parser.fromGrammar(path);
//
// String[] token1 = {"i", "b", "t", "a"};
// String[] token2 = {"i", "b", "t", "i", "b", "t", "a"};
// String[] token3 = {"i", "b", "t", "i", "b", "t", "i", "b", "t", "a"};
//
// assertThat(parser.parse(Arrays.asList(token1))).isTrue();
// assertThat(parser.parse(Arrays.asList(token2))).isTrue();
// assertThat(parser.parse(Arrays.asList(token3))).isTrue();
// }
//
// @Test
// void testException0() {
// LL1Parser parser = new LL1Parser(table0);
// String[] token1 = {"i", "b", "t"};
//
// assertThatThrownBy(() -> parser.parse(Arrays.asList(token1))).isInstanceOf(MyParseException.class);
// }
//
// @Test
// void testException1() {
// LL1Parser parser = new LL1Parser(table0);
// String[] token1 = {"i", "b", "t", "t"};
//
// assertThatThrownBy(() -> parser.parse(Arrays.asList(token1))).isInstanceOf(MyParseException.class);
// }
//
// @Test
// void testArithExpression() {
// LL1Parser parser = new LL1Parser(table1);
//
// String[] token1 = {"id", "+", "id"};
// String[] token2 = {"id", "*", "id", "*", "id"};
// String[] token3 = {"id", "+", "id", "*", "id"};
//
// assertThat(parser.parse(Arrays.asList(token1))).isTrue();
// assertThat(parser.parse(Arrays.asList(token2))).isTrue();
// assertThat(parser.parse(Arrays.asList(token3))).isTrue();
// }
//
// @Test
// void testArithExpressionFromFile() throws MyParseException, IOException, URISyntaxException {
// Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/SimpleGrammar1.grammar").toURI());
// LL1Parser parser = LL1Parser.fromGrammar(path);
//
// String[] token1 = {"id", "+", "id"};
// String[] token2 = {"id", "*", "id", "*", "id"};
// String[] token3 = {"id", "+", "id", "*", "id"};
//
// assertThat(parser.parse(Arrays.asList(token1))).isTrue();
// assertThat(parser.parse(Arrays.asList(token2))).isTrue();
// assertThat(parser.parse(Arrays.asList(token3))).isTrue();
// }
//
// @Test
// void testException2() {
// LL1Parser parser = new LL1Parser(table1);
// String[] token1 = {"id", "id"};
//
// assertThatThrownBy(() -> parser.parse(Arrays.asList(token1))).isInstanceOf(MyParseException.class);
// }
//
// @Disabled
// @Test
// void testDanglingElse() throws URISyntaxException, IOException {
// Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/DanglingElse.grammar").toURI());
// LL1Parser parser = LL1Parser.fromGrammar(path);
//
// String[] token1 = {"if", "expr", "then", "other"};
// String[] token2 = {"if", "expr", "then", "other", "else", "other"};
//
// assertThat(parser.parse(Arrays.asList(token1))).isTrue();
// assertThat(parser.parse(Arrays.asList(token2))).isTrue();
// }
//
//}

View File

@ -1,140 +0,0 @@
package util.dfa;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import util.tools.DFAViewUtil;
import java.util.HashSet;
import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
class DFATest {
@Test
@DisplayName("DFA has 2 Nodes, 2 Edges (Valid transitions)")
void testTwoNodesTwoEdges() {
Set<INode> nodes = new HashSet<>();
Node b = new Node("B", false);
Node c = new Node("C", true);
nodes.add(b);
nodes.add(c);
Set<IEdge> edges = new HashSet<>();
edges.add(new Edge(c, '0', c));
edges.add(new Edge(c, '1', b));
DFA dfa = new DFA(nodes, c, edges); // Hier werden die Nodes mit Edges befüllt
System.out.println(DFAViewUtil.toDot(dfa));
assertThat(c.getNext('0').getName()).isEqualTo("C");
assertThat(c.getNext('1').getName()).isEqualTo("B");
}
@Test
@DisplayName("DFA has 2 Nodes, 1 Edge (Invalid transition)")
void testTwoNodesNoSuchEdge() {
Set<INode> nodes = new HashSet<>();
Node b = new Node("B", false);
Node c = new Node("C", true);
nodes.add(b);
nodes.add(c);
Set<IEdge> edges = new HashSet<>();
edges.add(new Edge(c, '0', c));
DFA dfa = new DFA(nodes, c, edges);
System.out.println(DFAViewUtil.toDot(dfa));
assertThat(c.getNext('0').getName()).isEqualTo("C");
assertThatThrownBy(() -> c.getNext('1')).isInstanceOf(DFANoSuchEdgeException.class);
}
@Test
@DisplayName("DFA has 5 Nodes, 10 Edges (Valid transitions)")
void testAcceptByFinalState() {
Set<INode> nodes = new HashSet<>();
Node a = new Node("A", false);
Node b = new Node("B", false);
Node c = new Node("C", true);
Node d = new Node("D", false);
Node e = new Node("E", false);
nodes.add(a);
nodes.add(b);
nodes.add(c);
nodes.add(d);
nodes.add(e);
Set<IEdge> edges = new HashSet<>();
edges.add(new Edge(c, '0', c));
edges.add(new Edge(c, '1', b));
edges.add(new Edge(b, '0', e));
edges.add(new Edge(b, '1', a));
edges.add(new Edge(a, '0', b));
edges.add(new Edge(a, '1', e));
edges.add(new Edge(d, '0', a));
edges.add(new Edge(d, '1', d));
edges.add(new Edge(e, '0', d));
edges.add(new Edge(e, '1', c));
DFA dfa = new DFA(nodes, c, edges);
System.out.println(DFAViewUtil.toDot(dfa));
assertThat(dfa.accept("10010")).isEqualTo("CBEDDA ablehnen");
assertThat(dfa.accept("11001")).isEqualTo("CBABEC akzeptieren");
assertThat(dfa.accept("1")).isEqualTo("CB ablehnen");
assertThat(dfa.accept("10")).isEqualTo("CBE ablehnen");
assertThat(dfa.accept("1011")).isEqualTo("CBECB ablehnen");
assertThat(dfa.accept("1011")).isEqualTo("CBECB ablehnen");
assertThat(dfa.accept("100100011")).isEqualTo("CBEDDABECB ablehnen");
assertThat(dfa.accept("11001")).isEqualTo("CBABEC akzeptieren");
assertThat(dfa.accept("110010")).isEqualTo("CBABECC akzeptieren");
assertThat(dfa.accept("1100100")).isEqualTo("CBABECCC akzeptieren");
assertThat(dfa.accept("11001000")).isEqualTo("CBABECCCC akzeptieren");
assertThat(dfa.accept("101")).isEqualTo("CBEC akzeptieren");
assertThat(dfa.accept("10010001")).isEqualTo("CBEDDABEC akzeptieren");
assertThat(dfa.accept("1001110001")).isEqualTo("CBEDDDDABEC akzeptieren");
assertThat(dfa.accept("100111110001")).isEqualTo("CBEDDDDDDABEC akzeptieren");
}
@Test
@DisplayName("DFA has 5 Nodes, 10 Edges (Invalid transitions)")
void testAcceptByInput() {
Set<INode> nodes = new HashSet<>();
Node a = new Node("A", false);
Node b = new Node("B", true);
Node c = new Node("C", false);
Node d = new Node("D", true);
Node e = new Node("E", false);
nodes.add(a);
nodes.add(b);
nodes.add(c);
nodes.add(d);
nodes.add(e);
Set<IEdge> edges = new HashSet<>();
edges.add(new Edge(a, 'a', b));
edges.add(new Edge(a, 'b', c));
edges.add(new Edge(b, 'a', d));
edges.add(new Edge(b, 'b', e));
edges.add(new Edge(c, 'b', b));
edges.add(new Edge(d, 'b', b));
edges.add(new Edge(e, 'a', d));
DFA dfa = new DFA(nodes, a, edges);
System.out.println(DFAViewUtil.toDot(dfa));
assertThat(dfa.accept("a")).isEqualTo("AB akzeptieren");
assertThat(dfa.accept("aa")).isEqualTo("ABD akzeptieren");
assertThat(dfa.accept("bbab")).isEqualTo("ACBDB akzeptieren");
assertThat(dfa.accept("abb")).isEqualTo("ABE ablehnen");
assertThat(dfa.accept("aabb")).isEqualTo("ABDBE ablehnen");
assertThat(dfa.accept("bbb")).isEqualTo("ACBE ablehnen");
}
}