Compilers Principles, Techniques, & Tools Taught by Jing Zhang

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

CS 31003: Compilers Introduction to Phases of Compiler.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation.
CPSC Compiler Tutorial 9 Review of Compiler.
Programming Languages Structure
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.
CPSC 388 – Compiler Design and Construction Lecture: MWF 11:00am-12:20pm, Room 106 Colton.
CIS Computer Programming Logic
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.
CSC 338: Compiler design and implementation
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.
CST320 - Lec 11 Why study compilers? n n Ties lots of things you know together: –Theory (finite automata, grammars) –Data structures –Modularization –Utilization.
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 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.
IN LINE FUNCTION AND MACRO Macro is processed at precompilation time. An Inline function is processed at compilation time. Example : let us consider this.
Programming Languages
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
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.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
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.
Programming Languages Concepts Chapter 1: Programming Languages Concepts Lecture # 4.
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 Samuel College of Computer Science & Technology Harbin Engineering University.
Chapter 1. Introduction.
Lecture 9 Symbol Table and Attributed Grammars
System Software Theory (5KS03).
Advanced Computer Systems
Component 1.6.
Compiler Design (40-414) Main Text Book:
PRINCIPLES OF COMPILER DESIGN
Chapter 1 Introduction.
Introduction to Compiler Construction
PROGRAMMING LANGUAGES
A Simple Syntax-Directed Translator
Constructing Precedence Table
Compiler Construction (CS-636)
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Ch. 7 Programming Languages
Chapter 1 Introduction.
课程名 编译原理 Compiling Techniques
Chapter 1: Introduction to Compiling (Cont.)
Compiler Lecture 1 CS510.
Compiler Construction
Course supervisor: Lubna Siddiqui
Compiler Construction
Compiler 薛智文 TH 6 7 8, DTH Spring.
CSE401 Introduction to Compiler Construction
COP4020 Programming Languages
Compiler 薛智文 TH 6 7 8, DTH Spring.
Compiler design.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler 薛智文 M 2 3 4, DTH Spring.
Presentation transcript:

Compilers Principles, Techniques, & Tools Taught by Jing Zhang (jzhang@njust.edu.cn)

About the Course Text book: Compilers – Principles, Techniques, & Tools The 8th – 16th Week (9 weeks) (Twice / week, on Monday morning and Wednesday afternoon) Final Examination: 17th week Final Grade: 20% attendance + 20% homework + 60% final exam Text book download: http://jz81.github.io/course/com piler/Compilers- Principes%20Techniques%20and %20Tools.pdf

Introduction

1.1 Language Processors-compiler What is a compiler? All the software running on all the computers was written in some program language. Before a program can be run, it first must be translated into a form in which it can be executed by a computer. The software system that do this translation are called compiler. Simply stated, a compiler is a program that can read a program in one language – the source language – and translate it into an equivalent program in another language – the target language. Source Program Compiler Target Program input Target Program output

1.1 Language Processors-interpreter An interpreter is another common kind of language processor. It directly execute the operations specified in the source program on inputs supplied by the user A hybrid compiler Source Program Compiler output input Source Program Translator Intermediate program Target Program output input

1.1 Language Processors-system Several other programs may be required to create an executable target program. Preprocessor Assembler Linker/Loader Questions What is a linker? What is a Loader?

1.2 The Structure of a Compiler

1.2 The Structure of a Compiler-Lexical Analysis Lexical Analysis (Scanning) Reads the stream of characters making up the source program and groups the characters into meaningful sequences called lexemes. For each lexeme, the lexical analyzer produces as output a token of the form E.g. Position = initial + rate * 60 <id, 1> <=> <id, 2> <+> <id,3> <*> <60> <number, 0xFFFF1230>

1.2 The Structure of a Compiler-Syntax Analysis Syntax Analysis (parsing) uses the first components of the tokens produced by the lexical analyzer to create a tree-like intermediate representation that depicts the grammatical structure of the token stream.

1.2 The Structure of a Compiler-Semantic Analysis The semantic analysis uses the syntax tree and the information in the symbol table to check the source program for semantic consistency with the language definition. It also gathers type information and saves it in either the syntax tree or the symbol table, for subsequent use during intermediate-code generation. Type checking

1.2 The Structure of a Compiler-Intermediate Code Generation After syntax and semantic analysis of the source program, many compilers generate an explicit low-level or machine- like intermediate representation, which we can think of as a program for an abstract machine. Two important properties: it should be easy to produce it should be easy to translate into the target machine Three-address code t1 = inttofloat(60) t2 = id3 * t1 t3 = id2 + t2 id1 = t3

1.2 The Structure of a Compiler-Code Optimization The machine-independent code-optimization phase attempts to improve the intermediate code so that better target code will result. t1 = inttofloat(60) t2 = id3 * t1 t3 = id2 + t2 id1 = t3 t1 = id3 * 60.0 id1 = id2 + t1

1.2 The Structure of a Compiler- Code Generation The code generator takes as input an intermediate representation of the source program and maps it into the target language. If the target language is machine code, registers or memory locations are selected for each of the variables used by the program. Then, the intermediate instructions are translated into sequences of machine instructions that perform the same task. judicious assignment of registers to hold variables LDF R2, id3 MULF R2, R2, #60.0 LDF R1, id2 ADDF R1, R1, R2 STF id, R1 t1 = id3 * 60.0 id1 = id2 + t1

1.2 The Structure of a Compiler Symbol-Table Management Compiler-Construction Tools Lex Yacc …..

1.3 Evolution of Programming Languages First-generation language Machine language Early 1950’s, Second-generation language - mnemonic assembly language Mnemonic representations of machine instructions Latter half of the 1950’s, Third-generation language – higher- level language Fortran (scientific computation) Cobol (business data processing) Lisp (symbolic computation) Object-oriented language (C++, Java) Fourth-generation language (for specific application) SQL for database, Postscript for text formatting Fifth-generation Logic- and constraint-based languages, Prolog

Some Classifications of Programming Languages Imperative language A program specifies how a computation is to be done C, C++, C#, Java Declarative language A program specifies what computation is to be done Functional language such as ML and Haskell Object-oriented language is one that support object- oriented programming Scripting languages are interpreted languages with high- level operators designed for “gluing together” computations. Awk, JavaScript, Perl, PHP, Python, Ruby and Tcl

1.6 Programming Language Basic Data Type and Operation Numeric data type: integer, real number (float and double), complex (Operations, + - * \ %) Logic data type: Boolean (Operations: and, or not) Character Pointer Data structure Array Record (struct) Abstract data structure Class Statement and control structure Expression Assignment statement Control statement If then else For Do while switch

1.6 Programming Language Basic The static/Dynamic Distinction Static policy: the issue can be decided at compile time; Dynamic policy: the issue only can be decided at run time. Understand keyword static in some programming language Environments and States The environment is a mapping form names to locations in the store. Variables refer to locations (“l-values” in the terminology of C) The state is a mapping from locations in store to their values. r-values

1.6 Programming Language Basic The static/Dynamic Distinction Static policy: the issue can be decided at compile time; Dynamic policy: the issue only can be decided at run time. Understand keyword static in some programming language Environments and States The environment is a mapping form names to locations in the store. Variables refer to locations (“l-values” in the terminology of C) The state is a mapping from locations in store to their values. r-values

1.6 Programming Language Basic Main() { int a = 1; int b = 1; { int b = 2; int a = 3; cout<<a<<b; } int b = 4; cout << a << b; cout << a<<b;

1.6 Programming Language Basic Names, Identifiers and Variables Name and variable refer to the same thing We use name in compile-time and use variable in run-time Identifier is a string of characters, typically letters or digits, that refers to (identifies) an entity, such as a data object, a procedure, a class, or a type. All identifiers are names, but not all names are identifiers. Names can also be expressions. For example, the name x.y might denote the field y of a structure denoted by x. Here, x and y are identifiers, while x.y is a name, but not an identifier. Static Scope and Block Structure The scope rules for C are based on program structure; the scope of a declaration is determined implicitly by where the declaration appears in the program. Later languages, such as C++, Java, and C#, also provide explicit control over scopes through the use of keywords like public, private, and protected

1.6 Programming Language Basic Static-scope rules for a language with blocks, where block is a grouping of declaration and statement Declarations and Definitions Declaration tells us the type of things Definition tell us about their values.

1.6 Programming Language Basic - Parameter Passing Mechanisms How the actual parameters (the parameters used in the call of a procedure) are associated with the formal parameters (those used in the procedure definition). Call by Value: In call-by-value, the actual parameter is evaluated (if it is an expression) or copied (if it is a variable) . Call by Reference: In call-by-reference, the address of the actual parameter is passed to the callee as the value of the corresponding formal parameter. Call by Name the actual parameter were substituted literally for the formal parameter in the code of the callee, as if the formal parameter were a macro standing for the actual parameter

Others The generation of a compiler Directly implementation Write a compiler for L2 language running on Machine A using L1 language and its compiler running on Machine A Transplant of a compiler Machine A -> Machine B

Homework Illustrate the phases of a compile. What is the main function for each phase? What are the differences among name, identifier and variable?   What are the differences between declaration and definition? Briefly explain the parameter passing mechanisms “call-by- value” and “call-by-reference”.