2/23/11

Symbol Table in Java

Symbol Table Definition :

In computer science, a symbol table is a data structure used by a language translator such as a compiler or interpreter, where each identifier in a program's source code is associated with information relating to its declaration or appearance in the source, such as its type, scope level and sometimes its location. 


Symbol Table code using java :

I will be reading a file named 4th.txt which has a c/c++ code in it and then it makes a symbol table of the code given.


4th.txt :

#include<iostream.h>

viod main(){

int a,b,c=10;

cout<<a;

cout<<b;

float flt=10.20;

float flt1=10.20;

cout<<flt;



JAVA CODE :
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.StringTokenizer;


public class SymbolTable {

    public static void main(String[] args) {
        
        try{
            String s;
            String fr[];
            String data[]=null;
            String val[]=null;
            String var[]=null;
            int st,ed,ln,i=0;
            File f=new File("4th.txt");
            FileReader fl=new FileReader(f);
            BufferedReader bf=new BufferedReader(fl);
            System.out.println("Datatype \t Variable \t Current Value");
            while((s=bf.readLine())!=null){
                StringTokenizer str=new StringTokenizer(s);
                    if(s.contains("int")==true){
                        fr=s.split(" ");
                        System.out.println();
                        System.out.print(fr[0] + "\t\t ");
                        fr[1]=fr[1].replaceAll(","," ");
                        var=fr[1].split(" ");
                        
                        for(i=0;i<var.length;i++){
                            if(var[i].contains("=")==true){        
                            var=var[i].split("=");
                            for(i=0;i<var.length;i++){
                                
                                System.out.print(var[i].replace(';', ' '));
                                System.out.print("\t\t ");
                            }
                            
                        }else{
                            System.out.print(var[i]);
                            System.out.print("\t\t ");
                            System.out.print(0);
                            System.out.println();
                            System.out.print("\t\t ");
                                            
                        }
                        }
                        
                    }else if(s.contains("float")==true){
                        fr=s.split(" ");
                        System.out.println();
                        System.out.print(fr[0] + "\t\t ");
                        fr[1]=fr[1].replaceAll(","," ");
                        var=fr[1].split(" ");
                        
                        for(i=0;i<var.length;i++){
                            if(var[i].contains("=")==true){        
                            var=var[i].split("=");
                            for(i=0;i<var.length;i++){
                                
                                System.out.print(var[i].replace(';', ' '));
                                System.out.print("\t\t ");
                            }
                            
                        }else{
                            System.out.print(var[i]);
                            System.out.print("\t\t ");
                            System.out.print(0);
                            System.out.println();
                            System.out.print("\t\t ");
                                            
                        }
                        }
                        
                    }
            }
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }

}

SHARE THIS POST: