Question: Given this code how can you properly separate it into three separate Java files. Main.java Lexer.java and Token.java. import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import
Given this code how can you properly separate it into three separate Java files. Main.java Lexer.java and Token.java.
import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern;
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
// 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) { //creating file object File file = new File("input.txt"); //scanner object to scan the input file Scanner sc = null; String input = ""; try { sc = new Scanner(file); //traversing the entire input file untile all the lines found while (sc.hasNextLine()) { input += sc.nextLine()+" "; } } catch (FileNotFoundException e) { System.out.println("File Not Found. Please Check Input File"); } // Create tokens and print them ArrayList
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
