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_Syntax {
- public static void main(String[] args) throws IOException {
- if (args.length != 1) {
- System.err.println("Usage: java Syntax <input-file>");
+ 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 Arithmetic_Syntax [-pp] <input-file>");
System.exit(1);
}
- String inputFile = args[0];
- String input = Files.readString(Paths.get(inputFile));
+ // Ensure there is exactly one input file
+ if (file_arg_list.size() != 1) {
+ System.err.println("Usage: java Arithmetic_Syntax [-pp] <input-file>");
+ System.exit(1);
+ }
+
+ String input_file = file_arg_list.get(0);
+ String input = Files.readString(Paths.get(input_file));
try {
- // Direct instantiation without reflection
ArithmeticLexer lexer = new ArithmeticLexer(CharStreams.fromString(input));
CommonTokenStream tokens = new CommonTokenStream(lexer);
ArithmeticParser parser = new ArithmeticParser(tokens);
+ ParseTree tree = parser.program();
- // Directly calling the start rule method
- ParseTree tree = parser.program(); // Assuming 'program' is the start rule
-
- Arithmetic_Syntax_PrintVisitor visitor = new Arithmetic_Syntax_PrintVisitor(parser.getRuleNames());
- String syntaxSyntax = visitor.visit(tree);
- System.out.println(syntaxSyntax);
+ Arithmetic_Syntax_PrintVisitor visitor = new Arithmetic_Syntax_PrintVisitor(parser.getRuleNames(), pretty_print);
+ String output = visitor.visit(tree);
+ System.out.println(output);
} catch (Exception e) {
e.printStackTrace();
}
import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
-public class Arithmetic_Syntax_PrintVisitor extends ArithmeticBaseVisitor<String> {
- private final String[] ruleNames;
+public class Arithmetic_Syntax_PrintVisitor extends ArithmeticBaseVisitor<String>{
+ private final String[] rule_names;
+ private final boolean pretty_print;
- public Arithmetic_Syntax_PrintVisitor(String[] ruleNames) {
- this.ruleNames = ruleNames;
+ public Arithmetic_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(ArithmeticParser.ProgramContext ctx) {
- return "program(" + visit(ctx.expression()) + ")";
+ public String visitProgram(ArithmeticParser.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(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 + ")";
+ public String visitExpression(ArithmeticParser.ExpressionContext ctx){
+ return visitExpression(ctx, 0);
+ }
+
+ private String visitExpression(ArithmeticParser.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();
}
}