Question: Design a program that reads a Java program and makes a list of all the identifiers. To do this,you should make use of two dictionaries.
Design a program that reads a Java program and makes a list of all the identifiers. To do this,you should make use of two dictionaries. The first dictionary should hold all the Java reserved words. The second dictionary should hold all the identifiers that you find. Whenever you encounter a token, you first should search the dictionary of reserved words. If the token is not a reserved word, you then should search the dictionary of identifiers. If the token is not in either dictionary, you should add it to the dictionary of identifiers.
import java.util.Scanner;
import java.io.File;
import java.util.Iterator;
/**
A class that uses a pair of dictionaries to locate identifiers in a .java file.
*/
public class Driver
{
public static void main(String[] args)
{
DictionaryInterface
new SortedVectorDictionary<> ();
DictionaryInterface
new SortedVectorDictionary<> ();
String mostRecentReserved = "";
Scanner in = new Scanner(System.in);
try
{
in = new Scanner(new File("CodeToBeRead.java"));
in.useDelimiter("\\W+");
} // end try
catch (Exception e)
{
e.printStackTrace();
} // end catch
addReservedWord(reservedWords, "public");
addReservedWord(reservedWords, "private");
addReservedWord(reservedWords, "static");
addReservedWord(reservedWords, "void");
addReservedWord(reservedWords, "int");
addReservedWord(reservedWords, "String");
addReservedWord(reservedWords, "float");
addReservedWord(reservedWords, "double");
addReservedWord(reservedWords, "long");
addReservedWord(reservedWords, "null");
addReservedWord(reservedWords, "class");
addReservedWord(reservedWords, "new");
// Your code here
} // end main
/** Adds a given reserved word to the given dictionary.
@param dictionary The dictionary to add the word to.
@param word The word to be added to the dictionary. */
public static void addReservedWord(DictionaryInterface
{
// Your code here
} // end addReservedWord
} // end driver
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
