From 8c00a20472a169d7ea3920806453069fdac6ade1 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Mon, 27 Nov 2023 20:46:09 -0500 Subject: [PATCH] Final push for Project 1 --- .idea/misc.xml | 2 +- src/edu/jt_kb/cs4308/compiler/Driver.java | 2 + .../compiler/models/JavaSyntaxAnalyzer.java | 3 +- .../compiler/models/PythonSyntaxAnalyzer.java | 49 ++++++++++++++++--- .../cs4308/compiler/models/TokenType.java | 4 +- 5 files changed, 47 insertions(+), 13 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index d15472f..ce297c6 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/src/edu/jt_kb/cs4308/compiler/Driver.java b/src/edu/jt_kb/cs4308/compiler/Driver.java index 2d08df4..f2334e9 100644 --- a/src/edu/jt_kb/cs4308/compiler/Driver.java +++ b/src/edu/jt_kb/cs4308/compiler/Driver.java @@ -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"); diff --git a/src/edu/jt_kb/cs4308/compiler/models/JavaSyntaxAnalyzer.java b/src/edu/jt_kb/cs4308/compiler/models/JavaSyntaxAnalyzer.java index bf82c9a..c049567 100644 --- a/src/edu/jt_kb/cs4308/compiler/models/JavaSyntaxAnalyzer.java +++ b/src/edu/jt_kb/cs4308/compiler/models/JavaSyntaxAnalyzer.java @@ -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; diff --git a/src/edu/jt_kb/cs4308/compiler/models/PythonSyntaxAnalyzer.java b/src/edu/jt_kb/cs4308/compiler/models/PythonSyntaxAnalyzer.java index 7280195..5c56873 100644 --- a/src/edu/jt_kb/cs4308/compiler/models/PythonSyntaxAnalyzer.java +++ b/src/edu/jt_kb/cs4308/compiler/models/PythonSyntaxAnalyzer.java @@ -5,6 +5,9 @@ import java.util.Arrays; import java.util.List; public class PythonSyntaxAnalyzer { + + private String active = null; + public List start(List source) { List values = new ArrayList<>(); for (String line : source) { @@ -15,23 +18,50 @@ public class PythonSyntaxAnalyzer { } private List readline(String line) { List 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; } diff --git a/src/edu/jt_kb/cs4308/compiler/models/TokenType.java b/src/edu/jt_kb/cs4308/compiler/models/TokenType.java index 4c3b0f2..66c5109 100644 --- a/src/edu/jt_kb/cs4308/compiler/models/TokenType.java +++ b/src/edu/jt_kb/cs4308/compiler/models/TokenType.java @@ -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. */