SPCC 2203121
SPCC 2203121
Code:
import re
keywords = {"int", "float", "char", "continue", "for", "if", "break", "while",
"string", "double"}
operators = {"+", "-", "*", "%", "/", "<", ">", "="}
separators = {";", ",", ":", "(", ")", "[", "]", "{", "}"}
ans = {
"keywords": set(),
"operators": set(),
"separators": set(),
"constants": set(), "identifier": set()
}
def split_string(s): split = []
temp = '' i = 0
while i < len(s): ch = s[i]
if ch.isspace(): if temp:
split.append(temp) temp = ''
elif ch in separators: if temp:
split.append(temp) temp = ''
split.append(ch) else:
temp += ch i += 1
if temp:
split.append(temp) return split
n = int(input("Enter number of lines: ")) print("Enter your code line by line:")
for _ in range(n):
s = input()
split = split_string(s) for token in split:
if not token: continue
if token.isdigit(): ans["constants"].add(token)
elif token in keywords: ans["keywords"].add(token)
elif token in operators: ans["operators"].add(token)
elif token in separators: ans["separators"].add(token)
elif token.startswith('"') and token.endswith('"'):
ans["constants"].add(token[1:-1])
else:
if len(token) > 1 and token[1] in operators: ans["identifier"].add(token[0])
ans["operators"].add(token[1])
else:
ans["identifier"].add(token) for key, value in ans.items():
print(f"{key}: {' '.join(sorted(value))}")
Output:
EXPERIMENT 2:
Aim: To study and implement programs using LEX and YACC tool.
Exp 1: Code:
%{
#include <stdio.h>
int yywrap() { return 1; } %}
%%
\n { printf("\nHello world"); } %%
int main() { yylex();
return 0; }
Output:
%%
\n {
printf("\nHello %s.\n", name);
return 1;
}
%%
Code:
%{
#include <stdio.h> void display(int flag);
int yywrap() { return 1; } // To avoid linker errors
%}
%%
[aeiouAEIOU][a-zA-Z]* { int flag = 1; display(flag);
return 1;
}
[a-zA-Z]+ {
int flag = 0; display(flag);
return 1;
}
.|\n ;
%%
Output:
%%
[a-
zA-
Z]+
\n {
int
flag
= 1;
displa
y(yyte
xt,
flag);
return
1;
}
[0-9]+\n {
int
flag
= 0;
dis
pla
y(y
yte
xt,
flag
);
retu
rn
1;
}
.+\n {
int flag = 2; // for
other symbols
display(yytext,
flag);
return 1;
}
%%
Output:
5. Program to count number of lines and words.
Code:
%{
#include <stdio.h>
int lc = 0, ch = 0;
%}
%%
\n { lc++; }
[^\n]+ { ch += yyleng; }
%%
Output:
Experiment 3:
EXPERIMENT 3:
Aim: Write a program to implement the FIRST and FOLLOW set for the given
grammar.
Code:
def split(init): productions = [] for curr in init:
index = -1 lhs = '' rhs_list = []
for j in range(len(curr)): if curr[j] == '-':
lhs = curr[j - 1] productions.append((lhs, [])) index = j + 1
continue
if j > index and curr[j] == '|': rhs_list = ''
for k in range(index + 1, j): rhs_list += curr[k]
productions[-1][1].append(rhs_list) index = j
elif j > index and j == len(curr) - 1: rhs_list = ''
for k in range(index + 1, j + 1): rhs_list += curr[k]
productions[-1][1].append(rhs_list) return productions
for _ in range(5):
for lhs, rules in productions: for rhs in rules:
for i, B in enumerate(rhs): if B.isupper():
epsilon_chain = True
for j in range(i + 1, len(rhs)): next_sym = rhs[j]
if next_sym.islower():
follow.setdefault(B, set()).add(next_sym) epsilon_chain = False
break
elif next_sym.isupper():
for f in first_str.get(next_sym, set()): if f != '@':
follow.setdefault(B, set()).add(f) if '@' in first.get(next_sym, set()):
continue
else:
epsilon_chain = False break
if epsilon_chain:
follow.setdefault(B, set()).update(follow.get(lhs, set()))
return follow
productions = split(init)
first = compute_first(productions) start_symbol = productions[0][0]
follow = compute_follow(productions, first, start_symbol)
Output:
Exp 4:
EXPERIMENT 4:
Aim: Write a program to implement Parser.
Code:
def split(init, n): productions = [] for i in range(n):
curr = init[i] index = -1 prod = []
for j in range(len(curr)): if curr[j] == '-':
productions.append((c
urr[j - 1], [])) index = j
+1
continue
if j > index and curr[j] == '|':
productions[-1]
[1].append(curr[index + 1:j]) index
=j
continue
if j > index and j == len(curr) - 1:
productions[-1]
[1].append(curr[index + 1:j + 1])
index = j
return productions
def
inputF_F(pro
ductions):
firsttt = []
follo
w=
[]
term
inals
=
set()
print("\nFirst:")
for prod, f_set in firsttt:
print(f"{prod}: {',
'.join(f_set)}")
terminals.update(f_se
t - {'@'})
print("\nFollow:")
for non_term, f_set in follow:
print(f"{non_term}: {',
'.join(f_set)}")
terminals.update(f_set)
'.join(terminals))
table = []
k=0
prod_index = 0
productions = split(init, n)
for non_term, prods in
productions: print(f"{non_term}
-> {' | '.join(prods)}")
firsttt, follow, terminals =
inputF_F(productions)
parse_table(productions, firsttt,
follow, terminals)
Output:
Exp 5:
EXPERIMENT 5:
Aim: Write a program to implement Three Address Code (TAC).
Code:
op_precedence = { '(': 5,
'*': 4,
'/': 3,
'+': 2,
'-': 1,
'$': 0
}
def is_operator(ch):
return ch in op_precedence
Output:
Exp 6:
EXPERIMENT 6:
Code:
data = [
't1=a+b',
't2=5',
't3=a+b', 't4=5+t3',
't5=a+b', 't6=a+t2',
]
Output:
Exp 7:
Caption
Caption
EXPERIMENT 7:
Aim: a) Write a program to implement Pass 1 of
Multi-Pass Assembler
b) Write a program to implement Pass 2 of Multi-Pass
Assembler
A.
Code:
opcode_table = { 'ADD': 100,
'SUB': 200,
'MUL': 300,
'DIV': 400,
'JUMP': 500
}
def parse_line(line):
parts = line.strip().split()
if not parts:
return label, opcode, operands
elif len(parts) == 1:
if parts[0] not in opcode_table:
label = parts[0] else:
opcode = parts[0]
if label:
if label in symbol_table:
print(f"Error: Duplicate symbol {label}") return None
symbol_table[label] = location_counter
symbol_table = pass1(assembly_code)
if symbol_table: print("\nSymbol Table:")
for symbol, address in symbol_table.items(): print(f"{symbol} : {address}")
Output:
B.
Code:
opc
ode
_ta
ble
=
{ 'A
DD'
:
100
,
'SU
B':
200
,
'MUL': 300,
'DIV': 400,
'JUMP': 500
}
def parse_line(line):
parts = line.strip().split()
if not parts:
return label, opcode, operands
if opcode ==
'START' and
operands: try:
location_counte
r=
int(operands[0])
except ValueError:
print(f"Invalid START
address: {operands[0]}")
return None
continue
if
opc
ode
==
'EN
D':
bre
ak
if label:
if label in symbol_table:
print(f"Error: Duplicate
symbol {label}") return
None
symbol_table[label] = location_counter
if opcode in
opcode_tabl
e:
location_cou
nter += 1
elif opcode not in ['START', 'END', None]:
print(f"Warning: Unknown opcode '{opcode}'
def pass2(assembly_code,
symbol_table):
machine_code = []
machine_opcode =
opcode_table[opcode]
resolved_operands = []
for operand in operands:
if operand in symbol_table:
resolved_operands.append(str(symbo
l_table[operand]))
else:
resolved_operands.append(operand)
return machine_code
print("Enter assembly code line-by-line. Type 'END' on a line by itself to
finish.\n") assembly_code = []
machine_code =
pass2(assembly_code,
symbol_table) if machine_code:
print("\nMachine Code:")
for instruction in
machine_code:
print(instruction)
Output:
Exp 8:
Caption
Caption
NAME: Tanmay Sarode
BATCH: C31
ROLL NUMBER: 2203145
EXPERIMENT 8:
Aim: Write a program to implement Multi-Pass Macroprocessor.
Code:
def split_string(s): return s.strip().split()
print("\nMDT:")
for key in
sorted(mdt.k
eys()):
print(f"{key}
{mdt[key]}")
print("\nMNT:")
for key in
sorted(mnt.k
eys()):
print(f"{key}
{mnt[key]}")
def main():
n = int(input()) code = []
for _ in range(n): line = input()
split = split_string(line) code.append(split)
Code:
def
split_string(s,
grammar):
left, right =
s.split("->")
grammar[left.strip()] = [prod.strip() for prod in right.split("|")]
def
remove_left_recursion(grammar):
modifications = {}
for non_terminal, productions in
grammar.items(): alpha = []
beta = []
new_non_terminal = non_terminal + "'"
if alpha:
modifications[non_terminal] = beta
modifications[new_non_terminal] = alpha +
["@"]
else:
modifications[non_terminal] =
productions return modifications
def print_grammar(grammar):
for nt, prods in
grammar.items():
print(f"{nt} -> {' |
'.join(prods)}")
modified_grammar = remove_left_recursion(grammar)
print("\nGrammar after removing left
recursion:")
print_grammar(modified_grammar)
Output:
Exp 10:
EXPERIMENT 10:
Aim: Write a program to implement Code Generation.
Code:
def main():
n = int(input("Enter the number of
expressions: ")) expressions = {}
expr_registers = {} codeGen = [] count = 0
opcodes = { "+": "ADD",
"-": "SUB",
"*": "MUL",
"/": "DIV"
}
for i in range(n):
exp = input(f"Enter TAC expression {i + 1} (e.g., t1 = a
+ b): ").strip() lhs, rhs = exp.split('=')
lhs = lhs.strip() rhs = rhs.strip()
{expr_registers[op2]}, {reg}"
codeGen.append(f"# {exp}\n{code}")
Output:
WRITTEN ASSIGNMENT 1:
Caption
Caption
Caption
Caption
Caption
Caption
Caption
Caption