Symbol Table Implementation Problem Description You are to d
Solution
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\\t\\tLines 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()+\"\\t\");
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\");
}
}
}
}
}


