From: Thomas Walker Lynch Date: Mon, 12 Aug 2024 18:07:17 +0000 (+0000) Subject: more parsser programs X-Git-Url: https://git.reasoningtechnology.com/usr/lib/python2.7/os.py?a=commitdiff_plain;h=9d7379c26a4f88172cd87eb3ec5c948a12f5b258;p=GQL-to-Cypher more parsser programs --- diff --git a/developer/executor/Arithmetic_Echo b/developer/executor/Arithmetic_Echo new file mode 100755 index 0000000..554719e --- /dev/null +++ b/developer/executor/Arithmetic_Echo @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +/var/user_data/Thomas-developer/GQL_to_Cypher/tool/jdk-22.0.1+8/bin/java -cp /var/user_data/Thomas-developer/GQL_to_Cypher/tool/executor/antlr-4.11.1-complete.jar:jvm:jvm/Arithmetic_Echo.jar Arithmetic_Echo $@ diff --git a/developer/executor/Arithmetic_Echo__Test b/developer/executor/Arithmetic_Echo__Test new file mode 100755 index 0000000..72a2062 --- /dev/null +++ b/developer/executor/Arithmetic_Echo__Test @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +/var/user_data/Thomas-developer/GQL_to_Cypher/tool/jdk-22.0.1+8/bin/java -cp /var/user_data/Thomas-developer/GQL_to_Cypher/tool/executor/antlr-4.11.1-complete.jar:jvm:jvm/Arithmetic_Echo__Test.jar Arithmetic_Echo__Test $@ diff --git a/developer/executor/Tree_Arithmetic b/developer/executor/Tree_Arithmetic new file mode 100755 index 0000000..dbd24ad --- /dev/null +++ b/developer/executor/Tree_Arithmetic @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +/var/user_data/Thomas-developer/GQL_to_Cypher/tool/jdk-22.0.1+8/bin/java -cp /var/user_data/Thomas-developer/GQL_to_Cypher/tool/executor/antlr-4.11.1-complete.jar:jvm:jvm/Tree_Arithmetic.jar Tree_Arithmetic $@ diff --git a/developer/executor/clean b/developer/executor/clean index dd931d1..c8c3091 100755 --- a/developer/executor/clean +++ b/developer/executor/clean @@ -7,7 +7,7 @@ # 2. General clean targets: # # > clean all # removes all things make built, and thus can be replaced by running make again. -# > clean program # removes all built executor scripts and corresponding jar files +# > clean program # removes all built ${EXECUTOR_IN_DIR} scripts and corresponding jar files # > clean class # removes the class files # > clean grammar # removes the generated java grammar files # @@ -22,16 +22,10 @@ display_usage() { echo "Usage: clean ] | program [] | grammar [] | class | temp[orary]>" } -# Function to clean a directory -clean_directory() { - local dir="$1" - find "$dir" -type f ! -name '.githolder' -exec rm -f {} + -} - -# Function to check if a program is in PROGRAM_FPL +# Function to check if a program is in EXECUTOR_IN_FPL is_program_in_fpl() { local program="$1" - for prog in $PROGRAM_FPL; do + for prog in $EXECUTOR_IN_FPL; do if [[ "$prog" == *"$program"* ]]; then return 0 fi @@ -44,9 +38,9 @@ delete_program() { local program="$1" if is_program_in_fpl "$program"; then echo "Deleting program files for '$program'" - rm -f "executor/$program" "$JAVA_COMP_OUT_DIR/$program.jar" "$JAVA_COMP_OUT_DIR/$program.class" + rm -f "${EXECUTOR_IN_DIR}/$program" "$JAVA_COMP_OUT_DIR/$program.jar" "$JAVA_COMP_OUT_DIR/$program.class" else - echo "Error: Program '$program' is not recognized or is not listed in PROGRAM_FPL." + echo "Error: Program '$program' is not recognized or is not listed in EXECUTOR_IN_FPL." return 1 fi } @@ -63,24 +57,24 @@ clean_command_parser() { elif [ "$token_count" -eq 1 ]; then case "$command" in "temporary" | "temp") - clean_directory "$TEMP_DIR" + ${EXECUTOR_IN_DIR}/clean_directory "$TEMP_DIR" ;; "all") - clean_directory "$TEMP_DIR" - clean_grammar - clean_file_list "JAVA_COMP_OUT_FPL" - clean_file_list "JAR_OUT_FPL" - clean_file_list "PROGRAM_FPL" + ${EXECUTOR_IN_DIR}/clean_directory "$TEMP_DIR" + ${EXECUTOR_IN_DIR}/clean_grammar + ${EXECUTOR_IN_DIR}/clean_file_list "JAVA_COMP_OUT_FPL" + ${EXECUTOR_IN_DIR}/clean_file_list "JAR_OUT_FPL" + ${EXECUTOR_IN_DIR}/clean_file_list "EXECUTOR_IN_FPL" ;; "program") - clean_file_list "JAR_OUT_FPL" - clean_file_list "PROGRAM_FPL" + ${EXECUTOR_IN_DIR}/clean_file_list "JAR_OUT_FPL" + ${EXECUTOR_IN_DIR}/clean_file_list "EXECUTOR_IN_FPL" ;; "class") - clean_file_list "JAVA_COMP_OUT_FPL" + ${EXECUTOR_IN_DIR}/clean_file_list "JAVA_COMP_OUT_FPL" ;; "grammar") - clean_grammar + ${EXECUTOR_IN_DIR}/clean_grammar ;; *) echo "Unknown clean option: $command" @@ -93,11 +87,11 @@ clean_command_parser() { delete_program "$arg" # Custom cleaning for each program. case "$arg" in - "SyntaxTree_Test") - clean_grammar "GQL_Test" + "Tree_Test") + ${EXECUTOR_IN_DIR}/clean_grammar "GQL_Test" ;; - "SyntaxTree_20240412") - clean_grammar "GQL_20240412" + "Tree_20240412") + ${EXECUTOR_IN_DIR}/clean_grammar "GQL_20240412" ;; "PrintRuleNameList") : @@ -113,7 +107,7 @@ clean_command_parser() { ;; "grammar") # Grammar name is $arg. - clean_grammar "$arg" + ${EXECUTOR_IN_DIR}/clean_grammar "$arg" ;; *) echo "Unknown clean command: $command" @@ -124,5 +118,6 @@ clean_command_parser() { fi } -source env_build +cd "$DEVELOPER_HOME" || { echo "Failed to change directory to" "$DEVELOPER_HOME"; exit 1; } +source ${EXECUTOR_IN_DIR}/env_build clean_command_parser $@ diff --git a/developer/executor/clean_directory b/developer/executor/clean_directory index e5d080d..873d400 100755 --- a/developer/executor/clean_directory +++ b/developer/executor/clean_directory @@ -1,4 +1,5 @@ #!/usr/bin/env bash +# helper function for `clean`, though can be called directly if [ -z "$1" ]; then echo "Error: Directory argument is empty" diff --git a/developer/executor/clean_file_list b/developer/executor/clean_file_list index 7922717..d9af5d9 100755 --- a/developer/executor/clean_file_list +++ b/developer/executor/clean_file_list @@ -1,4 +1,5 @@ #!/usr/bin/env bash +# helper function for `clean`, though can be called directly if [ -z "$1" ]; then exit 0 diff --git a/developer/executor/clean_grammar b/developer/executor/clean_grammar index e182a47..9e90319 100755 --- a/developer/executor/clean_grammar +++ b/developer/executor/clean_grammar @@ -1,8 +1,9 @@ #!/usr/bin/env bash +# helper function for `clean`, though can be called directly # Check if required variables are set if [ -z "$ANTLR_GRAMMAR_LIST" ] || [ -z "$ANTLR_OUT_DIR" ]; then - echo "Error: Required environment variables ANTLR_GRAMMAR_LIST or ANTLR_OUT_DIR are not set." + echo "Error: Required environment variables ANTLR_GRAMMAR_LIST and ANTLR_OUT_DIR are not set." exit 1 fi @@ -22,7 +23,7 @@ delete_grammar() { # If no argument is given, delete all grammars if [ -z "$1" ]; then - echo "No argument provided, deleting all grammars" + echo "Cleaning all grammar builds." for grammar in $ANTLR_GRAMMAR_LIST; do delete_grammar "$grammar" done diff --git a/developer/executor/clean_program b/developer/executor/clean_program new file mode 100755 index 0000000..b2ca550 --- /dev/null +++ b/developer/executor/clean_program @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +# helper function for `clean`, though can be called directly + +# Check if required variables are set +if [ -z "${EXECUTOR_IN_FPL}" ] || [ -z "${EXECUTOR_IN_DIR}" ]; then + echo "Error: Required environment variables EXECUTOR_IN_FPL and EXECUTOR_IN_DIR are not set." + exit 1 +fi + +# Function to delete a specific program +delete_program() { + local program=$1 + # echo "Attempting to delete program: $program" + if [ -z "$program" ]; then + echo "Error: Program name is empty" + elif echo "$ANTLR_PROGRAM_LIST" | grep -qw "$program"; then + rm -f "$ANTLR_OUT_DIR/$program"* + echo "Deleted files starting with $program in $ANTLR_OUT_DIR" + else + echo "Invalid program name: $program" + fi +} + +# If no argument is given, delete all programs +if [ -z "$1" ]; then + echo "removing all program and their jars." + for program in ${EXECUTOR_IN_FPL}; do + delete_program "$program" + done +else + echo "rmoving program $1 and its jar." + delete_program "$1" +fi diff --git a/developer/executor/env_build b/developer/executor/env_build index 3973575..67eb327 100755 --- a/developer/executor/env_build +++ b/developer/executor/env_build @@ -16,7 +16,26 @@ fi # _OUT things output by some program #-------------------------------------------------------------------------------- -# directories +# to be built + + +# Add each new executable to this list, and give it a custom target in the makefile. +export EXECUTOR_IN_FL="\ + PrintRuleNameList\ + Arithmetic_Echo\ + Arithmetic_Echo__Test\ + Arithmetic_Syntax\ + 20240412_Syntax\ + " + +# where make will put the programs +export EXECUTOR_IN_DIR="executor" + +# Generate the EXECUTOR_IN_FPL list +export EXECUTOR_IN_FPL=$(for program in $EXECUTOR_IN_FL; do echo "${EXECUTOR_IN_DIR}/${program}"; done | tr '\n' ' ') + +#-------------------------------------------------------------------------------- +# build directories # # ANTLR directories @@ -44,17 +63,6 @@ export JAVA_INTERP="${JAVA_HOME}/bin/java" export JAVA_ARCHIVE="${JAVA_HOME}/bin/jar" export CLASSPATH="$ANTLR_JAR" -#-------------------------------------------------------------------------------- -# to be built -# -export PROGRAM_PrintRuleNameList="executor/PrintRuleNameList" -export PROGRAM_SyntaxTree_Arithmetic="executor/SyntaxTree_Arithmetic" -export PROGRAM_Test__SyntaxTree_Arithmetic="executor/Test__SyntaxTree_Arithmetic" -export PROGRAM_SyntaxTree_20240412="executor/SyntaxTree_20240412" - -# an all programs list -export PROGRAM_FPL="${PROGRAM_PrintRuleNameList} ${PROGRAM_SyntaxTree_Arithmetic} ${PROGRAM_SyntaxTree_20240412} ${PROGRAM_Test__SyntaxTree_Arithmetic}" - #-------------------------------------------------------------------------------- # misc # @@ -109,7 +117,7 @@ export JAVA_COMP_OUT_FPL="${JAVA_COMP_OUT_PRIMARY_FPL} ${JAVA_COMP_OUT_ANTLR_FPL # List of JAR files to be built JAR_OUT_FPL="" -for program_path in ${PROGRAM_FPL}; do +for program_path in ${EXECUTOR_IN_FPL}; do program_name=$(basename ${program_path}) class_file="${JAVA_COMP_OUT_DIR}/${program_name}.class" # Check if the .class file is in JAVA_COMP_OUT_FPL diff --git a/developer/executor/make b/developer/executor/make index 9f3cd80..02e3b57 100755 --- a/developer/executor/make +++ b/developer/executor/make @@ -1,6 +1,15 @@ #!/usr/bin/env bash -cd "$DEVELOPER_HOME" || { echo "Failed to change directory to" "$DEVELOPER_HOME"; exit 1; } -source executor/env_build -/usr/bin/make --no-builtin-rules --keep-going $@ +if [[ -z "${DEVELOPER_HOME}" || -z "${EXECUTOR_IN_DIR}" ]]; then + echo "required variables not set. As seen by:" + echo " DEVELOPER_HOME: $DEVELOPER_HOME" + echo " EXECUTOR_IN_DIR: $EXECUTOR_IN_DIR" + exit 1 +fi +cd "$DEVELOPER_HOME" + +# in case there have been edits to the environment +source "${EXECUTOR_IN_DIR}"/env_build + +/usr/bin/make --no-builtin-rules -f "${EXECUTOR_IN_DIR}"/makefile $@ diff --git a/developer/executor/makefile b/developer/executor/makefile new file mode 100644 index 0000000..448f1d6 --- /dev/null +++ b/developer/executor/makefile @@ -0,0 +1,106 @@ +#================================================================================ +# Custom make targets +# + +all: setup $(EXECUTOR_IN_FPL) + + +PrintRuleNameList: $(EXECUTOR_PrintRuleNameList) + + +Arithmetic_Echo: $(ANTLR_OUT_Arithmetic_FPL) + @if [ -z "$(ANTLR_OUT_Arithmetic_FPL)" ]; then \ + echo "variable ANTLR_OUT_Arithmetic_FPL empty."; \ + exit 1; \ + fi + make $(EXECUTOR_IN_DIR)/Arithmetic_Echo + +Arithmetic_Echo__Test: $(ANTLR_OUT_Arithmetic_FPL) + @if [ -z "$(ANTLR_OUT_Arithmetic_FPL)" ]; then \ + echo "variable ANTLR_OUT_Arithmetic_FPL empty."; \ + exit 1; \ + fi + make $(EXECUTOR_IN_DIR)/Arithmetic_Echo__Test + +Arithmetic_Syntax: $(ANTLR_OUT_Arithmetic_FPL) + @if [ -z "$(ANTLR_OUT_Arithmetic_FPL)" ]; then \ + echo "variable ANTLR_OUT_Arithmetic_FPL empty."; \ + exit 1; \ + fi + make $(EXECUTOR_IN_DIR)/Arithmetic_Syntax + +Tree_20240412: $(ANTLR_OUT_GQL_20240412_FPL) + @if [ -z "$(ANTLR_OUT_GQL_20240412_FPL)" ]; then \ + echo "variable ANTLR_OUT_GQL_20240412_FPL empty."; \ + exit 1; \ + fi + make $(EXECUTOR_IN_DIR)/Tree_20240412 + + +# make the `.java` files for all grammars +grammar: setup $(ANTLR_OUT_FPL) + +# Specific grammar targets. Run them like this: +# > make +# e.g. > make GQL_test +$(foreach grammar,$(ANTLR_GRAMMAR_LIST),$(eval $(grammar): $(value ANTLR_OUT_$(grammar)_FPL))) + +# Compile all the .java files. +java: setup $(JAVA_COMP_OUT_FPL) + +.PHONY: version +version: + $(info ANTLR_JAR is '$(notdir $(ANTLR_JAR))') + @ $(JAVA_COMP) --version + @ $(JAVA_ARCHIVE) --version + @ make -v | head -n 1 + @ echo "makefile 0.4" + +.PHONY: setup +# ANTLR automatically creates $(ANTLR_OUT_DIR) +setup: + mkdir -p $(ANTLR_IN_PRIMARY_DIR) $(JAVA_COMP_IN_PRIMARY_DIR) $(JVM_IN_DIR) + mkdir -p $(EXECUTOR_IN_DIR) test deprecated experiment documentation temporary + +# Default clean target +.PHONY: setup +clean: + @echo "Use the clean script from the $(EXECUTOR_IN_DIR) directory instead of \`make clean\`" + @$(EXECUTOR_IN_DIR)/clean + +#useful for distinguishing initial make error messages and message generated by rules firing +nothing: + @: + +#================================================================================ +# generic targets, aka recipes +# + +$(ANTLR_OUT_DIR)/%Lexer.java \ +$(ANTLR_OUT_DIR)/%Parser.java \ +$(ANTLR_OUT_DIR)/%BaseVisitor.java \ +$(ANTLR_OUT_DIR)/%Visitor.java: $(ANTLR_IN_PRIMARY_FPL) + @echo "making grammar from:" $< + $(JAVA_INTERP) -jar $(ANTLR_JAR) -Dlanguage=Java -visitor -o $(ANTLR_OUT_DIR_PARENT) $< + +# Rule to build .class files from .java files +$(JAVA_COMP_OUT_DIR)/%.class: $(JAVA_COMP_IN_PRIMARY_DIR)/%.java + @echo "Compiling $<..." + $(JAVA_COMP) -d $(JAVA_COMP_OUT_DIR) -sourcepath $(JAVA_COMP_IN_DL) $< + @echo "Created $@" + +# Without this, GNU make inserts an 'rm -rf' not on the recipe and deletes the .jar files +# after making them. Why make why? +.PRECIOUS: $(JAVA_COMP_OUT_DIR)/%.jar +# Rule to build .jar files from .class files +$(JAVA_COMP_OUT_DIR)/%.jar: $(JAVA_COMP_OUT_DIR)/%.class + @echo "Building $*..." + $(JAVA_ARCHIVE) cf $@ -C $(JAVA_COMP_OUT_DIR) $*.class + @echo "Created $@" + +$(EXECUTOR_IN_DIR)/%: $(JAVA_COMP_OUT_DIR)/%.jar + @echo "Creating script for $*..." + @echo "#!/usr/bin/env bash" > $(EXECUTOR_IN_DIR)/$* + @echo "$(JAVA_INTERP) -cp ${CLASSPATH}:$(JAVA_COMP_OUT_DIR):$< $*" \$$\@ >> $(EXECUTOR_IN_DIR)/$* + chmod +x $(EXECUTOR_IN_DIR)/$* + @echo "Created script $(EXECUTOR_IN_DIR)/$*" diff --git a/developer/javac/20240412_Syntax.java b/developer/javac/20240412_Syntax.java new file mode 100644 index 0000000..34aebe9 --- /dev/null +++ b/developer/javac/20240412_Syntax.java @@ -0,0 +1,37 @@ +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.tree.*; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.lang.reflect.Method; + +public class Syntax_20240412 { + + // Versioned Lexer and Parser + static final Class GQL_Lexer = GQL_20240412Lexer.class; + static final Class GQL_Parser = GQL_20240412Parser.class; + + public static void main(String[] args) throws IOException { + if (args.length != 1) { + System.err.println("Usage: java Syntax20240412 "); + System.exit(1); + } + + String inputFile = args[0]; + String input = new String(Files.readAllBytes(Paths.get(inputFile))); + + try { + Lexer lexer = (Lexer) GQL_Lexer.getConstructor(CharStream.class).newInstance(CharStreams.fromString(input)); + CommonTokenStream tokens = new CommonTokenStream(lexer); + Parser parser = (Parser) GQL_Parser.getConstructor(TokenStream.class).newInstance(tokens); + Method startRule = parser.getClass().getMethod("program"); // Assuming 'program' is the start rule + ParseSyntax tree = (ParseSyntax) startRule.invoke(parser); + + PrintVisitor visitor = new PrintVisitor(parser.getRuleNames()); + String syntaxSyntax = visitor.visit(tree); + System.out.println(syntaxSyntax); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/developer/javac/Arithmetic_Echo.java b/developer/javac/Arithmetic_Echo.java new file mode 100644 index 0000000..663d56f --- /dev/null +++ b/developer/javac/Arithmetic_Echo.java @@ -0,0 +1,34 @@ +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.tree.*; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +public class Arithmetic_Echo { + + public static void main(String[] args) throws IOException { + if (args.length != 1) { + System.err.println("Usage: java Arithmetic_Echo "); + System.exit(1); + } + + String inputFile = args[0]; + String input = Files.readString(Paths.get(inputFile)); + + try { + // Direct instantiation without reflection + ArithmeticLexer lexer = new ArithmeticLexer(CharStreams.fromString(input)); + CommonTokenStream tokens = new CommonTokenStream(lexer); + ArithmeticParser parser = new ArithmeticParser(tokens); + + // Directly calling the start rule method + ParseTree tree = parser.program(); // Assuming 'program' is the start rule + + Arithmetic_Echo_PrintVisitor visitor = new Arithmetic_Echo_PrintVisitor(parser.getRuleNames()); + String output = visitor.visit(tree); + System.out.println(output); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/developer/javac/Arithmetic_Echo_PrintVisitor.java b/developer/javac/Arithmetic_Echo_PrintVisitor.java new file mode 100644 index 0000000..488d54a --- /dev/null +++ b/developer/javac/Arithmetic_Echo_PrintVisitor.java @@ -0,0 +1,31 @@ +import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor; + +public class Arithmetic_Echo_PrintVisitor extends ArithmeticBaseVisitor { + private final String[] ruleNames; + + public Arithmetic_Echo_PrintVisitor(String[] ruleNames) { + this.ruleNames = ruleNames; + } + + @Override + public String visitProgram(ArithmeticParser.ProgramContext ctx) { + return visit(ctx.expression()); + } + + @Override + public String visitExpression(ArithmeticParser.ExpressionContext ctx) { + if (ctx.INT() != null) { + return ctx.INT().getText(); + } else if (ctx.getChildCount() == 3 && ctx.getChild(0).getText().equals("(")) { + // Handle parentheses + return visit(ctx.expression(0)); // Remove the extra parentheses + } else { + String left = visit(ctx.expression(0)); + String right = visit(ctx.expression(1)); + String operator = ctx.getChild(1).getText(); + return "(" + left + " " + operator + " " + right + ")"; + } + } + +} + diff --git a/developer/javac/Arithmetic_Echo__Test.java b/developer/javac/Arithmetic_Echo__Test.java new file mode 100644 index 0000000..de89933 --- /dev/null +++ b/developer/javac/Arithmetic_Echo__Test.java @@ -0,0 +1,92 @@ +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.tree.*; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +public class Arithmetic_Echo__Test { + + public static boolean PrintVisitor_0() { + // Simple smoke test + try { + String input = "3 + 5"; + String expectedOutput = "(3 + 5)"; + String actualOutput = runSyntax(input); + return expectedOutput.equals(actualOutput); + } catch (Exception e) { + return false; + } + } + + public static boolean PrintVisitor_1() { + // Test with multiplication + try { + String input = "2 * 3"; + String expectedOutput = "(2 * 3)"; + String actualOutput = runSyntax(input); + return expectedOutput.equals(actualOutput); + } catch (Exception e) { + return false; + } + } + + public static boolean PrintVisitor_2() { + // Test with parentheses + try { + String input = "2 * (3 + 4)"; + String expectedOutput = "(2 * (3 + 4))"; + String actualOutput = runSyntax(input); + return expectedOutput.equals(actualOutput); + } catch (Exception e) { + return false; + } + } + + public static boolean PrintVisitor_3() { + // Test with division + try { + String input = "10 / 2"; + String expectedOutput = "(10 / 2)"; + String actualOutput = runSyntax(input); + return expectedOutput.equals(actualOutput); + } catch (Exception e) { + return false; + } + } + + public static boolean PrintVisitor_4() { + // Test with complex expression + try { + String input = "3 + 5 * (10 - 4)"; + String expectedOutput = "(3 + (5 * (10 - 4)))"; + String actualOutput = runSyntax(input); + // Uncomment the print statement for debugging + // System.out.println("PrintVisitor_4 - Expected: " + expectedOutput + ", Actual: " + actualOutput); + return expectedOutput.equals(actualOutput); + } catch (Exception e) { + return false; + } + } + + private static String runSyntax(String input) throws Exception { + Lexer lexer = new ArithmeticLexer(CharStreams.fromString(input)); + CommonTokenStream tokens = new CommonTokenStream(lexer); + ArithmeticParser parser = new ArithmeticParser(tokens); + Method startRule = parser.getClass().getMethod("program"); + ParseTree tree = (ParseTree) startRule.invoke(parser); + + Arithmetic_Echo_PrintVisitor visitor = new Arithmetic_Echo_PrintVisitor(parser.getRuleNames()); + return visitor.visit(tree); + } + + public static void main(String[] args) { + Map tests = new HashMap<>(); + tests.put("PrintVisitor_0", PrintVisitor_0()); + tests.put("PrintVisitor_1", PrintVisitor_1()); + tests.put("PrintVisitor_2", PrintVisitor_2()); + tests.put("PrintVisitor_3", PrintVisitor_3()); + tests.put("PrintVisitor_4", PrintVisitor_4()); + + TestBench.runTests(tests); + } +} diff --git a/developer/javac/Arithmetic_Syntax.java b/developer/javac/Arithmetic_Syntax.java new file mode 100644 index 0000000..e433223 --- /dev/null +++ b/developer/javac/Arithmetic_Syntax.java @@ -0,0 +1,34 @@ +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.tree.*; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +public class Arithmetic_Syntax{ + + public static void main(String[] args) throws IOException { + if (args.length != 1) { + System.err.println("Usage: java Syntax "); + System.exit(1); + } + + String inputFile = args[0]; + String input = Files.readString(Paths.get(inputFile)); + + try { + // Direct instantiation without reflection + ArithmeticLexer lexer = new ArithmeticLexer(CharStreams.fromString(input)); + CommonTokenStream tokens = new CommonTokenStream(lexer); + ArithmeticParser parser = new ArithmeticParser(tokens); + + // Directly calling the start rule method + ParseSyntax tree = parser.program(); // Assuming 'program' is the start rule + + PrintVisitor visitor = new PrintVisitor(parser.getRuleNames()); + String syntaxSyntax = visitor.visit(tree); + System.out.println(syntaxSyntax); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/developer/javac/Arithmetic_Syntax_PrintVisitor.java b/developer/javac/Arithmetic_Syntax_PrintVisitor.java new file mode 100644 index 0000000..b70c0f0 --- /dev/null +++ b/developer/javac/Arithmetic_Syntax_PrintVisitor.java @@ -0,0 +1,29 @@ +import org.antlr.v4.runtime.tree.AbstractParseSyntaxVisitor; + +public class Arithmetic_Syntax_PrintVisitor extends ArithmeticBaseVisitor { + private final String[] ruleNames; + + public PrintVisitor(String[] ruleNames) { + this.ruleNames = ruleNames; + } + + @Override + public String visitProgram(ArithmeticParser.ProgramContext ctx) { + return "program(" + visit(ctx.expression()) + ")"; + } + + @Override + public String visitExpression(ArithmeticParser.ExpressionContext ctx) { + if (ctx.INT() != null) { + return "INT(" + ctx.INT().getText() + ")"; + } else if (ctx.getChildCount() == 3 && ctx.getChild(0).getText().equals("(")) { + // Handle parentheses in the context of the grammar structure + return "expression(" + visit(ctx.expression(0)) + ")"; + } else { + String left = visit(ctx.expression(0)); + String right = visit(ctx.expression(1)); + String operator = "operator(" + ctx.getChild(1).getText() + ")"; + return "expression(" + left + " " + operator + " " + right + ")"; + } + } +} diff --git a/developer/javac/PrintVisitor.java b/developer/javac/PrintVisitor.java deleted file mode 100644 index c5a7d4a..0000000 --- a/developer/javac/PrintVisitor.java +++ /dev/null @@ -1,31 +0,0 @@ -import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor; - -public class PrintVisitor extends ArithmeticBaseVisitor { - private final String[] ruleNames; - - public PrintVisitor(String[] ruleNames) { - this.ruleNames = ruleNames; - } - - @Override - public String visitProgram(ArithmeticParser.ProgramContext ctx) { - return visit(ctx.expression()); - } - - @Override - public String visitExpression(ArithmeticParser.ExpressionContext ctx) { - if (ctx.INT() != null) { - return ctx.INT().getText(); - } else if (ctx.getChildCount() == 3 && ctx.getChild(0).getText().equals("(")) { - // Handle parentheses - return visit(ctx.expression(0)); // Remove the extra parentheses - } else { - String left = visit(ctx.expression(0)); - String right = visit(ctx.expression(1)); - String operator = ctx.getChild(1).getText(); - return "(" + left + " " + operator + " " + right + ")"; - } - } - -} - diff --git a/developer/javac/PrintVisitor_orig.java b/developer/javac/PrintVisitor_orig.java index ddb127d..22b34dd 100644 --- a/developer/javac/PrintVisitor_orig.java +++ b/developer/javac/PrintVisitor_orig.java @@ -1,6 +1,6 @@ -import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor; +import org.antlr.v4.runtime.tree.AbstractParseSyntaxVisitor; -public class PrintVisitor extends AbstractParseTreeVisitor implements ArithmeticVisitor { +public class PrintVisitor extends AbstractParseSyntaxVisitor implements ArithmeticVisitor { private final String[] ruleNames; public PrintVisitor(String[] ruleNames) { diff --git a/developer/javac/SyntaxTree_20240412.java b/developer/javac/SyntaxTree_20240412.java deleted file mode 100644 index f338cb1..0000000 --- a/developer/javac/SyntaxTree_20240412.java +++ /dev/null @@ -1,37 +0,0 @@ -import org.antlr.v4.runtime.*; -import org.antlr.v4.runtime.tree.*; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.lang.reflect.Method; - -public class SyntaxTree_20240412 { - - // Versioned Lexer and Parser - static final Class GQL_Lexer = GQL_20240412Lexer.class; - static final Class GQL_Parser = GQL_20240412Parser.class; - - public static void main(String[] args) throws IOException { - if (args.length != 1) { - System.err.println("Usage: java SyntaxTree20240412 "); - System.exit(1); - } - - String inputFile = args[0]; - String input = new String(Files.readAllBytes(Paths.get(inputFile))); - - try { - Lexer lexer = (Lexer) GQL_Lexer.getConstructor(CharStream.class).newInstance(CharStreams.fromString(input)); - CommonTokenStream tokens = new CommonTokenStream(lexer); - Parser parser = (Parser) GQL_Parser.getConstructor(TokenStream.class).newInstance(tokens); - Method startRule = parser.getClass().getMethod("program"); // Assuming 'program' is the start rule - ParseTree tree = (ParseTree) startRule.invoke(parser); - - PrintVisitor visitor = new PrintVisitor(parser.getRuleNames()); - String syntaxTree = visitor.visit(tree); - System.out.println(syntaxTree); - } catch (Exception e) { - e.printStackTrace(); - } - } -} diff --git a/developer/javac/SyntaxTree_Arithmetic.java b/developer/javac/SyntaxTree_Arithmetic.java deleted file mode 100644 index f841c31..0000000 --- a/developer/javac/SyntaxTree_Arithmetic.java +++ /dev/null @@ -1,37 +0,0 @@ -import org.antlr.v4.runtime.*; -import org.antlr.v4.runtime.tree.*; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.lang.reflect.Method; - -public class SyntaxTree_Arithmetic { - - // Versioned Lexer and Parser - static final Class GQL_Lexer = ArithmeticLexer.class; - static final Class GQL_Parser = ArithmeticParser.class; - - public static void main(String[] args) throws IOException { - if (args.length != 1) { - System.err.println("Usage: java SyntaxTree "); - System.exit(1); - } - - String inputFile = args[0]; - String input = new String(Files.readAllBytes(Paths.get(inputFile))); - - try { - Lexer lexer = (Lexer) GQL_Lexer.getConstructor(CharStream.class).newInstance(CharStreams.fromString(input)); - CommonTokenStream tokens = new CommonTokenStream(lexer); - Parser parser = (Parser) GQL_Parser.getConstructor(TokenStream.class).newInstance(tokens); - Method startRule = parser.getClass().getMethod("program"); // Assuming 'program' is the start rule - ParseTree tree = (ParseTree) startRule.invoke(parser); - - PrintVisitor visitor = new PrintVisitor(parser.getRuleNames()); - String syntaxTree = visitor.visit(tree); - System.out.println(syntaxTree); - } catch (Exception e) { - e.printStackTrace(); - } - } -} diff --git a/developer/javac/Test__SyntaxTree_Arithmetic.java b/developer/javac/Test__SyntaxTree_Arithmetic.java deleted file mode 100644 index 4b475f0..0000000 --- a/developer/javac/Test__SyntaxTree_Arithmetic.java +++ /dev/null @@ -1,94 +0,0 @@ -import org.antlr.v4.runtime.*; -import org.antlr.v4.runtime.tree.*; -import java.lang.reflect.Method; -import java.util.HashMap; -import java.util.Map; - -public class Test__SyntaxTree_Arithmetic { - - public static boolean PrintVisitor_0() { - // Simple smoke test - try { - String input = "3 + 5"; - String expectedOutput = "(3 + 5)"; - String actualOutput = runSyntaxTree(input); - return expectedOutput.equals(actualOutput); - } catch (Exception e) { - return false; - } - } - - public static boolean PrintVisitor_1() { - // Test with multiplication - try { - String input = "2 * 3"; - String expectedOutput = "(2 * 3)"; - String actualOutput = runSyntaxTree(input); - return expectedOutput.equals(actualOutput); - } catch (Exception e) { - return false; - } - } - - public static boolean PrintVisitor_2() { - // Test with parentheses - try { - String input = "2 * (3 + 4)"; - String expectedOutput = "(2 * (3 + 4))"; - String actualOutput = runSyntaxTree(input); - return expectedOutput.equals(actualOutput); - } catch (Exception e) { - return false; - } - } - - public static boolean PrintVisitor_3() { - // Test with division - try { - String input = "10 / 2"; - String expectedOutput = "(10 / 2)"; - String actualOutput = runSyntaxTree(input); - return expectedOutput.equals(actualOutput); - } catch (Exception e) { - return false; - } - } - - public static boolean PrintVisitor_4() { - // Test with complex expression - try { - String input = "3 + 5 * (10 - 4)"; - String expectedOutput = "(3 + (5 * (10 - 4)))"; - String actualOutput = runSyntaxTree(input); - // Uncomment the print statement for debugging - // System.out.println("PrintVisitor_4 - Expected: " + expectedOutput + ", Actual: " + actualOutput); - return expectedOutput.equals(actualOutput); - } catch (Exception e) { - return false; - } - } - - - - private static String runSyntaxTree(String input) throws Exception { - Lexer lexer = new ArithmeticLexer(CharStreams.fromString(input)); - CommonTokenStream tokens = new CommonTokenStream(lexer); - Parser parser = new ArithmeticParser(tokens); - Method startRule = parser.getClass().getMethod("program"); - ParseTree tree = (ParseTree) startRule.invoke(parser); - - PrintVisitor visitor = new PrintVisitor(parser.getRuleNames()); - return visitor.visit(tree); - } - - public static void main(String[] args) { - Map tests = new HashMap<>(); - tests.put("PrintVisitor_0", PrintVisitor_0()); - tests.put("PrintVisitor_1", PrintVisitor_1()); - tests.put("PrintVisitor_2", PrintVisitor_2()); - tests.put("PrintVisitor_3", PrintVisitor_3()); - tests.put("PrintVisitor_4", PrintVisitor_4()); - - TestBench.runTests(tests); - } -} diff --git a/developer/makefile b/developer/makefile deleted file mode 100644 index 9aa6991..0000000 --- a/developer/makefile +++ /dev/null @@ -1,103 +0,0 @@ -#================================================================================ -# Make targets -# - -all: setup $(PROGRAM_FPL) - - -PrintRuleNameList: $(PROGRAM_PrintRuleNameList) - - -SyntaxTree_Arithmetic: $(ANTLR_OUT_Arithmetic_FPL) - @if [ -z "$(ANTLR_OUT_Arithmetic_FPL)" ]; then \ - echo "variable ANTLR_OUT_Arithmetic_FPL empty."; \ - exit 1; \ - fi - make $(PROGRAM_SyntaxTree_Arithmetic) - - -Test__SyntaxTree_Arithmetic: $(ANTLR_OUT_Arithmetic_FPL) - @if [ -z "$(ANTLR_OUT_Arithmetic_FPL)" ]; then \ - echo "variable ANTLR_OUT_Arithmetic_FPL empty."; \ - exit 1; \ - fi - make $(PROGRAM_Test__SyntaxTree_Arithmetic) - - -SyntaxTree_20240412: $(ANTLR_OUT_GQL_20240412_FPL) - @if [ -z "$(ANTLR_OUT_GQL_20240412_FPL)" ]; then \ - echo "variable ANTLR_OUT_GQL_20240412_FPL empty."; \ - exit 1; \ - fi - make $(PROGRAM_SyntaxTree_20240412) - - -# make the `.java` files for all grammars -grammar: setup $(ANTLR_OUT_FPL) - -# Compile all the .java files. -java: setup $(JAVA_COMP_OUT_FPL) - -# Specific grammar targets. Run them like this: -# > make -# e.g. > make GQL_test -$(foreach grammar,$(ANTLR_GRAMMAR_LIST),$(eval $(grammar): $(value ANTLR_OUT_$(grammar)_FPL))) - -.PHONY: version -version: - $(info ANTLR_JAR is '$(notdir $(ANTLR_JAR))') - @ $(JAVA_COMP) --version - @ $(JAVA_ARCHIVE) --version - @ make -v | head -n 1 - @ echo "makefile 0.2" - -.PHONY: setup -# ANTLR automatically creates $(ANTLR_OUT_DIR) -setup: - mkdir -p $(ANTLR_IN_DIR) $(JAVA_COMP_IN_PRIMARY_DIR) $(JVM_IN_DIR) - mkdir -p executor test deprecated experiment documentation temporary - -# Default clean target -.PHONY: setup -clean: - @echo "Use the clean script from the executor directory instead of \`make clean\`" - @executor/clean - -#useful for distinguishing initial make error messages and message generated by rules firing -nothing: - @: - -#================================================================================ -# recipes - - -$(ANTLR_OUT_DIR)/%Lexer.java \ -$(ANTLR_OUT_DIR)/%Parser.java \ -$(ANTLR_OUT_DIR)/%BaseVisitor.java \ -$(ANTLR_OUT_DIR)/%Visitor.java: $(ANTLR_IN_PRIMARY_FPL) - @echo "making grammar from:" $< - $(JAVA_INTERP) -jar $(ANTLR_JAR) -Dlanguage=Java -visitor -o $(ANTLR_OUT_DIR_PARENT) $< - -# Rule to build .class files from .java files -$(JAVA_COMP_OUT_DIR)/%.class: $(JAVA_COMP_IN_PRIMARY_DIR)/%.java - @echo "Compiling $<..." - $(JAVA_COMP) -d $(JAVA_COMP_OUT_DIR) -sourcepath $(JAVA_COMP_IN_DL) $< - @echo "Created $@" - - -# Without this, GNU make inserts an 'rm -rf' not on the recipe and deletes the .jar files -# after making them. Why make why? -.PRECIOUS: $(JAVA_COMP_OUT_DIR)/%.jar - -# Rule to build .jar files from .class files -$(JAVA_COMP_OUT_DIR)/%.jar: $(JAVA_COMP_OUT_DIR)/%.class - @echo "Building $*..." - $(JAVA_ARCHIVE) cf $@ -C $(JAVA_COMP_OUT_DIR) $*.class - @echo "Created $@" - -executor/%: $(JAVA_COMP_OUT_DIR)/%.jar - @echo "Creating script for $*..." - @echo "#!/usr/bin/env bash" > executor/$* - @echo "$(JAVA_INTERP) -cp ${CLASSPATH}:$(JAVA_COMP_OUT_DIR):$< $*" \$$\@ >> executor/$* - chmod +x executor/$* - @echo "Created script executor/$*"