Compiler Construction (CS-636)

Slides:



Advertisements
Similar presentations
Symbol Table.
Advertisements

CS 31003: Compilers Introduction to Phases of Compiler.
Yu-Chen Kuo1 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.
COP4020 Programming Languages
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.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
D. M. Akbar Hussain: Department of Software & Media Technology 1 Compiler is tool: which translate notations from one system to another, usually from source.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
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.
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.
CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
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 CPSC 388 Ellen Walker Hiram College.
Chapter 1 Introduction Major Data Structures in Compiler
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
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.
Compilers I CNS History Wires Wires Machine Language Machine Language FFBA FFBA No Translation necessary No Translation necessary Assembly Language.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
CSC 8505 Compiler Construction
Sung-Dong Kim Dept. of Computer Engineering, Hansung University
CS510 Compiler Lecture 1. Sources Lecture Notes Book 1 : “Compiler construction principles and practice”, Kenneth C. Louden. Book 2 : “Compilers Principles,
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Programming Languages Concepts Chapter 1: Programming Languages Concepts Lecture # 4.
Prologue Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
Dr. Hussien Sharaf Dr Emad Nabil. Dr. Hussien M. Sharaf 2 position := initial + rate * Lexical analyzer 2. Syntax analyzer id 1 := id 2 + id 3 *
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
Chapter 1. Introduction.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
System Software Theory (5KS03).
Advanced Computer Systems
Compiler Design (40-414) Main Text Book:
Introduction Chapter : Introduction.
PRINCIPLES OF COMPILER DESIGN
Chapter 1 Introduction.
CS510 Compiler Lecture 1.
Introduction to Compiler Construction
CS 326 Programming Languages, Concepts and Implementation
A Simple Syntax-Directed Translator
Introduction.
CS 363 – Chapter 1 What is a programming language? Kinds of languages
Chapter 1 Introduction.
-by Nisarg Vasavada (Compiled*)
课程名 编译原理 Compiling Techniques
Chapter 1: Introduction to Compiling (Cont.)
Compiler Lecture 1 CS510.
Compiler Construction
Introduction to Compiler Construction
Compiler Construction
Compiler 薛智文 TH 6 7 8, DTH Spring.
Compilers B V Sai Aravind (11CS10008).
COP4020 Programming Languages
Compiler 薛智文 TH 6 7 8, DTH Spring.
Introduction to Compiler Construction
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Introduction Chapter : Introduction.
Compiler 薛智文 M 2 3 4, DTH Spring.
Introduction to Compiler Construction
Presentation transcript:

Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi

Outline Introduction to Compiler (Continue…) Summary

Introduction to Compiler Lecture: 2

Programs Related to a Compiler These programs include Interpreters Assemblers Linkers Loaders Preprocessors Editors Debuggers Profilers Project Managers

Interpreters Execute the source program immediately rather than generating object code Examples: BASIC, LISP, used often in educational or development situations Speed of execution is slower than compiled code by a factor of 10 or more Share many of their operations with compilers

Assemblers A translator for the assembly language of a particular computer Assembly language is a symbolic form of one machine language A compiler may generate assembly language as its target language and an assembler finished the translation into object code

Linkers Collect separate object files into a directly executable file Connect an object program to the code for standard library functions and to resource supplied by OS Becoming one of the principle activities of a compiler, depends on OS and processor

Loaders Resolve all re-locatable address relative to a given base Make executable code more flexible Often as part of the operating environment, rarely as an actual separate program

Preprocessors Delete comments, include other files, and perform macro substitutions Required by a language (as in C) or can be later add-ons that provide additional facilities

Editors Compiler have been bundled together with editor and other programs into an interactive development environment (IDE) Oriented toward the format or structure of the programming language, called structure-based May include some operations of a compiler, informing some errors

Debuggers Used to determine execution error in a compiled program Keep tracks of most or all of the source code information Halt execution at pre-specified locations called breakpoints Must be supplied with appropriate symbolic information by the compiler

Profilers Collect statistics on the behavior of an object program during execution Called Times for each procedures Percentage of execution time Used to improve the execution speed of the program

Project Managers Coordinate the files being worked on by different people, maintain coherent version of a program Language-independent or bundled together with a compiler Two popular project manager programs on Unix system Sccs (Source code control system) Rcs (revision control system)

Compiler Components Six Components Three Auxiliary Components Scanner Parser Semantic Analyzer Source Code Optimizer Code Generator Target Code Optimizer Three Auxiliary Components Literal Table Symbol Table Error Handler

The Compilation Process Scanner Parser Semantics Analyzer Source Code Optimizer Code Generator Target Code Optimizer Literal Table Symbol Error Handler Source code Syntax Tree Annotated Tree Intermediate code Target code Tokens

The Scanner Lexical analysis: it collects sequences of characters into meaningful units called tokens An example: a[index]=4+2 a identifier [ left bracket index identifier ] right bracket = assignment 4 number + plus sign 2 number Other operations: it may enter literals into the literal table

The Parser Syntax analysis: it determines the structure of the program The results of syntax analysis are a parse tree or a syntax tree An example: a[index]=4+2 Parse tree Syntax tree ( abstract syntax tree)

The Parse Tree a[index]=4+2 expression Assign-expression identifier identifier number number Expression [ expression ] expression + expression subscript-expression additive-expression expression = expression Assign-expression expression a[index]=4+2

subscript-expression additive-expression The Syntax Tree a[index]=4+2 a index 4 2 identifier identifier number number subscript-expression additive-expression Assign-expression

The Semantic Analyzer The semantics of a program are its “meaning”, as opposed to its syntax, or structure It determines some of its running time behaviors prior to execution Static Semantics: declarations and type checking Attributes: The extra pieces of information computed by semantic analyzer An example: a[index]=4=2 The syntax tree annotated with attributes

The Annotated Syntax Tree a index 4 2 identifier identifier number number subscript-expression additive-expression integer integer Assign-expression array of integer integer integer integer

The Source Code Optimizer The earliest point of most optimization steps is just after semantic analysis The code improvement depends only on the source code, and as a separate phase Individual compilers exhibit a wide variation in optimization kinds as well as placement An example: a[index]=4+2 Constant folding performed directly on annotated tree Using intermediate code: three-address code, p-code

Annotated Tree Without Optimization a index 4 2 identifier identifier number number subscript-expression additive-expression integer integer Assign-expression array of integer integer integer integer

Optimized Annotated Tree a index 6 identifier identifier number subscript-expression integer Assign-expression array of integer integer integer

Optimization on Intermediate Code a[index]=t t = 6 a[index]=6

The Code Generator It takes the intermediate code or IR and generates code for target machine The properties of the target machine become the major factor: Using instructions that exist on target machines How to make representation of data i.e. how many bytes and words will be allocated to a give type of a variable

The Target Code Optimizer It improves the target code generated by the code generator: By choosing addressing modes to increase performance Replacing slow instructions with faster ones eliminating redundant operations

Principal Data Structures Used in Compilers TOKENS A scanner collects characters into a token, as a value of an enumerated data type for tokens May also preserve the string of characters or other derived information, such as name of identifier, value of a number A single global variable or an array of tokens THE SYNTAX TREE A standard pointer-based structure generated by parser Each node represents information collected by parser, which maybe dynamically allocated in symbol table The node requires different attributes depending on kind of language structure, which may be represented as variable record

Principal Data Structures Used in Compilers (Continue…) THE SYMBOL TABLE Keeps information associated with identifiers: function, variable, constants, and data types Interacts with almost every phase of compiler Access operation need to be constant-time One or several hash tables are often used THE LITERAL TABLE Stores constants and strings, reducing size of program Quick insertion and lookup are essential If a literal is used more than once (as they often are in a program), we still want to store it only once

Principal Data Structures Used in Compilers (Continue…) INTERMEDIATE CODE Kept as an array of text string, a temporary text, or a linked list of structures, depending on kind of intermediate code (e.g. three-address code and p-code) Should be easy for reorganization TEMPORARY FILES Holds the product of intermediate steps during compiling Solve the problem of memory constraints or back-patch addressed during code generation

Error Handling Static (or compile-time) errors must be reported by a compiler Generate meaningful error messages and resume compilation after each error Each phase of a compiler needs different kind of error handing Must handle a wide range of errors Must handle multiple errors. Must not get stuck. Must not get into an infinite loop

Error Handling (Continue…) Exception handling Generate extra code to perform suitable runtime tests to guarantee all such errors to cause an appropriate event during execution

Summary Any Questions?