traducción por sintaxis

12
Traducción por Sintaxis De la sintaxis a la traducción

Upload: sef

Post on 18-Mar-2016

33 views

Category:

Documents


0 download

DESCRIPTION

Traducción por Sintaxis. De la sintaxis a la traducción. 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. Recordemos programa while. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Traducción por Sintaxis

Traducción por Sintaxis

De la sintaxis a la traducción

Page 2: Traducción por Sintaxis

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

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

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

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

¿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

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

¿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

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

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

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

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