Edición de «Definiciones y teoremas (Teoría de Lenguajes)»

De Cuba-Wiki
Advertencia: no has iniciado sesión. Tu dirección IP se hará pública si haces cualquier edición. Si inicias sesión o creas una cuenta, tus ediciones se atribuirán a tu nombre de usuario, además de otros beneficios.

Puedes deshacer la edición. Antes de deshacer la edición, comprueba la siguiente comparación para verificar que realmente es lo que quieres hacer, y entonces publica los cambios para así efectuar la reversión.

Revisión actual Tu texto
Línea 41: Línea 41:
SD(N \to \beta) =
SD(N \to \beta) =
\begin{cases}
\begin{cases}
\textrm{PRIMEROS}(\beta) & si\ \beta\ \textrm{no es anulable} \\
\textrm{PRIMEROS}(\beta) & si\ \beta\ \textrm{no\ es\ anulable} \\
\textrm{PRIMEROS}(\beta) \cup \textrm{SIGUIENTES}(N) & \textrm{si no}
\textrm{PRIMEROS}(\beta) \cup \textrm{SIGUIENTES}(N) & \textrm{sino}
\end{cases}
\end{cases}
</math>
</math>
Línea 107: Línea 107:
Pasa a ser:
Pasa a ser:
<br>A --> β1 <font color=blue>A'</font> | ... | βK <font color=blue>A'</font>
<br>A --> β1 <font color=blue>A'</font> | ... | βK <font color=blue>A'</font>
<br><font color=blue>A'</font> --> <font color=red>α1</font> A' | ... | <font color=red>αN</font> A' | λ
<br><font color=blue>A'</font> --> <font color=red>α1</font> <font color=blue>A'</font> | ... | <font color=red>αN</font> <font color=blue>A'</font> | lambda


= Gramáticas y parsers LR =
= Gramáticas y parsers LR =
Línea 132: Línea 132:
=== Los distintos algoritmos LR ===
=== Los distintos algoritmos LR ===


Construir el AFND ideal o necesario una gramática cualquiera no es fácil y puede llegar a tener miles de estados, ya que cada estado del AFND representa un momento determinado en el parsing de una cadena de entrada cualquiera, y tiene que tener implícita la información de todo lo se leyó hasta el momento.  
Construir el AFND ideal o necesario una una gramática cualquiera no es fácil y puede llegar a tener miles de estados, ya que cada estado del AFND representa un momento determinado en el parsing de una cadena de entrada cualquiera, y tiene que tener implícita la información de todo lo se leyó hasta el momento.  


Hay entonces formas de construir AFND más simples y que sirven en muchas situaciones (aunque no todas).  
Hay entonces formas de construir AFND más simples y que sirven en muchas situaciones (aunque no todas).  
Línea 155: Línea 155:
Para los detalles de estas funciones en cada parser recomiendo altamente leer este ppt:  
Para los detalles de estas funciones en cada parser recomiendo altamente leer este ppt:  


[http://oscarbonilla.com/courses/compilers/materials/09_Parser_Engines.ppt Algoritmos LR0, SLR, LR1 y LALR] [[Media:TLeng_Parser_Engines_Oscar_Bonilla.pdf|(mismas diapos pero en pdf)]]
[http://oscarbonilla.com/courses/compilers/materials/09_Parser_Engines.ppt Algoritmos LR0, SLR, LR1 y LALR]


Es didáctico, con ejemplos paso a paso y si bien parece largo por tener cientos de slides, la mayoría son por los ejemplos paso a paso. Recomiendo en serio leerlo si quieren ver como construir los AFND en detalle.
Es didáctico, con ejemplos paso a paso y si bien parece largo por tener cientos de slides, la mayoría son por los ejemplos paso a paso. Recomiendo en serio leerlo si quieren ver como construir los AFND en detalle.
= Gramaticas de Precedencia Simple =
=== Relaciones de precedencia ===
<math>X \dot = Y \Leftrightarrow A \rightarrow \ldots X Y \ldots</math>
<math>X \dot < Y \Leftrightarrow A \rightarrow \ldots X Y' \ldots \wedge Y' \rightarrow ^+ Y \ldots</math>
<math>X \dot > Y \Leftrightarrow A \rightarrow \ldots X' Y' \ldots \wedge X' \rightarrow ^+ \ldots X \wedge Y' \rightarrow ^* Y \ldots</math>
=== Gramatica ===
G = (''N'', &Sigma;, ''P'', ''S'') is a simple precedence grammar if all the production rules in ''P'' comply with the following constraints:
* There are no erasing rules (&epsilon;-productions)
* There are no useless rules (unreacheable symbols or unproductives rules)
* For each pair of symbols ''X'', ''Y'' (''X'', ''Y'' <math>\in</math> (''N'' <math>\cup</math> &Sigma;)) there is only one precedence relation.
* G is uniquely inversible
=== Algoritmo Parser ascendente de precedencia simple ===
'''Definiciones:'''
<math>P^+(X) = \{Y \in V \; / \; X \rightarrow^+ Y \alpha\}</math><br>
<math>U^+(X) = \{Y \in V \; / \; X \rightarrow^+ \alpha Y\}</math><br>
<math>P^*(X) = \{P^+(X) \cup \{X\}\} \cap V_T</math><br>
'''Completar tabla de PS'''
para cada produccion A -> α
    para cada XY in subCadenas(α)
      definir X = Y
    IF Y in VN
      definir X < P+(Y)
    IF X in VN
      definir U+(X) > P*(Y)
para cada X in VN Union VT
    definir $ < S Union P+(S)
    definir S Union U+(S) > $
'''Algoritmo de parsing'''
Tabla de precedencia sin conflictos y ''w'' cadena de entrada. (TC es donde apunta en ''w'').
apilar $
repetir
    IF TOPE < TC o TOPE = TC
        apilar TC
        avanzar ''w''
    ELSE
        IF TOPE > TC
            OBTENER α pivote en la pila
            BUSCAR la produccion A -> α
            IF no encuentra produccion A -> α
                ERROR
            ELSE 
                reemplazar α en la pila por A
        ELSE
            ERROR
hasta TC = $ y en la pila hay $S
Aceptar ''w''
=== Ejemplo parseo ===
Dado el lenguaje:
E  --> E + T' | T'
T' --> T
T  --> T * F  | F
F  --> ( E' ) | num
E' --> E
Y la tabla de parsing:
{|border="1" cellpadding="2"
|  ||E|| E' ||  T  ||      T' ||        F  ||    +            ||*||(||)||num || $
|-
| E || || || || || ||<math>\dot =</math>|| || ||<math>\dot ></math> || ||<math>\dot ></math>
|-
| E'|| || ||  || || || || || ||<math>\dot =</math>|| ||
|-
| T || || ||  || || ||<math>\dot ></math>||<math>\dot =</math>|| ||<math>\dot ></math>|| ||<math>\dot ></math>
|-
| T'|| || ||  || || ||<math>\dot ></math>|| || ||<math>\dot ></math>|| ||<math>\dot ></math>
|-
| F || || ||  || || ||<math>\dot ></math>||<math>\dot ></math>|| ||<math>\dot ></math>|| ||<math>\dot ></math>
|-
| + || || ||<math>\dot <</math>||<math>\dot =</math>||<math>\dot <</math>||                    || ||<math>\dot <</math>|| ||<math>\dot <</math> ||
|-
| * || || ||  || ||<math>\dot =</math>|| || ||<math>\dot <</math>|| ||<math>\dot <</math> ||
|-
| ( ||<math>\dot <</math>||<math>\dot =</math>||<math>\dot <</math>||<math>\dot <</math>||<math>\dot <</math>|| || ||<math>\dot <</math>|| ||<math>\dot <</math> ||
|-
| ) || || ||  || || ||<math>\dot ></math>||<math>\dot ></math>|| ||<math>\dot ></math>|| ||<math>\dot ></math>
|-
| num|| || ||  || || ||<math>\dot ></math>||<math>\dot ></math>||||<math>\dot ></math>|| ||<math>\dot ></math>
|-
| $ ||<math>\dot <</math>|| ||<math>\dot <</math>||<math>\dot <</math>||<math>\dot <</math>|| || ||<math>\dot <</math>|| ||<math>\dot <</math> ||
|}
El input ''2 * ( 1 + 3 )$'' se parsea:
<pre>
STACK                  PRECEDENCE    INPUT            ACTION
$                            <        2 * ( 1 + 3 )$  SHIFT
$ < 2                        >        * ( 1 + 3 )$    REDUCE (F -> num)
$ < F                        >        * ( 1 + 3 )$    REDUCE (T -> F)
$ < T                        =        * ( 1 + 3 )$    SHIFT
$ < T = *                    <        ( 1 + 3 )$      SHIFT
$ < T = * < (                <        1 + 3 )$        SHIFT
$ < T = * < ( < 1            >        + 3 )$          REDUCE 4 times (F -> num) (T -> F) (T' -> T) (E ->T ')
$ < T = * < ( < E            =        + 3 )$          SHIFT
$ < T = * < ( < E = +        <        3 )$            SHIFT
$ < T = * < ( < E = + < 3    >        )$              REDUCE 3 times (F -> num) (T -> F) (T' -> T)
$ < T = * < ( < E = + = T    >        )$              REDUCE 2 times (E -> E + T) (E' -> E)
$ < T = * < ( < E'          =        )$              SHIFT
$ < T = * < ( = E' = )      >        $                REDUCE (F -> ( E' ))
$ < T = * = F                >        $                REDUCE (T -> T * F)
$ < T                        >        $                REDUCE 2 times (T' -> T) (E -> T')
$ < E                        >        $                ACCEPT
</pre>


= Gramaticas de Atributos =
= Gramaticas de Atributos =
Ten en cuenta que todas las contribuciones a Cuba-Wiki pueden ser editadas, modificadas o eliminadas por otros colaboradores. Si no deseas que las modifiquen sin limitaciones, no las publiques aquí.
Al mismo tiempo, asumimos que eres el autor de lo que escribiste, o lo copiaste de una fuente en el dominio público o con licencia libre (véase Cuba-Wiki:Derechos de autor para más detalles). ¡No uses textos con copyright sin permiso!

Para editar esta página, responde la pregunta que aparece abajo (más información):

Cancelar Ayuda de edición (se abre en una ventana nueva)

Plantilla usada en esta página: