update + fixes for parser

This commit is contained in:
Christoph
2020-12-04 18:30:24 +01:00
parent f60eef549e
commit 7764b6cdf6
7 changed files with 113 additions and 57 deletions

View File

@ -160,13 +160,13 @@ class LL1ParserTest {
void testArithExpression() {
LL1Parser parser = new LL1Parser(table1);
String[] token1 = {"id", "+", "id", "*", "id"};
String[] token1 = {"id", "+", "id"};
String[] token2 = {"id", "*", "id", "*", "id"};
String[] token3 = {"id", "+", "id"};
String[] token3 = {"id", "+", "id", "*", "id"};
assertThatThrownBy(() -> parser.parse(Arrays.asList(token1))).isInstanceOf(MyParseException.class);
assertThatThrownBy(() -> parser.parse(Arrays.asList(token2))).isInstanceOf(MyParseException.class);
assertThatThrownBy(() -> parser.parse(Arrays.asList(token3))).isInstanceOf(MyParseException.class);
assertThat(parser.parse(Arrays.asList(token1))).isTrue();
assertThat(parser.parse(Arrays.asList(token2))).isTrue();
assertThat(parser.parse(Arrays.asList(token3))).isTrue();
}
@Test
@ -174,13 +174,13 @@ class LL1ParserTest {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/SimpleGrammar1.grammar").toURI());
LL1Parser parser = LL1Parser.fromGrammar(path);
String[] token1 = {"id", "+", "id", "*", "id"};
String[] token1 = {"id", "+", "id"};
String[] token2 = {"id", "*", "id", "*", "id"};
String[] token3 = {"id", "+", "id"};
String[] token3 = {"id", "+", "id", "*", "id"};
assertThatThrownBy(() -> parser.parse(Arrays.asList(token1))).isInstanceOf(MyParseException.class);
assertThatThrownBy(() -> parser.parse(Arrays.asList(token2))).isInstanceOf(MyParseException.class);
assertThatThrownBy(() -> parser.parse(Arrays.asList(token3))).isInstanceOf(MyParseException.class);
assertThat(parser.parse(Arrays.asList(token1))).isTrue();
assertThat(parser.parse(Arrays.asList(token2))).isTrue();
assertThat(parser.parse(Arrays.asList(token3))).isTrue();
}
@Test

View File

@ -1,29 +1,21 @@
// javac -cp .:junit-4.12.jar:hamcrest-core-1.3.jar TestFirstFollow.java
// java -cp .:junit-4.12.jar:hamcrest-core-1.3.jar org.junit.runner.JUnitCore TestFirstFollow
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.antlr.v4.tool.Grammar;
import org.antlr.v4.tool.Rule;
import org.junit.Test;
import org.junit.Before;
import org.junit.Test;
import parser.ILL1ParsingTable;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.AbstractMap.SimpleEntry;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Collections;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class TestFirstFollow {
@ -48,25 +40,25 @@ public class TestFirstFollow {
*/
List<String> nonterminals = new ArrayList<>();
String narray[] = {"X", "Y", "Z"};
String[] narray = {"X", "Y", "Z"};
nonterminals = Arrays.asList(narray);
List<String> terminals = new ArrayList<>();
String tarray[] = {"a", "c", "d", "$"};
String[] tarray = {"a", "c", "d", "$"};
terminals = Arrays.asList(tarray);
String startSymbol = "Z";
List<Rule> productions = new ArrayList<>();
String production0[] = {"d"};
String[] production0 = {"d"};
productions.add(new Rule("Z", Arrays.asList(production0)));
String production1[] = {"X", "Y", "Z"};
String[] production1 = {"X", "Y", "Z"};
productions.add(new Rule("Z", Arrays.asList(production1)));
productions.add(new Rule("Y", Collections.emptyList()));
String production2[] = {"c"};
String[] production2 = {"c"};
productions.add(new Rule("Y", Arrays.asList(production2)));
String production3[] = {"Y"};
String[] production3 = {"Y"};
productions.add(new Rule("X", Arrays.asList(production3)));
String production4[] = {"a"};
String[] production4 = {"a"};
productions.add(new Rule("X", Arrays.asList(production4)));
this.grammar0 = new Grammar(nonterminals, terminals,
@ -88,28 +80,28 @@ public class TestFirstFollow {
*/
List<String> nonterminals = new ArrayList<>();
String narray[] = {"E", "E'", "T", "T'", "F"};
String[] narray = {"E", "E'", "T", "T'", "F"};
nonterminals = Arrays.asList(narray);
List<String> terminals = new ArrayList<>();
String tarray[] = {"+", "*", "(", ")", "id", "$"};
String[] tarray = {"+", "*", "(", ")", "id", "$"};
terminals = Arrays.asList(tarray);
String startSymbol = "E";
List<Rule> productions = new ArrayList<>();
String production0[] = {"T", "E'"};
String[] production0 = {"T", "E'"};
productions.add(new Rule("E", Arrays.asList(production0)));
String production1[] = {"+", "T", "E'"};
String[] production1 = {"+", "T", "E'"};
productions.add(new Rule("E'", Arrays.asList(production1)));
productions.add(new Rule("E'", Collections.emptyList()));
String production2[] = {"F", "T'"};
String[] production2 = {"F", "T'"};
productions.add(new Rule("T", Arrays.asList(production2)));
String production3[] = {"*", "F", "T'"};
String[] production3 = {"*", "F", "T'"};
productions.add(new Rule("T'", Arrays.asList(production3)));
productions.add(new Rule("T'", Collections.emptyList()));
String production4[] = {"(", "E", ")"};
String[] production4 = {"(", "E", ")"};
productions.add(new Rule("F", Arrays.asList(production4)));
String production5[] = {"id"};
String[] production5 = {"id"};
productions.add(new Rule("F", Arrays.asList(production5)));
this.grammar1 = new Grammar(nonterminals, terminals,

View File

@ -3,6 +3,9 @@ EPS: epsilon
TERM: id + * ( )
NTERM: E E2 T T2 F
// Leerzeilen sind egal
// Man kann Line-Comments schreiben und Produktionen verodern
E -> T E2
E2 -> + T E2 | EPS
T -> F T2