CODIGO 3 DIRECCIONES (Ejemplos Basicos Resueltos)
Ejemplo:
Genera el codigo intermedio de : (a * b + h) - j * k + 1
Reutilizacion de temporales
t1 = a * b t1= a * b
t2 = t1 + h t2 = t1 + h
t3 = j * k t1 = t1 - t2
t4 = t2 - t3 t1 = t1 + 1
t5 = t4 + 1
Genera el codigo intermedio de : a + b * (c + d) * (f + k ) * d
Reutilizacion de temporales
t1 = c + d t1= c + d
t2 = t1 * b t1 = t1 * b
t3 = f + k t2 = f + k
t4 = t2 * t3 t1 = t1 * t2
t5 = t4 * d t1 = t1 * d
t6 = a + t5 t1 = a + t1
Escriba el codigo de 3 direcciones de: a > b OR b < k + h AND f * b + d == z OR NOT k>f
t1 = a > b
t2 = k + h
t3 = b < t2
t4 = f * b
t5 = t4 + d
t6 = t5 == z
t7 = t3 AND t6
t8 = t1 OR t7
t9 = k > f
t10 = NOT t9
t11 = t8 OR t10
Hacer el codigo 3 direcciones de la expresion : a > b + h OR b == d teniendo la restriccion de que los operadores realaciones no se puede escribir directamente (t1= t2 >b)
t1 = b + h
if a > t1 the goto l1
goto l2
l1:
t2 =1
goto l3
l2:
t2=0
l3:
if b ==d then goto l4
goto l5
l4:
t3= 1
goto l5
l5:
t3 =0
l6:
t4= t2 or t3
Corto Circuito
Es ejecutar el codigo de manera tal que si estamos evaluando un operador logico AND y la primera operacion es falsa el resultado sera falso. De igual manera si es OR si la primera es comparacion es verdadera entonces es verdadera. Esto segun las propiedades logicas del operador.
OR
Ejemplo:
Generar codigo de 3 direcciones de la entrada: a > b OR b < f OR d == k
if a > b then goto l1
goto l2
l2:
if b < f then goto l3
goto l4
l4:
if d == k then goto l5
goto l6
l1, l3, l5:
t1=1
goto l7
l3, l4, l6:
t1=0
l7:
AND
Ejemplo:
Generar codigo de 3 direcciones de la entrada: a > b AND b < f AND d == k
if a > b then goto l1
goto l2
l1:
if b < f then goto l3
goto l4
l3:
if d == k then goto l5
goto l6
l5:
t1=1
goto l7
l2, l4, l6:
t1=0
l7:
XOR
Generar codigo 3D para la entrada: a XOR b
if a then goto l1
goto l2
l2:
if b then goto l3
goto l4
l1:
if b then goto l5
goto l6
l3,l6:
goto l7
l5,l4:
l7:
NOT ( ! )
Es la negacion de cualquier expresion de tipo booleana, si esta era falsa sera entonces verdadera.
Generar codigo de 3D para la siguiente codicion : NOT (a < 5 AND b == 20)
Generar codigo de 3 direcciones para la entrada: a > b AND b > f OR d == k
Codigo 3D:
if a > b then goto l1
goto l2
l1:
if b > f then got l3
goto l4
l2,l4:
if d ==k then goto l5
goto l6
l3, l5:
t1=1
goto l7
l6 :
t1 = 0
l7:
Generar codigo de 3 Direcciones de la siguiente entrada: (a > b OR b < f ) AND d == k
if a > b then goto l1
goto l2
l2:
if b < f then goto l3
goto
l1,l3:
if d == k then goto l5
goto l6
l5:
t1 =1
goto l7
l4, l6:
t1 = 0
l7:
Codigo 3D:
Switch(E)
{
val1: sentencias 1
val2: sentencias 2
.
.
.
valn: sentencias n
}
Codigo 3D:
< codigo E >
if Eval != val 1 then goto l1
< codigo sentencia 1>
goto Lsalida
l1:
if Eval != val2 then goto l2
< codigo sentencia 2 >
goto Lsalida
l2:
.
.
.
if Eval != valn then goto ln
< codigo sentencia n >
goto Lsalida
ln:
Lsalida:
Ejemplo:
Genera el codigo intermedio de : (a * b + h) - j * k + 1
Reutilizacion de temporales
t1 = a * b t1= a * b
t2 = t1 + h t2 = t1 + h
t3 = j * k t1 = t1 - t2
t4 = t2 - t3 t1 = t1 + 1
t5 = t4 + 1
Genera el codigo intermedio de : a + b * (c + d) * (f + k ) * d
Reutilizacion de temporales
t1 = c + d t1= c + d
t2 = t1 * b t1 = t1 * b
t3 = f + k t2 = f + k
t4 = t2 * t3 t1 = t1 * t2
t5 = t4 * d t1 = t1 * d
t6 = a + t5 t1 = a + t1
Escriba el codigo de 3 direcciones de: a > b OR b < k + h AND f * b + d == z OR NOT k>f
t1 = a > b
t2 = k + h
t3 = b < t2
t4 = f * b
t5 = t4 + d
t6 = t5 == z
t7 = t3 AND t6
t8 = t1 OR t7
t9 = k > f
t10 = NOT t9
t11 = t8 OR t10
Hacer el codigo 3 direcciones de la expresion : a > b + h OR b == d teniendo la restriccion de que los operadores realaciones no se puede escribir directamente (t1= t2 >b)
t1 = b + h
if a > t1 the goto l1
goto l2
l1:
t2 =1
goto l3
l2:
t2=0
l3:
if b ==d then goto l4
goto l5
l4:
t3= 1
goto l5
l5:
t3 =0
l6:
t4= t2 or t3
Corto Circuito
Es ejecutar el codigo de manera tal que si estamos evaluando un operador logico AND y la primera operacion es falsa el resultado sera falso. De igual manera si es OR si la primera es comparacion es verdadera entonces es verdadera. Esto segun las propiedades logicas del operador.
OR
Ejemplo:
Generar codigo de 3 direcciones de la entrada: a > b OR b < f OR d == k
if a > b then goto l1
goto l2
l2:
if b < f then goto l3
goto l4
l4:
if d == k then goto l5
goto l6
l1, l3, l5:
t1=1
goto l7
l3, l4, l6:
t1=0
l7:
AND
Ejemplo:
Generar codigo de 3 direcciones de la entrada: a > b AND b < f AND d == k
if a > b then goto l1
goto l2
l1:
if b < f then goto l3
goto l4
l3:
if d == k then goto l5
goto l6
l5:
t1=1
goto l7
l2, l4, l6:
t1=0
l7:
XOR
Generar codigo 3D para la entrada: a XOR b
if a then goto l1
goto l2
l2:
if b then goto l3
goto l4
l1:
if b then goto l5
goto l6
l3,l6:
goto l7
l5,l4:
l7:
NOT ( ! )
Es la negacion de cualquier expresion de tipo booleana, si esta era falsa sera entonces verdadera.
Ejemplo:
Generar codigo de 3D para la siguiente codicion : NOT (a < 5 AND b == 20)
if a < 5 then goto l1
goto l2
l1:
if b == 20 then goto l3
goto l4
l2,l4:
t1 = 0
goto l5
l3:
t1 = 1
l5:
EJEMPLOS MEZCLADOSGenerar codigo de 3 direcciones para la entrada: a > b AND b > f OR d == k
Codigo 3D:
if a > b then goto l1
goto l2
l1:
if b > f then got l3
goto l4
l2,l4:
if d ==k then goto l5
goto l6
l3, l5:
t1=1
goto l7
l6 :
t1 = 0
l7:
Generar codigo de 3 Direcciones de la siguiente entrada: (a > b OR b < f ) AND d == k
if a > b then goto l1
goto l2
l2:
if b < f then goto l3
goto
l1,l3:
if d == k then goto l5
goto l6
l5:
t1 =1
goto l7
l4, l6:
t1 = 0
l7:
REPEAT
Entrada : repeat { Sentencias } until cond
Linicio:
< codigo sentencias >
< codigo condicion >
Lf:
goto Linicio
Lv:
WHILE
Entrada: while cond Do { Sentencias }
Linicio:
< codigo condicion>
Lv:
< codigo sentencias>
Lf:
-------------------------------------------------------------------------------------------------------------
Entrada : Do { Sentencias } while cond
goto Lsent
Lcond:
< codigo condicion >
Lv:
Lsent:
< Codigo sentencias >
goto Lcond
Lf:
--------------------------------------------------------------------------------------------------------------
Escriba el codigo en 3 Direcciones:
if a > b and k > f
{
while a > b do
while a > b do
{
a = a + b
a = a + b
if a == b
{
b = b - 1
}else{
break
}
b= b + 1
}
while a + b * 2 > f + k
{
a = b + f * k
break
}
break
}
}else{
a = a + b
}
if a > b then goto l1
goto l2
l1:
if k > f then goto l3
goto l4
l3:
l5:
if a > b then goto l7
goto l8
l7:
t1 = a + b
a = t1
if a == b then goto l9
goto l10
l9:
t2 = b -1
b = t2
goto l11
l10:
goto l6
l11:
t3 = b + 1
b = t3
l12:
t4 = b * 2
t5 = a + t4
t6 = f + k
if t5 > t6 then goto l4
goto l5
l14:
t7 = f * k
t8 = b + t7
a = t8
goto l13
goto l12
l15:
l13:
goto l6
goto l5
l8:
l6:
goto l16
l2, l4:
t9 = a + b
a = t9
l16:
Break's: l6,l3
SWITCH
Entrada:SWITCH
Switch(E)
{
val1: sentencias 1
val2: sentencias 2
.
.
.
valn: sentencias n
}
Codigo 3D:
< codigo E >
if Eval != val 1 then goto l1
< codigo sentencia 1>
goto Lsalida
l1:
if Eval != val2 then goto l2
< codigo sentencia 2 >
goto Lsalida
l2:
.
.
.
if Eval != valn then goto ln
< codigo sentencia n >
goto Lsalida
ln:
Lsalida:
Buenos ejemplos amigo
ReplyDelete