--- /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/developer/jvm:/var/user_data/Thomas-developer/GQL_to_Cypher/tool/executor/antlr-4.11.1-complete.jar:/var/user_data/Thomas-developer/GQL_to_Cypher/developer/jvm:/var/user_data/Thomas-developer/GQL_to_Cypher/tool/executor/antlr-4.11.1-complete.jar:/var/user_data/Thomas-developer/GQL_to_Cypher/developer/jvm:/var/user_data/Thomas-developer/GQL_to_Cypher/tool/executor/antlr-4.11.1-complete.jar:/var/user_data/Thomas-developer/GQL_to_Cypher/developer/jvm:/var/user_data/Thomas-developer/GQL_to_Cypher/tool/executor/antlr-4.11.1-complete.jar:/var/user_data/Thomas-developer/GQL_to_Cypher/developer/jvm:/var/user_data/Thomas-developer/GQL_to_Cypher/tool/executor/antlr-4.11.1-complete.jar:/var/user_data/Thomas-developer/GQL_to_Cypher/developer/jvm:/var/user_data/Thomas-developer/GQL_to_Cypher/developer/jvm/GeneratePrintVisitor.jar GeneratePrintVisitor $@
--- /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/developer/jvm:/var/user_data/Thomas-developer/GQL_to_Cypher/tool/executor/antlr-4.11.1-complete.jar:/var/user_data/Thomas-developer/GQL_to_Cypher/developer/jvm:/var/user_data/Thomas-developer/GQL_to_Cypher/tool/executor/antlr-4.11.1-complete.jar:/var/user_data/Thomas-developer/GQL_to_Cypher/developer/jvm:/var/user_data/Thomas-developer/GQL_to_Cypher/tool/executor/antlr-4.11.1-complete.jar:/var/user_data/Thomas-developer/GQL_to_Cypher/developer/jvm:/var/user_data/Thomas-developer/GQL_to_Cypher/tool/executor/antlr-4.11.1-complete.jar:/var/user_data/Thomas-developer/GQL_to_Cypher/developer/jvm:/var/user_data/Thomas-developer/GQL_to_Cypher/tool/executor/antlr-4.11.1-complete.jar:/var/user_data/Thomas-developer/GQL_to_Cypher/developer/jvm:/var/user_data/Thomas-developer/GQL_to_Cypher/developer/jvm/GeneratePrintVisitorMethod.jar GeneratePrintVisitorMethod $@
export EXECUTOR_IN_FL="\
RuleNameListRegx\
RuleNameList\
+ GeneratePrintVisitorMethod\
+ GeneratePrintVisitor\
ANTLRv4_RuleNameList\
Arithmetic_Echo\
Arithmetic_Echo__Test\
export JAR_OUT_FPL
-# LocalWords: ANTLRv PrintRuleNameListRegx
+# LocalWords: ANTLRv PrintRuleNameListRegx RuleNameListRegx RuleNameList
#--------------------------------------------------------------------------------
-# Project build
+# Project build
#
+# GNU makefile
# turn off implicit rules, because they can do unexpected things.
.SUFFIXES:
MAKEFLAGS += -r
-# Turns off the "feature" where `make` tries to make its makefiles as file targets. There
-# is no command line switch to turn this off.
+# `make` considers that PHONY targets do not correspond to files (other
+# targets do and the files have their dates checked). This declaration renders
+# innocuous the "feature" where `make` always tries to make its own
+# makefiles. There is no command line switch to turn this "feature" off.
.PHONY: $(MAKEFILE_LIST)
# 'make' has a "feature" where it deletes what it determines to be intermediate
-# files. There is no command line switch to turn this behavior off. Combine
-# this feature with implicit rules to have loads of fun. At least this prevents
-# make from deleting its makefiles if it happens to decide one is an
-# intermediate file.
+# files. There is no command line switch to turn this "feature" off. Combine
+# this "feature" with implicit rules and have loads of fun.
.PRECIOUS: $(MAKEFILE_LIST)
#$(info makefile: $(MAKEFILE_LIST))
#-----------------------------------------------
-# A utility for viewing all the rules in a grammar
+# These do not require antlr generated java files
RuleNameListRegx: $(EXECUTOR_IN_DIR)/RuleNameListRegx
RuleNameList: $(EXECUTOR_IN_DIR)/RuleNameList
+GeneratePrintVisitorMethod: $(EXECUTOR_IN_DIR)/GeneratePrintVisitorMethod
+GeneratePrintVisitor: $(EXECUTOR_IN_DIR)/GeneratePrintVisitor GeneratePrintVisitorMethod
+
+
+
#-----------------------------------------------
# Arithmetic
$(REMAKE) $(EXECUTOR_IN_DIR)/ANTLRv4_Syntax
-
#-----------------------------------------------
# GQL_20240412
#================================================================================
# Build the tools that are needed for building the project.
#
+# GNU makefile
# turn off implicit rules, because they can do unexpected things.
.SUFFIXES:
MAKEFLAGS += -r
-# Turns off the "feature" where `make` tries to make its makefiles as file targets. There
-# is no command line switch to turn this off.
+# `make` considers that PHONY targets do not correspond to files (other
+# targets do and the files have their dates checked). This declaration renders
+# innocuous the "feature" where `make` always tries to make its own
+# makefiles. There is no command line switch to turn this "feature" off.
.PHONY: $(MAKEFILE_LIST)
# 'make' has a "feature" where it deletes what it determines to be intermediate
-# files. There is no command line switch to turn this behavior off. Combine
-# this feature with implicit rules to have loads of fun. At least this prevents
-# make from deleting its makefiles if it happens to decide one is an
-# intermediate file.
+# files. There is no command line switch to turn this "feature" off. Combine
+# this "feature" with implicit rules and have loads of fun.
.PRECIOUS: $(MAKEFILE_LIST)
#$(info makefile: $(MAKEFILE_LIST))
#================================================================================
# top level makefile calls makefile-tool and makefile-project
#
+# GNU makefile
# turn off implicit rules, because they can do unexpected things.
.SUFFIXES:
MAKEFLAGS += -r
-# Turns off the "feature" where `make` tries to make its makefiles as file targets. There
-# is no command line switch to turn this off.
+# `make` considers that PHONY targets do not correspond to files (other
+# targets do and the files have their dates checked). This declaration renders
+# innocuous the "feature" where `make` always tries to make its own
+# makefiles. There is no command line switch to turn this "feature" off.
.PHONY: $(MAKEFILE_LIST)
# 'make' has a "feature" where it deletes what it determines to be intermediate
-# files. There is no command line switch to turn this behavior off. Combine
-# this feature with implicit rules to have loads of fun. At least this prevents
-# make from deleting its makefiles if it happens to decide one is an
-# intermediate file.
+# files. There is no command line switch to turn this "feature" off. Combine
+# this "feature" with implicit rules and have loads of fun.
.PRECIOUS: $(MAKEFILE_LIST)
#$(info makefile: $(MAKEFILE_LIST))
boolean pretty_print = false;
List<String> file_arg_list = new ArrayList<>();
boolean has_error = false;
+ String usage = "Usage: Arithmetic_Syntax [-pp] <source-file>"
// Parse the options and arguments
for (String arg : arg_array) {
// If there were any errors, print usage and exit
if (has_error) {
- System.err.println("Usage: java Arithmetic_Syntax [-pp] <input-file>");
+ System.err.println(usage);
System.exit(1);
}
// Ensure there is exactly one input file
if (file_arg_list.size() != 1) {
- System.err.println("Usage: java Arithmetic_Syntax [-pp] <input-file>");
+ System.err.println("usage");
System.exit(1);
}
--- /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.util.List;
+import java.io.PrintWriter;
+
+public class GeneratePrintVisitor {
+
+ public static void main(String[] args) throws IOException {
+ if (args.length != 2) {
+ System.err.println("Usage: java GeneratePrintVisitor <grammarFile> <outputFile>");
+ System.exit(1);
+ }
+
+ String grammarFile = args[0];
+ String outputFile = args[1];
+
+ // Extract the grammar name from the file name
+ String grammarName = Paths.get(grammarFile).getFileName().toString().replace(".g4", "");
+ String parserName = grammarName + "Parser";
+
+ // Parse the .g4 file
+ CharStream input = CharStreams.fromFileName(grammarFile);
+ ANTLRv4Lexer lexer = new ANTLRv4Lexer(input);
+ CommonTokenStream tokens = new CommonTokenStream(lexer);
+ ANTLRv4Parser parser = new ANTLRv4Parser(tokens);
+
+ // Extract rules
+ ParseTree tree = parser.grammarSpec();
+ List<String> ruleNames = extractRuleNames(parser);
+
+ // Template for the PrintVisitor class
+ String classTemplate = """
+ import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
+
+ public class PrintVisitor extends AbstractParseTreeVisitor<String> {
+ private final String[] ruleNames;
+
+ public PrintVisitor(String[] ruleNames) {
+ this.ruleNames = ruleNames;
+ }
+
+ // Generated print methods
+ """;
+
+ // Generate and output the PrintVisitor class
+ try (PrintWriter writer = new PrintWriter(outputFile)) {
+ // Write the class template
+ writer.print(classTemplate);
+
+ // Generate and write the print methods
+ for (String ruleName : ruleNames) {
+ GeneratePrintVisitorMethod.generatePrintMethod(parserName, ruleName, writer);
+ }
+
+ // Close the class
+ writer.println("}");
+ }
+ }
+
+ private static List<String> extractRuleNames(Parser parser) {
+ // Extract rule names from the parser
+ return List.of(parser.getRuleNames());
+ }
+}
--- /dev/null
+import java.io.PrintWriter;
+
+public class GeneratePrintVisitorMethod {
+
+ public static void main(String[] args) {
+ if (args.length != 3) {
+ System.err.println("Usage: GeneratePrintVisitorMethod <parserName> <ruleName> <outputFile>");
+ System.exit(1);
+ }
+
+ String parserName = args[0];
+ String ruleName = args[1];
+ String outputFile = args[2];
+
+ try (PrintWriter writer = new PrintWriter(outputFile)) {
+ generatePrintMethod(parserName, ruleName, writer);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ public static void generatePrintMethod(String parserName, String ruleName, PrintWriter writer) {
+ // Template for the print method using text blocks
+ String template = """
+ public String visit _______0_ (_______1_. _______0_ Context ctx) {
+ StringBuilder result = new StringBuilder();
+ result.append("_______0_(");
+ for (int i = 0; i < ctx.getChildCount(); i++) {
+ if (i > 0) result.append(", ");
+ result.append(visit(ctx.getChild(i)));
+ }
+ result.append(")");
+ return result.toString();
+ }
+ """;
+
+ // Fill in the blanks in the template
+ template = template.replace("_______0_", ruleName);
+ template = template.replace("_______1_", parserName);
+
+ // Write the template to the output file
+ writer.print(template);
+ }
+}