0% found this document useful (0 votes)
17 views3 pages

EX - NO:2 Symbol Table Generation: Program

The document describes a program that generates a symbol table by reading in source code from a file, parsing variable declarations, generating random addresses for the variables, and outputting each variable name, its data type, and assigned address. The program handles integer and character variable declarations, arrays with initialized sizes, and assignments within expressions.

Uploaded by

Prem Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

EX - NO:2 Symbol Table Generation: Program

The document describes a program that generates a symbol table by reading in source code from a file, parsing variable declarations, generating random addresses for the variables, and outputting each variable name, its data type, and assigned address. The program handles integer and character variable declarations, arrays with initialized sizes, and assignments within expressions.

Uploaded by

Prem Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

EX.

NO:2
SYMBOL TABLE GENERATION
Program:
import java.util.*;
import java.io.*;
import java.math.*;
public class Symbol{
public static void main(String[] args) throws IOException {
Scanner scan=new Scanner(System.in);
String s="";
String val[]=null;
String st[]=null;
File f=new File("input.txt");
FileReader f1=new FileReader(f);
BufferedReader b1=new BufferedReader(f1);
System.out.println("Value

"+"DataType

Random rand = new Random();


int address=rand.nextInt(1000);
while((s=b1.readLine())!=null)
{
if(s.contains("int"))
{
if(s.contains("main()"))
{
continue;
}
val=s.split(" ");
val[2]=val[2].replaceAll(";","");
st=val[2].split(",");

"+"Address ");

for(String sub:st)
{
if(sub.contains("["))
{
String cnt=sub.substring(sub.indexOf('[')+1,sub.indexOf(']') );
int cn=Integer.parseInt(cnt);
System.out.println(sub.substring(0,1)+"

"+val[1]+"[]"+"

"+address);

address+=(cn*4);
}
else
{
if(sub.contains("="))
{
sub=sub.substring(0,sub.indexOf("="));
}
System.out.println(sub+"
address+=4;
}
}
}
}
}
}

SAMPLE INPUT/OUTPUT:
Input:
#include<stdio.h>
int main()
{
int a,b,c=5,d[10],e
char a,b;
get(a);
get(b);

"+val[1]+"

"+address);

c=a+b;
int z=0.5;
}
Output:

You might also like