Last active
October 23, 2023 13:04
-
-
Save stefanofago73/c42c801bd8c9e62c0daa032f4e4a934e to your computer and use it in GitHub Desktop.
An example of ChatGPT prompt to generate the source code for a simple Domain Specific Language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* Here we have the originating grammar | |
* | |
* Grammar ::= ( | |
* 'SINGLE-WORD' | | |
* 'PARAMETERISED-WORD' '('[A-Z]+')' | | |
* 'WORD1' 'OPTIONAL-WORD'? | | |
* 'WORD2' ( 'WORD-CHOICE-A' | 'WORD-CHOICE-B' ) | | |
* 'WORD3'+ | |
* ) | |
* | |
* | |
* The Prompt is developed taking inspiration | |
* by L.Eder post: https://blog.jooq.org/the-java-fluent-api-designer-crash-course/ | |
* | |
* | |
*/ | |
public interface DSL { | |
// Entry point of the DSL | |
static StartBuilder start() { | |
return new DSLBuilder(); | |
} | |
// Exit point of the DSL | |
void build(); | |
// Interface for the starting point of the DSL | |
interface StartBuilder extends DSL { | |
// Method for the 'SINGLE-WORD' choice | |
SingleWordBuilder singleWord(); | |
// Method for the 'PARAMETERISED-WORD' choice | |
ParameterisedWordBuilder parameterisedWord(String parameter); | |
// Method for the 'WORD1' choice | |
Word1Builder word1(); | |
// Method for the 'WORD2' choice | |
Word2Builder word2(); | |
// Method for the 'WORD3' repetition | |
Word3Builder word3(); | |
} | |
// Interface for 'SINGLE-WORD' | |
interface SingleWordBuilder extends DSL { | |
// No additional methods needed | |
} | |
// Interface for 'PARAMETERISED-WORD' | |
interface ParameterisedWordBuilder extends DSL { | |
// No additional methods needed | |
} | |
// Interface for 'WORD1' | |
interface Word1Builder extends DSL { | |
// Method for the 'OPTIONAL-WORD' choice | |
Word1Builder optionalWord(); | |
} | |
// Interface for 'WORD2' | |
interface Word2Builder extends DSL { | |
// Method for the 'WORD-CHOICE-A' choice | |
Word2Builder wordChoiceA(); | |
// Method for the 'WORD-CHOICE-B' choice | |
Word2Builder wordChoiceB(); | |
} | |
// Interface for 'WORD3' | |
interface Word3Builder extends DSL { | |
// Method to repeat 'WORD3' | |
Word3Builder word3(); | |
} | |
} | |
class DSLBuilder implements DSL.StartBuilder, DSL.SingleWordBuilder, DSL.ParameterisedWordBuilder, DSL.Word1Builder, | |
DSL.Word2Builder, DSL.Word3Builder { | |
// Implementation for 'SINGLE-WORD' | |
@Override | |
public DSL.SingleWordBuilder singleWord() { | |
System.out.println("Implement the logic for 'SINGLE-WORD'"); | |
return this; | |
} | |
// Implementation for 'PARAMETERISED-WORD' | |
@Override | |
public DSL.ParameterisedWordBuilder parameterisedWord(String parameter) { | |
System.out.println(" Implement the logic for 'PARAMETERISED-WORD'"); | |
return this; | |
} | |
// Implementation for 'WORD1' | |
@Override | |
public DSL.Word1Builder word1() { | |
System.out.println(" Implement the logic for 'WORD1'"); | |
return this; | |
} | |
// Implementation for 'OPTIONAL-WORD' | |
@Override | |
public DSL.Word1Builder optionalWord() { | |
System.out.println(" Implement the logic for 'OPTIONAL-WORD'"); | |
return this; | |
} | |
// Implementation for 'WORD2' | |
@Override | |
public DSL.Word2Builder word2() { | |
System.out.println(" Implement the logic for 'WORD2'"); | |
return this; | |
} | |
// Implementation for 'WORD-CHOICE-A' | |
@Override | |
public DSL.Word2Builder wordChoiceA() { | |
System.out.println(" Implement the logic for 'WORD-CHOICE-A'"); | |
return this; | |
} | |
// Implementation for 'WORD-CHOICE-B' | |
@Override | |
public DSL.Word2Builder wordChoiceB() { | |
System.out.println(" Implement the logic for 'WORD-CHOICE-B'"); | |
return this; | |
} | |
// Implementation for 'WORD3' | |
@Override | |
public DSL.Word3Builder word3() { | |
System.out.println(" Implement the logic for 'WORD3'"); | |
return this; | |
} | |
// Implementation for the exit point of the DSL | |
@Override | |
public void build() { | |
System.out.println(" Implement the logic for building the DSL"); | |
} | |
} | |
public class DSLExample { | |
public static void main(String[] args) { | |
// Example path 1: SINGLE-WORD | |
DSL.start().singleWord().build(); | |
// Example path 2: PARAMETERISED-WORD | |
DSL.start().parameterisedWord("parameter").build(); | |
// Example path 3: WORD1 | |
DSL.start().word1().build(); | |
// Example path 4: WORD1 with OPTIONAL-WORD | |
DSL.start().word1().optionalWord().build(); | |
// Example path 5: WORD2 with WORD-CHOICE-A | |
DSL.start().word2().wordChoiceA().build(); | |
// Example path 6: WORD2 with WORD-CHOICE-B | |
DSL.start().word2().wordChoiceB().build(); | |
// Example path 7: WORD3 repetition | |
DSL.start().word3().word3().build(); | |
} | |
} | |
// ------------------------------------------------------------------------------------------------------------------ | |
// | |
// THE PROMPT USED | |
// ------------------------------------------------------------------------------------------------------------------ | |
// | |
// The original dialog: https://chat.openai.com/share/e168ad54-35c0-44cf-b9ff-c930e1de45fe | |
// | |
// ------------------------------------------------------------------------------------------------------------------ | |
//Act as a Java Senior Software engineer: You master all modern Java syntax, data structures, and constructs. temp = 0.3 | |
// | |
//You are provided with an EBFN grammar, as input, defined inside the tags """. | |
// | |
//""" | |
//Grammar ::= ( | |
// 'SINGLE-WORD' | | |
// 'PARAMETERISED-WORD' '('[A-Z]+')' | | |
// 'WORD1' 'OPTIONAL-WORD'? | | |
// 'WORD2' ( 'WORD-CHOICE-A' | 'WORD-CHOICE-B' ) | | |
// 'WORD3'+ | |
//) | |
//""" | |
// | |
//Your task is to create a DSL (Domain Specific Language) as a Fluent API using Java code. | |
// | |
//The Java code to create the DSL must respect the following rules: | |
//- The DSL has a Java Static Method as the entry point of the DSL. | |
//- The DSL has a Java Method as the exit point of the DSL. | |
//- Every DSL Keyword becomes a Java Method but every DSL Connection, expressed by the pipe character, becomes a new Java Interface. | |
//- When the DSL has a Mandatory Choice, every Keyword of that Choice is a Java Method in the current Java Interface. If only one Keyword is possible, then there is only one Java Method. | |
//- When the DSL has an Optional Keyword, the current Java Interface extends the next one in the grammar with all of its Java Methods. | |
//- When the DSL has a Repetition of Keywords, the Java Method representing the repeatable Keyword returns the Java interface itself, instead of the next Java Interface in the grammar. | |
//- Every DSL subdefinition becomes a parameter. This will allow for recursiveness. | |
// | |
//Verify that all paths of the grammar are covered with a code example. | |
//Create code in separate code blocks. | |
// | |
// ------------------------------------------------------------------------------------------------------------------ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment