update tests, disable old parser tests bc not token-based

This commit is contained in:
Christoph
2020-12-10 19:17:54 +01:00
parent 336a94d8a3
commit 66e672d142
3 changed files with 257 additions and 224 deletions

View File

@ -2,6 +2,7 @@ package lexer;
import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.Lexer; import org.antlr.v4.runtime.Lexer;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -84,7 +85,7 @@ class LexerTest {
List<String> token = this.getSymbols(lex); List<String> token = this.getSymbols(lex);
assertThat(token).hasSize(58) assertThat(token).hasSize(68)
.containsSequence("WHILE", .containsSequence("WHILE",
"L_PAREN", "L_PAREN",
"IDENTIFIER", "IDENTIFIER",
@ -127,7 +128,7 @@ class LexerTest {
List<String> token = this.getSymbols(lex); List<String> token = this.getSymbols(lex);
assertThat(token).hasSize(60) assertThat(token).hasSize(96)
.containsSequence("IF", .containsSequence("IF",
"L_PAREN", "L_PAREN",
"IDENTIFIER", "IDENTIFIER",

View File

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

View File

@ -3,7 +3,9 @@ package parser;
import lexer.StupsLexer; import lexer.StupsLexer;
import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.Lexer; import org.antlr.v4.runtime.Lexer;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import parser.grammar.Grammar;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
@ -30,21 +32,14 @@ class LexerParserGrammarTest {
return null; return null;
} }
private List<String> getSymbols(Lexer lex) {
return lex.getAllTokens().stream()
.map(tok -> lex.getVocabulary().getSymbolicName(tok.getType()))
.collect(Collectors.toUnmodifiableList());
}
@Test @Test
void testEmptyFile() throws URISyntaxException, IOException { void testEmptyFile() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI()); Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
LL1Parser parser = LL1Parser.fromGrammar(path); LL1Parser parser = LL1Parser.fromGrammar(path);
Lexer lex = this.initLexer("EmptyFile.stups"); Lexer lex = this.initLexer("EmptyFile.stups");
List<String> token = this.getSymbols(lex);
assertThat(parser.parse(token)).isTrue(); assertThat(parser.parse(lex.getAllTokens(), lex.getVocabulary())).isTrue();
} }
@Test @Test
@ -53,9 +48,8 @@ class LexerParserGrammarTest {
LL1Parser parser = LL1Parser.fromGrammar(path); LL1Parser parser = LL1Parser.fromGrammar(path);
Lexer lex = this.initLexer("EmptyMain.stups"); Lexer lex = this.initLexer("EmptyMain.stups");
List<String> token = this.getSymbols(lex);
assertThat(parser.parse(token)).isTrue(); assertThat(parser.parse(lex.getAllTokens(), lex.getVocabulary())).isTrue();
} }
@Test @Test
@ -64,9 +58,38 @@ class LexerParserGrammarTest {
LL1Parser parser = LL1Parser.fromGrammar(path); LL1Parser parser = LL1Parser.fromGrammar(path);
Lexer lex = this.initLexer("GeneralComment.stups"); Lexer lex = this.initLexer("GeneralComment.stups");
List<String> token = this.getSymbols(lex);
assertThat(parser.parse(token)).isTrue(); assertThat(parser.parse(lex.getAllTokens(), lex.getVocabulary())).isTrue();
}
@Test
void tesMultiDecl() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
LL1Parser parser = LL1Parser.fromGrammar(path);
Lexer lex = this.initLexer("MultipleDeclarations.stups");
assertThat(parser.parse(lex.getAllTokens(), lex.getVocabulary())).isTrue();
}
@Test
void testDeclarationAssignment() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
LL1Parser parser = LL1Parser.fromGrammar(path);
Lexer lex = this.initLexer("DeclarationAssignment.stups");
assertThat(parser.parse(lex.getAllTokens(), lex.getVocabulary())).isTrue();
}
@Test
void testExpr() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
LL1Parser parser = LL1Parser.fromGrammar(path);
Lexer lex = this.initLexer("Expr.stups");
assertThat(parser.parse(lex.getAllTokens(), lex.getVocabulary())).isTrue();
} }
@Test @Test
@ -75,8 +98,17 @@ class LexerParserGrammarTest {
LL1Parser parser = LL1Parser.fromGrammar(path); LL1Parser parser = LL1Parser.fromGrammar(path);
Lexer lex = this.initLexer("GeneralWhile.stups"); Lexer lex = this.initLexer("GeneralWhile.stups");
List<String> token = this.getSymbols(lex);
assertThat(parser.parse(token)).isTrue(); assertThat(parser.parse(lex.getAllTokens(), lex.getVocabulary())).isTrue();
}
@Test
void testGeneralIfElse() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
LL1Parser parser = LL1Parser.fromGrammar(path);
Lexer lex = this.initLexer("GeneralIfElse.stups");
assertThat(parser.parse(lex.getAllTokens(), lex.getVocabulary())).isTrue();
} }
} }