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
52 changes: 52 additions & 0 deletions src/main/java/grammars/left_recursive/LeftRecursive.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package grammars.left_recursive;

import component_construction.builders.grammar_objects.GrammarBuilder;
import grammar_objects.*;

public class LeftRecursive {

public static Grammar produce() {
return new GrammarBuilder()
.addRule(
new NonTerminal("SectionBlock"),
new LexicalElement[] {
new Token("SECTION"),
new Identifier("Identifier"),
new Token("{"),
new NonTerminal("PropertyList"),
new Token("}")
})

.addRule(
new NonTerminal("PropertyList"),
new LexicalElement[] {
new NonTerminal("PropertyListHead"),
new NonTerminal("Property")
})

.addRule(
new NonTerminal("PropertyListHead"),
new LexicalElement[] {
new NonTerminal("PropertyListHead"),
new NonTerminal("Property")
})

.addRule(
new NonTerminal("PropertyListHead"),
new LexicalElement[] {
new EmptyToken()
})

.addRule(
new NonTerminal("Property"),
new LexicalElement[] {
new Identifier("Identifier"),
new Token("="),
new Literal("Literal"),
new Token(";")
})

.produceGrammar();
}

}
34 changes: 34 additions & 0 deletions src/main/java/grammars/left_recursive/convertors/AutoConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package grammars.left_recursive.convertors;

import java.util.*;

import code_generation.Generator;
import grammar_objects.*;
import grammars.left_recursive.LeftRecursive;

public class AutoConfig {

public static RuleConvertor produce() {
Grammar grammar = LeftRecursive.produce();
Generator defaultGeneration = (elements) -> { return elements[0].getGeneration(); };

return new RuleConvertor(
grammar,
new HashMap<>(Map.of(
grammar.getRule(0), elements -> { // <SectionBlock> -> SECTION Identifier { <PropertyList> }
String id = elements[1].getGeneration();
return "/* Auto-Generated Config Block: " + id + " */\n" + elements[3].getGeneration();
},
grammar.getRule(1), defaultGeneration,// <PropertyList> -> <PropertyListHead> <Property>
grammar.getRule(2), defaultGeneration,// <PropertyListHead> -> <PropertyListHead> <Property>
grammar.getRule(3), defaultGeneration,// <PropertyListHead> -> epsilon
grammar.getRule(4), elements -> { // <Property> -> Identifier = Literal ;
String key = elements[0].getGeneration();
String val = elements[2].getGeneration();
return "System.setProperty(\"" + key + "\", " + val + ");\n";
}
))
);
}

}
10 changes: 10 additions & 0 deletions src/main/java/grammars/left_recursive/leftRecursive.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<SectionBlock> -> SECTION Identifier { <PropertyList> }
<PropertyList> -> <PropertyListHead> <Property>
<PropertyListHead> -> <PropertyListHead> <Property>
<PropertyListHead> -> epsilon
<Property> -> Identifier = Literal ;


Dynamic token regex
Identifier: [a-zA-Z_][a-zA-Z0-9_]*
Literal: [0-9]+|"[^"{}=;]*"
52 changes: 52 additions & 0 deletions src/main/java/grammars/right_recursive/RightRecursive.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package grammars.right_recursive;

import component_construction.builders.grammar_objects.GrammarBuilder;
import grammar_objects.*;

public class RightRecursive {

public static Grammar produce() {
return new GrammarBuilder()
.addRule(
new NonTerminal("SectionBlock"),
new LexicalElement[] {
new Token("SECTION"),
new Identifier("Identifier"),
new Token("{"),
new NonTerminal("PropertyList"),
new Token("}")
})

.addRule(
new NonTerminal("PropertyList"),
new LexicalElement[] {
new NonTerminal("Property"),
new NonTerminal("PropertyListTail"),
})

.addRule(
new NonTerminal("PropertyListTail"),
new LexicalElement[] {
new NonTerminal("Property"),
new NonTerminal("PropertyListTail"),
})

.addRule(
new NonTerminal("PropertyListTail"),
new LexicalElement[] {
new EmptyToken()
})

.addRule(
new NonTerminal("Property"),
new LexicalElement[] {
new Identifier("Identifier"),
new Token("="),
new Literal("Literal"),
new Token(";")
})

.produceGrammar();
}

}
34 changes: 34 additions & 0 deletions src/main/java/grammars/right_recursive/convertors/AutoConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package grammars.right_recursive.convertors;

import java.util.*;

import code_generation.Generator;
import grammar_objects.*;
import grammars.right_recursive.RightRecursive;

public class AutoConfig {

public static RuleConvertor produce() {
Grammar grammar = RightRecursive.produce();
Generator defaultGeneration = (elements) -> { return elements[0].getGeneration(); };

return new RuleConvertor(
grammar,
new HashMap<>(Map.of(
grammar.getRule(0), elements -> { // <SectionBlock> -> SECTION Identifier { <PropertyList> }
String id = elements[1].getGeneration();
return "/* Auto-Generated Config Block: " + id + " */\n" + elements[3].getGeneration();
},
grammar.getRule(1), defaultGeneration,// <PropertyList> -> <PropertyListHead> <Property>
grammar.getRule(2), defaultGeneration,// <PropertyListHead> -> <PropertyListHead> <Property>
grammar.getRule(3), defaultGeneration,// <PropertyListHead> -> epsilon
grammar.getRule(4), elements -> { // <Property> -> Identifier = Literal ;
String key = elements[0].getGeneration();
String val = elements[2].getGeneration();
return "System.setProperty(\"" + key + "\", " + val + ");\n";
}
))
);
}

}
10 changes: 10 additions & 0 deletions src/main/java/grammars/right_recursive/rightRecursive.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<SectionBlock> -> SECTION Identifier { <PropertyList> }
<PropertyList> -> <Property> <PropertyListTail>
<PropertyListTail> -> <Property> <PropertyListTail>
<PropertyListTail> -> epsilon
<Property> -> Identifier = Literal ;


Dynamic token regex
Identifier: [a-zA-Z_][a-zA-Z0-9_]*
Literal: [0-9]+|"[^"{}=;]*"
Loading