Professional Documents
Culture Documents
Semestre I, 2016
1 / 59
Agenda
1
Informacin de Contacto
Introduccin
Estructura Lxica
Bibliografa
Informacin de Contacto
2 / 59
Informacin de Contacto
Email: juan.bekios@ucn.cl
Horario de Atencin: Martes, 18:00 - 19:30 hrs.
Oficina: Y1 - 309
http://www.disc.ucn.cl
Profesor Responsable
I
Informacin de Contacto
3 / 59
Email: juan.bekios@ucn.cl
Telfono: +55 2355162
Oficina: Y1-309
Informacin de Contacto
4 / 59
Agenda
1
Informacin de Contacto
Introduccin
Estructura Lxica
Bibliografa
Introduccin
5 / 59
Introduccin
Introduccin
6 / 59
Introduccin
I
I
I
I
BNF (original).
BNF extendido (EBNF).
Diagramas sintcticos.
Introduccin
7 / 59
Agenda
1
Informacin de Contacto
Introduccin
Estructura Lxica
Bibliografa
Estructura Lxica
8 / 59
Estructura Lxica
I
I
I
I
Estructura Lxica
9 / 59
Estructura Lxica
Determinacin de tokens.
I
I
I
Estructura Lxica
10 / 59
Expresiones Regulares
I
I
Estructura Lxica
11 / 59
Ejercicio
Estructura Lxica
12 / 59
Ejercicio
Solucin:
^[+-]?[1-9]\d*\$
Estructura Lxica
12 / 59
Ejercicio
Solucin:
^[+-]?[1-9]\d*\$
Estructura Lxica
12 / 59
Ejercicio
I
Estructura Lxica
13 / 59
Ejercicio
Solucin simple:
^\S+@\S+\.\S+\$
Estructura Lxica
13 / 59
Ejercicio
I
Solucin simple:
^\S+@\S+\.\S+\$
Estructura Lxica
13 / 59
Ejercicio
I
Solucin simple:
^\S+@\S+\.\S+\$
Estructura Lxica
13 / 59
Ejercicio
I
Solucin simple:
^\S+@\S+\.\S+\$
Estructura Lxica
13 / 59
I
I
; ... . (Lisp)
% ... . (Prolog)
# ... . (Python, Ruby, Perl)
//..., / ... / . (Java, Javascript)
Estructura Lxica
14 / 59
Palabras reservadas
I
Identificadores predefinidos.
I
I
I
Estructura Lxica
15 / 59
Estructura Lxica
16 / 59
Estructura Lxica
17 / 59
Tokens .
I
I
Estructura Lxica
18 / 59
Scanner en C
1
2
4
5
6
7
8
9
10
11
12
13
14
TokenType getToken(void)
{ while ((curr_char = getchar()) == ); /* skip
blanks */
if (isdigit(curr_char)) /* recognize a NUMBER
token */
{ numval = 0;
while (isdigit(curr_char))
{ /* compute numeric value */
numval = 10 * numval + curr_char - 0;
curr_char = getchar();
}
/* put back last character onto input */
ungetc(curr_char,stdin);
return NUMBER;
}
...
Juan Bekios Calfa
Estructura Lxica
19 / 59
Scanner en C
...
else /* recognize a special symbol */
{ switch (curr_char)
{ case (: return LPAREN; break;
case ): return RPAREN; break;
case +: return PLUS; break;
case *: return TIMES; break;
case \n: return EOL; break;
default: return ERROR; break;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
Estructura Lxica
20 / 59
Scanner en C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
main()
{ TokenType token;
do
{ token = getToken();
switch (token)
{ case PLUS: printf("PLUS\n"); break;
case TIMES: printf("TIMES\n"); break;
case LPAREN: printf("LPAREN\n"); break;
case RPAREN: printf("RPAREN\n"); break;
case EOL: printf("EOL\n"); break;
case NUMBER: printf("NUMBER: %d\n", numval);
break;
case ERROR: printf("ERROR: %c\n", curr_char);
break;
}
} while (token != EOL);
return 0;
}
Juan Bekios Calfa
Estructura Lxica
21 / 59
Scanner en C
Estructura Lxica
22 / 59
Ejemplo de scanners
I
Lectura adicional:
http://web.stanford.edu/class/archive/cs/cs143/cs143.1128/
handouts/050%20Flex%20In%20A%20Nutshell.pdf
Estructura Lxica
23 / 59
1
2
3
4
5
Estructura Lxica
24 / 59
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Estructura Lxica
25 / 59
Salida: lex.yy.c
1
2
Generar lexer:
Ejemplo:
$> ./mylexer <<< "hola1234mundo"
Saw an integer: 1234
Estructura Lxica
26 / 59
Agenda
1
Informacin de Contacto
Introduccin
Estructura Lxica
Bibliografa
27 / 59
28 / 59
Incorrecto:
S > a*b
Correcto:
S > Ab
A > Aa |
Incorrecto:
S > a(b|c*)
29 / 59
Incorrecto:
S > a*b
Correcto:
S > Ab
A > Aa |
Incorrecto:
S > a(b|c*)
Correcto:
S > aX
X > b | C
C > C c |
29 / 59
GLC: Ejemplo
30 / 59
31 / 59
Agenda
1
Informacin de Contacto
Introduccin
Estructura Lxica
Bibliografa
32 / 59
33 / 59
Las hojas son los terminales, los nodos interiores son los
no-terminales y el nodo raz es el smbolo inicial.
Dada la expresin:
34 / 59
35 / 59
36 / 59
37 / 59
Ejemplo de Ambigedad
I
38 / 59
Ejemplo de Ambigedad
I
39 / 59
40 / 59
41 / 59
Agenda
1
Informacin de Contacto
Introduccin
Estructura Lxica
Bibliografa
42 / 59
43 / 59
Diagrama Sintctico
I
I
I
I
Herramientas en lnea:
I
I
http://mdaines.github.io/grammophone/
http://www.bottlecaps.de/rr/ui
44 / 59
Ejercicio
I
45 / 59
Agenda
1
Informacin de Contacto
Introduccin
Estructura Lxica
Bibliografa
46 / 59
47 / 59
Bottom-up o Shift-reduce
I
Como funciona:
I
I
I
I
E > E + F | F
F > F T | T
T > int | (E)
Ejemplo: 4 + 3 5 + 1.
Ver Button-Up_Sample.pdf
Juan Bekios Calfa
48 / 59
Top-down
E > T + E | T
T > int | (E)
Ejemplo: 4 + 3.
Ver Top-Down_DFS_Sample.pdf
49 / 59
Descenso recursivo
50 / 59
Descenso recursivo
error()
getToken()
match(char c)
51 / 59
52 / 59
53 / 59
1
2
3
4
5
6
7
8
%{
Declarations
%}
Definitions
%%
Productions
%%
User subroutines
54 / 59
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
%{
#include "integer.tab.h" // including parser
%}
/* This tells flex to read only one input file */
%option noyywrap
%%
[+-]?[1-9][0-9]* {
yylval = atoi(yytext);
return INTEGER;
}
[a-zA-Z]+ {
yylval = 0;
yyerror("Unknown character");
}
.|\n { /* do nothing */}
%%
55 / 59
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
%{
#include <stdio.h>
void yyerror(char *);
%}
/* The %token direction defines the "terminal symbol" token types */
%token INTEGER
%%
exp:
exp INTEGER { printf("exp INTEGER: %d\n", $2); }
| INTEGER { printf("INTEGER: %d\n", $1); }
;
%%
void yyerror(char *s) {
fprintf(stderr, " %s\n", s);
}
int main(void) {
yyparse();
}
56 / 59
1
2
3
4
Generar el analizador:
Ejemplo:
$> ./a.out <<< "hola1234mundo"
Unknown character
INTEGER: 1234
Unknown character
Juan Bekios Calfa
57 / 59
Agenda
1
Informacin de Contacto
Introduccin
Estructura Lxica
Bibliografa
Bibliografa
58 / 59
Bibliografa
Bibliografa
59 / 59