construction compilersmath.uni.lodz.pl/~robpleb/wyklad_1_ang.pdf · 12. lexical analysis lexical...

47
CONSTRUCTION COMPILERS LECTURE Robert Plebaniak

Upload: others

Post on 01-Aug-2020

19 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

CONSTRUCTION COMPILERS

LECTURE

Robert Plebaniak

Page 2: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

The software platform

LINUX (it may not contain LLgen, then the installation from the page http://tack.sourceforge.net);

WINDOWS (then we can use Cygwin, this program we can found http://www.cygwin.com);

2

Page 3: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation
Page 4: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation
Page 6: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

LEX and YACC GENERATORS

PCYACC - commercial version http://www.abxsoft.com

MKS LEX & YACC – commercial version http://www.mkssoftware.com

Bumble-Bee Parser Generator – commercial version http://www.bumblebeesoftware.com

6

Page 8: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation
Page 9: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

Compilers

Defintion 1.Compiler is a program whose purpose is to

convert code written in a programming language (C, Ada, Java), called the source language on the equivalent code in another language, called the language of the output.

9

Page 10: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

INTERPRETER

Definition 2.Interpreter – is a program which operates as a

compiler, except that it does not generate object code, but immediately follows the instructions contained in the source code of the program.

10

Page 11: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

Stages of the compiler

The source program; Analysis; The intermediate representation; Synthesis; The resulting program;

11

Page 12: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

STAGES OF COMPILATION

Analysis – at this stage, the compiler breaks down the source program into its constituent parts and then generates the intermediate representation.

Also at this stage, we have detect errors and possible informing them about the programmer.

12

Page 13: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation
Page 14: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

LEXICAL ANALYSIS

Lexical analysis is also called scanning or line analysis. The main task of lexical analysis is a grouping of characters in the input stream in lexical symbols otherwise known as token's.

14

Page 15: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

LEXICAL ANALYSIS

Tokens are different elementary component of the source language, eg .: number, keywords, operators and identifiers, parentheses, ...

15

Page 16: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

ANALIZA LEKSYKALNA

Lexical analysis is also called scanning or line analysis. The main task of lexical analysis is a grouping of characters in the input stream in lexical symbols otherwise known as token's.

16

Page 17: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

Exampe

There is a function on C++:

int name_fun (int s){

return s+s;}

17

Page 18: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

Example

Tokens are different elementary component of the source language, eg .: number, keywords, operators and identifiers, parentheses, ...

18

Page 19: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

Example

Leksems – they are strings that form lexical symbols;

Pattern – a "recipe" for the construction token: this may be a string underscores, small, large letters and digits;

Attributes – that is the lexical value, which sometimes occur with tokens.

19

Page 20: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

EXAMPLE

))‘)’

„s”[ _a-zA-Z][ _a-zA-z0-9]*

sIDENTintintKWD_INT((‘(‘

name_fun

[ _a-zA-Z][ _a-zA-z0-9]*

name_funIDENTintintKWD_INT

ATTRIBPATTERNLEKSEM TOKEN

20

Page 21: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

EXAMPLE

„s”[_a-zA-Z][_a-zA-z0-9]*

sIDENT

„s”[ _a-zA-Z][ _a-zA-z0-9]*

sIDENT

++OP_PLUS

returnreturnKWD_RET‘{‘‘{‘‘{‘

ATRYB.PATTERNLEKSEM TOKEN

21

Page 22: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

EXAMPLE

}}‘}’;;‘;‘

ATRYB.PATTERNLEKSEM TOKEN

22

Page 23: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation
Page 24: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

PARSING

Parsing is also called the analysis of hierarchical or “synktatyczną”. The task of parsing is to investigate whether lexical units make the correct design of a given programming language (eg.: if maintained is appropriate sequence of tokens)..

24

Page 25: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

EXAMPLE

There is a function on C++:

int (int s) name_fun{ } + s ; s return

25

Page 26: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation
Page 27: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

SEMANTIC ANALYSIS

Semantic analysis has the following tasks: - Checking program (source program) in terms

of compliance with the semantic definition of the source language, in which the program is written;

- Gathering information for the generation of intermediate code;

27

Page 28: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation
Page 29: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation
Page 30: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation
Page 31: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation
Page 32: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation
Page 33: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation
Page 34: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

CODE OPTIMILIZATION

The primary purpose of optimization is to improve the efficiency of finally code. It may be carried out at all levels of the code, ie. At the source code level, intermediate and also resulting.

34

Page 35: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

EXAMPLE

And here is how the intermediate code of our example after optimization:

i:=1

s:=10*i

@loop: if i>n goto @end

cout<<s

i:=i+1

s:=s+10

goto @loop

@end:

i:=1

@loop:

if i>n goto @end

s:=10*i

cout<<s

i:=i+1

goto @loop

@end: 35

Page 36: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation
Page 37: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation
Page 38: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation
Page 39: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

EXAMPLE

Let's create a flow chart for mathematical functions signum.

39

Page 40: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

EXAMPLE

Implementation of C ++ such a short program might look like this:

float signum (int a) {

if a<0 return -1; else if (a==0) return 0;

else return 1; }

40

Page 41: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

Example - flow graph of functions Example - flow graph of functions signumsignum

x:=a

If x<0 goto B

If x>0 goto A B: y:=-1 goto C

y=0 goto C A: y:=1 goto C

C: STOP

Page 42: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation
Page 43: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

RUN-TIME ENVIRONMENT- rules of visibilityRules of visibility define the way in which the

compiler interprets the references in the program to non-local names. There are two possibilities:

- rules of static visibility - text analysis program (C, Ada);

- rules of dynamic visibility - during the execution of the program (snobol, Lisp, APN)

43

Page 44: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

EXAMPLELet's take a bit of code in C ++:

int k=1; main() {cout<<„the value of k is ”<<k<<endl;

{int k=30;cout<<„in the block the value of k is ”<<k<<endl;}

cout<<„again outside the block k is”<<k;} 44

Page 45: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

EXAMPLE

And here is what appears on the screen as a result of the program:

the value of k is 1 in the blocks the value of k is 30

again outside the block k is 1

45

Page 46: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

EXAMPLEAnd here's how you can be appealed to the curtained

global names in C ++

int k=1;main(){cout<<„the value of k is ”<<k<<endl;

{int k=30;cout<<„in the block the value of k is ”<<k<<„ and the global value of k is”<<::k<<endl;}

Page 47: CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical analysis is also called scanning or line ... - Gathering information for the generation

END

END OF FIRST LECTURE