01 - Languages and Compilers

Preview:

DESCRIPTION

Compiladores

Citation preview

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

The compiler

Language Processing Technologies

Marco Maggini

2

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

Parsing and generation

Language Processing Technologies

Marco Maggini

4

X = A+B*C X *

+ =

C B A

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)

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

Lexical analysi (scanning)

Language Processing Technologies

Marco Maggini

7

int somma, diff = 0.3;

Syntatic Analyis (parsing)

Language Processing Technologies

Marco Maggini

8

assignment statement

identifier expression

expression

expression identifier

identifier number

expression

expression A

=

+

*

B C 60

Syntatic rules

Language Processing Technologies

Marco Maggini

9

base rules

recursive rules

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

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

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