Parsers
description
Transcript of Parsers
![Page 1: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/1.jpg)
Compiladores
Parsers
Parsers Predictivos, LL(0), LL(k), Parsers shift-reduce, Construcción de
un Parser LL(0)
![Page 2: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/2.jpg)
Oscar Bonilla 2 Universidad Galileo
Resumen
• Parsers Predictivos
• Implementando un Parser
• Ejemplo de un Parser shift-reduce
• ¿Por qué es difícil construir un parser engine?
• LR(k) parser tables
• Construyendo un Parser Engine LR(0)
![Page 3: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/3.jpg)
Oscar Bonilla 3 Universidad Galileo
Parsers Predictivos
• Algunas gramáticas pueden parsearse usando un algoritmo conocido como Recursive Descent
• Recursive Descent: Una función para cada no terminal, una clausula para cada terminal
![Page 4: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/4.jpg)
Oscar Bonilla 4 Universidad Galileo
Ejemplo: Parser Predictivo
• Gramática:<S> if <E> then <S> else <S><S> begin <S> <L><S> print <E><L> end<L> ; <S><L><E> num = num
![Page 5: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/5.jpg)
Oscar Bonilla 5 Universidad Galileo
Ejemplo: Parser PredictivoGramática:
<S> if <E> then <S> else <S><S> begin <S> <L><S> print <E><L> end<L> ; <S><L><E> num = num
void S (void) { switch(token) { case IF: eat(IF); E(); eat(THEN); S(); eat(ELSE); S(); break; case BEGIN: eat(BEGIN); S(); L(); break; case PRINT: eat(PRINT); E(); break; default: error(); }}
void L (void) { switch(token) { case END: eat(END); break; case SEMI: eat(SEMI); S(); L(); break; default: error(); }}
void E (void) { eat(NUM); eat(EQ); eat(NUM); }
![Page 6: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/6.jpg)
Oscar Bonilla 6 Universidad Galileo
Ejemplo: Parser PredictivoGramática:
<S> if <E> then <S> else <S><S> begin <S> <L><S> print <E><L> end<L> ; <S><L><E> num = num
void S (void) { switch(token) { case IF: eat(IF); E(); eat(THEN); S(); eat(ELSE); S(); break; case BEGIN: eat(BEGIN); S(); L(); break; case PRINT: eat(PRINT); E(); break; default: error(); }}
void L (void) { switch(token) { case END: eat(END); break; case SEMI: eat(SEMI); S(); L(); break; default: error(); }}
void E (void) { eat(NUM); eat(EQ); eat(NUM); }
No terminales
![Page 7: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/7.jpg)
Oscar Bonilla 7 Universidad Galileo
Ejemplo: Parser PredictivoGramática:
<S> if <E> then <S> else <S><S> begin <S> <L><S> print <E><L> end<L> ; <S><L><E> num = num
void S (void) { switch(token) { case IF: eat(IF); E(); eat(THEN); S(); eat(ELSE); S(); break; case BEGIN: eat(BEGIN); S(); L(); break; case PRINT: eat(PRINT); E(); break; default: error(); }}
void L (void) { switch(token) { case END: eat(END); break; case SEMI: eat(SEMI); S(); L(); break; default: error(); }}
void E (void) { eat(NUM); eat(EQ); eat(NUM); }
terminales
![Page 8: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/8.jpg)
Oscar Bonilla 8 Universidad Galileo
Parsers Predictivos
• Los parsers predictivos sólo funcionan para gramáticas donde el primer símbolo terminal de cada subexpresión provee suficiente información para determinar qué producción usar.
![Page 9: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/9.jpg)
Oscar Bonilla 9 Universidad Galileo
Ejemplo
• Gramática<E> <E> + <T><E> <E> - <T><E> <T><T> <T> * <F><T> <T> / <F><T> <F><F> id<F> num<F> ( <E> )
• Ejemplos:
( 1 * 2 – 3 ) + 4 ( 1 * 2 – 3 )
![Page 10: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/10.jpg)
Oscar Bonilla 10 Universidad Galileo
Ejemplo• Gramática
<E> <E> + <T><E> <E> - <T><E> <T><T> <T> * <F><T> <T> / <F><T> <F><F> id<F> num<F> ( <E> )
void E (void) { switch(token) { case ??: E(); eat(PLUS); T(); break; case ??: E(); eat(MINUS); T(); break; case ??: T(); break; default: error(); }}
Void T (void) { switch(token) { case ??: T(); eat(TIMES); F(); break; case ??: T(); eat(DIV); F(); break; case ??: F(); break; default: error(); }}
![Page 11: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/11.jpg)
Oscar Bonilla 11 Universidad Galileo
Ejemplo• Gramática
<E> <E> + <T><E> <E> - <T><E> <T><T> <T> * <F><T> <T> / <F><T> <F><F> id<F> num<F> ( <E> )
void E (void) { switch(token) { case ??: E(); eat(PLUS); T(); break; case ??: E(); eat(MINUS); T(); break; case ??: T(); break; default: error(); }}
Void T (void) { switch(token) { case ??: T(); eat(TIMES); F(); break; case ??: T(); eat(DIV); F(); break; case ??: F(); break; default: error(); }}
¿Qué ponemos aquí?
![Page 12: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/12.jpg)
Oscar Bonilla 12 Universidad Galileo
Ejemplo• Gramática
<E> <E> + <T><E> <E> - <T><E> <T><T> <T> * <F><T> <T> / <F><T> <F><F> id<F> num<F> ( <E> )
void E (void) { switch(token) { case ??: E(); eat(PLUS); T(); break; case ??: E(); eat(MINUS); T(); break; case ??: T(); break; default: error(); }}
Void T (void) { switch(token) { case ??: T(); eat(TIMES); F(); break; case ??: T(); eat(DIV); F(); break; case ??: F(); break; default: error(); }}
( 1 * 2 – 3 ) + 4
![Page 13: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/13.jpg)
Oscar Bonilla 13 Universidad Galileo
Ejemplo• Gramática
<E> <E> + <T><E> <E> - <T><E> <T><T> <T> * <F><T> <T> / <F><T> <F><F> id<F> num<F> ( <E> )
void E (void) { switch(token) { case ??: E(); eat(PLUS); T(); break; case ??: E(); eat(MINUS); T(); break; case ??: T(); break; default: error(); }}
Void T (void) { switch(token) { case ??: T(); eat(TIMES); F(); break; case ??: T(); eat(DIV); F(); break; case ??: F(); break; default: error(); }}
( 1 * 2 – 3 ) + 4
![Page 14: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/14.jpg)
Oscar Bonilla 14 Universidad Galileo
Ejemplo• Gramática
<E> <E> + <T><E> <E> - <T><E> <T><T> <T> * <F><T> <T> / <F><T> <F><F> id<F> num<F> ( <E> )
void E (void) { switch(token) { case LPAREN: E(); eat(PLUS); T(); break; case ??: E(); eat(MINUS); T(); break; case ??: T(); break; default: error(); }}
Void T (void) { switch(token) { case ??: T(); eat(TIMES); F(); break; case ??: T(); eat(DIV); F(); break; case ??: F(); break; default: error(); }}
( 1 * 2 – 3 ) + 4
![Page 15: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/15.jpg)
Oscar Bonilla 15 Universidad Galileo
Ejemplo• Gramática
<E> <E> + <T><E> <E> - <T><E> <T><T> <T> * <F><T> <T> / <F><T> <F><F> id<F> num<F> ( <E> )
void E (void) { switch(token) { case LPAREN: E(); eat(PLUS); T(); break; case ??: E(); eat(MINUS); T(); break; case ??: T(); break; default: error(); }}
Void T (void) { switch(token) { case ??: T(); eat(TIMES); F(); break; case ??: T(); eat(DIV); F(); break; case ??: F(); break; default: error(); }}
( 1 * 2 – 3 ) + 4( 1 * 2 – 3 )
![Page 16: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/16.jpg)
Oscar Bonilla 16 Universidad Galileo
Ejemplo• Gramática
<E> <E> + <T><E> <E> - <T><E> <T><T> <T> * <F><T> <T> / <F><T> <F><F> id<F> num<F> ( <E> )
void E (void) { switch(token) { case LPAREN: E(); eat(PLUS); T(); break; case ??: E(); eat(MINUS); T(); break; case ??: T(); break; default: error(); }}
Void T (void) { switch(token) { case ??: T(); eat(TIMES); F(); break; case ??: T(); eat(DIV); F(); break; case ??: F(); break; default: error(); }}
( 1 * 2 – 3 ) + 4( 1 * 2 – 3 )
![Page 17: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/17.jpg)
Oscar Bonilla 17 Universidad Galileo
Ejemplo• Gramática
<E> <E> + <T><E> <E> - <T><E> <T><T> <T> * <F><T> <T> / <F><T> <F><F> id<F> num<F> ( <E> )
void E (void) { switch(token) { case LPAREN: E(); eat(PLUS); T(); break; case ??: E(); eat(MINUS); T(); break; case LPAREN: T(); break; default: error(); }}
Void T (void) { switch(token) { case ??: T(); eat(TIMES); F(); break; case ??: T(); eat(DIV); F(); break; case ??: F(); break; default: error(); }}
( 1 * 2 – 3 ) + 4( 1 * 2 – 3 )
![Page 18: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/18.jpg)
Oscar Bonilla 18 Universidad Galileo
First(), Follow() y Nullable()
• Nullable(X) es verdadero si X puede derivar
• FIRST() es el conjunto de terminales que pueden comenzar strings derivados de
• FOLLOW(X) es el conjunto de terminales que pueden seguir a X. Esto es, t FOLLOW(X) si hay una derivación que contenga Xt. Esto puede ocurrir si una derivación contiene XYZt donde tanto Y como Z derivan .
![Page 19: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/19.jpg)
Oscar Bonilla 19 Universidad Galileo
Algoritmo para First(), Follow() y Nullable()
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
![Page 20: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/20.jpg)
Oscar Bonilla 20 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X
Y
Z
![Page 21: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/21.jpg)
Oscar Bonilla 21 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X
Y
Z
![Page 22: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/22.jpg)
Oscar Bonilla 22 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no
![Page 23: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/23.jpg)
Oscar Bonilla 23 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no
![Page 24: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/24.jpg)
Oscar Bonilla 24 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no
![Page 25: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/25.jpg)
Oscar Bonilla 25 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no
![Page 26: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/26.jpg)
Oscar Bonilla 26 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no
![Page 27: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/27.jpg)
Oscar Bonilla 27 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no
![Page 28: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/28.jpg)
Oscar Bonilla 28 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no
i = 1, j = 2, k = 1
![Page 29: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/29.jpg)
Oscar Bonilla 29 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no
i = 1, j = 2, k = 1
![Page 30: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/30.jpg)
Oscar Bonilla 30 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no
i = 1, j = 2, k = 1
![Page 31: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/31.jpg)
Oscar Bonilla 31 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no d
i = 1, j = 2, k = 1
![Page 32: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/32.jpg)
Oscar Bonilla 32 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no d
i = 1, j = 2, k = 1
![Page 33: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/33.jpg)
Oscar Bonilla 33 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no d
i = 1, j = 2, k = 1
![Page 34: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/34.jpg)
Oscar Bonilla 34 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no d
i = 1, j = 2, k = 1
![Page 35: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/35.jpg)
Oscar Bonilla 35 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no d
i = 1, j = 2, k = 1
![Page 36: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/36.jpg)
Oscar Bonilla 36 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no d
![Page 37: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/37.jpg)
Oscar Bonilla 37 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no d
![Page 38: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/38.jpg)
Oscar Bonilla 38 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no d
![Page 39: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/39.jpg)
Oscar Bonilla 39 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no d
i = 1, j = 2, k = 3
![Page 40: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/40.jpg)
Oscar Bonilla 40 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no d
i = 1, j = 2, k = 3
![Page 41: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/41.jpg)
Oscar Bonilla 41 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no d
i = 1, j = 2, k = 3
![Page 42: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/42.jpg)
Oscar Bonilla 42 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no d
i = 1, j = 2, k = 3
![Page 43: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/43.jpg)
Oscar Bonilla 43 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no d
i = 1, j = 2, k = 3
![Page 44: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/44.jpg)
Oscar Bonilla 44 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no d
i = 1, j = 2, k = 3
![Page 45: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/45.jpg)
Oscar Bonilla 45 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no d
i = 1, j = 3, k = 3
![Page 46: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/46.jpg)
Oscar Bonilla 46 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no d
i = 1, j = 3, k = 3
![Page 47: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/47.jpg)
Oscar Bonilla 47 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no d
i = 1, j = 3, k = 3
![Page 48: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/48.jpg)
Oscar Bonilla 48 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no d
i = 1, j = 3, k = 3
![Page 49: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/49.jpg)
Oscar Bonilla 49 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no d
i = 2, j = 3, k = 3
![Page 50: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/50.jpg)
Oscar Bonilla 50 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no d
i = 2, j = 3, k = 3
![Page 51: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/51.jpg)
Oscar Bonilla 51 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no d
i = 2, j = 3, k = 3
![Page 52: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/52.jpg)
Oscar Bonilla 52 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no
Z no d
i = 2, j = 3, k = 3
![Page 53: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/53.jpg)
Oscar Bonilla 53 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no d
Z no d
i = 2, j = 3, k = 3
![Page 54: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/54.jpg)
Oscar Bonilla 54 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no d
Z no d
i = 3, j = 4, k = 3
![Page 55: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/55.jpg)
Oscar Bonilla 55 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no d
Z no d
i = 3, j = 4, k = 3
![Page 56: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/56.jpg)
Oscar Bonilla 56 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no d
Z no d
i = 3, j = 4, k = 3
![Page 57: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/57.jpg)
Oscar Bonilla 57 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no d
Z no d
i = 3, j = 4, k = 3
![Page 58: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/58.jpg)
Oscar Bonilla 58 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no d
Z no d
i = 3, j = 4, k = 3
![Page 59: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/59.jpg)
Oscar Bonilla 59 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no d
Z no d
i = 3, j = 4, k = 3
![Page 60: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/60.jpg)
Oscar Bonilla 60 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no d
Z no d
![Page 61: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/61.jpg)
Oscar Bonilla 61 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no d
Z no d
![Page 62: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/62.jpg)
Oscar Bonilla 62 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no d
Z no d
![Page 63: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/63.jpg)
Oscar Bonilla 63 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y no d
Z no d
![Page 64: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/64.jpg)
Oscar Bonilla 64 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y yes d
Z no d
![Page 65: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/65.jpg)
Oscar Bonilla 65 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y yes d
Z no d
i = 1, j = 2, k = 0
![Page 66: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/66.jpg)
Oscar Bonilla 66 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y yes d
Z no d
![Page 67: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/67.jpg)
Oscar Bonilla 67 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y yes d
Z no d
![Page 68: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/68.jpg)
Oscar Bonilla 68 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y yes d
Z no d
![Page 69: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/69.jpg)
Oscar Bonilla 69 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y yes d
Z no d
i = 1, j = 2, k = 1
![Page 70: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/70.jpg)
Oscar Bonilla 70 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y yes d
Z no d
i = 1, j = 2, k = 1
![Page 71: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/71.jpg)
Oscar Bonilla 71 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y yes d
Z no d
i = 1, j = 2, k = 1
![Page 72: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/72.jpg)
Oscar Bonilla 72 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y yes c d
Z no d
i = 1, j = 2, k = 1
![Page 73: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/73.jpg)
Oscar Bonilla 73 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y yes c d
Z no d
i = 1, j = 2, k = 1
![Page 74: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/74.jpg)
Oscar Bonilla 74 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y yes c d
Z no d
i = 1, j = 2, k = 1
![Page 75: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/75.jpg)
Oscar Bonilla 75 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y yes c d
Z no d
i = 1, j = 2, k = 1
![Page 76: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/76.jpg)
Oscar Bonilla 76 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y yes c d
Z no d
i = 1, j = 2, k = 1
![Page 77: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/77.jpg)
Oscar Bonilla 77 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y yes c d
Z no d
![Page 78: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/78.jpg)
Oscar Bonilla 78 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y yes c d
Z no d
![Page 79: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/79.jpg)
Oscar Bonilla 79 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y yes c d
Z no d
![Page 80: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/80.jpg)
Oscar Bonilla 80 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X no
Y yes c d
Z no d
![Page 81: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/81.jpg)
Oscar Bonilla 81 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X yes
Y yes c d
Z no d
![Page 82: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/82.jpg)
Oscar Bonilla 82 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X yes
Y yes c d
Z no d
i = 1, j = 2, k = 1
![Page 83: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/83.jpg)
Oscar Bonilla 83 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X yes
Y yes c d
Z no d
i = 1, j = 2, k = 1
![Page 84: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/84.jpg)
Oscar Bonilla 84 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X yes
Y yes c d
Z no d
i = 1, j = 2, k = 1
![Page 85: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/85.jpg)
Oscar Bonilla 85 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X yes c
Y yes c d
Z no d
i = 1, j = 2, k = 1
![Page 86: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/86.jpg)
Oscar Bonilla 86 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X yes c
Y yes c d
Z no d
i = 1, j = 2, k = 1
![Page 87: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/87.jpg)
Oscar Bonilla 87 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X yes c
Y yes c d
Z no d
i = 1, j = 2, k = 1
![Page 88: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/88.jpg)
Oscar Bonilla 88 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X yes c
Y yes c d
Z no d
i = 1, j = 2, k = 1
![Page 89: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/89.jpg)
Oscar Bonilla 89 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X yes c
Y yes c d
Z no d
i = 1, j = 2, k = 1
![Page 90: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/90.jpg)
Oscar Bonilla 90 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X yes c
Y yes c d
Z no d
i = 1, j = 2, k = 1
![Page 91: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/91.jpg)
Oscar Bonilla 91 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X yes c
Y yes c d
Z no d
i = 1, j = 2, k = 1
![Page 92: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/92.jpg)
Oscar Bonilla 92 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X yes c
Y yes c d
Z no d
![Page 93: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/93.jpg)
Oscar Bonilla 93 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X yes c
Y yes c d
Z no d
![Page 94: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/94.jpg)
Oscar Bonilla 94 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X yes c
Y yes c d
Z no d
![Page 95: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/95.jpg)
Oscar Bonilla 95 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X yes c
Y yes c d
Z no d
i = 1, j = 2, k = 1
![Page 96: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/96.jpg)
Oscar Bonilla 96 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X yes c
Y yes c d
Z no d
i = 1, j = 2, k = 1
![Page 97: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/97.jpg)
Oscar Bonilla 97 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X yes c
Y yes c d
Z no d
i = 1, j = 2, k = 1
![Page 98: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/98.jpg)
Oscar Bonilla 98 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X yes a c
Y yes c d
Z no d
i = 1, j = 2, k = 1
![Page 99: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/99.jpg)
Oscar Bonilla 99 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X yes a c
Y yes c d
Z no d
i = 1, j = 2, k = 1
![Page 100: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/100.jpg)
Oscar Bonilla 100 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X yes a c
Y yes c d
Z no d
i = 1, j = 2, k = 1
![Page 101: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/101.jpg)
Oscar Bonilla 101 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X yes a c
Y yes c d
Z no d
i = 1, j = 2, k = 1
![Page 102: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/102.jpg)
Oscar Bonilla 102 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X yes a c
Y yes c d
Z no d
i = 1, j = 2, k = 1
![Page 103: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/103.jpg)
Oscar Bonilla 103 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X yes a c
Y yes c d
Z no d
![Page 104: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/104.jpg)
Oscar Bonilla 104 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X yes a c
Y yes c d
Z no d
![Page 105: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/105.jpg)
Oscar Bonilla 105 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X yes a c
Y yes c d
Z no d
![Page 106: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/106.jpg)
Oscar Bonilla 106 Universidad Galileo
EjemploGramática
<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
Incializar FIRST y FOLLOW al conjunto vacío y nullable a falsoPara cada símbolo terminal Z, FIRST[Z] = {Z}Repetir
Para cada producción X Y1 Y2 Yk
Si Y1 Yk son todos nullable (o si k=0) entonces nullable[X] = true Para cada i = 1 to k, j = i +1 to k Si Y1 Yi-1 son todos nullable (o si i = 1) entonces FIRST[X] = FIRST[X] FIRST[Yi] Si Yi+1 Yk son todos nullable (o si i = k ) entonces FOLLOW[Yi] = FOLLOW[Yi] FOLLOW[X] Si Yi+1 Yj-1 son todos nullable (o si i +1 = j ) entonces FOLLOW[Yi] = FOLLOW[Yi] FIRST[Yj]
Hasta que FIRST, FOLLOW y nullable no cambien en esta iteración
nullable FIRST FOLLOW
X yes a c a c d
Y yes c a c d
Z no a c d
![Page 107: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/107.jpg)
Oscar Bonilla 107 Universidad Galileo
Construyendo un Parser Predictivo
• Usando la información de FIRST y FOLLOW podemos crear una tabla de parseo
• Ingresamos la producción X en la fila X, columna T de una tabla para cada T FIRST()
• Si es nullable, ingresamos la producción en la fila X, columna T para cada T FOLLOW(X)
![Page 108: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/108.jpg)
Oscar Bonilla 108 Universidad Galileo
Ejemplo: Tabla de Parser Predictivo
nullable FIRST FOLLOW
X yes a c a c d
Y yes c a c d
Z no a c d
Gramática<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
a c d
X
Y
Z
Ingresamos la producción X en la fila X, columna T de una tabla para cada T FIRST()
Si es nullable, ingresamos la producción en la fila X, columna T para cada T FOLLOW(X)
![Page 109: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/109.jpg)
Oscar Bonilla 109 Universidad Galileo
Ejemplo: Tabla de Parser Predictivo
nullable FIRST FOLLOW
X yes a c a c d
Y yes c a c d
Z no a c d
Gramática<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
a c d
X
Y
Z
Ingresamos la producción X en la fila X, columna T de una tabla para cada T FIRST()
Si es nullable, ingresamos la producción en la fila X, columna T para cada T FOLLOW(X)
![Page 110: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/110.jpg)
Oscar Bonilla 110 Universidad Galileo
Ejemplo: Tabla de Parser Predictivo
nullable FIRST FOLLOW
X yes a c a c d
Y yes c a c d
Z no a c d
Gramática<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
a c d
X
Y
Z
Ingresamos la producción X en la fila X, columna T de una tabla para cada T FIRST()
Si es nullable, ingresamos la producción en la fila X, columna T para cada T FOLLOW(X)
![Page 111: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/111.jpg)
Oscar Bonilla 111 Universidad Galileo
Ejemplo: Tabla de Parser Predictivo
nullable FIRST FOLLOW
X yes a c a c d
Y yes c a c d
Z no a c d
Gramática<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
a c d
X
Y
Z
Ingresamos la producción X en la fila X, columna T de una tabla para cada T FIRST()
Si es nullable, ingresamos la producción en la fila X, columna T para cada T FOLLOW(X)
![Page 112: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/112.jpg)
Oscar Bonilla 112 Universidad Galileo
Ejemplo: Tabla de Parser Predictivo
nullable FIRST FOLLOW
X yes a c a c d
Y yes c a c d
Z no a c d
Gramática<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
a c d
X
Y
Z <Z> d
Ingresamos la producción X en la fila X, columna T de una tabla para cada T FIRST()
Si es nullable, ingresamos la producción en la fila X, columna T para cada T FOLLOW(X)
![Page 113: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/113.jpg)
Oscar Bonilla 113 Universidad Galileo
Ejemplo: Tabla de Parser Predictivo
nullable FIRST FOLLOW
X yes a c a c d
Y yes c a c d
Z no a c d
Gramática<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
a c d
X
Y
Z <Z> d
Ingresamos la producción X en la fila X, columna T de una tabla para cada T FIRST()
Si es nullable, ingresamos la producción en la fila X, columna T para cada T FOLLOW(X)
![Page 114: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/114.jpg)
Oscar Bonilla 114 Universidad Galileo
Ejemplo: Tabla de Parser Predictivo
nullable FIRST FOLLOW
X yes a c a c d
Y yes c a c d
Z no a c d
Gramática<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
a c d
X
Y
Z <Z> d
Ingresamos la producción X en la fila X, columna T de una tabla para cada T FIRST()
Si es nullable, ingresamos la producción en la fila X, columna T para cada T FOLLOW(X)
![Page 115: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/115.jpg)
Oscar Bonilla 115 Universidad Galileo
Ejemplo: Tabla de Parser Predictivo
nullable FIRST FOLLOW
X yes a c a c d
Y yes c a c d
Z no a c d
Gramática<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
a c d
X
Y
Z<Z> <X><Y><Z> <Z> <X><Y><Z>
<Z> d
<Z> <X><Y><Z>
Ingresamos la producción X en la fila X, columna T de una tabla para cada T FIRST()
Si es nullable, ingresamos la producción en la fila X, columna T para cada T FOLLOW(X)
![Page 116: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/116.jpg)
Oscar Bonilla 116 Universidad Galileo
Ejemplo: Tabla de Parser Predictivo
nullable FIRST FOLLOW
X yes a c a c d
Y yes c a c d
Z no a c d
Gramática<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
a c d
X
Y
Z<Z> <X><Y><Z> <Z> <X><Y><Z>
<Z> d
<Z> <X><Y><Z>
Ingresamos la producción X en la fila X, columna T de una tabla para cada T FIRST()
Si es nullable, ingresamos la producción en la fila X, columna T para cada T FOLLOW(X)
![Page 117: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/117.jpg)
Oscar Bonilla 117 Universidad Galileo
Ejemplo: Tabla de Parser Predictivo
nullable FIRST FOLLOW
X yes a c a c d
Y yes c a c d
Z no a c d
Gramática<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
a c d
X
Y
Z<Z> <X><Y><Z> <Z> <X><Y><Z>
<Z> d
<Z> <X><Y><Z>
Ingresamos la producción X en la fila X, columna T de una tabla para cada T FIRST()
Si es nullable, ingresamos la producción en la fila X, columna T para cada T FOLLOW(X)
![Page 118: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/118.jpg)
Oscar Bonilla 118 Universidad Galileo
Ejemplo: Tabla de Parser Predictivo
nullable FIRST FOLLOW
X yes a c a c d
Y yes c a c d
Z no a c d
Gramática<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
a c d
X
Y
Z<Z> <X><Y><Z> <Z> <X><Y><Z>
<Z> d
<Z> <X><Y><Z>
Ingresamos la producción X en la fila X, columna T de una tabla para cada T FIRST()
Si es nullable, ingresamos la producción en la fila X, columna T para cada T FOLLOW(X)
![Page 119: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/119.jpg)
Oscar Bonilla 119 Universidad Galileo
Ejemplo: Tabla de Parser Predictivo
nullable FIRST FOLLOW
X yes a c a c d
Y yes c a c d
Z no a c d
Gramática<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
a c d
X
Y <Y> <Y> <Y>
Z<Z> <X><Y><Z> <Z> <X><Y><Z>
<Z> d
<Z> <X><Y><Z>
Ingresamos la producción X en la fila X, columna T de una tabla para cada T FIRST()
Si es nullable, ingresamos la producción en la fila X, columna T para cada T FOLLOW(X)
![Page 120: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/120.jpg)
Oscar Bonilla 120 Universidad Galileo
Ejemplo: Tabla de Parser Predictivo
nullable FIRST FOLLOW
X yes a c a c d
Y yes c a c d
Z no a c d
Gramática<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
a c d
X
Y <Y> <Y> <Y>
Z<Z> <X><Y><Z> <Z> <X><Y><Z>
<Z> d
<Z> <X><Y><Z>
Ingresamos la producción X en la fila X, columna T de una tabla para cada T FIRST()
Si es nullable, ingresamos la producción en la fila X, columna T para cada T FOLLOW(X)
![Page 121: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/121.jpg)
Oscar Bonilla 121 Universidad Galileo
Ejemplo: Tabla de Parser Predictivo
nullable FIRST FOLLOW
X yes a c a c d
Y yes c a c d
Z no a c d
Gramática<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
a c d
X
Y <Y> <Y> <Y> c
<Y>
Z<Z> <X><Y><Z> <Z> <X><Y><Z>
<Z> d
<Z> <X><Y><Z>
Ingresamos la producción X en la fila X, columna T de una tabla para cada T FIRST()
Si es nullable, ingresamos la producción en la fila X, columna T para cada T FOLLOW(X)
![Page 122: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/122.jpg)
Oscar Bonilla 122 Universidad Galileo
Ejemplo: Tabla de Parser Predictivo
nullable FIRST FOLLOW
X yes a c a c d
Y yes c a c d
Z no a c d
Gramática<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
a c d
X
Y <Y> <Y> <Y> c
<Y>
Z<Z> <X><Y><Z> <Z> <X><Y><Z>
<Z> d
<Z> <X><Y><Z>
Ingresamos la producción X en la fila X, columna T de una tabla para cada T FIRST()
Si es nullable, ingresamos la producción en la fila X, columna T para cada T FOLLOW(X)
![Page 123: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/123.jpg)
Oscar Bonilla 123 Universidad Galileo
Ejemplo: Tabla de Parser Predictivo
nullable FIRST FOLLOW
X yes a c a c d
Y yes c a c d
Z no a c d
Gramática<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
a c d
X
Y <Y> <Y> <Y> c
<Y>
Z<Z> <X><Y><Z> <Z> <X><Y><Z>
<Z> d
<Z> <X><Y><Z>
Ingresamos la producción X en la fila X, columna T de una tabla para cada T FIRST()
Si es nullable, ingresamos la producción en la fila X, columna T para cada T FOLLOW(X)
![Page 124: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/124.jpg)
Oscar Bonilla 124 Universidad Galileo
Ejemplo: Tabla de Parser Predictivo
nullable FIRST FOLLOW
X yes a c a c d
Y yes c a c d
Z no a c d
Gramática<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
a c d
X <X> <Y>
Y <Y> <Y> <Y> c
<Y>
Z<Z> <X><Y><Z> <Z> <X><Y><Z>
<Z> d
<Z> <X><Y><Z>
Ingresamos la producción X en la fila X, columna T de una tabla para cada T FIRST()
Si es nullable, ingresamos la producción en la fila X, columna T para cada T FOLLOW(X)
![Page 125: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/125.jpg)
Oscar Bonilla 125 Universidad Galileo
Ejemplo: Tabla de Parser Predictivo
nullable FIRST FOLLOW
X yes a c a c d
Y yes c a c d
Z no a c d
Gramática<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
a c d
X <X> <Y> <X> <Y> <X> <Y>
Y <Y> <Y> <Y> c
<Y>
Z<Z> <X><Y><Z> <Z> <X><Y><Z>
<Z> d
<Z> <X><Y><Z>
Ingresamos la producción X en la fila X, columna T de una tabla para cada T FIRST()
Si es nullable, ingresamos la producción en la fila X, columna T para cada T FOLLOW(X)
![Page 126: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/126.jpg)
Oscar Bonilla 126 Universidad Galileo
Ejemplo: Tabla de Parser Predictivo
nullable FIRST FOLLOW
X yes a c a c d
Y yes c a c d
Z no a c d
Gramática<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
a c d
X <X> <Y> <X> <Y> <X> <Y>
Y <Y> <Y> <Y> c
<Y>
Z<Z> <X><Y><Z> <Z> <X><Y><Z>
<Z> d
<Z> <X><Y><Z>
Ingresamos la producción X en la fila X, columna T de una tabla para cada T FIRST()
Si es nullable, ingresamos la producción en la fila X, columna T para cada T FOLLOW(X)
![Page 127: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/127.jpg)
Oscar Bonilla 127 Universidad Galileo
Ejemplo: Tabla de Parser Predictivo
nullable FIRST FOLLOW
X yes a c a c d
Y yes c a c d
Z no a c d
Gramática<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
a c d
X <X> <Y>
<X> a
<X> <Y> <X> <Y>
Y <Y> <Y> <Y> c
<Y>
Z<Z> <X><Y><Z> <Z> <X><Y><Z>
<Z> d
<Z> <X><Y><Z>
Ingresamos la producción X en la fila X, columna T de una tabla para cada T FIRST()
Si es nullable, ingresamos la producción en la fila X, columna T para cada T FOLLOW(X)
![Page 128: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/128.jpg)
Oscar Bonilla 128 Universidad Galileo
Ejemplo: Tabla de Parser Predictivo
nullable FIRST FOLLOW
X yes a c a c d
Y yes c a c d
Z no a c d
Gramática<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
a c d
X <X> <Y>
<X> a
<X> <Y> <X> <Y>
Y <Y> <Y> <Y> c
<Y>
Z<Z> <X><Y><Z> <Z> <X><Y><Z>
<Z> d
<Z> <X><Y><Z>
Ingresamos la producción X en la fila X, columna T de una tabla para cada T FIRST()
Si es nullable, ingresamos la producción en la fila X, columna T para cada T FOLLOW(X)
![Page 129: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/129.jpg)
Oscar Bonilla 129 Universidad Galileo
Ejemplo: Tabla de Parser Predictivo
nullable FIRST FOLLOW
X yes a c a c d
Y yes c a c d
Z no a c d
Gramática<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
a c d
X <X> <Y>
<X> a
<X> <Y> <X> <Y>
Y <Y> <Y> <Y> c
<Y>
Z<Z> <X><Y><Z> <Z> <X><Y><Z>
<Z> d
<Z> <X><Y><Z>
Ingresamos la producción X en la fila X, columna T de una tabla para cada T FIRST()
Si es nullable, ingresamos la producción en la fila X, columna T para cada T FOLLOW(X)
![Page 130: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/130.jpg)
Oscar Bonilla 130 Universidad Galileo
Ejemplo: Tabla de Parser Predictivo
nullable FIRST FOLLOW
X yes a c a c d
Y yes c a c d
Z no a c d
Gramática<Z> d<Z> <X><Y><Z><Y> <Y> c<X> <Y><X> a
a c d
X <X> <Y>
<X> a
<X> <Y> <X> <Y>
Y <Y> <Y> <Y> c
<Y>
Z<Z> <X><Y><Z> <Z> <X><Y><Z>
<Z> d
<Z> <X><Y><Z>
Ingresamos la producción X en la fila X, columna T de una tabla para cada T FIRST()
Si es nullable, ingresamos la producción en la fila X, columna T para cada T FOLLOW(X)
¡¡Múltiples Entradas!!
![Page 131: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/131.jpg)
Oscar Bonilla 131 Universidad Galileo
Parsers Predictivos
• Los parsers predictivos sólo funcionan para gramáticas donde el primer símbolo terminal de cada subexpresión provee suficiente información para determinar qué producción usar.
• ¡La gramática que usamos es ambigua!
![Page 132: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/132.jpg)
Oscar Bonilla 132 Universidad Galileo
Gramáticas LL(1)
• A las gramáticas para las que la tabla de parseo de un parser predictivo no contiene entradas duplicadas se les llama LL(1)
• Si generalizamos la noción de FIRST para describir k tokens (en vez de 1), podemos construir una tabla de parseo LL(k)
• Esto casi no se hace ya que las tablas de parseo serían demasiado grandes.
![Page 133: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/133.jpg)
Oscar Bonilla 133 Universidad Galileo
Recursión Izquierda
• Si una gramática tiene recursión izquierda, no puede ser parseada por un parser predictivo
• Ejemplo:<E> <E> + <T><E> <T>
• Las gramáticas con recursión izquierda no son LL(1)
![Page 134: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/134.jpg)
Oscar Bonilla 134 Universidad Galileo
Eliminando Recursión Izquierda
• Para eliminar la recursión izquierda, podemos reescribir la gramática usando recursión derecha.
![Page 135: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/135.jpg)
Oscar Bonilla 135 Universidad Galileo
Ejemplo: Eliminando Recursión Izquierda
<E> <E> + <T><E> <T>
<E> <T><E’><E’> + <T><E’><E’>
![Page 136: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/136.jpg)
Oscar Bonilla 136 Universidad Galileo
Eliminando Recursión Izquierda
• En general, cuando tenemos producciones de la forma <X> <X> y <X> donde no comienza con <X>, sabemos que esto deriva strings de la forma *, así que la podemos reescribir usando recursión derecha.
11
22
11
22
<X> <X ><X> <X>
<X> <X ><X> <X>
<X > <X ><X>
<X > <X ><X>
<X >
![Page 137: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/137.jpg)
Oscar Bonilla 137 Universidad Galileo
Factorización Izquierda
• Un problema similar ocurre cuando dos producciones con el mismo no-terminal comienzan con el mismo símbolo.
• Ejemplo:<S> if <E> then <S> else <S><S> if <E> then <S>
• En este caso, podemos factorizar la gramática por la izquierda, es decir, tomar las terminaciones posibles y crear un nuevo no terminal <X> que las incluya.
![Page 138: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/138.jpg)
Oscar Bonilla 138 Universidad Galileo
Ejemplo: Factorización Izquierda
<S> if <E> then <S> else <S><S> if <E> then <S>
<S> if <E> then <S> <X><X> <X> else <S>
Nota: La gramática todavía es ambigua, pero las producciones resultantes no van a ser problema para un parser predictivo (podemos usar siempre la producción “else <S>”)
![Page 139: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/139.jpg)
Oscar Bonilla 139 Universidad Galileo
Resumen
• Parsers Predictivos
• Implementando un Parser
• Ejemplo de un Parser shift-reduce
• ¿Por qué es difícil construir un parser engine?
• LR(k) parser tables
• Construyendo un Parser Engine LR(0)
![Page 140: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/140.jpg)
Oscar Bonilla 140 Universidad Galileo
Implementando un Parser
• Técnicas diferentes– Cada una puede manejar distintas CFGs– Categorización de Técnicas
![Page 141: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/141.jpg)
Oscar Bonilla 141 Universidad Galileo
Implementando un Parser
• Técnicas diferentes– Cada una puede manejar distintas CFGs– Categorización de Técnicas
( )
![Page 142: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/142.jpg)
Oscar Bonilla 142 Universidad Galileo
Implementando un Parser
• Técnicas diferentes– Cada una puede manejar distintas CFGs– Categorización de Técnicas
( )
L – parsear de izquierda a derechaR – parsear de derecha a izquierda
![Page 143: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/143.jpg)
Oscar Bonilla 143 Universidad Galileo
Implementando un Parser
• Técnicas diferentes– Cada una puede manejar distintas CFGs– Categorización de Técnicas
( )
L - leftmost derivationR - rightmost derivation
![Page 144: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/144.jpg)
Oscar Bonilla 144 Universidad Galileo
Implementando un Parser
• Técnicas diferentes– Cada una puede manejar distintas CFGs– Categorización de Técnicas
( )
Número de caracteres de lookahead
![Page 145: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/145.jpg)
Oscar Bonilla 145 Universidad Galileo
Implementando un Parser
• Técnicas diferentes– Cada una puede manejar distintas CFGs– Categorización de Técnicas
– Ejemplos: LL(0), LR(1)
( )
![Page 146: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/146.jpg)
Oscar Bonilla 146 Universidad Galileo
Implementando un Parser
• Técnicas diferentes– Cada una puede manejar distintas CFGs– Categorización de Técnicas
– Ejemplos: LL(0), LR(1)
• Ya vimos parser LL(1)• Vamos a estudiar parsers LR(k)
( )L R k
![Page 147: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/147.jpg)
Oscar Bonilla 147 Universidad Galileo
Resumen
• Parsers Predictivos
• Implementando un Parser
• Ejemplo de un Parser shift-reduce
• ¿Por qué es difícil construir un parser engine?
• LR(k) parser tables
• Construyendo un Parser Engine LR(0)
![Page 148: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/148.jpg)
Oscar Bonilla 148 Universidad Galileo
Por qué usar un parser LR(k)
• Puede ser construido para que reconozca una gran gama de CFGs– Virtualmente todas las construcciones de los lenguajes de
programación
• Es el método más general de parseo que no necesita retroceder
• Se puede contruir un parser engine muy eficiente
• Puede detectar errores sintácticos tan pronto como es posible detectarlos
![Page 149: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/149.jpg)
Oscar Bonilla 149 Universidad Galileo
Veamos la implementación de un Parser
• Funcionamiento de un parser LR(k)
• parsea de izquierda a derecha
• rightmost derivation– Comenzamos con el string de entrada– Terminamos con el símbolo de inicio
![Page 150: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/150.jpg)
Oscar Bonilla 150 Universidad Galileo
Acciones de un Parser shift-reduce
Arbol deParseo
![Page 151: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/151.jpg)
Oscar Bonilla 151 Universidad Galileo
Acciones de un Parser shift-reduce
ParseTree
Arbol deParseo
![Page 152: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/152.jpg)
Oscar Bonilla 152 Universidad Galileo
Acciones de un Parser shift-reduce
ParseTree
Arbol deParseo
![Page 153: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/153.jpg)
Oscar Bonilla 153 Universidad Galileo
Acciones de un Parser shift-reduce
ParseTree
Arbol deParseo
![Page 154: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/154.jpg)
Oscar Bonilla 154 Universidad Galileo
Acciones de un Parser shift-reduce
ParseTree
Arbol deParseo
![Page 155: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/155.jpg)
Oscar Bonilla 155 Universidad Galileo
Acciones de un Parser shift-reduce
ParseTree
Arbol deParseo
![Page 156: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/156.jpg)
Oscar Bonilla 156 Universidad Galileo
Acciones de un Parser shift-reduce
ParseTree
Arbol deParseo
![Page 157: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/157.jpg)
Oscar Bonilla 157 Universidad Galileo
Acciones de un Parser shift-reduce
ParseTree
Arbol deParseo
![Page 158: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/158.jpg)
Oscar Bonilla 158 Universidad Galileo
Acciones de un Parser shift-reduce
Arbol deParseo
![Page 159: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/159.jpg)
Oscar Bonilla 159 Universidad Galileo
Acciones de un Parser shift-reduce
Arbol deParseo
![Page 160: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/160.jpg)
Oscar Bonilla 160 Universidad Galileo
Acciones de un Parser shift-reduce
Arbol deParseo
![Page 161: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/161.jpg)
Oscar Bonilla 161 Universidad Galileo
Acciones de un Parser shift-reduce
Arbol deParseo
![Page 162: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/162.jpg)
Oscar Bonilla 162 Universidad Galileo
Acciones de un Parser shift-reduce
• No podemos crear un sub-árbol de parseo completo
• Necesitamos la información del look ahead
• Por lo tanto, mantenemos estado
ParseTree
![Page 163: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/163.jpg)
Oscar Bonilla 163 Universidad Galileo
Acciones de un Parser shift-reduce
ParseTree
Est
ado
Act
ual
![Page 164: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/164.jpg)
Oscar Bonilla 164 Universidad Galileo
Acciones de un Parser shift-reduceS
tack
Símbolo Actual
stack
Acción
del P
arserParserEngine
![Page 165: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/165.jpg)
Oscar Bonilla 165 Universidad Galileo
Acciones de un Parser shift-reduce• Shift
– Shift del símbolo actual al top del stack– Movemos el pointer actual
• Reduce– Aplicamos una producción– El top del stack debe hacer match con el RHS– Eliminamos esos símbolos del stack– Agregamos los no-terminales del LHS
• Accept– Llegamos al final del stream de input &– El stack sólo tiene el símbolo de inicio
• Reject– Llegamos al final del stream de input, pero – El stack tiene más que el símbolo de inicio
![Page 166: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/166.jpg)
Oscar Bonilla 166 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* ( + num )numnum
![Page 167: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/167.jpg)
Oscar Bonilla 167 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* ( + num )numnum
![Page 168: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/168.jpg)
Oscar Bonilla 168 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* ( + num )numnum
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
![Page 169: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/169.jpg)
Oscar Bonilla 169 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* ( + num )numnum
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
![Page 170: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/170.jpg)
Oscar Bonilla 170 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* ( + num )numnum
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
![Page 171: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/171.jpg)
Oscar Bonilla 171 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* ( + num )num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
num
![Page 172: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/172.jpg)
Oscar Bonilla 172 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* ( + num )numnum
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *num
SH
IFT
![Page 173: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/173.jpg)
Oscar Bonilla 173 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* ( + num )numnum
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *num
![Page 174: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/174.jpg)
Oscar Bonilla 174 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* ( + num )numnum
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *num
RE
DU
CE
![Page 175: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/175.jpg)
Oscar Bonilla 175 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* ( + num )num
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *<expr>
RE
DU
CE
![Page 176: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/176.jpg)
Oscar Bonilla 176 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* ( + num )num
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *<expr>
![Page 177: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/177.jpg)
Oscar Bonilla 177 Universidad Galileo
Ejemplo: Parser Shift-Reduce
( + num )num
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num *
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
*
<expr>
SH
IFT
![Page 178: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/178.jpg)
Oscar Bonilla 178 Universidad Galileo
Ejemplo: Parser Shift-Reduce
( + num )num
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num *
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
*
<expr>
![Page 179: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/179.jpg)
Oscar Bonilla 179 Universidad Galileo
Ejemplo: Parser Shift-Reduce
( + num )num
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num *
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
*
<expr>
RE
DU
CE
![Page 180: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/180.jpg)
Oscar Bonilla 180 Universidad Galileo
Ejemplo: Parser Shift-Reduce
( + num )num
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num *
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
<expr>
RE
DU
CE
![Page 181: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/181.jpg)
Oscar Bonilla 181 Universidad Galileo
(
Ejemplo: Parser Shift-Reduce
+ num )num
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num *
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
<expr>
![Page 182: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/182.jpg)
Oscar Bonilla 182 Universidad Galileo
Ejemplo: Parser Shift-Reduce
+ num )num
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num * (
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
(
<expr>
SH
IFT
![Page 183: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/183.jpg)
Oscar Bonilla 183 Universidad Galileo
Ejemplo: Parser Shift-Reduce
+ num )num
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num * (
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
(
<expr>
![Page 184: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/184.jpg)
Oscar Bonilla 184 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* + num )
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num ( num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
(
num
<expr>
SH
IFT
![Page 185: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/185.jpg)
Oscar Bonilla 185 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* + num )
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num ( num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
(
num
<expr>
![Page 186: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/186.jpg)
Oscar Bonilla 186 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* + num )
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num ( num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
(
num
<expr>
RE
DU
CE
![Page 187: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/187.jpg)
Oscar Bonilla 187 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* + num )
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num ( num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
(
<expr>
<expr>
RE
DU
CE
![Page 188: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/188.jpg)
Oscar Bonilla 188 Universidad Galileo
+
Ejemplo: Parser Shift-Reduce
* num )
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num ( num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
(
<expr>
<expr>
![Page 189: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/189.jpg)
Oscar Bonilla 189 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* num )
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num ( num +
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
(
<expr>
+
<expr>
SH
IFT
![Page 190: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/190.jpg)
Oscar Bonilla 190 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* num )
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num ( num +
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
(
<expr>
+
<expr>
![Page 191: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/191.jpg)
Oscar Bonilla 191 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* num )
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num ( num +
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
(
<expr>
+
<expr>
RE
DU
CE
![Page 192: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/192.jpg)
Oscar Bonilla 192 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* num )
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num ( num +
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
(
<expr>
<op>
<expr>
RE
DU
CE
![Page 193: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/193.jpg)
Oscar Bonilla 193 Universidad Galileo
num
Ejemplo: Parser Shift-Reduce
* )
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num ( num +
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
(
<expr>
<op>
<expr>
![Page 194: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/194.jpg)
Oscar Bonilla 194 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* )
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num ( num + num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
(
<expr>
<op>
num
<expr>
SH
IFT
![Page 195: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/195.jpg)
Oscar Bonilla 195 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* )
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num ( num + num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
(
<expr>
<op>
num
<expr>
![Page 196: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/196.jpg)
Oscar Bonilla 196 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* )
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num ( num + num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
(
<expr>
<op>
num
<expr>
RE
DU
CE
![Page 197: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/197.jpg)
Oscar Bonilla 197 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* )
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num ( num + num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
(
<expr>
<op>
<expr>
<expr>
RE
DU
CE
![Page 198: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/198.jpg)
Oscar Bonilla 198 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* )
<expr> <op>
<expr>
<expr>
<expr>
num ( num + num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
(
<expr>
<op>
<expr>
<expr>
<op><expr> <expr>
![Page 199: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/199.jpg)
Oscar Bonilla 199 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* )
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num ( num + num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
(
<expr>
<op>
<expr>
<expr>
RE
DU
CE
![Page 200: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/200.jpg)
Oscar Bonilla 200 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* )
<expr> <expr> <expr><op> <op>
<expr>
<expr>
num ( num + num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
(
<expr>
<expr>
<expr>
RE
DU
CE
![Page 201: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/201.jpg)
Oscar Bonilla 201 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* )
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num ( num + num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
(
<expr>
<expr>
![Page 202: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/202.jpg)
Oscar Bonilla 202 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* )
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num ( num + num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
(
<expr>
)
<expr>
SH
IFT
![Page 203: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/203.jpg)
Oscar Bonilla 203 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* )
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num ( num + num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
(
<expr>
)
<expr>
![Page 204: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/204.jpg)
Oscar Bonilla 204 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* )
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num ( num + num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
(
<expr>
)
<expr>
RE
DU
CE
![Page 205: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/205.jpg)
Oscar Bonilla 205 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* )
<expr> <expr> <expr><op> <op>
<expr>
num ( num + num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
<expr>
<expr>
<expr>
<expr>
RE
DU
CE
![Page 206: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/206.jpg)
Oscar Bonilla 206 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* )
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num ( num + num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
<expr>
<expr>
![Page 207: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/207.jpg)
Oscar Bonilla 207 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* )
<expr> <expr> <expr><op> <op>
<expr>
<expr>
<expr>
num ( num + num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
<op>
<expr>
<expr>
RE
DU
CE
![Page 208: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/208.jpg)
Oscar Bonilla 208 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* )
<expr> <expr> <expr><op> <op>
<expr>
<expr>
num ( num + num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *<expr>
<expr>
RE
DU
CE
![Page 209: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/209.jpg)
Oscar Bonilla 209 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* )
<expr> <expr> <expr><op> <op>
<expr>
<expr>
num ( num + num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *<expr>
<expr>
![Page 210: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/210.jpg)
Oscar Bonilla 210 Universidad Galileo
Ejemplo: Parser Shift-Reduce
* )
<expr> <expr> <expr><op> <op>
<expr>
<expr>
num ( num + num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *<expr>
<expr>
AC
CE
PT
![Page 211: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/211.jpg)
Oscar Bonilla 211 Universidad Galileo
¿Qúe hace el parser engine?
• Si los símbolos en el top del stack hacen match con el RHS de una producción, hacemos la reducción– Pop del RHS del top del stack– Push del símbolo del LHS al stack
• Si no se encuentra ninguna producción, se hace un shift– push del input actual al stack
• Si el input está vacío– accept si en el stack sólo está el símbolo de inicio– reject en cualquier otro caso
ParserEngine
![Page 212: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/212.jpg)
Oscar Bonilla 212 Universidad Galileo
Resumen
• Parsers Predictivos
• Implementando un Parser
• Ejemplo de un Parser shift-reduce
• ¿Por qué es difícil construir un Parser Engine?
• LR(k) parser tables
• Construyendo un Parser Engine LR(0)
![Page 213: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/213.jpg)
Oscar Bonilla 213 Universidad Galileo
¿Qúe hace el parser engine?
• Si los símbolos en el top del stack hacen match con el RHS de una producción, hacemos la reducción– Pop del RHS del top del stack– Push del símbolo del LHS al stack
• Si no se encuentra ninguna producción, se hace un shift– push del input actual al stack
• Si el input está vacío– accept si en el stack sólo está el símbolo de inicio– reject en cualquier otro caso
ParserEngine
![Page 214: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/214.jpg)
Oscar Bonilla 214 Universidad Galileo
¡No es tan simple!
• Muchas opciones para reducir– Match con múltiples RHS
• Elección entre shift y reduce– Stack contiene un RHS– Pero puede ser que reducir no sea lo más adecuado– Puede ser que si hacemos shift del input, después
encontremos una reducción distinta
![Page 215: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/215.jpg)
Oscar Bonilla 215 Universidad Galileo
Ejemplo: Parser Shift-Reduce
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
•Un cambio en la gramática
![Page 216: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/216.jpg)
Oscar Bonilla 216 Universidad Galileo
Ejemplo: Parser Shift-Reduce
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> - <expr>
<expr> num
<op> +
<op> -
<op> *
•Un cambio en la gramática
![Page 217: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/217.jpg)
Oscar Bonilla 217 Universidad Galileo
Ejemplo: Parser Shift-Reduce
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -<expr> num
<op> +
<op> -
<op> *
•Un cambio en la gramática
![Page 218: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/218.jpg)
Oscar Bonilla 218 Universidad Galileo
Ejemplo: Parser Shift-Reduce
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -<expr> num
<op> +
<op> -
<op> *
•Un cambio en la gramática
![Page 219: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/219.jpg)
Oscar Bonilla 219 Universidad Galileo
Ejemplo: Parser Shift-Reduce
- numnum
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
![Page 220: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/220.jpg)
Oscar Bonilla 220 Universidad Galileo
Ejemplo: Parser Shift-Reduce
- num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
num
![Page 221: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/221.jpg)
Oscar Bonilla 221 Universidad Galileo
num
Ejemplo: Parser Shift-Reduce
- num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum
SH
IFT
![Page 222: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/222.jpg)
Oscar Bonilla 222 Universidad Galileo
num
Ejemplo: Parser Shift-Reduce
- num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum
![Page 223: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/223.jpg)
Oscar Bonilla 223 Universidad Galileo
num
Ejemplo: Parser Shift-Reduce
- num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum
RE
DU
CE
![Page 224: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/224.jpg)
Oscar Bonilla 224 Universidad Galileo
<expr>
Ejemplo: Parser Shift-Reduce
- num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum
RE
DU
CE
<expr>
![Page 225: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/225.jpg)
Oscar Bonilla 225 Universidad Galileo
<expr>
Ejemplo: Parser Shift-Reduce
- num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum
<expr>
![Page 226: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/226.jpg)
Oscar Bonilla 226 Universidad Galileo
-<expr>
Ejemplo: Parser Shift-Reduce
num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum -
<op><expr>
SH
IFT
![Page 227: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/227.jpg)
Oscar Bonilla 227 Universidad Galileo
-<expr>
Ejemplo: Parser Shift-Reduce
num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum -
<op><expr>
![Page 228: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/228.jpg)
Oscar Bonilla 228 Universidad Galileo
-<expr>
Ejemplo: Parser Shift-Reduce
num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum -
¡¡¡Tenemos elección!!!<op><expr>
RE
DU
CE
![Page 229: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/229.jpg)
Oscar Bonilla 229 Universidad Galileo
-<expr>
Ejemplo: Parser Shift-Reduce
num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum -
¡¡Pero esto no es lo correcto!!<op><expr>
RE
DU
CE
![Page 230: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/230.jpg)
Oscar Bonilla 230 Universidad Galileo
<expr>
Ejemplo: Parser Shift-Reduce
num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum -
¡¡Pero esto no es lo correcto!!<expr>
<expr>
RE
DU
CE
![Page 231: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/231.jpg)
Oscar Bonilla 231 Universidad Galileo
<expr>
Ejemplo: Parser Shift-Reduce
num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum -
¡¡Pero esto no es lo correcto!!<expr>
<expr>
![Page 232: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/232.jpg)
Oscar Bonilla 232 Universidad Galileo
num
<expr>
Ejemplo: Parser Shift-Reduce
num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum -
¡¡Pero esto no es lo correcto!!<expr>
<expr>
SH
IFT
![Page 233: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/233.jpg)
Oscar Bonilla 233 Universidad Galileo
num
<expr>
Ejemplo: Parser Shift-Reduce
num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum -
¡¡Pero esto no es lo correcto!!<expr>
<expr>
![Page 234: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/234.jpg)
Oscar Bonilla 234 Universidad Galileo
num
<expr>
Ejemplo: Parser Shift-Reduce
num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum -
¡¡Pero esto no es lo correcto!!<expr>
<expr>
RE
DU
CE
![Page 235: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/235.jpg)
Oscar Bonilla 235 Universidad Galileo
<expr>
<expr>
Ejemplo: Parser Shift-Reduce
num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum -
¡¡Pero esto no es lo correcto!!<expr>
<expr>
<expr>
RE
DU
CE
![Page 236: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/236.jpg)
Oscar Bonilla 236 Universidad Galileo
<expr>
<expr>
Ejemplo: Parser Shift-Reduce
num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum -
¡¡Pero esto no es lo correcto!!<expr>
<expr>
<expr>
![Page 237: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/237.jpg)
Oscar Bonilla 237 Universidad Galileo
<expr>
<expr>
Ejemplo: Parser Shift-Reduce
num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum -
¡¡Pero esto no es lo correcto!! ¡¡No hay más acciones!!
<expr>
<expr>
<expr>
ER
RO
R
![Page 238: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/238.jpg)
Oscar Bonilla 238 Universidad Galileo
Ejemplo: Parser Shift-Reduce
numnumnum -
• Pero este input es perfectamente válido para la gramática
• Elegimos la producción equivocada
• Veamos cuál es la producción apropiada
![Page 239: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/239.jpg)
Oscar Bonilla 239 Universidad Galileo
-<expr>
Ejemplo: Parser Shift-Reduce
num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum -
Tenemos elección<op><expr>
RE
DU
CE
El paso anterior a nuestro error
![Page 240: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/240.jpg)
Oscar Bonilla 240 Universidad Galileo
-<expr>
Ejemplo: Parser Shift-Reduce
num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum -
Usamos laotra producción
<op><expr>
RE
DU
CE
![Page 241: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/241.jpg)
Oscar Bonilla 241 Universidad Galileo
<op>
<expr>
Ejemplo: Parser Shift-Reduce
num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum -
<expr>
RE
DU
CE
<op> Usamos laotra producción
![Page 242: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/242.jpg)
Oscar Bonilla 242 Universidad Galileo
<op>
<expr>
Ejemplo: Parser Shift-Reduce
num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum -
<expr> <op>
![Page 243: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/243.jpg)
Oscar Bonilla 243 Universidad Galileo
num
<op>
<expr>
Ejemplo: Parser Shift-Reduce
num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum -
<expr> <op>
SH
IFT
![Page 244: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/244.jpg)
Oscar Bonilla 244 Universidad Galileo
num
<op>
<expr>
Ejemplo: Parser Shift-Reduce
num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum -
<expr> <op>
![Page 245: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/245.jpg)
Oscar Bonilla 245 Universidad Galileo
num
<op>
<expr>
Ejemplo: Parser Shift-Reduce
num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum -
<expr> <op>
RE
DU
CE
![Page 246: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/246.jpg)
Oscar Bonilla 246 Universidad Galileo
<expr>
<op>
<expr>
Ejemplo: Parser Shift-Reduce
num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum -
<expr> <op>
RE
DU
CE
<expr>
![Page 247: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/247.jpg)
Oscar Bonilla 247 Universidad Galileo
<expr>
<op>
<expr>
Ejemplo: Parser Shift-Reduce
num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum -
<expr> <op> <expr>
![Page 248: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/248.jpg)
Oscar Bonilla 248 Universidad Galileo
<expr>
<op>
<expr>
Ejemplo: Parser Shift-Reduce
num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum -
<expr> <op> <expr>
RE
DU
CE
![Page 249: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/249.jpg)
Oscar Bonilla 249 Universidad Galileo
<expr>
Ejemplo: Parser Shift-Reduce
num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum -
<expr> <op> <expr>
RE
DU
CE
<expr>
![Page 250: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/250.jpg)
Oscar Bonilla 250 Universidad Galileo
<expr>
Ejemplo: Parser Shift-Reduce
num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum -
<expr> <op> <expr>
<expr>
![Page 251: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/251.jpg)
Oscar Bonilla 251 Universidad Galileo
<expr>
Ejemplo: Parser Shift-Reduce
num
<expr> <expr> <op> <expr>
<expr> ( <expr> )
<expr> <expr> -
<expr> num
<op> +
<op> -
<op> *
numnum -
<expr> <op> <expr>
<expr>
AC
CE
PT
![Page 252: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/252.jpg)
Oscar Bonilla 252 Universidad Galileo
¡No es tan simple!
• Muchas opciones para reducir– Match con múltiples RHS
• Elección entre shift y reduce– Stack contiene un RHS– Pero puede ser que reducir no sea lo más adecuado– Puede ser que si hacemos shift del input después
encontremos una reducción distinta
• Mantenemos información adicional
![Page 253: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/253.jpg)
Oscar Bonilla 253 Universidad Galileo
Resumen
• Parsers Predictivos
• Implementando un Parser
• Ejemplo de un Parser shift-reduce
• ¿Por qué es difícil construir un Parser Engine?
• LR(k) parser tables
• Construyendo un Parser Engine LR(0)
![Page 254: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/254.jpg)
Oscar Bonilla 254 Universidad Galileo
Construyendo un Parser LR(k)
• Vamos a construir unos cuantos parsers LR(k)– LR(0), – SLR (o simple LR)– LR(1)
![Page 255: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/255.jpg)
Oscar Bonilla 255 Universidad Galileo
Construyendo un Parser LR(k)
• Vamos a construir unos cuantos parsers LR(k)– LR(0), – SLR (o simple LR)– LR(1)
• Ya seguimos las acciones del parseo
• ¿Qué hay en el parse engine?– elegir entre shift y reduce– elegir la reducción correcta
![Page 256: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/256.jpg)
Oscar Bonilla 256 Universidad Galileo
Acciones de un Parser Shift-ReduceS
tack
Símbolo Actual
stack
Acción
del P
arserParserEngine
![Page 257: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/257.jpg)
Oscar Bonilla 257 Universidad Galileo
Construyendo un Parser LR(k)
• Creamos un DFA – Codifica todos los posibles estados en que puede estar
el parser– Ocurre una transición de estado del DFA en terminales
y no-terminales
• Creamos una Tabla de Parseo– Guarda que acción debe ser tomada a partir del estado
actual y el símbolo de entrada actual
• Mantenemos un stack de estados– En paralelo con el stack de símbolos
![Page 258: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/258.jpg)
Oscar Bonilla 258 Universidad Galileo
LR(k) Parser Engine
Símbolo Actual
Acción
del P
arser
LR(k)ParserEngine
Sta
ck d
e S
ímb
olos
Sta
ck d
e E
stad
os
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
![Page 259: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/259.jpg)
Oscar Bonilla 259 Universidad Galileo
Tablas de Parseo
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
• Buscamos – [top del stack de estados]
– [ símbolo de entrada]
en la tabla de parseo• Ejecutamos la acción descrita
![Page 260: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/260.jpg)
Oscar Bonilla 260 Universidad Galileo
Tablas de Parseo
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
• Shift to sn– Push del token de entrada en el stack de símbolos– Push sn en el stack de estados– Avanzamos al siguiente símbolo de entrada
![Page 261: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/261.jpg)
Oscar Bonilla 261 Universidad Galileo
Tablas de Parseo
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
• Reduce (n)– Pop de ambos stacks tantas veces como el número de
símbolos en el RHS de la regla n – Push LHS de la regla n en el stack de símbolos– Buscar [top del stack de estados][top del stack de símbolos]– Push de ese estado (en goto k) en el stack de estados
![Page 262: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/262.jpg)
Oscar Bonilla 262 Universidad Galileo
Tablas de Parseo
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
• Accept– Dejar de parsear y reportar éxito
![Page 263: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/263.jpg)
Oscar Bonilla 263 Universidad Galileo
Tablas de Parseo
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
• Error– Dejar de parsear y reportar Error
![Page 264: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/264.jpg)
Oscar Bonilla 264 Universidad Galileo
Ejemplo LR
• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
![Page 265: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/265.jpg)
Oscar Bonilla 265 Universidad Galileo
Pregunta
• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
• ¿Cuál es el lenguaje aceptado por esta CFG?
![Page 266: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/266.jpg)
Oscar Bonilla 266 Universidad Galileo
Tabla de Parseo en Acción• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
![Page 267: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/267.jpg)
Oscar Bonilla 267 Universidad Galileo
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)ACTION Goto
State ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
![Page 268: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/268.jpg)
Oscar Bonilla 268 Universidad Galileo
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
$
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
![Page 269: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/269.jpg)
Oscar Bonilla 269 Universidad Galileo
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
![Page 270: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/270.jpg)
Oscar Bonilla 270 Universidad Galileo
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 271: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/271.jpg)
Oscar Bonilla 271 Universidad Galileo
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( )(
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 272: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/272.jpg)
Oscar Bonilla 272 Universidad Galileo
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( )( $
![Page 273: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/273.jpg)
Oscar Bonilla 273 Universidad Galileo
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( )( $
![Page 274: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/274.jpg)
Oscar Bonilla 274 Universidad Galileo
(
s2 (
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
))(
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 275: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/275.jpg)
Oscar Bonilla 275 Universidad Galileo
s2 (
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
))( (
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 276: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/276.jpg)
Oscar Bonilla 276 Universidad Galileo
s2 (
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
))( (
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 277: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/277.jpg)
Oscar Bonilla 277 Universidad Galileo
s2 (
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
))( (
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 278: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/278.jpg)
Oscar Bonilla 278 Universidad Galileo
s2 (s2 (
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
))( (
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 279: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/279.jpg)
Oscar Bonilla 279 Universidad Galileo
s2 (s2 (
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 280: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/280.jpg)
Oscar Bonilla 280 Universidad Galileo
s2 (s2 (
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 281: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/281.jpg)
Oscar Bonilla 281 Universidad Galileo
s2 (s2 (
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 282: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/282.jpg)
Oscar Bonilla 282 Universidad Galileo
s5 )s2 (s2 (
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 283: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/283.jpg)
Oscar Bonilla 283 Universidad Galileo
s5 )s2 (s2 (
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 284: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/284.jpg)
Oscar Bonilla 284 Universidad Galileo
s5 )s2 (s2 (
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 285: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/285.jpg)
Oscar Bonilla 285 Universidad Galileo
s5 )s2 (s2 (
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 286: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/286.jpg)
Oscar Bonilla 286 Universidad Galileo
s5 )s2 (s2 (
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 287: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/287.jpg)
Oscar Bonilla 287 Universidad Galileo
s5 )s2 (s2 (
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
s5 )s2 (
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 288: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/288.jpg)
Oscar Bonilla 288 Universidad Galileo
Xs2 (
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 289: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/289.jpg)
Oscar Bonilla 289 Universidad Galileo
Xs2 (
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 290: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/290.jpg)
Oscar Bonilla 290 Universidad Galileo
Xs2 (
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 291: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/291.jpg)
Oscar Bonilla 291 Universidad Galileo
s3 Xs2 (
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 292: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/292.jpg)
Oscar Bonilla 292 Universidad Galileo
s3 Xs2 (
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 293: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/293.jpg)
Oscar Bonilla 293 Universidad Galileo
s3 Xs2 (
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 294: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/294.jpg)
Oscar Bonilla 294 Universidad Galileo
s3 Xs2 (
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 295: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/295.jpg)
Oscar Bonilla 295 Universidad Galileo
s4 )s3 Xs2 (
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 296: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/296.jpg)
Oscar Bonilla 296 Universidad Galileo
s4 )s3 Xs2 (
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 297: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/297.jpg)
Oscar Bonilla 297 Universidad Galileo
s4 )s3 Xs2 (
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 298: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/298.jpg)
Oscar Bonilla 298 Universidad Galileo
s4 )s3 Xs2 (
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 299: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/299.jpg)
Oscar Bonilla 299 Universidad Galileo
s4 )s3 Xs2 (
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
s4 )s3 Xs2 (
![Page 300: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/300.jpg)
Oscar Bonilla 300 Universidad Galileo
X
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 301: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/301.jpg)
Oscar Bonilla 301 Universidad Galileo
X
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 302: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/302.jpg)
Oscar Bonilla 302 Universidad Galileo
X
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 303: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/303.jpg)
Oscar Bonilla 303 Universidad Galileo
s1 X
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 304: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/304.jpg)
Oscar Bonilla 304 Universidad Galileo
s1 X
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 305: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/305.jpg)
Oscar Bonilla 305 Universidad Galileo
s1 X
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
![Page 306: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/306.jpg)
Oscar Bonilla 306 Universidad Galileo
s1 X
Tabla de Parseo en Acción
• Tabla de Acciones• La gramática
<S> <X> $ (1)
<X> ( <X> ) (2)
<X> ( ) (3)
s0 $
)( ( )
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
$
Accept
![Page 307: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/307.jpg)
Oscar Bonilla 307 Universidad Galileo
Resumen
• Parsers Predictivos
• Implementando un Parser
• Ejemplo de un Parser shift-reduce
• ¿Por qué es difícil construir un Parser Engine?
• LR(k) parser tables
• Construyendo un Parser Engine LR(0)
![Page 308: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/308.jpg)
Oscar Bonilla 308 Universidad Galileo
Items LR(0)
• Tenemos que capturar cuánto de una producción hemos escaneado hasta ahora
<X> ( <X> )
¿Estamos aquí? ¿o aquí? ¿o aquí? ¿o aquí?
![Page 309: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/309.jpg)
Oscar Bonilla 309 Universidad Galileo
Items LR(0)
• Tenemos que capturar cuánto de una producción hemos escaneado hasta ahora
• Representado por 4 ítems– <X> • ( <X> )
– <X> ( • <X> )
– <X> ( <X> • )
– <X> ( <X> ) •
<X> ( <X> )
![Page 310: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/310.jpg)
Oscar Bonilla 310 Universidad Galileo
Ejemplo de Items
• La gramática<S> <X> $
<X> ( <X> )
<X> ( )
• Items<S> • <X> $
<S> <X> • $
<X> • ( <X> )
<X> ( • <X> )
<X> ( <X> • )
<X> ( <X> ) •
<X> • ( )
<X> ( • )
<X> ( ) •
![Page 311: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/311.jpg)
Oscar Bonilla 311 Universidad Galileo
Idea clave de los Items
• Si el “estado actual” contiene el ítemA • c y el símbolo actual en el buffer de entrada es c – El estado le dice al parser que ejecute un shift– El siguiente estado va a contener A c •
• Si el “estado” contiene el ítem A •– El estado le dice al parser que ejecute un reduce
• Si el “estado” contiene el ítem S • $ y el buffer de entrada está vacío– El estado le dice al parser que ejecute un accept
![Page 312: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/312.jpg)
Oscar Bonilla 312 Universidad Galileo
Closure() de un conjunto de ítems
• Closure encuentra todos los ítems en el mismo “estado”
• Algoritmo para closure(I)– Todo ítem en I es también un ítem en closure(I)– Si A • B está en closure(I) y B • es un
ítem, entonces agregamos B • a closure(I)– Repetir hasta que no se puedan agregar más ítems a
closure(I)
![Page 313: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/313.jpg)
Oscar Bonilla 313 Universidad Galileo
Ejemplo de Closure
• Encontrar closure(<X> ( • <X> ) ) • Items<S> • <X> $<S> <X> • $<X> • ( <X> ) <X> ( • <X> )<X> ( <X> • )<X> ( <X> ) •<X> • ( )<X> ( • )<X> ( ) •
<X> ( • <X> ) <X> • ( <X> )<X> • ( )
![Page 314: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/314.jpg)
Oscar Bonilla 314 Universidad Galileo
Pregunta: Encuentren Closure
• Encuentren closure(<S> • <X> $ ) • Items<S> • <X> $<S> <X> • $<X> • ( <X> ) <X> ( • <X> )<X> ( <X> • )<X> ( <X> ) •<X> • ( )<X> ( • )<X> ( ) •
????
![Page 315: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/315.jpg)
Oscar Bonilla 315 Universidad Galileo
Pregunta: Encuentren Closure
• Encuentren closure(<S> • <X> $ ) • Items<S> • <X> $<S> <X> • $<X> • ( <X> ) <X> ( • <X> )<X> ( <X> • )<X> ( <X> ) •<X> • ( )<X> ( • )<X> ( ) •
<S> • <X> $ <X> • ( <X> )<X> • ( )
![Page 316: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/316.jpg)
Oscar Bonilla 316 Universidad Galileo
Goto() de un conjunto de ítems
• Goto encuentra el nuevo estado después de consumir un símbolo de la gramática mientras estamos en el estado actual
• Algoritmo para goto(I, X)donde I es un conjunto de ítems y X es un símbolo de la gramática
goto(I, X) = closure( { A X • | A • X en I })
• Goto es el nuevo conjunto obtenido al “mover el punto” sobre X
![Page 317: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/317.jpg)
Oscar Bonilla 317 Universidad Galileo
Ejemplo de Goto
• Encontrar goto(<X> ( • <X> ), <X> ) Items<S> • <X> $<S> <X> • $<X> • ( <X> ) <X> ( • <X> )<X> ( <X> • )<X> ( <X> ) •<X> • ( )<X> ( • )<X> ( ) •
<X> ( <X> • )
![Page 318: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/318.jpg)
Oscar Bonilla 318 Universidad Galileo
Pregunta: Encuentren goto
• Encuentren goto(<X> • ( <X> ), ( ) Items<S> • <X> $<S> <X> • $<X> • ( <X> ) <X> ( • <X> )<X> ( <X> • )<X> ( <X> ) •<X> • ( )<X> ( • )<X> ( ) •
????
![Page 319: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/319.jpg)
Oscar Bonilla 319 Universidad Galileo
Pregunta: Encuentren goto
• Encuentren goto(<X> • ( <X> ), ( ) Items<S> • <X> $<S> <X> • $<X> • ( <X> ) <X> ( • <X> )<X> ( <X> • )<X> ( <X> ) •<X> • ( )<X> ( • )<X> ( ) •
<X> ( • <X> ) <X> • ( <X> )<X> • ( )
![Page 320: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/320.jpg)
Oscar Bonilla 320 Universidad Galileo
• Comenzamos con la producción <S’> • <S> $• El primer estado es closure(<S’> • <S> $)• Elegimos un estado I
– Para cada A • X en I
• encontrar goto(I, X)
• si goto(I, X) no es ya un estado, creamos uno
• Agregamos una arista X del estado I al estado goto(I, X)
• Repetimos hasta que no sea posible agregar nada más
Construyendo los estados del DFA
![Page 321: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/321.jpg)
Oscar Bonilla 321 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
![Page 322: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/322.jpg)
Oscar Bonilla 322 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
![Page 323: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/323.jpg)
Oscar Bonilla 323 Universidad Galileo
<S> • <X> $
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
![Page 324: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/324.jpg)
Oscar Bonilla 324 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $
![Page 325: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/325.jpg)
Oscar Bonilla 325 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
![Page 326: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/326.jpg)
Oscar Bonilla 326 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0
![Page 327: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/327.jpg)
Oscar Bonilla 327 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0
![Page 328: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/328.jpg)
Oscar Bonilla 328 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0
![Page 329: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/329.jpg)
Oscar Bonilla 329 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0
![Page 330: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/330.jpg)
Oscar Bonilla 330 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0
![Page 331: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/331.jpg)
Oscar Bonilla 331 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0
goto( , <X>)
![Page 332: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/332.jpg)
Oscar Bonilla 332 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
goto( , <X>)
![Page 333: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/333.jpg)
Oscar Bonilla 333 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
![Page 334: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/334.jpg)
Oscar Bonilla 334 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1
![Page 335: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/335.jpg)
Oscar Bonilla 335 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1
![Page 336: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/336.jpg)
Oscar Bonilla 336 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
![Page 337: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/337.jpg)
Oscar Bonilla 337 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
![Page 338: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/338.jpg)
Oscar Bonilla 338 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
![Page 339: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/339.jpg)
Oscar Bonilla 339 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
goto( , ( )
![Page 340: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/340.jpg)
Oscar Bonilla 340 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
goto( , ( )
![Page 341: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/341.jpg)
Oscar Bonilla 341 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
goto( , ( )
![Page 342: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/342.jpg)
Oscar Bonilla 342 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
goto( , ( )
![Page 343: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/343.jpg)
Oscar Bonilla 343 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
![Page 344: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/344.jpg)
Oscar Bonilla 344 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
![Page 345: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/345.jpg)
Oscar Bonilla 345 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
![Page 346: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/346.jpg)
Oscar Bonilla 346 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(
![Page 347: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/347.jpg)
Oscar Bonilla 347 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(
![Page 348: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/348.jpg)
Oscar Bonilla 348 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(
![Page 349: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/349.jpg)
Oscar Bonilla 349 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(
goto( , ( )
![Page 350: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/350.jpg)
Oscar Bonilla 350 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(
goto( , ( )El mismo de la vez pasada
![Page 351: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/351.jpg)
Oscar Bonilla 351 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(
![Page 352: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/352.jpg)
Oscar Bonilla 352 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(
![Page 353: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/353.jpg)
Oscar Bonilla 353 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(
![Page 354: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/354.jpg)
Oscar Bonilla 354 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(
![Page 355: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/355.jpg)
Oscar Bonilla 355 Universidad Galileo
Ejemplo de construcción de los estados del DFAComenzar con la producción <S’> • <S> $Crear el primer estado como closure(<S’> • <S> $)Elegir un estado I
Para cada A • X en Iencontrar goto(I, X)si goto(I, X) no es ya un estado, crear unoAgregar una arista X del estado I al estado goto(I, X)
Repetir hasta que no sea posible agregar nada más
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(
![Page 356: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/356.jpg)
Oscar Bonilla 356 Universidad Galileo
Ejemplo de construcción de los estados del DFA
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(
![Page 357: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/357.jpg)
Oscar Bonilla 357 Universidad Galileo
Ejemplo de construcción de los estados del DFA
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(<X> ( <X> • )
X
s3
![Page 358: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/358.jpg)
Oscar Bonilla 358 Universidad Galileo
Ejemplo de construcción de los estados del DFA
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(<X> ( <X> • )
X
s3
(
![Page 359: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/359.jpg)
Oscar Bonilla 359 Universidad Galileo
Ejemplo de construcción de los estados del DFA
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(<X> ( <X> • )
X
s3
(
<X> ( ) •
)s5
![Page 360: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/360.jpg)
Oscar Bonilla 360 Universidad Galileo
Ejemplo de construcción de los estados del DFA
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(<X> ( <X> • )
X
s3
(
<X> ( ) •
)s5
![Page 361: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/361.jpg)
Oscar Bonilla 361 Universidad Galileo
Ejemplo de construcción de los estados del DFA
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(<X> ( <X> • )
X
s3
(
<X> ( ) •
)s5
![Page 362: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/362.jpg)
Oscar Bonilla 362 Universidad Galileo
Ejemplo de construcción de los estados del DFA
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(<X> ( <X> • )
X
s3
(
<X> ( ) •
)s5
<X> ( <X> ) •
)s4
![Page 363: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/363.jpg)
Oscar Bonilla 363 Universidad Galileo
Ejemplo de construcción de los estados del DFA
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(<X> ( <X> • )
X
s3
(
<X> ( ) •
)s5
<X> ( <X> ) •
)s4
![Page 364: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/364.jpg)
Oscar Bonilla 364 Universidad Galileo
Ejemplo de construcción de los estados del DFA
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(<X> ( <X> • )
X
s3
(
<X> ( ) •
)s5
<X> ( <X> ) •
)s4
![Page 365: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/365.jpg)
Oscar Bonilla 365 Universidad Galileo
Construyendo un Parser Engine LR(0)
• Construir un DFA– HECHO
• Construir una tabla de parseo usando el DFA
![Page 366: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/366.jpg)
Oscar Bonilla 366 Universidad Galileo
Creando las tablas de parseo
• Para cada estado• Transición a otro estado usando un símbolo
terminal es un shift a ese estado (shift to sn)• Transición a otro estado usando un no-terminal es
un goto a ese estado (goto sn)• Si hay un ítem A • en el estado hacemos una
reducción con esa producción para todos los terminales (reduce k)
![Page 367: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/367.jpg)
Oscar Bonilla 367 Universidad Galileo
Ejemplo de Construcción de Parse Table
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(<X> ( <X> • )
X
s3
(
<X> ( ) •
)s5
<X> ( <X> ) •
)s4
![Page 368: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/368.jpg)
Oscar Bonilla 368 Universidad Galileo
Ejemplo de Construcción de Parse Table
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(<X> ( <X> • )
X
s3
(
<X> ( ) •
)s5
<X> ( <X> ) •
)s4
ACTION GotoState ( ) $ X
![Page 369: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/369.jpg)
Oscar Bonilla 369 Universidad Galileo
Ejemplo de Construcción de Parse Table
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(<X> ( <X> • )
X
s3
(
<X> ( ) •
)s5
<X> ( <X> ) •
)s4
ACTION GotoState ( ) $ Xs0s1s2s3s4s5
![Page 370: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/370.jpg)
Oscar Bonilla 370 Universidad Galileo
Ejemplo de Construcción de Parse Table
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(<X> ( <X> • )
X
s3
(
<X> ( ) •
)s5
<X> ( <X> ) •
)s4
ACTION GotoState ( ) $ Xs0s1s2s3s4s5
![Page 371: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/371.jpg)
Oscar Bonilla 371 Universidad Galileo
Ejemplo de Construcción de Parse Table
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(<X> ( <X> • )
X
s3
(
<X> ( ) •
)s5
<X> ( <X> ) •
)s4
ACTION GotoState ( ) $ Xs0 shift to s2s1s2s3s4s5
<S> • <X> $<X> • ( <X> )<X> • ( )
![Page 372: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/372.jpg)
Oscar Bonilla 372 Universidad Galileo
Ejemplo de Construcción de Parse Table
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(<X> ( <X> • )
X
s3
(
<X> ( ) •
)s5
<X> ( <X> ) •
)s4
ACTION GotoState ( ) $ Xs0 shift to s2 goto s1s1s2s3s4s5
<S> • <X> $<X> • ( <X> )<X> • ( )
![Page 373: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/373.jpg)
Oscar Bonilla 373 Universidad Galileo
Ejemplo de Construcción de Parse Table
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(<X> ( <X> • )
X
s3
(
<X> ( ) •
)s5
<X> ( <X> ) •
)s4
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1s2s3s4s5
<S> • <X> $<X> • ( <X> )<X> • ( )
![Page 374: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/374.jpg)
Oscar Bonilla 374 Universidad Galileo
Ejemplo de Construcción de Parse Table
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(<X> ( <X> • )
X
s3
(
<X> ( ) •
)s5
<X> ( <X> ) •
)s4
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1s2s3s4s5
<S> • <X> $<X> • ( <X> )<X> • ( )
![Page 375: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/375.jpg)
Oscar Bonilla 375 Universidad Galileo
Ejemplo de Construcción de Parse Table
<S> • <X> $<X> • ( <X> )<X> • ( )
s0
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(<X> ( <X> • )
X
s3
(
<X> ( ) •
)s5
<X> ( <X> ) •
)s4
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 accept s2s3s4s5
<S> <X> • $
![Page 376: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/376.jpg)
Oscar Bonilla 376 Universidad Galileo
Ejemplo de Construcción de Parse Table
<S> • <X> $<X> • ( <X> )<X> • ( )
s0
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(<X> ( <X> • )
X
s3
(
<X> ( ) •
)s5
<X> ( <X> ) •
)s4
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2s3s4s5
<S> <X> • $
![Page 377: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/377.jpg)
Oscar Bonilla 377 Universidad Galileo
Ejemplo de Construcción de Parse Table
<S> • <X> $<X> • ( <X> )<X> • ( )
s0
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(<X> ( <X> • )
X
s3
(
<X> ( ) •
)s5
<X> ( <X> ) •
)s4
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2s3s4s5
<S> <X> • $
![Page 378: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/378.jpg)
Oscar Bonilla 378 Universidad Galileo
Ejemplo de Construcción de Parse Table
<S> • <X> $<X> • ( <X> )<X> • ( )
s0
s1X
s2
(<X> ( <X> • )
X
s3
(
<X> ( ) •
)s5
<X> ( <X> ) •
)s4
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2s3s4s5
<S> <X> • $
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
![Page 379: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/379.jpg)
Oscar Bonilla 379 Universidad Galileo
Ejemplo de Construcción de Parse Table
<S> • <X> $<X> • ( <X> )<X> • ( )
s0
s1X
s2
(<X> ( <X> • )
X
s3
(
<X> ( ) •
)s5
<X> ( <X> ) •
)s4
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5s3s4s5
<S> <X> • $
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
![Page 380: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/380.jpg)
Oscar Bonilla 380 Universidad Galileo
Ejemplo de Construcción de Parse Table
<S> • <X> $<X> • ( <X> )<X> • ( )
s0
s1X
s2
(<X> ( <X> • )
X
s3
(
<X> ( ) •
)s5
<X> ( <X> ) •
)s4
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 goto s3s3s4s5
<S> <X> • $
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
![Page 381: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/381.jpg)
Oscar Bonilla 381 Universidad Galileo
Ejemplo de Construcción de Parse Table
<S> • <X> $<X> • ( <X> )<X> • ( )
s0
s1X
s2
(<X> ( <X> • )
X
s3
(
<X> ( ) •
)s5
<X> ( <X> ) •
)s4
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3s4s5
<S> <X> • $
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
![Page 382: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/382.jpg)
Oscar Bonilla 382 Universidad Galileo
Ejemplo de Construcción de Parse Table
<S> • <X> $<X> • ( <X> )<X> • ( )
s0
s1X
s2
(<X> ( <X> • )
X
s3
(
<X> ( ) •
)s5
<X> ( <X> ) •
)s4
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3s4s5
<S> <X> • $
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
![Page 383: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/383.jpg)
Oscar Bonilla 383 Universidad Galileo
Ejemplo de Construcción de Parse Table
<S> • <X> $<X> • ( <X> )<X> • ( )
s0
s1X
s2
(
X
s3
(
<X> ( ) •
)s5
<X> ( <X> ) •
)s4
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 shift to s4 s4 s5
<S> <X> • $
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( ) <X> ( <X> • )
![Page 384: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/384.jpg)
Oscar Bonilla 384 Universidad Galileo
Ejemplo de Construcción de Parse Table
<S> • <X> $<X> • ( <X> )<X> • ( )
s0
s1X
s2
(
X
s3
(
<X> ( ) •
)s5
<X> ( <X> ) •
)s4
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 s5
<S> <X> • $
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( ) <X> ( <X> • )
![Page 385: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/385.jpg)
Oscar Bonilla 385 Universidad Galileo
Ejemplo de Construcción de Parse Table
<S> • <X> $<X> • ( <X> )<X> • ( )
s0
s1X
s2
(
X
s3
(
<X> ( ) •
)s5
<X> ( <X> ) •
)s4
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 s5
<S> <X> • $
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( ) <X> ( <X> • )
![Page 386: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/386.jpg)
Oscar Bonilla 386 Universidad Galileo
Ejemplo de Construcción de Parse Table
<S> • <X> $<X> • ( <X> )<X> • ( )
s0
s1X
s2
(
X
s3
(
<X> ( ) •
)s5
<X> ( <X> ) •
)s4
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5
<S> <X> • $
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( ) <X> ( <X> • )
![Page 387: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/387.jpg)
Oscar Bonilla 387 Universidad Galileo
Ejemplo de Construcción de Parse Table
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(<X> ( <X> • )
X
s3
(
<X> ( ) •
)s5
<X> ( <X> ) •
)s4
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5
![Page 388: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/388.jpg)
Oscar Bonilla 388 Universidad Galileo
Ejemplo de Construcción de Parse Table
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(<X> ( <X> • )
X
s3
(
<X> ( ) •
)s5
<X> ( <X> ) •
)s4
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
![Page 389: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/389.jpg)
Oscar Bonilla 389 Universidad Galileo
Ejemplo de Construcción de Parse Table
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(<X> ( <X> • )
X
s3
(
<X> ( ) •
)s5
<X> ( <X> ) •
)s4
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
![Page 390: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/390.jpg)
Oscar Bonilla 390 Universidad Galileo
Construcción de un Parse Engine LR(0)
• Agregamos la producción especial S’ S $
• Encontramos los ítems de la CFG
• Creamos el DFA– Usando las funciones closure y goto
• Construimos la tabla de parseo
LR(0)ParserEngine
![Page 391: Parsers](https://reader031.fdocuments.mx/reader031/viewer/2022013004/56815069550346895dbe6787/html5/thumbnails/391.jpg)
Oscar Bonilla 391 Universidad Galileo
Lecturas
• El Tigre– Secciones 3.0 a 3.3
• El Dragón– Secciones 4.2 a 4.5 y 4.7