Вы находитесь на странице: 1из 12

#define L 10

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

Error
Messages

Compiler

0010011000110001
1000111011001010
1101010010101001
1010101010101111
1010100111111111
0101010101010101

Languages & Compilers


From source code to executable
code

2
Marco Maggini

The compiler

Language Processing
Technologies

3
Marco Maggini

Language Processing
Technologies

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

4
Marco Maggini

Parsing and generation

=
X = A+B*C

+
*

A
B

Language Processing
Technologies

5
Marco Maggini

Language Processing
Technologies

The compiler context


Source code skeleton

preprocessor
source program

assembly code

assembler
object code
libreries

linker/loader
executable code

include, define (macro)

6
Marco Maggini

Language Processing
Technologies

Compiler structure
lexical analysis
syntactic analysis
semantical analysis

symbol table

intermediate code
generation
optimization
object code
generation

error handling

7
Marco Maggini

Lexical analysi (scanning)

int somma, diff = 0.3;

Language Processing
Technologies

8
Marco Maggini

Language Processing
Technologies

Syntatic Analyis (parsing)

assignment
statement
identifier

expression

expression
identifier

expression

+
expression
identifier

expression
number

60

9
Marco Maggini

Language Processing
Technologies

Syntatic rules

base rules

recursive rules

10
Marco Maggini

Language Processing
Technologies

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..)
*
int D;
float C;

C
D

type conversion function


int -> float

11
Marco Maggini

Language Processing
Technologies

Symbol table
It memorizes the identifiers and their associated attributes

memory allocation
type
visibility scope
number and type of function/procedure arguments

memory allocation

12
Marco Maggini

Language Processing
Technologies

Object code generation


=

internal intermediate
representation

id1

+
id2

*
id3

int-to-float
60

intermediate code
generation
code optimization

object code
generation

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
.............

Вам также может понравиться