Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

*Need help in Java: scan input file (input1.txt) and return each of the tokens (one at a time).

*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 Token currentToken; 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); int lineNumber = 1; while(sc.hasNextLine()) { String line = sc.nextLine(); System.out.println("line " + lineNumber + " :" + line); lineNumber++; } } private String nextLine() { // TODO Auto-generated method stub return null; } private boolean hasNextLine() { // TODO Auto-generated method stub return false; } public String getNext() { // TODO Auto-generated method stub return null; } } 

________________________________________________________

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

}

}

. . 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

Professional Visual Basic 6 Databases

Authors: Charles Williams

1st Edition

1861002025, 978-1861002020

More Books

Students also viewed these Databases questions