0% found this document useful (0 votes)
31 views8 pages

Question

Uploaded by

Dileesha A
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)
31 views8 pages

Question

Uploaded by

Dileesha A
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/ 8

Digital Assignment -1

COURSE NAME: Compiler Design Lab


COURSE CODE: BCSE307P
FACULTY NAME: Sabyasachi Kamila

NAME OF THE STUDENT: A.Dileesha


REGISTRATION NUMBER: 22BCI0131
Question
Write a program in any language to create a Symbol Table to Insert, Delete, Display all <token,
lexeme> pairs from a given Input code Snippet.

Input code snippet also can be of any language.

Sample Input:
float f1;

int x, i=0;

while(i<5)

if((i+2) == 3)

break;

else

print("Value of i =%d", i);

Sample Output:
Make a Symbol Table entry where following details should be present

Token, lexeme

id, f1

id, x

id, I

key, float

key, int

... .so on, all token, lexeme pairs

You should also be able to insert/delete/display any<token, lexeme> pairs


Code:
import re

class Symbol:

def __init__(self, token, lexeme):

self.token = token

self.lexeme = lexeme

def __str__(self):

return f"{self.token}, {self.lexeme}"

class SymbolTable:

def __init__(self):

self.symbols = []

self.keywords = {"int", "float", "char", "while", "if", "break", "else",


"print","double","cout","continue","for","do","while","in","and","goto","not","is","or","string","vect
or","tuple","arrayList","println","input","void","def","new","class","struct","cout","cin","map"}

def add_symbol(self, token, lexeme):

self.symbols.append(Symbol(token, lexeme))

print(f"Inserted: ({token}, {lexeme})")

def delete_symbol(self, lexeme):

initial_length = len(self.symbols)

self.symbols = [s for s in self.symbols if s.lexeme != lexeme]

if len(self.symbols) < initial_length:

print(f"Deleted symbol with lexeme: {lexeme}")

else:

print(f"Symbol with lexeme '{lexeme}' not found.")

def display_symbol_table(self):

print("\nToken, lexeme")
for symbol in self.symbols:

print(symbol)

def parse_code(self, code):

identifiers = set()

key = set()

lit = set()

literals = re.findall(r'"[^"]*"', code)

code = re.sub(r'"[^"]*"', '', code)

symbols = re.findall(r'\b\w+\b', code)

for literal in literals:

lit.add(literal)

for symbol in symbols:

if symbol.isdigit():

lit.add(symbol)

elif symbol.isalnum():

if symbol not in self.keywords:

identifiers.add(symbol)

else:

key.add(symbol)

for keyword in key:

symbol_table.add_symbol("key", keyword)

for identifier in identifiers:

symbol_table.add_symbol("id", identifier)

for literal in lit:

symbol_table.add_symbol("lit", literal)

code_lines = []
print("Enter code:")

while True:

line = input()

if line.strip() == "":

break

code_lines.append(line)

code_snippet = "\n".join(code_lines)

symbol_table = SymbolTable()

symbol_table.parse_code(code_snippet)

symbol_table.display_symbol_table()

while True:

action = input("\nDo you want to add or delete a symbol? (Enter 'add', 'delete', 'display', or 'quit'):
").strip().lower()

if action == 'add':

new_code_snippet = input("Enter new line of code: ")

symbol_table.parse_code(new_code_snippet)

elif action == 'delete':

lexeme = input("Enter the lexeme of the symbol to delete: ").strip()

symbol_table.delete_symbol(lexeme)

elif action == 'display':

symbol_table.display_symbol_table()

elif action == 'quit':

break

else:

print("Invalid action. Please enter 'add', 'delete', or 'quit'.")

symbol_table.display_symbol_table()
Output:

You might also like