You are on page 1of 6

M.

MUKARRAM (02-134192-054)

Lab 10: Symbol Table

Objective(s): Upon completion of this lab session, learners will be able to


Implement symbol table

Exercise 1

Write a program to implement symbol table


Input
A program stored in a file. Read all the variables
#include <iostream>
#include <string>
using namespace std;
string inp[]={"int","a",";","int","a",";"};
int S=0;
string N,T;
struct symbol{
string name;
string type;
int scope;
};
symbol a[2];
void insert(string N,string T,int S){
int j=0;
a[j].name=N;
a[j].type=T;
a[j].scope=S;
j++;
}
bool lookup(string N, int S){
for(int i=0;i<inp[i].length();i++){
if(a[i].name==N && a[i].scope==S){
return false;
}
}
M. MUKARRAM (02-134192-054)

return true;
}
bool check(){
int i=0;
while(i<6){
if(inp[i]=="int"){
T =inp[i];
i++;
if(inp[i]=="a" || inp[i]=="b"){
N=inp[i];
i++;
if(lookup(N,S)){
insert(N,T,S);
}
else {
cout<<"redeclaration";
}
if(inp[i]==";"){

i++;
}
}
}
}
return true;
}
int main(){
if(check()){
cout<<"\ninserted";}}

Output
M. MUKARRAM (02-134192-054)

Exercise 2
Write a program to implement functions of lookup and Insert to push the
element in Symbol Table
Input:
import tb as tb
from beautifultable import
BeautifulTable
import re
inp = input('Enter your
input ,separated by space : ')
dt = ['int', 'char', 'string',
'double', 'float']
tab = []
tok = []
tok = inp.split(' ')
print(tok, "\n")
def lookup(name, scope):
i=0
if len(tab) != 0:
while (i < len(tab)):
if name in tab[i] and
scope in tab[i]:
return False
else:
i += 1
return True
else:
return True
def insert(name, typee,
scope):
tb.append([name, typee,
scope])
def main():
table = BeautifulTable()
table.column_headers =
M. MUKARRAM (02-134192-054)

["Name", "Type", "Scope"]


error = False
flag = True
scopee = -1
i=0
while (i < len(tok)):
if tok[i] == '{':
flag = True
scopee += 1
i += 1
while (i < len(tok) and
flag == True):
namee = ""
tipe = ""
if tok[i] in dt:
tipe = tok[i]
i += 1
if re.match('^[a-
zA-Z]+$', tok[i]):
namee = tok[i]
if
lookup(namee, scopee):
insert(namee,
tipe, scopee)

table.append_row([namee,
tipe, scopee])
else:

print("ERROR:", namee, " is


already defined at scope",
scopee)
error = True
i += 1
if tok[i] == '=':
None
i += 1
if tok[i - 3] ==
'int' and re.match('^[0-9]+$',
M. MUKARRAM (02-134192-054)

tok[i]):
i += 1
elif tok[i - 3] ==
'string' and re.match('^"[\w]
+"$', tok[i]):
i += 1
elif tok[i - 3] ==
'char' and re.match("^'[A-Za-
z0-9]'$", tok[i]):
i += 1
else:

print("ERROR:", namee, "'s


Datatype Mismatch at
scope", scopee)
error = True
if len(tab) !=
0:
tab.pop()

table.pop_row()
i += 1
if tok[i] == ';':
i += 1
else:
print("ERROR:",
namee, "'s Terminator
missing at scope", scopee)
error = True
if len(tab) != 0:
tab.pop()

table.pop_row()
if tok[i] == '}':
i += 1
flag = False

elif re.match('^[a-zA-
Z]+$', tok[i]):
M. MUKARRAM (02-134192-054)

print("ERROR:",
tok[i], " is not Declared as
scope", scopee)
error = True
i += 1
else:
i += 1
else:
i += 1
if (not error):
print("No Error
Found!!!")
print("\nSymbol Table")
print(table)
main()

Output:

You might also like