Professional Documents
Culture Documents
MUKARRAM (02-134192-054)
Exercise 1
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)
table.append_row([namee,
tipe, scopee])
else:
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:
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: