1 / 12

Traducción por Sintaxis

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.

sef
Download Presentation

Traducción por Sintaxis

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Traducción por Sintaxis De la sintaxis a la traducción

  2. 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

  3. 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

  4. 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...

  5. 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,00 0102 7404 JZ 0108 0104 FEC8 DEC AL 0106 EBF8 JMP 0100 0108 FEC0 INC AL 010A FEC0 INC AL 010C FEC0 INC AL 010E 80FC00 CMP AH,00 0111 7404 JZ 0117 0113 FECC DEC AH 0115 EBF7 JMP 010E 0117 FEC4 INC AH 0119 FEC4 INC AH 011B FEC4 INC AH 011D 80FB00 CMP BL,00 0120 7404 JZ 0126 0122 FECB DEC BL 0124 EBF7 JMP 011D 0126 80FF00 CMP BH,00 0129 7404 JZ 012F 012B FECF DEC BH 012D EBF7 JMP 0126

  6. ¿Cómo se logra? • Definición dirigida por la sintaxis Programa -> 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

  7. Programa -> 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 Prog.t := Ins.t Prog.t := Rut.t Rut.t := Ins.t & NL & Ins.t Rut.t := Ins.t & Rut1.t Ins.t := “nop” Ins.t := “inc ” & Var.t Ins.t := “dec ” & Var.t Ins.t := Pru.t & Prog.t Pru.t := “cmp “ & Var.t & “,0” & NL & “jz ” & Prog.p Pru.t := “cmp ” & Var.t & “,0” & NL & “jnz ” & Prog.p Reglas semánticas

  8. Programa -> 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 Prog.p := Ins.p Prog.p := Rut.p Rut.p := Ins.p + Ins.p Rut.p := Ins.p + Rut1.p Ins.p := 1 Ins.p := 2 Ins.p := 2 Ins.p := Pru.p + Prog.p Pru.p := 4 Pru.p := 4 ¿Prog.p?

  9. Instrucción While Prueba do Programa { Rutina } Variable <> 0 Instrucción ; Instrucción Variable ++ Variable -- Arbol sintáctico Prog.t = cmpah,0 NLjz Prop.pinc al NL dec ah While v <> 0 do {x++; v--} Programa Ins.t = cmpah,0 NLjz Prop.pinc al NL dec ah Prog.t = inc al NL dec ah Pru.t = cmpah,0 NLjz Prop.p Rut.t = inc al NL dec ah Var.t = ah Ins.t = inc al Ins.t = dec ah Var.t = al Var.t = ah

  10. Expr -> Expr + Término Expr -> Expr - Término Expr -> Término Término -> 0 Término -> 1 Término -> 2 .... Término -> 9 Expr.t := Expr.t & Término.t & “+” Expr.t := Expr.t & Término.t & “-” Expr.t := Término.t Término.t := “0” Término.t := “1” Término.t := “2” .... Término.t := “9” Infijo a postfijo

  11. Expr -> Expr1 + Término Expr -> Expr1 - Término Expr -> Término Término -> 0 Término -> 1 Término -> 2 .... Término -> 9 Expr.t := Expr1.t & Término.t & “+” Expr.t := Expr1.t & Término.t & “-” Expr.t := Término.t Término.t := “0” Término.t := “1” Término.t := “2” .... Término.t := “9” Infijo a postfijo

  12. Ejercicio • Evalúe la expresión: 2+5-4 • Construya el árbol de análisis sintáctico • Transforme a postfijo con las reglas semánticas dadas • Modifique las reglas semánticas para que los números se separen por coma

More Related