Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Symbol Table Implementation Problem Description You are to develop a symbol tabl

ID: 3929640 • Letter: S

Question

Symbol Table Implementation Problem Description You are to develop a symbol table for storing identifiers. An identifier is a name created by a programmer for naming variables, methods, classes, etc. in a program. The symbol table for the following partial Java program is given below. public class Assignment public static void main(String[] args) Scanner input = new Scanner(System.in); nt n string name; System.out.println("Enter a number: "); n = input.nextlnt(); System.out.println("Enter a name: ") name input.next() SYMBOL TABLE Identifier Name Lines Appearing Orn Assignment args input name out println System 5, 10, 13 6, 10 7, 13 9, 12 9, 12 9, 12

Explanation / Answer

package symb;

public class LineNumNode {
private int lineNum;
private LineNumNode next;

public LineNumNode(int lineNum, LineNumNode next) {
this.lineNum = lineNum;
this.next = next;
}

public int getLineNum() {
return lineNum;
}

public LineNumNode getNext() {
return next;
}

public void setLineNum(int lineNum) {
this.lineNum = lineNum;
}

public void setNext(LineNumNode next) {
this.next = next;
}
}

--------------------------------------------------------------------------------------------------------------------------------------------------------

package symb;

public class IdentifierNode {

private String identifier;
private LineNumNode next;

public IdentifierNode(String identifier, LineNumNode next) {
this.identifier = identifier;
this.next = next;
}

public String getIdentifier() {
return identifier;
}

public void setIdentifier(String identifier) {
this.identifier = identifier;
}

public void setNext(LineNumNode next) {
this.next = next;
}

public LineNumNode getNext() {
return next;
}
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

package symb;

public class Symbol_Table {
IdentifierNode symbol_table[]=new IdentifierNode[50];

public Symbol_Table() {
for(int i=0;i<symbol_table.length;i++)
{
this.symbol_table[i]=new IdentifierNode("",null);
}
}
  
public void AddIdentifier(String identifier, int lineNum)
{
LineNumNode t1=new LineNumNode(lineNum,null),t2=null;
for(int i=0;i<symbol_table.length;i++)
{
if(symbol_table[i].getIdentifier().equals(identifier))
{

t2=symbol_table[i].getNext();
while(t2.getNext()!=null)
t2=t2.getNext();

t2.setNext(t1);
break;
}
else if(symbol_table[i].getIdentifier().equals(""))
{
symbol_table[i].setIdentifier(identifier);
symbol_table[i].setNext(new LineNumNode(lineNum,null));
break;
}
}
}
  
public void display()
{
System.out.println("SYMBOL TABLE");
System.out.println("Identifier Lines Appearing on");
System.out.println("------------------------------------------------------------------");   
for(int i=0;i<symbol_table.length;i++)
{
if(symbol_table[i].getIdentifier().equals(""))
break;
else
{
System.out.print(symbol_table[i].getIdentifier()+" ");
LineNumNode t2=symbol_table[i].getNext();
while(t2!=null)
{
System.out.print(t2.getLineNum()+",");
t2=t2.getNext();
}
System.out.println(" ");

}
  
}
}

}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

package symb;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Assignment {

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n,ch;
String name;
Symbol_Table st=new Symbol_Table();
while (true)
{
System.out.println("ENTER YOUR CHOICE FROM THE FOLLOWING OPTIONS");
System.out.println("ENTER 1 TO INSERT AN IDENTIFIER INTO THE SYMBOL TABLE");
System.out.println("ENTER 2 TO DISPLAY THE SYMBOL TABLE");
System.out.println("ENTER 3 TO EXIT");
ch=Integer.parseInt(br.readLine());
{
switch(ch)
{
case 1:
System.out.println("Enter a identifier :");
name = br.readLine();
System.out.println("Enter a number: ");
n = Integer.parseInt(br.readLine());
  
st.AddIdentifier(name, n);
break;

case 2:
st.display();
break;

case 3:
System.exit(0);
break;

default:
System.out.println("Not a correct choice");
}
}
}
}
}