Traducción por Sintaxis
description
Transcript of Traducción por Sintaxis
![Page 1: Traducción por Sintaxis](https://reader036.fdocuments.es/reader036/viewer/2022083015/56814acd550346895db7e62a/html5/thumbnails/1.jpg)
Traducción por Sintaxis
De la sintaxis a la traducción
![Page 2: Traducción por Sintaxis](https://reader036.fdocuments.es/reader036/viewer/2022083015/56814acd550346895db7e62a/html5/thumbnails/2.jpg)
Recordemos programa while
{ y := B; w := B;x := 0; v := 0;While w <> 0 do {
x++;v := x*x;w := y – v
};w := v – y;While w <> 0 do {
x--;w := 0;
}}
Sea B = 3
![Page 3: Traducción por Sintaxis](https://reader036.fdocuments.es/reader036/viewer/2022083015/56814acd550346895db7e62a/html5/thumbnails/3.jpg)
Recordemos programa while
{ y := 3; w := 3;x := 0; v := 0;While w <> 0 do {
x++;v := x*x;w := y – v
};w := v – y;While w <> 0 do {
x--;w := 0;
}}
Se transforma en
![Page 4: Traducción por Sintaxis](https://reader036.fdocuments.es/reader036/viewer/2022083015/56814acd550346895db7e62a/html5/thumbnails/4.jpg)
Recordemos programa while
{ While y <> 0 do y--;y++; y++; y++;While w <> 0 do w--;w++; w++; w++;While x <> 0 do x--;While v <> 0 do v--;While w <> 0 do {
x++;...
}
Demasiado largo...
![Page 5: Traducción por Sintaxis](https://reader036.fdocuments.es/reader036/viewer/2022083015/56814acd550346895db7e62a/html5/thumbnails/5.jpg)
Assembler (Debug)
{ While y <> 0 do y--;y++; y++; y++;While w <> 0 do w--;w++; w++; w++;While x <> 0 do x--;While v <> 0 do v--;While w <> 0 do {
x++;...
}
0100 3C00 CMP AL,000102 7404 JZ 01080104 FEC8 DEC AL0106 EBF8 JMP 01000108 FEC0 INC AL010A FEC0 INC AL010C FEC0 INC AL010E 80FC00 CMP AH,000111 7404 JZ 01170113 FECC DEC AH0115 EBF7 JMP 010E0117 FEC4 INC AH0119 FEC4 INC AH011B FEC4 INC AH011D 80FB00 CMP BL,000120 7404 JZ 01260122 FECB DEC BL0124 EBF7 JMP 011D0126 80FF00 CMP BH,000129 7404 JZ 012F012B FECF DEC BH012D EBF7 JMP 0126
![Page 6: Traducción por Sintaxis](https://reader036.fdocuments.es/reader036/viewer/2022083015/56814acd550346895db7e62a/html5/thumbnails/6.jpg)
¿Cómo se logra?
Definición dirigida por la sintaxisPrograma -> Instrucción | { Rutina }Rutina -> Instrucción ; Instrucción |Instrucción ; Rutina
Instrucción -> nil | Variable ++ | Variable -- |While Prueba do Programa
Prueba -> Variable <> 0 | Variable = 0
![Page 7: Traducción por Sintaxis](https://reader036.fdocuments.es/reader036/viewer/2022083015/56814acd550346895db7e62a/html5/thumbnails/7.jpg)
Reglas semánticas
Programa -> Instrucción | { Rutina }
Rutina -> Instrucción ; Instrucción |
Instrucción ; RutinaInstrucción -> nil | Variable ++
|Variable -- |While Prueba do
ProgramaPrueba -> Variable <> 0 |
Variable = 0
Prog.t := Ins.tProg.t := Rut.tRut.t := Ins.t & NL & Ins.tRut.t := Ins.t & Rut1.t
Ins.t := “nop”Ins.t := “inc ” & Var.tIns.t := “dec ” & Var.tIns.t := Pru.t & Prog.tPru.t := “cmp “ & Var.t & “,0”
& NL & “jz ” & Prog.pPru.t := “cmp ” & Var.t & “,0”
& NL & “jnz ” & Prog.p
![Page 8: Traducción por Sintaxis](https://reader036.fdocuments.es/reader036/viewer/2022083015/56814acd550346895db7e62a/html5/thumbnails/8.jpg)
¿Prog.p?
Programa -> Instrucción | { Rutina }
Rutina -> Instrucción ; Instrucción |
Instrucción ; RutinaInstrucción -> nil | Variable ++
|Variable -- |While Prueba do
ProgramaPrueba -> Variable <> 0 |
Variable = 0
Prog.p := Ins.pProg.p := Rut.pRut.p := Ins.p + Ins.pRut.p := Ins.p + Rut1.p
Ins.p := 1Ins.p := 2Ins.p := 2Ins.p := Pru.p + Prog.pPru.p := 4Pru.p := 4
![Page 9: Traducción por Sintaxis](https://reader036.fdocuments.es/reader036/viewer/2022083015/56814acd550346895db7e62a/html5/thumbnails/9.jpg)
Arbol sintáctico
While v <> 0 do {x++; v--}Programa
Instrucción
While Prueba do Programa
{ Rutina }
Instrucción ; Instrucción
Variable ++ Variable --Var.t = al Var.t = ah
Ins.t = inc al Ins.t = dec ah
Rut.t = inc al NL dec ah
Prog.t = inc al NL dec ah
Variable<> 0Var.t = ah
Pru.t = cmp ah,0 NLjz Prop.p
Ins.t = cmp ah,0 NLjz Prop.p
inc al NL dec ah
Prog.t = cmp ah,0 NLjz Prop.p
inc al NL dec ah
![Page 10: Traducción por Sintaxis](https://reader036.fdocuments.es/reader036/viewer/2022083015/56814acd550346895db7e62a/html5/thumbnails/10.jpg)
Infijo a postfijo
Expr -> Expr + TérminoExpr -> Expr - TérminoExpr -> TérminoTérmino -> 0Término -> 1Término -> 2....Término -> 9
Expr.t := Expr.t & Término.t & “+”
Expr.t := Expr.t & Término.t & “-”
Expr.t := Término.tTérmino.t := “0”Término.t := “1”Término.t := “2”....Término.t := “9”
![Page 11: Traducción por Sintaxis](https://reader036.fdocuments.es/reader036/viewer/2022083015/56814acd550346895db7e62a/html5/thumbnails/11.jpg)
Infijo a postfijo
Expr -> Expr1 + TérminoExpr -> Expr1 - TérminoExpr -> TérminoTérmino -> 0Término -> 1Término -> 2....Término -> 9
Expr.t := Expr1.t & Término.t & “+”
Expr.t := Expr1.t & Término.t & “-”
Expr.t := Término.tTérmino.t := “0”Término.t := “1”Término.t := “2”....Término.t := “9”
![Page 12: Traducción por Sintaxis](https://reader036.fdocuments.es/reader036/viewer/2022083015/56814acd550346895db7e62a/html5/thumbnails/12.jpg)
Ejercicio
1. Evalúe la expresión: 2+5-42. Construya el árbol de análisis sintáctico3. Transforme a postfijo con las reglas
semánticas dadas4. Modifique las reglas semánticas para que
los números se separen por coma