--- /dev/null
+#!/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 $@
--- /dev/null
+#!/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 $@
--- /dev/null
+#!/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 $@
# 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
#
echo "Usage: clean <all [<program_name>] | program [<program_name>] | grammar [<name>] | 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
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
}
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"
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")
:
;;
"grammar")
# Grammar name is $arg.
- clean_grammar "$arg"
+ ${EXECUTOR_IN_DIR}/clean_grammar "$arg"
;;
*)
echo "Unknown clean command: $command"
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 $@
#!/usr/bin/env bash
+# helper function for `clean`, though can be called directly
if [ -z "$1" ]; then
echo "Error: Directory argument is empty"
#!/usr/bin/env bash
+# helper function for `clean`, though can be called directly
if [ -z "$1" ]; then
exit 0
#!/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
# 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
--- /dev/null
+#!/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
# _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
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
#
# 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
#!/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 $@
--- /dev/null
+#================================================================================
+# 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 <grammar_name>
+# 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)/$*"
--- /dev/null
+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 <input-file>");
+ 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();
+ }
+ }
+}
--- /dev/null
+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 <input-file>");
+ 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();
+ }
+ }
+}
--- /dev/null
+import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
+
+public class Arithmetic_Echo_PrintVisitor extends ArithmeticBaseVisitor<String> {
+ 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 + ")";
+ }
+ }
+
+}
+
--- /dev/null
+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<String, Boolean> 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);
+ }
+}
--- /dev/null
+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 <input-file>");
+ 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();
+ }
+ }
+}
--- /dev/null
+import org.antlr.v4.runtime.tree.AbstractParseSyntaxVisitor;
+
+public class Arithmetic_Syntax_PrintVisitor extends ArithmeticBaseVisitor<String> {
+ 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 + ")";
+ }
+ }
+}
+++ /dev/null
-import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
-
-public class PrintVisitor extends ArithmeticBaseVisitor<String> {
- 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 + ")";
- }
- }
-
-}
-
-import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
+import org.antlr.v4.runtime.tree.AbstractParseSyntaxVisitor;
-public class PrintVisitor extends AbstractParseTreeVisitor<String> implements ArithmeticVisitor<String> {
+public class PrintVisitor extends AbstractParseSyntaxVisitor<String> implements ArithmeticVisitor<String> {
private final String[] ruleNames;
public PrintVisitor(String[] ruleNames) {
+++ /dev/null
-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 <input-file>");
- 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();
- }
- }
-}
+++ /dev/null
-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 <input-file>");
- 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();
- }
- }
-}
+++ /dev/null
-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<String, Boolean> 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);
- }
-}
+++ /dev/null
-#================================================================================
-# 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 <grammar_name>
-# 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/$*"