From 42f8c59a58760c4e530d8eb27bd9f078d4578057 Mon Sep 17 00:00:00 2001 From: Kraemerd <Dominic_Daniel.Kraemer@Student.Reutlingen-University.de> Date: Fri, 6 Jan 2023 05:43:52 +0100 Subject: [PATCH] implemented lexter in Lexter.java and fitting tests in Application.java --- src/Application.java | 13 ++++++++- src/Lexer.java | 67 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/src/Application.java b/src/Application.java index 00796fe..8f0b93c 100644 --- a/src/Application.java +++ b/src/Application.java @@ -1,3 +1,14 @@ -public class Application { +import java.util.Arrays; +import java.util.List; +public class Application { + public static void main(String[] args) { + List<String> test = Arrays.asList("1 + x^2 - (31 * x)"); + for (String value: test) { + List<Lexer.Token> tokens = Lexer.lex(value); + for(Lexer.Token singleToken: tokens) { + System.out.println(singleToken.toString()); + } + } + } } diff --git a/src/Lexer.java b/src/Lexer.java index 7b604e8..5cc07dd 100644 --- a/src/Lexer.java +++ b/src/Lexer.java @@ -1,3 +1,70 @@ +import java.util.LinkedList; +import java.util.List; + public class Lexer { + public static enum TokenType { + NUMBER, VARIABLE, SPECIAL + } + + public static class Token { + protected TokenType type; + protected String data; + public Token(TokenType type, String data) { + this.type = type; + this.data = data; + } + + public TokenType getType() { + return type; + } + + public String getData() { + return data; + } + + @Override + public String toString() { + return String.format("type: %s, data: %s", type, data); + } + } + + public static List<Token> lex(String input) { + List<Token> result = new LinkedList<Token>(); + for(int index=0; index < input.length();) { + + char current = input.charAt(index); + if(Character.isDigit(current)) { + int endIndex = index+1; + if(endIndex<input.length()) { + while(Character.isDigit(input.charAt(endIndex)) && endIndex<input.length()) { + endIndex += 1; + } + } + Token token = new Token(TokenType.NUMBER, input.substring(index,endIndex)); + result.add(token); + index += endIndex - index; + } + else if(Character.isWhitespace(current)) { + index++; + } + else if(Character.isLetter(current)){ + int endIndex = index+1; + if(endIndex<input.length()) { + while(Character.isLetter(input.charAt(endIndex)) && endIndex<input.length()) { + endIndex += 1; + } + } + Token token = new Token(TokenType.VARIABLE, input.substring(index,endIndex)); + result.add(token); + index += endIndex - index; + } + else { + Token token = new Token(TokenType.SPECIAL, String.valueOf(current)); + result.add(token); + index++; + } + } + return result; + } } -- GitLab