This commit is contained in:
Christoph
2020-12-04 01:27:30 +01:00
parent 38929e674a
commit 27862eb75c
3 changed files with 479 additions and 232 deletions

View File

@ -1,7 +0,0 @@
O
src/main/java/util/ast/AST.java,1/d/1db155d1c826f7ce81093ab2ee88b0afa9c698d8
P
src/main/java/util/ast/Node.java,a/e/ae3ca6b6b8a06fc2cba3a596d8a0d8d2acd8a16f
S
#src/main/java/parser/LL1Parser.java,8/1/8144fa070804a52542f36f405b65ca25f794ce3c

View File

@ -2,21 +2,18 @@
package lexer;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.RuntimeMetaData;
import org.antlr.v4.runtime.Vocabulary;
import org.antlr.v4.runtime.VocabularyImpl;
import org.antlr.v4.runtime.atn.ATN;
import org.antlr.v4.runtime.atn.ATNDeserializer;
import org.antlr.v4.runtime.atn.LexerATNSimulator;
import org.antlr.v4.runtime.atn.PredictionContextCache;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.atn.*;
import org.antlr.v4.runtime.dfa.DFA;
import org.antlr.v4.runtime.misc.*;
@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"})
public class StupsLexer extends Lexer {
public static final String[] ruleNames = makeRuleNames();
static { RuntimeMetaData.checkVersion("4.8", RuntimeMetaData.VERSION); }
protected static final DFA[] _decisionToDFA;
protected static final PredictionContextCache _sharedContextCache =
@ -49,12 +46,7 @@ public class StupsLexer extends Lexer {
"STRING_LIT", "BOOLEAN_LIT", "IDENTIFIER"
};
}
/**
* @deprecated Use {@link #VOCABULARY} instead.
*/
@Deprecated
public static final String[] tokenNames;
public static final String[] ruleNames = makeRuleNames();
private static String[] makeLiteralNames() {
return new String[] {
@ -65,9 +57,7 @@ public class StupsLexer extends Lexer {
"']'", "';'", "','", "'.'"
};
}
private static final String[] _LITERAL_NAMES = makeLiteralNames();
private static String[] makeSymbolicNames() {
return new String[] {
null, "WHITESPACE", "MULTILINE_COMMENT", "LINE_COMMENT", "CLASS", "PUBLIC",
@ -79,14 +69,14 @@ public class StupsLexer extends Lexer {
"IDENTIFIER"
};
}
private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames();
public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES);
static {
RuntimeMetaData.checkVersion("4.8", RuntimeMetaData.VERSION);
}
/**
* @deprecated Use {@link #VOCABULARY} instead.
*/
@Deprecated
public static final String[] tokenNames;
static {
tokenNames = new String[_SYMBOLIC_NAMES.length];
for (int i = 0; i < tokenNames.length; i++) {
@ -101,9 +91,10 @@ public class StupsLexer extends Lexer {
}
}
public StupsLexer(CharStream input) {
super(input);
_interp = new LexerATNSimulator(this, _ATN, _decisionToDFA, _sharedContextCache);
@Override
@Deprecated
public String[] getTokenNames() {
return tokenNames;
}
@Override
@ -112,21 +103,21 @@ public class StupsLexer extends Lexer {
return VOCABULARY;
}
@Override
@Deprecated
public String[] getTokenNames() {
return tokenNames;
public StupsLexer(CharStream input) {
super(input);
_interp = new LexerATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache);
}
@Override
public String getGrammarFileName() { return "StupsLexer.g4"; }
@Override
public String[] getRuleNames() { return ruleNames; }
@Override
public String getSerializedATN() { return _serializedATN; }
@Override
public String getGrammarFileName() { return "StupsLexer.g4"; }
@Override
public String[] getChannelNames() { return channelNames; }
@ -242,7 +233,6 @@ public class StupsLexer extends Lexer {
"\u0140\3\b\2\2";
public static final ATN _ATN =
new ATNDeserializer().deserialize(_serializedATN.toCharArray());
static {
_decisionToDFA = new DFA[_ATN.getNumberOfDecisions()];
for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) {

View File

@ -0,0 +1,264 @@
// 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 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.Set;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Collections;
public class TestFirstFollow {
private Grammar grammar0;
private Grammar grammar1;
@Before
public void setUp() {
this.initGrammar0();
this.initGrammar1();
}
private void initGrammar0() {
/*
Z -> d
Z -> X Y Z
Y ->
Y -> c
X -> Y
X -> a
*/
List<String> nonterminals = new ArrayList<>();
String narray[] = {"X", "Y", "Z"};
nonterminals = Arrays.asList(narray);
List<String> terminals = new ArrayList<>();
String tarray[] = {"a", "c", "d", "$"};
terminals = Arrays.asList(tarray);
String startSymbol = "Z";
List<Rule> productions = new ArrayList<>();
String production0[] = {"d"};
productions.add(new Rule("Z", Arrays.asList(production0)));
String production1[] = {"X", "Y", "Z"};
productions.add(new Rule("Z", Arrays.asList(production1)));
productions.add(new Rule("Y", Collections.emptyList()));
String production2[] = {"c"};
productions.add(new Rule("Y", Arrays.asList(production2)));
String production3[] = {"Y"};
productions.add(new Rule("X", Arrays.asList(production3)));
String production4[] = {"a"};
productions.add(new Rule("X", Arrays.asList(production4)));
this.grammar0 = new Grammar(nonterminals, terminals,
startSymbol, productions);
}
private void initGrammar1() {
/*
E -> T E'
E' -> + T E'
E' ->
T -> F T'
T' -> * F T'
T' ->
F -> ( E )
F -> id
*/
List<String> nonterminals = new ArrayList<>();
String narray[] = {"E", "E'", "T", "T'", "F"};
nonterminals = Arrays.asList(narray);
List<String> terminals = new ArrayList<>();
String tarray[] = {"+", "*", "(", ")", "id", "$"};
terminals = Arrays.asList(tarray);
String startSymbol = "E";
List<Rule> productions = new ArrayList<>();
String production0[] = {"T", "E'"};
productions.add(new Rule("E", Arrays.asList(production0)));
String production1[] = {"+", "T", "E'"};
productions.add(new Rule("E'", Arrays.asList(production1)));
productions.add(new Rule("E'", Collections.emptyList()));
String production2[] = {"F", "T'"};
productions.add(new Rule("T", Arrays.asList(production2)));
String production3[] = {"*", "F", "T'"};
productions.add(new Rule("T'", Arrays.asList(production3)));
productions.add(new Rule("T'", Collections.emptyList()));
String production4[] = {"(", "E", ")"};
productions.add(new Rule("F", Arrays.asList(production4)));
String production5[] = {"id"};
productions.add(new Rule("F", Arrays.asList(production5)));
this.grammar1 = new Grammar(nonterminals, terminals,
startSymbol, productions);
}
@Test
public void testNullable0() {
GrammarAnalyzer ga = new GrammarAnalyzer(this.grammar0);
Set<String> nullable = ga.getNullable();
assertTrue(nullable.contains("Y"));
assertTrue(nullable.contains("X"));
assertTrue(!nullable.contains("Z"));
assertEquals(2, nullable.size());
}
@Test
public void testNullable1() {
GrammarAnalyzer ga = new GrammarAnalyzer(this.grammar1);
Set<String> nullable = ga.getNullable();
assertTrue(nullable.contains("E'"));
assertTrue(nullable.contains("T'"));
assertTrue(!nullable.contains("E"));
assertEquals(2, nullable.size());
}
@Test
public void testFirst0() {
GrammarAnalyzer ga = new GrammarAnalyzer(this.grammar0);
Map<String, Set<String>> first = ga.getFirst();
assertTrue(first.get("Y").contains("c"));
assertEquals(1, first.get("Y").size());
assertTrue(first.get("X").contains("c"));
assertTrue(first.get("X").contains("a"));
assertEquals(2, first.get("X").size());
assertTrue(first.get("Z").contains("c"));
assertTrue(first.get("Z").contains("a"));
assertTrue(first.get("Z").contains("d"));
assertEquals(3, first.get("Z").size());
}
@Test
public void testFirst1() {
GrammarAnalyzer ga = new GrammarAnalyzer(this.grammar1);
Map<String, Set<String>> first = ga.getFirst();
assertTrue(first.get("E").contains("id"));
assertTrue(first.get("E").contains("("));
assertEquals(2, first.get("E").size());
assertTrue(first.get("T").contains("id"));
assertTrue(first.get("T").contains("("));
assertEquals(2, first.get("T").size());
assertTrue(first.get("E'").contains("+"));
assertEquals(1, first.get("E'").size());
assertTrue(first.get("T'").contains("*"));
assertEquals(1, first.get("T'").size());
assertTrue(first.get("F").contains("id"));
assertTrue(first.get("F").contains("("));
assertEquals(2, first.get("F").size());
}
@Test
public void testFollow0() {
GrammarAnalyzer ga = new GrammarAnalyzer(this.grammar0);
Map<String, Set<String>> follow = ga.getFollow();
assertTrue(follow.get("X").contains("a"));
assertTrue(follow.get("X").contains("c"));
assertTrue(follow.get("X").contains("d"));
assertEquals(3, follow.get("X").size());
assertTrue(follow.get("Y").contains("a"));
assertTrue(follow.get("Y").contains("c"));
assertTrue(follow.get("Y").contains("d"));
assertEquals(3, follow.get("Y").size());
assertTrue(follow.get("Z").contains("$"));
assertEquals(1, follow.get("Z").size());
}
@Test
public void testFollow1() {
GrammarAnalyzer ga = new GrammarAnalyzer(this.grammar1);
Map<String, Set<String>> follow = ga.getFollow();
assertTrue(follow.get("E").contains(")"));
assertTrue(follow.get("E").contains("$"));
assertEquals(2, follow.get("E").size());
assertTrue(follow.get("T").contains("+"));
assertTrue(follow.get("T").contains(")"));
assertTrue(follow.get("T").contains("$"));
assertEquals(3, follow.get("T").size());
assertTrue(follow.get("T'").contains("$"));
assertTrue(follow.get("T'").contains("+"));
assertTrue(follow.get("T'").contains(")"));
assertEquals(3, follow.get("T'").size());
assertTrue(follow.get("E'").contains("$"));
assertTrue(follow.get("E'").contains(")"));
assertEquals(2, follow.get("E'").size());
assertTrue(follow.get("F").contains("+"));
assertTrue(follow.get("F").contains(")"));
assertTrue(follow.get("F").contains("*"));
assertTrue(follow.get("F").contains("$"));
assertEquals(4, follow.get("F").size());
}
@Test
public void testTable1() {
GrammarAnalyzer ga = new GrammarAnalyzer(this.grammar1);
ILL1ParsingTable table = ga.getTable();
List production;
production = table.get("E", "id");
assertTrue(production.get(0).equals("T"));
assertTrue(production.get(1).equals("E'"));
production = table.get("T", "id");
assertTrue(production.get(0).equals("F"));
assertTrue(production.get(1).equals("T'"));
production = table.get("F", "id");
assertTrue(production.get(0).equals("id"));
production = table.get("E'", "+");
assertTrue(production.get(0).equals("+"));
assertTrue(production.get(1).equals("T"));
assertTrue(production.get(2).equals("E'"));
production = table.get("T'", "+");
assertEquals(0, production.size());
production = table.get("T'", "*");
assertTrue(production.get(0).equals("*"));
assertTrue(production.get(1).equals("F"));
assertTrue(production.get(2).equals("T'"));
production = table.get("E", "(");
assertTrue(production.get(0).equals("T"));
assertTrue(production.get(1).equals("E'"));
production = table.get("T", "(");
assertTrue(production.get(0).equals("F"));
assertTrue(production.get(1).equals("T'"));
production = table.get("F", "(");
assertTrue(production.get(0).equals("("));
assertTrue(production.get(1).equals("E"));
assertTrue(production.get(2).equals(")"));
production = table.get("E'", ")");
assertEquals(0, production.size());
production = table.get("T'", ")");
assertEquals(0, production.size());
production = table.get("E'", "$");
assertEquals(0, production.size());
production = table.get("T'", "$");
assertEquals(0, production.size());
}
}