Final push for Project 1
This commit is contained in:
parent
b317739362
commit
8c00a20472
@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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" />
|
||||
</component>
|
||||
</project>
|
@ -43,6 +43,7 @@ public class Driver {
|
||||
|
||||
|
||||
private void printMainMenu() {
|
||||
System.out.println();
|
||||
System.out.println("-- Main Menu --");
|
||||
System.out.println("1. Analyze Java Code");
|
||||
System.out.println("2. Analyze Python Code");
|
||||
@ -50,6 +51,7 @@ public class Driver {
|
||||
}
|
||||
|
||||
private void printFileSelection(String type) {
|
||||
System.out.println();
|
||||
System.out.println("-- Select File Type --");
|
||||
System.out.println("1. Provided File: " + type + ".txt ");
|
||||
System.out.println("2. Custom Input");
|
||||
|
@ -86,8 +86,7 @@ public class JavaSyntaxAnalyzer {
|
||||
TokenType token = null;
|
||||
switch (lexeme) {
|
||||
case '.':
|
||||
// token = TokenType.PERIOD;
|
||||
token = TokenType.UNKNOWN;
|
||||
token = TokenType.REFERENCE;
|
||||
break;
|
||||
case '=':
|
||||
token = TokenType.ASSIGN_OP;
|
||||
|
@ -5,6 +5,9 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class PythonSyntaxAnalyzer {
|
||||
|
||||
private String active = null;
|
||||
|
||||
public List<Pair> start(List<String> source) {
|
||||
List<Pair> values = new ArrayList<>();
|
||||
for (String line : source) {
|
||||
@ -15,23 +18,50 @@ public class PythonSyntaxAnalyzer {
|
||||
}
|
||||
private List<Pair> readline(String line) {
|
||||
List<Pair> analyzedLine = new ArrayList<>();
|
||||
String active = line.trim();
|
||||
active = line;
|
||||
Pair result = null;
|
||||
do {
|
||||
active = active.trim();
|
||||
result = lex(active);
|
||||
if (result != null) {
|
||||
if (this.checkIndent()) {
|
||||
result = new Pair("\t", TokenType.TAB);
|
||||
analyzedLine.add(result);
|
||||
if (result.lexeme.length() > 1) {
|
||||
active = active.replaceFirst(result.lexeme, "");
|
||||
} else {
|
||||
active = active.substring(1);
|
||||
this.removeIndent();
|
||||
} else {
|
||||
active = active.trim();
|
||||
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);
|
||||
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) {
|
||||
if (line == null || line.isEmpty()) {
|
||||
return null;
|
||||
@ -119,6 +149,9 @@ public class PythonSyntaxAnalyzer {
|
||||
case '$':
|
||||
token = TokenType.EOF;
|
||||
break;
|
||||
case '_':
|
||||
token = TokenType.UNDERSCORE;
|
||||
break;
|
||||
default:
|
||||
token = TokenType.UNKNOWN;
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
package edu.jt_kb.cs4308.compiler.models;
|
||||
|
||||
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),
|
||||
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. */
|
||||
|
Reference in New Issue
Block a user