Compilers Design Chapter1 : Introduction, Structure of a Compiler

Slides:



Advertisements
Similar presentations
Introduction to Compiler Construction
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.
BİL744 Derleyici Gerçekleştirimi (Compiler Design)1.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
CCSA 221 Programming in C CHAPTER 2 SOME FUNDAMENTALS 1 ALHANOUF ALAMR.
CPSC 388 – Compiler Design and Construction Lecture: MWF 11:00am-12:20pm, Room 106 Colton.
September 7, September 7, 2015September 7, 2015September 7, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
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.
Introduction to Compiler Construction Robert van Engelen COP5621 Compiler Construction Copyright Robert.
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.
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.
410/510 1 of 31 Week 1 – Lecture 1 Introduction The Textbook Assessment Overview Compiler Construction.
1 Chapter 1 Introduction. 2 Outlines 1.1 Overview and History 1.2 What Do Compilers Do? 1.3 The Structure of a Compiler 1.4 The Syntax and Semantics of.
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.
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.
Chapter 1 Introduction Major Data Structures in Compiler
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,
Compiler Construction Composed By, Muhammad Bilal Qureshi.
 Fall Chart 2  Translators and Compilers  Textbook o Programming Language Processors in Java, Authors: David A. Watts & Deryck F. Brown, 2000,
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.
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.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
Presented by : A best website designer company. Chapter 1 Introduction Prof Chung. 1.
CS416 Compiler Design1. 2 Course Information Instructor : Dr. Ilyas Cicekli –Office: EA504, –Phone: , – Course Web.
Prologue Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
Objective of the course Understanding the fundamentals of the compilation technique Assist you in writing you own compiler (or any part of 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
Compiler Construction (CS-636)
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
Introduction to Compiler Construction
Course supervisor: Lubna Siddiqui
Introduction CI612 Compiler Design CI612 Compiler Design.
Compiler Construction
Compiler 薛智文 TH 6 7 8, DTH Spring.
CS416 Compiler Design lec00-outline February 23, 2019
Introduction to Compiler Construction
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Introduction to Compiler Construction
Presentation transcript:

Compilers Design Chapter1 : Introduction, Structure of a Compiler

Textbook Compilers Principles, Techniques, and Tools, Alfred V.Aho, Ravi Sethi, and Jeffrey D. Ullman, Addison-Wesley References Programming Language Processors in Java. Compilers and Interpreters, D.A. Watt and D.F. Brown, Pearson Education Ltd.

Objectives * To introduce principles, techniques, and tools for compiler construction * To obtaining the knowledge what a compiler does and how to build one.

Course Outline 1. Introduction, Structure of a Compiler 2. Lexical Analysis: Tokens, Regular Expressions 3. Parsing: Context-free grammars, predictive 4. Abstract Syntax: Semantic actions, abstract parse trees 5. Semantic Analysis: Symbol tables, bindings, type-checking 6. Stack Frames: Representation and Abstraction 7. Intermediate Code: Representation trees, translation 8. Basic Blocks: Canonical trees, conditional branches 9. Instruction Selection: Algorithms for selection 10. Liveness Analysis: Solution of dataflow equations 11. Register Allocation: Spilling

Why we need to know compilers? Seeing the development of a compiler gives you a feeling for how programs work. A great example of interplay between theory and practice. Many algorithms and models you will use in compilers are fundamental, and will be useful to you elsewhere: automata, regular expressions (lexing) context-free grammars, trees (parsing) hash tables (symbol table) dynamic programming, graph coloring (code gen.)

Computer Organization Applications Compiler Operating System Hardware

History, Programming Languages Machine coding (binary programming – punch holes) (first generation) The computer’s ‘native language’, binary digits (0s, 1s) 0100 0001 0110 1110 0100 0001 0001 0010 1100 0100 0000 1101 Assembly Language (second generation) One-to-one correspondence to machine language MOV AX, 5h MOV DX, 3h ADD AX Assembler: translates assembly language programs into machine language

C, C++, Java, Fortran, QuickBasic History, Programming Languages (High-Level Languages) Procedural Languages (third generation) * Instructions translate into machine language instructions * Uses common words rather than abbreviated mnemonics C, C++, Java, Fortran, QuickBasic A= 3 B= A * 2 - 1 D= A / B + A^5 Compiler - translates the entire program at once Interpreter - translates and executes one source program statement at a time

Sturtured Query Language (SQL) History, Programming Languages (High-Level Languages) Nonprocedural Languages (fourth generation) Allows the user to specify the desired result without having to specify the detailed procedures needed for achieving the result Sturtured Query Language (SQL) Natural Language Programming Languages (fifth generation (intelligent) languages) Translates natural languages into a structured, machine-readable form

High-Level Languages Expressions: such as +, -, *, / Data Types: simple types (e.g. Boolean, int, float) as well as composite structures (records) and arrays - can be defined by the programmer Control Structures: allow programming of selective computation as well as iterative computation Declaration: introduce identifiers to indicate const. Values, variables, procedures etc. Abstraction: separation of concerns i.e. break a problem up and deal with sub-sets Encapsulation: (data abstraction) grouping relevant relations and selectively hiding specific information (e.g. classes)

Language Processors Editors: to enter text Translator: translates text from one language to another Compiler: translates from a high-level language to low-level language Interpreter: takes a text (in a particular language) and runs it immediately Assembler: translates from an assembly language into the corresponding machine code. Programming language (source code) Translator program (Assembler, Compiler, or Interpreter) Machine language (object code)

What is a Compiler? A compiler is program that reads a program written in one language (source language) and translates it into an equivalent program in another language (target language) Compiler source program target program error messages Page 1

Compiler Source programs: Many possible source languages, from traditional, to application specific languages. Programming languages (High-level) Modeling languages Document description languages Database query languages Target programs: Another programming language, often the machine language of a particular computer system High-level programming language Low-level programming language (assembler or machine code) Application-specific target language Error messages: Essential for program development

compilers are used to do this conversion Do we need Compilers? * Machines understand only 1’s and 0’s. High-level languages, make it easier for the user to program in, but not for the machine to understand. * Once the programmer has written and edited the program (in an Editor), it needs to be translated into machine language (1’s and 0’s) before it can be executed. compilers are used to do this conversion

Where are compilers used? Implementation of programming languages C, C++, Java, Lisp, Prolog, SML, Haskell, Ada, Fortran Document processing DVI  PostScript, Word documents  PDF Natural language processing NL  database query language  database commands Hardware design silicon compilers, CAD data  machine operations, equipment lists Report generation CAD data  list of parts, All kinds of input/output translations various UNIX text filters, . . .

Compilers & Interpreters * Interpreters are another class of translators * Compiler: translates a program once and for all into target language. C++ * Interpreter: effectively translates a source program every time it is run. Basic * Compilers and interpreters (highbred) are used together Java  Java compiled into Java byte code,  byte code interpreted by a Java Virtual Machine (JVM).

Compiler / Translator and Interpreter * A translator is used to produce an “equivalent” program in another language (e.g. from C to Pascal) * Compiler is a translator that generally takes in a higher level language (e.g. C) and transforms it into a low level language (usually object or machine code). * Compiler/Translator produce the entire output code before executing * Interpreter compiles and executes a statement at a time before moving on to the next statement

Analysis-Synthesis Model of Compilation There are two parts of compilation Part1, Analysis: breaks up the source program into constituent pieces and creates an intermediate representation of the source program. Part2, Synthesis: constructs the desired target program from the intermediate representation. It requires the most specialized techniques Page 2

The phases of a compiler Source Program Target Program Semantic Analyser Intermediate Code Generator Code Optimizer Code Generator Syntax Analyser Lexical Analyser Symbol Table Manager Error Handler The phases of a compiler

Intermediate Code Generator Source Program Target Program Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator Syntax Analyzer Lexical Analyzer Symbol Table Manager Error Handler Decompose statements into tokens Detects errors, Reports errors Parsing, check order of tokens with grammar, create Abstract Syntax Tree Type checking, identify operators & operands Stores record for each identifier and its attributes First translation & create temp. sub-result variables Improve speed, efficiency Generates final assembly code

Part1, Analysis of the Source Program Analysis consists of three phases: Lexical (Linear or Scanning): reads from left-to-right and grouped into tokens that are sequences of characters having a collective meaning. Syntax Analysis (Hierarchical or Parsing): 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. Page 5

Lexical Analysis (Linear Analysis/ Scanning) Translate the input program, entered as a sequence of characters, into a sequence of words or symbols (tokens). For example, the keyword for should be treated as a single entity, not as a 3 character string. position := initial + rate * 60 The assignment statement would be grouped into the following tokens 1. The identifier position 2. The assignment symbol := 3. The identifier initial 4. The plus sign + 5. The identifier rate 6. The multiplication sign * 7. The number 60 Note: the blank separating the characters of these tokens would normally be eliminated during lexical analysis

Syntax Analysis (Hierarchical Analysis or Parsing) It involves grouping the tokens of the source program into grammatical phrases that are used by the compiler to synthesize output. Usually, the grammatical phrases of the source program are represented by a parse tree such as the following slide: Determine the structure of the program, for example, identify the components of each statement and expression and check for syntax errors.

position := initial + rate * 60 Syntax Tree assignment statement identifier expression number rate initial position := 60 * + Parse Tree

Semantic Analysis * Checks the source program for semantic errors and gathers type information for subsequent code generation phase * It uses the hierarchy structure determined by the syntax-analysis phase * Check that the program is reasonable, for example, that it does not include references to undefined variables. * An important component of semantic analysis is type checking position initial rate := + * 60 inttoreal

Part2, Synthesis Intermediate Code Generation: as a program for an abstract machine. It should be easy to produce and easy to translate into the target program. Code Optimization: attempts to improve the intermediate code. The program can be fixed during the code optimization phase. Code Generation: memory locations are selected for each of the variables used by the program. Intermediate instructions are each translated into a sequence of machine instructions that perform the same task. A crucial aspect is the assignment of variables to registers.

Intermediate Code Generator temp1 := inttoreal (60) temp2 := rate * temp1 temp3 := initial + temp2 position := temp3 Code Optimizer temp1 := rate * 60.0 position := initial + temp1 Code Generator MOVF rate, R2 MULF #60, R2 MOVF initial, R1 ADDF R2, R1 MOVF R1, position

Symbol table Error handler A symbol table is a data structure containing a record for each identifier, with fields for the attributes of the identifier. The data structure allows us to find the record for each identifier quickly and to store or retrieve data from that record quickly. Error handler Each phase can encounter errors. However, after detecting an error, a phase must somehow deal with that error, so that compilation can proceed, allowing further errors in the source program to be deducted.

Inside the Compiler sequence of characters scanner sequence of tokens parser checker optimizer/ code generator Lexical Analysis sequence of tokens Syntactic Analysis/ Parsing Abstract Syntax Tree (AST) Contextual Analysis/ checking Static Semantics verified/ annotated AST Optimization and Code Generation target code

Language Processing System skeletal source program Pre-processor source program Compiler target assembly program Assembler Re-locatable machine code Loader/Linker

Language Processing System Converts mnemonics (assembly code) into object code. Two- pass assembler : 1. denote storage locations for identifiers in symbol table 2. translate code into machine code, translate locations into addresses skeletal source program Performs: Macro-processing, File inclusion, “Rational” reprocessor, Language extension Pre-processor source program Split into 6 phases. Produces assembly code. Some compilers include the assembler too. Compiler target assembly program Assembler Links other object & library files with object code Reads file, placing relocatable addresses into proper locations in memory Re-locatable machine code Loader/Linker