Lectura 4: Compilación e Interpretación B. Universidad Autónoma de Puebla Algoritmos y Programación Primavera de 2006 Lectura 4: Compilación e Interpretación Javier M. Hernández FCFM
Del Código Fuente al Código Ejecutable program gcd(input, output); var i, j: integer; begin read(i, j); while i <> j do if i > j then i := i – j; else j := j – i; writeln(i) end. Compilación Compiler translate high-level source program intro an equivalent target program
Compilando un Programa Un programa llamado un “compilador” se usa para traslada su “programa fuente” (en un lenguaje como C++) hacia un“programa objeto” (en el lenguaje de máquina de su sistema). ANALISIS LEXICO Divide el programa fuente en palabras como “void”, “x”, “>”, y “;”. PARSING Analize la sintaxis gramatical del programa fuente (e.g., “if (x > y)” tiene sentido, pero “if (x > ) y” no). GENERACION DEL CODIGO Generate un programa equivalente en lenguaje de máquina. #include <iostream.h> void main() { int x, y; cout << “Enter two integers: ”; cin >> x >> y; if (x > y) cout << x << “ is the largest!”; else cout << y << “ is the largest!”; return; } 110101000101100011000010010110110100010101011110010101011100000010011100101011001110101010111001010100101010101000000110110111011101010100111110101010101001001001010000010101010101000000101111100101100001011101010101010100010101111110010100100100101000 Programa Fuente Programa Objeto COMPILACION
Compilación e Interpretación Un compilador es un programa que translada programas fuente de alto nivel en programas blanco Un interprete es un programa que ejecuta otro programa Interpretation leads to greater flexibility and better diagnostics. Late binding in language that can create new pieces of code during execution and execute them Compilation leads to better performance.
Mezclando Compilación e Interpretación Diferencia difusa: Un lenguaje es interpretado cuando la traducción inicial es simple Un lenguaje es compilado cuando el proceso de traduccion es complicado Java as an example
Preprocesamiento Macros #define <macro> <replacement name> #define FALSE 0 #define max(A,B) ( (A) > (B) ? (A):(B)) Token substitution: use a C example on the board Linking is ubiquitous
Linking Bibliotecas de subrutinas Java API
Linking y carga CODIGO EJECUTABLE Programa unido Programas Biblioteca Después de ser compilado, el programa objeto debe ser “linked (unido)” (i.e., conectado a otro código compilado de las bibliotecas, como funciones matemáticas u operadores input/output) y entonces “loaded (cargado)” en la memoria principal para su ejecución. Programa fuente Programa objeto Programa unido COMPILAR CODIGO EJECUTABLE LINK Programas Biblioteca compiladas CARGAR
Portabilidad Lenguaje en lugar de lenguaje de máquina Código fuente intermedio
Ambiente de Programación Mucho mas que compiladores e interpretes Assemblers, debuggers, preprocesadores y linkers Editores Impresion embellecida Verificación de estilo Manejo de versiones Ambientes integrados Mas allá de un simple bus error Emacs ? Look for tools you may need
Vistazo de la Compilación program gcd(input, output); var i, j: integer; begin read(i, j); while i <> j do if i > j then i := i – j; else j := j – i; writeln(i) end. Compilación
Fases de la Compilación
Ejemplo Lenguaje de calculadora de mesa Programa de ejemplo: read A read B sum := A + B write sum write sum / 2 Explain the notation
Análisis Léxico Tokens: literal = digit digit * id = letter ( letter | digit ) * [ except "read" and "write" ] literal = digit digit * ":=", "+", "-", "*", "/", "(", ")“ $$$ [end of file]
Análisis Sintáctico Gramática en EBNF <pgm> -> <statement list> $$$ <stmt list> -> <stmt list> <stmt> | E <stmt> -> id := <expr> | read <id> | write <expr> <expr> -> <term> | <expr> <add op> <term> <term> -> <factor | <term> <mult op> <factor <factor> -> ( <expr> ) | id | literal <add op> -> + | - <mult op> -> * | / Precedence
Generación de código Código Intermedio: read pop A pop B push A push B add pop sum push sum write push 2 div
Generación de código Código Blanco: .data A: .long 0 B: .long 0 sum: .long 0 .text main: jsr read movl d0,d1 movl d1,A jsr read movl d1,B movl A,d1
Generación de código movl B,d2 addl d1,d2 movl d1,sum movl sum,d1 movl d1,d0 jsr write movl #2,d2 divsl d1,d2