Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Similar documents
COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

CST-402(T): Language Processors

Working of the Compilers

LECTURE 3. Compiler Phases

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

Why are there so many programming languages? Why do we have programming languages? What is a language for? What makes a language successful?

Early computers (1940s) cost millions of dollars and were programmed in machine language. less error-prone method needed

COP4020 Programming Languages. Compilers and Interpreters Prof. Robert van Engelen

Why are there so many programming languages?


Compiling Regular Expressions COMP360

Compilation I. Hwansoo Han

Programming Languages

COMPILER DESIGN LECTURE NOTES

Crafting a Compiler with C (II) Compiler V. S. Interpreter

LECTURE 2. Compilers and Interpreters

Concepts of Programming Languages

COP 3402 Systems Software. Lecture 4: Compilers. Interpreters

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Static and Dynamic Semantics

Compilers. Prerequisites

Introduction. Compilers and Interpreters

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

Compilers and Interpreters

Introduction to Programming Languages. CSE 307 Principles of Programming Languages Stony Brook University

Lexical Scanning COMP360

Pioneering Compiler Design

A Simple Syntax-Directed Translator

Compiler Design. Computer Science & Information Technology (CS) Rank under AIR 100

CS 415 Midterm Exam Spring 2002

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

The role of semantic analysis in a compiler

CS 4201 Compilers 2014/2015 Handout: Lab 1

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILING

Introduction to Compiler Construction

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Chapter 1. Preview. Reason for Studying OPL. Language Evaluation Criteria. Programming Domains

Introduction. Interpreters High-level language intermediate code which is interpreted. Translators. e.g.

Introduction to Compiler Construction

Theory and Compiling COMP360

Earlier edition Dragon book has been revised. Course Outline Contact Room 124, tel , rvvliet(at)liacs(dot)nl

Programming Languages, Summary CSC419; Odelia Schwartz

names names identifiers variables subroutines constants

CD Assignment I. 1. Explain the various phases of the compiler with a simple example.

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Compiler Design (40-414)

Compilers and interpreters

What is a compiler? Xiaokang Qiu Purdue University. August 21, 2017 ECE 573

Programmiersprachen (Programming Languages)

Undergraduate Compilers in a Day

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

A simple syntax-directed

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Compiler Theory Introduction and Course Outline Sandro Spina Department of Computer Science

CS5363 Final Review. cs5363 1

General Concepts. Abstraction Computational Paradigms Implementation Application Domains Influence on Success Influences on Design

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

G Programming Languages - Fall 2012

Compilers and Code Optimization EDOARDO FUSELLA

CS 242. Fundamentals. Reading: See last slide

ST. XAVIER S COLLEGE

INSTITUTE OF AERONAUTICAL ENGINEERING

Informatica 3 Syntax and Semantics

Semantic Analysis and Type Checking

CS426 Compiler Construction Fall 2006

CMPT 379 Compilers. Anoop Sarkar.

Lecture 09. Ada to Software Engineering. Mr. Mubashir Ali Lecturer (Dept. of Computer Science)

Principles of Programming Languages. Lecture Outline

Programming 1. Lecture 1 COP 3014 Fall August 28, 2017

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

Intermediate Code Generation

Design & Implementation Overview

Where We Are. Lexical Analysis. Syntax Analysis. IR Generation. IR Optimization. Code Generation. Machine Code. Optimization.

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

SLIDE 2. At the beginning of the lecture, we answer question: On what platform the system will work when discussing this subject?

SCAN: Computer Science - second year. Christian Wolf

Compiler Code Generation COMP360

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Introduction to Compiler Construction

Low-Level Languages. Computer Programs and Programming Languages

COMPILER DESIGN. For COMPUTER SCIENCE

CS321 Languages and Compiler Design I. Winter 2012 Lecture 1

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

Programming 1 - Honors

CSCE 314 Programming Languages. Type System

Introduction to Compiler Construction

Types. What is a type?

Programming Languages Third Edition. Chapter 7 Basic Semantics

What do Compilers Produce?

COSC121: Computer Systems: Runtime Stack

Chapter 2 A Quick Tour

Chapter 1. Preliminaries

History of Compilers The term

Transcription:

Copyright R.A. van Engelen, FSU Department of Computer Science, 2000 Overview of Compilers and Interpreters Common compiler and interpreter configurations Virtual machines Integrated programming environments Compiler phases Lexical analysis Syntax analysis Semantic analysis Code generation Note: These slides cover Chapter 1 of the textbook Compiling and Interpreting Programming Languages The compiler versus interpreter implementation is often fuzzy One can view an interpreter as a virtual machine A processor (CPU) is an implementation in hardware of a virtual machine for machine code Some languages cannot be purely compiled into machine cod when the language allows a program to rewrite its own code, requiring the interpreter or virtual machine to invoke the compiler In general, compilers try to be as smart as possible to fix decisions that can be taken at compile time to avoid to genera code that makes a decision at run time Compilation leads to better performance in general Allocation of variables without variable lookup at run tim Aggressive code optimization to exploit hardware feature 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

Compilation and Interpretation Compilation (conceptual): Source Program Compiler Target Program Input Target Program Output Interpretation (conceptual): Source Program Input Interpreter Output Pure Compilation and Linking Adopted by the typical Fortran implementation Library routines are separately linked (merged) with the obj code of the program Source Program Compiler Incomplete Object Code Incomplete Object Code Library Routines Linker Object Code

Compilation, Assembly, and Linking Adopted by most compilers Facilitates debugging of the compiler Source Program Compiler Assembly Assembly Assembler Incomplete Object Code Incomplete Object Code Library Routines Linker Object Code Mixed Compilation and Interpretation Adopted by Pascal, Java, functional and logic languages, and most scripting languages Pascal compilers generate P-code that can be interpreted or compiled into object code Java compilers generate byte code that is interpreted by the Ja virtual machine (or translated into machine code by a just-in-time (JIT) compiler) Functional and logic languages are compiled, but also allow dynamically created code to be compiled at run time for whic the virtual machine invokes the compiler Source Program Translator Intermediate Program Intermediate Program Input Virtual Machine Output

Preprocessing Compilers for C and C++ adopt a preprocessor Source Program Preprocessor Modified Source Program Modified Source Program Compiler Assembly Early C++ compilers generated intermediate C code Source Program Preprocessor Modified Source Program Modified Source Program C++ Compiler C Code C Code C Compiler Assembly Integrated Programming Environments Programming tools (editors, compilers, interpreters, debugger function together in concert Trace facilities to monitor execution of the program Upon run time error in compiled code the editor is invoked w cursor at source line Fundamental to Smalltalk-80 Java Studio, Visual C++, VisualStudio Overview of Compilation Compilation of a program proceeds through a series of phase where subsequent phases use information found in an earlier phase or uses a form of the program produced by an earlier phase Each phase may consist of a number of passes over the progr representation Character Stream Scanner

Token Stream Parser Parse Tree Semantic Analysis and Intermediate Code Generation Abstract Syntax Tree or Other Intermediate Form Machine-Independent Code Improvement Modified Intermediate Form Target Code Generation Assembly or Object Code Machine-Specific Code Improvement Modified Assembly or Object Code Front end (analysis) Back end (synthesis) Lexical Analysis Lexical analysis breaks up a program (e.g. in Pascal) 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. into 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. This is also known as scanning performed by a scanner Note: a scanner in Java

Context-Free Grammars A context-free grammar defines the syntax of a programming language The grammar defines syntactic categories Statements Expressions Declarations Categories are subdivided into more detailed categories Loop-statement If-statement Logical-expression... Most programming language manuals include language grammars <statement> -> <loop-statement> <statement> -> <if-statement> <loop-statement> -> for (<expression>; <expression>; <expression <statement> <expression> -> <logical-expression>... Syntax Analysis Parsing organizes tokens into a hierarchy called a parse tree A grammar of a language with the token stream defines the structure of the parse tree Syntax analysis is applied by a compiler to check the syntax o program by constructing a parse tree of the program Example (incomplete) Pascal grammar: <Program> -> program <id> ( <id> <More_ids> ) ; <Block>. <Block> -> <Variables> begin <Stmt> <More_Stmts> end <More_ids> ->, <id> <More_ids> e <Variables> -> var <id> <More_ids> : <Type> ; <More_Variables e <More_Variables> -> <id> <More_ids> : <Type> ; <More_Variable e <Stmt> -> <id> := <Exp> read ( <id> <More_ids> ) writeln ( <Exp> <More_Exps> ) if <Exp> then <Stmt> else <Stmt> while <Exp> do <Stmt> begin <Stmt> <More_Stmts> end Note: An interactive parser demo demonstrates the parsing of the gcd Pascal example program into a parse tree (see also textbook pp. 20-21)

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 (see later) 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 Strong Typing A language is strongly typed "if (type) errors are always detected" Such errors are listed on previous slide Errors are either detected at compile time or at run time Strong typing makes language safe and easier to use, but slow because of dynamic semantic checks Languages that are strongly typed are Ada Java ML, Haskell Languages that are not strongly typed are Fortran, Pascal, C Lisp, C++ In some languages, most (type) errors are detected late at run time which is detrimental to reliability (e.g. early Basic, Lis Prolog, some script languages)

Intermediate Code Generation 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 Pascal program: 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 generated by the compiler This machine-specific code is optimized to exploit specific hardware features