1 / 17

Análisis Sintáctico

Análisis Sintáctico. Determinar si una cadena puede ser generada. Analizador sintáctico. ¿Es posible construír el árbol? Métodos: Descendente De la raíz a las hojas Popular y eficiente Ascendente De las hojas a la raíz Más gramáticas. Analizador descendente. Algoritmo

tricia
Download Presentation

Análisis Sintáctico

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. Análisis Sintáctico Determinar si una cadena puede ser generada

  2. Analizador sintáctico • ¿Es posible construír el árbol? • Métodos: • Descendente • De la raíz a las hojas • Popular y eficiente • Ascendente • De las hojas a la raíz • Más gramáticas

  3. Analizador descendente • Algoritmo • Seleccione una producción y construya los hijos • Encuentre el siguiente nodo (no terminal) • Si hay otro nodo aplicar recursivamente • Si no hay más nodos • Si la cadena coincide entonces ACEPTAR • Si no coincide, probar recursivamente

  4. Ejemplo • Gramática de tipos en Pascal Tipo -> Simple | ^id | array [ Simple ] of Tipo Simple -> integer | char | núm puntopunto núm

  5. array [ núm puntopunto núm] of integer Analizar sintácticamente

  6. Descendente (símbolo inicial) Function Tipo() Resu = Simple() If Not Resu Then Resu = Match(“^”) And Match(“id”) If Not Resu Then Resu = Match(“Array”) And Match(“[”) And Simple() And Match(“]”) And Match(“of”) And Tipo() End End Tipo = Resu End Function

  7. Descendente (nodo) Function Simple() Resu = Match(“integer”) If Not Resu Then Resu = Match(“char”) If Not Resu Then Resu = Match(“núm”) And Match(“puntopunto”) And Match(“núm”) End End Simple = Resu End Function

  8. Match() - parea() Function Match(Valor) Tmp = GetNextToken() If Tmp = Valor Then Match = True /* Avanzar posición en entrada */ Else Match = False End If End Function

  9. Match() simplificado Function Match(Valor) Match = GetNextToken() = Valor End Function Ojo: queda pendiente avanzar la entrada

  10. GetNextToken() • Inteligente • Reconoce separadores: • Espacio, fin de línea, tabuladores, etc. • Token especiales: >,>=,<,<= • Palabras clave: While de WhileFlag

  11. Recursión Izquierda Problema para análisis sintáctico

  12. Gramática infijo Expr -> Expr + Término Expr -> Expr - Término Expr -> Término Término -> 0 Término -> 1 Término -> 2 .... Término -> 9 Recursión izquierda

  13. Análisis descendente Function Expr() Resu = Expr() And Match(“+”) And Término() If Not Resu Then Resu = Expr() And Match(“-”) And Término() If Not Resu Then Resu = Término() End End Expr = Resu End Function

  14. Recursión izquierda • Ojo: no se puede cambiar la gramática • Gramática iguales si árboles iguales • NT -> NT t1 | t2 • NT: No Terminal • t1, t2: terminales • Expr -> Expr + Término

  15. Corrección de recursión izquierda • NT -> NT t1 | t2 • t2 t1 t1 t1 ... t1 • t2 t1* • NT -> t2 R • R -> t1 R | nil • t2 t1 t1 t1 ... t1 • t2 t1*

  16. Gramática infijo corregida Expr -> Término Resto Resto -> + Expr | - Expr | nil Término -> 0 Término -> 1 Término -> 2 .... Término -> 9

  17. Proyecto 1 • Gramática de nombres y direcciones de email • Reglas semánticas • Reconocedor-extractor de emails mejorado • GetNextToken() • Match() • Análizador descendente

More Related