Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In JAVA : scan input file (input1.txt) and return each of the tokens (one at a time). Thank you so much!! Need to *Create a

In JAVA: scan input file (input1.txt) and return each of the tokens (one at a time). Thank you so much!!

Need to *Create a Scanner.java by following descriptions

  • There needs to be a constructor:

public Scanner(String sourceFileNm, SymbolTable symbolTable)

    • sourceFileNm will be the name of the Pickle source code
    • symbolTable will be used for all assignments after this one
  • Provides access to currentToken which is an instance variable for Scanner.
  • Token delimiters
    • private final static String delimiters = " \t;:()\'\"=!<>+-*/[]#,^ "; // terminate a token
  • What may be different from what you have done before is that the delimiters may also be significant tokens.
  • Scanner provides the following methods:

getNext()

  • functionally returns a string for the next token
  • provides values for currentToken:
    • tokenStr
    • primClassif - must be able to distinguish
      • operator +, -, *, /, <, >, !, = # ^
      • separator (, ), :, ;, [, ], an actual comma
      • operand
      • eof
    • subClassif - in Pgm#1, it only distinguishes operands:
      • identifier
      • string literal (also remove the surrounding quotes)
      • integer constant
      • float constant
  • set tokenStr appropriately
  • automatically skips white space (blanks, tabs, ) which isn't within a string literal; therefore, automatically skips blank lines
  • a string literal must end on the source text line where it began
  • automatically advances to next line of input (and prints that line with its line nr (relative to 1)).
  • when EOF is encountered:
    • sets the token's primClassif to Classif.EOF.
    • getNext() functionally returns ""

_________________________________________________________________________________

*input1.txt

Int x; Float pi; String yStr; x = 11; x = x + 1; pi = 3.14; print ("x=", x); print ("pi=", pi); yStr = 'Hello'; print ("yStr=", yStr); yStr = "Can't"; print ("yStr=", yStr); x = 10*3 + 7; if x > 20: print ("x=", x); yStr = "yes"; else: print ("x is low"); endif; yStr = 'Can\'t wait'; print (yStr);

____________________________________________________________________

*Here are my other classes for this project

1. Pickle.java : code for Pickle.java which is a driver for calling your scanner

package pickle;

public class Pickle

{ public static void main(String[] args)

{ // Create the SymbolTable

SymbolTable symbolTable = new SymbolTable();

try

{

// Print a column heading

System.out.printf("%-11s %-12s %s "

, "primClassif"

, "subClassif"

, "tokenStr");

Scanner scan = new Scanner(args[0], symbolTable);

while (! scan.getNext().isEmpty())

{

scan.currentToken.printToken();

}

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

_______________________________________________________________________

2. Classif.java -enum class with classifications

package pickle;

public enum Classif

{

EMPTY, // empty

OPERAND, // constants, identifier

OPERATOR, // + - * / < > = !

SEPARATOR, // ( ) , : ; [ ]

FUNCTION, // TBD

CONTROL, // TBD

EOF // EOF encountered

}

____________________________________________________________________

3. SubClassif.java - enum class with subclassifications

package pickle;

public enum SubClassif

{

EMPTY, // empty

// OPERAND's subclassifications

IDENTIFIER, // identifier

INTEGER, // integer constant

FLOAT, // float constant

BOOLEAN, // boolean constant

STRING, // string constant

DATE, // date constant

VOID, // void

// CONTROL's subclassifications

FLOW, // flow statement (e.g., if)

END, // end statement (e.g., endif)

DECLARE, // declare statement (e.g., Int)

// FUNCTION's subclassfications

BUILTIN, // builtin function (e.g., print)

USER // user-defined function

}

__________________________________________________________________

4. Token.java : starting file for the Token class. This contains printToken() which prints a token. This class represent a token for the Scanner Class.

package pickle;

import java.util.*;

public class Token

{

public String tokenStr = "";

public Classif primClassif = Classif.EMPTY;

public SubClassif subClassif = SubClassif.EMPTY;

public int iSourceLineNr = 0;

/** Column location in the source file for this token. column positions are

* relative to zero.

*/

public int iColPos = 0;

public Token(String value)

{

this.tokenStr = value;

}

public Token()

{

this(""); // invoke the other constructor

}

/**

* Prints the primary classification, sub-classification, and token string

*

* If the classification is EMPTY, it uses "**garbage**".

* If the sub-classification is EMPTY, it uses "-".

*/

public void printToken()

{

String primClassifStr;

String subClassifStr;

if (primClassif != Classif.EMPTY)

primClassifStr = primClassif.toString();

else

primClassifStr = "**garbage**";

if (subClassif != SubClassif.EMPTY)

subClassifStr = subClassif.toString();

else

subClassifStr = "-";

System.out.printf("%-11s %-12s %s "

, primClassifStr

, subClassifStr

, tokenStr);

}

}

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_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions

Question

What steps are commonly used by unions to organize a workplace?

Answered: 1 week ago

Question

Explain the different types of marketing strategies.

Answered: 1 week ago

Question

Explain product positioning.

Answered: 1 week ago

Question

Explain Industrial market segment.

Answered: 1 week ago