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

Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
CPSC Compiler Tutorial 9 Review of Compiler.
Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
Compiler Design Lexical Analysis Syntactical Analysis Semantic Analysis Optimization Code Generation.
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.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 1, Lab.
COP4020 Programming Languages
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Chapter 17 Programming Tools The Architecture of Computer Hardware and Systems Software: An Information Technology Approach 3rd Edition, Irv Englander.
Course Revision Contents  Compilers  Compilers Vs Interpreters  Structure of Compiler  Compilation Phases  Compiler Construction Tools  A Simple.
(1.1) COEN 171 Programming Languages Winter 2000 Ron Danielson.
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.
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
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
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.
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
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.
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.
Programming Languages
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.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
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.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
Chapter 1. Introduction.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Advanced Computer Systems
Intro to compilers Based on end of Ch. 1 and start of Ch. 2 of textbook, plus a few additional references.
Compiler Design (40-414) Main Text Book:
Introduction Chapter : Introduction.
PRINCIPLES OF COMPILER DESIGN
Chapter 1 Introduction.
CS 3304 Comparative Languages
Introduction to Compiler Construction
CS 3304 Comparative Languages
CS 326 Programming Languages, Concepts and Implementation
Compiler Construction (CS-636)
Introduction.
Chapter 1 Introduction.
课程名 编译原理 Compiling Techniques
Compiler Lecture 1 CS510.
Introduction to Compiler Construction
Compiler 薛智文 TH 6 7 8, DTH Spring.
CSE401 Introduction to Compiler Construction
Compiler 薛智文 TH 6 7 8, DTH Spring.
CMP 131 Introduction to Computer Programming
Introduction to Compiler Construction
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Introduction Chapter : Introduction.
COP4020 Programming Languages
COP4020 Programming Languages
Compiler 薛智文 M 2 3 4, DTH Spring.
Introduction to Compiler Construction
Presentation transcript:

COP4020 Programming Languages Introduction Prof. Robert van Engelen

Overview Common compiler and interpreter configurations Virtual machines Integrated programming environments Compiler phases Lexical analysis Syntax analysis Semantic analysis Code generation 12/6/2018 COP4020 Fall 2006 COP4020 Fall 2006

Compilers versus Interpreters The compiler versus interpreter implementation is often fuzzy One can view an interpreter as a virtual machine A processor (CPU) can be viewed as an implementation in hardware of a virtual machine Some languages cannot be purely compiled into machine code Some languages allow programs to rewrite/add code In general, compilers try to be as smart as possible to fix decisions that can be taken at compile time to avoid to generate code that makes a decision at run time Compilation leads to better performance in general Allocation of variables without variable lookup at run time Aggressive code optimization to exploit hardware features Interpretation leads to better diagnostics of a programming problem Procedures can be invoked from command line by a user Variable values can be inspected and modified by a user 12/6/2018 COP4020 Fall 2006

Compilation Compilation is the conceptual process of translating source code into a CPU-executable binary target code Compiler runs on the same platform X as the target code Source Program Compiler Debug Target Program Compile on X Target Program Input Output Run on X 12/6/2018 COP4020 Fall 2006

Cross Compilation Compiler runs on platform X, target code runs on platform Y Debug (emulate) Source Program Cross Compiler Target Program Compile on X Copy to Y Target Program Input Output Run on Y 12/6/2018 COP4020 Fall 2006

Interpretation Interpretation is the conceptual process of running high-level code by an interpreter Source Program Interpreter Output Input 12/6/2018 COP4020 Fall 2006

Virtual Machines A virtual machine executes an instruction stream in software Adopted by Pascal, Java, Smalltalk-80, C#, functional and logic languages, and some scripting languages Pascal compilers generate P-code that can be interpreted or compiled into object code Java compilers generate bytecode that is interpreted by the Java virtual machine (JVM) The JVM may translate bytecode into machine code by just-in-time (JIT) compilation 12/6/2018 COP4020 Fall 2006

Compilation and Execution on Virtual Machines Compiler generates intermediate program Virtual machine interprets the intermediate program Source Program Compiler Intermediate Program Compile on X Run on VM Virtual Machine Input Output Run on X, Y, Z, … 12/6/2018 COP4020 Fall 2006

Pure Compilation and Static Linking Adopted by the typical Fortran implementation Library routines are separately linked (merged) with the object code of the program Source Program Compiler Incomplete Object Code extern printf(); Linker _printf _fget _fscan … Static Library Object Code Binary Executable 12/6/2018 COP4020 Fall 2006

Compilation, Assembly, and Static Linking Facilitates debugging of the compiler Source Program Compiler Assembly Program Assembler Linker Static Library Object Code Binary Executable 12/6/2018 COP4020 Fall 2006

Compilation, Assembly, and Dynamic Linking Dynamic libraries (DLL, .so, .dylib) are linked at run-time by the OS (via stubs in the executable) Source Program Compiler Assembly Program Assembler Shared Dynamic Libraries Incomplete Executable Output Input 12/6/2018 COP4020 Fall 2006

Preprocessing Most C and C++ compilers use a preprocessor to expand macros Source Program Preprocessor Modified Source Program Compiler Assembly or Object Code 12/6/2018 COP4020 Fall 2006

Assembly or Object Code The CPP Preprocessor Early C++ compilers used the CPP preprocessor to generated C code for compilation C++ Source Code C++ Preprocessor C Source Code C Compiler Assembly or Object Code 12/6/2018 COP4020 Fall 2006

Integrated Development Environments Programming tools function together in concert Editors Compilers/preprocessors/interpreters Debuggers Emulators Assemblers Linkers Advantages Tools and compilation stages are hidden Automatic source-code dependency checking Debugging made simpler Editor with search facilities Examples Smalltalk-80, Eclipse, MS VisualStudio, Borland 12/6/2018 COP4020 Fall 2006

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 12/6/2018 COP4020 Fall 2006

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 (semantic 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 12/6/2018 COP4020 Fall 2006

Scanner: Lexical Analysis Lexical analysis breaks up a program into 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 . 12/6/2018 COP4020 Fall 2006

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> 12/6/2018 COP4020 Fall 2006

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> 12/6/2018 COP4020 Fall 2006

Parser: Syntax Analysis Parsing organizes tokens into a hierarchy called a parse tree (more about this later) Essentially, a grammar of a language defines the structure of the parse tree, which in turn describes the program structure A syntax error is produced by a compiler when the parse tree cannot be constructed for a program 12/6/2018 COP4020 Fall 2006

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 Static semantic checks 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 12/6/2018 COP4020 Fall 2006

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 12/6/2018 COP4020 Fall 2006

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 12/6/2018 COP4020 Fall 2006

Target Code Generation and Optimization The AST with the annotated information is traversed by the compiler to generate a low-level intermediate form of code, close to assembly This machine-independent intermediate form is optimized From the machine-independent form assembly or object code is generated by the compiler This machine-specific code is optimized to exploit specific hardware features 12/6/2018 COP4020 Fall 2006