Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public LexicalAnalyzer(String fileName) throws LexicalException, FileNotFoundException { if (fileName == null) throw new IllegalArgumentException (null string argument); tokens = new ArrayList (); int rowNum =

public LexicalAnalyzer(String fileName) throws LexicalException, FileNotFoundException

{

if (fileName == null)

throw new IllegalArgumentException ("null string argument");

tokens = new ArrayList();

int rowNum = 1;

Scanner input = new Scanner (new File (fileName));

while (input.hasNext())

{

String line = input.nextLine();

processLine (line, rowNum, 0);

rowNum++;

}

input.close();

tokens.add(new Token (TokenType.EOS, "EOS", rowNum, 1));

}

/**

* @param line cannot be null

* @param rowNum must be positive

* @param index must be non-negative

* preconditions verified using assertions

* @throws LexicalException if scanning error found on line

*/

private void processLine(String line, int rowNum, int index) throws LexicalException

{

assert line != null;

assert rowNum > 0;

assert index >= 0;

index = skipWhiteSpace (line, index);

while (index < line.length())

{

String lexeme = getLexeme (line, index);

TokenType tokType = getTokenType (lexeme, rowNum, index+1);

tokens.add(new Token(tokType, lexeme, rowNum, index+1));

index += lexeme.length();

index = skipWhiteSpace (line, index);

}

}

/**

* @param lexeme cannot be null - checked with an assertion

* @return whether string contains only digits

*/

private boolean allDigits(String lexeme)

{

assert lexeme != null;

int i = 0;

while (i < lexeme.length() && Character.isDigit(lexeme.charAt(i)))

i++;

return i == lexeme.length();

}

private String getLexeme(String line, int index)

{

assert line != null;

assert index >= 0;

assert !Character.isWhitespace(line.charAt(index));

int location = index + 1;

while (location < line.length() && !Character.isWhitespace(line.charAt(location)))

++location;

return line.substring(index, location);

}

/**

* @param line cannot be null

* @param index must be non-negative

* @return index of the first subsequent non- whitespace character in line

*/

private int skipWhiteSpace(String line, int index)

{

assert line != null;

assert index >= 0;

int current = index;

while (current < line.length() && Character.isWhitespace(line.charAt(current)))

++current;

return current;

}

Convert the Java Code to Python

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

1 2 3 Data Base Techniques

Authors: Dick Andersen

1st Edition

0880223464, 978-0880223461

More Books

Students also viewed these Databases questions