From e9dde58fc7e4c63fcf7a8dfec2ca8e4b3f6732bd Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Sat, 25 Nov 2023 13:59:48 -0500 Subject: [PATCH] Modified some aspects and added the driver. --- src/edu/jt_kb/cs4308/compiler/Driver.java | 128 ++++++++++++++++-- .../compiler/models/JavaSyntaxAnalyzer.java | 7 + 2 files changed, 125 insertions(+), 10 deletions(-) diff --git a/src/edu/jt_kb/cs4308/compiler/Driver.java b/src/edu/jt_kb/cs4308/compiler/Driver.java index 3f79089..03bebba 100644 --- a/src/edu/jt_kb/cs4308/compiler/Driver.java +++ b/src/edu/jt_kb/cs4308/compiler/Driver.java @@ -4,29 +4,33 @@ import edu.jt_kb.cs4308.compiler.FileManagement.FileReader; import edu.jt_kb.cs4308.compiler.models.JavaSyntaxAnalyzer; import edu.jt_kb.cs4308.compiler.models.Pair; import edu.jt_kb.cs4308.compiler.models.TokenType; -import org.xml.sax.ext.LexicalHandler; import java.io.File; -import java.sql.Array; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Scanner; public class Driver { public static void main(String[] args) { - System.out.println("Default Setup"); - Driver program = new Driver(); - program.start(); - } - - public void start() { File java = new File("src/edu/jt_kb/cs4308/compiler/resources/Java.txt"); File python = new File("src/edu/jt_kb/cs4308/compiler/resources/Python.txt"); + Driver program = new Driver(java, python); + } + private final List java; + private final List python; + + Driver(File java, File python) { + this.java = FileReader.readFile(java); + this.python = FileReader.readFile(python); + + this.getType(); + } + + private void start() { List code = new ArrayList<>(); JavaSyntaxAnalyzer analyzer = new JavaSyntaxAnalyzer(); - code = FileReader.readFile(java); + List analysis = analyzer.start(code); for (Pair curr : analysis) { if (curr.type != TokenType.EOF) { @@ -37,4 +41,108 @@ public class Driver { } + private void printMainMenu() { + System.out.println("-- Main Menu --"); + System.out.println("1. Analyze Java Code"); + System.out.println("2. Analyze Python Code"); + System.out.println("3. Exit"); + } + + private void printFileSelection(String type) { + System.out.println("-- Select File Type --"); + System.out.println("1. Provided File: " + type + ".txt "); + System.out.println("2. Custom Input"); + System.out.println("3. Back"); + } + + private void getType() { + int option = -1; + while (option != 3) { + printMainMenu(); + option = getOptionInput(); + if (option == 1) { + getSourceFile("Java"); + } + if (option == 2) { + getSourceFile("Python"); + } else if (0 > option || option > 3) { + System.out.println("Please enter a number between 1-3."); + System.out.println(); + } + } + } + + private void getSourceFile(String type) { + int option = -1; + while (option != 3) { + printFileSelection(type); + option = getOptionInput(); + + if (option == 1) { + this.performAnalysis(type); + break; + } else if (option == 2) { + List source = new ArrayList<>(); + Scanner sc = new Scanner(System.in); + int counter = 0; + while (true) { + System.out.print("Line " + counter + ": "); + String input = sc.nextLine(); + if (input.equalsIgnoreCase("EOF")) { + break; + } + source.add(input); + counter++; + } + + this.performAnalysis(type, source); + + } else if (0 > option || option > 3) { + System.out.println("Please enter a number between 1-3."); + } + } + } + + private void performAnalysis(String type) { + List results = new ArrayList<>(); + if (type.equalsIgnoreCase("java")) { + JavaSyntaxAnalyzer analyzer = new JavaSyntaxAnalyzer(); + results = analyzer.start(this.java); + } else { + // Python Analyzer Code + } + for (Pair lex : results) { + System.out.println("The next token is: " + lex.type.value + " next lexeme is: " + lex.lexeme); + } + } + + private void performAnalysis(String type, List source) { + List results = new ArrayList<>(); + if (type.equalsIgnoreCase("java")) { + JavaSyntaxAnalyzer analyzer = new JavaSyntaxAnalyzer(); + results = analyzer.start(source); + } else { + // Python Analyzer Code + } + for (Pair lex : results) { + System.out.println("The next token is: " + lex.type.value + " next lexeme is: " + lex.lexeme); + } + } + private int getOptionInput() { + boolean valid = false; + int value = -1; + System.out.println(); + while (!valid) { + Scanner sc = new Scanner(System.in); + System.out.print("Option: "); + String input = sc.nextLine(); + try { + value = Integer.parseInt(input); + valid = true; + } catch (Exception e) { + System.out.println("Please enter a number."); + } + } + return value; + } } \ No newline at end of file diff --git a/src/edu/jt_kb/cs4308/compiler/models/JavaSyntaxAnalyzer.java b/src/edu/jt_kb/cs4308/compiler/models/JavaSyntaxAnalyzer.java index 699283e..bf82c9a 100644 --- a/src/edu/jt_kb/cs4308/compiler/models/JavaSyntaxAnalyzer.java +++ b/src/edu/jt_kb/cs4308/compiler/models/JavaSyntaxAnalyzer.java @@ -85,6 +85,10 @@ public class JavaSyntaxAnalyzer { public Pair lookup(char lexeme) { TokenType token = null; switch (lexeme) { + case '.': + // token = TokenType.PERIOD; + token = TokenType.UNKNOWN; + break; case '=': token = TokenType.ASSIGN_OP; break; @@ -134,6 +138,9 @@ public class JavaSyntaxAnalyzer { } private Pair lookup(String lexeme) { + if (lexeme.equals("EOF")) { + return new Pair("EOF", TokenType.EOF); + } TokenType token = null; List keywords = new ArrayList<>(Arrays.asList("public","class","static","void","int")); if (keywords.contains(lexeme.trim())) {