Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 31st International Conference Dexa 2020 Bratislava Slovakia September 14 17 2020 Proceedings Part 1 Lncs 12391

Authors: Sven Hartmann ,Josef Kung ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

303059002X, 978-3030590024

More Books

Students also viewed these Databases questions

Question

List the various types of predictive algorithms.

Answered: 1 week ago

Question

Differentiate 3sin(9x+2x)

Answered: 1 week ago

Question

Compute the derivative f(x)=(x-a)(x-b)

Answered: 1 week ago