Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.

Slides:



Advertisements
Similar presentations
Yu-Chen Kuo1 Chapter 1 Introduction to Compiling.
Advertisements

Compiler Construction1 A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University First Semester 2009/2010.
Compiler design Computer Science Rensselaer Polytechnic Lecture 1.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Course Revision Contents  Compilers  Compilers Vs Interpreters  Structure of Compiler  Compilation Phases  Compiler Construction Tools  A Simple.
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 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.
1 Programming Languages Tevfik Koşar Lecture - II January 19 th, 2006.
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.
Parse & Syntax Trees Syntax & Semantic Errors Mini-Lecture.
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.
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.
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.
COMPILER CONSTRUCTION
Introduction CPSC 388 Ellen Walker Hiram College.
Chapter 1 Introduction Major Data Structures in Compiler
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.
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.
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.
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,
Prologue Sung-Dong Kim, Dept. of Computer Engineering, Hansung 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.
CS510 Compiler Lecture 1.
Introduction to Compiler Construction
Chapter 6 Compiler.
Compiler Construction (CS-636)
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
Introduction CI612 Compiler Design CI612 Compiler Design.
Compiler Construction
Compiler 薛智文 TH 6 7 8, DTH Spring.
Compilers B V Sai Aravind (11CS10008).
Compiler 薛智文 TH 6 7 8, DTH Spring.
Introduction to Compiler Construction
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 10: Compilers and Language Translation
Compiler 薛智文 M 2 3 4, DTH Spring.
Introduction to Compiler Construction
Presentation transcript:

Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University

Compilers Compilers are computer programs that translate one language to another. –Very complex program from 10,000 to 1,000,000 lines of code. Its input is a program written in its source language. It produces an equivalent program written in its target language.

Translation Process This is a book. Step 1: lexical analysis This / is / a / book /. Step 2: syntax analysis This  subject is  predicate a  quantifier book  object.  end Step 3: semantic analysis This  pronoun 这 is  copula 是 a  numeral 一 book  noun 书.  period 。 Step 4: This is a book. Step 5: 这是一书。 Step 6: 这是一本书。

Translation Process ScannerParser Semantic Analyzer Source Code Optimizer Code Generator Target Code Optimizer Source Code Literal Table Symbol Table Error Handler Target Code

Translation Process ScannerParser Semantic Analyzer Source Code Optimizer Code Generator Target Code Optimizer Source Code Literal Table Symbol Table Error Handler 1

The Scanner Reads the source program (stream of characters). Performs lexical analysis: collects sequences of characters into meaningful units called tokens. Example: a[index] = a identifier [ left bracket index identifier ] right bracket = assignment 4 number + plus sign 2 number

Translation Process ScannerParser Semantic Analyzer Source Code Optimizer Code Generator Target Code Optimizer Source Code Tokens Literal Table Symbol Table Error Handler 2

The Parser Receives the source in form of tokens. Performs syntax analysis – determines the structure of the program – similar to performing grammatical analysis on a sentence in natural language. The result is represented as a parse tree or a syntax tree.

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

Abstract Syntax Tree An abstract syntax tree is a condensation of the information contained in a parse tree. assign-expression subscript-expression additive-expression identifier a identifier index number 4 number 2 a[index] = 4 + 2

Translation Process ScannerParser Semantic Analyzer Source Code Optimizer Code Generator Target Code Optimizer Source Code Tokens Syntax Tree Literal Table Symbol Table Error Handler 3

The Semantic Analyzer The semantics of a program are its “meaning”. The semantics of a program determine its runtime behavior. Most programming languages have features (called static semantics) that can be determined prior to execution. Typical static semantics features –Declarations –Type checking The extra information computed by the semantic analyzer are called attributes. –They are added to the tree as annotations, or “decorations”

Abstract Syntax Tree An abstract syntax tree is a condensation of the information contained in a parse tree. assign-expression subscript-expression additive-expression identifier a identifier index number 4 number 2 a[index] = 4 + 2

assign-expression subscript-expression additive-expression identifier a identifier index number 4 number 2 a[index] = Abstract Syntax Tree

Annotated Tree assign-expression subscript-expression integer additive-expression integer identifier a array of integer identifier index integer number 4 integer number 2 integer a[index] = 4 + 2

Translation Process ScannerParser Semantic Analyzer Source Code Optimizer Code Generator Target Code Optimizer Source Code Tokens Syntax Tree Annotated Tree Literal Table Symbol Table Error Handler 4

The Source Code Optimizer The earliest point at which optimization steps can be performed is just after semantic analysis. There may be possibilities that depend only on the source code. Compilers exhibit a wide variation in the kind of optimization and its placement. The output of the source code optimizer is the intermediate representation (IR) or intermediate code.

Example can be precomputed by the compiler. –This optimization is known as constant folding. –This optimization can be performed on the annotated syntax tree by collapsing the right hand subtree to its constant value. assign-expression subscript-expression integer additive-expression integer identifier a array of integer identifier index integer number 4 integer number 2 integer

Example can be precomputed by the compiler. –This optimization is known as constant folding. –This optimization can be performed on the annotated syntax tree by collapsing the right hand subtree to its constant value. assign-expression subscript-expression integer number 6 integer identifier a array of integer identifier index integer

Translation Process ScannerParser Semantic Analyzer Source Code Optimizer Code Generator Target Code Optimizer Source Code Tokens Syntax Tree Annotated Tree Intermediate code Literal Table Symbol Table Error Handler 5

The Code Generator The code generator takes the intermediate code or IR and generates code for the target machine. –We will write target code in assembly language form. Most compilers generate object code directly. The properties of the target machine become important. –Use instructions of the target machine. –Data representations: how many bytes or words integer and floating-point data types occupy in memory.

Example &a is the address of a (the base address of the array) *R1 means indirect register addressing We assumed that the machine performs byte addressing. Integers occupy two bytes of memory. MOV R0, index ;; value of index -> R0 MUL R0, 2 ;; double value in R0 MOV R1, &a ;; address of a ->R1 ADD R1, R0 ;; add R0 to R1 MOV *R1, 6 ;; constant 6 -> address in R1 a[index] = a[index] = 6

Translation Process ScannerParser Semantic Analyzer Source Code Optimizer Code Generator Target Code Optimizer Source Code Tokens Syntax Tree Annotated Tree Intermediate code Target Code Literal Table Symbol Table Error Handler 6

The Target Code Optimizer Improvements include –Choosing addressing modes to improve performance. –Replacing slow instructions by faster ones. –Eliminating redundant or unnecessary operations Example: MOV R0, index ;; value of index -> R0 SHL R0 ;; double the value in R0 MOV &a[R0], 6 ;; constant 6 -> address a+R0 MOV R0, index ;; value of index -> R0 MUL R0, 2 ;; double value in R0 MOV R1, &a ;; address of a ->R1 ADD R1, R0 ;; add R0 to R1 MOV *R1, 6 ;; constant 6 -> address in R1 a[index] = 6

Translation Process ScannerParser Semantic Analyzer Source Code Optimizer Code Generator Target Code Optimizer Source Code Target Code Tokens Syntax Tree Annotated Tree Intermediate code Target Code Literal Table Symbol Table Error Handler

Interpreters An interpreter is a language translator like a compiler. The difference: the source program is executed immediately, not after translation is complete. Programming language can be either interpreted or compiled. Interpreted languages: BASIC, LISP, Java Compiled languages: FORTRAN, C, C++. Interpreters share many operations with compilers.

Assemblers An assembler is a translator for the assembly language of a particular computer. Assembly language is a symbolic form of the machine language and it is easy to translate. Sometimes, a compiler will generate assembly language as its target language. Then assembler will finish the translation into object code.

Linkers A linker collects code separately compiled or assembled in different object files into final executable file. Also connects to the code for standard library functions and to resources supplied by OS (memory allocators, I/O devices) A linker was originally one of the principal activities of a compiler.

Loaders In object code the primary memory references are made relative to an undetermined starting location that can be anywhere in memory. Loader will resolve all relocateable addresses to a given starting address. Usually, the loading process is part of OS.

Preprocessors A preprocessor is a separate program that is called by the compiler before the translation begins. Preprocessors can –Delete comments –Include other files –Perform macro substitutions A macro is a shorthand description of a repeated sequence of text

Editors Source programs are written using an editor that produces a standard file (ASCII). Recently, compilers have been bundled with editors and other programs into an interactive development environment (IDE). Such editors may be oriented towards the format of programming language. –Programmer may be informed of errors as the program is written. The compiler can be called from within the editor.

Debuggers A debugger is a program that determines execution errors in a compiled program. –It is also packaged in IDE. The debugger keeps track of the source code information such as line numbers, names of variables and procedures. It can halt execution at breakpoint and provide information on called functions and current values of variables.

Homework 1.2 Given the C assignment a[i+1] = a[i] + 2 draw a parse tree and a syntax tree for the expression, using the similar example as a guide.