From f8d1f0b27a04a26e2988de556b5fa7026436be47 Mon Sep 17 00:00:00 2001 From: warburec <77669019+warburec@users.noreply.github.com> Date: Sat, 7 Nov 2026 00:02:09 +0000 Subject: [PATCH 1/2] Left Recursive --- .../left_recursive/LeftRecursive.java | 52 +++++++++++++++++++ .../left_recursive/convertors/AutoConfig.java | 34 ++++++++++++ .../grammars/left_recursive/leftRecursive.txt | 10 ++++ 3 files changed, 96 insertions(+) create mode 100644 src/main/java/grammars/left_recursive/LeftRecursive.java create mode 100644 src/main/java/grammars/left_recursive/convertors/AutoConfig.java create mode 100644 src/main/java/grammars/left_recursive/leftRecursive.txt diff --git a/src/main/java/grammars/left_recursive/LeftRecursive.java b/src/main/java/grammars/left_recursive/LeftRecursive.java new file mode 100644 index 0000000..a48985d --- /dev/null +++ b/src/main/java/grammars/left_recursive/LeftRecursive.java @@ -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(); + } + +} diff --git a/src/main/java/grammars/left_recursive/convertors/AutoConfig.java b/src/main/java/grammars/left_recursive/convertors/AutoConfig.java new file mode 100644 index 0000000..895261a --- /dev/null +++ b/src/main/java/grammars/left_recursive/convertors/AutoConfig.java @@ -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 -> { // -> SECTION Identifier { } + String id = elements[1].getGeneration(); + return "/* Auto-Generated Config Block: " + id + " */\n" + elements[3].getGeneration(); + }, + grammar.getRule(1), defaultGeneration,// -> + grammar.getRule(2), defaultGeneration,// -> + grammar.getRule(3), defaultGeneration,// -> epsilon + grammar.getRule(4), elements -> { // -> Identifier = Literal ; + String key = elements[0].getGeneration(); + String val = elements[2].getGeneration(); + return "System.setProperty(\"" + key + "\", " + val + ");\n"; + } + )) + ); + } + +} diff --git a/src/main/java/grammars/left_recursive/leftRecursive.txt b/src/main/java/grammars/left_recursive/leftRecursive.txt new file mode 100644 index 0000000..b60e09e --- /dev/null +++ b/src/main/java/grammars/left_recursive/leftRecursive.txt @@ -0,0 +1,10 @@ + -> SECTION Identifier { } + -> + -> + -> epsilon + -> Identifier = Literal ; + + +Dynamic token regex +Identifier: [a-zA-Z_][a-zA-Z0-9_]* +Literal: [0-9]+|"[^"{}=;]*" \ No newline at end of file From 10be031f31cd74ce4bb84b6b8a015d772f98eeeb Mon Sep 17 00:00:00 2001 From: warburec <77669019+warburec@users.noreply.github.com> Date: Sat, 7 Nov 2026 00:02:22 +0000 Subject: [PATCH 2/2] Right Recursive --- .../right_recursive/RightRecursive.java | 52 +++++++++++++++++++ .../convertors/AutoConfig.java | 34 ++++++++++++ .../right_recursive/rightRecursive.txt | 10 ++++ 3 files changed, 96 insertions(+) create mode 100644 src/main/java/grammars/right_recursive/RightRecursive.java create mode 100644 src/main/java/grammars/right_recursive/convertors/AutoConfig.java create mode 100644 src/main/java/grammars/right_recursive/rightRecursive.txt diff --git a/src/main/java/grammars/right_recursive/RightRecursive.java b/src/main/java/grammars/right_recursive/RightRecursive.java new file mode 100644 index 0000000..dcdf718 --- /dev/null +++ b/src/main/java/grammars/right_recursive/RightRecursive.java @@ -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(); + } + +} diff --git a/src/main/java/grammars/right_recursive/convertors/AutoConfig.java b/src/main/java/grammars/right_recursive/convertors/AutoConfig.java new file mode 100644 index 0000000..594b3bf --- /dev/null +++ b/src/main/java/grammars/right_recursive/convertors/AutoConfig.java @@ -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 -> { // -> SECTION Identifier { } + String id = elements[1].getGeneration(); + return "/* Auto-Generated Config Block: " + id + " */\n" + elements[3].getGeneration(); + }, + grammar.getRule(1), defaultGeneration,// -> + grammar.getRule(2), defaultGeneration,// -> + grammar.getRule(3), defaultGeneration,// -> epsilon + grammar.getRule(4), elements -> { // -> Identifier = Literal ; + String key = elements[0].getGeneration(); + String val = elements[2].getGeneration(); + return "System.setProperty(\"" + key + "\", " + val + ");\n"; + } + )) + ); + } + +} diff --git a/src/main/java/grammars/right_recursive/rightRecursive.txt b/src/main/java/grammars/right_recursive/rightRecursive.txt new file mode 100644 index 0000000..81e3f80 --- /dev/null +++ b/src/main/java/grammars/right_recursive/rightRecursive.txt @@ -0,0 +1,10 @@ + -> SECTION Identifier { } + -> + -> + -> epsilon + -> Identifier = Literal ; + + +Dynamic token regex +Identifier: [a-zA-Z_][a-zA-Z0-9_]* +Literal: [0-9]+|"[^"{}=;]*" \ No newline at end of file