Date:
Ex 1(a) SYMBOL TABLE
Aim:
To implement a simple lexical analyzer that processes C code, identifies data types, and
manages identifiers with memory addresses.
Algorithm:
1.Initialize Symbol Table: Create an empty symbol table and set the starting memory address to
1000.
2.Check Identifier: Define a function to check if an identifier already exists in the symbol table.
3.Add Identifier: Create a function to add a new identifier, update the symbol table, and adjust
the address based on data type size.
4.Process Line: Split a code line into tokens, extract data type and identifier, and clean the
identifier.
5.Handle Identifiers: If the identifier is new, add it; if it exists, print its previous address.
6.Input Loop: Continuously read input until the user types "exit," processing each line.
Code:
import java.util.ArrayList;
import java.util.Scanner;
class Symbol {
String identifier;
String datatype;
int address;
public Symbol(String identifier, String datatype, int address) {
this.identifier = identifier;
this.datatype = datatype;
this.address = address;
}
}
public class LexicalAnalyzer {
static ArrayList<Symbol> symbolTable = new ArrayList<>();
static int address = 1000; // starting address
static int checkIdentifier(String id) {
for (int i = 0; i < symbolTable.size(); i++) {
if (symbolTable.get(i).identifier.equals(id)) {
return i;
}
}
return -1;
}
static void addIdentifier(String id, String type) {
symbolTable.add(new Symbol(id, type, address));
if (type.equals("int")) {
address += 4;
} else if (type.equals("float")) {
address += 8;
} else if (type.equals("char")) {
address += 1;
}
}
static void processLine(String line) {
String[] tokens = line.split("\\s+");
String type = tokens[0];
String id = tokens[1].replaceAll("[^a-zA-Z0-9]", "");
if (type.equals("int") || type.equals("float") || type.equals("char")) {
int index = checkIdentifier(id);
if (index == -1) {
addIdentifier(id, type);
System.out.println("Identifier: " + id + ", Type: " + type + ", Address: " + (address -
(type.equals("int") ? 4 : (type.equals("float") ? 8 : 1))));
} else {
System.out.println("The identifier '" + id + "' has already been read before at address "
+ symbolTable.get(index).address + ".");
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String line;
System.out.println("Enter C code lines (type 'exit' to quit):");
while (true) {
System.out.print(">> ");
line = scanner.nextLine();
if (line.equals("exit")) {
break;
}
processLine(line);
}
scanner.close();
}
}
output
Enter C code lines (type 'exit' to quit):
>> int a = 6;
Identifier: a, Type: int, Address: 1000
>> int b =10;
Identifier: b, Type: int, Address: 1004
>> int c = 10;
Identifier: c, Type: int, Address: 1008
>> int a =40;
The identifier 'a' has already been read before at address 1000.
>> float a= 70.0;
Result:
Hence a simple lexical analyzer that processes C code, identifies data types, and manages
identifiers with memory addresses has been written, implemented and its output verified
successfully.