college of engineering salahaddin university compilers ......advanced compiler design and...

16
Ministry of Higher Education and Scientific research Directorate of Quality Assurance and Accreditation بو ڕێوه بو رایو جۆری ویای دڵنی تیتمانو م بو خشینSoftware and Informatics Engineering Department College of Engineering Salahaddin University Compilers Course Book – (Year 3) Lecturer's name: Dr. Abbas M. Ali Academic Year 2018/2019

Upload: others

Post on 20-Mar-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: College of Engineering Salahaddin University Compilers ......Advanced Compiler Design and Implementation, Steven Muchnick, | ISBN-10: ... Introduction to Compiling. o Some basic terms

Ministry of Higher Education and Scientific research

Directorate of Quality Assurance and Accreditation خشین‌بو‌متمانوتی‌دڵنیایی‌جۆری‌و‌‌رایو‌بو‌ڕێوه‌بو

Software and Informatics Engineering Department

College of Engineering

Salahaddin University

Compilers Course Book – (Year 3)

Lecturer's name: Dr. Abbas M. Ali

Academic Year 2018/2019

Page 2: College of Engineering Salahaddin University Compilers ......Advanced Compiler Design and Implementation, Steven Muchnick, | ISBN-10: ... Introduction to Compiling. o Some basic terms

Ministry of Higher Education and Scientific research

Directorate of Quality Assurance and Accreditation خشین‌بو‌متمانوتی‌دڵنیایی‌جۆری‌و‌‌رایو‌بو‌ڕێوه‌بو

Course Book

1. Course name Compilers

2. Lecturer in charge Dr. Abbas M. Ali

3. Department/ College Software Engineering/College of Enineering 4. Contact e-mail: [email protected]

5. Time (in hours) per week Theory: 3

6. Office hours Monday 8:30 – 11:30

7. Course code SE 8. Teacher's academic profile Dr.Abbas M. Ali

Lecturer at Software Engineering Department College of Engineering – Salahaddine University Hawler – Kurdistan Current Lecture : Operating Systems (4th year) theory and practical and Compilers principles (3rd year) theory Past: Microprocessors (2nd year) theory and practical, Compilers (3rd year) . Education: B.Sc in Computer science M.Sc in Computer science Ph.D in Computer A.I.

9. Keywords Lexical , Syntax, Semantic, Interpreter, Symbol table, First

follow, token

10. Course overview: This course is a mandatory requirement for the BSc in Software Engineering. This course is Introduction

to Compiling; Lexical analysis: specification and recognition of tokens, finite automata; Syntax analysis:

grammars, top-down and bottom-up passing; Syntax-directed translation; Semantic routines; Storage-

allocation strategies; Code generation; Error recovery.

This course aims to equip students with the essentials of compilation concept and practice and offers an

opportunity for students to achieve practical skills in implementing some languages like C++, Data

structure and software engineering priciple.

11. Course objective: The main objective of compilers course to

Page 3: College of Engineering Salahaddin University Compilers ......Advanced Compiler Design and Implementation, Steven Muchnick, | ISBN-10: ... Introduction to Compiling. o Some basic terms

Ministry of Higher Education and Scientific research

Directorate of Quality Assurance and Accreditation خشین‌بو‌متمانوتی‌دڵنیایی‌جۆری‌و‌‌رایو‌بو‌ڕێوه‌بو

introduce the compilation concepts and phases : Lexical analysis, Syntax analysis, Syntax-

directed translation, Semantic routines,Intermediate code generation Storage-allocation

strategies, Code generation, Error recovery.

To implement the data structure subject practically in compilation

It is practical approach to implement software Engineering concepts

12. Student's obligation Homework is normally given and unexpected quizzes provide an active way to keep the students active

and more in touch with the subject. In addition quizzes, students attendance and their activity on the

lectures will all collected together to form the 3% assessment of each semester.

In addition to there will be programs to be written by the students as an H.W. and the achievement of

these programs will be graded the will be admit as assignments.

13. Forms of teaching Lectures: power point presentations are used in addition to the whiteboard clarification which is usually used to

make a frequent step by step communication with the students

14. Assessment scheme The course breaks compound two parts theoretical part, and assignment sessions. There are exams to

assess each student; 1st semester exam, 2nd semester exam, and the final exam.

The theoretical Exams only cover the theoretical part of the unit. The total marks will be as follows:

First Semester Exam: 17 %

First Semester daily activities (quize and homewoks): 3%

Second Semester Exam: 17%

Second Semester daily activities (quizzes and homeworks): 3%

Average Marks for both semesters (1&2) are: 40 %

Final Exam: 60%‌

15. Student learning outcome: At the end of this course, students will be able to:

Page 4: College of Engineering Salahaddin University Compilers ......Advanced Compiler Design and Implementation, Steven Muchnick, | ISBN-10: ... Introduction to Compiling. o Some basic terms

Ministry of Higher Education and Scientific research

Directorate of Quality Assurance and Accreditation خشین‌بو‌متمانوتی‌دڵنیایی‌جۆری‌و‌‌رایو‌بو‌ڕێوه‌بو

1- Describe the purpose and function of compilers.

2- Identify the importance of Compilers in computer systems.

3- Understand the fundamental of structure and architecture of Compilers.

4- Install and gain basic of how Compilers working and interface with the OS.

5- Understand the fundamental concepts of OS Stages.

6- Write fundamental programs of Compilers algorithms.

16. Course Reading List and References‌: Compilers Principles, Techniques, and Tools, Alfred V. Aho, Ravi Seth, and Jeffrey D. Ulman., M ISBN:0-201-10194-7 Publisher: Addison-Wesley, 2007. Advanced Compiler Design and Implementation, Steven Muchnick, | ISBN-10: 1558603204 | ISBN-13: 978-1558603202 .

17. The Topics: Lecturer's name Week Outcome Indicators and Details Assessment

Methods

1-4

1. Introduction to Compiling.

o Some basic terms in compilers

o The phases of a compiler.

o Compiler-construction tools.

Dr.Abbas

5-8 2. A simple One-Pass Compiler.

o Syntax definition and syntax directed translation.

o Parsing.

o A translator of simple expressions.

o Lexical Analysis.

o Incorporating a symbol table.

Quiz 1

HW1

9-12

3. Lexical Analysis.

o The role of the lexical analyzer.

o Input buffering.

o Specification of tokens and Recognition of tokens.

o Lex. Analysis & Finite Automata.

Page 5: College of Engineering Salahaddin University Compilers ......Advanced Compiler Design and Implementation, Steven Muchnick, | ISBN-10: ... Introduction to Compiling. o Some basic terms

Ministry of Higher Education and Scientific research

Directorate of Quality Assurance and Accreditation خشین‌بو‌متمانوتی‌دڵنیایی‌جۆری‌و‌‌رایو‌بو‌ڕێوه‌بو

13-20

4. Syntax Analysis.

o The role of parser.

o Context-free grammars.

o Writing a grammar.

o Top-down Parsing.

o Bottom-up parsing and operator-precedence parsing.

o Ambiguous grammars

o Error detection & reporting.

HW2

21-22

5. Type checking.

o Type systems.

o Specification of a simple type checker.

o Equivalence of type expressions and type conversion.

23-24

6. Intermediate Code generation.

o Intermediate Languages.

o Declarations.

o Assignment Statements.

HW3

Quiz 2

25-27 7. Code Generation.

o Issues in the design of a code generator.

o The target machine.

o Run-time storage management.

o Basic blocks and flow graphs.

HW4

28-30 8. Code Optimization

o Introduction.

o The principal sources of optimization.

19. Examinations:

Sample of Exam paper:

Q1:A: Construct and write a recursive descent parser for the language generated by the following

grammar. (15 Marks)

E ----- > E + F | E - F | F

F ------- > NUM | ID | ( E )

Main()

Page 6: College of Engineering Salahaddin University Compilers ......Advanced Compiler Design and Implementation, Steven Muchnick, | ISBN-10: ... Introduction to Compiling. o Some basic terms

Ministry of Higher Education and Scientific research

Directorate of Quality Assurance and Accreditation خشین‌بو‌متمانوتی‌دڵنیایی‌جۆری‌و‌‌رایو‌بو‌ڕێوه‌بو

{

lookahead =lex();

expr();

}

void expr()

{

term();

if ( lookahead ==“+”)

{ match(‘+’);

expr();

}

}

Void factor ()

{

Switch(lookahead)

{

Case “(“ : match(‘(‘);

expr();

match(‘)’);

break;

Case Number : match(‘number’);

break;

Default: error (“error in factor”);

}

}

Page 7: College of Engineering Salahaddin University Compilers ......Advanced Compiler Design and Implementation, Steven Muchnick, | ISBN-10: ... Introduction to Compiling. o Some basic terms

Ministry of Higher Education and Scientific research

Directorate of Quality Assurance and Accreditation خشین‌بو‌متمانوتی‌دڵنیایی‌جۆری‌و‌‌رایو‌بو‌ڕێوه‌بو

Void match(t)

{

if ( lookahead ==t)

lookahead =lex();

else

error(“syntax error”);

}

B: By using Block diagram show that how the statement (10 Marks)

Position := initial + rate * 60

Will be compiled show the phases of the compiler in you block diagram

19

lexical analyzer

syntax analyzer

semantic analyzer

Position := initial + rate * 60

id1 := id2 + id3 * 60

:=

id1 +

id2 *

id3 60

:=

id1 +

id2 *

id3 inttoreal

60

intermediate code generator

temp1 := inttoreal (60)

temp2 := id3 * temp1

temp3 := id2 + temp2

id1 := temp3

code optimizer

temp1 := id3 * 60.0

id1 := id2 + temp1

code generator

MOVF id3, R2

MULF #60.0, R2

MOVF id2, R1

ADDF R2, R1

MOVF R1, id1

The Phases of a Compiler

Q2:A: complete the following sentences: (10 Marks)

1- The Postfix Notation for (((3+6)*(2-4))+7) is …3 6+ 2 4 - * 7 +…..

2- The Front end of the compiler is ‌ all the operations that are related to the source like scanner ,

Page 8: College of Engineering Salahaddin University Compilers ......Advanced Compiler Design and Implementation, Steven Muchnick, | ISBN-10: ... Introduction to Compiling. o Some basic terms

Ministry of Higher Education and Scientific research

Directorate of Quality Assurance and Accreditation خشین‌بو‌متمانوتی‌دڵنیایی‌جۆری‌و‌‌رایو‌بو‌ڕێوه‌بو

parser , source code optimizer.

3- The preprocessor is expands shorthands, called macros, into source language statements

4- The Four fields of Quadruple form are …operation arg1 , arg2 and the result.

5- An LR(0) item of a grammar G is the production with dot position in the body of the production.

B: What is meant by Bootstraping explain this operation using T-diagram. (10 Marks)

Bootstrapping And Porting

• A more reasonable approach today is to write the compiler in another language for which a compiler already exist .

• If the existing compiler already runs on the target machine , then we need only compile the new compiler using the existing compiler to get a running program

Existing compiler for language B

Compiler for language A written in language B

Running compiler for language A

• T- diagram : is a diagram to describe the source and target code in compilers.

• Where S: source language , T: target Language H : Host language

• If H is not machine code then we consider it to be executable code for hypothetical machine.

Page 9: College of Engineering Salahaddin University Compilers ......Advanced Compiler Design and Implementation, Steven Muchnick, | ISBN-10: ... Introduction to Compiling. o Some basic terms

Ministry of Higher Education and Scientific research

Directorate of Quality Assurance and Accreditation خشین‌بو‌متمانوتی‌دڵنیایی‌جۆری‌و‌‌رایو‌بو‌ڕێوه‌بو

Quick and dirty compiler

• Quick and dirty compiler running but inefficient compiler we use it to compile the good compiler the we recompile the good compiler to produce the final version

running but inefficient

compiler

quick and dirty

compiler

final version compiler

A H

A A H

H

A H

H

A H

A A H

H

A H

H

Page 10: College of Engineering Salahaddin University Compilers ......Advanced Compiler Design and Implementation, Steven Muchnick, | ISBN-10: ... Introduction to Compiling. o Some basic terms

Ministry of Higher Education and Scientific research

Directorate of Quality Assurance and Accreditation خشین‌بو‌متمانوتی‌دڵنیایی‌جۆری‌و‌‌رایو‌بو‌ڕێوه‌بو

Q3: Consider the following grammar

E ----- E # T | T

T ------- T & F | F

F ------- (E) | id

A: 1- Compute FIRST and FOLLOW for the grammar (15 Marks)

2- Construct predictive parsing table for the predictive parser

3- Parse the sentence id # id & ( id) using your predictive parsing table.

Ans:

Suppose that # is + and & is *

• E → TE'

• E' → +TE'

• E' → ε

• T → FT'

• T' → *FT'

• T' → ε

• F → (E)

• F → id

Page 11: College of Engineering Salahaddin University Compilers ......Advanced Compiler Design and Implementation, Steven Muchnick, | ISBN-10: ... Introduction to Compiling. o Some basic terms

Ministry of Higher Education and Scientific research

Directorate of Quality Assurance and Accreditation خشین‌بو‌متمانوتی‌دڵنیایی‌جۆری‌و‌‌رایو‌بو‌ڕێوه‌بو

E → TE‘ , E' → +TE‘| ε, T → FT‘,T' → *FT‘| ε, F → (E)| id

$Id)(*+

TE‘TE‘E

εε+TE‘E’

FT‘FT‘T

εε*FT‘εT’

id(E)F

First(E)= first(T)= FIRST(F) ={(,Id}First(E’)= {+, ε}, first(T’)={*,ε}Follow (E)=follow(E’)= {$,)}Follow(T) =follow(T’) =(),$,+)FOLLOW(F)={),+,$,*}

1- Compute CLOSURE and GOTO functions for the grammar symbols. (15 Marks)

2- Construct parsing table for the LR parser

3- Parse the sentence id # id & ( id) using your parsing table.

Page 12: College of Engineering Salahaddin University Compilers ......Advanced Compiler Design and Implementation, Steven Muchnick, | ISBN-10: ... Introduction to Compiling. o Some basic terms

Ministry of Higher Education and Scientific research

Directorate of Quality Assurance and Accreditation خشین‌بو‌متمانوتی‌دڵنیایی‌جۆری‌و‌‌رایو‌بو‌ڕێوه‌بو

Full DFA

1

S ’ → . S $S → . ( L )S → . id

S ’ → S . $

final state

S →( . L )L → . SL → . L , SS →. ( L )S → . id

S →id . L → L , . SS → . ( L )S → . id

S → ( L . )L → L . , S

L → S . S → ( L ) .

L → L , S .

S

$

(

id

S

id

L

)

,

id

S

(

1

4

2

3

7

8

5

6

9

S →( L ) | idL →S | L , S

(

1

Page 13: College of Engineering Salahaddin University Compilers ......Advanced Compiler Design and Implementation, Steven Muchnick, | ISBN-10: ... Introduction to Compiling. o Some basic terms

Ministry of Higher Education and Scientific research

Directorate of Quality Assurance and Accreditation خشین‌بو‌متمانوتی‌دڵنیایی‌جۆری‌و‌‌رایو‌بو‌ڕێوه‌بو

3

Parsing Example: ((x),y)

S →( L ) | idL →S | L , S

Q4: A: Describe the advantages and disadvantages of generating intermediate code (such as

quads) vs. generating machine code directly from an AST. (10 Marks)

Intermediate Representation (IR) : A kind of abstract machine language that can express the target machine

operations without committing to too much machine details.

Page 14: College of Engineering Salahaddin University Compilers ......Advanced Compiler Design and Implementation, Steven Muchnick, | ISBN-10: ... Introduction to Compiling. o Some basic terms

Ministry of Higher Education and Scientific research

Directorate of Quality Assurance and Accreditation خشین‌بو‌متمانوتی‌دڵنیایی‌جۆری‌و‌‌رایو‌بو‌ڕێوه‌بو

4

Without IR

C

Pascal

FORTRAN

C++

SPARC

HP PA

x86

IBM PPC

5

With IR

C

Pascal

FORTRAN

C++

SPARC

HP PA

x86

IBM PPC

IR

1. Retargeting - Build a compiler for a new machine by attaching a new code generator to an existing front-end.

2. Optimization - reuse intermediate code optimizers in compilers for different languages and different machines.

Note: the terms “intermediate code”, “intermediate language”, and “intermediate representation” are all

used interchangeably.

Page 15: College of Engineering Salahaddin University Compilers ......Advanced Compiler Design and Implementation, Steven Muchnick, | ISBN-10: ... Introduction to Compiling. o Some basic terms

Ministry of Higher Education and Scientific research

Directorate of Quality Assurance and Accreditation خشین‌بو‌متمانوتی‌دڵنیایی‌جۆری‌و‌‌رایو‌بو‌ڕێوه‌بو

B: Give a BNF syntax for unsigned floating point constants that would allow constants

such as (15 Marks)

1.2 3E4 0.23E-8

but not

.123 20. 21.E4

Construct a finite state machine that could be used in a lexical analyser to read in

such floating point numbers.

Ans

Unsigned numbers in Pascal are string 580, 39,377,6.336E4 or 1.894E-4

• digit --- 0|1|….9

• digits ---digit digit*

• Optional-fraction --- .digits | ϵ

• Optional –exponent ---- (E(+|-| ϵ)digits)| ϵ

• num --- digits optional-fraction optional-exponent

Unsigned numbers

• Transition diagram for unsigned number in Pascal

• Num- d+(.d+)?(E(+|-)?d+)?

13.dstart

d

1912 14 15E

d

d

16

17 18

21.dstart

20 22 23otherd

24

26dstart

25other

27

d d

d

20. Extra notes:

21. Peer review ڵ‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌ی‌هاوه‌وه‌پێداچوونه‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌

Page 16: College of Engineering Salahaddin University Compilers ......Advanced Compiler Design and Implementation, Steven Muchnick, | ISBN-10: ... Introduction to Compiling. o Some basic terms

Ministry of Higher Education and Scientific research

Directorate of Quality Assurance and Accreditation خشین‌بو‌متمانوتی‌دڵنیایی‌جۆری‌و‌‌رایو‌بو‌ڕێوه‌بو