Question: How can you divide this Java file into 3 separate files that can be run together. Main.java, Lexer.java, and Token.java. The main class should read

How can you divide this Java file into 3 separate files that can be run together. Main.java, Lexer.java, and Token.java. The main class should read the file.When doing so there are some errors. How can the errors be resolved?

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 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) { //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 tokens = lex(input); for (Token token : tokens) System.out.println(token); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!