diff --git a/build.gradle b/build.gradle index fa02c46..054f743 100644 --- a/build.gradle +++ b/build.gradle @@ -22,8 +22,10 @@ sourceSets { dependencies { implementation platform('org.junit:junit-bom:5.10.3') implementation 'org.junit.jupiter:junit-jupiter' + testImplementation platform('org.junit:junit-bom:5.10.3') testImplementation 'org.junit.jupiter:junit-jupiter' + testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } @@ -38,6 +40,7 @@ tasks.named('test') { testLogging { events = [TestLogEvent.FAILED, TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.STANDARD_OUT] exceptionFormat = TestExceptionFormat.FULL + showExceptions = true showCauses = true showStackTraces = true @@ -46,6 +49,7 @@ tasks.named('test') { events = [TestLogEvent.STARTED, TestLogEvent.FAILED, TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.STANDARD_ERROR, TestLogEvent.STANDARD_OUT] exceptionFormat = TestExceptionFormat.FULL } + info.events = debug.events info.exceptionFormat = debug.exceptionFormat diff --git a/docs/diagrams/GeneralCompilation.drawio.svg b/docs/diagrams/GeneralCompilation.drawio.svg index c78edd2..6398963 100644 --- a/docs/diagrams/GeneralCompilation.drawio.svg +++ b/docs/diagrams/GeneralCompilation.drawio.svg @@ -1,4 +1,4 @@ - + @@ -6,10 +6,10 @@ - + - -
+ +
Grammar_objects @@ -24,14 +24,14 @@ - - + + - + - -
+ +
TestFramework @@ -39,60 +39,31 @@
- + TestFramework - - - - - - - -
-
-
-
- GrammarParts -
-
-
-
-
-
-
-
-
- - GrammarParts... - -
-
-
- + - +
- Grammar (Abstract) + Grammar
-
-
- Grammar (Abstract) + Grammar
@@ -102,10 +73,10 @@
- + - -
+ +
Code_generation @@ -113,7 +84,7 @@
- + Code_generation @@ -123,9 +94,9 @@ - + - +
@@ -148,9 +119,9 @@ - + - +
@@ -177,9 +148,9 @@ - + - +
@@ -202,9 +173,9 @@ - + - +
@@ -227,9 +198,9 @@ - + - +
@@ -252,9 +223,9 @@ - + - +
@@ -281,9 +252,9 @@ - + - +
@@ -310,9 +281,9 @@ - + - +
@@ -339,9 +310,9 @@ - + - +
@@ -364,9 +335,9 @@ - + - +
@@ -390,9 +361,9 @@ - + - +
@@ -411,9 +382,9 @@ - + - +
@@ -433,13 +404,13 @@ - + - + - -
+ +
@@ -451,20 +422,20 @@
- + EmptyToken - + - + - -
+ +
@@ -476,21 +447,21 @@
- + Token - - + + - + - -
+ +
Extends @@ -498,20 +469,20 @@
- + Extends - + - + - -
+ +
@@ -523,21 +494,21 @@
- + EOF - - + + - + - -
+ +
Extends @@ -545,7 +516,7 @@
- + Extends @@ -555,9 +526,9 @@ - + - +
@@ -577,13 +548,13 @@ - + - + - -
+ +
@@ -595,21 +566,21 @@
- + Identifier - - + + - + - -
+ +
Extends @@ -617,20 +588,20 @@
- + Extends - + - + - -
+ +
@@ -642,20 +613,20 @@
- + LexicalElement (Abstract... - + - + - -
+ +
@@ -667,21 +638,21 @@
- + Literal - - + + - + - -
+ +
Extends @@ -689,20 +660,20 @@
- + Extends - + - + - -
+ +
@@ -714,21 +685,21 @@
- + NonTerminal - - + + - + - -
+ +
Extends @@ -736,7 +707,7 @@
- + Extends @@ -746,9 +717,9 @@ - + - +
@@ -768,13 +739,13 @@ - + - + - -
+ +
@@ -786,21 +757,21 @@
- + Token - - + + - + - -
+ +
Extends @@ -808,7 +779,7 @@
- + Extends @@ -816,12 +787,12 @@ - + - + - +
@@ -841,9 +812,9 @@ - + - +
@@ -866,9 +837,9 @@ - + - +
@@ -892,13 +863,13 @@ - + - + - -
+ +
@@ -910,20 +881,20 @@
- + FirstSetGenerator - + - + - -
+ +
@@ -935,20 +906,20 @@
- + FollowSetGenerator - + - + - -
+ +
@@ -960,7 +931,7 @@
- + LexElementFinder @@ -970,9 +941,9 @@ - + - +
@@ -995,9 +966,9 @@ - + - +
@@ -1020,9 +991,9 @@ - + - +
@@ -1055,9 +1026,9 @@ - + - +
@@ -1080,9 +1051,9 @@ - + - +
@@ -1109,9 +1080,9 @@ - + - +
@@ -1134,9 +1105,9 @@ - + - +
@@ -1163,9 +1134,9 @@ - + - +
@@ -1189,13 +1160,13 @@ - + - + - -
+ +
@@ -1207,21 +1178,21 @@
- + RegexFeatureChecker - - + + - + - -
+ +
Lexical_Analysis @@ -1229,20 +1200,20 @@
- + Lexical_Analysis - + - + - -
+ +
@@ -1254,20 +1225,20 @@
- + GeneralLexicalAnalyser - + - + - -
+ +
@@ -1279,24 +1250,24 @@
- + LexicalAnalyser <<I>> - - + + - + - + - -
+ +
@@ -1308,42 +1279,20 @@
- + LexicalError - - - - - - - - -
-
-
- Extends -
-
-
-
- - Extends - -
-
-
- + - +
@@ -1362,9 +1311,9 @@ - + - +
@@ -1387,9 +1336,9 @@ - + - +
@@ -1416,9 +1365,9 @@ - + - +
@@ -1446,9 +1395,9 @@ - + - +
@@ -1457,7 +1406,7 @@
- + Syntax_Analysis @@ -1467,9 +1416,9 @@ - + - +
@@ -1492,9 +1441,9 @@ - + - +
@@ -1519,9 +1468,9 @@ - + - +
@@ -1544,9 +1493,9 @@ - + - +
@@ -1570,10 +1519,10 @@ - + - -
+ +
Extends @@ -1591,9 +1540,9 @@ - + - +
@@ -1616,9 +1565,9 @@ - + - +
@@ -1646,9 +1595,9 @@ - + - +
@@ -1675,9 +1624,9 @@ - + - +
@@ -1700,9 +1649,9 @@ - + - +
@@ -1725,9 +1674,9 @@ - + - +
@@ -1751,9 +1700,9 @@ - + - +
@@ -1772,9 +1721,9 @@ - + - +
@@ -1797,9 +1746,9 @@ - + - +
@@ -1823,9 +1772,9 @@ - + - +
@@ -1844,9 +1793,9 @@ - + - +
@@ -1870,10 +1819,10 @@ - + - -
+ +
Extends @@ -1891,9 +1840,9 @@ - + - +
@@ -1917,10 +1866,10 @@ - + - -
+ +
Extends @@ -1938,9 +1887,9 @@ - + - +
@@ -1964,9 +1913,9 @@ - + - +
@@ -1985,9 +1934,9 @@ - + - +
@@ -2010,9 +1959,9 @@ - + - +
@@ -2043,9 +1992,9 @@ - + - +
@@ -2068,9 +2017,9 @@ - + - +
@@ -2094,9 +2043,9 @@ - + - +
@@ -2105,7 +2054,7 @@
- + Extends @@ -2115,9 +2064,9 @@ - + - +
@@ -2140,9 +2089,9 @@ - + - +
@@ -2166,10 +2115,10 @@ - + - -
+ +
Extends @@ -2188,10 +2137,10 @@ - + - -
+ +
Extends @@ -2206,13 +2155,13 @@ - + - + - -
+ +
@@ -2228,23 +2177,23 @@
- + IntComputationLex - - + + - + - +
@@ -2268,9 +2217,9 @@ - + - +
@@ -2279,7 +2228,7 @@
- + Grammars @@ -2290,9 +2239,9 @@ - + - +
@@ -2301,7 +2250,7 @@
- + Implementation @@ -2312,9 +2261,9 @@ - + - +
@@ -2323,7 +2272,7 @@
- + Convertors @@ -2333,9 +2282,9 @@ - + - +
@@ -2358,81 +2307,37 @@ - + - +
- Convertor + Implementation
- Convertor - -
-
-
- - - - - - - - -
-
-
- Extends -
-
-
-
- - Extends - -
-
-
- - - - - - - - -
-
-
- Extends -
-
-
-
- - Extends + Implementation
- + - + - -
+ +
- TestGrammar (Abstract) + TestGrammar

@@ -2440,20 +2345,20 @@
- - TestGrammar (Abstract) + + TestGrammar - + - + - -
+ +
@@ -2465,21 +2370,21 @@
- + GrammarType (enum) - - + + - + - -
+ +
TestGrammars @@ -2487,21 +2392,21 @@
- + TestGrammars - - + + - + - -
+ +
Implementation @@ -2509,20 +2414,20 @@
- + Implementation - + - + - -
+ +
@@ -2534,89 +2439,23 @@
- + Implementation - - - - - - - - - - - -
-
-
- Extends -
-
-
-
- - Extends - -
-
-
- - - - - - - - -
-
-
- Extends -
-
-
-
- - Extends - -
-
-
- - - - - - - - -
-
-
- Extends -
-
-
-
- - Extends - -
-
+ +
- + - +
@@ -2634,13 +2473,13 @@ - + - + - -
+ +
@@ -2652,21 +2491,21 @@
- + DynamicToken - - + + - + - -
+ +
Extends @@ -2674,21 +2513,21 @@
- + Extends - - + + - + - -
+ +
Component Construction @@ -2696,20 +2535,20 @@
- + Component Construc... - + - + - -
+ +
@@ -2719,20 +2558,20 @@
- + ParameterError - + - + - -
+ +
@@ -2742,43 +2581,21 @@
- + Compiler - - + + - + - -
-
-
- Extends -
-
-
-
- - Extends - -
-
-
- - - - - - - - -
+ +
Builders @@ -2786,20 +2603,20 @@
- + Builders - + - + - -
+ +
@@ -2809,21 +2626,21 @@
- + Factory Interface <<I>> - - + + - + - -
+ +
Bundles @@ -2831,20 +2648,20 @@
- + Bundles - + - + - -
+ +
@@ -2854,20 +2671,20 @@
- + GrammarBundle - + - + - -
+ +
@@ -2877,20 +2694,20 @@
- + LexicalBundle - + - + - -
+ +
@@ -2900,21 +2717,21 @@
- + DynamicTokenRegex - - + + - + - -
+ +
Factories @@ -2922,20 +2739,42 @@
- + Factories - + + - + - -
+ +
+
+
+ builds +
+
+
+
+ + builds + + + + + + + + + + + +
@@ -2945,21 +2784,21 @@
- + CompilerBuilder - - + + - + - -
+ +
Component Section @@ -2967,20 +2806,20 @@
- + Component Section - + - + - -
+ +
@@ -2990,24 +2829,24 @@
- + Implementation - - + + - + - + - -
+ +
@@ -3017,20 +2856,20 @@
- + UnsupportedGrammarException - + - + - -
+ +
@@ -3040,12 +2879,325 @@
- + UnsupportedSentenceException + + + + + + + +
+
+
+
+ ToStringFormatter +
+
+
+
+
+ + ToStringFormatter + +
+
+
+ + + + + + + + +
+
+
+ Grammar Objects +
+
+
+
+ + Grammar Objects + +
+
+
+ + + + + + + + +
+
+
+ builds +
+
+
+
+ + builds + +
+
+
+ + + + + + + +
+
+
+
+ GrammarBuilder +
+
+
+
+
+ + GrammarBuilder + +
+
+
+ + + + + + + +
+
+
+
+ RuleConvertorBuilder +
+
+
+
+
+ + RuleConvertorBuilder + +
+
+
+ + + + + + + + +
+
+
+ Test Aids +
+
+
+
+ + Test Aids + +
+
+
+ + + + + + + + +
+
+
+ builds +
+
+
+
+ + builds + +
+
+
+ + + + + + + +
+
+
+
+ TestGrammarBuilder +
+
+
+
+
+ + TestGrammarBuilder + +
+
+
+ + + + + + + +
+
+
+
+ GrammarParts +
+
+
+
+
+ + GrammarParts + +
+
+
+ + + + + + + + +
+
+
+ Extends +
+
+
+
+ + Extends + +
+
+
+ + + + + + + + +
+
+
+ Extends +
+
+
+
+ + Extends + +
+
+
+ + + + + + + + +
+
+
+ Extends +
+
+
+
+ + Extends + +
+
+
+ + + + + + + + +
+
+
+ Extends +
+
+
+
+ + Extends + +
+
+
+ + + + + + + + +
+
+
+ Extends +
+
+
+
+ + Extends + +
+
+
diff --git a/src/main/java/component_construction/builders/grammar_objects/RuleConvertorBuilder.java b/src/main/java/component_construction/builders/grammar_objects/RuleConvertorBuilder.java index cb661d8..4c46442 100644 --- a/src/main/java/component_construction/builders/grammar_objects/RuleConvertorBuilder.java +++ b/src/main/java/component_construction/builders/grammar_objects/RuleConvertorBuilder.java @@ -144,7 +144,7 @@ public RuleOptions rule(int ruleNumber) { private void populateDefaultConversions() { - for (ProductionRule productionRule : grammar.productionRules()) { + for (ProductionRule productionRule : grammar.getProductionRules()) { if (conversions.containsKey(productionRule)) continue; conversions.put(productionRule, defaultConversion); diff --git a/src/main/java/grammar_objects/Grammar.java b/src/main/java/grammar_objects/Grammar.java index 70fe049..a23db5a 100644 --- a/src/main/java/grammar_objects/Grammar.java +++ b/src/main/java/grammar_objects/Grammar.java @@ -1,13 +1,27 @@ package grammar_objects; +import static helper_objects.ToStringFormatting.indentFormat; + import java.util.*; -public record Grammar( - Set tokens, - Set nonTerminals, - List productionRules, - NonTerminal sentinal -) { +public class Grammar { + + protected Set tokens; + protected Set nonTerminals; + protected List productionRules; + protected NonTerminal sentinal; + + public Grammar( + Set tokens, + Set nonTerminals, + List productionRules, + NonTerminal sentinal + ) { + this.tokens = tokens; + this.nonTerminals = nonTerminals; + this.productionRules = productionRules; + this.sentinal = sentinal; + } public ProductionRule getRule(int index) { return productionRules.get(index); @@ -23,16 +37,14 @@ public GrammarParts getParts() { } @Override - public boolean equals(Object object){ + public boolean equals(Object object) { if(!(object instanceof Grammar)) { return false; } Grammar otherGrammar = (Grammar)object; if(!sentinal.equals(otherGrammar.sentinal)) { return false; } - if(!tokens.equals(otherGrammar.tokens)) { return false; } if(!nonTerminals.equals(otherGrammar.nonTerminals)) { return false; } - if(!productionRules.equals(otherGrammar.productionRules)) { return false; } return true; @@ -43,20 +55,27 @@ public String toString() { String out = ""; out += "Sentinal:\n\t" + sentinal.toString() + "\n\n"; - out += "Tokens:\n" + tabFormat(tokens) + "\n"; - out += "Non-terminals:\n" + tabFormat(nonTerminals) + "\n"; - out += "Production Rules:\n" + tabFormat(productionRules) + "\n"; + out += "Tokens:\n" + indentFormat(tokens) + "\n\n"; + out += "Non-terminals:\n" + indentFormat(nonTerminals) + "\n\n"; + out += "Production Rules:\n" + indentFormat(productionRules) + "\n"; return out; } - private String tabFormat(Collection collection) { - String out = ""; + public Set getTokens() { + return tokens; + } - for (Object object : collection) { - out += "\t" + object.toString() + "\n"; - } + public Set getNonTerminals() { + return nonTerminals; + } - return out; + public List getProductionRules() { + return productionRules; + } + + public NonTerminal getSentinal() { + return sentinal; } + } diff --git a/src/main/java/grammar_objects/GrammarParts.java b/src/main/java/grammar_objects/GrammarParts.java index 3a7a2d4..73ff582 100644 --- a/src/main/java/grammar_objects/GrammarParts.java +++ b/src/main/java/grammar_objects/GrammarParts.java @@ -1,5 +1,7 @@ package grammar_objects; +import static helper_objects.ToStringFormatting.indentFormat; + import java.util.Set; public record GrammarParts( @@ -7,4 +9,17 @@ public record GrammarParts( Set nonTerminals, Set productionRules, NonTerminal sentinal -) {} +) { + + @Override + public final String toString() { + String out = ""; + + out += "Tokens:\n" + indentFormat(tokens) + "\n\n"; + out += "NonTerminals:\n" + indentFormat(nonTerminals) + "\n\n"; + out += "Production Rules:\n" + indentFormat(productionRules) + "\n\n"; + out += "Sentinal: " + sentinal.toString() + "\n"; + + return out; + } +} diff --git a/src/main/java/grammar_objects/RuleConvertor.java b/src/main/java/grammar_objects/RuleConvertor.java index beece05..47cbaf7 100644 --- a/src/main/java/grammar_objects/RuleConvertor.java +++ b/src/main/java/grammar_objects/RuleConvertor.java @@ -44,8 +44,8 @@ public RuleConvertor( conversions.putAll(conversions); } - if (conversions.size() < grammar.productionRules().size() + 1) - throw new IncompleteConversionsException(conversions, grammar.productionRules()); + if (conversions.size() < grammar.getProductionRules().size() + 1) + throw new IncompleteConversionsException(conversions, grammar.getProductionRules()); this.grammar = grammar; this.bookends = bookends; diff --git a/src/main/java/helper_objects/ToStringFormatting.java b/src/main/java/helper_objects/ToStringFormatting.java new file mode 100644 index 0000000..5f296bd --- /dev/null +++ b/src/main/java/helper_objects/ToStringFormatting.java @@ -0,0 +1,33 @@ +package helper_objects; + +import java.util.Collection; + +public class ToStringFormatting { + + /** + * Creates a new line and adds indentation before the string representation of each value in the collection. + * @param The type which the given collection contains + * @param collection The collection provided + * @return The produced string representation of the collection + */ + public static String indentFormat(Collection collection) { + String out = ""; + + for (Object object : collection) { + out += "\t" + object.toString().replace("\n", "\n\t") + "\n"; + } + + return out.stripTrailing(); + } + + /** + * Creates a new line and adds indentation before the string representation of the given value. + * @param The type of the provided value + * @param value The value provided + * @return The produced string representation of the value + */ + public static String indentFormat(E value) { + return "\t" + value.toString().replace("\n", "\n\t"); + } + +} diff --git a/src/main/java/syntax_analysis/grammar_structure_creation/Accept.java b/src/main/java/syntax_analysis/grammar_structure_creation/Accept.java index 3e5865c..8d46ef9 100644 --- a/src/main/java/syntax_analysis/grammar_structure_creation/Accept.java +++ b/src/main/java/syntax_analysis/grammar_structure_creation/Accept.java @@ -2,4 +2,9 @@ public record Accept() implements Action { + @Override + public final String toString() { + return "ACCEPT"; + } + } diff --git a/src/main/java/syntax_analysis/grammar_structure_creation/State.java b/src/main/java/syntax_analysis/grammar_structure_creation/State.java index f75244a..bfc0b0c 100644 --- a/src/main/java/syntax_analysis/grammar_structure_creation/State.java +++ b/src/main/java/syntax_analysis/grammar_structure_creation/State.java @@ -1,5 +1,7 @@ package syntax_analysis.grammar_structure_creation; +import static helper_objects.ToStringFormatting.indentFormat; + import java.util.*; import java.util.Map.*; @@ -107,13 +109,6 @@ public int hashCode() { @Override public String toString() { - String string = "State:\n"; - - for (GrammarPosition position : positions) { - string += "\t" + position.toString() + "\n"; - } - - string.stripTrailing(); - return string; + return "State:\n" + indentFormat(positions); } } diff --git a/src/test/java/code_generation/BasicCodeGeneratorTests.java b/src/test/java/code_generation/BasicCodeGeneratorTests.java index 57afddb..f487b47 100644 --- a/src/test/java/code_generation/BasicCodeGeneratorTests.java +++ b/src/test/java/code_generation/BasicCodeGeneratorTests.java @@ -7,47 +7,47 @@ import grammar_objects.RuleConvertor; import syntax_analysis.parsing.ParseState; import test_aids.*; -import test_aids.test_grammars.basic_identifier.BasicIdentifierTestGrammar; -import test_aids.test_grammars.small_grammar.SmallTestGrammar; +import test_aids.test_grammars.BasicIdentifierTestGrammar; +import test_aids.test_grammars.SmallTestGrammar; public class BasicCodeGeneratorTests { @Test public void testGrammarJavaGeneration() { - TestGrammar grammar = new SmallTestGrammar(GrammarType.LR0); - String sentence = "1+0*1"; String language = "Java"; - RuleConvertor ruleConvertor = grammar.getRuleConvertor(sentence, language); + String sentence = "1+0*1"; + TestGrammar grammar = new SmallTestGrammar().getGrammar(GrammarType.LR0); + RuleConvertor ruleConvertor = grammar.getRuleConvertor(language + " " + sentence); CodeGenerator codeGenerator = new BasicCodeGenerator(ruleConvertor); ParseState rootParseState = grammar.getParseRoot(sentence); String resultingCode = codeGenerator.generate(rootParseState); - String expectedCode = grammar.getGeneratedCode(sentence, language); + String expectedCode = grammar.getCodeGeneration(language + " " + sentence); assertEquals(expectedCode, resultingCode); } @Test public void testGrammarCGeneration() { - TestGrammar grammar = new SmallTestGrammar(GrammarType.LR0); - String sentence = "1+0*1"; String language = "C"; - RuleConvertor ruleConvertor = grammar.getRuleConvertor(sentence, language); + String sentence = "1+0*1"; + TestGrammar grammar = new SmallTestGrammar().getGrammar(GrammarType.LR0); + RuleConvertor ruleConvertor = grammar.getRuleConvertor(language + " " + sentence); CodeGenerator codeGenerator = new BasicCodeGenerator(ruleConvertor); ParseState rootParseState = grammar.getParseRoot(sentence); String resultingCode = codeGenerator.generate(rootParseState); - String expectedCode = grammar.getGeneratedCode(sentence, language); + String expectedCode = grammar.getCodeGeneration(language + " " + sentence); assertEquals(expectedCode, resultingCode); } @Test public void testGrammarJavaMissingStateGeneration() { - TestGrammar grammar = new SmallTestGrammar(GrammarType.LR0); - String sentence = "1+0*1MissingReduction"; String language = "Java"; - RuleConvertor ruleConvertor = grammar.getRuleConvertor(sentence, language); + String sentence = "1+0*1MissingReduction"; + TestGrammar grammar = new SmallTestGrammar().getGrammar(GrammarType.LR0); + RuleConvertor ruleConvertor = grammar.getRuleConvertor(language + " " + sentence); CodeGenerator codeGenerator = new BasicCodeGenerator(ruleConvertor); ParseState rootParseState = grammar.getParseRoot(sentence); @@ -56,25 +56,25 @@ public void testGrammarJavaMissingStateGeneration() { @Test public void testGrammarSigleDigitJavaGeneration() { - TestGrammar grammar = new SmallTestGrammar(GrammarType.LR0); - String sentence = "1"; String language = "Java"; - RuleConvertor ruleConvertor = grammar.getRuleConvertor(sentence, language); + String sentence = "1"; + TestGrammar grammar = new SmallTestGrammar().getGrammar(GrammarType.LR0); + RuleConvertor ruleConvertor = grammar.getRuleConvertor(language + " " + sentence); CodeGenerator codeGenerator = new BasicCodeGenerator(ruleConvertor); ParseState rootParseState = grammar.getParseRoot(sentence); String resultingCode = codeGenerator.generate(rootParseState); - String expectedCode = grammar.getGeneratedCode(sentence, language); + String expectedCode = grammar.getCodeGeneration(language + " " + sentence); assertEquals(expectedCode, resultingCode); } @Test public void testGrammarEmptyReduceJavaGeneration() { - TestGrammar grammar = new SmallTestGrammar(GrammarType.LR0); - String sentence = "emptyReduce"; String language = "Java"; - RuleConvertor ruleConvertor = grammar.getRuleConvertor(sentence, language); + String sentence = "emptyReduce"; + TestGrammar grammar = new SmallTestGrammar().getGrammar(GrammarType.LR0); + RuleConvertor ruleConvertor = grammar.getRuleConvertor(language + " " + sentence); CodeGenerator codeGenerator = new BasicCodeGenerator(ruleConvertor); ParseState rootParseState = grammar.getParseRoot(sentence); @@ -83,16 +83,16 @@ public void testGrammarEmptyReduceJavaGeneration() { @Test public void XToYToXGeneration() { - TestGrammar grammar = new BasicIdentifierTestGrammar(GrammarType.LR0); - String sentence = "XToYToX"; String language = "Java"; - RuleConvertor ruleConvertor = grammar.getRuleConvertor(sentence, language); + String sentence = "XToYToX"; + TestGrammar grammar = new BasicIdentifierTestGrammar().getGrammar(GrammarType.LR0); + RuleConvertor ruleConvertor = grammar.getRuleConvertor(language + " " + sentence); CodeGenerator codeGenerator = new BasicCodeGenerator(ruleConvertor); ParseState rootParseState = grammar.getParseRoot(sentence); String resultingCode = codeGenerator.generate(rootParseState); - String expectedCode = grammar.getGeneratedCode(sentence, language); + String expectedCode = grammar.getCodeGeneration(language + " " + sentence); assertEquals(expectedCode, resultingCode); } } diff --git a/src/test/java/component_construction/CompilerTests.java b/src/test/java/component_construction/CompilerTests.java index be9b9f8..8f3f7c7 100644 --- a/src/test/java/component_construction/CompilerTests.java +++ b/src/test/java/component_construction/CompilerTests.java @@ -12,7 +12,7 @@ import lexical_analysis.DynamicTokenRegex; import syntax_analysis.parsing.ParseFailedException; import test_aids.GrammarType; -import test_aids.test_grammars.basic_identifier.BasicIdentifierTestGrammar; +import test_aids.test_grammars.BasicIdentifierTestGrammar; public class CompilerTests { @@ -47,7 +47,7 @@ public void basicIdentifierCompiler() throws ParseFailedException { String output = compiler.compile(inputSentence); - String expected = new BasicIdentifierTestGrammar(GrammarType.LR0).getGeneratedCode("XToYToXSemantic", "Java"); + String expected = new BasicIdentifierTestGrammar().getGrammar(GrammarType.LR0).getCodeGeneration("Java XToYToXSemantic"); assertEquals(expected, output); } } diff --git a/src/test/java/mock_objects/IntComputationLex.java b/src/test/java/mock_objects/IntComputationLex.java deleted file mode 100644 index e55687a..0000000 --- a/src/test/java/mock_objects/IntComputationLex.java +++ /dev/null @@ -1,13 +0,0 @@ -package mock_objects; - -import grammar_objects.Token; -import lexical_analysis.LexicalAnalyser; - -public class IntComputationLex implements LexicalAnalyser { - - @Override - public Token[] analyse(String sentence) { - throw new UnsupportedOperationException("Unimplemented method 'analyse'"); - } - -} diff --git a/src/test/java/semantic_analysis/TypeCheckerTests.java b/src/test/java/semantic_analysis/TypeCheckerTests.java index 23c54d5..94d440b 100644 --- a/src/test/java/semantic_analysis/TypeCheckerTests.java +++ b/src/test/java/semantic_analysis/TypeCheckerTests.java @@ -5,10 +5,9 @@ import code_generation.*; import grammar_objects.*; -import grammars.basic_identifier.convertors.XToYToXSemantic; import syntax_analysis.parsing.ParseState; import test_aids.*; -import test_aids.test_grammars.basic_identifier.BasicIdentifierTestGrammar; +import test_aids.test_grammars.BasicIdentifierTestGrammar; public class TypeCheckerTests { @@ -16,14 +15,14 @@ public class TypeCheckerTests { public void XToYToXGeneration() { String language = "Java"; String sentence = "XToYToXSemantic"; - TestGrammar grammar = new BasicIdentifierTestGrammar(GrammarType.LR0); - RuleConvertor ruleConvertor = XToYToXSemantic.produce(); + TestGrammar grammar = new BasicIdentifierTestGrammar().getGrammar(GrammarType.LR0); + RuleConvertor ruleConvertor = grammar.getRuleConvertor(language + " " + sentence); CodeGenerator codeGenerator = new BasicCodeGenerator(ruleConvertor); - ParseState rootParseState = grammar.getParseRoot("XToYToXSemantic"); + ParseState rootParseState = grammar.getParseRoot(sentence); String resultingCode = codeGenerator.generate(rootParseState); - String expectedCode = grammar.getGeneratedCode(sentence, language); + String expectedCode = grammar.getCodeGeneration(language + " " + sentence); assertEquals(expectedCode, resultingCode); } diff --git a/src/test/java/syntax_analysis/CLR1ParserTests.java b/src/test/java/syntax_analysis/CLR1ParserTests.java index a1492b7..1a5ffcb 100644 --- a/src/test/java/syntax_analysis/CLR1ParserTests.java +++ b/src/test/java/syntax_analysis/CLR1ParserTests.java @@ -8,11 +8,11 @@ import syntax_analysis.grammar_structure_creation.*; import syntax_analysis.parsing.*; import test_aids.*; -import test_aids.test_grammars.basic_CLR1.BasicCLR1TestGrammar; -import test_aids.test_grammars.basic_SLR1.BasicSLR1TestGrammar; -import test_aids.test_grammars.basic_identifier.BasicIdentifierTestGrammar; -import test_aids.test_grammars.self_referential.SelfReferentialTestGrammar; -import test_aids.test_grammars.small_grammar.SmallTestGrammar; +import test_aids.test_grammars.BasicCLR1TestGrammar; +import test_aids.test_grammars.BasicIdentifierTestGrammar; +import test_aids.test_grammars.BasicSLR1TestGrammar; +import test_aids.test_grammars.SelfReferentialTestGrammar; +import test_aids.test_grammars.SmallTestGrammar; public class CLR1ParserTests { @@ -33,7 +33,7 @@ public void nonTerminalNull() { @Test public void smallTestGrammarStates() { - TestGrammar grammar = new SmallTestGrammar(GrammarType.CLR1); + TestGrammar grammar = new SmallTestGrammar().getGrammar(GrammarType.CLR1); GrammarParts grammarParts = grammar.getParts(); CLR1Parser syntaxAnalyser = new CLR1Parser(grammarParts.tokens(), @@ -102,7 +102,7 @@ public void smallTestGrammarStates() { @Test public void parsingTestGrammarIncompleteSentence() throws ParseFailedException { - TestGrammar grammar = new SmallTestGrammar(GrammarType.LR0); + TestGrammar grammar = new SmallTestGrammar().getGrammar(GrammarType.LR0); GrammarParts grammarParts = grammar.getParts(); CLR1Parser syntaxAnalyser = new CLR1Parser(grammarParts.tokens(), grammarParts.nonTerminals(), @@ -122,7 +122,7 @@ public void parsingTestGrammarIncompleteSentence() throws ParseFailedException { @Test public void parsingTestGrammarIncorrectSentence() { - TestGrammar grammar = new SmallTestGrammar(GrammarType.LR0); + TestGrammar grammar = new SmallTestGrammar().getGrammar(GrammarType.LR0); GrammarParts grammarParts = grammar.getParts(); CLR1Parser syntaxAnalyser = new CLR1Parser(grammarParts.tokens(), grammarParts.nonTerminals(), @@ -143,7 +143,7 @@ public void parsingTestGrammarIncorrectSentence() { @Test public void selfReferentialGrammarStates() { - TestGrammar grammar = new SelfReferentialTestGrammar(GrammarType.CLR1); + TestGrammar grammar = new SelfReferentialTestGrammar().getGrammar(GrammarType.CLR1); GrammarParts grammarParts = grammar.getParts(); CLR1Parser syntaxAnalyser = new CLR1Parser(grammarParts.tokens(), @@ -188,7 +188,7 @@ public void selfReferentialGrammarStates() { @Test public void basicIdentifierGrammarStates() { - TestGrammar grammar = new BasicIdentifierTestGrammar(GrammarType.LR0); + TestGrammar grammar = new BasicIdentifierTestGrammar().getGrammar(GrammarType.LR0); GrammarParts grammarParts = grammar.getParts(); CLR1Parser syntaxAnalyser = new CLR1Parser(grammarParts.tokens(), @@ -304,7 +304,7 @@ public void nonDeterminism() { @Test public void basicSLR1GrammarStates() { - TestGrammar grammar = new BasicSLR1TestGrammar(GrammarType.SLR1); + TestGrammar grammar = new BasicSLR1TestGrammar().getGrammar(); GrammarParts grammarParts = grammar.getParts(); CLR1Parser syntaxAnalyser = new CLR1Parser(grammarParts.tokens(), @@ -372,7 +372,7 @@ public void basicSLR1GrammarStates() { @Test public void CLR1Grammar() { - TestGrammar grammar = new BasicCLR1TestGrammar(GrammarType.CLR1); + TestGrammar grammar = new BasicCLR1TestGrammar().getGrammar(); GrammarParts grammarParts = grammar.getParts(); CLR1Parser syntaxAnalyser = new CLR1Parser(grammarParts.tokens(), @@ -387,7 +387,7 @@ public void CLR1Grammar() { @Test public void CLR1BasicParsing() throws ParseFailedException { - TestGrammar grammar = new BasicCLR1TestGrammar(GrammarType.CLR1); + TestGrammar grammar = new BasicCLR1TestGrammar().getGrammar(); GrammarParts grammarParts = grammar.getParts(); CLR1Parser syntaxAnalyser = new CLR1Parser(grammarParts.tokens(), diff --git a/src/test/java/syntax_analysis/LR0ParserTests.java b/src/test/java/syntax_analysis/LR0ParserTests.java index 8c52ab9..c9504c7 100644 --- a/src/test/java/syntax_analysis/LR0ParserTests.java +++ b/src/test/java/syntax_analysis/LR0ParserTests.java @@ -8,9 +8,9 @@ import syntax_analysis.grammar_structure_creation.*; import syntax_analysis.parsing.*; import test_aids.*; -import test_aids.test_grammars.basic_identifier.BasicIdentifierTestGrammar; -import test_aids.test_grammars.self_referential.SelfReferentialTestGrammar; -import test_aids.test_grammars.small_grammar.SmallTestGrammar; +import test_aids.test_grammars.BasicIdentifierTestGrammar; +import test_aids.test_grammars.SelfReferentialTestGrammar; +import test_aids.test_grammars.SmallTestGrammar; public class LR0ParserTests { @@ -31,7 +31,7 @@ public void nonTerminalNull() { @Test public void smallTestGrammarStates() { - TestGrammar grammar = new SmallTestGrammar(GrammarType.LR0); + TestGrammar grammar = new SmallTestGrammar().getGrammar(GrammarType.LR0); GrammarParts grammarParts = grammar.getParts(); LR0Parser syntaxAnalyser = new LR0Parser(grammarParts.tokens(), @@ -46,7 +46,7 @@ public void smallTestGrammarStates() { @Test public void smallTestGrammarAction() { - TestGrammar grammar = new SmallTestGrammar(GrammarType.LR0); + TestGrammar grammar = new SmallTestGrammar().getGrammar(GrammarType.LR0); GrammarParts grammarParts = grammar.getParts(); LR0Parser syntaxAnalyser = new LR0Parser(grammarParts.tokens(), @@ -61,7 +61,7 @@ public void smallTestGrammarAction() { @Test public void smallTestGrammarGoto() { - TestGrammar grammar = new SmallTestGrammar(GrammarType.LR0); + TestGrammar grammar = new SmallTestGrammar().getGrammar(GrammarType.LR0); GrammarParts grammarParts = grammar.getParts(); LR0Parser syntaxAnalyser = new LR0Parser(grammarParts.tokens(), @@ -76,7 +76,7 @@ public void smallTestGrammarGoto() { @Test public void parsingTestGrammarCompleteSentence() throws ParseFailedException { - TestGrammar grammar = new SmallTestGrammar(GrammarType.LR0); + TestGrammar grammar = new SmallTestGrammar().getGrammar(GrammarType.LR0); GrammarParts grammarParts = grammar.getParts(); LR0Parser syntaxAnalyser = new LR0Parser(grammarParts.tokens(), grammarParts.nonTerminals(), @@ -99,7 +99,7 @@ public void parsingTestGrammarCompleteSentence() throws ParseFailedException { @Test public void parsingTestGrammarIncompleteSentence() { - TestGrammar grammar = new SmallTestGrammar(GrammarType.LR0); + TestGrammar grammar = new SmallTestGrammar().getGrammar(GrammarType.LR0); GrammarParts grammarParts = grammar.getParts(); LR0Parser syntaxAnalyser = new LR0Parser(grammarParts.tokens(), grammarParts.nonTerminals(), @@ -119,7 +119,7 @@ public void parsingTestGrammarIncompleteSentence() { @Test public void parsingTestGrammarIncorrectSentence() { - TestGrammar grammar = new SmallTestGrammar(GrammarType.LR0); + TestGrammar grammar = new SmallTestGrammar().getGrammar(GrammarType.LR0); GrammarParts grammarParts = grammar.getParts(); LR0Parser syntaxAnalyser = new LR0Parser(grammarParts.tokens(), grammarParts.nonTerminals(), @@ -140,7 +140,7 @@ public void parsingTestGrammarIncorrectSentence() { @Test public void selfReferentialGrammarStates() { - TestGrammar grammar = new SelfReferentialTestGrammar(GrammarType.LR0); + TestGrammar grammar = new SelfReferentialTestGrammar().getGrammar(GrammarType.LR0); GrammarParts grammarParts = grammar.getParts(); LR0Parser syntaxAnalyser = new LR0Parser(grammarParts.tokens(), @@ -155,7 +155,7 @@ public void selfReferentialGrammarStates() { @Test public void selfReferentialGrammarAction() { - TestGrammar grammar = new SelfReferentialTestGrammar(GrammarType.LR0); + TestGrammar grammar = new SelfReferentialTestGrammar().getGrammar(GrammarType.LR0); GrammarParts grammarParts = grammar.getParts(); LR0Parser syntaxAnalyser = new LR0Parser(grammarParts.tokens(), @@ -170,7 +170,7 @@ public void selfReferentialGrammarAction() { @Test public void selfReferentialGrammarGoto() { - TestGrammar grammar = new SelfReferentialTestGrammar(GrammarType.LR0); + TestGrammar grammar = new SelfReferentialTestGrammar().getGrammar(GrammarType.LR0); GrammarParts grammarParts = grammar.getParts(); LR0Parser syntaxAnalyser = new LR0Parser(grammarParts.tokens(), @@ -185,7 +185,7 @@ public void selfReferentialGrammarGoto() { @Test public void basicIdentifierGrammarStates() { - TestGrammar grammar = new BasicIdentifierTestGrammar(GrammarType.LR0); + TestGrammar grammar = new BasicIdentifierTestGrammar().getGrammar(GrammarType.LR0); GrammarParts grammarParts = grammar.getParts(); LR0Parser syntaxAnalyser = new LR0Parser(grammarParts.tokens(), @@ -200,7 +200,7 @@ public void basicIdentifierGrammarStates() { @Test public void basicIdentifierGrammarAction() { - TestGrammar grammar = new BasicIdentifierTestGrammar(GrammarType.LR0); + TestGrammar grammar = new BasicIdentifierTestGrammar().getGrammar(GrammarType.LR0); GrammarParts grammarParts = grammar.getParts(); LR0Parser syntaxAnalyser = new LR0Parser(grammarParts.tokens(), @@ -215,7 +215,7 @@ public void basicIdentifierGrammarAction() { @Test public void basicIdentifierGrammarGoto() { - TestGrammar grammar = new BasicIdentifierTestGrammar(GrammarType.LR0); + TestGrammar grammar = new BasicIdentifierTestGrammar().getGrammar(GrammarType.LR0); GrammarParts grammarParts = grammar.getParts(); LR0Parser syntaxAnalyser = new LR0Parser(grammarParts.tokens(), @@ -230,7 +230,7 @@ public void basicIdentifierGrammarGoto() { @Test public void XToYToXGrammarCompleteSentence() throws ParseFailedException { - TestGrammar grammar = new BasicIdentifierTestGrammar(GrammarType.LR0); + TestGrammar grammar = new BasicIdentifierTestGrammar().getGrammar(GrammarType.LR0); GrammarParts grammarParts = grammar.getParts(); LR0Parser syntaxAnalyser = new LR0Parser(grammarParts.tokens(), grammarParts.nonTerminals(), diff --git a/src/test/java/syntax_analysis/SLR1ParserTests.java b/src/test/java/syntax_analysis/SLR1ParserTests.java index e17cf58..326106f 100644 --- a/src/test/java/syntax_analysis/SLR1ParserTests.java +++ b/src/test/java/syntax_analysis/SLR1ParserTests.java @@ -8,10 +8,10 @@ import syntax_analysis.grammar_structure_creation.*; import syntax_analysis.parsing.*; import test_aids.*; -import test_aids.test_grammars.basic_SLR1.BasicSLR1TestGrammar; -import test_aids.test_grammars.basic_identifier.BasicIdentifierTestGrammar; -import test_aids.test_grammars.self_referential.SelfReferentialTestGrammar; -import test_aids.test_grammars.small_grammar.SmallTestGrammar; +import test_aids.test_grammars.BasicIdentifierTestGrammar; +import test_aids.test_grammars.BasicSLR1TestGrammar; +import test_aids.test_grammars.SelfReferentialTestGrammar; +import test_aids.test_grammars.SmallTestGrammar; public class SLR1ParserTests { @@ -32,7 +32,7 @@ public void nonTerminalNull() { @Test public void smallTestGrammarStates() { - TestGrammar grammar = new SmallTestGrammar(GrammarType.LR0); + TestGrammar grammar = new SmallTestGrammar().getGrammar(GrammarType.LR0); GrammarParts grammarParts = grammar.getParts(); SLR1Parser syntaxAnalyser = new SLR1Parser(grammarParts.tokens(), @@ -47,7 +47,7 @@ public void smallTestGrammarStates() { @Test public void smallTestGrammarAction() { - TestGrammar grammar = new SmallTestGrammar(GrammarType.SLR1); + TestGrammar grammar = new SmallTestGrammar().getGrammar(GrammarType.SLR1); GrammarParts grammarParts = grammar.getParts(); SLR1Parser syntaxAnalyser = new SLR1Parser(grammarParts.tokens(), @@ -62,7 +62,7 @@ public void smallTestGrammarAction() { @Test public void smallTestGrammarGoto() { - TestGrammar grammar = new SmallTestGrammar(GrammarType.LR0); + TestGrammar grammar = new SmallTestGrammar().getGrammar(GrammarType.LR0); GrammarParts grammarParts = grammar.getParts(); SLR1Parser syntaxAnalyser = new SLR1Parser(grammarParts.tokens(), @@ -77,7 +77,7 @@ public void smallTestGrammarGoto() { @Test public void parsingTestGrammarCompleteSentence() throws ParseFailedException { - TestGrammar grammar = new SmallTestGrammar(GrammarType.LR0); + TestGrammar grammar = new SmallTestGrammar().getGrammar(GrammarType.LR0); GrammarParts grammarParts = grammar.getParts(); SLR1Parser syntaxAnalyser = new SLR1Parser(grammarParts.tokens(), grammarParts.nonTerminals(), @@ -100,7 +100,7 @@ public void parsingTestGrammarCompleteSentence() throws ParseFailedException { @Test public void parsingTestGrammarIncompleteSentence() { - TestGrammar grammar = new SmallTestGrammar(GrammarType.LR0); + TestGrammar grammar = new SmallTestGrammar().getGrammar(GrammarType.LR0); GrammarParts grammarParts = grammar.getParts(); SLR1Parser syntaxAnalyser = new SLR1Parser(grammarParts.tokens(), grammarParts.nonTerminals(), @@ -120,7 +120,7 @@ public void parsingTestGrammarIncompleteSentence() { @Test public void parsingTestGrammarIncorrectSentence() { - TestGrammar grammar = new SmallTestGrammar(GrammarType.LR0); + TestGrammar grammar = new SmallTestGrammar().getGrammar(GrammarType.LR0); GrammarParts grammarParts = grammar.getParts(); SLR1Parser syntaxAnalyser = new SLR1Parser(grammarParts.tokens(), grammarParts.nonTerminals(), @@ -141,7 +141,7 @@ public void parsingTestGrammarIncorrectSentence() { @Test public void selfReferentialGrammarStates() { - TestGrammar grammar = new SelfReferentialTestGrammar(GrammarType.LR0); + TestGrammar grammar = new SelfReferentialTestGrammar().getGrammar(GrammarType.LR0); GrammarParts grammarParts = grammar.getParts(); SLR1Parser syntaxAnalyser = new SLR1Parser(grammarParts.tokens(), @@ -156,7 +156,7 @@ public void selfReferentialGrammarStates() { @Test public void selfReferentialGrammarAction() { - TestGrammar grammar = new SelfReferentialTestGrammar(GrammarType.SLR1); + TestGrammar grammar = new SelfReferentialTestGrammar().getGrammar(GrammarType.SLR1); GrammarParts grammarParts = grammar.getParts(); SLR1Parser syntaxAnalyser = new SLR1Parser(grammarParts.tokens(), @@ -171,7 +171,7 @@ public void selfReferentialGrammarAction() { @Test public void selfReferentialGrammarGoto() { - TestGrammar grammar = new SelfReferentialTestGrammar(GrammarType.LR0); + TestGrammar grammar = new SelfReferentialTestGrammar().getGrammar(GrammarType.LR0); GrammarParts grammarParts = grammar.getParts(); SLR1Parser syntaxAnalyser = new SLR1Parser(grammarParts.tokens(), @@ -186,7 +186,7 @@ public void selfReferentialGrammarGoto() { @Test public void basicIdentifierGrammarStates() { - TestGrammar grammar = new BasicIdentifierTestGrammar(GrammarType.LR0); + TestGrammar grammar = new BasicIdentifierTestGrammar().getGrammar(GrammarType.LR0); GrammarParts grammarParts = grammar.getParts(); SLR1Parser syntaxAnalyser = new SLR1Parser(grammarParts.tokens(), @@ -201,7 +201,7 @@ public void basicIdentifierGrammarStates() { @Test public void basicIdentifierGrammarAction() { - TestGrammar grammar = new BasicIdentifierTestGrammar(GrammarType.SLR1); + TestGrammar grammar = new BasicIdentifierTestGrammar().getGrammar(GrammarType.SLR1); GrammarParts grammarParts = grammar.getParts(); SLR1Parser syntaxAnalyser = new SLR1Parser(grammarParts.tokens(), @@ -216,7 +216,7 @@ public void basicIdentifierGrammarAction() { @Test public void basicIdentifierGrammarGoto() { - TestGrammar grammar = new BasicIdentifierTestGrammar(GrammarType.LR0); + TestGrammar grammar = new BasicIdentifierTestGrammar().getGrammar(GrammarType.LR0); GrammarParts grammarParts = grammar.getParts(); SLR1Parser syntaxAnalyser = new SLR1Parser(grammarParts.tokens(), @@ -231,7 +231,7 @@ public void basicIdentifierGrammarGoto() { @Test public void XToYToXGrammarCompleteSentence() throws ParseFailedException { - TestGrammar grammar = new BasicIdentifierTestGrammar(GrammarType.LR0); + TestGrammar grammar = new BasicIdentifierTestGrammar().getGrammar(GrammarType.LR0); GrammarParts grammarParts = grammar.getParts(); SLR1Parser syntaxAnalyser = new SLR1Parser(grammarParts.tokens(), grammarParts.nonTerminals(), @@ -301,7 +301,7 @@ public void nonDeterminism() { @Test public void basicSLR1GrammarStates() { - TestGrammar grammar = new BasicSLR1TestGrammar(GrammarType.SLR1); + TestGrammar grammar = new BasicSLR1TestGrammar().getGrammar(); GrammarParts grammarParts = grammar.getParts(); SLR1Parser syntaxAnalyser = new SLR1Parser(grammarParts.tokens(), @@ -316,7 +316,7 @@ public void basicSLR1GrammarStates() { @Test public void basicSLR1GrammarAction() { - TestGrammar grammar = new BasicSLR1TestGrammar(GrammarType.SLR1); + TestGrammar grammar = new BasicSLR1TestGrammar().getGrammar(); GrammarParts grammarParts = grammar.getParts(); SLR1Parser syntaxAnalyser = new SLR1Parser(grammarParts.tokens(), @@ -331,7 +331,7 @@ public void basicSLR1GrammarAction() { @Test public void basicSLR1GrammarGoto() { - TestGrammar grammar = new BasicSLR1TestGrammar(GrammarType.SLR1); + TestGrammar grammar = new BasicSLR1TestGrammar().getGrammar(); GrammarParts grammarParts = grammar.getParts(); SLR1Parser syntaxAnalyser = new SLR1Parser(grammarParts.tokens(), @@ -346,7 +346,7 @@ public void basicSLR1GrammarGoto() { @Test public void basicSLR1GrammarCompleteSentence() throws ParseFailedException { - TestGrammar grammar = new BasicSLR1TestGrammar(GrammarType.SLR1); + TestGrammar grammar = new BasicSLR1TestGrammar().getGrammar(); GrammarParts grammarParts = grammar.getParts(); SLR1Parser syntaxAnalyser = new SLR1Parser(grammarParts.tokens(), grammarParts.nonTerminals(), diff --git a/src/test/java/test_aids/TestGrammar.java b/src/test/java/test_aids/TestGrammar.java index 766fbc3..18ec43d 100644 --- a/src/test/java/test_aids/TestGrammar.java +++ b/src/test/java/test_aids/TestGrammar.java @@ -1,97 +1,45 @@ package test_aids; -import java.util.*; +import static helper_objects.ToStringFormatting.indentFormat; +import java.util.*; +import java.util.stream.Collectors; import grammar_objects.*; import syntax_analysis.grammar_structure_creation.*; import syntax_analysis.parsing.ParseState; -import test_aids.test_grammars.UnsupportedSentenceException; - -//TODO: Rework to use Grammar builder -public abstract class TestGrammar { //TODO allow implementation of grammar interface for propper testing +public class TestGrammar extends Grammar { - protected Grammar grammar; - - //TODO: Change all based on chosen grammar type, rework type usage - private List states = new ArrayList<>(); - private Map> actionTable = new HashMap<>(); - private Map> gotoTable = new HashMap<>(); - private Map> ruleConvertorMap = new HashMap<>(); //Language, - private Map> codeGenerations = new HashMap<>(); //Language, - private Map parseRootMap = new HashMap<>(); - - protected interface ParseTreeBuilder { - public ParseState buildTree(); - } - - public TestGrammar(GrammarType type) { - grammar = setUpGrammar(type); - - setUpStates( - type, - states, - new ProductionRule(null, new LexicalElement[] {grammar.getParts().sentinal()}), - new EOF() - ); - - for (State state : getStates()) { - actionTable.put(state, new HashMap<>()); - gotoTable.put(state, new HashMap<>()); - } - - setUpActionTable(type, actionTable, new EOF()); - setUpGotoTable(type, gotoTable); - - setUpParseTrees(parseRootMap); - - setUpRuleConvertors(type, ruleConvertorMap); - setUpCodeGenerations(type, codeGenerations); - } - - protected abstract Grammar setUpGrammar(GrammarType type); - - protected abstract void setUpStates(GrammarType type, List states, ProductionRule extraRootRule, Token endOfFile); - /** - * Populates the action table for this grammar, states are populated in-order by setUpStates() and may be accessed with getState(int index) - * @param type The type of the grammar to be used - * @param actionTable The action table to be populated - * @param endOfFile The end of file token - */ - protected abstract void setUpActionTable(GrammarType type, Map> actionTable, Token endOfFile); - /** - * Populates the goto table for this grammar, states are populated in-order by setUpStates() and may be accessed with getState(int index) - * @param type The type of the grammar to be used - * @param gotoTable The goto table to be populated - */ - protected abstract void setUpGotoTable(GrammarType type, Map> gotoTable); - /** - * Sets up the parse trees for sentences supported by this TestGrammar - * @param parseRootMap A map of sentence name and the builder function for its parse tree - */ - protected abstract void setUpParseTrees(Map parseRootMap); - protected abstract void setUpRuleConvertors(GrammarType type, Map> ruleConvertorMap); - protected abstract void setUpCodeGenerations(GrammarType type, Map> codeGenerations); - - public ProductionRule getRule(int index) { - return grammar.getRule(index); - } - - public GrammarParts getParts() { - return grammar.getParts(); + protected Set states; + protected Map> actionTable; + protected Map> gotoTable; + protected Map ruleConvertors; + protected Map codeGenerations; + protected Map parseRoots; + + public TestGrammar( + Set tokens, + Set nonTerminals, + List productionRules, + NonTerminal sentinal, + Set states, + Map> actionTable, + Map> gotoTable, + Map ruleConvertors, + Map codeGenerations, + Map parseRoots + ) { + super(tokens, nonTerminals, productionRules, sentinal); + this.states = states; + this.actionTable = actionTable; + this.gotoTable = gotoTable; + this.ruleConvertors = ruleConvertors; + this.codeGenerations = codeGenerations; + this.parseRoots = parseRoots; } public Set getStates() { - return new HashSet<>(states); - } - - /** - * Gets a state populated in-order by setUpStates() - * @param index The index of the state to be returned - * @return The state found - */ - protected State getState(int index) { - return states.get(index); + return states; } public Map> getActionTable() { @@ -102,52 +50,50 @@ public Map> getGotoTable() { return gotoTable; } - /** - * Gets the root parse state (and contained parse tree) for a given sentence - * @param sentence The sentence to be parsed - * @return The root ParseState of the parse tree - */ - public ParseState getParseRoot(String sentence) { //TODO: Make dependent on grammar type - ParseState root = parseRootMap.get(sentence).buildTree(); + public ParseState getParseRoot(String sentenceName) { + return parseRoots.get(sentenceName); + } - if(root == null) { - throw new UnsupportedSentenceException("parse root", sentence); - } + public RuleConvertor getRuleConvertor(String sentenceName) { + return ruleConvertors.get(sentenceName); + } - return root; + public String getCodeGeneration(String sentenceName) { + return codeGenerations.get(sentenceName); } - public RuleConvertor getRuleConvertor(String sentence, String language) { - RuleConvertor ruleConvertor = null; + @Override + public String toString() { + String out = super.toString() + "\n"; - try { - ruleConvertor = ruleConvertorMap.get(language).get(sentence); - } - catch (NullPointerException e) { - throw new UnsupportedSentenceException("langage and rule convertor", sentence); - } + out += "States:\n" + indentFormat(states) + "\n\n"; - if(ruleConvertor == null) { - throw new UnsupportedSentenceException("rule convertor", sentence); - } + out += "ActionTable:\n" + indentFormat(actionTable.entrySet().stream() + .map(entry -> entry.getKey().toString() + ": " + entry.getValue().toString()) + .collect(Collectors.toList()) + ) + "\n\n"; - return ruleConvertor; - } + out += "GotoTable:\n" + indentFormat(actionTable.entrySet().stream() + .map(entry -> entry.getKey().toString() + ": " + entry.getValue().toString()) + .collect(Collectors.toList()) + ) + "\n\n"; - public String getGeneratedCode(String sentence, String language) { - String generatedCode = null; + out += "RuleConvertors:\n" + indentFormat(actionTable.entrySet().stream() + .map(entry -> entry.getKey().toString() + ": " + entry.getValue().toString()) + .collect(Collectors.toList()) + ) + "\n\n"; - try { - generatedCode = codeGenerations.get(language).get(sentence); - } - catch (NullPointerException e) { - throw new UnsupportedSentenceException("langage and code generation", sentence); - } + out += "CodeGenerations:\n" + indentFormat(actionTable.entrySet().stream() + .map(entry -> entry.getKey().toString() + ": " + entry.getValue().toString()) + .collect(Collectors.toList()) + ) + "\n\n"; - if(generatedCode == null) { - throw new UnsupportedSentenceException("code generation", sentence); - } + out += "ParseRoots:\n" + indentFormat(actionTable.entrySet().stream() + .map(entry -> entry.getKey().toString() + ": " + entry.getValue().toString()) + .collect(Collectors.toList()) + ) + "\n"; - return generatedCode; + return out; } + } diff --git a/src/test/java/test_aids/TestGrammarBuilder.java b/src/test/java/test_aids/TestGrammarBuilder.java new file mode 100644 index 0000000..4a84970 --- /dev/null +++ b/src/test/java/test_aids/TestGrammarBuilder.java @@ -0,0 +1,250 @@ +package test_aids; + +import java.util.*; +import java.util.Map.Entry; +import grammar_objects.*; +import syntax_analysis.grammar_structure_creation.*; +import syntax_analysis.parsing.ParseState; + +//TODO: For all "select" options, ensure the value is held within the builder (or add it if this seems reasonable) +public class TestGrammarBuilder { + + protected final TestGrammarBuilder builder; + + // Helper properties + public final ProductionRule extraRootRule; + public final EOF endOfFile = new EOF(); + + protected Grammar grammar; + protected Set states = new HashSet<>(); + protected Map> actionTable = new HashMap<>(); + protected Map> gotoTable = new HashMap<>(); + protected Map ruleConvertors = new HashMap<>(); + protected Map codeGenerations = new HashMap<>(); + protected Map parseRoots = new HashMap<>(); + + public TestGrammarBuilder(Grammar grammar) { + this.grammar = grammar; + extraRootRule = new ProductionRule(null, new LexicalElement[] { grammar.getParts().sentinal() }); + builder = this; + } + + public StateGatherer setUp() { + return new StateGatherer(); + } + + public class StateGatherer { + + public StateGatherer addState(State state) { + builder.states.add(state); + return this; + } + + public StateGatherer addStates(List states) { + builder.states.addAll(states); + return this; + } + + public StateGatherer addBranch(State state, Route route) { + state.addBranch(route); + return this; + } + + public StateGatherer addBranches(State states, Collection routes) { + for (Route route : routes) { + addBranch(states, route); + } + + return this; + } + + /** + * Selects the state for addingBranches. + * @param state The state to be selecte. If the state was not already added then this operation will also add the state to the TestGrammar being built. + * @return This builder for method chaining. + */ + public SelectedStateGatherer selectState(State state) { + builder.states.add(state); + return new SelectedStateGatherer(this, state); + } + + public TableGatherer commitStates() { + return new TableGatherer(); + } + + public class SelectedStateGatherer { + + StateGatherer stateGatherer; + State selectedState; + + public SelectedStateGatherer(StateGatherer stateGatherer, State state) { + this.stateGatherer = stateGatherer; + this.selectedState = state; + } + + public SelectedStateGatherer addBranch(Route route) { + stateGatherer.addBranch(selectedState, route); + return this; + } + + public SelectedStateGatherer addBranches(Collection routes) { + stateGatherer.addBranches(selectedState, routes); + return this; + } + + public StateGatherer deselectState() { + return stateGatherer; + } + + } + + } + + public class TableGatherer { + + public TableGatherer() { + for (State state : states) { + builder.actionTable.put(state, new HashMap<>()); + builder.gotoTable.put(state, new HashMap<>()); + } + } + + public SelectedTableGatherer selectState(State state) { + if (!builder.states.contains(state)) + throw new StateNotCommitted(state); + + return new SelectedTableGatherer(this, state); + } + + public ParseTreeGatherer commitTables() { + return new ParseTreeGatherer(); + } + + //TODO: Ensure the right amount of action and goto states if possible + public class SelectedTableGatherer { + + TableGatherer tableGatherer; + State selectedState; + + public SelectedTableGatherer(TableGatherer tableGatherer, State state) { + this.tableGatherer = tableGatherer; + this.selectedState = state; + } + + public SelectedTableGatherer addAction(Token token, Action action) { + builder.actionTable.get(selectedState).put(token, action); + return this; + } + + public SelectedTableGatherer addActions(Map actions) { + for (Entry entry : actions.entrySet()) { + addAction(entry.getKey(), entry.getValue()); + } + + return this; + } + + public SelectedTableGatherer addGoto(NonTerminal nonTerminal, State gotoState) { + builder.gotoTable.get(selectedState).put(nonTerminal, gotoState); + return this; + } + + public SelectedTableGatherer addGotos(Map gotos) { + for (Entry entry : gotos.entrySet()) { + addGoto(entry.getKey(), entry.getValue()); + } + + return this; + } + + public TableGatherer deselectState() { + return tableGatherer; + } + + } + + } + + //TODO: Consider grouping all sentence-wise details by selecting sentence and adding options + public class ParseTreeGatherer { + + public ParseTreeGatherer setParseTreeRoot(String sentenceName, ParseState parseRoot) { + builder.parseRoots.put(sentenceName, parseRoot); + return new ParseTreeGatherer(); + } + + public ParseTreeGatherer setParseTreeRoots(Map parseRoots) { + builder.parseRoots.putAll(parseRoots); + return new ParseTreeGatherer(); + } + + public RuleConvertorGatherer commitParseTrees() { + return new RuleConvertorGatherer(); + } + + } + + public class RuleConvertorGatherer { + + public RuleConvertorGatherer setRuleConvertor(String sentenceName, RuleConvertor ruleConvertor) { + builder.ruleConvertors.put(sentenceName, ruleConvertor); + return new RuleConvertorGatherer(); + } + + public RuleConvertorGatherer setRuleConversions(Map conversions) { + builder.ruleConvertors.putAll(conversions); + return new RuleConvertorGatherer(); + } + + public CodeGenerationGatherer commitRuleConvertors() { + return new CodeGenerationGatherer(); + } + + } + + public class CodeGenerationGatherer { + + public CodeGenerationGatherer setCodeGeneration(String sentenceName, String generatedCode) { + builder.codeGenerations.put(sentenceName, generatedCode); + return new CodeGenerationGatherer(); + } + + public CodeGenerationGatherer setCodeGenerations(Map generations) { + builder.codeGenerations.putAll(generations); + return new CodeGenerationGatherer(); + } + + public TestGrammarFinalizer commitCodeGenerations() { + return new TestGrammarFinalizer(); + } + + } + + public class TestGrammarFinalizer { + + public TestGrammar generateTestGrammar() { + return new TestGrammar( + grammar.getTokens(), + grammar.getNonTerminals(), + grammar.getProductionRules(), + grammar.getSentinal(), + builder.states, + builder.actionTable, + builder.gotoTable, + builder.ruleConvertors, + builder.codeGenerations, + builder.parseRoots + ); + } + + } + + public class StateNotCommitted extends RuntimeException { + + public StateNotCommitted(State state) { + super("The state was not committed to the test grammar: " + state); + } + + } + +} diff --git a/src/test/java/test_aids/test_grammars/basic_CLR1/BasicCLR1TestGrammar.java b/src/test/java/test_aids/test_grammars/BasicCLR1TestGrammar.java similarity index 57% rename from src/test/java/test_aids/test_grammars/basic_CLR1/BasicCLR1TestGrammar.java rename to src/test/java/test_aids/test_grammars/BasicCLR1TestGrammar.java index 135e2a7..b4972f9 100644 --- a/src/test/java/test_aids/test_grammars/basic_CLR1/BasicCLR1TestGrammar.java +++ b/src/test/java/test_aids/test_grammars/BasicCLR1TestGrammar.java @@ -1,4 +1,4 @@ -package test_aids.test_grammars.basic_CLR1; +package test_aids.test_grammars; import java.util.*; @@ -7,19 +7,55 @@ import syntax_analysis.grammar_structure_creation.*; import test_aids.*; -public class BasicCLR1TestGrammar extends TestGrammar { +public class BasicCLR1TestGrammar { - public BasicCLR1TestGrammar(GrammarType type) { - super(type); - } + private Grammar grammar = BasicCLR1Grammar.produce(); + private List states; + + public TestGrammar getGrammar() { + TestGrammarBuilder builder = new TestGrammarBuilder(grammar); + + states = gatherStates(builder.extraRootRule); + + return builder.setUp() + .addStates(gatherStates(builder.extraRootRule)) + .selectState(states.get(0)) + .addBranch(new Route(states.get(1), new NonTerminal("S"))) + .addBranch(new Route(states.get(2), new NonTerminal("X"))) + .addBranch(new Route(states.get(7), new Token("a"))) + .addBranch(new Route(states.get(9), new Token("b"))) + .deselectState() + + .selectState(states.get(2)) + .addBranch(new Route(states.get(3), new NonTerminal("X"))) + .addBranch(new Route(states.get(4), new Token("a"))) + .addBranch(new Route(states.get(6), new Token("b"))) + .deselectState() + + .selectState(states.get(4)) + .addBranch(new Route(states.get(5), new NonTerminal("X"))) + .addBranch(new Route(states.get(4), new Token("a"))) + .addBranch(new Route(states.get(6), new Token("b"))) + .deselectState() + + .selectState(states.get(7)) + .addBranch(new Route(states.get(8), new NonTerminal("X"))) + .addBranch(new Route(states.get(7), new Token("a"))) + .addBranch(new Route(states.get(9), new Token("b"))) + .deselectState() + + .commitStates() + .commitTables() + .commitParseTrees() + .commitRuleConvertors() + .commitCodeGenerations() + .generateTestGrammar(); - @Override - protected Grammar setUpGrammar(GrammarType type) { - return BasicCLR1Grammar.produce(); } - @Override - protected void setUpStates(GrammarType type, List states, ProductionRule extraRootRule, Token endOfFile) { + private List gatherStates(ProductionRule extraRootRule) { + states = new ArrayList(); + states.add(new State( //0 Set.of(new GrammarPosition[] { new CLR1Position(extraRootRule, 0, Set.of(new EOF())), @@ -40,7 +76,7 @@ protected void setUpStates(GrammarType type, List states, ProductionRule Set.of(new GrammarPosition[] { new CLR1Position(extraRootRule, 1, Set.of(new EOF())), }), - getState(0) + states.get(0) )); states.add(new State( //2 @@ -49,14 +85,14 @@ protected void setUpStates(GrammarType type, List states, ProductionRule new CLR1Position(getRule(1), 0, Set.of(new EOF())), new CLR1Position(getRule(2), 0, Set.of(new EOF())), }), - getState(0) + states.get(0) )); states.add(new State( //3 Set.of(new GrammarPosition[] { new CLR1Position(getRule(0), 2, Set.of(new EOF())), }), - getState(2) + states.get(2) )); states.add(new State( //4 @@ -65,21 +101,21 @@ protected void setUpStates(GrammarType type, List states, ProductionRule new CLR1Position(getRule(1), 0, Set.of(new EOF())), new CLR1Position(getRule(2), 0, Set.of(new EOF())), }), - getState(2) + states.get(2) )); states.add(new State( //5 Set.of(new GrammarPosition[] { new CLR1Position(getRule(1), 2, Set.of(new EOF())), }), - getState(4) + states.get(4) )); states.add(new State( //6 Set.of(new GrammarPosition[] { new CLR1Position(getRule(2), 1, Set.of(new EOF())), }), - getState(4) + states.get(4) )); states.add(new State( //7 @@ -97,7 +133,7 @@ protected void setUpStates(GrammarType type, List states, ProductionRule new Token("b") )), }), - getState(0) + states.get(0) )); states.add(new State( //8 @@ -107,7 +143,7 @@ protected void setUpStates(GrammarType type, List states, ProductionRule new Token("b") )), }), - getState(7) + states.get(7) )); states.add(new State( //9 @@ -117,55 +153,14 @@ protected void setUpStates(GrammarType type, List states, ProductionRule new Token("b") )), }), - getState(7) + states.get(7) )); - - getState(0) - .addBranch(new Route(getState(1), new NonTerminal("S"))) - .addBranch(new Route(getState(2), new NonTerminal("X"))) - .addBranch(new Route(getState(7), new Token("a"))) - .addBranch(new Route(getState(9), new Token("b"))); - - getState(2) - .addBranch(new Route(getState(3), new NonTerminal("X"))) - .addBranch(new Route(getState(4), new Token("a"))) - .addBranch(new Route(getState(6), new Token("b"))); - - getState(4) - .addBranch(new Route(getState(5), new NonTerminal("X"))) - .addBranch(new Route(getState(4), new Token("a"))) - .addBranch(new Route(getState(6), new Token("b"))); - - getState(7) - .addBranch(new Route(getState(8), new NonTerminal("X"))) - .addBranch(new Route(getState(7), new Token("a"))) - .addBranch(new Route(getState(9), new Token("b"))); - } - - @Override - protected void setUpActionTable(GrammarType type, Map> actionTable, Token endOfFile) { - // Unimplemented + return states; } - @Override - protected void setUpGotoTable(GrammarType type, Map> gotoTable) { - // Unimplemented - } - - @Override - protected void setUpParseTrees(Map parseRootMap) { - // Unimplemented - } - - @Override - protected void setUpRuleConvertors(GrammarType type, Map> ruleConvertorMap) { - // Unimplemented - } - - @Override - protected void setUpCodeGenerations(GrammarType type, Map> codeGenerations) { - // Unimplemented + private ProductionRule getRule(int index) { + return grammar.getRule(index); } } diff --git a/src/test/java/test_aids/test_grammars/BasicIdentifierTestGrammar.java b/src/test/java/test_aids/test_grammars/BasicIdentifierTestGrammar.java new file mode 100644 index 0000000..84bd961 --- /dev/null +++ b/src/test/java/test_aids/test_grammars/BasicIdentifierTestGrammar.java @@ -0,0 +1,563 @@ +package test_aids.test_grammars; + +import java.util.*; +import java.util.function.Function; +import java.util.stream.Collectors; +import grammar_objects.*; +import grammars.basic_identifier.BasicIdentifierGrammar; +import grammars.basic_identifier.convertors.*; +import syntax_analysis.grammar_structure_creation.*; +import syntax_analysis.parsing.*; +import test_aids.*; +import test_aids.TestGrammarBuilder.TableGatherer; +import test_aids.test_grammars.exceptions.UnsupportedGrammarException; + +/** + * := | + * + * := identifier = + ; + * := identifier | number + */ +public class BasicIdentifierTestGrammar { + + private Grammar grammar = BasicIdentifierGrammar.produce(); + private EOF eof; + private List states; + private List allTokens; + + public TestGrammar getGrammar(GrammarType grammarType) { + TestGrammarBuilder builder = new TestGrammarBuilder(grammar); + + eof = builder.endOfFile; + states = gatherStates(builder.extraRootRule); + + allTokens = new ArrayList<>(); + allTokens.addAll(grammar.getParts().tokens()); + allTokens.add(eof); + + TableGatherer tableGatherer = builder.setUp() + .addStates(states) + .selectState(states.get(0)) + .addBranch(new Route(states.get(1), new NonTerminal("statement list"))) + .addBranch(new Route(states.get(10), new NonTerminal("statement"))) + .addBranch(new Route(states.get(3), new Identifier("identifier"))) + .deselectState() + + .selectState(states.get(1)) + .addBranch(new Route(states.get(2), new NonTerminal("statement"))) + .addBranch(new Route(states.get(3), new Identifier("identifier"))) + .deselectState() + + .selectState(states.get(3)) + .addBranch(new Route(states.get(4), new Token("="))) + .deselectState() + + .selectState(states.get(4)) + .addBranch(new Route(states.get(5), new NonTerminal("element"))) + .addBranch(new Route(states.get(8), new Identifier("identifier"))) + .addBranch(new Route(states.get(9), new Literal("number"))) + .deselectState() + + .selectState(states.get(5)) + .addBranch(new Route(states.get(6), new Token("+"))) + .deselectState() + + .selectState(states.get(6)) + .addBranch(new Route(states.get(7), new NonTerminal("element"))) + .addBranch(new Route(states.get(8), new Identifier("identifier"))) + .addBranch(new Route(states.get(9), new Literal("number"))) + .deselectState() + + .selectState(states.get(7)) + .addBranch(new Route(states.get(11), new Token(";"))) + .deselectState() + + .commitStates(); + + tableGatherer = setUpActionTable(grammarType, tableGatherer); + + return tableGatherer + .selectState(states.get(0)) + .addGoto(new NonTerminal("statement list"), states.get(1)) + .addGoto(new NonTerminal("statement"), states.get(10)) + .deselectState() + + .selectState(states.get(1)) + .addGoto(new NonTerminal("statement"), states.get(2)) + .deselectState() + + .selectState(states.get(4)) + .addGoto(new NonTerminal("element"), states.get(5)) + .deselectState() + + .selectState(states.get(6)) + .addGoto(new NonTerminal("element"), states.get(7)) + .deselectState() + + .commitTables() + + .setParseTreeRoot("XToYToX", parseTree0()) + .setParseTreeRoot("XToYToXSemantic", parseTree0()) + + .commitParseTrees() + + .setRuleConvertor("Java XToYToX", XToYToX.produce()) + .setRuleConvertor("Java XToYToXSemantic", XToYToXSemantic.produce()) + + .commitRuleConvertors() + + .setCodeGeneration("Java XToYToX", + "public class TestGrammar {\n" + + "\tpublic static void main(String[] args) {\n" + + "\t\tx = 1 + 2;\n" + + "\t\ty = x + 3;\n" + + "\t\tx = y + 0;\n" + + "\t\tSystem.out.println(x);\n" + + "\t}\n" + + "}" + ) + .setCodeGeneration("Java XToYToXSemantic", + "public class TestGrammar {\n" + + "\tpublic static void main(String[] args) {\n" + + "\t\tint x = 1 + 2;\n" + + "\t\tint y = x + 3;\n" + + "\t\tx = y + 0;\n" + + "\t\tSystem.out.println(x);\n" + + "\t}\n" + + "}" + ) + + .commitCodeGenerations() + .generateTestGrammar(); + } + + private List gatherStates(ProductionRule extraRootRule) { + states = new ArrayList(); + + states.add(new State( //0 + Set.of(new GrammarPosition[] { + new GrammarPosition(extraRootRule, 0), + new GrammarPosition(getRule(1), 0), + new GrammarPosition(getRule(0), 0), + new GrammarPosition(getRule(2), 0) + }), + null + )); + + states.add(new State( //1 + Set.of(new GrammarPosition[] { + new GrammarPosition(extraRootRule, 1), + new GrammarPosition(getRule(1), 1), + new GrammarPosition(getRule(2), 0) + }), + states.get(0) + )); + + states.add(new State( //2 + Set.of(new GrammarPosition[] { + new GrammarPosition(getRule(1), 2), + }), + states.get(1) + )); + + states.add(new State( //3 + Set.of(new GrammarPosition[] { + new GrammarPosition(getRule(2), 1), + }), + states.get(1) + )); + + states.add(new State( //4 + Set.of(new GrammarPosition[] { + new GrammarPosition(getRule(2), 2), + new GrammarPosition(getRule(3), 0), + new GrammarPosition(getRule(4), 0) + }), + states.get(3) + )); + + states.add(new State( //5 + Set.of(new GrammarPosition[] { + new GrammarPosition(getRule(2), 3) + }), + states.get(4) + )); + + states.add(new State( //6 + Set.of(new GrammarPosition[] { + new GrammarPosition(getRule(2), 4), + new GrammarPosition(getRule(3), 0), + new GrammarPosition(getRule(4), 0) + }), + states.get(5) + )); + + states.add(new State( //7 + Set.of(new GrammarPosition[] { + new GrammarPosition(getRule(2), 5) + }), + states.get(6) + )); + + states.add(new State( //8 + Set.of(new GrammarPosition[] { + new GrammarPosition(getRule(3), 1) + }), + states.get(6) + )); + + states.add(new State( //9 + Set.of(new GrammarPosition[] { + new GrammarPosition(getRule(4), 1) + }), + states.get(6) + )); + + states.add(new State( //10 + Set.of(new GrammarPosition[] { + new GrammarPosition(getRule(0), 1) + }), + states.get(0) + )); + + states.add(new State( //11 + Set.of(new GrammarPosition[] { + new GrammarPosition(getRule(2), 6) + }), + states.get(7) + )); + + return states; + } + + private TableGatherer setUpActionTable(GrammarType type, TableGatherer tableGatherer) { + switch (type) { + case LR0 -> lr0ActionTable(tableGatherer); + case SLR1 -> slr1ActionTable(tableGatherer); + + default -> throw new UnsupportedGrammarException(type); + } + + return tableGatherer; + } + + private void lr0ActionTable(TableGatherer tableGatherer) { + tableGatherer + .selectState(states.get(0)) + .addAction(new Identifier("identifier"), new Shift(states.get(3))) + .deselectState() + + .selectState(states.get(1)) + .addAction(new Identifier("identifier"), new Shift(states.get(3))) + .addAction(eof, new Accept()) + .deselectState() + + .selectState(states.get(2)) + .addActions(allTokens.stream().collect(Collectors.toMap( + Function.identity(), + token -> new Reduction(getRule(1)) + ))) + .deselectState() + + .selectState(states.get(3)) + .addAction(new Token("="), new Shift(states.get(4))) + .deselectState() + + .selectState(states.get(4)) + .addAction(new Identifier("identifier"), new Shift(states.get(8))) + .addAction(new Literal("number"), new Shift(states.get(9))) + .deselectState() + + .selectState(states.get(5)) + .addAction(new Token("+"), new Shift(states.get(6))) + .deselectState() + + .selectState(states.get(6)) + .addAction(new Identifier("identifier"), new Shift(states.get(8))) + .addAction(new Literal("number"), new Shift(states.get(9))) + .deselectState() + + .selectState(states.get(7)) + .addAction(new Token(";"), new Shift( states.get(11))) + .deselectState() + + .selectState(states.get(8)) + .addActions(allTokens.stream().collect(Collectors.toMap( + Function.identity(), + token -> new Reduction(getRule(3)) + ))) + .deselectState() + + .selectState(states.get(9)) + .addActions(allTokens.stream().collect(Collectors.toMap( + Function.identity(), + token -> new Reduction(getRule(4)) + ))) + .deselectState() + + .selectState(states.get(10)) + .addActions(allTokens.stream().collect(Collectors.toMap( + Function.identity(), + token -> new Reduction(getRule(0)) + ))) + .deselectState() + + .selectState(states.get(11)) + .addActions(allTokens.stream().collect(Collectors.toMap( + Function.identity(), + token -> new Reduction(getRule(2)) + ))) + .deselectState(); + } + + private void slr1ActionTable(TableGatherer tableGatherer) { + tableGatherer + .selectState(states.get(0)) + .addAction(new Identifier("identifier"), new Shift(states.get(3))) + .deselectState() + + .selectState(states.get(1)) + .addAction(new Identifier("identifier"), new Shift(states.get(3))) + .addAction(eof, new Accept()) + .deselectState() + + .selectState(states.get(2)) + .addAction(new Identifier("identifier"), new Reduction(getRule(1))) + .addAction(eof, new Reduction(getRule(1))) + .deselectState() + + .selectState(states.get(3)) + .addAction(new Token("="), new Shift(states.get(4))) + .deselectState() + + .selectState(states.get(4)) + .addAction(new Identifier("identifier"), new Shift(states.get(8))) + .addAction(new Literal("number"), new Shift(states.get(9))) + .deselectState() + + .selectState(states.get(5)) + .addAction(new Token("+"), new Shift(states.get(6))) + .deselectState() + + .selectState(states.get(6)) + .addAction(new Identifier("identifier"), new Shift(states.get(8))) + .addAction(new Literal("number"), new Shift(states.get(9))) + .deselectState() + + .selectState(states.get(7)) + .addAction(new Token(";"),new Shift( states.get(11))) + .deselectState() + + .selectState(states.get(8)) + .addAction(new Token("+"), new Reduction(getRule(3))) + .addAction(new Token(";"), new Reduction(getRule(3))) + .deselectState() + + .selectState(states.get(9)) + .addAction(new Token("+"), new Reduction(getRule(4))) + .addAction(new Token(";"), new Reduction(getRule(4))) + .deselectState() + + .selectState(states.get(10)) + .addAction(new Identifier("identifier"), new Reduction(getRule(0))) + .addAction(eof, new Reduction(getRule(0))) + .deselectState() + + .selectState(states.get(11)) + .addAction(new Identifier("identifier"), new Reduction(getRule(2))) + .addAction(eof, new Reduction(getRule(2))) + .deselectState(); + } + + /** + * Parse tree for the sentence "XToYToX" + * @return The root ParseState of the tree + */ + private ParseState parseTree0() { + List parseStates = new ArrayList<>(); + + parseStates.add(new ShiftedState( + states.get(3), + new Identifier("identifier", "x"))); + + parseStates.add(new ShiftedState( + states.get(4), + new Token("="))); + + parseStates.add(new ShiftedState( + states.get(9), + new Literal("number", "1"))); + + parseStates.add(new ShiftedState( + states.get(6), + new Token("+"))); + + parseStates.add(new ShiftedState( + states.get(9), + new Literal("number", "2"))); + + parseStates.add(new ShiftedState( + states.get(11), + new Token(";"))); + + parseStates.add(new ShiftedState( + states.get(3), + new Identifier("identifier", "y"))); + + parseStates.add(new ShiftedState( + states.get(4), + new Token("="))); + + parseStates.add(new ShiftedState( + states.get(8), + new Identifier("identifier", "x"))); + + parseStates.add(new ShiftedState( + states.get(6), + new Token("+"))); + + parseStates.add(new ShiftedState( + states.get(9), + new Literal("number", "3"))); + + parseStates.add(new ShiftedState( + states.get(11), + new Token(";"))); + + parseStates.add(new ShiftedState( + states.get(3), + new Identifier("identifier", "x"))); + + parseStates.add(new ShiftedState( + states.get(4), + new Token("="))); + + parseStates.add(new ShiftedState( + states.get(8), + new Identifier("identifier", "y"))); + + parseStates.add(new ShiftedState( + states.get(6), + new Token("+"))); + + parseStates.add(new ShiftedState( + states.get(9), + new Literal("number", "0"))); + + parseStates.add(new ShiftedState( + states.get(11), + new Token(";"))); + + + parseStates.add(new ReducedState( + states.get(5), + getRule(4), + Arrays.asList(new ParseState[] { + parseStates.get(2) + }))); + + parseStates.add(new ReducedState( + states.get(7), + getRule(4), + Arrays.asList(new ParseState[] { + parseStates.get(4) + }))); + + parseStates.add(new ReducedState( + states.get(5), + getRule(3), + Arrays.asList(new ParseState[] { + parseStates.get(8) + }))); + + parseStates.add(new ReducedState( + states.get(7), + getRule(4), + Arrays.asList(new ParseState[] { + parseStates.get(10) + }))); + + parseStates.add(new ReducedState( + states.get(5), + getRule(3), + Arrays.asList(new ParseState[] { + parseStates.get(14) + }))); + + parseStates.add(new ReducedState( + states.get(7), + getRule(4), + Arrays.asList(new ParseState[] { + parseStates.get(16) + }))); + + + parseStates.add(new ReducedState( + states.get(10), + getRule(2), + Arrays.asList(new ParseState[] { + parseStates.get(0), + parseStates.get(1), + parseStates.get(18), + parseStates.get(3), + parseStates.get(19), + parseStates.get(5) + }))); + + parseStates.add(new ReducedState( + states.get(2), + getRule(2), + Arrays.asList(new ParseState[] { + parseStates.get(6), + parseStates.get(7), + parseStates.get(20), + parseStates.get(9), + parseStates.get(21), + parseStates.get(11) + }))); + + parseStates.add(new ReducedState( + states.get(2), + getRule(2), + Arrays.asList(new ParseState[] { + parseStates.get(12), + parseStates.get(13), + parseStates.get(22), + parseStates.get(15), + parseStates.get(23), + parseStates.get(17) + }))); + + + parseStates.add(new ReducedState( + states.get(1), + getRule(0), + Arrays.asList(new ParseState[] { + parseStates.get(24) + }))); + + + parseStates.add(new ReducedState( + states.get(1), + getRule(1), + Arrays.asList(new ParseState[] { + parseStates.get(27), + parseStates.get(25) + }))); + + parseStates.add(new ReducedState( + states.get(1), + getRule(1), + Arrays.asList(new ParseState[] { + parseStates.get(28), + parseStates.get(26) + }))); + + return parseStates.get(parseStates.size() - 1); + } + + private ProductionRule getRule(int index) { + return grammar.getRule(index); + } + +} \ No newline at end of file diff --git a/src/test/java/test_aids/test_grammars/BasicSLR1TestGrammar.java b/src/test/java/test_aids/test_grammars/BasicSLR1TestGrammar.java new file mode 100644 index 0000000..3b260da --- /dev/null +++ b/src/test/java/test_aids/test_grammars/BasicSLR1TestGrammar.java @@ -0,0 +1,215 @@ +package test_aids.test_grammars; + +import java.util.*; + +import grammar_objects.*; +import grammars.basic_SLR1.BasicSLR1Grammar; +import syntax_analysis.grammar_structure_creation.*; +import syntax_analysis.parsing.*; +import test_aids.*; + +/** + * S –> AA + * A –> aA + * A –> b + */ +public class BasicSLR1TestGrammar { + + private Grammar grammar = BasicSLR1Grammar.produce(); + private List states; + + public TestGrammar getGrammar() { + TestGrammarBuilder builder = new TestGrammarBuilder(grammar); + + states = gatherStates(builder.extraRootRule); + + return builder.setUp() + .addStates(states) + .selectState(states.get(0)) + .addBranch(new Route(states.get(1), new NonTerminal("S"))) + .addBranch(new Route(states.get(2), new NonTerminal("A"))) + .addBranch(new Route(states.get(4), new Token("a"))) + .addBranch(new Route(states.get(6), new Token("b"))) + .deselectState() + + .selectState(states.get(2)) + .addBranch(new Route(states.get(3), new NonTerminal("A"))) + .addBranch(new Route(states.get(4), new Token("a"))) + .addBranch(new Route(states.get(6), new Token("b"))) + .deselectState() + + .selectState(states.get(4)) + .addBranch(new Route(states.get(5), new NonTerminal("A"))) + .addBranch(new Route(states.get(4), new Token("a"))) + .addBranch(new Route(states.get(6), new Token("b"))) + .deselectState() + + .commitStates() + + .selectState(states.get(0)) + .addAction(new Token("a"), new Shift(states.get(4))) + .addAction(new Token("b"), new Shift(states.get(6))) + .deselectState() + + //Accept EOF at state 1 + .selectState(states.get(1)) + .addAction(new EOF(), new Accept()) + .deselectState() + + .selectState(states.get(2)) + .addAction(new Token("a"), new Shift(states.get(4))) + .addAction(new Token("b"), new Shift(states.get(6))) + .deselectState() + + .selectState(states.get(3)) + .addAction(new EOF(), new Reduction(getRule(0))) + .deselectState() + + .selectState(states.get(4)) + .addAction(new Token("a"), new Shift(states.get(4))) + .addAction(new Token("b"), new Shift(states.get(6))) + .deselectState() + + .selectState(states.get(5)) + .addAction(new Token("a"), new Reduction(getRule(1))) + .addAction(new Token("b"), new Reduction(getRule(1))) + .addAction(new EOF(), new Reduction(getRule(1))) + .deselectState() + + .selectState(states.get(6)) + .addAction(new Token("a"), new Reduction(getRule(2))) + .addAction(new Token("b"), new Reduction(getRule(2))) + .addAction(new EOF(), new Reduction(getRule(2))) + .deselectState() + + .selectState(states.get(0)) + .addGoto(new NonTerminal("S"), states.get(1)) + .addGoto(new NonTerminal("A"), states.get(2)) + .deselectState() + + .selectState(states.get(2)) + .addGoto(new NonTerminal("A"), states.get(3)) + .deselectState() + + .selectState(states.get(4)) + .addGoto(new NonTerminal("A"), states.get(5)) + .deselectState() + + .commitTables() + + .setParseTreeRoot("CompleteSentence", completeSentenceParse()) + + .commitParseTrees() + .commitRuleConvertors() + .commitCodeGenerations() + .generateTestGrammar(); + } + + private List gatherStates(ProductionRule extraRootRule) { + states = new ArrayList(); + + states.add(new State( + Set.of( + new GrammarPosition(extraRootRule, 0), + new GrammarPosition(getRule(0), 0), + new GrammarPosition(getRule(1), 0), + new GrammarPosition(getRule(2), 0) + ), + null + )); + states.add(new State( + Set.of( + new GrammarPosition(extraRootRule, 1) + ), + states.get(0) + )); + states.add(new State( + Set.of( + new GrammarPosition(getRule(0), 1), + new GrammarPosition(getRule(1), 0), + new GrammarPosition(getRule(2), 0) + ), + states.get(0) + )); + states.add(new State( + Set.of( + new GrammarPosition(getRule(0), 2) + ), + states.get(2) + )); + states.add(new State( + Set.of( + new GrammarPosition(getRule(1), 1), + new GrammarPosition(getRule(1), 0), + new GrammarPosition(getRule(2), 0) + ), + states.get(2) + )); + states.add(new State( + Set.of( + new GrammarPosition(getRule(1), 2) + ), + states.get(4) + )); + states.add(new State( + Set.of( + new GrammarPosition(getRule(2), 1) + ), + states.get(4) + )); + + return states; + } + + /** + * Parse tree for the sentence "CompleteSentence" + * @return The root ParseState of the tree + */ + private ParseState completeSentenceParse() { + List parseStates = new ArrayList<>(); + + parseStates.add(new ShiftedState(states.get(6), new Token("b"))); + + + parseStates.add(new ReducedState(states.get(2), getRule(2), Arrays.asList(new ParseState[] { + parseStates.get(0) + }))); + + parseStates.add(new ShiftedState(states.get(4), new Token("a"))); + parseStates.add(new ShiftedState(states.get(4), new Token("a"))); + parseStates.add(new ShiftedState(states.get(4), new Token("a"))); + parseStates.add(new ShiftedState(states.get(6), new Token("b"))); + + + parseStates.add(new ReducedState(states.get(5), getRule(2), Arrays.asList(new ParseState[] { + parseStates.get(5) + }))); + + parseStates.add(new ReducedState(states.get(5), getRule(1), Arrays.asList(new ParseState[] { + parseStates.get(4), + parseStates.get(6) + }))); + + parseStates.add(new ReducedState(states.get(5), getRule(1), Arrays.asList(new ParseState[] { + parseStates.get(3), + parseStates.get(7) + }))); + + parseStates.add(new ReducedState(states.get(3), getRule(1), Arrays.asList(new ParseState[] { + parseStates.get(2), + parseStates.get(8) + }))); + + + parseStates.add(new ReducedState(states.get(1), getRule(0), Arrays.asList(new ParseState[] { + parseStates.get(1), + parseStates.get(9) + }))); + + return parseStates.get(parseStates.size() - 1); + } + + private ProductionRule getRule(int index) { + return grammar.getRule(index); + } +} diff --git a/src/test/java/test_aids/test_grammars/SelfReferentialTestGrammar.java b/src/test/java/test_aids/test_grammars/SelfReferentialTestGrammar.java new file mode 100644 index 0000000..677ea14 --- /dev/null +++ b/src/test/java/test_aids/test_grammars/SelfReferentialTestGrammar.java @@ -0,0 +1,393 @@ +package test_aids.test_grammars; + +import java.util.*; +import java.util.function.Function; +import java.util.stream.Collectors; +import grammar_objects.*; +import grammars.self_referential.SelfReferentialGrammar; +import syntax_analysis.grammar_structure_creation.*; +import test_aids.*; +import test_aids.TestGrammarBuilder.*; +import test_aids.test_grammars.exceptions.UnsupportedGrammarException; + +/** + * H → h A + * A → a L + * L → l L //Self-referential + * L → o + */ +public class SelfReferentialTestGrammar { + + private Grammar grammar = SelfReferentialGrammar.produce(); + private EOF eof; + private ProductionRule extraRootRule; + private List states; + private List allTokens; + + public TestGrammar getGrammar(GrammarType grammarType) { + TestGrammarBuilder builder = new TestGrammarBuilder(grammar); + + eof = builder.endOfFile; + extraRootRule = builder.extraRootRule; + + allTokens = new ArrayList<>(); + allTokens.addAll(grammar.getParts().tokens()); + allTokens.add(eof); + + states = setUpStates(grammarType, builder.extraRootRule); + + TableGatherer tableGatherer = builder.setUp() + .addStates(states) + //Branches are already linked + .commitStates(); + + tableGatherer = setUpActionTable(grammarType, tableGatherer); + + return tableGatherer + .selectState(states.get(0)) + .addGoto(new NonTerminal("H"), states.get(1)) + .deselectState() + + .selectState(states.get(2)) + .addGoto(new NonTerminal("A"), states.get(3)) + .deselectState() + + .selectState(states.get(4)) + .addGoto(new NonTerminal("L"), states.get(5)) + .deselectState() + + .selectState(states.get(6)) + .addGoto(new NonTerminal("L"), states.get(7)) + .deselectState() + + .commitTables() + + .commitParseTrees() + .commitRuleConvertors() + .commitCodeGenerations() + .generateTestGrammar(); + + } + + /** + * Creates states and links necessary branches + * @param grammarType + * @param extraRootRule + * @return + */ + private List setUpStates(GrammarType grammarType, ProductionRule extraRootRule) { + List states = new ArrayList<>(); + + switch (grammarType) { + case LR0, SLR1 -> lr0States(states); + case CLR1 -> clr1States(states); + + default -> throw new UnsupportedGrammarException(grammarType); + } + + return states; + } + + private void lr0States(List states) { + states.add(new State( //0 + Set.of(new GrammarPosition[] { + new GrammarPosition(extraRootRule, 0), + new GrammarPosition(getRule(0), 0) + }), + null + )); + + states.add(new State( //1 + Set.of(new GrammarPosition[] { + new GrammarPosition(extraRootRule, 1), + }), + states.get(0) + )); + + states.add(new State( //2 + Set.of(new GrammarPosition[] { + new GrammarPosition(getRule(0), 1), + new GrammarPosition(getRule(1), 0) + }), + states.get(0) + )); + + states.add(new State( //3 + Set.of(new GrammarPosition[] { + new GrammarPosition(getRule(0), 2), + }), + states.get(2) + )); + + states.add(new State( //4 + Set.of(new GrammarPosition[] { + new GrammarPosition(getRule(1), 1), + new GrammarPosition(getRule(2), 0), + new GrammarPosition(getRule(3), 0) + }), + states.get(2) + )); + + states.add(new State( //5 + Set.of(new GrammarPosition[] { + new GrammarPosition(getRule(1), 2) + }), + states.get(4) + )); + + states.add(new State( //6 + Set.of(new GrammarPosition[] { + new GrammarPosition(getRule(2), 1), + new GrammarPosition(getRule(2), 0), + new GrammarPosition(getRule(3), 0) + }), + states.get(4) + )); + + states.add(new State( //7 + Set.of(new GrammarPosition[] { + new GrammarPosition(getRule(2), 2) + }), + states.get(6) + )); + + states.add(new State( //8 + Set.of(new GrammarPosition[] { + new GrammarPosition(getRule(3), 1) + }), + states.get(6) + )); + + //Tree branches + states.get(0) + .addBranch(new Route(states.get(1), new NonTerminal("H"))) + .addBranch(new Route(states.get(2), new Token("h"))); + + states.get(2) + .addBranch(new Route(states.get(3), new NonTerminal("A"))) + .addBranch(new Route(states.get(4), new Token("a"))); + + states.get(4) + .addBranch(new Route(states.get(5), new NonTerminal("L"))) + .addBranch(new Route(states.get(6), new Token("l"))); + + states.get(6) + .addBranch(new Route(states.get(7), new NonTerminal("L"))) + .addBranch(new Route(states.get(8), new Token("o"))); + + //Graph branches, links to existing states + states.get(4) + .addBranch(new Route(states.get(8), new Token("o"))); + + states.get(6) + .addBranch(new Route(states.get(6), new Token("l"))); + } + + private void clr1States(List states) { + states.add(new State( //0 + Set.of(new CLR1Position[] { + new CLR1Position(extraRootRule, 0, Set.of(new EOF())), + new CLR1Position(getRule(0), 0, Set.of(new EOF())) + }), + null + )); + + states.add(new State( //1 + Set.of(new CLR1Position[] { + new CLR1Position(extraRootRule, 1, Set.of(new EOF())), + }), + states.get(0) + )); + + states.add(new State( //2 + Set.of(new CLR1Position[] { + new CLR1Position(getRule(0), 1, Set.of(new EOF())), + new CLR1Position(getRule(1), 0, Set.of(new EOF())) + }), + states.get(0) + )); + + states.add(new State( //3 + Set.of(new CLR1Position[] { + new CLR1Position(getRule(0), 2, Set.of(new EOF())), + }), + states.get(2) + )); + + states.add(new State( //4 + Set.of(new CLR1Position[] { + new CLR1Position(getRule(1), 1, Set.of(new EOF())), + new CLR1Position(getRule(2), 0, Set.of(new EOF())), + new CLR1Position(getRule(3), 0, Set.of(new EOF())) + }), + states.get(2) + )); + + states.add(new State( //5 + Set.of(new CLR1Position[] { + new CLR1Position(getRule(1), 2, Set.of(new EOF())) + }), + states.get(4) + )); + + states.add(new State( //6 + Set.of(new CLR1Position[] { + new CLR1Position(getRule(2), 1, Set.of(new EOF())), + new CLR1Position(getRule(2), 0, Set.of(new EOF())), + new CLR1Position(getRule(3), 0, Set.of(new EOF())) + }), + states.get(4) + )); + + states.add(new State( //7 + Set.of(new CLR1Position[] { + new CLR1Position(getRule(2), 2, Set.of(new EOF())) + }), + states.get(6) + )); + + states.add(new State( //8 + Set.of(new CLR1Position[] { + new CLR1Position(getRule(3), 1, Set.of(new EOF())) + }), + states.get(6) + )); + + //Tree branches + states.get(0) + .addBranch(new Route(states.get(1), new NonTerminal("H"))) + .addBranch(new Route(states.get(2), new Token("h"))); + + states.get(2) + .addBranch(new Route(states.get(3), new NonTerminal("A"))) + .addBranch(new Route(states.get(4), new Token("a"))); + + states.get(4) + .addBranch(new Route(states.get(5), new NonTerminal("L"))) + .addBranch(new Route(states.get(6), new Token("l"))); + + states.get(6) + .addBranch(new Route(states.get(7), new NonTerminal("L"))) + .addBranch(new Route(states.get(8), new Token("o"))); + + //Graph branches, links to existing states + states.get(4) + .addBranch(new Route(states.get(8), new Token("o"))); + + states.get(6) + .addBranch(new Route(states.get(6), new Token("l"))); + } + + protected TableGatherer setUpActionTable(GrammarType type, TableGatherer tableGatherer) { + switch (type) { + case LR0 -> lr0ActionTable(tableGatherer); + case SLR1 -> slr1ActionTable(tableGatherer); + case CLR1 -> { /* Unimplemented */ } + + default -> throw new UnsupportedGrammarException(type); + } + + return tableGatherer; + } + + private void lr0ActionTable(TableGatherer tableGatherer) { + tableGatherer + .selectState(states.get(0)) + .addAction(new Token("h"), new Shift(states.get(2))) + .deselectState() + + //Accept EOF at state 1 + .selectState(states.get(1)) + .addAction(eof, new Accept()) + .deselectState() + + .selectState(states.get(2)) + .addAction(new Token("a"), new Shift(states.get(4))) + .deselectState() + + .selectState(states.get(3)) + .addActions(allTokens.stream().collect(Collectors.toMap( + Function.identity(), + token -> new Reduction(getRule(0)) + ))) + .deselectState() + + .selectState(states.get(4)) + .addAction(new Token("l"), new Shift(states.get(6))) + .addAction(new Token("o"), new Shift(states.get(8))) + .deselectState() + + .selectState(states.get(5)) + .addActions(allTokens.stream().collect(Collectors.toMap( + Function.identity(), + token -> new Reduction(getRule(1)) + ))) + .deselectState() + + .selectState(states.get(6)) + .addAction(new Token("l"), new Shift(states.get(6))) + .addAction(new Token("o"), new Shift(states.get(8))) + .deselectState() + + .selectState(states.get(7)) + .addActions(allTokens.stream().collect(Collectors.toMap( + Function.identity(), + token -> new Reduction(getRule(2)) + ))) + .deselectState() + + .selectState(states.get(8)) + .addActions(allTokens.stream().collect(Collectors.toMap( + Function.identity(), + token -> new Reduction(getRule(3)) + ))) + .deselectState(); + } + + private void slr1ActionTable(TableGatherer tableGatherer) { + tableGatherer + .selectState(states.get(0)) + .addAction(new Token("h"), new Shift(states.get(2))) + .deselectState() + + //Accept EOF at state 1 + .selectState(states.get(1)) + .addAction(new EOF(), new Accept()) + .deselectState() + + .selectState(states.get(2)) + .addAction(new Token("a"), new Shift(states.get(4))) + .deselectState() + + .selectState(states.get(3)) + .addAction(new EOF(), new Reduction(getRule(0))) + .deselectState() + + .selectState(states.get(4)) + .addAction(new Token("l"), new Shift(states.get(6))) + .addAction(new Token("o"), new Shift(states.get(8))) + .deselectState() + + .selectState(states.get(5)) + .addAction(new EOF(), new Reduction(getRule(1))) + .deselectState() + + .selectState(states.get(6)) + .addAction(new Token("l"), new Shift(states.get(6))) + .addAction(new Token("o"), new Shift(states.get(8))) + .deselectState() + + .selectState(states.get(7)) + .addAction(new EOF(), new Reduction(getRule(2))) + .deselectState() + + .selectState(states.get(8)) + .addAction(new EOF(), new Reduction(getRule(3))) + .deselectState(); + } + + private ProductionRule getRule(int index) { + return grammar.getRule(index); + } + +} diff --git a/src/test/java/test_aids/test_grammars/SmallTestGrammar.java b/src/test/java/test_aids/test_grammars/SmallTestGrammar.java new file mode 100644 index 0000000..67fccd4 --- /dev/null +++ b/src/test/java/test_aids/test_grammars/SmallTestGrammar.java @@ -0,0 +1,494 @@ +package test_aids.test_grammars; + +import java.util.*; +import grammar_objects.*; +import grammars.small_grammar.SmallGrammar; +import grammars.small_grammar.convertors.*; +import syntax_analysis.grammar_structure_creation.*; +import syntax_analysis.grammar_structure_creation.State; +import syntax_analysis.parsing.*; +import test_aids.*; +import test_aids.TestGrammarBuilder.TableGatherer; +import test_aids.test_grammars.exceptions.UnsupportedGrammarException; + +import java.util.function.Function; +import java.util.stream.Collectors; + +/** + * E->E+B + * E->E*B + * E->B + * B->0 + * B->1 + */ +public class SmallTestGrammar { + + private Grammar grammar = SmallGrammar.produce(); + private EOF eof; + private List states; + private List allTokens; + + public TestGrammar getGrammar(GrammarType grammarType) { + TestGrammarBuilder builder = new TestGrammarBuilder(grammar); + + eof = builder.endOfFile; + states = gatherStates(builder.extraRootRule); + + allTokens = new ArrayList<>(); + allTokens.addAll(grammar.getParts().tokens()); + allTokens.add(eof); + + TableGatherer tableGatherer = builder.setUp() + .addStates(states) + + //Tree branches + .selectState(states.get(0)) + .addBranch(new Route(states.get(1), new NonTerminal("E"))) + .addBranch(new Route(states.get(6), new NonTerminal("B"))) + .addBranch(new Route(states.get(7), new Token("0"))) + .addBranch(new Route(states.get(8), new Token("1"))) + .deselectState() + + .selectState(states.get(1)) + .addBranch(new Route(states.get(4), new Token("+"))) + .addBranch(new Route(states.get(2), new Token("*"))) + .deselectState() + + .selectState(states.get(2)) + .addBranch(new Route(states.get(3), new NonTerminal("B"))) + .deselectState() + + .selectState(states.get(4)) + .addBranch(new Route(states.get(5), new NonTerminal("B"))) + .deselectState() + + //Graph branches, links to existing states + .selectState(states.get(2)) + .addBranch(new Route(states.get(7), new Token("0"))) + .addBranch(new Route(states.get(8), new Token("1"))) + .deselectState() + + .selectState(states.get(4)) + .addBranch(new Route(states.get(7), new Token("0"))) + .addBranch(new Route(states.get(8), new Token("1"))) + .deselectState() + + .commitStates(); + + tableGatherer = setUpActionTable(grammarType, tableGatherer); + + return tableGatherer + .selectState(states.get(0)) + .addGoto(new NonTerminal("E"), states.get(1)) + .addGoto(new NonTerminal("B"), states.get(6)) + .deselectState() + + .selectState(states.get(4)) + .addGoto(new NonTerminal("B"), states.get(5)) + .deselectState() + + .selectState(states.get(2)) + .addGoto(new NonTerminal("B"), states.get(3)) + .deselectState() + + .commitTables() + + .setParseTreeRoot("1+0*1", parseTree0(states)) + .setParseTreeRoot("1", parseTree1(states)) + .setParseTreeRoot("emptyReduce", parseTree2(states)) + .setParseTreeRoot("1+0*1MissingReduction", parseTree3(states)) + + .commitParseTrees() + + .setRuleConvertor("Java 1+0*1", OpZtO.produce()) + .setRuleConvertor("Java 1", One.produce()) + .setRuleConvertor("Java emptyReduce", EmptyReduce.produce()) + .setRuleConvertor("Java 1+0*1MissingReduction", OpZtOMissingReduction.produce()) + + .setRuleConvertor("C 1+0*1", COpZtO.produce()) + + .commitRuleConvertors() + + .setCodeGenerations(setUpCodeGenerations()) + + .commitCodeGenerations() + + .generateTestGrammar(); + } + + private ProductionRule getRule(int index) { + return grammar.getRule(index); + } + + private List gatherStates(ProductionRule extraRootRule) { + states = new ArrayList(); + + states.add(new State( + Set.of(new GrammarPosition[] { + new GrammarPosition(extraRootRule, 0), + new GrammarPosition(getRule(1), 0), + new GrammarPosition(getRule(0), 0), + new GrammarPosition(getRule(2), 0), + new GrammarPosition(getRule(3), 0), + new GrammarPosition(getRule(4), 0), + }), + null + )); + + states.add(new State( + Set.of(new GrammarPosition[] { + new GrammarPosition(extraRootRule, 1), + new GrammarPosition(getRule(1), 1), + new GrammarPosition(getRule(0), 1), + }), + states.get(0) + )); + + states.add(new State( + Set.of(new GrammarPosition[] { + new GrammarPosition(getRule(1), 2), + new GrammarPosition(getRule(3), 0), + new GrammarPosition(getRule(4), 0), + }), + states.get(1) + )); + + states.add(new State( + Set.of(new GrammarPosition[] { + new GrammarPosition(getRule(1), 3), + }), + states.get(2) + )); + + states.add(new State( + Set.of(new GrammarPosition[] { + new GrammarPosition(getRule(0), 2), + new GrammarPosition(getRule(3), 0), + new GrammarPosition(getRule(4), 0), + }), + states.get(1) + )); + + states.add(new State( + Set.of(new GrammarPosition[] { + new GrammarPosition(getRule(0), 3), + }), + states.get(4) + )); + + states.add(new State( + Set.of(new GrammarPosition[] { + new GrammarPosition(getRule(2), 1), + }), + states.get(0) + )); + + states.add(new State( + Set.of(new GrammarPosition[] { + new GrammarPosition(getRule(3), 1), + }), + states.get(0) + )); + + states.add(new State( + Set.of(new GrammarPosition[] { + new GrammarPosition(getRule(4), 1), + }), + states.get(0) + )); + + return states; + } + + private TableGatherer setUpActionTable(GrammarType type, TableGatherer tableGatherer) { + switch (type) { + case LR0 -> lr0ActionTable(tableGatherer); + case SLR1 -> slr1ActionTable(tableGatherer); + case CLR1 -> { /* Unimplemented */} + + default -> throw new UnsupportedGrammarException(type); + } + + return tableGatherer; + } + + private void lr0ActionTable(TableGatherer tableGatherer) { + tableGatherer + .selectState(states.get(0)) + .addAction(new Token("0"), new Shift(states.get(7))) + .addAction(new Token("1"), new Shift(states.get(8))) + .deselectState() + + .selectState(states.get(1)) + .addAction(new Token("*"), new Shift(states.get(2))) + .addAction(new Token("+"), new Shift(states.get(4))) + .addAction(eof, new Accept()) + .deselectState() + + .selectState(states.get(2)) + .addAction(new Token("0"), new Shift(states.get(7))) + .addAction(new Token("1"), new Shift(states.get(8))) + .deselectState() + + .selectState(states.get(3)) + .addActions(allTokens.stream().collect(Collectors.toMap( + Function.identity(), + token -> new Reduction(getRule(1)) + ))) + .deselectState() + + .selectState(states.get(4)) + .addAction(new Token("0"), new Shift(states.get(7))) + .addAction(new Token("1"), new Shift(states.get(8))) + .deselectState() + + .selectState(states.get(5)) + .addActions(allTokens.stream().collect(Collectors.toMap( + Function.identity(), + token -> new Reduction(getRule(0)) + ))) + .deselectState() + + .selectState(states.get(6)) + .addActions(allTokens.stream().collect(Collectors.toMap( + Function.identity(), + token -> new Reduction(getRule(2)) + ))) + .deselectState() + + .selectState(states.get(7)) + .addActions(allTokens.stream().collect(Collectors.toMap( + Function.identity(), + token -> new Reduction(getRule(3)) + ))) + .deselectState() + + .selectState(states.get(8)) + .addActions(allTokens.stream().collect(Collectors.toMap( + Function.identity(), + token -> new Reduction(getRule(4)) + ))) + .deselectState(); + } + + private void slr1ActionTable(TableGatherer tableGatherer) { + tableGatherer + .selectState(states.get(0)) + .addAction(new Token("0"), new Shift(states.get(7))) + .addAction(new Token("1"), new Shift(states.get(8))) + .deselectState() + + .selectState(states.get(1)) + .addAction(new Token("*"), new Shift(states.get(2))) + .addAction(new Token("+"), new Shift(states.get(4))) + .addAction(new EOF(), new Accept()) + .deselectState() + + .selectState(states.get(2)) + .addAction(new Token("0"), new Shift(states.get(7))) + .addAction(new Token("1"), new Shift(states.get(8))) + .deselectState() + + .selectState(states.get(3)) + .addAction(new Token("+"), new Reduction(getRule(1))) + .addAction(new Token("*"), new Reduction(getRule(1))) + .addAction(new EOF(), new Reduction(getRule(1))) + .deselectState() + + .selectState(states.get(4)) + .addAction(new Token("0"), new Shift(states.get(7))) + .addAction(new Token("1"), new Shift(states.get(8))) + .deselectState() + + .selectState(states.get(5)) + .addAction(new Token("+"), new Reduction(getRule(0))) + .addAction(new Token("*"), new Reduction(getRule(0))) + .addAction(new EOF(), new Reduction(getRule(0))) + .deselectState() + + .selectState(states.get(6)) + .addAction(new Token("+"), new Reduction(getRule(2))) + .addAction(new Token("*"), new Reduction(getRule(2))) + .addAction(new EOF(), new Reduction(getRule(2))) + .deselectState() + + .selectState(states.get(7)) + .addAction(new Token("+"), new Reduction(getRule(3))) + .addAction(new Token("*"), new Reduction(getRule(3))) + .addAction(new EOF(), new Reduction(getRule(3))) + .deselectState() + + .selectState(states.get(8)) + .addAction(new Token("+"), new Reduction(getRule(4))) + .addAction(new Token("*"), new Reduction(getRule(4))) + .addAction(new EOF(), new Reduction(getRule(4))) + .deselectState(); + } + + /** + * Parse tree for the sentence "1+0*1" + * @return The root ParseState of the tree + */ + private ParseState parseTree0(List states) { + List parseStates = new ArrayList<>(); + + parseStates.add(new ShiftedState(states.get(8), new Token("1"))); + + + parseStates.add(new ReducedState(states.get(6), getRule(4), Arrays.asList(new ParseState[] { + parseStates.get(0) + }))); + parseStates.add(new ShiftedState(states.get(7), new Token("0"))); + + + parseStates.add(new ReducedState(states.get(1), getRule(2), Arrays.asList(new ParseState[] { + parseStates.get(1) + }))); + parseStates.add(new ShiftedState(states.get(4), new Token("+"))); + parseStates.add(new ReducedState(states.get(5), getRule(3), Arrays.asList(new ParseState[] { + parseStates.get(2) + }))); + parseStates.add(new ShiftedState(states.get(8), new Token("1"))); + + + parseStates.add(new ReducedState(states.get(1), getRule(0), Arrays.asList(new ParseState[] { + parseStates.get(3), + parseStates.get(4), + parseStates.get(5) + }))); + parseStates.add(new ShiftedState(states.get(2), new Token("*"))); + parseStates.add(new ReducedState(states.get(3), getRule(4), Arrays.asList(new ParseState[] { + parseStates.get(6) + }))); + + + parseStates.add(new ReducedState(states.get(1), getRule(1), Arrays.asList(new ParseState[] { + parseStates.get(7), + parseStates.get(8), + parseStates.get(9) + }))); + + return parseStates.get(parseStates.size() - 1); + } + + /** + * Parse tree for the sentence "1" + * @return The root ParseState of the tree + */ + private ParseState parseTree1(List states) { + List parseStates = new ArrayList<>(); + + parseStates.add(new ShiftedState(states.get(8), new Token("1"))); + + return parseStates.get(parseStates.size() - 1); + } + + /** + * Parse tree for the sentence "emptyReduce" + * @return The root ParseState of the tree + */ + private ParseState parseTree2(List states) { + List parseStates = new ArrayList<>(); + + parseStates.add(new ReducedState(states.get(1), getRule(1), Arrays.asList(new ParseState[] { + null, + null, + null + }))); + + return parseStates.get(parseStates.size() - 1); + } + + /** + * Parse tree for the sentence "1+0*1MissingReduction" + * @return The root ParseState of the tree + */ + private ParseState parseTree3(List states) { + List parseStates = new ArrayList<>(); + + parseStates.add(new ShiftedState(states.get(8), new Token("1"))); + + + parseStates.add(new ReducedState(states.get(6), getRule(4), Arrays.asList(new ParseState[] { + parseStates.get(0) + }))); + parseStates.add(new ShiftedState(states.get(7), new Token("0"))); + + + parseStates.add(new ReducedState(states.get(1), getRule(2), Arrays.asList(new ParseState[] { + parseStates.get(1) + }))); + parseStates.add(new ShiftedState(states.get(4), new Token("+"))); + parseStates.add(new ReducedState(states.get(5), getRule(3), Arrays.asList(new ParseState[] { + parseStates.get(2) + }))); + parseStates.add(new ShiftedState(states.get(8), new Token("1"))); + + + parseStates.add(new ReducedState(states.get(1), getRule(0), Arrays.asList(new ParseState[] { + parseStates.get(3), + parseStates.get(4), + null //Missing (Reduction) + }))); + parseStates.add(new ShiftedState(states.get(2), new Token("*"))); + parseStates.add(new ReducedState(states.get(3), getRule(4), Arrays.asList(new ParseState[] { + parseStates.get(6) + }))); + + + parseStates.add(new ReducedState(states.get(1), getRule(1), Arrays.asList(new ParseState[] { + parseStates.get(7), + parseStates.get(8), + parseStates.get(9) + }))); + + return parseStates.get(parseStates.size() - 1); + } + + protected Map setUpCodeGenerations() { + Map generations = new HashMap<>(); + + generations.put("Java 1+0*1", + "public class TestGrammar {\n" + + "\tpublic static void main(String[] args) {\n" + + "\t\tSystem.out.println(1 + 0 * 1);\n" + + "\t}\n" + + "}" + ); + + generations.put("Java 1", + "public class TestGrammar {\n" + + "\tpublic static void main(String[] args) {\n" + + "\t\tSystem.out.println(1);\n" + + "\t}\n" + + "}" + ); + + generations.put("Java emptyReduce", + "public class TestGrammar {\n" + + "\tpublic static void main(String[] args) {\n" + + "\t\tSystem.out.println();\n" + + "\t}\n" + + "}" + ); + + generations.put("Java 1+0*1MissingReduction", + "public class TestGrammar {\n" + + "\tpublic static void main(String[] args) {\n" + + "\t\tSystem.out.println(1 + 0 * 1);\n" + + "\t}\n" + + "}" + ); + + generations.put("C 1+0*1", + "#include \n" + + "\n" + + "main()\n" + + "\tprintf(1 + 0 * 1);\n" + + "}" + ); + + return generations; + } + +} diff --git a/src/test/java/test_aids/test_grammars/basic_SLR1/BasicSLR1TestGrammar.java b/src/test/java/test_aids/test_grammars/basic_SLR1/BasicSLR1TestGrammar.java deleted file mode 100644 index a40bb02..0000000 --- a/src/test/java/test_aids/test_grammars/basic_SLR1/BasicSLR1TestGrammar.java +++ /dev/null @@ -1,201 +0,0 @@ -package test_aids.test_grammars.basic_SLR1; - -import java.util.*; - -import grammar_objects.*; -import grammars.basic_SLR1.BasicSLR1Grammar; -import syntax_analysis.grammar_structure_creation.*; -import syntax_analysis.parsing.*; -import test_aids.*; - -/** - * S –> AA - * A –> aA - * A –> b - */ -public class BasicSLR1TestGrammar extends TestGrammar { - - public BasicSLR1TestGrammar(GrammarType type) { - super(type); - } - - @Override - protected Grammar setUpGrammar(GrammarType type) { - return BasicSLR1Grammar.produce(); - } - - @Override - protected void setUpStates(GrammarType type, List states, ProductionRule extraRootRule, Token endOfFile) { - states.add(new State( - Set.of( - new GrammarPosition(extraRootRule, 0), - new GrammarPosition(getRule(0), 0), - new GrammarPosition(getRule(1), 0), - new GrammarPosition(getRule(2), 0) - ), - null - )); - states.add(new State( - Set.of( - new GrammarPosition(extraRootRule, 1) - ), - getState(0) - )); - states.add(new State( - Set.of( - new GrammarPosition(getRule(0), 1), - new GrammarPosition(getRule(1), 0), - new GrammarPosition(getRule(2), 0) - ), - getState(0) - )); - states.add(new State( - Set.of( - new GrammarPosition(getRule(0), 2) - ), - getState(2) - )); - states.add(new State( - Set.of( - new GrammarPosition(getRule(1), 1), - new GrammarPosition(getRule(1), 0), - new GrammarPosition(getRule(2), 0) - ), - getState(2) - )); - states.add(new State( - Set.of( - new GrammarPosition(getRule(1), 2) - ), - getState(4) - )); - states.add(new State( - Set.of( - new GrammarPosition(getRule(2), 1) - ), - getState(4) - )); - - getState(0).addBranch(new Route(getState(1), new NonTerminal("S"))); - getState(0).addBranch(new Route(getState(2), new NonTerminal("A"))); - getState(0).addBranch(new Route(getState(4), new Token("a"))); - getState(0).addBranch(new Route(getState(6), new Token("b"))); - - getState(2).addBranch(new Route(getState(3), new NonTerminal("A"))); - getState(2).addBranch(new Route(getState(4), new Token("a"))); - getState(2).addBranch(new Route(getState(6), new Token("b"))); - - getState(4).addBranch(new Route(getState(5), new NonTerminal("A"))); - getState(4).addBranch(new Route(getState(4), new Token("a"))); - getState(4).addBranch(new Route(getState(6), new Token("b"))); - } - - @Override - protected void setUpActionTable(GrammarType type, Map> actionTable, Token endOfFile) { - Map stateActions = actionTable.get(getState(0)); - stateActions.put(new Token("a"), new Shift(getState(4))); - stateActions.put(new Token("b"), new Shift(getState(6))); - - //Accept EOF at state 1 - stateActions = actionTable.get(getState(1)); - stateActions.put(new EOF(), new Accept()); - - stateActions = actionTable.get(getState(2)); - stateActions.put(new Token("a"), new Shift(getState(4))); - stateActions.put(new Token("b"), new Shift(getState(6))); - - stateActions = actionTable.get(getState(3)); - stateActions.put(new EOF(), new Reduction(getRule(0))); - - stateActions = actionTable.get(getState(4)); - stateActions.put(new Token("a"), new Shift(getState(4))); - stateActions.put(new Token("b"), new Shift(getState(6))); - - stateActions = actionTable.get(getState(5)); - stateActions.put(new Token("a"), new Reduction(getRule(1))); - stateActions.put(new Token("b"), new Reduction(getRule(1))); - stateActions.put(new EOF(), new Reduction(getRule(1))); - - stateActions = actionTable.get(getState(6)); - stateActions.put(new Token("a"), new Reduction(getRule(2))); - stateActions.put(new Token("b"), new Reduction(getRule(2))); - stateActions.put(new EOF(), new Reduction(getRule(2))); - } - - @Override - protected void setUpGotoTable(GrammarType type, Map> gotoTable) { - Map currentGotoActions = gotoTable.get(getState(0)); - currentGotoActions.put(new NonTerminal("S"), getState(1)); - currentGotoActions.put(new NonTerminal("A"), getState(2)); - - currentGotoActions = gotoTable.get(getState(2)); - currentGotoActions.put(new NonTerminal("A"), getState(3)); - - currentGotoActions = gotoTable.get(getState(4)); - currentGotoActions.put(new NonTerminal("A"), getState(5)); - } - - @Override - protected void setUpParseTrees(Map parseRootMap) { - parseRootMap.put("CompleteSentence", () -> completeSentenceParse()); - } - - /** - * Parse tree for the sentence "CompleteSentence" - * @return The root ParseState of the tree - */ - private ParseState completeSentenceParse() { - List parseStates = new ArrayList<>(); - - parseStates.add(new ShiftedState(getState(6), new Token("b"))); - - - parseStates.add(new ReducedState(getState(2), getRule(2), Arrays.asList(new ParseState[] { - parseStates.get(0) - }))); - - parseStates.add(new ShiftedState(getState(4), new Token("a"))); - parseStates.add(new ShiftedState(getState(4), new Token("a"))); - parseStates.add(new ShiftedState(getState(4), new Token("a"))); - parseStates.add(new ShiftedState(getState(6), new Token("b"))); - - - parseStates.add(new ReducedState(getState(5), getRule(2), Arrays.asList(new ParseState[] { - parseStates.get(5) - }))); - - parseStates.add(new ReducedState(getState(5), getRule(1), Arrays.asList(new ParseState[] { - parseStates.get(4), - parseStates.get(6) - }))); - - parseStates.add(new ReducedState(getState(5), getRule(1), Arrays.asList(new ParseState[] { - parseStates.get(3), - parseStates.get(7) - }))); - - parseStates.add(new ReducedState(getState(3), getRule(1), Arrays.asList(new ParseState[] { - parseStates.get(2), - parseStates.get(8) - }))); - - - parseStates.add(new ReducedState(getState(1), getRule(0), Arrays.asList(new ParseState[] { - parseStates.get(1), - parseStates.get(9) - }))); - - return parseStates.get(parseStates.size() - 1); - } - - @Override - protected void setUpRuleConvertors(GrammarType type, Map> ruleConvertorMap) { - // Unimplemented - } - - @Override - protected void setUpCodeGenerations(GrammarType type, Map> codeGenerations) { - // Unimplemented - } - -} diff --git a/src/test/java/test_aids/test_grammars/basic_identifier/BasicIdentifierTestGrammar.java b/src/test/java/test_aids/test_grammars/basic_identifier/BasicIdentifierTestGrammar.java deleted file mode 100644 index 799ab5d..0000000 --- a/src/test/java/test_aids/test_grammars/basic_identifier/BasicIdentifierTestGrammar.java +++ /dev/null @@ -1,516 +0,0 @@ -package test_aids.test_grammars.basic_identifier; - -import java.util.*; - -import grammar_objects.*; -import grammars.basic_identifier.BasicIdentifierGrammar; -import grammars.basic_identifier.convertors.*; -import syntax_analysis.grammar_structure_creation.*; -import syntax_analysis.parsing.*; -import test_aids.*; -import test_aids.test_grammars.*; - -/** - * := | - * - * := identifier = + ; - * := identifier | number - */ -public class BasicIdentifierTestGrammar extends TestGrammar { - - public BasicIdentifierTestGrammar(GrammarType type) { - super(type); - } - - @Override - protected Grammar setUpGrammar(GrammarType type) { - return BasicIdentifierGrammar.produce(); - } - - @Override - protected void setUpStates(GrammarType type, List states, ProductionRule extraRootRule, Token endOfFile) { - states.add(new State( //0 - Set.of(new GrammarPosition[] { - new GrammarPosition(extraRootRule, 0), - new GrammarPosition(getRule(1), 0), - new GrammarPosition(getRule(0), 0), - new GrammarPosition(getRule(2), 0) - }), - null - )); - - states.add(new State( //1 - Set.of(new GrammarPosition[] { - new GrammarPosition(extraRootRule, 1), - new GrammarPosition(getRule(1), 1), - new GrammarPosition(getRule(2), 0) - }), - getState(0) - )); - - states.add(new State( //2 - Set.of(new GrammarPosition[] { - new GrammarPosition(getRule(1), 2), - }), - getState(1) - )); - - states.add(new State( //3 - Set.of(new GrammarPosition[] { - new GrammarPosition(getRule(2), 1), - }), - getState(1) - )); - - states.add(new State( //4 - Set.of(new GrammarPosition[] { - new GrammarPosition(getRule(2), 2), - new GrammarPosition(getRule(3), 0), - new GrammarPosition(getRule(4), 0) - }), - getState(3) - )); - - states.add(new State( //5 - Set.of(new GrammarPosition[] { - new GrammarPosition(getRule(2), 3) - }), - getState(4) - )); - - states.add(new State( //6 - Set.of(new GrammarPosition[] { - new GrammarPosition(getRule(2), 4), - new GrammarPosition(getRule(3), 0), - new GrammarPosition(getRule(4), 0) - }), - getState(5) - )); - - states.add(new State( //7 - Set.of(new GrammarPosition[] { - new GrammarPosition(getRule(2), 5) - }), - getState(6) - )); - - states.add(new State( //8 - Set.of(new GrammarPosition[] { - new GrammarPosition(getRule(3), 1) - }), - getState(6) - )); - - states.add(new State( //9 - Set.of(new GrammarPosition[] { - new GrammarPosition(getRule(4), 1) - }), - getState(6) - )); - - states.add(new State( //10 - Set.of(new GrammarPosition[] { - new GrammarPosition(getRule(0), 1) - }), - getState(0) - )); - - states.add(new State( //11 - Set.of(new GrammarPosition[] { - new GrammarPosition(getRule(2), 6) - }), - getState(7) - )); - - //Branches - getState(0) - .addBranch(new Route(getState(1), new NonTerminal("statement list"))) - .addBranch(new Route(getState(10), new NonTerminal("statement"))) - .addBranch(new Route(getState(3), new Identifier("identifier"))); - - getState(1) - .addBranch(new Route(getState(2), new NonTerminal("statement"))) - .addBranch(new Route(getState(3), new Identifier("identifier"))); - - getState(3) - .addBranch(new Route(getState(4), new Token("="))); - - getState(4) - .addBranch(new Route(getState(5), new NonTerminal("element"))) - .addBranch(new Route(getState(8), new Identifier("identifier"))) - .addBranch(new Route(getState(9), new Literal("number"))); - - getState(5) - .addBranch(new Route(getState(6), new Token("+"))); - - getState(6) - .addBranch(new Route(getState(7), new NonTerminal("element"))) - .addBranch(new Route(getState(8), new Identifier("identifier"))) - .addBranch(new Route(getState(9), new Literal("number"))); - - getState(7) - .addBranch(new Route(getState(11), new Token(";"))); - } - - @Override - protected void setUpActionTable(GrammarType type, Map> actionTable, Token endOfFile) { - switch (type) { - case LR0 -> lr0ActionTable(type, actionTable, endOfFile); - case SLR1 -> slr1ActionTable(type, actionTable, endOfFile); - - default -> throw new UnsupportedGrammarException(type); - } - } - - private void lr0ActionTable(GrammarType type, Map> actionTable, Token endOfFile) { - List allTokens = new ArrayList<>(); - allTokens.addAll(grammar.getParts().tokens()); - allTokens.add(endOfFile); - - Map stateActions = actionTable.get(getState(0)); - stateActions.put(new Identifier("identifier"), new Shift(getState(3))); - - stateActions = actionTable.get(getState(1)); - stateActions.put(new Identifier("identifier"), new Shift(getState(3))); - stateActions.put(endOfFile, new Accept()); - - stateActions = actionTable.get(getState(2)); - for(Token token : allTokens) { - stateActions.put(token, new Reduction(getRule(1))); - } - - stateActions = actionTable.get(getState(3)); - stateActions.put(new Token("="), new Shift(getState(4))); - - stateActions = actionTable.get(getState(4)); - stateActions.put(new Identifier("identifier"), new Shift(getState(8))); - stateActions.put(new Literal("number"), new Shift(getState(9))); - - stateActions = actionTable.get(getState(5)); - stateActions.put(new Token("+"), new Shift(getState(6))); - - stateActions = actionTable.get(getState(6)); - stateActions.put(new Identifier("identifier"), new Shift(getState(8))); - stateActions.put(new Literal("number"), new Shift(getState(9))); - - stateActions = actionTable.get(getState(7)); - stateActions.put(new Token(";"),new Shift( getState(11))); - - stateActions = actionTable.get(getState(8)); - for(Token token : allTokens) { - stateActions.put(token, new Reduction(getRule(3))); - } - - stateActions = actionTable.get(getState(9)); - for(Token token : allTokens) { - stateActions.put(token, new Reduction(getRule(4))); - } - - stateActions = actionTable.get(getState(10)); - for(Token token : allTokens) { - stateActions.put(token, new Reduction(getRule(0))); - } - - stateActions = actionTable.get(getState(11)); - for(Token token : allTokens) { - stateActions.put(token, new Reduction(getRule(2))); - } - } - - private void slr1ActionTable(GrammarType type, Map> actionTable, Token endOfFile) { - Map stateActions = actionTable.get(getState(0)); - stateActions.put(new Identifier("identifier"), new Shift(getState(3))); - - stateActions = actionTable.get(getState(1)); - stateActions.put(new Identifier("identifier"), new Shift(getState(3))); - stateActions.put(endOfFile, new Accept()); - - stateActions = actionTable.get(getState(2)); - stateActions.put(new Identifier("identifier"), new Reduction(getRule(1))); - stateActions.put(endOfFile, new Reduction(getRule(1))); - - stateActions = actionTable.get(getState(3)); - stateActions.put(new Token("="), new Shift(getState(4))); - - stateActions = actionTable.get(getState(4)); - stateActions.put(new Identifier("identifier"), new Shift(getState(8))); - stateActions.put(new Literal("number"), new Shift(getState(9))); - - stateActions = actionTable.get(getState(5)); - stateActions.put(new Token("+"), new Shift(getState(6))); - - stateActions = actionTable.get(getState(6)); - stateActions.put(new Identifier("identifier"), new Shift(getState(8))); - stateActions.put(new Literal("number"), new Shift(getState(9))); - - stateActions = actionTable.get(getState(7)); - stateActions.put(new Token(";"),new Shift( getState(11))); - - stateActions = actionTable.get(getState(8)); - stateActions.put(new Token("+"), new Reduction(getRule(3))); - stateActions.put(new Token(";"), new Reduction(getRule(3))); - - stateActions = actionTable.get(getState(9)); - stateActions.put(new Token("+"), new Reduction(getRule(4))); - stateActions.put(new Token(";"), new Reduction(getRule(4))); - - stateActions = actionTable.get(getState(10)); - stateActions.put(new Identifier("identifier"), new Reduction(getRule(0))); - stateActions.put(endOfFile, new Reduction(getRule(0))); - - stateActions = actionTable.get(getState(11)); - stateActions.put(new Identifier("identifier"), new Reduction(getRule(2))); - stateActions.put(endOfFile, new Reduction(getRule(2))); - } - - @Override - protected void setUpGotoTable(GrammarType type, Map> gotoTable) { - Map currentGotoActions = new HashMap<>(); - - currentGotoActions.put(new NonTerminal("statement list"), getState(1)); - currentGotoActions.put(new NonTerminal("statement"), getState(10)); - gotoTable.put(getState(0), new HashMap<>(currentGotoActions)); - currentGotoActions.clear(); - - currentGotoActions.put(new NonTerminal("statement"), getState(2)); - gotoTable.put(getState(1), new HashMap<>(currentGotoActions)); - currentGotoActions.clear(); - - currentGotoActions.put(new NonTerminal("element"), getState(5)); - gotoTable.put(getState(4), new HashMap<>(currentGotoActions)); - currentGotoActions.clear(); - - currentGotoActions.put(new NonTerminal("element"), getState(7)); - gotoTable.put(getState(6), new HashMap<>(currentGotoActions)); - currentGotoActions.clear(); - } - - @Override - protected void setUpParseTrees(Map parseRootMap) { - parseRootMap.put("XToYToX", () -> parseTree0()); - parseRootMap.put("XToYToXSemantic", () -> parseTree0()); - } - - /** - * Parse tree for the sentence "XToYToX" - * @return The root ParseState of the tree - */ - private ParseState parseTree0() { - List parseStates = new ArrayList<>(); - - parseStates.add(new ShiftedState( - getState(3), - new Identifier("identifier", "x"))); - - parseStates.add(new ShiftedState( - getState(4), - new Token("="))); - - parseStates.add(new ShiftedState( - getState(9), - new Literal("number", "1"))); - - parseStates.add(new ShiftedState( - getState(6), - new Token("+"))); - - parseStates.add(new ShiftedState( - getState(9), - new Literal("number", "2"))); - - parseStates.add(new ShiftedState( - getState(11), - new Token(";"))); - - parseStates.add(new ShiftedState( - getState(3), - new Identifier("identifier", "y"))); - - parseStates.add(new ShiftedState( - getState(4), - new Token("="))); - - parseStates.add(new ShiftedState( - getState(8), - new Identifier("identifier", "x"))); - - parseStates.add(new ShiftedState( - getState(6), - new Token("+"))); - - parseStates.add(new ShiftedState( - getState(9), - new Literal("number", "3"))); - - parseStates.add(new ShiftedState( - getState(11), - new Token(";"))); - - parseStates.add(new ShiftedState( - getState(3), - new Identifier("identifier", "x"))); - - parseStates.add(new ShiftedState( - getState(4), - new Token("="))); - - parseStates.add(new ShiftedState( - getState(8), - new Identifier("identifier", "y"))); - - parseStates.add(new ShiftedState( - getState(6), - new Token("+"))); - - parseStates.add(new ShiftedState( - getState(9), - new Literal("number", "0"))); - - parseStates.add(new ShiftedState( - getState(11), - new Token(";"))); - - - parseStates.add(new ReducedState( - getState(5), - getRule(4), - Arrays.asList(new ParseState[] { - parseStates.get(2) - }))); - - parseStates.add(new ReducedState( - getState(7), - getRule(4), - Arrays.asList(new ParseState[] { - parseStates.get(4) - }))); - - parseStates.add(new ReducedState( - getState(5), - getRule(3), - Arrays.asList(new ParseState[] { - parseStates.get(8) - }))); - - parseStates.add(new ReducedState( - getState(7), - getRule(4), - Arrays.asList(new ParseState[] { - parseStates.get(10) - }))); - - parseStates.add(new ReducedState( - getState(5), - getRule(3), - Arrays.asList(new ParseState[] { - parseStates.get(14) - }))); - - parseStates.add(new ReducedState( - getState(7), - getRule(4), - Arrays.asList(new ParseState[] { - parseStates.get(16) - }))); - - - parseStates.add(new ReducedState( - getState(10), - getRule(2), - Arrays.asList(new ParseState[] { - parseStates.get(0), - parseStates.get(1), - parseStates.get(18), - parseStates.get(3), - parseStates.get(19), - parseStates.get(5) - }))); - - parseStates.add(new ReducedState( - getState(2), - getRule(2), - Arrays.asList(new ParseState[] { - parseStates.get(6), - parseStates.get(7), - parseStates.get(20), - parseStates.get(9), - parseStates.get(21), - parseStates.get(11) - }))); - - parseStates.add(new ReducedState( - getState(2), - getRule(2), - Arrays.asList(new ParseState[] { - parseStates.get(12), - parseStates.get(13), - parseStates.get(22), - parseStates.get(15), - parseStates.get(23), - parseStates.get(17) - }))); - - - parseStates.add(new ReducedState( - getState(1), - getRule(0), - Arrays.asList(new ParseState[] { - parseStates.get(24) - }))); - - - parseStates.add(new ReducedState( - getState(1), - getRule(1), - Arrays.asList(new ParseState[] { - parseStates.get(27), - parseStates.get(25) - }))); - - parseStates.add(new ReducedState( - getState(1), - getRule(1), - Arrays.asList(new ParseState[] { - parseStates.get(28), - parseStates.get(26) - }))); - - return parseStates.get(parseStates.size() - 1); - } - - @Override - protected void setUpRuleConvertors(GrammarType type, Map> ruleConvertorMap) { - ruleConvertorMap.put("Java", new HashMap<>()); - - ruleConvertorMap.get("Java").put("XToYToX", XToYToX.produce()); - ruleConvertorMap.get("Java").put("XToYToXSemantic", XToYToXSemantic.produce()); - } - - @Override - protected void setUpCodeGenerations(GrammarType type, Map> codeGenerations) { - codeGenerations.put("Java", new HashMap<>()); - - codeGenerations.get("Java").put("XToYToX", - "public class TestGrammar {\n" + - "\tpublic static void main(String[] args) {\n" + - "\t\tx = 1 + 2;\n" + - "\t\ty = x + 3;\n" + - "\t\tx = y + 0;\n" + - "\t\tSystem.out.println(x);\n" + - "\t}\n" + - "}" - ); - - codeGenerations.get("Java").put("XToYToXSemantic", - "public class TestGrammar {\n" + - "\tpublic static void main(String[] args) {\n" + - "\t\tint x = 1 + 2;\n" + - "\t\tint y = x + 3;\n" + - "\t\tx = y + 0;\n" + - "\t\tSystem.out.println(x);\n" + - "\t}\n" + - "}" - ); - } - -} \ No newline at end of file diff --git a/src/test/java/test_aids/test_grammars/UnsupportedGrammarException.java b/src/test/java/test_aids/test_grammars/exceptions/UnsupportedGrammarException.java similarity index 84% rename from src/test/java/test_aids/test_grammars/UnsupportedGrammarException.java rename to src/test/java/test_aids/test_grammars/exceptions/UnsupportedGrammarException.java index 8eaa0c3..ed41ea8 100644 --- a/src/test/java/test_aids/test_grammars/UnsupportedGrammarException.java +++ b/src/test/java/test_aids/test_grammars/exceptions/UnsupportedGrammarException.java @@ -1,4 +1,4 @@ -package test_aids.test_grammars; +package test_aids.test_grammars.exceptions; import test_aids.GrammarType; diff --git a/src/test/java/test_aids/test_grammars/UnsupportedSentenceException.java b/src/test/java/test_aids/test_grammars/exceptions/UnsupportedSentenceException.java similarity index 86% rename from src/test/java/test_aids/test_grammars/UnsupportedSentenceException.java rename to src/test/java/test_aids/test_grammars/exceptions/UnsupportedSentenceException.java index 0b5582e..b907063 100644 --- a/src/test/java/test_aids/test_grammars/UnsupportedSentenceException.java +++ b/src/test/java/test_aids/test_grammars/exceptions/UnsupportedSentenceException.java @@ -1,4 +1,4 @@ -package test_aids.test_grammars; +package test_aids.test_grammars.exceptions; public class UnsupportedSentenceException extends RuntimeException { diff --git a/src/test/java/test_aids/test_grammars/self_referential/SelfReferentialTestGrammar.java b/src/test/java/test_aids/test_grammars/self_referential/SelfReferentialTestGrammar.java deleted file mode 100644 index 0858295..0000000 --- a/src/test/java/test_aids/test_grammars/self_referential/SelfReferentialTestGrammar.java +++ /dev/null @@ -1,352 +0,0 @@ -package test_aids.test_grammars.self_referential; - -import java.util.*; - -import grammar_objects.*; -import grammars.self_referential.SelfReferentialGrammar; -import syntax_analysis.grammar_structure_creation.*; -import test_aids.*; -import test_aids.test_grammars.UnsupportedGrammarException; - -/** - * H → h A - * A → a L - * L → l L //Self-referential - * L → o - */ -public class SelfReferentialTestGrammar extends TestGrammar { - - public SelfReferentialTestGrammar(GrammarType type) { - super(type); - } - - @Override - protected Grammar setUpGrammar(GrammarType type) { - return SelfReferentialGrammar.produce(); - } - - @Override - protected void setUpStates(GrammarType type, List states, ProductionRule extraRootRule, Token endOfFile) { - switch (type) { - case LR0 -> lr0States(type, states, extraRootRule, endOfFile); - case SLR1 -> lr0States(type, states, extraRootRule, endOfFile); - case CLR1 -> clr1States(type, states, extraRootRule, endOfFile); - - default -> throw new UnsupportedGrammarException(type); - } - } - - private void lr0States(GrammarType type, List states, ProductionRule extraRootRule, Token endOfFile) { - states.add(new State( //0 - Set.of(new GrammarPosition[] { - new GrammarPosition(extraRootRule, 0), - new GrammarPosition(getRule(0), 0) - }), - null - )); - - states.add(new State( //1 - Set.of(new GrammarPosition[] { - new GrammarPosition(extraRootRule, 1), - }), - getState(0) - )); - - states.add(new State( //2 - Set.of(new GrammarPosition[] { - new GrammarPosition(getRule(0), 1), - new GrammarPosition(getRule(1), 0) - }), - getState(0) - )); - - states.add(new State( //3 - Set.of(new GrammarPosition[] { - new GrammarPosition(getRule(0), 2), - }), - getState(2) - )); - - states.add(new State( //4 - Set.of(new GrammarPosition[] { - new GrammarPosition(getRule(1), 1), - new GrammarPosition(getRule(2), 0), - new GrammarPosition(getRule(3), 0) - }), - getState(2) - )); - - states.add(new State( //5 - Set.of(new GrammarPosition[] { - new GrammarPosition(getRule(1), 2) - }), - getState(4) - )); - - states.add(new State( //6 - Set.of(new GrammarPosition[] { - new GrammarPosition(getRule(2), 1), - new GrammarPosition(getRule(2), 0), - new GrammarPosition(getRule(3), 0) - }), - getState(4) - )); - - states.add(new State( //7 - Set.of(new GrammarPosition[] { - new GrammarPosition(getRule(2), 2) - }), - getState(6) - )); - - states.add(new State( //8 - Set.of(new GrammarPosition[] { - new GrammarPosition(getRule(3), 1) - }), - getState(6) - )); - - //Tree branches - getState(0) - .addBranch(new Route(getState(1), new NonTerminal("H"))) - .addBranch(new Route(getState(2), new Token("h"))); - - getState(2) - .addBranch(new Route(getState(3), new NonTerminal("A"))) - .addBranch(new Route(getState(4), new Token("a"))); - - getState(4) - .addBranch(new Route(getState(5), new NonTerminal("L"))) - .addBranch(new Route(getState(6), new Token("l"))); - - getState(6) - .addBranch(new Route(getState(7), new NonTerminal("L"))) - .addBranch(new Route(getState(8), new Token("o"))); - - //Graph branches, links to existing states - getState(4) - .addBranch(new Route(getState(8), new Token("o"))); - - getState(6) - .addBranch(new Route(getState(6), new Token("l"))); - } - - private void clr1States(GrammarType type, List states, ProductionRule extraRootRule, Token endOfFile) { - states.add(new State( //0 - Set.of(new CLR1Position[] { - new CLR1Position(extraRootRule, 0, Set.of(new EOF())), - new CLR1Position(getRule(0), 0, Set.of(new EOF())) - }), - null - )); - - states.add(new State( //1 - Set.of(new CLR1Position[] { - new CLR1Position(extraRootRule, 1, Set.of(new EOF())), - }), - getState(0) - )); - - states.add(new State( //2 - Set.of(new CLR1Position[] { - new CLR1Position(getRule(0), 1, Set.of(new EOF())), - new CLR1Position(getRule(1), 0, Set.of(new EOF())) - }), - getState(0) - )); - - states.add(new State( //3 - Set.of(new CLR1Position[] { - new CLR1Position(getRule(0), 2, Set.of(new EOF())), - }), - getState(2) - )); - - states.add(new State( //4 - Set.of(new CLR1Position[] { - new CLR1Position(getRule(1), 1, Set.of(new EOF())), - new CLR1Position(getRule(2), 0, Set.of(new EOF())), - new CLR1Position(getRule(3), 0, Set.of(new EOF())) - }), - getState(2) - )); - - states.add(new State( //5 - Set.of(new CLR1Position[] { - new CLR1Position(getRule(1), 2, Set.of(new EOF())) - }), - getState(4) - )); - - states.add(new State( //6 - Set.of(new CLR1Position[] { - new CLR1Position(getRule(2), 1, Set.of(new EOF())), - new CLR1Position(getRule(2), 0, Set.of(new EOF())), - new CLR1Position(getRule(3), 0, Set.of(new EOF())) - }), - getState(4) - )); - - states.add(new State( //7 - Set.of(new CLR1Position[] { - new CLR1Position(getRule(2), 2, Set.of(new EOF())) - }), - getState(6) - )); - - states.add(new State( //8 - Set.of(new CLR1Position[] { - new CLR1Position(getRule(3), 1, Set.of(new EOF())) - }), - getState(6) - )); - - //Tree branches - getState(0) - .addBranch(new Route(getState(1), new NonTerminal("H"))) - .addBranch(new Route(getState(2), new Token("h"))); - - getState(2) - .addBranch(new Route(getState(3), new NonTerminal("A"))) - .addBranch(new Route(getState(4), new Token("a"))); - - getState(4) - .addBranch(new Route(getState(5), new NonTerminal("L"))) - .addBranch(new Route(getState(6), new Token("l"))); - - getState(6) - .addBranch(new Route(getState(7), new NonTerminal("L"))) - .addBranch(new Route(getState(8), new Token("o"))); - - //Graph branches, links to existing states - getState(4) - .addBranch(new Route(getState(8), new Token("o"))); - - getState(6) - .addBranch(new Route(getState(6), new Token("l"))); - } - - @Override - protected void setUpActionTable(GrammarType type, Map> actionTable, Token endOfFile) { - switch (type) { - case LR0 -> lr0ActionTable(type, actionTable, endOfFile); - case SLR1 -> slr1ActionTable(type, actionTable, endOfFile); - case CLR1 -> { /* Unimplemented */ } - - default -> throw new UnsupportedGrammarException(type); - } - } - - private void lr0ActionTable(GrammarType type, Map> actionTable, Token endOfFile) { - List allTokens = new ArrayList<>(); - allTokens.addAll(grammar.getParts().tokens()); - allTokens.add(endOfFile); - - Map stateActions = actionTable.get(getState(0)); - stateActions.put(new Token("h"), new Shift(getState(2))); - - //Accept EOF at state 1 - stateActions = actionTable.get(getState(1)); - stateActions.put(endOfFile, new Accept()); - - stateActions = actionTable.get(getState(2)); - stateActions.put(new Token("a"), new Shift(getState(4))); - - stateActions = actionTable.get(getState(3)); - for(Token token : allTokens) { - stateActions.put(token, new Reduction(getRule(0))); - } - - stateActions = actionTable.get(getState(4)); - stateActions.put(new Token("l"), new Shift(getState(6))); - stateActions.put(new Token("o"), new Shift(getState(8))); - - stateActions = actionTable.get(getState(5)); - for(Token token : allTokens) { - stateActions.put(token, new Reduction(getRule(1))); - } - - stateActions = actionTable.get(getState(6)); - stateActions.put(new Token("l"), new Shift(getState(6))); - stateActions.put(new Token("o"), new Shift(getState(8))); - - stateActions = actionTable.get(getState(7)); - for(Token token : allTokens) { - stateActions.put(token, new Reduction(getRule(2))); - } - - stateActions = actionTable.get(getState(8)); - for(Token token : allTokens) { - stateActions.put(token, new Reduction(getRule(3))); - } - } - - private void slr1ActionTable(GrammarType type, Map> actionTable, Token endOfFile) { - Map stateActions = actionTable.get(getState(0)); - stateActions.put(new Token("h"), new Shift(getState(2))); - - //Accept EOF at state 1 - stateActions = actionTable.get(getState(1)); - stateActions.put(new EOF(), new Accept()); - - stateActions = actionTable.get(getState(2)); - stateActions.put(new Token("a"), new Shift(getState(4))); - - stateActions = actionTable.get(getState(3)); - stateActions.put(new EOF(), new Reduction(getRule(0))); - - stateActions = actionTable.get(getState(4)); - stateActions.put(new Token("l"), new Shift(getState(6))); - stateActions.put(new Token("o"), new Shift(getState(8))); - - stateActions = actionTable.get(getState(5)); - stateActions.put(new EOF(), new Reduction(getRule(1))); - - stateActions = actionTable.get(getState(6)); - stateActions.put(new Token("l"), new Shift(getState(6))); - stateActions.put(new Token("o"), new Shift(getState(8))); - - stateActions = actionTable.get(getState(7)); - stateActions.put(new EOF(), new Reduction(getRule(2))); - - stateActions = actionTable.get(getState(8)); - stateActions.put(new EOF(), new Reduction(getRule(3))); - } - - @Override - protected void setUpGotoTable(GrammarType type, Map> gotoTable) { - Map currentGotoActions = new HashMap<>(); - - currentGotoActions.put(new NonTerminal("H"), getState(1)); - gotoTable.put(getState(0), new HashMap<>(currentGotoActions)); - currentGotoActions.clear(); - - currentGotoActions.put(new NonTerminal("A"), getState(3)); - gotoTable.put(getState(2), new HashMap<>(currentGotoActions)); - currentGotoActions.clear(); - - currentGotoActions.put(new NonTerminal("L"), getState(5)); - gotoTable.put(getState(4), new HashMap<>(currentGotoActions)); - currentGotoActions.clear(); - - currentGotoActions.put(new NonTerminal("L"), getState(7)); - gotoTable.put(getState(6), new HashMap<>(currentGotoActions)); - currentGotoActions.clear(); - } - - @Override - protected void setUpParseTrees(Map parseRootMap) { - // Unimplemented - } - - @Override - protected void setUpRuleConvertors(GrammarType type, Map> ruleConvertorMap) { - // Unimplemented - } - - @Override - protected void setUpCodeGenerations(GrammarType type, Map> codeGenerations) { - // Unimplemented - } - -} diff --git a/src/test/java/test_aids/test_grammars/small_grammar/SmallTestGrammar.java b/src/test/java/test_aids/test_grammars/small_grammar/SmallTestGrammar.java deleted file mode 100644 index 66b24f7..0000000 --- a/src/test/java/test_aids/test_grammars/small_grammar/SmallTestGrammar.java +++ /dev/null @@ -1,441 +0,0 @@ -package test_aids.test_grammars.small_grammar; - -import java.util.*; - -import grammar_objects.*; -import grammars.small_grammar.SmallGrammar; -import grammars.small_grammar.convertors.*; -import syntax_analysis.grammar_structure_creation.*; -import syntax_analysis.parsing.*; -import test_aids.*; -import test_aids.test_grammars.*; - -/** - * E->E+B - * E->E*B - * E->B - * B->0 - * B->1 - */ -public class SmallTestGrammar extends TestGrammar { - - public SmallTestGrammar(GrammarType type) { - super(type); - } - - @Override - protected Grammar setUpGrammar(GrammarType type) { - return SmallGrammar.produce(); - } - - @Override - protected void setUpStates(GrammarType type, List states, ProductionRule extraRootRule, Token endOfFile) { - states.add(new State( - Set.of(new GrammarPosition[] { - new GrammarPosition(extraRootRule, 0), - new GrammarPosition(getRule(1), 0), - new GrammarPosition(getRule(0), 0), - new GrammarPosition(getRule(2), 0), - new GrammarPosition(getRule(3), 0), - new GrammarPosition(getRule(4), 0), - }), - null - )); - - states.add(new State( - Set.of(new GrammarPosition[] { - new GrammarPosition(extraRootRule, 1), - new GrammarPosition(getRule(1), 1), - new GrammarPosition(getRule(0), 1), - }), - getState(0) - )); - - states.add(new State( - Set.of(new GrammarPosition[] { - new GrammarPosition(getRule(1), 2), - new GrammarPosition(getRule(3), 0), - new GrammarPosition(getRule(4), 0), - }), - getState(1) - )); - - states.add(new State( - Set.of(new GrammarPosition[] { - new GrammarPosition(getRule(1), 3), - }), - getState(2) - )); - - states.add(new State( - Set.of(new GrammarPosition[] { - new GrammarPosition(getRule(0), 2), - new GrammarPosition(getRule(3), 0), - new GrammarPosition(getRule(4), 0), - }), - getState(1) - )); - - states.add(new State( - Set.of(new GrammarPosition[] { - new GrammarPosition(getRule(0), 3), - }), - getState(4) - )); - - states.add(new State( - Set.of(new GrammarPosition[] { - new GrammarPosition(getRule(2), 1), - }), - getState(0) - )); - - states.add(new State( - Set.of(new GrammarPosition[] { - new GrammarPosition(getRule(3), 1), - }), - getState(0) - )); - - states.add(new State( - Set.of(new GrammarPosition[] { - new GrammarPosition(getRule(4), 1), - }), - getState(0) - )); - - //Tree branches - getState(0) - .addBranch(new Route(getState(1), new NonTerminal("E"))) - .addBranch(new Route(getState(6), new NonTerminal("B"))) - .addBranch(new Route(getState(7), new Token("0"))) - .addBranch(new Route(getState(8), new Token("1"))); - - getState(1) - .addBranch(new Route(getState(4), new Token("+"))) - .addBranch(new Route(getState(2), new Token("*"))); - - getState(2) - .addBranch(new Route(getState(3), new NonTerminal("B"))); - - getState(4) - .addBranch(new Route(getState(5), new NonTerminal("B"))); - - //Graph branches, links to existing states - getState(2) - .addBranch(new Route(getState(7), new Token("0"))) - .addBranch(new Route(getState(8), new Token("1"))); - - getState(4) - .addBranch(new Route(getState(7), new Token("0"))) - .addBranch(new Route(getState(8), new Token("1"))); - } - - @Override - protected void setUpActionTable(GrammarType type, Map> actionTable, Token endOfFile) { - switch (type) { - case LR0 -> lr0ActionTable(type, actionTable, endOfFile); - case SLR1 -> slr1ActionTable(type, actionTable, endOfFile); - case CLR1 -> { /* Unimplemented */ } - - default -> throw new UnsupportedGrammarException(type); - } - } - - private void lr0ActionTable(GrammarType type, Map> actionTable, Token endOfFile) { - List allTokens = new ArrayList<>(); - allTokens.addAll(grammar.getParts().tokens()); - allTokens.add(endOfFile); - - Map stateActions = actionTable.get(getState(0)); - stateActions.put(new Token("0"), new Shift(getState(7))); - stateActions.put(new Token("1"), new Shift(getState(8))); - - stateActions = actionTable.get(getState(1)); - stateActions.put(new Token("*"), new Shift(getState(2))); - stateActions.put(new Token("+"), new Shift(getState(4))); - stateActions.put(endOfFile, new Accept()); - - stateActions = actionTable.get(getState(2)); - stateActions.put(new Token("0"), new Shift(getState(7))); - stateActions.put(new Token("1"), new Shift(getState(8))); - - stateActions = actionTable.get(getState(3)); - for(Token token : allTokens) { - stateActions.put(token, new Reduction(getRule(1))); - } - - stateActions = actionTable.get(getState(4)); - stateActions.put(new Token("0"), new Shift(getState(7))); - stateActions.put(new Token("1"), new Shift(getState(8))); - - stateActions = actionTable.get(getState(5)); - for(Token token : allTokens) { - stateActions.put(token, new Reduction(getRule(0))); - } - - stateActions = actionTable.get(getState(6)); - for(Token token : allTokens) { - stateActions.put(token, new Reduction(getRule(2))); - } - - stateActions = actionTable.get(getState(7)); - for(Token token : allTokens) { - stateActions.put(token, new Reduction(getRule(3))); - } - - stateActions = actionTable.get(getState(8)); - for(Token token : allTokens) { - stateActions.put(token, new Reduction(getRule(4))); - } - } - - private void slr1ActionTable(GrammarType type, Map> actionTable, Token endOfFile) { - Map stateActions = actionTable.get(getState(0)); - stateActions.put(new Token("0"), new Shift(getState(7))); - stateActions.put(new Token("1"), new Shift(getState(8))); - - stateActions = actionTable.get(getState(1)); - stateActions.put(new Token("*"), new Shift(getState(2))); - stateActions.put(new Token("+"), new Shift(getState(4))); - stateActions.put(new EOF(), new Accept()); - - stateActions = actionTable.get(getState(2)); - stateActions.put(new Token("0"), new Shift(getState(7))); - stateActions.put(new Token("1"), new Shift(getState(8))); - - stateActions = actionTable.get(getState(3)); - stateActions.put(new Token("+"), new Reduction(getRule(1))); - stateActions.put(new Token("*"), new Reduction(getRule(1))); - stateActions.put(new EOF(), new Reduction(getRule(1))); - - stateActions = actionTable.get(getState(4)); - stateActions.put(new Token("0"), new Shift(getState(7))); - stateActions.put(new Token("1"), new Shift(getState(8))); - - stateActions = actionTable.get(getState(5)); - stateActions.put(new Token("+"), new Reduction(getRule(0))); - stateActions.put(new Token("*"), new Reduction(getRule(0))); - stateActions.put(new EOF(), new Reduction(getRule(0))); - - stateActions = actionTable.get(getState(6)); - stateActions.put(new Token("+"), new Reduction(getRule(2))); - stateActions.put(new Token("*"), new Reduction(getRule(2))); - stateActions.put(new EOF(), new Reduction(getRule(2))); - - stateActions = actionTable.get(getState(7)); - stateActions.put(new Token("+"), new Reduction(getRule(3))); - stateActions.put(new Token("*"), new Reduction(getRule(3))); - stateActions.put(new EOF(), new Reduction(getRule(3))); - - stateActions = actionTable.get(getState(8)); - stateActions.put(new Token("+"), new Reduction(getRule(4))); - stateActions.put(new Token("*"), new Reduction(getRule(4))); - stateActions.put(new EOF(), new Reduction(getRule(4))); - } - - @Override - protected void setUpGotoTable(GrammarType type, Map> gotoTable) { - Map currentGotoActions = new HashMap<>(); - - currentGotoActions.put(new NonTerminal("E"), getState(1)); - currentGotoActions.put(new NonTerminal("B"), getState(6)); - gotoTable.put(getState(0), new HashMap<>(currentGotoActions)); - currentGotoActions.clear(); - - currentGotoActions.put(new NonTerminal("B"), getState(5)); - gotoTable.put(getState(4), new HashMap<>(currentGotoActions)); - currentGotoActions.clear(); - - currentGotoActions.put(new NonTerminal("B"), getState(3)); - gotoTable.put(getState(2), new HashMap<>(currentGotoActions)); - currentGotoActions.clear(); - } - - @Override - protected void setUpParseTrees(Map parseRootMap) { - parseRootMap.put("1+0*1", () -> parseTree0()); - parseRootMap.put("1", () -> parseTree1()); - parseRootMap.put("emptyReduce", () -> parseTree2()); - parseRootMap.put("1+0*1MissingReduction", () -> parseTree3()); - } - - /** - * Parse tree for the sentence "1+0*1" - * @return The root ParseState of the tree - */ - private ParseState parseTree0() { - List parseStates = new ArrayList<>(); - - parseStates.add(new ShiftedState(getState(8), new Token("1"))); - - - parseStates.add(new ReducedState(getState(6), getRule(4), Arrays.asList(new ParseState[] { - parseStates.get(0) - }))); - parseStates.add(new ShiftedState(getState(7), new Token("0"))); - - - parseStates.add(new ReducedState(getState(1), getRule(2), Arrays.asList(new ParseState[] { - parseStates.get(1) - }))); - parseStates.add(new ShiftedState(getState(4), new Token("+"))); - parseStates.add(new ReducedState(getState(5), getRule(3), Arrays.asList(new ParseState[] { - parseStates.get(2) - }))); - parseStates.add(new ShiftedState(getState(8), new Token("1"))); - - - parseStates.add(new ReducedState(getState(1), getRule(0), Arrays.asList(new ParseState[] { - parseStates.get(3), - parseStates.get(4), - parseStates.get(5) - }))); - parseStates.add(new ShiftedState(getState(2), new Token("*"))); - parseStates.add(new ReducedState(getState(3), getRule(4), Arrays.asList(new ParseState[] { - parseStates.get(6) - }))); - - - parseStates.add(new ReducedState(getState(1), getRule(1), Arrays.asList(new ParseState[] { - parseStates.get(7), - parseStates.get(8), - parseStates.get(9) - }))); - - return parseStates.get(parseStates.size() - 1); - } - - /** - * Parse tree for the sentence "1" - * @return The root ParseState of the tree - */ - private ParseState parseTree1() { - List parseStates = new ArrayList<>(); - - parseStates.add(new ShiftedState(getState(8), new Token("1"))); - - return parseStates.get(parseStates.size() - 1); - } - - /** - * Parse tree for the sentence "emptyReduce" - * @return The root ParseState of the tree - */ - private ParseState parseTree2() { - List parseStates = new ArrayList<>(); - - parseStates.add(new ReducedState(getState(1), getRule(1), Arrays.asList(new ParseState[] { - null, - null, - null - }))); - - return parseStates.get(parseStates.size() - 1); - } - - /** - * Parse tree for the sentence "1+0*1MissingReduction" - * @return The root ParseState of the tree - */ - private ParseState parseTree3() { - List parseStates = new ArrayList<>(); - - parseStates.add(new ShiftedState(getState(8), new Token("1"))); - - - parseStates.add(new ReducedState(getState(6), getRule(4), Arrays.asList(new ParseState[] { - parseStates.get(0) - }))); - parseStates.add(new ShiftedState(getState(7), new Token("0"))); - - - parseStates.add(new ReducedState(getState(1), getRule(2), Arrays.asList(new ParseState[] { - parseStates.get(1) - }))); - parseStates.add(new ShiftedState(getState(4), new Token("+"))); - parseStates.add(new ReducedState(getState(5), getRule(3), Arrays.asList(new ParseState[] { - parseStates.get(2) - }))); - parseStates.add(new ShiftedState(getState(8), new Token("1"))); - - - parseStates.add(new ReducedState(getState(1), getRule(0), Arrays.asList(new ParseState[] { - parseStates.get(3), - parseStates.get(4), - null //Missing (Reduction) - }))); - parseStates.add(new ShiftedState(getState(2), new Token("*"))); - parseStates.add(new ReducedState(getState(3), getRule(4), Arrays.asList(new ParseState[] { - parseStates.get(6) - }))); - - - parseStates.add(new ReducedState(getState(1), getRule(1), Arrays.asList(new ParseState[] { - parseStates.get(7), - parseStates.get(8), - parseStates.get(9) - }))); - - return parseStates.get(parseStates.size() - 1); - } - - @Override - protected void setUpRuleConvertors(GrammarType type, Map> ruleConvertorMap) { - ruleConvertorMap.put("Java", new HashMap<>()); - - ruleConvertorMap.get("Java").put("1+0*1", OpZtO.produce()); - ruleConvertorMap.get("Java").put("1", One.produce()); - ruleConvertorMap.get("Java").put("emptyReduce", EmptyReduce.produce()); - ruleConvertorMap.get("Java").put("1+0*1MissingReduction", OpZtOMissingReduction.produce()); - - ruleConvertorMap.put("C", new HashMap<>()); - - ruleConvertorMap.get("C").put("1+0*1", COpZtO.produce()); - } - - @Override - protected void setUpCodeGenerations(GrammarType type, Map> codeGenerations) { - codeGenerations.put("Java", new HashMap<>()); - - codeGenerations.get("Java").put("1+0*1", - "public class TestGrammar {\n" + - "\tpublic static void main(String[] args) {\n" + - "\t\tSystem.out.println(1 + 0 * 1);\n" + - "\t}\n" + - "}" - ); - codeGenerations.get("Java").put("1", - "public class TestGrammar {\n" + - "\tpublic static void main(String[] args) {\n" + - "\t\tSystem.out.println(1);\n" + - "\t}\n" + - "}" - ); - codeGenerations.get("Java").put("emptyReduce", - "public class TestGrammar {\n" + - "\tpublic static void main(String[] args) {\n" + - "\t\tSystem.out.println();\n" + - "\t}\n" + - "}" - ); - codeGenerations.get("Java").put("1+0*1MissingReduction", - "public class TestGrammar {\n" + - "\tpublic static void main(String[] args) {\n" + - "\t\tSystem.out.println(1 + 0 * 1);\n" + - "\t}\n" + - "}" - ); - - codeGenerations.put("C", new HashMap<>()); - - codeGenerations.get("C").put("1+0*1", - "#include \n" + - "\n" + - "main()\n" + - "\tprintf(1 + 0 * 1);\n" + - "}" - ); - } - -}