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:

Related Posts:

  • Java Programming with Eclipse : Basics This is intended to be a basic guide to using Eclipse. This guide is by no means comprehensive, but is sufficient to give you enough knowledge to work on your projects. Running and using Ecli… Read More
  • OOPs Concept : Inheritance After discussing Encapsulation, now its time for 'Inheritance' as OOP Concept. Inheritance can be defined as the process where one object acquires the properties of another. With the use of inheritance the information is … Read More
  • Object Oriented Programming (OOP) Explanation Object-oriented programming (OOP) is a programming paradigm that uses "objects" to design applications and computer programs. It utilizes several techniques from previously established paradigms, including inheritance… Read More
  • OOPs Concept: Encapsulation Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction. RFAZHE7YNFV8 Encapsulation is the technique of making the fields in a class private and provid… Read More
  • Create Linked List using Java The LinkedList class extends AbstractSequentialList and implements the List interface. It provides a linked-list data structure. It has the two constructors, shown here: RFAZHE7YNFV8 Link… Read More