1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.

Slides:



Advertisements
Similar presentations
Lecture 01 - Introduction Eran Yahav 1. 2 Who? Eran Yahav Taub 734 Tel: Monday 13:30-14:30
Advertisements

Yu-Chen Kuo1 Chapter 1 Introduction to Compiling.
Chapter 1: Introduction to Compiling
Compiler Construction1 A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University First Semester 2009/2010.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
CS 355 – Programming Languages
(1.1) COEN 171 Programming Languages Winter 2000 Ron Danielson.
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.
1 Programming Languages Tevfik Koşar Lecture - II January 19 th, 2006.
CSC 338: Compiler design and implementation
Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
RIT 08/11/47Chapter 11 Chapter 1: Introduction to Compiling Dr. Winai Wichaipanitch Rajamangala Institute of Technology Klong 6 Thanyaburi Pathumthani.
COMP 3438 – Part II - Lecture 2: Lexical Analysis (I) Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ. 1.
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.
Compiler design Lecture 1: Compiler Overview Sulaimany University 2 Oct
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
1. 2 Preface In the time since the 1986 edition of this book, the world of compiler design has changed significantly 3.
Introduction Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
Topic #1: Introduction EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Overview of Previous Lesson(s) Over View  A program must be translated into a form in which it can be executed by a computer.  The software systems.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? , Semester 1,
Introduction to Compiling
Introduction CPSC 388 Ellen Walker Hiram College.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
INTRODUCTION TO COMPILERS(cond….) Prepared By: Mayank Varshney(04CS3019)
Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases.
Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,
The Model of Compilation Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
Introduction to Language Programming Hierarchy of programming lang. Based on machine independences: 1. Machine language 2. Assembly language 3. Higher.
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.
Dr. Mohamed Ramadan Saady 314ALL CH1.1 Chapter 1: Introduction to Compiling.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
CSC 4181 Compiler Construction
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
Presented by : A best website designer company. Chapter 1 Introduction Prof Chung. 1.
Objective of the course Understanding the fundamentals of the compilation technique Assist you in writing you own compiler (or any part of compiler)
Compilers Design Chapter1 : Introduction, Structure of a Compiler
Chapter 1. Introduction.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Advanced Computer Systems
Compiler Design (40-414) Main Text Book:
PRINCIPLES OF COMPILER DESIGN
Chapter 1 Introduction.
Introduction to Compiler Construction
CS 3304 Comparative Languages
Compiler Construction (CS-636)
Chapter 1 Introduction.
-by Nisarg Vasavada (Compiled*)
课程名 编译原理 Compiling Techniques
Chapter 1: Introduction to Compiling (Cont.)
Compiler Construction
Course supervisor: Lubna Siddiqui
Compilers B V Sai Aravind (11CS10008).
COP4020 Programming Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 1 Introduction.
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Compiler Structures 1. Overview Objective
Presentation transcript:

1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.

2 Overview of the Subject (COMP 3438) Overview of Unix Sys. Prog. ProcessFile System Overview of Device Driver Development Character Device Driver Development Introduction to Block Device Driver Overview of Complier Design Lexical Analysis (HW #3) Syntax Analysis (HW #4) Part I: Unix System Programming (Device Driver Development) Part II: Compiler Design Course Organization (This lecture is in red)

Outline  Programming language: High-level vs. Low level  What is a compiler?  Phases of a compiler 3

Programming language – Machine Language  Machine languages Everything is a binary number Operations, data, addresses, … e.g. In MIPS 2000, # $t5 + 4  $t6 Machines like it BUT not us 4

Programming language – Assembly Language  Assembly languages Symbolic representation of Machine Language e.g. Machine Code: # $t5 + 4  $t6 Assembly Code: add $t6, $t5, 4 5

High-level Programming language  High-level languages Procedural (modular) programming Group instructions into meaningful abstractions, e.g., data types, control structures, functions, etc. C, Pascal, Perl Object oriented programming Group “data” and “methods” into “objects” Naturally represents the world around us C++, Java, JavaScript Logical programming: Prolog Functional programming: ML 6

Why High-level Languages?  Hide unnecessary details, so have a higher level of abstraction, increasing productivity  Make programs more robust, e.g., meaning of information is specified before its use, enabling substantial error checking at compile time  Make programs more portable 7

Compilers are Translators C/C++ Fortran Java Perl Matlab Natural Language Command Machine code Virtual Machine Code Transformed code (C, Java, …) Lower level commands Semantic components ……. Translate 8

Translation Mechanisms  Compilation To translate a source program in one language into an executable program in another language, and produce results while executing the new program Examples: C, C++, Fortune  Interpretation To read a source program and produce results while understanding that program Examples: Basic  Case Study: Java First, translate to java bytecode (compilation) Second, execute by interpretation (JVM)/compilation (JIT (Just-In-Time)) 9

Comparison of Compiler/Interpreter CompilerInterpreter Overview AdvantagesFast program execution; Fully exploit architecture features; Easy to debug; Flexible to modify; Machine independent; DisadvantagesPre-processing of program; Complicated; Execution overhead; Source Code Compiler Object Code Data Results Source Code Data Interpreter Results 10

What is a compiler?  A compiler is a software that takes a program written in one language (called the source language) and translates it into an equivalent program in another language (called the target language).  It also reports to its user the presence of errors in the source program. Compiler Source program Target program Error messages 11

The Phases of a Compiler Source program Lexical Analyzer Syntax Analyzer (Parser) Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator Target program Symbol-table Manager Error Handler 12

 Scan the source program and group sequences of characters into tokens.  A token is the smallest element of a language a group of characters (e.g., a series of alphabetic characters forms a keyword; a series of digits forms a number).  The sub-module of the compiler that performs lexical analysis is called a lexical analyzer.  Example: position := initial + rate * 60 (pascal statement) Lexical Analysis Value Toke Type position ID rate ID := Operator * Operator initial ID 60 NUM 13

 Once the tokens are identified, syntax analysis groups sequence of tokens into language constructs e.g., identifiers, numbers, and operators can be grouped into expressions. e.g., keywords, identifiers, expressions and operators can be combined to form statements.  The sub-module of the compiler that performs syntax analysis is called the parser/ Syntax Analyzer. Syntax Analysis 14

 Result of syntax analysis is recorded in a hierarchical structure called a syntax tree, each node represents an operation and its children represent the arguments of the operation. evaluation begins from bottom and moves up. e.g., parse tree for postion := initial + rate * 60 Syntax Analysis – Syntax (Parse) Tree = id1 + id2* id3 NUM (60) 15

Semantic Analysis  Determine the meaning using the syntax tree Put semantic meaning into the syntax tree Perform checks to ensure that components fit together meaningful, e.g. Type checking = id1 + id2* id3 NUM (60) inttoreal 16

Intermediate Code Generation  Generate IR (Intermediate Representation) code temp1 := inttoreal(60) temp2 := id3*temp1 temp3 := id2+temp2 id1 := temp3 Easier to generate machine code from IR code = id1 + id2* id3 NUM (60) inttoreal 17

 Code Optimization: Modify program representation so that program can run faster, use less memory, power, … IR Code Optimized Code Code Optimization temp1 := inttoreal(60) temp2 := id3*temp1 temp3 := id2+temp2 id1 := temp3 temp1 := id3* 60.0 id1 := id2+temp1 18

Code Generation  Generate target program. Machine Code temp1 := id3* 60.0 id1 := id2+temp1 MOVF id3, R2 MULF #60.0, R2 MOVF id2, R1 ADDF R2, R1 MOVF R1, id1 19

Symbol Table Management  Collect and maintain information about ID Attributes: Storage: where to store (Data, Heap, Stack, …) Type: char, int, pointer, … Scope: effective range Number: value  Information is added and used by all phases  Debuggers use symbol table 20

Front End and Back End Source program Lexical Analyzer Syntax Analyzer (Parser) Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator Target program Symbol-table Manager Error Handler Front End Back End 21

Distinction between Phases and Passes  Passes: the times going through a program representation 1-pass, 2-pass, multiple-pass compilation Language become more complex – more passes  Phases: conceptual stages Not completely separate Semantic phase may do things that syntax should do 22

Compiler Tools Phases Tools Lexical Analysis Lex, flex Syntax Analysis yacc, bison Semantic Analysis Intermediate Code Code Optimization Code Generation 23