Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
ERRORES
DISEÑO Y CONSTRUCCIÓN DE
COMPILADORES
LICENCIATURA EN CIENCIAS DE LA
COMPUTACIÓN
Profesora Responsable: Dra. Victoria Aragón
Jefe de Trabajos Prácticos: Dr. Edgardo Ferretti
Auxiliar de Práctico: Lic. María José Garciarena Ucelay
https://sites.google.com/site/ddcompilersunsl/
Lexicográficos
DCC- UNSL
Sintácticos
Semánticos
De ejecución
De lógica
Ante la presencia de Errores
Causas
DCC- UNSL
1. No poseer conocimiento del lenguaje
usar construcciones inválidas
2. No aplicar reglas del lenguaje
no declarar identicadores antes su uso
3. Escribir incorrectamente símbolos del lenguaje
lexicográficos scanner
Estrategias de Recuperación:
Pánico
Antipánico 3
ESTRATEGIAS DE RECUPERACIÓN:
Pánico: ANTE LA PRESENCIA DE UN ERROR SE SALTA SOBRE
SÍMBOLOS DEL TEXTO FUENTE HASTA QUE APAREZCA UN
SÍMBOLO ADECUADO QUE PERTENEZCA A UN CONJUNTO DE
TOKENS (sincronización) DE RECUPERACIÓN.
VENTAJA: FACIL IMPLEMENTACION.
DCC- UNSL
DESVENTAJA: PUEDEN QUEDAR SECUENCIAS LARGAS DEL
PROGRAMA SIN ANALIZAR.
Antipánico: LA IDEA ES CONTINUAR SALTANDO SOBRE
SÍMBOLOS DE LA ENTRADA, ANTE LA PRESENCIA DE UN ERROR,
PERO TRATANDO DE PERDER LA MENOR CANTIDAD POSIBLE DE
SÍMBOLOS DEL TEXTO FUENTE.
EJEMPLO:
A = A 3 + B ; pánico → salta hasta el primer fin de sentencia, es decir “ ;”
4
error antipánico → Avanza hasta el 3
ESTRATEGIA DE RECUPERACIÓN: PÁNICO Y ANTIPÁNICO
CARACTERÍSTICAS DESEABLES DEL LENGUAJE DE PROGRAMACIÓN
PALABRAS CLAVES.
DCC- UNSL
If <eb> then <ss> fi ;
Símbolos de
sincronización
If <eb> then <ss> fi ;
antipánico
DCC- UNSL
folset = {conjunto de tokens que pueden seguir a esa invocación del procedimiento A}.
DCC- UNSL
cjto1 = { símbolos válidos que se espera en la entrada }.
cjto2 = { posibles símbolos de reconfiguración }.
n = número de error, que identifica el tipo de error.
DCC- UNSL
Procedure X(folset:cjto de sbolos);
begin
test (c1,c2, m);//la 1° sentencia no es una llamada a procedimiento
match(if )
......
end {X}
DCC- UNSL
X .........a
Procedure X(folset:cjto de sbolos);
begin
.....
match(asymbol)
test (c1,c2, m)
end {X}
c1 = folset
Suponga G:
S → AuB | CaA S → AuB | CaA
DCC- UNSL
A → b | dd A → b | dd
B → AnA B → AnA
C → kk | C → kk |
No Test Test
terminal inicial final
S x
A
B x x
10
C x
EJEMPLO: CONSTRUCCIÓN DEL PDR CON LA INCLUSIÓN
DEL ESQUEMA DE RECUPERACIÓN ANTIPÁNICO
DCC- UNSL
4. <sent> ::= ID := <expr> ;
READ ( <idlist>) ;
WRITE ( <listexpr> ) ;
5. <idlist> ::= ID <listidtail>
6. <listidtail> ::= , ID <listidtail>
7. <listexpr> ::= <expr> <listexprtail>
8. <listexprtail> ::= , <expr> <listexprtail>
9. <expr> ::= <factor> <exprtail>
10. <exprtail> ::= <op> <factor> <exprtail>
11. <factor> ::= ( <expr> ) ID CTEENT
12. <op> ::= + -
11
1º PASO: CALCULAR LOS CONJUNTOS
FIRST1 Y FOLLOW1
DCC- UNSL
<listsenttail> ID READ WRITE, END
<sent> ID, READ, WRITE ID READ WRITE END
<idlist> ID )
<listidtail> , )
<listexpr> ( ID CTEENT )
<listexprtail> , )
<expr> ( ID CTEENT ;,)
<exprtail> + - ;,)
<factor> ( ID CTEENT +-;,)
<op> + - ( ID CTEENT
12
2° PASO:
DETERMINAR QUE PROCESOS LLEVAN INVOCACIONES A
TEST Y DÓNDE
Test Test
inicial final
<programa>::= BEGIN <listsent> END
DCC- UNSL
<listsent>::= <sent> <listsenttail>
< listsenttail >::= <sent><listsenttail>
<sent>::= ID := <expr> ; |READ ( <idlist>) ;
| WRITE ( <listexpr> );
<listexpr>::= <expr> <listexprtail>
<listexprtail>::= , <expr> <listexprtail>
<expr>::= <factor> <exprtail>
<exprtail>::= <op> <factor> <exprtail>
<factor> ::= ( <expr> ) ID CTEENT
<op>::= + -
13
<idlist> ::= ID <listidtail>
<listidtail>::= , ID <listidtail>
2° PASO:
DETERMINAR QUE PROCESOS LLEVAN INVOCACIONES A
TEST Y DÓNDE
Test Test
inicial final
<programa>::= BEGIN <listsent> END
DCC- UNSL
<listsent>::= <sent> <listsenttail> X X
< listsenttail >::= <sent><listsenttail> X
<sent>::= ID := <expr> ; |READ ( <idlist>) ;
| WRITE ( <listexpr> );
<listexpr>::= <expr> <listexprtail> X X
<listexprtail>::= , <expr> <listexprtail> X
<expr>::= <factor> <exprtail> X X
<exprtail>::= <op> <factor> <exprtail> X
<factor> ::= ( <expr> ) ID CTEENT
<op>::= + - X
14
<idlist> ::= ID <listidtail> X
<listidtail>::= , ID <listidtail> X
3° PASO:
ESCRIBIR EL PARSER DESCENDENTE RECURSIVO CON
EL ESQUEMA DE RECUPERACION DE ERRORES
PROGRAM parser;
DCC- UNSL
{
//inicializaciones para el scanner, si fuere necesario
lookahead := get_next_token
programa([eof]);
match(eof);
}
15
PROCEDIMIENTO PARA <PROGRAMA> ::= BEGIN <LISTSENT> END
DCC- UNSL
procedure programa(folset: conjunto de símbolos);
{
test([BEGIN], [ID READ WRITE] +[END] +folset, n1);
match(BEGIN);
listsent(folset + [END]);
match(END);
test(folset,[], n2);
}(*procedure programa*)
16
PROCEDIMIENTO PARA <PROGRAMA> ::= BEGIN <LISTSENT> END
Puntos de
procedure programa(folset: conjunto de símbolos); reconfiguración
de la estrategia
{ Puntos de
pánico
reconfiguración
DCC- UNSL
de la estrategia
antipánico
17
Ejemplo: BEGIN READ(g); END END eof
PROCEDIMIENTO PARA <LISTSENT>::= <SENT> <LISTSENTTAIL>
procedure listsent ( folset: conjunto de símbolos);
{
sent (folset + [ ID READ WRITE] );
listsenttail ( folset );
} (*listsent*)
DCC- UNSL
PROCEDIMIENTO PARA <LISTSENTTAIL ::= <SENT> <LISTSENTTAIL>
DCC- UNSL
}
READ: { lookahead := get_next_token;
match (();
idlist ( folset + [ ) ;] );
match ());
match(;);
}
WRITE: { lookahead := get_next_token;
match (();
listexpr( folset + [ ) ; ] );
match ());
match(;);
}
default: error(....);
19
endcase
test (folset ,[], n6); ¿qué pasa con los ()?
} (*sent*)
PROCEDIMIENTOS PARA
<IDLIST> ::= ID <LISTIDTAIL>
<LISTIDTAIL> ::= , ID <LISTIDTAIL>
DCC- UNSL
match(ID);
listidtalil ( folset );
} (*idlist*)
DCC- UNSL
listexprtail ( folset );
} (*listexpr*)
21
PROCEDIMIENTOS PARA
<EXPR> ::= <FACTOR> <EXPRTAIL>
<EXPRTAIL> ::= <OP> <FACTOR> <EXPRTAIL>
DCC- UNSL
exprtail ( folset );
} (*expr*)
DCC- UNSL
CTEENT : lookahead := get_next_symbol;
endcase
test ( folset, [ ], n12)
}(*factor*)
DCC- UNSL
{
sent (folset + [ ID READ WRITE] );
listsenttail ( folset );
} end; (*listsent*)
DCC- UNSL
procedure listsent ( folset: conjunto de símbolos);
{
sent (folset + [ID READ WRITE] );
while lookahead in first( sent)
{
sent (folset + [ ID READ WRITE] );
test( first(sent) + folset, [], n);
}
} (*listsent*)
25
SI SE UNIFICAN LAS DOS PRODUCCIONES SE OBTIENE
DCC- UNSL
<listsent> <sent> { <sent> }*
DCC- UNSL
{ test ( first(idlist), folset + [,], n1 );
match(ID);
listidtail ( folset );
}end; (*idlist*)
DCC- UNSL
test ( first(idlist), folset + [,], n1 );
match(ID);
while lookahead in [, ID]
{
match(,);
match(ID);
test ([,] + folset, [ID], n2);
}
}
28
SI SE UNIFICAN LAS DOS PRODUCCIONES SE OBTIENE
<idlist> ID { , ID }*
DCC- UNSL
{ test ( first(idlist), folset + [,], n1 );
test ( first(idlist), folset + [,], n1 ); match(ID);
listidtail ( folset );
match(ID); }end; (*idlist*)
while lookahead in [, ID] procedure listidtail ( folset: conjunto de símbolos);
{ test (first(listidtail) + folset , [ID], n2);
{ if lookahead in [, ID] then
match(,); {match(,);
match (ID);
match(ID); }
listidtail ( folset );
}
}
Se debe colocar un test como última sentencia del cuerpo de la
iteración para que se obtenga el mismo comportamiento que se
tenía anteriormente con las otras dos producciones. Se mantiene
el test del inicio. 29