Compilation in More Detail Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Principles of Programming Languages.

Slides:



Advertisements
Similar presentations
Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00.
Advertisements

Compilers and Language Translation
Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS AM MWF 10 Evans.
UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-
CPSC Compiler Tutorial 9 Review of Compiler.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics Fall 2005.
1 Introduction to Compilation Cheng-Chia Chen. 2 What is a compiler? l a program that translates an executable program in one language into an executable.
Prof Fateman CS 164 Lecture 11 Introduction to Programming Languages and Compilers Prof. Richard Fateman CS164 Fall :30-11AM Room 22 Warren Hall.
Prof. Hilfinger CS 164 Lecture 11 Introduction to Programming Languages and Compilers CS164 11:00-12:00 MWF 10 Evans Notes by G. Necula, with additions.
College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -1- Compiler Construction Principles & Implementation.
BİL744 Derleyici Gerçekleştirimi (Compiler Design)1.
CPSC 388 – Compiler Design and Construction Lecture: MWF 11:00am-12:20pm, Room 106 Colton.
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 10: Compiler I: Syntax Analysis slide 1www.idc.ac.il/tecs.
1 How are Languages Implemented? Two major strategies: –Interpreters (older, less studied) –Compilers (newer, more extensively studied) Interpreters run.
Invitation to Computer Science 5th Edition
INTRODUCTION TO COMPUTING CHAPTER NO. 06. Compilers and Language Translation Introduction The Compilation Process Phase 1 – Lexical Analysis Phase 2 –
COP4020 Programming Languages
Chapter 1. Introduction.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
CSC 338: Compiler design and implementation
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
1 What are Compilers? Translates from one representation of the program to another Typically from high level source code to low level machine code or object.
D. M. Akbar Hussain: Department of Software & Media Technology 1 Compiler is tool: which translate notations from one system to another, usually from source.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 3, 09/11/2003 Prof. Roy Levow.
Some parts are Copyright © 2004 Pearson Addison-Wesley. All rights reserved.3-1 Programming Language Specification and Translation ICOM 4036 Spring 2009.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
CPS 506 Comparative Programming Languages Syntax Specification.
Topic #1: Introduction EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Introduction CPSC 388 Ellen Walker Hiram College.
Prof. Necula CS 164 Lecture 11 Introduction to Programming Languages and Compilers CS164 11:00-12:30 TT 10 Evans.
UPRM ICOM 4029 (Adapted from: Prof. Necula UCB CS 164) 1 Introduction to Programming Languages and Compilers CS164 11:00-12:30 TT 10 Evans.
INTRODUCTION TO COMPILERS(cond….) Prepared By: Mayank Varshney(04CS3019)
The Model of Compilation Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
What is a compiler? –A program that reads a program written in one language (source language) and translates it into an equivalent program in another language.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
Prof Fateman CS 164 Lecture 21 Introduction to Programming Languages and Compilers: Parsing and More CS164.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
©SoftMoore ConsultingSlide 1 Structure of Compilers.
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.3-1 Language Specification and Translation ICOM 4036 Spring 2004 Lecture 3.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
Chapter 1. Introduction.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Design (40-414) Main Text Book:
Chapter 1 Introduction.
CS 3304 Comparative Languages
Introduction to Programming Languages and Compilers
Chapter 1 Introduction.
课程名 编译原理 Compiling Techniques
Chapter 1: Introduction to Compiling (Cont.)
Compiler Lecture 1 CS510.
Compiler Construction
CS416 Compiler Design lec00-outline September 19, 2018
Course supervisor: Lubna Siddiqui
Compilers CSCI/CMPE 3334 David Egle.
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Review: Compiler Phases:
Compilers B V Sai Aravind (11CS10008).
Subject: Language Processor
CS416 Compiler Design lec00-outline February 23, 2019
Programming Language Specification and Translation
Language Specification and Translation
Programming Language Specification and Translation
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 10: Compilers and Language Translation
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Programming Language Specification and Translation
Presentation transcript:

Compilation in More Detail Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Principles of Programming Languages

Phases of Compilation

3 The Structure of a Compiler 1.Lexical Analysis 2.Parsing 3.Semantic Analysis 4.Symbol Table 5.Optimization 6.Code Generation The first 3, at least, can be understood by analogy to how humans comprehend English.

4 1. Lexical Analysis First step: recognize words. – Smallest unit above letters This is a sentence. Note the – Capital “T” (start of sentence symbol) – Blank “ “ (word separator) – Period “.” (end of sentence symbol)

5 More Lexical Analysis Lexical analysis is not trivial. Consider: ist his ase nte nce Plus, programming languages are typically more cryptic than English: *p->f += e-5

6 And More Lexical Analysis Lexical analyzer divides program text into “words” or “tokens” if x == y then z = 1; else z = 2; Units: if, x, ==, y, then, z, =, 1, ;, else, z, =, 2, ;

7 2. Parsing Once words are understood, the next step is to understand sentence structure Parsing = Diagramming Sentences – The diagram is a tree

Diagramming a Sentence 8 Thislineisalongersentence verbarticlenounarticleadjectivenoun subjectobject sentence

9 Parsing Programs Parsing program expressions is the same Consider: If x == y then z = 1; else z = 2; Diagrammed: if-then-else xyz1z2== assignrelationassign predicateelse-stmtthen-stmt

10 3. Semantic Analysis Once sentence structure is understood, we can try to understand “meaning” – But meaning is too hard for compilers Compilers perform limited analysis to catch inconsistencies Some do more analysis to improve the performance of the program Parse tree Semantic Analyzer Intermediate Program

11 Semantic Analysis in English Example: Jack said Jerry left his assignment at home. What does “his” refer to? Jack or Jerry? Even worse: Jack said Jack left his assignment at home? How many Jacks are there? Which one left the assignment?

Programming languages define strict rules to avoid such ambiguities This C++ code prints “4”; the inner definition is used Semantic Analysis in Programming 12 { int Jack = 3; { int Jack = 4; cout << Jack; }

13 More Semantic Analysis Compilers perform many semantic checks besides variable bindings Example: Jack left her homework at home. A “type mismatch” between her and Jack; we know they are different people – Presumably Jack is male

After the lexical analyzing and parsing, symbol table is created. Lexical anayzer gives symbol table as an output Below table shows tokens for a pascal statemet – toplam:=değer+10; 4. Symbol Table

15 5. Optimization No strong counterpart in English, but akin to editing Automatically modify programs so that they – Run faster – Use less memory – In general, conserve some resource

Result = function1(a+b) + function2(a+b) How do you optimize this code? Machine independent optimization

Code motion – Invariant expressions should be executed only once – E.g. for (int i = 0; i < x.length; i++) x[i] *= Math.PI * Math.cos(y); double picosy = Math.PI * Math.cos(y); for (int i = 0; i < x.length; i++) x[i] *= picosy; Machine independent optimization

Multiplying a number with the power of 2 – Shift to the left Dividing a number with the power of 2 – Shift to the right Machine dependent optimization

19 Issues Compiling is almost this simple, but there are many pitfalls. Example: How are erroneous programs handled? Language design has big impact on compiler – Determines what is easy and hard to compile – Course theme: many trade-offs in language design

20 Compilers Today The overall structure of almost every compiler adheres to this outline The proportions have changed since FORTRAN – Early: lexing, parsing most complex, expensive – Today: optimization dominates all other phases, lexing and parsing are cheap