20
ICS611 Introduction to Compilers Set 1

ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine

Embed Size (px)

Citation preview

Page 1: ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine

ICS611

Introduction to Compilers

Set 1

Page 2: ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine

What is a Compiler?

• A compiler is software (a program) that translates a high-level programming language to machine language

• A simple representation would be:Source Code ----> Compiler -----> Machine Language (Object File)

• It's not as simple as an assembler translator. • A compiler has to perform several steps to produce

an object file in machine code form.

Page 3: ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine

Analysis of the source code

• Lexical Analysis using a program called a SCANNER

• Syntax Analysis using a program called a PARSER

• Semantic Analysis using CODE GENERATION routines

Page 4: ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine

Lexical Analysis

• Scan the input source code to identify tokens of the programming language. 

• Tokens are basic units (keywords, identifier names, etc.) from which the source code is constructed

The program to do this is called a SCANNER 

Page 5: ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine

Syntax Analysis

• Group the tokens identified by the scanner into grammatical phrases that will be used by the compiler to generate the output code. 

This process is called parsing and is performed by a parser.

Page 6: ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine

Synthesis of the target program

• Generate an intermediate representation of the source program

• Code Optimization

• Code Generation

Page 7: ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine

Generate an intermediate representation

• This is performed by some compilers, but not necessarily all.

 

It should be easy to produce and easy to translate into the target program.

Page 8: ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine

Code Optimization

• Improve the intermediate code to produce a faster running machine code in the final translation.  

• Not all compilers include the code optimization

step, which can require a lot of time.

Page 9: ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine

Linking the programs produced by the compiler

• The linker program links the object files from the program modules, and any additional library files to create the executable program. 

• This requires the use of relocatable addresses

within the program to allow the program to run in different memory locations.

Page 10: ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine

Grammar 1

1.  SENTENCE -> NOUNPHRASE VERB NOUNPHRASE2.  NOUNPHRASE -> the ADJECTIVE NOUN3.  NOUNPHRASE -> the NOUN4.  VERB -> pushed5.  VERB -> helped6.  ADJECTIVE -> pretty7.  ADJECTIVE -> poor8.  NOUN -> man9.  NOUN -> boy10. NOUN -> cat

Page 11: ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine

Grammar 1 is an example of a context-free grammar (the only kind we will deal with).

The grammar consist of 10 productions e.g. production 3 is

nounphrase -> the noun

Here “nounphrase” is referred to as the lefthand side (lhs) and “the noun” is referred to as the righthand side (rhs)

Page 12: ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine

The set of all lhs’s constitutes the set of nonterminals of the grammar.

In this case they are: {SENTENCE, NOUNPHRASE,VERB, ADJECTIVE, NOUN}

All the other symbols occurring in the grammar (i.e. in some rhs, but never as any lhs) are the terminals of the grammar.

In this case {the,pushed,helped,pretty,poor,…}

Page 13: ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine

The lhs of the first production is called the goal symbol, in this case “sentence”.

A derivation of a string in the grammar is a list of strings starting with the goal symbol, in which each string, except the first, is obtained from the preceding one by applying a substitution of one of its symbols using one of the productions as a substitution rule

Page 14: ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine

A string which has a derivation is said to be derivable.

Derivable strings that consist entirely of terminal symbols are called sentences of the grammar. E.g.

the man helped the poor boy

is a sentence of Grammar 1.

The set of all sentences of a grammar is called the language defined by the grammar

Page 15: ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine

Grammar 1 (Cont.1)

Derivation of the sentence:  "the man helped the poor boy“

1.      SENTENCE                       (goal symbol)  2. ==>  NOUNPHRASE VERB NOUNPHRASE     (by Rule 1) 3. ==>  the NOUN VERB NOUNPHRASE       (Rule 3)4. ==>  the man VERB NOUNPHRASE        (Rule 8)5. ==>  the man helped NOUNPHRASE6. ==>  the man helped the ADJECTIVE NOUN7. ==>  the man helped the poor NOUN8. ==>  the man helped the poor boy

(this derivation shows that "the man helped the poor boy“ is a sentence in the language defined by the grammar.)

Page 16: ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine

Grammar 1 (Cont.2)

This derivation may also be represented diagrammatically by a syntax tree:

       

Page 17: ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine

Typical format of a grammar for a programming language

PROGRAM -> PROGRAM STATEMENTPROGRAM -> STATEMENTSTATEMENT -> ASSIGNMENT-STATEMENTSTATEMENT -> IF-STATEMENTSTATEMENT -> DO-STATEMENT...ASSIGNMENT-STATEMENT -> ......IF-STATEMENT -> ......DO-STATEMENT -> ......

Page 18: ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine

Grammar 2

A simple grammar for arithmetic statements

1.    E -> E + T

2.    E -> T

3.    T -> T * a

4.    T -> a

Page 19: ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine

Grammar 2 (Cont.1)

Derivation of:  a + a * a

1.        E Goal Symbol2. ==>    E + T Rule 13. ==>    E + T * a Rule 34. ==>    E + a * a Rule 45. ==>    T + a * a Rule 26. ==>    a + a * a Rule 4

Page 20: ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine

Grammar 2 (Cont.2)

Derivation of: a + a * a written in reverse:

1.   a + a * a Given sentential form2.   T + a * a Rule 4 in reverse3.   E + a * a Rule 2 in reverse4.   E + T * a Rule 45.   E + T Rule 3 in reverse6.   E Rule 1