Question: Java program Specifications For this assignment, you will implement four Java classes: SymTable, Sym, DuplicateSymException, and EmptySymTableException. You will also write a program called P1.java

Java program Specifications

For this assignment, you will implement four Java classes: SymTable, Sym, DuplicateSymException, and EmptySymTableException. You will also write a program called P1.java to test your implementations and answer some questions in a file named Questions.txt.

The SymTable class will be used by the compiler you write later in the semester to represent a symbol table: a data structure that stores the identifiers declared in the program being compiled (e.g., function and variable names) and information about each identifier (e.g., its type, where it will be stored at runtime). The symbol table will be implemented as a List of HashMaps. Eventually, each HashMap will store the identifiers declared in one scope in the program being compiled.

The HashMap keys will be Strings (the declared identifier names) and the associated information will be Syms (you will also implement the Sym class). For now, the only information in a Sym will be the type of the identifier, represented using a String (e.g., "int", "double", etc.).

The DuplicateSymException and EmptySymTableException classes will define exceptions that can be thrown by methods of the SymTable class.

In addition to defining the four classes, you will write a main program to test your implementation. You will be graded on the correctness of your Sym and SymTable classes, on how thoroughly you test the classes that you implement, on the efficiency of your code, and on your programming style. You will also answer a couple questions designed to get you thinking about implementation issues.

The Sym Class

The Sym class must be in a file named Sym.java. You must implement the following Sym constructor and public methods (and no other public or protected methods):

Sym(String type) This is the constructor; it should initialize the Sym to have the given type.
String getType() Return this Sym's type.
String toString() Return this Sym's type. (This method will be changed later when more information is stored in a Sym.)

The SymTable Class

The SymTable class must be in a file named SymTable.java. It must be implemented using a List of HashMaps. (Think about the operations that will be done on a SymTable to decide whether to use an ArrayList or a LinkedList.) The HashMaps must map a String to a Sym. This means that the SymTable class will have a (private) field of type List>.

List and HashMap are defined in the java.util package. This means that you will need to have the line

import java.util.*;

at the top of SymTable.java.

You must implement the following SymTable constructor and public methods (and no other public or protected methods):

SymTable() This is the constructor; it should initialize the SymTable's List field to contain a single, empty HashMap.
void addDecl(String name, Sym sym) throws DuplicateSymException, EmptySymTableException If this SymTable's list is empty, throw an EmptySymTableException. If either name or sym (or both) is null, throw an IllegalArgumentException. If the first HashMap in the list already contains the given name as a key, throw a DuplicateSymException. Otherwise, add the given name and sym to the first HashMap in the list.
void addScope() Add a new, empty HashMap to the front of the list.
Sym lookupLocal(String name) throws EmptySymTableException If this SymTable's list is empty, throw an EmptySymTableException. Otherwise, if the first HashMap in the list contains name as a key, return the associated Sym; otherwise, return null.
Sym lookupGlobal(String name) throws EmptySymTableException If this SymTable's list is empty, throw an EmptySymTableException. Otherwise, if any HashMap in the list contains name as a key, return the first associated Sym (i.e., the one from the HashMap that is closest to the front of the list); otherwise, return null.
void removeScope() throws EmptySymTableException If this SymTable's list is empty, throw an EmptySymTableException; otherwise, remove the HashMap from the front of the list. To clarify, throw an exception only if before attempting to remove, the list is empty (i.e., there are no HashMaps to remove).
void print() This method is for debugging. First, print " ** Sym Table ** ". Then, for each HashMap M in the list, print M.toString() followed by a newline. Finally, print one more newline. All output should go to System.out.

The DuplicateSymException and EmptySymTableException Classes

These two classes (which must be in files named DuplicateSymException.java and EmptySymTableException.java) will simply define the two checked exceptions that can be thrown by the SymTable class. Each exception must be able to be created using a constructor that takes no arguments.

To define a checked exception named XXX, you can use code like this:

public class XXX extends Exception { }

Note that the class has an empty body (it will have a no-argument constructor by default).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!