r/learnjava • u/Ok_Perspective_8040 • Feb 03 '26
Lexical Analyzer
hey guys, i was trying to build a lexicon follwoing this tutotiral, while I was developing my tokening function they wrote this following line of code as the tokeniser detects each char typed:
while (expression.hasNext()) {
final Character currentChar = getValidNextCharacter(expression);
}
however there was no previous mention of the function ever described on the webpage: https://www.baeldung.com/java-lexical-analysis-compilation
I'm suspecting that this function was already written in the code and was named something else but I was under the assumption Gram had already taken care of this part. please help, here's a full context of the code Id dhave written so far:
private enum Gram {
ADDITION('+'),
SUBTRACTION('-'),
MULTIPLICATION('*'),
DIVISION('/');
private final char _op;
Gram(char _op) {
this._op = _op;
}
public static boolean isOperator(char symbol) {
return Arrays.stream(Gram.values())
.anyMatch(gram -> gram._op == symbol);
}
public static boolean isDigit(char num){
return Character.isDigit(num);
}
public static boolean isWhiteSpace(char space) { //isWhiteSpace
return Character.isWhitespace(space);
}
public static boolean isValidSymbol (char character) {
return isOperator(character) || isWhiteSpace(character) || isDigit(character);
}
}
public class Expression {
private final String value; //the final value returned after all that is stuff
private int index = 0;
public Expression(String value) {
if (value != null) {
this.value = value;
} else {
this.value = "";
}
//[this.value = value != null ? value : "";] this is called a ternary operator however i don't know how to use it so i'm just gonna use somethin i do know
}
public Optional<Character> next() { //Optional<> is prefered over null cuz more leniency
if (index >= value.length()) {
return Optional.empty();
}
return Optional.of(value.charAt(index++));
}
public boolean hasNext() {
return index < value.length();
}
}
public abstract class Token {
private final String value;
public enum TokenType {
NUMBER,
OPERATOR
};
private final TokenType type;
protected Token(TokenType type, String value) {
this.type = type;
this.value = value;
}
public TokenType getType() {
return type;
}
public String getValue() {
return value;
}
}
public class TokenNum extends Token {
protected TokenNum(String value) {
super(TokenType.NUMBER, value);
}
public int getValueAsInt() {
return Integer.parseInt(getValue());
}
}
public class TokenOperator extends Token {
protected TokenOperator(String value) {
super(TokenType.OPERATOR, value);
}
}
private enum State {
INTIAL,
NUMBER,
OPERATOR,
INVALID
}
public List<Token> tokenize(Expression expression) {
State state = State.INTIAL;
StringBuilder currentToken = new StringBuilder();
ArrayList<Token> tokens = new ArrayList<>();
while (expression.hasNext()) {
final Character currentChar = getValidNextCharacter(expression);
}
return tokens;
}
}
View all comments
Show parent comment
1
u/Lloydbestfan Feb 04 '26
In-depth follow-up
We can attempt to guess what could have motivated the author to delegate to such a method, that they forgot to talk about in their tutorial, instead of writing it exactly like I propose in my replacement.
Here is my guess.
Once upon a time, the Optional class didn't propose an orElseThrow() method. Instead, the way to get the Optional's content was to call method get(), which does the exact same thing, but by name makes it a lot less obvious that there is a possibility that the call won't provide a value and it will throw an exception instead.
During these times, when you would call get() on an optional without having first called isPresent() or something else that makes it very immediately obvious to the compiler's flow analysis that you ensured that the call to get() won't throw an exception, then the compiler would display a warning on that call. And warnings look bad, you don't want to have them in your tutorials.
To avoid this warning you could do some additional defensive programming such as:
That gets rid of the warning, but that adds three lines of clutter code to the operation, when all you're doing is getting your character out of the optional, which you had already ensured won't ever throw an exception, it's just you did it in a way that's clear to the programmer but not to the compiler.
By hiding these lines in a dedicated method and just calling this method to get your character, you make it a one-liner back, and it is reasonably clear to read within the flow of code.
Now, with orElseThrow() that wouldn't normally produce such warnings, doing that is not necessary.