Compiler construction in4020 – course 2001/2002

40
Compiler construction in4020 – course 2001/2002 Koen Langendoen Delft University of Technology The Netherlands

description

Compiler construction in4020 – course 2001/2002. Koen Langendoen Delft University of Technology The Netherlands. Goals. understand the structure of a compiler understand how the components operate understand the tools involved scanner generator, parser generator, etc. understanding means - PowerPoint PPT Presentation

Transcript of Compiler construction in4020 – course 2001/2002

Page 1: Compiler construction in4020 – course 2001/2002

Compiler constructionin4020 – course 2001/2002

Koen Langendoen

Delft University of TechnologyThe Netherlands

Page 2: Compiler construction in4020 – course 2001/2002

Goals

• understand the structure of a compiler

• understand how the components operate

• understand the tools involved• scanner generator, parser generator, etc.

• understanding means• [theory] be able to read source code

• [practice] be able to adapt/write source code

Page 3: Compiler construction in4020 – course 2001/2002

Format:“werkcollege” + practicum

• 14 x 2 hours of interactive lectures 1 sp• book “Modern Compiler Design”• schedule: see blackboard• handouts: see blackboard

• assignment 2 sp• groups of 2 students• modify reference compiler

• oral exam 1 sp

Page 4: Compiler construction in4020 – course 2001/2002

Homework

• find a partner for the “practicum”

• register your group• send e-mail to [email protected]

Page 5: Compiler construction in4020 – course 2001/2002

What is a compiler?

program

in some

source

language

executable

code for

target

machine

compiler

Page 6: Compiler construction in4020 – course 2001/2002

What is a compiler?

program

in some

source

language

executable

code for

target

machine

front-endanalysis

semanticrepresen-

tation

back-endsynthesis

compiler

Page 7: Compiler construction in4020 – course 2001/2002

Why study compilerconstruction?

• curiosity

• better understanding of programming language concepts

• wide applicability• transforming “data” is very common

• many useful data structures and algorithms

• practical application of “theory”

Page 8: Compiler construction in4020 – course 2001/2002

Overviewlecture 1

• [introduction]

• compiler structure• exercise

----------------- 15 min. break ----------------------

• lexical analysis• excercise

Page 9: Compiler construction in4020 – course 2001/2002

Compiler structure

• L+M modules = LxM compilers

program

in some

source

language

front-endanalysis

semanticrepresen-

tation

executable

code for

target

machine

back-endsynthesis

compiler

program

in some

source

language

front-endanalysis

executable

code for

target

machine

back-endsynthesis

executable

code for

target

machine

back-endsynthesis

Page 10: Compiler construction in4020 – course 2001/2002

Limitations of modular approach

• performance• generic vs specific

• loss of information

• variations must be small• same programming paradigm

• similar processor architecture

program

in some

source

language

front-endanalysis

semanticrepresen-

tation

executable

code for

target

machine

back-endsynthesis

compiler

program

in some

source

language

front-endanalysis

executable

code for

target

machine

back-endsynthesis

executable

code for

target

machine

back-endsynthesis

Page 11: Compiler construction in4020 – course 2001/2002

Semantic representation

• heart of the compiler

• intermediate code• linked lists of pseudo instructions

• abstract syntax tree (AST)

program

in some

source

language

executable

code for

target

machine

front-endanalysis

semanticrepresen-

tation

back-endsynthesis

compiler

Page 12: Compiler construction in4020 – course 2001/2002

AST example

• expression grammar

expression expression ‘+’ term | expression ‘-’ term | term

term term ‘*’ factor | term ‘/’ factor | factorfactor identifier | constant | ‘(‘ expression ‘)’

• example expression

b*b – 4*a*c

Page 13: Compiler construction in4020 – course 2001/2002

parse tree: b*b – 4*a*c

‘b’

identifier

expression

term

factor

term

‘b’

factor

identifier

‘*’

‘4’

constant

term

factor

term

‘a’

factor

identifier

‘*’

term

factor‘*’

‘c’

identifier

expression

‘-’

Page 14: Compiler construction in4020 – course 2001/2002

AST: b*b – 4*a*c

‘*’

‘c’

‘-’

‘b’

‘4’

‘*’

‘a’

‘*’

‘b’

Page 15: Compiler construction in4020 – course 2001/2002

annotated AST: b*b – 4*a*c

• identifier

• constant

• term

• expression

‘*’

‘c’

‘-’

‘b’

‘4’

‘*’

type: real

loc: reg1

type: real

loc: reg2

type: real

loc: const

type: real

loc: sp+24

type: real

loc: reg2

‘a’

type: real

loc: sp+8

‘*’

type: real

loc: reg1

type: real

loc: sp+16 ‘b’

type: real

loc: sp+16

Page 16: Compiler construction in4020 – course 2001/2002

AST exercise (5 min.)

• expression grammar

expression expression ‘+’ term | expression ‘-’ term | term

term term ‘*’ factor | term ‘/’ factor | factorfactor identifier | constant | ‘(‘ expression ‘)’

• example expression

b*b – (4*a*c)

• draw parse tree and AST

Page 17: Compiler construction in4020 – course 2001/2002

Answers

Page 18: Compiler construction in4020 – course 2001/2002

answerparse tree: b*b – 4*a*c

‘b’

identifier

expression

term

factor

term

‘b’

factor

identifier

‘*’

‘4’

constant

term

factor

term

‘a’

factor

identifier

‘*’

term

factor‘*’

‘c’

identifier

expression

‘-’

Page 19: Compiler construction in4020 – course 2001/2002

answerparse tree: b*b – (4*a*c)

‘b’

identifier

expression

term

factor

term

‘b’

factor

identifier

‘*’

term

expression

‘-’

expression

factor

‘(’ ‘)’

‘4*a*c’

Page 20: Compiler construction in4020 – course 2001/2002

Break

Page 21: Compiler construction in4020 – course 2001/2002

front-end: from program text to AST

program text

lexical analysis

syntax analysis

context handling

annotated AST

tokens

AST

front-end

Page 22: Compiler construction in4020 – course 2001/2002

front-end: from program text to AST

program text

lexical analysis

syntax analysis

context handling

annotated AST

tokens

AST

scanner

generator

token

description

parser

generator

language

grammar

Page 23: Compiler construction in4020 – course 2001/2002

Lexical analysis

• covert stream of characters to stream of tokens

• what is a token?• sequence of characters with a semantic notion, see

language definition

• rule of thumb: two characters belong to the same token if inserting white space changes the meaning.

digit = *ptr++ - ’0’;

digit = *ptr+ + - ’0’;

lex-i-cal: of or relating to words or the vocabulary of a language as distinguished from its grammar and construction

Webster’s Dictionary

Page 24: Compiler construction in4020 – course 2001/2002

Lexical analysis

• covert stream of characters to stream of tokens

• what is a token?• sequence of characters with a semantic notion, see

language definition

• rule of thumb: two characters belong to the same token if inserting white space changes the meaning.

digit = *ptr++ - ’0’;

digit = *ptr+ + - ’0’;

Page 25: Compiler construction in4020 – course 2001/2002

Tokens

• attributes• type

• lexeme

• value

• file position

• examples

typedef struct {

int class;

char *repr;

file_pos position;

} Token_Type;

type lexeme

IDENTIFIER foo, t3, ptr

NUMBER 15, 082, 666

REAL 1.2, .002, 1e6

IF if

Page 26: Compiler construction in4020 – course 2001/2002

Non-tokens

• white spacesspaces, tabs, newlines

• comments/* a C-style comment */

// a C++ comment

• preprocessor directives#include “lex.h”

#define is_digit(d) (’0’ <= (d) && (d) <= ’9’)

Page 27: Compiler construction in4020 – course 2001/2002

Regular expressions

Basic patterns Matchingx the character x. any character, usually except a newline[abcA-Z] any of the characters a,b,c and the range A-ZRepetition operatorsR? an R or nothing (= optionally an R)R* zero or more occurrences of RR+ one or more occurrences of RComposition operators

R1 R2 an R1 followed by an R2

R1 | R2 either an R1 or an R2

Grouping( R ) R itself

Page 28: Compiler construction in4020 – course 2001/2002

Examples ofregular expressions

• an integer is a sequence of digits:

[0-9]+

• an identifier is a sequence of letters and digits; the first character must be a letter:

[a-z][a-z0-9]*

Page 29: Compiler construction in4020 – course 2001/2002

Regular descriptions

• structuring regular expressions by introducing named sub expressions

letter [a-zA-Z]

digit [0-9]

letter_or_digit letter | digit

identifier letter letter_or_digit*

• define before use

Page 30: Compiler construction in4020 – course 2001/2002

Exercise (5 min.)

• write down regular descriptions for the following descriptions:

• an integral number is a non-zero sequence of digits optionally followed by a letter denoting the base class (b for binary and o for octal).

• a fixed-point number is an (optional) sequence of digits followed by a dot (’.’) followed by a sequence of digits.

• an identifier is a sequence of letters and digits; the first character must be a letter. The underscore _ counts as a letter, but may not be used as the first or last character.

Page 31: Compiler construction in4020 – course 2001/2002

Answers

Page 32: Compiler construction in4020 – course 2001/2002

Answers

base [bo]integral_number digit+ base?

dot \.fixed_point_number digit* dot digit+

letter [a-zA-Z]digit [0-9]underscore _letter_or_digit letter | digitletter_or_digit_or_und letter_or_digit | underscoreidentifier letter (letter_or_digit_or_und* letter_or_digit+)?

Page 33: Compiler construction in4020 – course 2001/2002

syntaxanalyzer

Lexical analysis

• covert stream of characters to stream of tokens

• tokens are defined by a regular description

• tokens are demanded one-by-one by the syntax analyzer

program

textAST

lexicalanalyzer tokens

get_next_token()

Page 34: Compiler construction in4020 – course 2001/2002

interface

extern Token_Type Token;

/* Global variable that holds the current token.

*/

void start_lex(void);

/* Must be called before the first call to

* get_next_token().

*/

void get_next_token(void);

/* Load the next token into the global

* variable Token.

*/

Page 35: Compiler construction in4020 – course 2001/2002

lexical analysis by hand

• read complete program text into memory for simplicity• avoids buffering and arbitrary limits

• variable length tokens

• get_next_token() dispatches on the next character

input:

dot

main() { printf( ”hello world\n”);}

Page 36: Compiler construction in4020 – course 2001/2002

void get_next_token(void) {

int start_dot;

skip_layout_and_comment();

/* now we are at the start of a token or at end-of-file, so: */

note_token_position();

/* split on first character of the token */

start_dot = dot;

if (is_end_of_input(input_char)) {

Token.class = EoF; Token.repr = "<EoF>"; return;

}

if (is_letter(input_char)) {recognize_identifier();}

else

if (is_digit(input_char)) {recognize_integer();}

else

if (is_operator(input_char) || is_separator(input_char)) {

Token.class = input_char; next_char();

}

else {Token.class = ERRONEOUS; next_char();}

Token.repr = input_to_zstring(start_dot, dot-start_dot);

}

Page 37: Compiler construction in4020 – course 2001/2002

#define is_end_of_input(ch) ((ch) == '\0')

#define is_layout(ch) (!is_end_of_input(ch) && (ch) <= ' ')

#define is_uc_letter(ch) ('A' <= (ch) && (ch) <= 'Z')

#define is_lc_letter(ch) ('a' <= (ch) && (ch) <= 'z')

#define is_letter(ch) (is_uc_letter(ch) || is_lc_letter(ch))

#define is_digit(ch) ('0' <= (ch) && (ch) <= '9')

#define is_letter_or_digit(ch) (is_letter(ch) || is_digit(ch))

#define is_underscore(ch) ((ch) == '_')

#define is_operator(ch) (strchr("+-*/", (ch)) != NULL)

#define is_separator(ch) (strchr(";,(){}", (ch)) != NULL)

void recognize_integer(void) {

Token.class = INTEGER; next_char();

while (is_digit(input_char)) {next_char();}

}

Character classification & token recognition

Page 38: Compiler construction in4020 – course 2001/2002

Summary

• compiler is a structured toolbox• front-end: program text annotated AST• back-end: annotated AST executable code

• lexical analysis: program text tokens• token specifications• implementation by hand

• exercises• AST• regular descriptions

Page 39: Compiler construction in4020 – course 2001/2002

Next week

• Generating a lexical analyzer• generic methods

• specific tool lex

program text

lexical analysis

syntax analysis

context handling

annotated AST

tokens

AST

scanner

generator

token

description

Page 40: Compiler construction in4020 – course 2001/2002

Homework

• find a partner for the “practicum”

• register your group• send e-mail to [email protected]

• print handout lecture 2 [blackboard]