Question: Hi, I am new to Java. How can I get my program to read from a text file and print the individual tokens? import java.util.ArrayList;
Hi,
I am new to Java. How can I get my program to read from a text file and print the individual tokens?
import java.util.ArrayList; import java.util.regex.Pattern; import java.util.regex.Matcher; public class Lexer { public static enum TokenType { // Token types cannot have underscores NUMBER("-?[0-9]+"), BINARYOP("[*|/|+|-]"), WHITESPACE("[ \t\f ]+"); public final String pattern; private TokenType(String pattern) { this.pattern = pattern; } } public static class Token { public TokenType type; public String data; public Token(TokenType type, String data) { this.type = type; this.data = data; } @Override public String toString() { return String.format("(%s %s)", type.name(), data); } } public static ArrayList lex(String input) { // The tokens to return ArrayList tokens = new ArrayList(); // Lexer logic begins here StringBuffer tokenPatternsBuffer = new StringBuffer(); for (TokenType tokenType : TokenType.values()) tokenPatternsBuffer.append(String.format("|(?<%s>%s)", tokenType.name(), tokenType.pattern)); Pattern tokenPatterns = Pattern.compile(new String(tokenPatternsBuffer.substring(1))); // Begin matching tokens Matcher matcher = tokenPatterns.matcher(input); while (matcher.find()) { if (matcher.group(TokenType.NUMBER.name()) != null) { tokens.add(new Token(TokenType.NUMBER, matcher.group(TokenType.NUMBER.name()))); continue; } else if (matcher.group(TokenType.BINARYOP.name()) != null) { tokens.add(new Token(TokenType.BINARYOP, matcher.group(TokenType.BINARYOP.name()))); continue; } else if (matcher.group(TokenType.WHITESPACE.name()) != null) continue; } return tokens; } public static void main(String[] args) { String input = "11 + 22 - 33"; // Create tokens and print them ArrayList tokens = lex(input); for (Token token : tokens) System.out.println(token); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
