Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*Please help java: scan input file (input1.txt) and return each of the tokens (one at a time). Thanks a lot!! * Incomplete Scanner.java (instruction below)

*Please help java: scan input file (input1.txt) and return each of the tokens (one at a time). Thanks a lot!!

*Incomplete Scanner.java (instruction below) *Please help getNext() method

package pickle; import java.io.File; import java.io.FileReader; import java.util.Collection; import java.util.*; import java.io.FileNotFoundException; public class Scanner { public String tokenStr = ""; public int iColPos = 0; // beginning column position for the token public int lineNumber = 0; // source line number where the token was found public char[] textCharM; //char [] for the current text line public Object currentToken; //The token established with the most recent call to getNext(). public Object nextToken; //The token following the currentToken. public Classif primClassif = Classif.EMPTY; public SubClassif subClassif = SubClassif.EMPTY; private String path; private final static String delimiters = " \t;:()\'\"=!+-*/[]#,^ "; // terminate a token public Scanner(String sourceFileNm, SymbolTable symbolTable) { this.path = sourceFileNm; // File text = new File(sourceFileNm); // Scanner sc = new Scanner(sourceFileNm,symbolTable); // while(sc.hasNextLine()) // { // String line = sc.nextLine(); //each elemement is a string as currentToken // System.out.println("line " + lineNumber + " :" + line); // currentToken = sc.getNext(); // lineNumber++; // } ArrayList sourceLineM = new ArrayList(); //array list of source text lines try { java.util.Scanner input = new java.util.Scanner(new File (sourceFileNm)); while (input.hasNextLine()) { sourceLineM.add(input.nextLine()); } } catch (FileNotFoundException e) { e.printStackTrace(); } } private String nextLine() { // TODO Auto-generated method stub return null; } private boolean hasNextLine() { // TODO Auto-generated method stub return false; } /* gets the next token. Automatically advances to the next source line when necessary. If there are no more tokens, it returns ""; otherwise, it returns the tokenStr for the current token. It also sets these attributes in scan.currentToken: tokenStr - the string representation of the token primeClassif - the primary classification of the token (OPERAND, OPERATOR) subClassif - the sub classification of the token (e.g., IDENTIFIER, INTEGER) iSourceLineNr - source line number where the token was found iColPos - beginning column position for the token */ public String getNext() { // TODO Auto-generated method stub // String primClassifStr; // String subClassifStr; // if (primClassif != Classif.EMPTY) // primClassifStr = primClassif.toString(); // else // primClassifStr = "**garbage**"; // // if (subClassif != SubClassif.EMPTY) // subClassifStr = subClassif.toString(); // else // subClassifStr = "-"; return tokenStr; } } 

_______________________________________________________________________

*Instruction

image text in transcribed

_______________________________________________________________

*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);

}

}

_______________________________

*SymbolTable.java

package pickle; public class SymbolTable { }

. . a . o o Scanner Attributes - see lecture notes There needs to be a constructor: public Scanner(String sourceFileNm, Symbol Table symbol Table) o sourceFileNm will be the name of the Pickle source code symbol Table 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;:()\'\"=!+-+7]#, "; // 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 +, -, *, 1, , !, = # ^ 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 o 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 o O o o o 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 "" . . . a . o o Scanner Attributes - see lecture notes There needs to be a constructor: public Scanner(String sourceFileNm, Symbol Table symbol Table) o sourceFileNm will be the name of the Pickle source code symbol Table 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;:()\'\"=!+-+7]#, "; // 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 +, -, *, 1, , !, = # ^ 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 o 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 o O o o o 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

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

25 Vba Macros For Data Analysis In Microsoft Excel

Authors: Klemens Nguyen

1st Edition

B0CNSXYMTC, 979-8868455629

More Books

Students also viewed these Databases questions