remove dfa
This commit is contained in:
@ -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();
|
||||
// }
|
||||
//
|
||||
//}
|
||||
@ -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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user