SPCC Adheesh
SPCC Adheesh
Program: -
def extract_macros(source_code):
macros = []
lines = source_code.split('\n')
index = 0
for line in lines:
if line.strip().startswith("MACRO"):
macro_name = lines[index+1].strip().split()[0]
macros.append((macro_name, index+1))
index += 1
return macros
def display_macro_name_table(macros):
print("Macro Name Table:")
print("Index\tMacro Name\tMDT Index")
for i, (macro_name, index) in enumerate(macros, start=1):
print(f"{i}\t{macro_name}\t\t{index}")
prog.txt
MACRO
INCR1 &ARG1,&ARG2
A 1,&ARG1
L 2,&ARG2
MEND
MACRO
INCR2 &ARG1,&ARG2
M 1,&ARG1
S 2,&ARG2
MEND
MACRO
COMPLEX &X
INCR1 &X,DATA1
INCR2 &X,DATA2
MEND
OUTPUT: -
Source Code:
MACRO
INCR1 &ARG1,&ARG2
A 1,&ARG1
L 2,&ARG2
MEND
MACRO
INCR2 &ARG1,&ARG2
M 1,&ARG1
S 2,&ARG2
MEND
MACRO
COMPLEX &X
INCR1 &X,DATA1
INCR2 &X,DATA2
MEND
print("Input Code:")
print(code)
print("\nLexical Analysis:")
tokens = lexical_analyzer(code)
for token in tokens:
print(token[0], "-", token[1])
OUTPUT: -
Input Code:
int main() {
int x = 10 ;
float y = 3.14 ;
if ( x == 10 ) {
printf ( " Hello , World ! " ) ;
}
}
Lexical Analysis:
int - Keyword
main() - Unknown
{ - Delimiter
int - Keyword
x - Identifier
= - Operator
10 - Integer Literal
; - Delimiter
float - Keyword
y - Identifier
= - Operator
3.14 - Float Literal
; - Delimiter
if - Keyword
( - Delimiter
x - Identifier
== - Operator
10 - Integer Literal
) - Delimiter
{ - Delimiter
printf - Identifier
( - Delimiter
" - Unknown
Hello - Identifier
, - Delimiter
World - Identifier
! - Unknown
" - Unknown
) - Delimiter
; - Delimiter
} - Delimiter
} – Delimiter
EXP NO 4
Program: -
def eliminate_left_recursion(grammar):
non_terminals = list(grammar.keys())
for A in non_terminals:
productions_A = grammar[A]
if not left_recursive_productions:
continue
return grammar
# Main program
if __name__ == "__main__":
print("Enter the grammar productions:")
user_grammar = parse_grammar()
OUTPUT: -
Enter the grammar productions:
Enter production (or type 'done' to finish): E -> E + T | T
Enter production (or type 'done' to finish): T -> T * F | F
Enter production (or type 'done' to finish): F -> (E)|id
Enter production (or type 'done' to finish): done
for i in pos:
if i not in OPERATORS:
exp_stack.append(i)
else:
print(f't{t} := {exp_stack[-2]} {i} {exp_stack[-1]}')
exp_stack=exp_stack[:-2]
exp_stack.append(f't{t}')
t+=1
OUTPUT: -
INPUT THE EXPRESSION: a+b*9-d+c
PREFIX: +-+a*b9dc
POSTFIX: ab9*+d-c+
### THREE ADDRESS CODE GENERATION ###
t1 := b * 9
t2 := a + t1
t3 := t2 - d
t4 := t3 + c
EXP NO 7
Program: -
import re
expr_buffer = []
optm_code = []
with open(r"sample_code_unopt.txt") as source:
code = source.readlines()
source.close()
print("\nInput Expressions: \n")
for line in code:
print(line)
for line in code:
spline = line.replace(" ", "")
if "=" in spline:
expr = (re.findall(r"(\w)+=\S*", spline))
for y in expr_buffer:
for x in y:
if expr[0] == x:
expr_buffer.remove(y)
if "/" in spline:
expr = (re.findall(r"[a-zA-Z]+[\d]*/[a-zA-Z]+[\d]*", spline))
for x in expr:
if x not in expr_buffer:
expr_buffer.append(x)
s = "t"+ str(expr_buffer.index(x))
optm_code.append(s + " = " + x + "\n")
spline = spline.replace(x, s)
else:
s = "t" + str(expr_buffer.index(x))
spline = spline.replace(x, s)
if "*" in spline:
expr = (re.findall(r"[a-zA-Z]+[\d]*\*[a-zA-Z]+[\d]*", spline))
for x in expr:
if x not in expr_buffer:
expr_buffer.append(x)
s = "t"+ str(expr_buffer.index(x))
optm_code.append(s + " = " + x + "\n")
spline = spline.replace(x, s)
else:
s = "t" + str(expr_buffer.index(x))
spline = spline.replace(x, s)
if "+" in spline:
expr = (re.findall(r"[a-zA-Z]+[\d]*\+[a-zA-Z]+[\d]*", spline))
for x in expr:
if x not in expr_buffer:
expr_buffer.append(x)
s = "t"+ str(expr_buffer.index(x))
optm_code.append(s + " = " + x + "\n")
spline = spline.replace(x, s)
else:
s = "t" + str(expr_buffer.index(x))
spline = spline.replace(x, s)
if "-" in spline:
expr = (re.findall(r"[a-zA-Z]+[\d]*-[a-zA-Z]+[\d]*", spline))
for x in expr:
if x not in expr_buffer:
expr_buffer.append(x)
s = "t"+ str(expr_buffer.index(x))
optm_code.append(s + " = " + x + "\n")
spline = spline.replace(x, s)
else:
s = "t" + str(expr_buffer.index(x))
spline = spline.replace(x, s)
optm_code.append(spline)
print("\nOptimized Expressions with computed compiler variable assignments: \n")
for line in optm_code:
print(line)
OUTPUT: -
Input Expressions:
c = a/b * d
a=c*d
f = a/b * c/d + e
g = f * a/b - c/d
Optimized Expressions with computed compiler variable assignments:
t0 = a/b
t1 = t0*d
c=t1
t1 = c*d
a=t1
t2 = a/b
t3 = c/d
t4 = t2*t3
t5 = t4+e
f=t5
t6 = f*t2
t7 = t6-t3
g=t7
EXP NO 8
Program: -
import re
f = open("IntermediateCode.txt", 'r')
lines = f.readlines()
f.close()
fifo_return_reg = 'R0'
reg = [0] * 13
var = {}
store_seq = []
fifo_reg = 0
operator_list = {'+': 'ADD', '-': 'SUB', '*': 'MUL', '/': 'DIV', '=': 'MOV', '==': 'NE', '>': 'G', '>=': 'GE', '<': 'L',
'<=': 'LE', 'and': 'AND', 'or': 'OR'}
def fifo():
global fifo_reg
global fifo_return_reg
for k, v in var.copy().items():
if v == 'R' + str(fifo_reg):
fifo_return_reg = v
var.pop(k)
if k in store_seq:
store_seq.remove(k)
print("ST", k, ',', v, sep='')
fifo_reg = int(fifo_return_reg[1:]) + 1
return fifo_return_reg
def getreg():
for i in range(0, 13):
if reg[i] == 0:
reg[i] = 1
return 'R' + str(i)
register = fifo()
return register
line = line.split()
length = len(line)
if length == 0:
continue
if length == 1:
print(line[0])
continue
if re.match(r'^t[0-9]+$', line[0]):
continue
if length == 3:
lhs = line[0]
operand = line[2]
if operand not in var:
var[operand] = getreg()
if operand.isalpha():
print("LD", var[operand], ', ', operand, sep="")
else:
print("MOV", var[operand], ', #', operand, sep="")
if lhs in store_seq:
old_reg = var[lhs]
store_seq.remove(lhs)
print("ST", lhs, ',', old_reg, sep='')
var[lhs] = var[operand]
store_seq.append(lhs)
else:
if len(line) >= 5:
oper = line[3]
operand1 = line[2]
operand2 = line[4]
lhs = line[0]
if operand1 not in var:
var[operand1] = getreg()
if operand1.isalpha():
print("LD", var[operand1], ', ', operand1, sep="")
else:
print("MOV", var[operand1], ', #', operand1, sep="")
if operand2 not in var:
var[operand2] = getreg()
if operand2.isalpha():
print("LD", var[operand2], ', ', operand2, sep="")
else:
print("MOV", var[operand2], ', #', operand2, sep="")
operator_print = operator_list.get(oper)
if operator_print:
if lhs in store_seq:
old_reg = var[lhs]
store_seq.remove(lhs)
print("ST", lhs, ',', old_reg, sep='')
var[lhs] = getreg()
store_seq.append(lhs)
print(operator_print, var[lhs], ',', var[operand1], ',', var[operand2], sep=' ')
else:
operand = line[3]
lhs = line[0]
if operand not in var:
var[operand] = getreg()
if operand.isalpha():
print("LD", var[operand], ', ', operand, sep="")
else:
print("MOV", var[operand], ', #', operand, sep="")
if lhs in store_seq:
old_reg = var[lhs]
store_seq.remove(lhs)
print("ST", lhs, ',', old_reg, sep='')
var[lhs] = getreg()
store_seq.append(lhs)
print("NOT", var[lhs], ',', var[operand], sep='')
for i in store_seq:
print("ST", i, ',', var[i], sep='')
IntermediateCode.txt
a = 10
b=9
t0= 10 + 9
tl = 19 + 100
c = 119
e = 10
f=8
t2 = 10 * 8
d = 80
|0:
t3 = 10 >= 9
t4 = not 1
if 0 goto |1
a = 19
15 = 80 * 100
g = 8000
|1:
u = 10
j = 99
OUTPUT: -
MOVR0, #10
MOVR1, #9
NOTR2,R1
MOVR3, #19
MOVR4, #100
ADD R5 , R3 , R4
MOVR6, #119
MOVR7, #8
MOVR8, #80
|0:
MOVR9, #0
BNEZ R9 , |1
STa,R0
MUL R10 , R8 , R4
MOVR11, #8000
|1:
MOVR12, #99
STb,R1
STt0=,R2
STtl,R5
STc,R6
STe,R0
STf,R7
STd,R8
STa,R3
ST15,R10
STg,R11
STu,R0
STj,R12