01 - languages and compilers

12
Languages & Compilers From source code to executable code #define L 10 main() { int i,k,r[L]; for(i=0;i<L;i++) { k = 3*i+2; r[i] = k*k; } } 0010011000110001 1000111011001010 1101010010101001 1010101010101111 1010100111111111 0101010101010101 Compiler Error Messages

Upload: alepacheko

Post on 24-Dec-2015

220 views

Category:

Documents


0 download

DESCRIPTION

Compiladores

TRANSCRIPT

Page 1: 01 - Languages and Compilers

Languages & Compilers From source code to executable code

#define L 10

main() { int i,k,r[L]; for(i=0;i<L;i++) { k = 3*i+2; r[i] = k*k; } }

0010011000110001 1000111011001010 1101010010101001 1010101010101111 1010100111111111 0101010101010101

Compiler

Error Messages

Page 2: 01 - Languages and Compilers

The compiler

Language Processing Technologies

Marco Maggini

2

Page 3: 01 - Languages and Compilers

Source and object languages

•  There are... hundreds of programming languages ▫  General purpose programming languages

  C, C++, Pascal, Fortran, Java, Basic, Lisp, Prolog, perl.... ▫  Special purpose languages

  Text formatting (Tex, Latex...)   Database management and querying (SQL)

•  The compiler translates the source language into ▫  Another high-level programming language ▫  e.g. pascal -> C ▫  The machine code for a given processor/architecture

Language Processing Technologies

Marco Maggini

3

Page 4: 01 - Languages and Compilers

Parsing and generation

Language Processing Technologies

Marco Maggini

4

X = A+B*C X *

+ =

C B A

Page 5: 01 - Languages and Compilers

The compiler “context”

Language Processing Technologies

Marco Maggini

5

preprocessor

assembler

linker/loader

Source code skeleton

source program

assembly code

object code

executable code

libreries

include, define (macro)

Page 6: 01 - Languages and Compilers

Compiler structure

Language Processing Technologies

Marco Maggini

6

lexical analysis

syntactic analysis

semantical analysis

intermediate code generation

optimization

object code generation

error handling symbol table

Page 7: 01 - Languages and Compilers

Lexical analysi (scanning)

Language Processing Technologies

Marco Maggini

7

int somma, diff = 0.3;

Page 8: 01 - Languages and Compilers

Syntatic Analyis (parsing)

Language Processing Technologies

Marco Maggini

8

assignment statement

identifier expression

expression

expression identifier

identifier number

expression

expression A

=

+

*

B C 60

Page 9: 01 - Languages and Compilers

Syntatic rules

Language Processing Technologies

Marco Maggini

9

base rules

recursive rules

Page 10: 01 - Languages and Compilers

Sematical analysis •  Yields the sematics associated to the syntatic structure ▫  It verifies that the usage rules of the language are satisfied

  identifier declarations (e.g. duplicate definitions,...)   type check (compatibility of types in expressions, automatic

type conversion, type check for vector indexes, ecc..)

Language Processing Technologies

Marco Maggini

10

*

C

D

int D; float C;

type conversion function int -> float

Page 11: 01 - Languages and Compilers

Symbol table •  It memorizes the identifiers and their associated attributes ▫  memory allocation ▫  type ▫  visibility scope ▫  number and type of function/procedure arguments

Language Processing Technologies

Marco Maggini

11

memory allocation

Page 12: 01 - Languages and Compilers

Object code generation

Language Processing Technologies

Marco Maggini

12

= id1

id2 id3

+ * int-to-float

60

t1 = int-to-float(60) t2 = id3 * t1 t3 = id2 + t2 id1 = t3

t1 = id3 * 60.0 id1 = id2 + t1

MOVF id3, R2 MULF #60.0, R2 .............

internal intermediate representation

intermediate code generation

code optimization

object code generation