Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.

Slides:



Advertisements
Similar presentations
Introduction to Compiler Construction
Advertisements

CS 31003: Compilers Introduction to Phases of Compiler.
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.
Lecture 2 Phases of Compiler. Preprocessors, Compilers, Assemblers, and Linkers Preprocessor Compiler Assembler Linker Skeletal Source Program Source.
September 7, September 7, 2015September 7, 2015September 7, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Course Revision Contents  Compilers  Compilers Vs Interpreters  Structure of Compiler  Compilation Phases  Compiler Construction Tools  A Simple.
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.
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 History of compiler development 1953 IBM develops the 701 EDPM (Electronic Data Processing Machine), the first general purpose computer, built as a “defense.
Introduction to Compiler Construction Robert van Engelen COP5621 Compiler Construction Copyright Robert.
CSC 338: Compiler design and implementation
CST320 - Lec 11 Why study compilers? n n Ties lots of things you know together: –Theory (finite automata, grammars) –Data structures –Modularization –Utilization.
RIT 08/11/47Chapter 11 Chapter 1: Introduction to Compiling Dr. Winai Wichaipanitch Rajamangala Institute of Technology Klong 6 Thanyaburi Pathumthani.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
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.
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.
Introduction to Compiling
Chapter 1 Introduction Major Data Structures in Compiler
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
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,
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
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.
CH1.1 CSE244 Chapter 1: Introduction to Compiling Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University of Connecticut.
Dr. Mohamed Ramadan Saady 314ALL CH1.1 Chapter 1: Introduction to Compiling.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
CSC 4181 Compiler Construction
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
ICS312 Introduction to Compilers Set 23. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
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 Samuel College of Computer Science & Technology Harbin Engineering University.
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
Compiler Construction (CS-636)
Introduction.
Chapter 1 Introduction.
History of compiler development
Chapter 1: Introduction to Compiling (Cont.)
Compiler Construction
Introduction to Compiler Construction
Compiler Construction
Compiler 薛智文 TH 6 7 8, DTH Spring.
Compiler 薛智文 TH 6 7 8, DTH Spring.
Introduction to Compiler Construction
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 1 Introduction.
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Compiler 薛智文 M 2 3 4, DTH Spring.
Introduction to Compiler Construction
Presentation transcript:

Introduction to Compilers

Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software engineering

Compilers A compiler is a program that reads a program written in one language – the source language – and translate it into an equivalent program in another language - the target language. Early compilers ’s

Machine Language, Assembly Language, High-Level Language Machine language is the native language of the computer on which the program is run. Native code It consists of bit strings which are interpreted by the mechanism inside the computer. Example in IBM 370 Binary: Hexadecimal: 1835 Copy the content of Register 5 into Register 3 LR 3, 5 Assembler, assembly language

Machine Language, Assembly Language, High-Level Language Example High-level language X := Y + Z; Assembly language L 3, Y ; Load the working register with Y A 3, Z ; Add Z ST 3, X ; Store the result in X

Terminology Source language Java, C, C++ Object language Machine language Object code Object file, object module Target machine The computer on which the program is to be run

Terminology Cross compiler A compiler that generates code for a machine that is different from the machine on which the compiler runs. Example: A compiler which can be run on a IBM PC but which compiles to the machine language of a special- purpose embedded system.

Compilers and Interpreters Compiler Translates the high-level program to the target program. Interpreter Executes the program.

The Environment of the Compiler

Example COMP myprog ; Compiles the program LINK myprog ; Links the program RUN myprog ; Runs the program

Phases of a Compiler Five (six) phases of compilation Lexical analysis Syntactic analysis (Semantic analysis) Intermediate code generation Optimization Object code generation

Phases of a Compiler Language Processing System

Phases of a Compiler

Two Parts of Compilation Analysis breaks up the source program creates an intermediate representation Synthesis constructs the desired target program from the intermediate representation

Analysis Lexical Analysis linear analysis, scanning Syntax Analysis parsing, hierarchical analysis Semantic Analysis Intermediate Code Generation Advantage of dividing analysis simple design compiler efficiency compiler portability

Analysis of the Source Program Lexical analysis (linear analysis) the streams of characters making up the source program is read from left-to-right and grouped into tokens Syntax analysis (hierarchical analysis) characters or tokens are grouped hierarchically into nested collections with collective meaning Semantic analysis certain checks are performed to ensure that the components of a program fit together meaningfully

Lexical Analysis Linear analysis, scanning Reads the stream of characters in the source program from left to right, and groups into tokens Tokens are sequences of characters having a collective meaning

Example: Lexical Analysis position := initial + rate * 60 id1 := id2 + id3 * 60

Syntax Analysis Parsing Hierarchical analysis Groups the tokens of the source program into grammatical phrases represented by parse tree that are used by the compiler to synthesize output.

Example: Syntax Analysis

Semantic Analysis Checks the source program for semantic errors and gathers type or semantic information for the subsequent code generation phase

Example: Semantic Analysis

Error Handler When each phases of compilation encounters error, a phase must somehow deal with that error. Error in Lexical Phase The characters in the input do not form any token of the language. Error in Syntax Phase The token stream violates the structure rules (syntax) of the language. Error in Semantic Phase Constructs have the right syntactic structure, but no meaning to the operation involved.

S/W Tools Performing Analysis Structure editors a sequence of command => a source program Pretty printers indentation, fonts Static checkers a program => discover bugs without run Interpreters performing operations

Performing Analysis Text formatters typeset text Silicon compilers circuit design Query interpreters DB

Intermediate Code Generation Explicit intermediate representation A program for an abstract machine Two properties of intermediate code Easy to produce Easy to translate into the target program Intermediate form Three address form (quadruples, triples) Two address form

Three-Address Code Has at most three operands Each three-address instruction has at most one operator in addition to the assignment The compiler must generate a temporary name to hold the value computed by each instruction May have fewer than 3 operands

Example: Three-Address Code

Example: Intermediate Code

Synthesis Part The synthesis part constructs the desired target program from the intermediate representation.

Code Optimization Improve the intermediate code to get the fast- running machine code Optimizing compiler

Example: Code Optimization

Code Generation Generates the target codes re-locatable machine code assembly code

Example: Code Generation

System Support There is a certain amount of supporting code to be supplied to the compilation. Symbol table management Error handling

System Support Symbol table handler The central repository of information about the names or identifiers in the program Error handling Implements the compiler’s response to errors in the code it is compiling. Diagnostics Where the error was found and what kind of error it was

Passes, Front End, Back End The compiler makes one or more passes through the program. A pass consists of reading a version of the program from a file and writing a new version of it to an output file. A pass normally comprises more than one phase, but the number of passes, and the phases they cover, varies.

Passes, Front End, Back End Front End Dependent on the source language and have little or no concern with the target machine Lexical analysis (Semantic analysis) Intermediate code generation Back End Machine-dependent Code optimization Target code generation

Writing a Compiler The first compiler was written in assembly language; there was no other alternative. High-level language compilers Cross compiler Useful tools – “compiler compilers” Lex Yacc

Retargetable Compilers In many cases, a compiler writer will want to adapt a compiler for use with a new target A compiler that can be modified in this way is said to be retargetable. Cross compiler Alternative approaches Distinction between Front End and Back End Compiler for imaginary machine (virtual machine)

Cousins of the Compiler Preprocessors Assemblers Loaders and Link-Editors

Preprocessor A preprocessor is a simple translator that is applied to the source program before it is submitted to the compiler. Before the program is compiled it is passed through the preprocessor, which replaces all occurrences of the pre-defined expression with the defined sequence of instructions. Example

Functions of Preprocessors Macro processing File inclusion Language extensions DB query languages embedded in highlevel languages

Example: Preprocessors The C Programming Language

Assemblers Two-pass assembly Loaders and Link-Editors

Assemblers Assembly code a mnemonic version of machine code, in which names are used instead of binary codes for operations, and names are also given to memory addresses

Two-Pass Assembly(1) In the first pass, All the identifiers that denote storage locations are found and stored in a symbol table Identifiers are assigned storage locations as they are encountered for the first time Example: b := a + 2

Two-Pass Assembly (2) In the second pass, The assembler scans the input again. It translates each operation code into the sequence of bits representing that operation in machine language It translates each identifier representing a location into the address given for that identifier in the symbol table The output of the second pass is usually relocatable machine code

Example: Relocatable Addresses Altering the relocatable address to absolute or unrelocatable machine code: Suppose that the address space containing the data is to be loaded starting at location “L = ” L must be added to the address of the instruction

Loaders and Link-Editors Loader Performs the two functions of loading and link-editing Loading Consists of taking relocatable machine code, altering the relocatable addresses, and placing the altered instructions and data in memory at the proper locations

Link-Editors Link-editor Allows us to make a single program from several files of relocatable machine code. These files may have been the result of several different compilations, and One or more may be library files. External references In which the code of one file refers to a location in another file.

Summary A quick overall picture of what a compiler does, what goes into it, and how it is organized