grammar update
This commit is contained in:
@ -21,8 +21,8 @@ class Demo {
|
||||
|
||||
private Lexer initLexer(String program) {
|
||||
try {
|
||||
Path path = Paths.get(this.getClass().getClassLoader().getResource("examplePrograms/" + program).toURI());
|
||||
String programCode = Files.readString(path, StandardCharsets.US_ASCII);
|
||||
final Path path = Paths.get(this.getClass().getClassLoader().getResource("examplePrograms/" + program).toURI());
|
||||
final String programCode = Files.readString(path, StandardCharsets.US_ASCII);
|
||||
return new StupsLexer(CharStreams.fromString(programCode));
|
||||
} catch (Exception ignore) {
|
||||
ignore.printStackTrace();
|
||||
@ -33,24 +33,26 @@ class Demo {
|
||||
|
||||
@Test
|
||||
void demoClean() throws URISyntaxException, IOException {
|
||||
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||
Grammar grammar = Grammar.fromFile(path);
|
||||
StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
||||
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||
final Grammar grammar = Grammar.fromFile(path);
|
||||
final StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
||||
|
||||
Lexer lex = this.initLexer("General.stups");
|
||||
AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
||||
final Lexer lex = this.initLexer("General.stups");
|
||||
final AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
||||
|
||||
System.out.println(tree);
|
||||
ASTCompacter.clean(tree, grammar);
|
||||
System.out.println(tree);
|
||||
}
|
||||
|
||||
@Test
|
||||
void demoLeftPrecedence() throws URISyntaxException, IOException {
|
||||
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||
Grammar grammar = Grammar.fromFile(path);
|
||||
StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
||||
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||
final Grammar grammar = Grammar.fromFile(path);
|
||||
final StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
||||
|
||||
Lexer lex = this.initLexer("General.stups");
|
||||
AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
||||
final Lexer lex = this.initLexer("General.stups");
|
||||
final AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
||||
|
||||
ASTCompacter.clean(tree, grammar);
|
||||
ASTBalancer.flip(tree);
|
||||
@ -61,12 +63,12 @@ class Demo {
|
||||
|
||||
@Test
|
||||
void demoOperatorPrecedence() throws URISyntaxException, IOException {
|
||||
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||
Grammar grammar = Grammar.fromFile(path);
|
||||
StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
||||
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||
final Grammar grammar = Grammar.fromFile(path);
|
||||
final StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
||||
|
||||
Lexer lex = this.initLexer("General.stups");
|
||||
AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
||||
final Lexer lex = this.initLexer("General.stups");
|
||||
final AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
||||
|
||||
ASTCompacter.clean(tree, grammar);
|
||||
ASTBalancer.flip(tree);
|
||||
@ -78,12 +80,12 @@ class Demo {
|
||||
|
||||
@Test
|
||||
void demoTypeCheck() throws URISyntaxException, IOException {
|
||||
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||
Grammar grammar = Grammar.fromFile(path);
|
||||
StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
||||
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||
final Grammar grammar = Grammar.fromFile(path);
|
||||
final StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
||||
|
||||
Lexer lex = this.initLexer("General.stups");
|
||||
AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
||||
final Lexer lex = this.initLexer("General.stups");
|
||||
final AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
||||
|
||||
tree.postprocess(grammar);
|
||||
System.out.println("After Postprocessing:" + tree);
|
||||
|
@ -24,23 +24,24 @@ class GrammarAnalyzerTest {
|
||||
E -> b
|
||||
*/
|
||||
|
||||
Set<String> nonterminals;
|
||||
String[] narray = {"S", "E"};
|
||||
final Set<String> nonterminals;
|
||||
final String[] narray = {"S", "E"};
|
||||
nonterminals = new HashSet<>(Arrays.asList(narray));
|
||||
|
||||
Set<String> terminals;
|
||||
String[] tarray = {"a", "b", "e", "i", "t"};
|
||||
final Set<String> terminals;
|
||||
final String[] tarray = {"a", "b", "e", "i", "t"};
|
||||
terminals = new HashSet<>(Arrays.asList(tarray));
|
||||
|
||||
String startSymbol = "S";
|
||||
String epsilonSymbol = "epsilon";
|
||||
final String startSymbol = "S";
|
||||
final String epsilonSymbol = "epsilon";
|
||||
|
||||
Set<GrammarRule> rules = new HashSet<>();
|
||||
final Set<GrammarRule> rules = new HashSet<>();
|
||||
rules.add(new GrammarRule("S", "a"));
|
||||
rules.add(new GrammarRule("S", "i", "E", "t", "S"));
|
||||
rules.add(new GrammarRule("E", "b"));
|
||||
|
||||
grammar0 = new Grammar(terminals, nonterminals, startSymbol, epsilonSymbol, null, null, rules);
|
||||
grammar0 = new Grammar(terminals, nonterminals, startSymbol, epsilonSymbol, null, null,
|
||||
null, null, null, rules);
|
||||
}
|
||||
|
||||
@BeforeAll
|
||||
@ -49,18 +50,18 @@ class GrammarAnalyzerTest {
|
||||
Folie 4b/32
|
||||
*/
|
||||
|
||||
Set<String> nonterminals;
|
||||
String[] narray = {"E", "T", "E2", "T2", "F"};
|
||||
final Set<String> nonterminals;
|
||||
final String[] narray = {"E", "T", "E2", "T2", "F"};
|
||||
nonterminals = new HashSet<>(Arrays.asList(narray));
|
||||
|
||||
Set<String> terminals;
|
||||
String[] tarray = {"id", "+", "*", "(", ")"};
|
||||
final Set<String> terminals;
|
||||
final String[] tarray = {"id", "+", "*", "(", ")"};
|
||||
terminals = new HashSet<>(Arrays.asList(tarray));
|
||||
|
||||
String startSymbol = "E";
|
||||
String epsilonSymbol = "epsilon";
|
||||
final String startSymbol = "E";
|
||||
final String epsilonSymbol = "epsilon";
|
||||
|
||||
Set<GrammarRule> rules = new HashSet<>();
|
||||
final Set<GrammarRule> rules = new HashSet<>();
|
||||
rules.add(new GrammarRule("E", "T", "E2"));
|
||||
rules.add(new GrammarRule("E2", "+", "T", "E2"));
|
||||
rules.add(new GrammarRule("E2", epsilonSymbol));
|
||||
@ -70,7 +71,8 @@ class GrammarAnalyzerTest {
|
||||
rules.add(new GrammarRule("F", "(", "E", ")"));
|
||||
rules.add(new GrammarRule("F", "id"));
|
||||
|
||||
grammar1 = new Grammar(terminals, nonterminals, startSymbol, epsilonSymbol, null, null, rules);
|
||||
grammar1 = new Grammar(terminals, nonterminals, startSymbol, epsilonSymbol, null, null,
|
||||
null, null, null, rules);
|
||||
}
|
||||
|
||||
@BeforeAll
|
||||
@ -84,18 +86,18 @@ class GrammarAnalyzerTest {
|
||||
X -> a
|
||||
*/
|
||||
|
||||
Set<String> nonterminals;
|
||||
String[] narray = {"X", "Y", "Z"};
|
||||
final Set<String> nonterminals;
|
||||
final String[] narray = {"X", "Y", "Z"};
|
||||
nonterminals = new HashSet<>(Arrays.asList(narray));
|
||||
|
||||
Set<String> terminals;
|
||||
String[] tarray = {"a", "c", "d"};
|
||||
final Set<String> terminals;
|
||||
final String[] tarray = {"a", "c", "d"};
|
||||
terminals = new HashSet<>(Arrays.asList(tarray));
|
||||
|
||||
String startSymbol = "Z";
|
||||
String epsilonSymbol = "epsilon";
|
||||
final String startSymbol = "Z";
|
||||
final String epsilonSymbol = "epsilon";
|
||||
|
||||
Set<GrammarRule> rules = new HashSet<>();
|
||||
final Set<GrammarRule> rules = new HashSet<>();
|
||||
rules.add(new GrammarRule("Z", "d"));
|
||||
rules.add(new GrammarRule("Z", "X", "Y", "Z"));
|
||||
rules.add(new GrammarRule("Y", epsilonSymbol));
|
||||
@ -103,12 +105,13 @@ class GrammarAnalyzerTest {
|
||||
rules.add(new GrammarRule("X", "Y"));
|
||||
rules.add(new GrammarRule("X", "a"));
|
||||
|
||||
grammar2 = new Grammar(terminals, nonterminals, startSymbol, epsilonSymbol, null, null, rules);
|
||||
grammar2 = new Grammar(terminals, nonterminals, startSymbol, epsilonSymbol, null, null,
|
||||
null, null, null, rules);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFirstGrammar0() {
|
||||
GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar0);
|
||||
final GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar0);
|
||||
|
||||
assertThat(analyzer.getFirst().get("S")).containsOnly("i", "a");
|
||||
assertThat(analyzer.getFirst().get("E")).containsOnly("b");
|
||||
@ -116,7 +119,7 @@ class GrammarAnalyzerTest {
|
||||
|
||||
@Test
|
||||
void testFirstGrammar1() {
|
||||
GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar1);
|
||||
final GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar1);
|
||||
|
||||
assertThat(analyzer.getFirst().get("E")).containsOnly("id", "(");
|
||||
assertThat(analyzer.getFirst().get("E2")).containsOnly("+", grammar1.getEpsilonSymbol());
|
||||
@ -127,7 +130,7 @@ class GrammarAnalyzerTest {
|
||||
|
||||
@Test
|
||||
void testFirstGrammar2() {
|
||||
GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar2);
|
||||
final GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar2);
|
||||
|
||||
assertThat(analyzer.getFirst().get("X")).containsOnly("c", "a", grammar2.getEpsilonSymbol());
|
||||
assertThat(analyzer.getFirst().get("Y")).containsOnly("c", grammar2.getEpsilonSymbol());
|
||||
@ -136,7 +139,7 @@ class GrammarAnalyzerTest {
|
||||
|
||||
@Test
|
||||
void testFollowGrammar0() {
|
||||
GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar0);
|
||||
final GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar0);
|
||||
|
||||
assertThat(analyzer.getFollow().get("S")).containsOnly("$");
|
||||
assertThat(analyzer.getFollow().get("E")).containsOnly("t");
|
||||
@ -144,7 +147,7 @@ class GrammarAnalyzerTest {
|
||||
|
||||
@Test
|
||||
void testFollowGrammar1() {
|
||||
GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar1);
|
||||
final GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar1);
|
||||
|
||||
assertThat(analyzer.getFollow().get("E")).containsOnly(")", "$");
|
||||
assertThat(analyzer.getFollow().get("E2")).containsOnly(")", "$");
|
||||
@ -155,7 +158,7 @@ class GrammarAnalyzerTest {
|
||||
|
||||
@Test
|
||||
void testFollowGrammar2() {
|
||||
GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar2);
|
||||
final GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar2);
|
||||
|
||||
assertThat(analyzer.getFollow().get("X")).containsOnly("a", "c", "d");
|
||||
assertThat(analyzer.getFollow().get("Y")).containsOnly("a", "c", "d");
|
||||
@ -164,8 +167,8 @@ class GrammarAnalyzerTest {
|
||||
|
||||
@Test
|
||||
void testTableGrammar1() {
|
||||
GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar1);
|
||||
ParsingTable table = analyzer.getTable();
|
||||
final GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar1);
|
||||
final ParsingTable table = analyzer.getTable();
|
||||
|
||||
assertThat(table.get("E", "id")).isEqualTo("T E2");
|
||||
assertThat(table.get("E", "(")).isEqualTo("T E2");
|
||||
|
@ -8,7 +8,7 @@ import parser.StupsParser;
|
||||
import parser.ast.AST;
|
||||
import parser.grammar.Grammar;
|
||||
import typechecker.SymbolAlreadyDefinedException;
|
||||
import typechecker.SymbolTable;
|
||||
import typechecker.TypeTable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
@ -20,12 +20,12 @@ import java.nio.file.Paths;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
class SymbolTableTest {
|
||||
class TypeTableTest {
|
||||
|
||||
private Lexer initLexer(String program) {
|
||||
try {
|
||||
Path path = Paths.get(this.getClass().getClassLoader().getResource("examplePrograms/" + program).toURI());
|
||||
String programCode = Files.readString(path, StandardCharsets.US_ASCII);
|
||||
final Path path = Paths.get(this.getClass().getClassLoader().getResource("examplePrograms/" + program).toURI());
|
||||
final String programCode = Files.readString(path, StandardCharsets.US_ASCII);
|
||||
return new StupsLexer(CharStreams.fromString(programCode));
|
||||
} catch (Exception ignore) {
|
||||
ignore.printStackTrace();
|
||||
@ -36,15 +36,15 @@ class SymbolTableTest {
|
||||
|
||||
@Test
|
||||
void testSingleSymbol() throws URISyntaxException, IOException {
|
||||
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||
Grammar grammar = Grammar.fromFile(path);
|
||||
StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
||||
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||
final Grammar grammar = Grammar.fromFile(path);
|
||||
final StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
||||
|
||||
Lexer lex = this.initLexer("SingleSymbol.stups");
|
||||
AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
||||
final Lexer lex = this.initLexer("SingleSymbol.stups");
|
||||
final AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
||||
tree.postprocess(grammar);
|
||||
|
||||
SymbolTable table = SymbolTable.fromAST(tree);
|
||||
final TypeTable table = TypeTable.fromAST(tree);
|
||||
|
||||
assertThat(table.getSymbolType("i")).isEqualTo("INTEGER_TYPE");
|
||||
assertThat(table.getSymbolCount()).isEqualTo(1);
|
||||
@ -52,15 +52,15 @@ class SymbolTableTest {
|
||||
|
||||
@Test
|
||||
void testMultipleSymbol() throws URISyntaxException, IOException {
|
||||
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||
Grammar grammar = Grammar.fromFile(path);
|
||||
StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
||||
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||
final Grammar grammar = Grammar.fromFile(path);
|
||||
final StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
||||
|
||||
Lexer lex = this.initLexer("MultipleSymbol.stups");
|
||||
AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
||||
final Lexer lex = this.initLexer("MultipleSymbol.stups");
|
||||
final AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
||||
tree.postprocess(grammar);
|
||||
|
||||
SymbolTable table = SymbolTable.fromAST(tree);
|
||||
final TypeTable table = TypeTable.fromAST(tree);
|
||||
|
||||
assertThat(table.getSymbolType("i")).isEqualTo("INTEGER_TYPE");
|
||||
assertThat(table.getSymbolType("ii")).isEqualTo("INTEGER_TYPE");
|
||||
@ -73,14 +73,14 @@ class SymbolTableTest {
|
||||
|
||||
@Test
|
||||
void testExistingSymbol() throws URISyntaxException, IOException {
|
||||
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||
Grammar grammar = Grammar.fromFile(path);
|
||||
StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
||||
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||
final Grammar grammar = Grammar.fromFile(path);
|
||||
final StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
||||
|
||||
Lexer lex = this.initLexer("ExistingSymbol.stups");
|
||||
AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
||||
final Lexer lex = this.initLexer("ExistingSymbol.stups");
|
||||
final AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
||||
tree.postprocess(grammar);
|
||||
|
||||
assertThatThrownBy(() -> SymbolTable.fromAST(tree)).isInstanceOf(SymbolAlreadyDefinedException.class);
|
||||
assertThatThrownBy(() -> TypeTable.fromAST(tree)).isInstanceOf(SymbolAlreadyDefinedException.class);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user