Modified some aspects and added the driver.

This commit is contained in:
Jonathan Turner 2023-11-25 13:59:48 -05:00
parent 78c8452874
commit e9dde58fc7
2 changed files with 125 additions and 10 deletions

View File

@ -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<String> java;
private final List<String> python;
Driver(File java, File python) {
this.java = FileReader.readFile(java);
this.python = FileReader.readFile(python);
this.getType();
}
private void start() {
List<String> code = new ArrayList<>();
JavaSyntaxAnalyzer analyzer = new JavaSyntaxAnalyzer();
code = FileReader.readFile(java);
List<Pair> 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<String> 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<Pair> 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<String> source) {
List<Pair> 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;
}
}

View File

@ -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<String> keywords = new ArrayList<>(Arrays.asList("public","class","static","void","int"));
if (keywords.contains(lexeme.trim())) {