Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

Expand All @@ -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
Expand All @@ -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

Expand Down
1,348 changes: 750 additions & 598 deletions docs/diagrams/GeneralCompilation.drawio.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
55 changes: 37 additions & 18 deletions src/main/java/grammar_objects/Grammar.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
package grammar_objects;

import static helper_objects.ToStringFormatting.indentFormat;

import java.util.*;

public record Grammar(
Set<Token> tokens,
Set<NonTerminal> nonTerminals,
List<ProductionRule> productionRules,
NonTerminal sentinal
) {
public class Grammar {

protected Set<Token> tokens;
protected Set<NonTerminal> nonTerminals;
protected List<ProductionRule> productionRules;
protected NonTerminal sentinal;

public Grammar(
Set<Token> tokens,
Set<NonTerminal> nonTerminals,
List<ProductionRule> productionRules,
NonTerminal sentinal
) {
this.tokens = tokens;
this.nonTerminals = nonTerminals;
this.productionRules = productionRules;
this.sentinal = sentinal;
}

public ProductionRule getRule(int index) {
return productionRules.get(index);
Expand All @@ -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;
Expand All @@ -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 <E> String tabFormat(Collection<E> collection) {
String out = "";
public Set<Token> getTokens() {
return tokens;
}

for (Object object : collection) {
out += "\t" + object.toString() + "\n";
}
public Set<NonTerminal> getNonTerminals() {
return nonTerminals;
}

return out;
public List<ProductionRule> getProductionRules() {
return productionRules;
}

public NonTerminal getSentinal() {
return sentinal;
}

}
17 changes: 16 additions & 1 deletion src/main/java/grammar_objects/GrammarParts.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
package grammar_objects;

import static helper_objects.ToStringFormatting.indentFormat;

import java.util.Set;

public record GrammarParts(
Set<Token> tokens,
Set<NonTerminal> nonTerminals,
Set<ProductionRule> 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;
}
}
4 changes: 2 additions & 2 deletions src/main/java/grammar_objects/RuleConvertor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/helper_objects/ToStringFormatting.java
Original file line number Diff line number Diff line change
@@ -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 <E> The type which the given collection contains
* @param collection The collection provided
* @return The produced string representation of the collection
*/
public static <E> String indentFormat(Collection<E> 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 <E> The type of the provided value
* @param value The value provided
* @return The produced string representation of the value
*/
public static <E> String indentFormat(E value) {
return "\t" + value.toString().replace("\n", "\n\t");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@

public record Accept() implements Action {

@Override
public final String toString() {
return "ACCEPT";
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package syntax_analysis.grammar_structure_creation;

import static helper_objects.ToStringFormatting.indentFormat;

import java.util.*;
import java.util.Map.*;

Expand Down Expand Up @@ -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);
}
}
48 changes: 24 additions & 24 deletions src/test/java/code_generation/BasicCodeGeneratorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);
}
}
4 changes: 2 additions & 2 deletions src/test/java/component_construction/CompilerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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);
}
}
13 changes: 0 additions & 13 deletions src/test/java/mock_objects/IntComputationLex.java

This file was deleted.

Loading
Loading