#note also the .gitignore files in the tool and developer directories and any
# other directory that might have one.
+#*#
*~
a.out
+++ /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 $@
/*
-Run the command with no arguments for a usage message.
+ Run the command with no arguments for a usage message.
-Accepts an antlr grammar file name of the form: [path/]<name>[Lexer/Parser][.g4]
+ Accepts an antlr grammar file name of the form: [path/]<name>[Lexer/Parser][.g4]
-Prints a space-separated list of files antlr4 would output.
+ Prints a space-separated list of files antlr4 would output.
-The <name>Lexer or <name>Parser suffix, or absence thereof, tell this program if
-antlr would create lexer and parser files, or both.
+ The <name>Lexer or <name>Parser suffix, or absence thereof, tell this program if
+ antlr would create lexer and parser files, or both.
*/
public class ANTLR_OUT_FL {
- // Constant for the usage message
- private static final String USAGE_MESSAGE = "Usage: java ANTLR_OUT_FL <grammar-file> " +
- "[-visitor (default)] [-no-visitor] " +
- "[-listener] [-no-listener (default)] " +
- "[-tokens] [-no-tokens (default)] " +
- "[-path <path>]";
-
- public static void main(String[] args) {
- if (args.length == 0) {
- System.err.println(USAGE_MESSAGE);
- System.exit(1);
- }
+ // Constant for the usage message
+ private static final String USAGE_MESSAGE = "Usage: java ANTLR_OUT_FL <grammar-file> " +
+ "[-visitor (default)] [-no-visitor] " +
+ "[-listener] [-no-listener (default)] " +
+ "[-tokens] [-no-tokens (default)] " +
+ "[-path <path>]";
+
+ public static void main(String[] args) {
+ if (args.length == 0) {
+ System.err.println(USAGE_MESSAGE);
+ System.exit(1);
+ }
- // Defaults
- boolean visitor = true;
- boolean noListener = true;
- boolean noTokens = true;
- String outputPath = ""; // Default empty path
- List<String> argList = new ArrayList<>();
-
- // Parse the arguments
- for (int i = 0; i < args.length; i++) {
- String arg = args[i];
- if (arg.startsWith("-")) {
- switch (arg) {
- case "-visitor":
- visitor = true;
- break;
- case "-no-visitor":
- visitor = false;
- break;
- case "-listener":
- noListener = false;
- break;
- case "-no-listener":
- noListener = true;
- break;
- case "-tokens":
- noTokens = false;
- break;
- case "-no-tokens":
- noTokens = true;
- break;
- case "-path":
- // Ensure the next argument exists and isn't another option
- if (i + 1 < args.length && !args[i + 1].startsWith("-")) {
- outputPath = args[++i]; // Get the next argument as the path
- if (!outputPath.endsWith("/")) {
- outputPath += "/"; // Ensure the path ends with a slash
- }
- } else {
- System.err.println(USAGE_MESSAGE);
- System.exit(1);
- }
- break;
- default:
- System.err.println("Unrecognized option: " + arg);
- System.err.println(USAGE_MESSAGE);
- System.exit(1);
- }
- } else {
- argList.add(arg);
+ // Defaults
+ boolean visitor = true;
+ boolean noListener = true;
+ boolean noTokens = true;
+ String outputPath = ""; // Default empty path
+ List<String> argList = new ArrayList<>();
+
+ // Parse the arguments
+ for (int i = 0; i < args.length; i++) {
+ String arg = args[i];
+ if (arg.startsWith("-")) {
+ switch (arg) {
+ case "-visitor":
+ visitor = true;
+ break;
+ case "-no-visitor":
+ visitor = false;
+ break;
+ case "-listener":
+ noListener = false;
+ break;
+ case "-no-listener":
+ noListener = true;
+ break;
+ case "-tokens":
+ noTokens = false;
+ break;
+ case "-no-tokens":
+ noTokens = true;
+ break;
+ case "-path":
+ // Ensure the next argument exists and isn't another option
+ if (i + 1 < args.length && !args[i + 1].startsWith("-")) {
+ outputPath = args[++i]; // Get the next argument as the path
+ if (!outputPath.endsWith("/")) {
+ outputPath += "/"; // Ensure the path ends with a slash
}
- }
-
- // Ensure there is exactly one grammar file argument
- if (argList.size() != 1) {
+ } else {
System.err.println(USAGE_MESSAGE);
System.exit(1);
+ }
+ break;
+ default:
+ System.err.println("Unrecognized option: " + arg);
+ System.err.println(USAGE_MESSAGE);
+ System.exit(1);
}
-
- String grammarFile = argList.get(0);
-
- List<String> generatedFiles = generateFileList(grammarFile, visitor, noListener, noTokens, outputPath);
-
- // Print the files in a space-separated format on a single line
- if (!generatedFiles.isEmpty()) {
- System.out.print(generatedFiles.get(0)); // Print the first file
- for (int i = 1; i < generatedFiles.size(); i++) {
- System.out.print(" " + generatedFiles.get(i)); // Print space and each subsequent file
- }
- }
- System.out.println(); // Print a newline at the end
+ } else {
+ argList.add(arg);
+ }
}
- public static List<String> generateFileList(String grammarFile, boolean visitor, boolean noListener, boolean noTokens, String outputPath) {
- String baseName = new File(grammarFile).getName().replace(".g4", "");
- List<String> fileList = new ArrayList<>();
-
- // Determine if this is a lexer, parser, or both
- boolean isLexer = baseName.endsWith("Lexer");
- boolean isParser = baseName.endsWith("Parser");
- boolean isCombined = !isLexer && !isParser;
-
- if (isLexer || isCombined) {
- // Lexer files
- fileList.add(outputPath + baseName + "Lexer.java");
- if (!noTokens) fileList.add(outputPath + baseName + "Lexer.tokens");
- }
+ // Ensure there is exactly one grammar file argument
+ if (argList.size() != 1) {
+ System.err.println(USAGE_MESSAGE);
+ System.exit(1);
+ }
- if (isParser || isCombined) {
- // Parser files
- fileList.add(outputPath + baseName + "Parser.java");
- if (!noTokens) fileList.add(outputPath + baseName + ".tokens");
+ String grammarFile = argList.get(0);
- // Listener-related files
- if (!noListener) {
- fileList.add(outputPath + baseName + "Listener.java");
- fileList.add(outputPath + baseName + "BaseListener.java");
- }
+ List<String> generatedFiles = generateFileList(grammarFile, visitor, noListener, noTokens, outputPath);
- // Visitor-related files
- if (visitor) {
- fileList.add(outputPath + baseName + "Visitor.java");
- fileList.add(outputPath + baseName + "BaseVisitor.java");
- }
- }
+ // Print the files in a space-separated format on a single line
+ if (!generatedFiles.isEmpty()) {
+ System.out.print(generatedFiles.get(0)); // Print the first file
+ for (int i = 1; i < generatedFiles.size(); i++) {
+ System.out.print(" " + generatedFiles.get(i)); // Print space and each subsequent file
+ }
+ }
+ System.out.println(); // Print a newline at the end
+ }
+
+ public static List<String> generateFileList(String grammarFile, boolean visitor, boolean noListener, boolean noTokens, String outputPath) {
+ String baseName = new File(grammarFile).getName().replace(".g4", "");
+ List<String> fileList = new ArrayList<>();
+
+ // Determine if this is a lexer, parser, or both
+ boolean isLexer = baseName.endsWith("Lexer");
+ boolean isParser = baseName.endsWith("Parser");
+ boolean isCombined = !isLexer && !isParser;
+
+ if (isLexer || isCombined) {
+ // Lexer files
+ fileList.add(outputPath + baseName + "Lexer.java");
+ if (!noTokens) fileList.add(outputPath + baseName + "Lexer.tokens");
+ }
- return fileList;
+ if (isParser || isCombined) {
+ // Parser files
+ fileList.add(outputPath + baseName + "Parser.java");
+ if (!noTokens) fileList.add(outputPath + baseName + ".tokens");
+
+ // Listener-related files
+ if (!noListener) {
+ fileList.add(outputPath + baseName + "Listener.java");
+ fileList.add(outputPath + baseName + "BaseListener.java");
+ }
+
+ // Visitor-related files
+ if (visitor) {
+ fileList.add(outputPath + baseName + "Visitor.java");
+ fileList.add(outputPath + baseName + "BaseVisitor.java");
+ }
}
+
+ return fileList;
+ }
}
+++ /dev/null
-/*
-Takes an 'ANTLRv4' grammar source file. Parses it. Outputs an annotated
-version of the source file while labeling what parts of the grammar the syntax
-objects belong to. Note the -pp option.
-
-*/
-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.ArrayList;
-import java.util.List;
-
-public class ANTLRv4_Syntax {
-
- public static void main(String[] arg_array) throws IOException {
- boolean pretty_print = false;
- List<String> file_arg_list = new ArrayList<>();
- boolean has_error = false;
-
- // Parse the options and arguments
- for (String arg : arg_array) {
- if (arg.startsWith("-")) {
- if (arg.equals("-pp")) {
- pretty_print = true;
- } else {
- System.err.println("Unrecognized option: " + arg);
- has_error = true;
- }
- } else {
- file_arg_list.add(arg);
- }
- }
-
- // If there were any errors, print usage and exit
- if (has_error) {
- System.err.println("Usage: java ANTLRv4_Syntax [-pp] <input-file>");
- System.exit(1);
- }
-
- // Ensure there is exactly one input file
- if (file_arg_list.size() != 1) {
- System.err.println("Usage: java ANTLRv4_Syntax [-pp] <input-file>");
- System.exit(1);
- }
-
- String input_file = file_arg_list.get(0);
- String input = Files.readString(Paths.get(input_file));
-
- try {
- ANTLRv4Lexer lexer = new ANTLRv4Lexer(CharStreams.fromString(input));
- CommonTokenStream tokens = new CommonTokenStream(lexer);
- ANTLRv4Parser parser = new ANTLRv4Parser(tokens);
- ParseTree tree = parser.grammarSpec();
-
- ANTLRv4_Syntax_PrintVisitor visitor = new ANTLRv4_Syntax_PrintVisitor(parser.getRuleNames(), pretty_print);
- 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 ANTLRv4_Syntax_PrintVisitor extends ANTLRv4BaseVisitor<String>{
-
- private final String[] rule_names;
- private final boolean pretty_print;
-
- public ANTLRv4_Syntax_PrintVisitor(
-String[] rule_names, boolean pretty_print)
-{
-
- this.rule_names = rule_names;
- this.pretty_print = pretty_print;
- }
-
-
- private String indent(
-int level)
-{
-
- return " ".repeat(
-level)
-;
- }
-
-
- @Override
- public Void visitgrammarSpec
- (
-ANTLRv4Parser.grammarSpec
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("grammarSpec
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitgrammarDecl
- (ANTLRv4Parser.grammarDecl
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"grammarDecl
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitgrammarType
- (
-ANTLRv4Parser.grammarType
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("grammarType
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitprequelConstruct
- (ANTLRv4Parser.prequelConstruct
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"prequelConstruct
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitoptionsSpec
- (
-ANTLRv4Parser.optionsSpec
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("optionsSpec
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitoption
- (ANTLRv4Parser.option
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"option
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitoptionValue
- (
-ANTLRv4Parser.optionValue
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("optionValue
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitdelegateGrammars
- (ANTLRv4Parser.delegateGrammars
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"delegateGrammars
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitdelegateGrammar
- (
-ANTLRv4Parser.delegateGrammar
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("delegateGrammar
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visittokensSpec
- (ANTLRv4Parser.tokensSpec
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"tokensSpec
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitchannelsSpec
- (
-ANTLRv4Parser.channelsSpec
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("channelsSpec
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitidList
- (ANTLRv4Parser.idList
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"idList
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitaction_
- (
-ANTLRv4Parser.action_
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("action_
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitactionScopeName
- (ANTLRv4Parser.actionScopeName
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"actionScopeName
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitactionBlock
- (
-ANTLRv4Parser.actionBlock
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("actionBlock
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitargActionBlock
- (ANTLRv4Parser.argActionBlock
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"argActionBlock
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitmodeSpec
- (
-ANTLRv4Parser.modeSpec
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("modeSpec
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitrules
- (ANTLRv4Parser.rules
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"rules
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitruleSpec
- (
-ANTLRv4Parser.ruleSpec
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("ruleSpec
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitparserRuleSpec
- (ANTLRv4Parser.parserRuleSpec
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"parserRuleSpec
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitexceptionGroup
- (
-ANTLRv4Parser.exceptionGroup
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("exceptionGroup
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitexceptionHandler
- (ANTLRv4Parser.exceptionHandler
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"exceptionHandler
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitfinallyClause
- (
-ANTLRv4Parser.finallyClause
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("finallyClause
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitrulePrequel
- (ANTLRv4Parser.rulePrequel
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"rulePrequel
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitruleReturns
- (
-ANTLRv4Parser.ruleReturns
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("ruleReturns
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitthrowsSpec
- (ANTLRv4Parser.throwsSpec
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"throwsSpec
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitlocalsSpec
- (
-ANTLRv4Parser.localsSpec
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("localsSpec
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitruleAction
- (ANTLRv4Parser.ruleAction
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"ruleAction
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitruleModifiers
- (
-ANTLRv4Parser.ruleModifiers
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("ruleModifiers
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitruleModifier
- (ANTLRv4Parser.ruleModifier
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"ruleModifier
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitruleBlock
- (
-ANTLRv4Parser.ruleBlock
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("ruleBlock
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitruleAltList
- (ANTLRv4Parser.ruleAltList
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"ruleAltList
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitlabeledAlt
- (
-ANTLRv4Parser.labeledAlt
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("labeledAlt
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitlexerRuleSpec
- (ANTLRv4Parser.lexerRuleSpec
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"lexerRuleSpec
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitlexerRuleBlock
- (
-ANTLRv4Parser.lexerRuleBlock
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("lexerRuleBlock
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitlexerAltList
- (ANTLRv4Parser.lexerAltList
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"lexerAltList
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitlexerAlt
- (
-ANTLRv4Parser.lexerAlt
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("lexerAlt
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitlexerElements
- (ANTLRv4Parser.lexerElements
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"lexerElements
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitlexerElement
- (
-ANTLRv4Parser.lexerElement
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("lexerElement
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitlexerBlock
- (ANTLRv4Parser.lexerBlock
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"lexerBlock
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitlexerCommands
- (
-ANTLRv4Parser.lexerCommands
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("lexerCommands
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitlexerCommand
- (ANTLRv4Parser.lexerCommand
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"lexerCommand
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitlexerCommandName
- (
-ANTLRv4Parser.lexerCommandName
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("lexerCommandName
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitlexerCommandExpr
- (ANTLRv4Parser.lexerCommandExpr
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"lexerCommandExpr
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitaltList
- (
-ANTLRv4Parser.altList
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("altList
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitalternative
- (ANTLRv4Parser.alternative
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"alternative
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitelement
- (
-ANTLRv4Parser.element
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("element
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitpredicateOptions
- (ANTLRv4Parser.predicateOptions
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"predicateOptions
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitpredicateOption
- (
-ANTLRv4Parser.predicateOption
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("predicateOption
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitlabeledElement
- (ANTLRv4Parser.labeledElement
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"labeledElement
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitebnf
- (
-ANTLRv4Parser.ebnf
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("ebnf
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitblockSuffix
- (ANTLRv4Parser.blockSuffix
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"blockSuffix
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitebnfSuffix
- (
-ANTLRv4Parser.ebnfSuffix
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("ebnfSuffix
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitlexerAtom
- (ANTLRv4Parser.lexerAtom
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"lexerAtom
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitatom
- (
-ANTLRv4Parser.atom
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("atom
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitnotSet
- (ANTLRv4Parser.notSet
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"notSet
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitblockSet
- (
-ANTLRv4Parser.blockSet
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("blockSet
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitsetElement
- (ANTLRv4Parser.setElement
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"setElement
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitblock
- (
-ANTLRv4Parser.block
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("block
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitruleref
- (ANTLRv4Parser.ruleref
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"ruleref
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitcharacterRange
- (
-ANTLRv4Parser.characterRange
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("characterRange
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitterminalDef
- (ANTLRv4Parser.terminalDef
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"terminalDef
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitelementOptions
- (
-ANTLRv4Parser.elementOptions
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("elementOptions
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitelementOption
- (ANTLRv4Parser.elementOption
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println(
-"elementOption
- ")
-;
- return visitChildren(
-ctx)
-;
- }
-}
-
- @Override
- public Void visitidentifier
- (
-ANTLRv4Parser.identifier
- Context ctx)
- {
-{
-
- // Logic to print the current rule's name
- System.out.println("identifier
- ");
- return visitChildren(ctx);
- }}
-
-}
+++ /dev/null
-
-
-import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
-
-public class ANTLRv4_Syntax_PrintVisitor extends ANTLRv4BaseVisitor<String>{
- private final String[] rule_names;
- private final boolean pretty_print;
-
- public ANTLRv4_Syntax_PrintVisitor(String[] rule_names, boolean pretty_print){
- this.rule_names = rule_names;
- this.pretty_print = pretty_print;
- }
-
- private String indent(int level){
- return " ".repeat(level);
- }
-
- @Override
- public Void visitgrammarSpec
- (ANTLRv4Parser.grammarSpec
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("grammarSpec
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitgrammarDecl
- (ANTLRv4Parser.grammarDecl
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("grammarDecl
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitgrammarType
- (ANTLRv4Parser.grammarType
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("grammarType
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitprequelConstruct
- (ANTLRv4Parser.prequelConstruct
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("prequelConstruct
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitoptionsSpec
- (ANTLRv4Parser.optionsSpec
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("optionsSpec
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitoption
- (ANTLRv4Parser.option
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("option
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitoptionValue
- (ANTLRv4Parser.optionValue
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("optionValue
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitdelegateGrammars
- (ANTLRv4Parser.delegateGrammars
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("delegateGrammars
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitdelegateGrammar
- (ANTLRv4Parser.delegateGrammar
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("delegateGrammar
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visittokensSpec
- (ANTLRv4Parser.tokensSpec
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("tokensSpec
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitchannelsSpec
- (ANTLRv4Parser.channelsSpec
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("channelsSpec
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitidList
- (ANTLRv4Parser.idList
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("idList
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitaction_
- (ANTLRv4Parser.action_
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("action_
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitactionScopeName
- (ANTLRv4Parser.actionScopeName
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("actionScopeName
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitactionBlock
- (ANTLRv4Parser.actionBlock
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("actionBlock
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitargActionBlock
- (ANTLRv4Parser.argActionBlock
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("argActionBlock
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitmodeSpec
- (ANTLRv4Parser.modeSpec
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("modeSpec
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitrules
- (ANTLRv4Parser.rules
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("rules
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitruleSpec
- (ANTLRv4Parser.ruleSpec
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("ruleSpec
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitparserRuleSpec
- (ANTLRv4Parser.parserRuleSpec
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("parserRuleSpec
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitexceptionGroup
- (ANTLRv4Parser.exceptionGroup
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("exceptionGroup
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitexceptionHandler
- (ANTLRv4Parser.exceptionHandler
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("exceptionHandler
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitfinallyClause
- (ANTLRv4Parser.finallyClause
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("finallyClause
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitrulePrequel
- (ANTLRv4Parser.rulePrequel
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("rulePrequel
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitruleReturns
- (ANTLRv4Parser.ruleReturns
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("ruleReturns
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitthrowsSpec
- (ANTLRv4Parser.throwsSpec
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("throwsSpec
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitlocalsSpec
- (ANTLRv4Parser.localsSpec
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("localsSpec
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitruleAction
- (ANTLRv4Parser.ruleAction
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("ruleAction
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitruleModifiers
- (ANTLRv4Parser.ruleModifiers
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("ruleModifiers
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitruleModifier
- (ANTLRv4Parser.ruleModifier
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("ruleModifier
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitruleBlock
- (ANTLRv4Parser.ruleBlock
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("ruleBlock
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitruleAltList
- (ANTLRv4Parser.ruleAltList
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("ruleAltList
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitlabeledAlt
- (ANTLRv4Parser.labeledAlt
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("labeledAlt
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitlexerRuleSpec
- (ANTLRv4Parser.lexerRuleSpec
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("lexerRuleSpec
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitlexerRuleBlock
- (ANTLRv4Parser.lexerRuleBlock
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("lexerRuleBlock
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitlexerAltList
- (ANTLRv4Parser.lexerAltList
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("lexerAltList
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitlexerAlt
- (ANTLRv4Parser.lexerAlt
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("lexerAlt
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitlexerElements
- (ANTLRv4Parser.lexerElements
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("lexerElements
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitlexerElement
- (ANTLRv4Parser.lexerElement
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("lexerElement
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitlexerBlock
- (ANTLRv4Parser.lexerBlock
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("lexerBlock
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitlexerCommands
- (ANTLRv4Parser.lexerCommands
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("lexerCommands
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitlexerCommand
- (ANTLRv4Parser.lexerCommand
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("lexerCommand
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitlexerCommandName
- (ANTLRv4Parser.lexerCommandName
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("lexerCommandName
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitlexerCommandExpr
- (ANTLRv4Parser.lexerCommandExpr
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("lexerCommandExpr
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitaltList
- (ANTLRv4Parser.altList
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("altList
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitalternative
- (ANTLRv4Parser.alternative
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("alternative
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitelement
- (ANTLRv4Parser.element
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("element
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitpredicateOptions
- (ANTLRv4Parser.predicateOptions
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("predicateOptions
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitpredicateOption
- (ANTLRv4Parser.predicateOption
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("predicateOption
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitlabeledElement
- (ANTLRv4Parser.labeledElement
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("labeledElement
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitebnf
- (ANTLRv4Parser.ebnf
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("ebnf
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitblockSuffix
- (ANTLRv4Parser.blockSuffix
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("blockSuffix
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitebnfSuffix
- (ANTLRv4Parser.ebnfSuffix
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("ebnfSuffix
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitlexerAtom
- (ANTLRv4Parser.lexerAtom
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("lexerAtom
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitatom
- (ANTLRv4Parser.atom
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("atom
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitnotSet
- (ANTLRv4Parser.notSet
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("notSet
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitblockSet
- (ANTLRv4Parser.blockSet
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("blockSet
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitsetElement
- (ANTLRv4Parser.setElement
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("setElement
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitblock
- (ANTLRv4Parser.block
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("block
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitruleref
- (ANTLRv4Parser.ruleref
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("ruleref
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitcharacterRange
- (ANTLRv4Parser.characterRange
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("characterRange
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitterminalDef
- (ANTLRv4Parser.terminalDef
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("terminalDef
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitelementOptions
- (ANTLRv4Parser.elementOptions
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("elementOptions
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitelementOption
- (ANTLRv4Parser.elementOption
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("elementOption
- ");
- return visitChildren(ctx);
- }}
- @Override
- public Void visitidentifier
- (ANTLRv4Parser.identifier
- Context ctx) {{
- // Logic to print the current rule's name
- System.out.println("identifier
- ");
- return visitChildren(ctx);
- }}
-
-}
-} /* corrected closing brace */} /* corrected closing brace */} /* corrected closing brace */
\ No newline at end of file
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
public class Arithmetic_Echo {
+ // Constant for the usage message
+ private static final String USAGE_MESSAGE = "Usage: java Arithmetic_Echo <input-file> " +
+ "[-version]";
public static void main(String[] args) throws IOException {
- if (args.length != 1) {
- System.err.println("Usage: java Arithmetic_Echo <input-file>");
+ if (args.length == 0) {
+ System.err.println(USAGE_MESSAGE);
System.exit(1);
}
- String inputFile = args[0];
+ // Defaults
+ List<String> argList = new ArrayList<>();
+
+ // Parse the arguments
+ for (int i = 0; i < args.length; i++) {
+ String arg = args[i];
+ if (arg.startsWith("-")) {
+ switch (arg) {
+ case "-version":
+ System.out.println("Version 0.1");
+ System.exit(0);
+ break;
+ default:
+ System.err.println("Unrecognized option: " + arg);
+ System.err.println(USAGE_MESSAGE);
+ System.exit(1);
+ }
+ } else {
+ argList.add(arg);
+ }
+ }
+
+ // Ensure there is exactly one input file argument
+ if (argList.size() != 1) {
+ System.err.println(USAGE_MESSAGE);
+ System.exit(1);
+ }
+
+ String inputFile = argList.get(0);
String input = Files.readString(Paths.get(inputFile));
try {
import java.util.Map;
public class Arithmetic_Echo__Test {
+ // Constant for the usage message
+ private static final String USAGE_MESSAGE = "Usage: Arithmetic_Echo__Test " +
+ "[-version]";
+
+ public static void main(String[] args) {
+ // if (args.length == 0) {
+ // System.err.println(USAGE_MESSAGE);
+ // System.exit(1);
+ // }
+
+ // Parse the arguments
+ for (String arg : args) {
+ if (arg.startsWith("-")) {
+ switch (arg) {
+ case "-version":
+ System.out.println("Version 0.1");
+ System.exit(0);
+ break;
+ default:
+ System.err.println("Unrecognized option: " + arg);
+ System.err.println(USAGE_MESSAGE);
+ System.exit(1);
+ }
+ }
+ }
+
+ Map<String, Boolean> tests = new HashMap<>();
+ tests.put("Arithmetic_Echo_0", Arithmetic_Echo_0());
+ tests.put("Arithmetic_Echo_1", Arithmetic_Echo_1());
+ tests.put("Arithmetic_Echo_2", Arithmetic_Echo_2());
+ tests.put("Arithmetic_Echo_3", Arithmetic_Echo_3());
+ tests.put("Arithmetic_Echo_4", Arithmetic_Echo_4());
+
+ TestBench.runTests(tests);
+ }
public static boolean Arithmetic_Echo_0() {
// Simple smoke test
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("Arithmetic_Echo_4 - Expected: " + expectedOutput + ", Actual: " + actualOutput);
return expectedOutput.equals(actualOutput);
} catch (Exception e) {
return false;
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("Arithmetic_Echo_0", Arithmetic_Echo_0());
- tests.put("Arithmetic_Echo_1", Arithmetic_Echo_1());
- tests.put("Arithmetic_Echo_2", Arithmetic_Echo_2());
- tests.put("Arithmetic_Echo_3", Arithmetic_Echo_3());
- tests.put("Arithmetic_Echo_4", Arithmetic_Echo_4());
-
- TestBench.runTests(tests);
- }
}
import java.util.List;
public class Arithmetic_Syntax {
+ // Constant for the usage message
+ private static final String USAGE_MESSAGE = "Usage: Arithmetic_Syntax [-pp] <source-file> " +
+ "[-version]";
+
+ public static void main(String[] args) throws IOException {
+ if (args.length == 0) {
+ System.err.println(USAGE_MESSAGE);
+ System.exit(1);
+ }
- public static void main(String[] arg_array) throws IOException {
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) {
+ for (int i = 0; i < args.length; i++) {
+ String arg = args[i];
if (arg.startsWith("-")) {
- if (arg.equals("-pp")) {
+ switch (arg) {
+ case "-pp":
pretty_print = true;
- } else {
+ break;
+ case "-version":
+ System.out.println("Version 0.1");
+ System.exit(0);
+ break;
+ default:
System.err.println("Unrecognized option: " + arg);
has_error = true;
}
// If there were any errors, print usage and exit
if (has_error) {
- System.err.println(usage);
+ System.err.println(USAGE_MESSAGE);
System.exit(1);
}
// Ensure there is exactly one input file
if (file_arg_list.size() != 1) {
- System.err.println("usage");
+ System.err.println(USAGE_MESSAGE);
System.exit(1);
}
+
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
import java.nio.file.Files;
return visitor.visit(tree);
}
+ // Constant for the usage message
+ private static final String USAGE_MESSAGE = "Usage: Arithmetic_Syntax__Test [-version]";
+
public static void main(String[] args) {
+ // if (args.length == 0) {
+ // System.err.println(USAGE_MESSAGE);
+ // System.exit(1);
+ // }
+
+ // Parse the arguments
+ for (String arg : args) {
+ if (arg.startsWith("-")) {
+ switch (arg) {
+ case "-version":
+ System.out.println("Version 0.1");
+ System.exit(0);
+ break;
+ default:
+ System.err.println("Unrecognized option: " + arg);
+ System.err.println(USAGE_MESSAGE);
+ System.exit(1);
+ }
+ }
+ }
+
// Use LinkedHashMap to maintain order of test results
- Map<String ,Boolean> tests = new LinkedHashMap<>();
- tests.put("Arithmetic_Syntax_0" ,Arithmetic_Syntax_0());
- tests.put("Arithmetic_Syntax_1" ,Arithmetic_Syntax_1());
- tests.put("Arithmetic_Syntax_2" ,Arithmetic_Syntax_2());
- tests.put("Arithmetic_Syntax_3" ,Arithmetic_Syntax_3());
- tests.put("Arithmetic_Syntax_4" ,Arithmetic_Syntax_4());
+ Map<String, Boolean> tests = new LinkedHashMap<>();
+ tests.put("Arithmetic_Syntax_0", Arithmetic_Syntax_0());
+ tests.put("Arithmetic_Syntax_1", Arithmetic_Syntax_1());
+ tests.put("Arithmetic_Syntax_2", Arithmetic_Syntax_2());
+ tests.put("Arithmetic_Syntax_3", Arithmetic_Syntax_3());
+ tests.put("Arithmetic_Syntax_4", Arithmetic_Syntax_4());
TestBench.runTests(tests);
}
+
}
+
+++ /dev/null
-/*
-Takes an 'GQL_20240412' grammar source file. Parses it. Outputs an annotated
-version of the source file while labeling what parts of the grammar the syntax
-objects belong to. Note the -pp option.
-
-*/
-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.ArrayList;
-import java.util.List;
-
-public class GQL_20240412_Syntax {
-
- public static void main(String[] arg_array) throws IOException {
- boolean pretty_print = false;
- List<String> file_arg_list = new ArrayList<>();
- boolean has_error = false;
-
- // Parse the options and arguments
- for (String arg : arg_array) {
- if (arg.startsWith("-")) {
- if (arg.equals("-pp")) {
- pretty_print = true;
- } else {
- System.err.println("Unrecognized option: " + arg);
- has_error = true;
- }
- } else {
- file_arg_list.add(arg);
- }
- }
-
- // If there were any errors, print usage and exit
- if (has_error) {
- System.err.println("Usage: java GQL_20240412_Syntax [-pp] <input-file>");
- System.exit(1);
- }
-
- // Ensure there is exactly one input file
- if (file_arg_list.size() != 1) {
- System.err.println("Usage: java GQL_20240412_Syntax [-pp] <input-file>");
- System.exit(1);
- }
-
- String input_file = file_arg_list.get(0);
- String input = Files.readString(Paths.get(input_file));
-
- try {
- GQL_20240412Lexer lexer = new GQL_20240412Lexer(CharStreams.fromString(input));
- CommonTokenStream tokens = new CommonTokenStream(lexer);
- GQL_20240412Parser parser = new GQL_20240412Parser(tokens);
- ParseTree tree = parser.program();
-
- GQL_20240412_Syntax_PrintVisitor visitor = new GQL_20240412_Syntax_PrintVisitor(parser.getRuleNames(), pretty_print);
- 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 GQL_20240412_Syntax_PrintVisitor extends GQL_20240412BaseVisitor<String>{
- private final String[] rule_names;
- private final boolean pretty_print;
-
- public GQL_20240412_Syntax_PrintVisitor(String[] rule_names, boolean pretty_print){
- this.rule_names = rule_names;
- this.pretty_print = pretty_print;
- }
-
- private String indent(int level){
- return " ".repeat(level);
- }
-
- @Override
- public String visitProgram(GQL_20240412Parser.ProgramContext ctx){
- if(pretty_print){
- StringBuilder result = new StringBuilder();
- result.append("program\n").append(visitExpression(ctx.expression(), 1));
- return result.toString();
- }else{
- return "program(" + visit(ctx.expression()) + ")";
- }
- }
-
- @Override
- public String visitExpression(GQL_20240412Parser.ExpressionContext ctx){
- return visitExpression(ctx, 0);
- }
-
- private String visitExpression(GQL_20240412Parser.ExpressionContext ctx, int indent_level){
- StringBuilder result = new StringBuilder();
- if(pretty_print){
- result.append(indent(indent_level)).append("expression(\n");
- if( ctx.INT() != null ){
- result.append(indent(indent_level + 1)).append("INT(").append(ctx.INT().getText()).append(")\n");
- }else if( ctx.getChildCount() == 3 && ctx.getChild(0).getText().equals("(") ){
- result.append(indent(indent_level + 1)).append("(\n");
- result.append(visitExpression(ctx.expression(0), indent_level + 2));
- result.append(indent(indent_level + 1)).append(")\n");
- }else{
- result.append(visitExpression(ctx.expression(0), indent_level + 1));
- result.append(indent(indent_level + 1)).append("operator(").append(ctx.getChild(1).getText()).append(")\n");
- result.append(visitExpression(ctx.expression(1), indent_level + 1));
- }
- result.append(indent(indent_level)).append(")\n");
- }else{
- if( ctx.INT() != null ){
- result.append("INT(").append(ctx.INT().getText()).append(")");
- }else if( ctx.getChildCount() == 3 && ctx.getChild(0).getText().equals("(") ){
- result.append("(").append(visit(ctx.expression(0))).append(")");
- }else{
- String left = visit(ctx.expression(0));
- String right = visit(ctx.expression(1));
- String operator = "operator(" + ctx.getChild(1).getText() + ")";
- result.append("expression(").append(left).append(" ").append(operator).append(" ").append(right).append(")");
- }
- }
- return result.toString();
- }
-}
+++ /dev/null
-/*
-Separates the grammar and lexer rules out into two files.
-
-Purpose for this is so we can transform the grammar rules by collecting lexer tokens
-into categories.
-*/
-import java.io.*;
-import java.nio.file.*;
-import java.util.*;
-
-public class GrammarSplitter {
-
- public static void main(String[] args) throws IOException {
- String originalFile = "GQL_20240412.g4";
- String lexerFile = "lexer_rules.g4";
- String grammarFile = "grammar_rules.g4";
-
- List<String> originalLines = Files.readAllLines(Paths.get(originalFile));
-
- List<String> lexerLines = new ArrayList<>();
- List<String> grammarLines = new ArrayList<>();
-
- boolean isLexerSection = false;
-
- for (String line : originalLines) {
- line = line.trim();
-
- // Check if line starts a lexer rule or fragment
- if (isLexerRule(line)) {
- isLexerSection = true;
- }
-
- // Add the line to the correct section based on the current state
- if (isLexerSection) {
- lexerLines.add(line);
- // End of a lexer rule or fragment
- if (line.endsWith(";")) {
- isLexerSection = false;
- }
- } else {
- grammarLines.add(line);
- }
- }
-
- Files.write(Paths.get(lexerFile), lexerLines);
- Files.write(Paths.get(grammarFile), grammarLines);
-
- System.out.println("Splitting complete. Lexer rules saved to " + lexerFile + ", Grammar rules saved to " + grammarFile);
- }
-
- private static boolean isLexerRule(String line) {
- // Identify lexer rules based on common patterns, capitalization, or presence of 'fragment'
- return line.matches("^(fragment\\s+|[A-Z]).*:\\s*.*") || line.matches(".*\\b(DIGIT|WHITESPACE|SP|LF|TAB|CR|FS|GS|RS|US|VT|FF|HEX_DIGIT)\\b.*");
- }
-}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-public class PrintRuleNameListRegx {
+public class RuleNameListRegx {
+ // Constant for the usage message
+ private static final String USAGE_MESSAGE = "Usage: java RuleNameListRegx <path-to-g4-file> " +
+ "[-version]";
public static void main(String[] args) {
- if (args.length != 1) {
- System.out.println("Usage: PrintRuleNameListRegx <path-to-g4-file>");
- return;
+ if (args.length == 0) {
+ System.err.println(USAGE_MESSAGE);
+ System.exit(1);
+ }
+
+ String filePath = null;
+
+ // Parse the arguments
+ for (int i = 0; i < args.length; i++) {
+ String arg = args[i];
+ if (arg.startsWith("-")) {
+ switch (arg) {
+ case "-version":
+ System.out.println("Version 0.1");
+ System.exit(0);
+ break;
+ default:
+ System.err.println("Unrecognized option: " + arg);
+ System.err.println(USAGE_MESSAGE);
+ System.exit(1);
+ }
+ } else {
+ filePath = arg;
+ }
+ }
+
+ // Ensure there is exactly one file path argument
+ if (filePath == null) {
+ System.err.println(USAGE_MESSAGE);
+ System.exit(1);
}
- String filePath = args[0];
Set<String> ruleNames = new HashSet<>();
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
import java.util.List;
import java.io.PrintWriter;
-public class GeneratePrintVisitor {
+public class Synthesize_PrintVisitor {
+ // Constant for the usage message
+ private static final String USAGE_MESSAGE = "Usage: java Synthesize_PrintVisitor <grammarFile> <outputFile> [indentLevel] " +
+ "[-version]";
- public static void main(String[] args) throws IOException {
- if (args.length < 2 || args.length > 3) {
- System.err.println("Usage: java GeneratePrintVisitor <grammarFile> <outputFile> [indentLevel]");
- System.exit(1);
+ public static void main(String[] args) throws IOException {
+ if (args.length == 0) {
+ System.err.println(USAGE_MESSAGE);
+ System.exit(1);
+ }
+
+ String grammarFile = null;
+ String outputFile = null;
+ int indentLevel = 0;
+
+ // Parse the arguments
+ for (int i = 0; i < args.length; i++) {
+ String arg = args[i];
+ if (arg.startsWith("-")) {
+ switch (arg) {
+ case "-version":
+ System.out.println("Version 0.1");
+ System.exit(0);
+ break;
+ default:
+ System.err.println("Unrecognized option: " + arg);
+ System.err.println(USAGE_MESSAGE);
+ System.exit(1);
}
+ } else {
+ if (grammarFile == null) {
+ grammarFile = arg;
+ } else if (outputFile == null) {
+ outputFile = arg;
+ } else {
+ indentLevel = Integer.parseInt(arg);
+ }
+ }
+ }
- String grammarFile = args[0];
- String outputFile = args[1];
- int indentLevel = args.length == 3 ? Integer.parseInt(args[2]) : 0;
+ // Ensure there are exactly two or three arguments
+ if (grammarFile == null || outputFile == null) {
+ System.err.println(USAGE_MESSAGE);
+ System.exit(1);
+ }
- // Extract the grammar name from the file name
- String grammarName = Paths.get(grammarFile).getFileName().toString().replace(".g4", "");
- String parserName = grammarName + "Parser";
+ // 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);
+ // 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);
+ // 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;
+ // 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 class PrintVisitor extends AbstractParseTreeVisitor<String> {
+ private final String[] ruleNames;
- public PrintVisitor(String[] ruleNames) {
- this.ruleNames = ruleNames;
- }
+ public PrintVisitor(String[] ruleNames) {
+ this.ruleNames = ruleNames;
+ }
- // Generated print methods
- """;
+ // Generated print methods
+ """;
- // Indent the class template
- String indentedClassTemplate = StringUtils.indentString(classTemplate, indentLevel);
+ // Indent the class template
+ String indentedClassTemplate = StringUtils.indentString(classTemplate, indentLevel);
- // Generate and output the PrintVisitor class
- try (PrintWriter writer = new PrintWriter(outputFile)) {
- // Write the class template
- writer.print(indentedClassTemplate);
+ // Generate and output the PrintVisitor class
+ try (PrintWriter writer = new PrintWriter(outputFile)) {
+ // Write the class template
+ writer.print(indentedClassTemplate);
- for (String ruleName : ruleNames) {
- GeneratePrintVisitorMethod.generatePrintMethod(parserName, ruleName, writer, indentLevel + 1);
- }
+ for (String ruleName : ruleNames) {
+ Synthesize_PrintVisitorMethod.generatePrintMethod(parserName, ruleName, writer, indentLevel + 1);
+ }
- // Close the class
- writer.println(StringUtils.indentString("}", indentLevel));
- }
+ // Close the class
+ writer.println(StringUtils.indentString("}", indentLevel));
}
+ }
- private static List<String> extractRuleNames(Parser parser) {
- // Extract rule names from the parser
- return List.of(parser.getRuleNames());
- }
+ private static List<String> extractRuleNames(Parser parser) {
+ // Extract rule names from the parser
+ return List.of(parser.getRuleNames());
+ }
}
import java.io.PrintWriter;
-public class GeneratePrintVisitorMethod {
+public class Synthesize_PrintVisitorMethod {
+ // Constant for the usage message
+ private static final String USAGE_MESSAGE = "Usage: java Synthesize_PrintVisitorMethod <parserName> <ruleName> <outputFile> " +
+ "[-version]";
- public static void main(String[] args) {
- if (args.length != 3) {
- System.err.println("Usage: java GeneratePrintVisitorMethod <parserName> <ruleName> <outputFile>");
- System.exit(1);
- }
+ public static void main(String[] args) {
+ if (args.length == 0) {
+ System.err.println(USAGE_MESSAGE);
+ System.exit(1);
+ }
- String parserName = args[0];
- String ruleName = args[1];
- String outputFile = args[2];
+ String parserName = null;
+ String ruleName = null;
+ String outputFile = null;
- try (PrintWriter writer = new PrintWriter(outputFile)) {
- generatePrintMethod(parserName, ruleName, writer, 0); // Default indent level 0
- } catch (Exception e) {
- e.printStackTrace();
+ // Parse the arguments
+ for (int i = 0; i < args.length; i++) {
+ String arg = args[i];
+ if (arg.startsWith("-")) {
+ switch (arg) {
+ case "-version":
+ System.out.println("Version 0.1");
+ System.exit(0);
+ break;
+ default:
+ System.err.println("Unrecognized option: " + arg);
+ System.err.println(USAGE_MESSAGE);
+ System.exit(1);
+ }
+ } else {
+ if (parserName == null) {
+ parserName = arg;
+ } else if (ruleName == null) {
+ ruleName = arg;
+ } else {
+ outputFile = arg;
}
+ }
+ }
+
+ // Ensure there are exactly three arguments
+ if (parserName == null || ruleName == null || outputFile == null) {
+ System.err.println(USAGE_MESSAGE);
+ System.exit(1);
}
- public static void generatePrintMethod(String parserName, String ruleName, PrintWriter writer, int indentLevel) {
- // 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);
-
- // Indent the template
- String indentedTemplate = StringUtils.indentString(template, indentLevel);
-
- // Write the template to the output file
- writer.print(indentedTemplate);
- writer.println();
+ try (PrintWriter writer = new PrintWriter(outputFile)) {
+ generatePrintMethod(parserName, ruleName, writer, 0); // Default indent level 0
+ } catch (Exception e) {
+ e.printStackTrace();
}
+ }
+
+ public static void generatePrintMethod(String parserName, String ruleName, PrintWriter writer, int indentLevel) {
+ // 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);
+
+ // Indent the template
+ String indentedTemplate = StringUtils.indentString(template, indentLevel);
+
+ // Write the template to the output file
+ writer.print(indentedTemplate);
+ writer.println();
+ }
}
import java.util.*;
public class TerminalToCategory {
+ // Constant for the usage message
+ private static final String USAGE_MESSAGE = "Usage: java TerminalToCategory <literalsFile> <originalFile> <outputFile> " +
+ "[-version]";
- public static void main(String[] args) throws IOException {
- String literalsFile = "GQL_20240412_rf1_literals.g4";
- String originalFile = "GQL_20240412.g4";
- String outputFile = "GQL_20240412_substituted.g4";
-
- Map<String, String> tokenMap = loadTokenMap(literalsFile);
- List<String> originalLines = Files.readAllLines(Paths.get(originalFile));
-
- // Print categories and their terminals
- printTokenMap(tokenMap);
-
- List<String> updatedLines = new ArrayList<>();
-
- for (int i = 0; i < originalLines.size(); i++) {
- String line = originalLines.get(i);
- String updatedLine = line;
- for (Map.Entry<String, String> entry : tokenMap.entrySet()) {
- String terminal = entry.getKey();
- String category = entry.getValue();
-
- // Use a regex to replace whole words only, and avoid replacing category names
- String regex = "\\b" + terminal + "\\b";
- if (!category.equals("CONTROL_FLOW") && updatedLine.matches(".*" + regex + ".*")) {
- // Print the substitution details
- System.out.println("Substituting " + terminal + " with " + category +
- " on line " + (i + 1) + ": " + updatedLine.trim());
- // Add a comment with the original rule
- updatedLines.add("// Original: " + updatedLine.trim());
- updatedLine = updatedLine.replaceAll(regex, category);
- }
- }
- updatedLines.add(updatedLine);
- }
-
- Files.write(Paths.get(outputFile), updatedLines);
- System.out.println("Substitution complete. Output written to " + outputFile);
+ public static void main(String[] args) throws IOException {
+ if (args.length == 0) {
+ System.err.println(USAGE_MESSAGE);
+ System.exit(1);
}
-
- private static Map<String, String> loadTokenMap(String literalsFile) throws IOException {
- Map<String, String> tokenMap = new HashMap<>();
- List<String> lines = Files.readAllLines(Paths.get(literalsFile));
-
- String currentToken = null;
- StringBuilder symbolsBuilder = new StringBuilder();
-
- for (String line : lines) {
- line = line.trim();
- if (line.isEmpty() || line.startsWith("//")) {
- continue;
- }
- if (line.contains(":")) {
- if (currentToken != null) {
- addSymbolsToMap(currentToken, symbolsBuilder.toString(), tokenMap);
- }
- String[] parts = line.split(":");
- currentToken = parts[0].trim();
- symbolsBuilder = new StringBuilder(parts[1].trim());
- } else if (line.endsWith(";")) {
- symbolsBuilder.append(" ").append(line.replace(";", "").trim());
- addSymbolsToMap(currentToken, symbolsBuilder.toString(), tokenMap);
- currentToken = null;
- symbolsBuilder = new StringBuilder();
- } else {
- symbolsBuilder.append(" ").append(line);
- }
+
+ String literalsFile = null;
+ String originalFile = null;
+ String outputFile = null;
+
+ // Parse the arguments
+ for (int i = 0; i < args.length; i++) {
+ String arg = args[i];
+ if (arg.startsWith("-")) {
+ switch (arg) {
+ case "-version":
+ System.out.println("Version 0.1");
+ System.exit(0);
+ break;
+ default:
+ System.err.println("Unrecognized option: " + arg);
+ System.err.println(USAGE_MESSAGE);
+ System.exit(1);
}
- return tokenMap;
- }
-
- private static void addSymbolsToMap(String token, String symbols, Map<String, String> tokenMap) {
- for (String symbol : symbols.split("\\s+")) {
- tokenMap.put(symbol, token);
+ } else {
+ if (literalsFile == null) {
+ literalsFile = arg;
+ } else if (originalFile == null) {
+ originalFile = arg;
+ } else {
+ outputFile = arg;
}
+ }
+ }
+
+ // Ensure there are exactly three arguments
+ if (literalsFile == null || originalFile == null || outputFile == null) {
+ System.err.println(USAGE_MESSAGE);
+ System.exit(1);
}
-
- private static void printTokenMap(Map<String, String> tokenMap) {
- Map<String, List<String>> categorizedMap = new HashMap<>();
-
- for (Map.Entry<String, String> entry : tokenMap.entrySet()) {
- categorizedMap.computeIfAbsent(entry.getValue(), k -> new ArrayList<>()).add(entry.getKey());
+
+ Map<String, String> tokenMap = loadTokenMap(literalsFile);
+ List<String> originalLines = Files.readAllLines(Paths.get(originalFile));
+
+ // Print categories and their terminals
+ printTokenMap(tokenMap);
+
+ List<String> updatedLines = new ArrayList<>();
+
+ for (int i = 0; i < originalLines.size(); i++) {
+ String line = originalLines.get(i);
+ String updatedLine = line;
+ for (Map.Entry<String, String> entry : tokenMap.entrySet()) {
+ String terminal = entry.getKey();
+ String category = entry.getValue();
+
+ // Use a regex to replace whole words only, and avoid replacing category names
+ String regex = "\\b" + terminal + "\\b";
+ if (!category.equals("CONTROL_FLOW") && updatedLine.matches(".*" + regex + ".*")) {
+ // Print the substitution details
+ System.out.println("Substituting " + terminal + " with " + category +
+ " on line " + (i + 1) + ": " + updatedLine.trim());
+ // Add a comment with the original rule
+ updatedLines.add("// Original: " + updatedLine.trim());
+ updatedLine = updatedLine.replaceAll(regex, category);
}
-
- for (Map.Entry<String, List<String>> entry : categorizedMap.entrySet()) {
- System.out.println("Category: " + entry.getKey());
- System.out.println("Terminals: " + String.join(", ", entry.getValue()));
- System.out.println();
+ }
+ updatedLines.add(updatedLine);
+ }
+
+ Files.write(Paths.get(outputFile), updatedLines);
+ System.out.println("Substitution complete. Output written to " + outputFile);
+ }
+
+ private static Map<String, String> loadTokenMap(String literalsFile) throws IOException {
+ Map<String, String> tokenMap = new HashMap<>();
+ List<String> lines = Files.readAllLines(Paths.get(literalsFile));
+
+ String currentToken = null;
+ StringBuilder symbolsBuilder = new StringBuilder();
+
+ for (String line : lines) {
+ line = line.trim();
+ if (line.isEmpty() || line.startsWith("//")) {
+ continue;
+ }
+ if (line.contains(":")) {
+ if (currentToken != null) {
+ addSymbolsToMap(currentToken, symbolsBuilder.toString(), tokenMap);
}
+ String[] parts = line.split(":");
+ currentToken = parts[0].trim();
+ symbolsBuilder = new StringBuilder(parts[1].trim());
+ } else if (line.endsWith(";")) {
+ symbolsBuilder.append(" ").append(line.replace(";", "").trim());
+ addSymbolsToMap(currentToken, symbolsBuilder.toString(), tokenMap);
+ currentToken = null;
+ symbolsBuilder = new StringBuilder();
+ } else {
+ symbolsBuilder.append(" ").append(line);
+ }
+ }
+ return tokenMap;
+ }
+
+ private static void addSymbolsToMap(String token, String symbols, Map<String, String> tokenMap) {
+ for (String symbol : symbols.split("\\s+")) {
+ tokenMap.put(symbol, token);
+ }
+ }
+
+ private static void printTokenMap(Map<String, String> tokenMap) {
+ Map<String, List<String>> categorizedMap = new HashMap<>();
+
+ for (Map.Entry<String, String> entry : tokenMap.entrySet()) {
+ categorizedMap.computeIfAbsent(entry.getValue(), k -> new ArrayList<>()).add(entry.getKey());
+ }
+
+ for (Map.Entry<String, List<String>> entry : categorizedMap.entrySet()) {
+ System.out.println("Category: " + entry.getKey());
+ System.out.println("Terminals: " + String.join(", ", entry.getValue()));
+ System.out.println();
}
+ }
}
+++ /dev/null
-antlr-mode.el -> .emacs.d
-(autoload 'antlr-mode "antlr-mode" "Major mode for editing ANTLR grammars." t)
-(add-to-list 'auto-mode-alist '("\\.g4\\'" . antlr-mode))
-
-
+antlr-mode.el -> .emacs.d
(autoload 'antlr-mode "antlr-mode" "Major mode for editing ANTLR grammars." t)
(add-to-list 'auto-mode-alist '("\\.g4\\'" . antlr-mode))