Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How to divide into 3 java files. Lexer.java, Main.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

How to divide into 3 java files. Lexer.java, Main.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 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

Current Trends In Database Technology Edbt 2006 Edbt 2006 Workshops Phd Datax Iidb Iiha Icsnw Qlqp Pim Parma And Reactivity On The Web Munich Germany March 2006 Revised Selected Papers Lncs 4254

Authors: Torsten Grust ,Hagen Hopfner ,Arantza Illarramendi ,Stefan Jablonski ,Marco Mesiti ,Sascha Muller ,Paula-Lavinia Patranjan ,Kai-Uwe Sattler ,Myra Spiliopoulou ,Jef Wijsen

2006th Edition

3540467882, 978-3540467885

More Books

Students also viewed these Databases questions

Question

Describe the five steps in the performance appraisal process.

Answered: 1 week ago

Question

5. Wyeth Pharmaceuticals

Answered: 1 week ago