Digital Assignment -1
COURSE NAME: Compiler Design Lab
COURSE CODE: BCSE307P
FACULTY NAME: Sabyasachi Kamila
NAME OF THE STUDENT: [Link]
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):
[Link] = token
[Link] = lexeme
def __str__(self):
return f"{[Link]}, {[Link]}"
class SymbolTable:
def __init__(self):
[Link] = []
[Link] = {"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):
[Link](Symbol(token, lexeme))
print(f"Inserted: ({token}, {lexeme})")
def delete_symbol(self, lexeme):
initial_length = len([Link])
[Link] = [s for s in [Link] if [Link] != lexeme]
if len([Link]) < 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 [Link]:
print(symbol)
def parse_code(self, code):
identifiers = set()
key = set()
lit = set()
literals = [Link](r'"[^"]*"', code)
code = [Link](r'"[^"]*"', '', code)
symbols = [Link](r'\b\w+\b', code)
for literal in literals:
[Link](literal)
for symbol in symbols:
if [Link]():
[Link](symbol)
elif [Link]():
if symbol not in [Link]:
[Link](symbol)
else:
[Link](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 [Link]() == "":
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: