Introduction.

Slides:



Advertisements
Similar presentations
Introduction to Compiler Construction
Advertisements

CPSC Compiler Tutorial 9 Review of Compiler.
Yu-Chen Kuo1 Chapter 1 Introduction to Compiling.
Compiler Chang Chi-Chung Textbook Compilers: Principles, Techniques, and Tools, 2/E.  Alfred V. Aho, Columbia University  Monica S. Lam,
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.
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.
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.
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.
Chapter : Introduction1 Introduction Supplement: Cross compiler, History.
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.
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
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
Introduction CPSC 388 Ellen Walker Hiram College.
Chapter 1 Introduction Major Data Structures in Compiler
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
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.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
CSC 4181 Compiler Construction
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
CSC 8505 Compiler Construction
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.
CS416 Compiler Design1. 2 Course Information Instructor : Dr. Ilyas Cicekli –Office: EA504, –Phone: , – Course Web.
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
CHAPTER 1 INTRODUCTION TO COMPILER SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Chapter 1. Introduction.
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 3304 Comparative Languages
Compiler Construction (CS-636)
Short introduction to 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
Introduction to Compiler Construction
Course supervisor: Lubna Siddiqui
Introduction CI612 Compiler Design CI612 Compiler Design.
Compilers B V Sai Aravind (11CS10008).
CS416 Compiler Design lec00-outline February 23, 2019
Introduction to Compiler Construction
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 1 Introduction.
Chapter 10: Compilers and Language Translation
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Introduction Chapter : Introduction.
Introduction to Compiler Construction
Presentation transcript:

Introduction

What is a Compiler? A compiler is a computer program that translates a program in a source language into an equivalent program in a target language. A source program/code is a program/code written in the source language, which is usually a high-level language. A target program/code is a program/code written in the target language, which often is a machine language or an intermediate code. compiler Source program Target program Error message

The Analysis-Synthesis Model of Compilation There are two parts to compilation: Analysis determines the operations implied by the source program which are recorded in a tree structure Synthesis takes the tree structure and translates the operations therein into the target program Intermediate representation Object code Input C program Analysis of the input program Synthesis of the intermediate program

The Grouping of Phases Compiler front and back ends: Compiler passes: Front end: analysis (machine independent) Back end: synthesis (machine dependent) Compiler passes: A collection of phases is done only once (single pass) or multiple times (multi pass) Single pass: usually requires everything to be defined before being used in source program Multi pass: compiler may have to keep entire program representation in memory

Process of Compiling scanner parser Semantic analyzer Stream of characters scanner Stream of tokens parser Parse/syntax tree Semantic analyzer Annotated tree Intermediate code generator Intermediate code Code optimization Intermediate code Code generator Target code Code optimization Target code

Programmer (source code producer) Source string A=B+C; Phase Output Sample Programmer (source code producer) Source string A=B+C; Scanner (performs lexical analysis) Token string ‘A’, ‘=’, ‘B’, ‘+’, ‘C’, ‘;’ And symbol table with names Parser (performs syntax analysis based on the grammar of the programming language) Parse tree or abstract syntax tree ; | = / \ A + / \ B C Semantic analyzer (type checking, etc) Annotated parse tree or abstract syntax tree Intermediate code generator Three-address code, quads, or RTL Int2fp B t1 + t1 C t2 := t2 A Optimizer int2fp B t1 + t1 #2.3 A Code generator Assembly code MOVF #2.3,r1 ADDF2 r1,r2 MOVF r2,A Peephole optimizer ADDF2 #2.3,r2 MOVF r2,A

Preprocessors, Compilers, Assemblers, and Linkers Skeletal Source Program Preprocessor Source Program Try for example: gcc -v myprog.c Compiler Target Assembly Program Assembler Relocatable Object Code Libraries and Relocatable Object Files Linker Absolute Machine Code

Symbol table Literal table Parse tree Some Data Structures Symbol table Literal table Parse tree Chapter 1

Symbol Table Identifiers are names of variables, constants, functions, data types, etc. Store information associated with identifiers Information associated with different types of identifiers can be different Information associated with variables are name, type, address,size (for array), etc. Information associated with functions are name,type of return value, parameters, address, etc. Chapter 1

Symbol Table (cont’d) Accessed in every phase of compilers The scanner, parser, and semantic analyzer put names of identifiers in symbol table. The semantic analyzer stores more information (e.g. data types) in the table. The intermediate code generator, code optimizer and code generator use information in symbol table to generate appropriate code. Mostly use hash table for efficiency. Chapter 1

Literal table Store constants and strings used in program reduce the memory size by reusing constants and strings Can be combined with symbol table Chapter 1

Scanning A scanner reads a stream of characters and puts them together into some meaningful (with respect to the source language) units called tokens. It produces a stream of tokens for the next phase of compiler. Chapter 1

Parsing A parser gets a stream of tokens from the scanner, and determines if the syntax (structure) of the program is correct according to the (context-free) grammar of the source language. Then, it produces a data structure, called a parse tree or an abstract syntax tree, which describes the syntactic structure of the program. Chapter 1

Semantic analysis It gets the parse tree from the parser together with information about some syntactic elements It determines if the semantics or meaning of the program is correct. This part deals with static semantic. semantic of programs that can be checked by reading off from the program only. syntax of the language which cannot be described in context-free grammar. Mostly, a semantic analyzer does type checking. It modifies the parse tree in order to get that (static) semantically correct code. Chapter 1

Intermediate code generation An intermediate code generator takes a parse tree from the semantic analyzer generates a program in the intermediate language. In some compilers, a source program is translated into an intermediate code first and then the intermediate code is translated into the target language. In other compilers, a source program is translated directly into the target language. Chapter 1

Intermediate code generation (cont’d) Using intermediate code is beneficial when compilers which translates a single source language to many target languages are required. The front-end of a compiler – scanner to intermediate code generator – can be used for every compilers. Different back-ends – code optimizer and code generator– is required for each target language. One of the popular intermediate code is three-address code. A three-address code instruction is in the form of x = y op z. Chapter 1

Code optimization Replacing an inefficient sequence of instructions with a better sequence of instructions. Sometimes called code improvement. Code optimization can be done: after semantic analyzing performed on a parse tree after intermediate code generation performed on a intermediate code after code generation performed on a target code Chapter 1

Code generation A code generator takes either an intermediate code or a parse tree produces a target program. Chapter 1

Error Handling Error can be found in every phase of compilation. Errors found during compilation are called static (or compile-time) errors. Errors found during execution are called dynamic (or run-time) errors Compilers need to detect, report, and recover from error found in source programs Error handlers are different in different phases of compiler. Chapter 1 2301373: Introduction

Parse tree Dynamically-allocated, pointer-based structure Information for different data types related to parse trees need to be stored somewhere. Nodes are variant records, storing information for different types of data Nodes store pointers to information stored in other data structure, e.g. symbol table Chapter 1

Cross Compiler a compiler which generates target code for a different machine from one on which the compiler runs. A host language is a language in which the compiler is written. T-diagram Cross compilers are used very often in practice. S T H Chapter 1 2301373: Introduction

Cross Compilers (cont’d) If we want a compiler from language A to language B on a machine with language E, write one with E write one with D if you have a compiler from D to E on some machine It is better than the former approach if D is a high-level language but E is a machine language write one from G to B with E if we have a compiler from A to G written in E A B E A B D D E ? A G G B E E Chapter 1 2301373: Introduction

Porting Porting: construct a compiler between a source and a target language using one host language from another host language A K A K A A A H A K H H A A K K H K Chapter 1 2301373: Introduction

Bootstrapping If we have to implement, from scratch, a compiler from a high-level language A to a machine, which is also a host, language, direct method bootstrapping A H H A H A1 A1 H A2 A2 H A3 A3 H H Chapter 1 2301373: Introduction

Linkers Loaders Interpreters Assemblers Cousins of Compilers Linkers Loaders Interpreters Assemblers Chapter 1 2301373: Introduction

History (1930’s -40’s) 1930’s John von Neumann invented the concept of stored-program computer. Alan Turing defined Turing machine and computability. 1940’s Many electro-mechanic, stored-program computers were constructed. ABC (Atanasoff Berry Computer) at Iowa Z1-4 (by Zuse) in Germany ENIAC (programmed by a plug board) Chapter 1 2301373: Introduction

History : 1950 Many electronic, stored-program computers were designed. EDVAC (by von Neumann) ACE (by Turing) Programs were written in machine languages. Later, programs are written in assembly languages instead. Assemblers translate symbolic code and memory address to machine code. John Backus developed FORTRAN (no recursive call) and FORTRAN compiler. Noam Chomsky studied structure of languages and classified them into classes called Chomsky hierarchy. 0A 1F 83 90 4B op code, address,.. LDI B, 4 LDI C, 3 LDI A, 0 ST: ADI A, C DEC B JNZ B, ST STO 0XF0, A Grammar Chapter 1 2301373: Introduction

Recursive-descent parsing was introduced. History (1960’s) Recursive-descent parsing was introduced. Nuar designed Algol60, Pascal’s ancestor, which allows recursive call. Backus-Nuar form (BNF) was used to described Algol60. LL(1) parsing was proposed by Lewis and Stearns. General LR parsing was invented by Knuth. SLR parsing was developed by DeRemer. Chapter 1 2301373: Introduction

LALR was develpoed by DeRemer. History (1970’s) LALR was develpoed by DeRemer. Aho and Ullman founded the theory of LR parsing techniques. Yacc (Yet Another Compiler Compiler) was developed by Johnson. Type inference was studied by Milner. Chapter 1 2301373: Introduction

Reading Assignment Louden, K.C., Compiler Construction: Principles and Practice, PWS Publishing, 1997. ->Chapter 1 Chapter 1 2301373: Introduction