Final push for Project 1

This commit is contained in:
Jonathan Turner 2023-11-27 20:46:09 -05:00
parent b317739362
commit 8c00a20472
5 changed files with 47 additions and 13 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_18" default="true" project-jdk-name="openjdk-18" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_19" default="true" project-jdk-name="openjdk-18" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>

View File

@ -43,6 +43,7 @@ public class Driver {
private void printMainMenu() { private void printMainMenu() {
System.out.println();
System.out.println("-- Main Menu --"); System.out.println("-- Main Menu --");
System.out.println("1. Analyze Java Code"); System.out.println("1. Analyze Java Code");
System.out.println("2. Analyze Python Code"); System.out.println("2. Analyze Python Code");
@ -50,6 +51,7 @@ public class Driver {
} }
private void printFileSelection(String type) { private void printFileSelection(String type) {
System.out.println();
System.out.println("-- Select File Type --"); System.out.println("-- Select File Type --");
System.out.println("1. Provided File: " + type + ".txt "); System.out.println("1. Provided File: " + type + ".txt ");
System.out.println("2. Custom Input"); System.out.println("2. Custom Input");

View File

@ -86,8 +86,7 @@ public class JavaSyntaxAnalyzer {
TokenType token = null; TokenType token = null;
switch (lexeme) { switch (lexeme) {
case '.': case '.':
// token = TokenType.PERIOD; token = TokenType.REFERENCE;
token = TokenType.UNKNOWN;
break; break;
case '=': case '=':
token = TokenType.ASSIGN_OP; token = TokenType.ASSIGN_OP;

View File

@ -5,6 +5,9 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
public class PythonSyntaxAnalyzer { public class PythonSyntaxAnalyzer {
private String active = null;
public List<Pair> start(List<String> source) { public List<Pair> start(List<String> source) {
List<Pair> values = new ArrayList<>(); List<Pair> values = new ArrayList<>();
for (String line : source) { for (String line : source) {
@ -15,23 +18,50 @@ public class PythonSyntaxAnalyzer {
} }
private List<Pair> readline(String line) { private List<Pair> readline(String line) {
List<Pair> analyzedLine = new ArrayList<>(); List<Pair> analyzedLine = new ArrayList<>();
String active = line.trim(); active = line;
Pair result = null; Pair result = null;
do { do {
active = active.trim(); if (this.checkIndent()) {
result = lex(active); result = new Pair("\t", TokenType.TAB);
if (result != null) {
analyzedLine.add(result); analyzedLine.add(result);
if (result.lexeme.length() > 1) { this.removeIndent();
active = active.replaceFirst(result.lexeme, ""); } else {
} else { active = active.trim();
active = active.substring(1); result = lex(active);
if (result != null) {
analyzedLine.add(result);
if (result.lexeme.length() > 1) {
active = active.replaceFirst(result.lexeme, "");
} else {
active = active.substring(1);
}
} }
} }
} while (result != null && result.type != TokenType.EOF); } while (result != null && result.type != TokenType.EOF);
return analyzedLine; return analyzedLine;
} }
private void removeIndent() {
char[] temp = active.toCharArray();
String value = "";
for (int i = 0; i < temp.length; i++) {
if (temp[i] != ' ' || i > 3) {
value += temp[i];
}
}
this.active = value;
}
private boolean checkIndent() {
char[] temp = active.toCharArray();
for (int i = 0; i < 4; i++) {
if (temp[i] != ' ') {
return false;
}
}
return true;
}
private Pair lex(String line) { private Pair lex(String line) {
if (line == null || line.isEmpty()) { if (line == null || line.isEmpty()) {
return null; return null;
@ -119,6 +149,9 @@ public class PythonSyntaxAnalyzer {
case '$': case '$':
token = TokenType.EOF; token = TokenType.EOF;
break; break;
case '_':
token = TokenType.UNDERSCORE;
break;
default: default:
token = TokenType.UNKNOWN; token = TokenType.UNKNOWN;
} }

View File

@ -1,10 +1,10 @@
package edu.jt_kb.cs4308.compiler.models; package edu.jt_kb.cs4308.compiler.models;
public enum TokenType { public enum TokenType {
INT_LIT(10),IDENT(11),ASSIGN_OP(20),ADD_OP(21),SUB_OP(22),MULT_OP(23), INT_LIT(10),IDENT(11),REFERENCE(12),ASSIGN_OP(20),ADD_OP(21),SUB_OP(22),MULT_OP(23),
DIV_OP(24),LEFT_PAREN(26),RIGHT_PAREN(27),SEMI_COLON(28), STRING_LITERAL(29), DIV_OP(24),LEFT_PAREN(26),RIGHT_PAREN(27),SEMI_COLON(28), STRING_LITERAL(29),
COMMA(30), LEFT_CURLY(31), RIGHT_CURLY(32), LEFT_SQUARE(33), RIGHT_SQUARE(34), COMMA(30), LEFT_CURLY(31), RIGHT_CURLY(32), LEFT_SQUARE(33), RIGHT_SQUARE(34),
RESERVED_WORD(35),EOF(-1),UNKNOWN(-2); RESERVED_WORD(35),TAB(36),UNDERSCORE(37),EOF(-1),UNKNOWN(-2);
public final int value; /* represents which Token you are referencing. */ public final int value; /* represents which Token you are referencing. */