COP4020 Programming Languages

Slides:



Advertisements
Similar presentations
Copyright © 2005 Elsevier Imperative languages Group languages as –imperative von Neumann(Fortran, Pascal, Basic, C) object-oriented(Smalltalk, Eiffel,
Advertisements

CPSC Compiler Tutorial 9 Review 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.
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.
ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
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.
1 Programming Languages Tevfik Koşar Lecture - II January 19 th, 2006.
Introduction to Compiler Construction Robert van Engelen COP5621 Compiler Construction Copyright Robert.
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.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
COP4020 Programming Languages Semantics Prof. Xin Yuan.
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.
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.
Introduction Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
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.
Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? , Semester 1,
. n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
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.
The Model of Compilation Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
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
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
©SoftMoore ConsultingSlide 1 Structure of Compilers.
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.
COP4020 Programming Languages Introduction Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
Presented by : A best website designer company. Chapter 1 Introduction Prof Chung. 1.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
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:
Introduction Chapter : Introduction.
PRINCIPLES OF COMPILER DESIGN
Chapter 1 Introduction.
Introduction to Compiler Construction
CS 326 Programming Languages, Concepts and Implementation
Introduction.
Chapter 1 Introduction.
课程名 编译原理 Compiling Techniques
COMPILER DESIGN UNIT-I.
Chapter 1: Introduction to Compiling (Cont.)
Compiler Construction
Introduction to Compiler Construction
Course supervisor: Lubna Siddiqui
Compilers B V Sai Aravind (11CS10008).
COP4020 Programming Languages
Introduction to Compiler Construction
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Introduction Chapter : Introduction.
Compiler Structures 1. Overview Objective
Introduction to Compiler Construction
Presentation transcript:

COP4020 Programming Languages Compiler phases Prof. Xin Yuan

Overview Compiler phases Lexical analysis Syntax analysis Semantic analysis Intermediate (machine-independent) code generation Intermediate code optimization Target (machine-dependent) code generation Target code optimization 4/22/2017 COP4020 Spring 2014 COP4020 Fall 2006

A typical compilation process Preprocessor Source program with macros A typical compilation process Preprocessor Source program Compiler Target assembly program Try g++ with –v, -E, -S flags on linprog. assembler Relocatable machine code linker Absolute machine code 4/22/2017 COP4020 Spring 2014

What is a compiler? Target Source program compiler program A program that reads a program written in one language (source language) and translates it into an equivalent program in another language (target language). Two components Understand the program (make sure it is correct) Rewrite the program in the target language. Traditionally, the source language is a high level language and the target language is a low level language (machine code). Target program Source program compiler Error message 4/22/2017 COP4020 Spring 2014

Compilation Phases and Passes Compilation of a program proceeds through a fixed series of phases Each phase use an (intermediate) form of the program produced by an earlier phase Subsequent phases operate on lower-level code representations Each phase may consist of a number of passes over the program representation Pascal, FORTRAN, C languages designed for one-pass compilation, which explains the need for function prototypes Single-pass compilers need less memory to operate Java and ADA are multi-pass 4/22/2017 COP4020 Spring 2014

Compiler Front- and Back-end Abstract syntax tree or other intermediate form Source program (character stream) Scanner (lexical analysis) Machine-Independent Code Improvement Tokens Parser (syntax analysis) Modified intermediate form Front end analysis Back end synthesis Target Code Generation Parse tree Semantic Analysis and Intermediate Code Generation Assembly or object code Machine-Specific Code Improvement Abstract syntax tree or other intermediate form Modified assembly or object code 4/22/2017 COP4020 Spring 2014

Scanner: Lexical Analysis Lexical analysis breaks up a program into tokens Grouping characters into non-separatable units (tokens) Changing a stream to characters to a stream of tokens program gcd (input, output); var i, j : integer; begin   read (i, j);   while i <> j do     if i > j then i := i - j else j := j - i;   writeln (i) end. program gcd ( input , output ) ; var i , j : integer ; begin read ( i , j ) ; while i <> j do if i > j then i := i - j else j := i - i ;  writeln ( i ) end . 4/22/2017 COP4020 Spring 2014

Scanner: Lexical Analysis What kind of errors can be reported by lexical analyzer? A = b + @3; 4/22/2017 COP4020 Spring 2014

Parser: Syntax Analysis Checks whether the token stream meets the grammatical specification of the language and generates the syntax tree. A syntax error is produced by the compiler when the program does not meet the grammatical specification. For grammatically correct program, this phase generates an internal representation that is easy to manipulate in later phases Typically a syntax tree (also called a parse tree). A grammar of a programming language is typically described by a context free grammer, which also defines the structure of the parse tree. 4/22/2017 COP4020 Spring 2014

Context-Free Grammars A context-free grammar defines the syntax of a programming language The syntax defines the syntactic categories for language constructs Statements Expressions Declarations Categories are subdivided into more detailed categories A Statement is a For-statement If-statement Assignment <statement> ::= <for-statement> | <if-statement> | <assignment> <for-statement> ::= for ( <expression> ; <expression> ; <expression> ) <statement> <assignment> ::= <identifier> := <expression> 4/22/2017 COP4020 Spring 2014

Example: Micro Pascal <Program> ::= program <id> ( <id> <More_ids> ) ; <Block> . <Block> ::= <Variables> begin <Stmt> <More_Stmts> end <More_ids> ::= , <id> <More_ids> |  <Variables> ::= var <id> <More_ids> : <Type> ; <More_Variables> |  <More_Variables> ::= <id> <More_ids> : <Type> ; <More_Variables> |  <Stmt> ::= <id> := <Exp> | if <Exp> then <Stmt> else <Stmt> | while <Exp> do <Stmt> | begin <Stmt> <More_Stmts> end <Exp> ::= <num> | <id> | <Exp> + <Exp> | <Exp> - <Exp> 4/22/2017 COP4020 Spring 2014

Parsing examples Pos = init + / rate * 60  id1 = id2 + / id3 * const  syntax error (exp ::= exp + exp cannot be reduced). Pos = init + rate * 60  id1 = id2 + id3 * const  := id1 + id2 * id3 60 4/22/2017 COP4020 Spring 2014

Semantic Analysis Semantic analysis is applied by a compiler to discover the meaning of a program by analyzing its parse tree or abstract syntax tree. A program without grammatical errors may not always be correct program. pos = init + rate * 60 What if pos is a class while init and rate are integers? This kind of errors cannot be found by the parser Semantic analysis finds this type of error and ensure that the program has a meaning. 4/22/2017 COP4020 Spring 2014

Semantic Analysis Static semantic checks (done by the compiler) are performed at compile time Type checking Every variable is declared before used Identifiers are used in appropriate contexts Check subroutine call arguments Check labels Dynamic semantic checks are performed at run time, and the compiler produces code that performs these checks Array subscript values are within bounds Arithmetic errors, e.g. division by zero Pointers are not dereferenced unless pointing to valid object A variable is used but hasn't been initialized When a check fails at run time, an exception is raised 4/22/2017 COP4020 Spring 2014

Semantic Analysis and Strong Typing A language is strongly typed "if (type) errors are always detected" Errors are either detected at compile time or at run time Examples of such errors are listed on previous slide Languages that are strongly typed are Ada, Java, ML, Haskell Languages that are not strongly typed are Fortran, Pascal, C/C++, Lisp Strong typing makes language safe and easier to use, but potentially slower because of dynamic semantic checks In some languages, most (type) errors are detected late at run time which is detrimental to reliability e.g. early Basic, Lisp, Prolog, some script languages 4/22/2017 COP4020 Spring 2014

Code Generation and Intermediate Code Forms A typical intermediate form of code produced by the semantic analyzer is an abstract syntax tree (AST) The AST is annotated with useful information such as pointers to the symbol table entry of identifiers Example AST for the gcd program in Pascal 4/22/2017 COP4020 Spring 2014

Code Generation and Intermediate Code Forms Other intermediate code forms intermediate code is something that is both close to the final machine code and easy to manipulate (for optimization). One example is the three-address code: dst = op1 op op2 The three-address code for the assignment statement: temp1 = 60 temp2 = id3 + temp1 temp3 = id2 + temp2 id1 = temp3 Machine-independent Intermediate code improvement temp1 = id3 * 60.0 id1 = id2 + temp1 4/22/2017 COP4020 Spring 2014

Target Code Generation and Optimization From the machine-independent form assembly or object code is generated by the compiler MOVF id3, R2 MULF #60.0, R2 MOVF id2, R1 ADDF R2, R1 MOVF R1, id1 This machine-specific code is optimized to exploit specific hardware features 4/22/2017 COP4020 Spring 2014 COP4020 Fall 2006

Summary Compiler front-end: lexical analysis, syntax analysis, semantic analysis Tasks: understanding the source code, making sure the source code is written correctly Compiler back-end: Intermediate code generation/improvement, and Machine code generation/improvement Tasks: translating the program to a semantically the same program (in a different language). 4/22/2017 COP4020 Spring 2014