diff --git a/src/Application.java b/src/Application.java index b8e4e45f08075ed7b365333a687d6bd7d50829ba..f9d586f369030370a6e91f41e58450c8fb3f0081 100644 --- a/src/Application.java +++ b/src/Application.java @@ -3,9 +3,13 @@ import java.util.List; public class Application { public static void main(String[] args) { + //List with a fitting String to be parsed into multiple tokens List<String> test = Arrays.asList("1 + xy^2 - (31 * x)y"); + //Iterates through every String for (String value: test) { + //creates a list of tokens out of the String List<Lexer.Token> tokens = Lexer.lex(value); + //prints each token out (with .toString()) for(Lexer.Token singleToken: tokens) { System.out.println(singleToken.toString()); } diff --git a/src/Lexer.java b/src/Lexer.java index 250e8631aa081571ccc743481aa0197dda98b97d..a61e4cc3cdac280f042f1796efbf7314d887961d 100644 --- a/src/Lexer.java +++ b/src/Lexer.java @@ -2,10 +2,14 @@ import java.util.LinkedList; import java.util.List; public class Lexer { + + //enum TokenType that can either be a number, variable or a special character (like * or / etc.) public static enum TokenType { NUMBER, VARIABLE, SPECIAL } + //tokens with a TokenType that represents their type and a String with the actual data + //has for both values a getter method and overrides toString with it's fitting values public static class Token { protected TokenType type; protected String data; @@ -28,11 +32,16 @@ public class Lexer { } } + //creates a list of tokens from a given String public static List<Token> lex(String input) { + //the list that will be returned at the end List<Token> result = new LinkedList<Token>(); + //for-loop that iterates through each character of the String for(int index=0; index < input.length();) { char current = input.charAt(index); + //checks if a character is a digit, then creates a token with one or multiple numbers + //with TokenType=NUMBER if(Character.isDigit(current)) { int endIndex = index+1; if(endIndex<input.length()) { @@ -44,9 +53,8 @@ public class Lexer { result.add(token); index += endIndex - index; } - else if(Character.isWhitespace(current)) { - index++; - } + //checks if a character is a letter, then creates for each following character a separate token + //with TokenType=VARIABLE else if(Character.isLetter(current)){ int iterator = index; while(Character.isLetter(input.charAt(iterator)) && iterator<input.length()) { @@ -59,6 +67,12 @@ public class Lexer { } index += iterator - index; } + //whitespaces are ignored + else if(Character.isWhitespace(current)) { + index++; + } + //every char of the string that isn't a digit, letter or whitespace will be + //used for creating a token with TokenType=SPECIAL else { Token token = new Token(TokenType.SPECIAL, String.valueOf(current)); result.add(token);