COSE312: Compilers. Lecture 1 Overview of Compilers

Similar documents
CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Introduction to Compiler Design

Compiling Techniques

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

A simple syntax-directed

CS 4201 Compilers 2014/2015 Handout: Lab 1

COMP 181 Compilers. Administrative. Last time. Prelude. Compilation strategy. Translation strategy. Lecture 2 Overview

Introduction to Compiler

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

CA Compiler Construction

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

LECTURE 3. Compiler Phases

2068 (I) Attempt all questions.

Compiler Construction

COSE 215: Theory of Computation. Undecidability (2) Hakjoo Oh 2016 Spring

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

COSE312: Compilers. Lecture 20 Data-Flow Analysis (2)

Undergraduate Compilers in a Day

Compilers and Interpreters

Front End. Hwansoo Han

COMP455: COMPILER AND LANGUAGE DESIGN. Dr. Alaa Aljanaby University of Nizwa Spring 2013

Introduction to Compiler Construction

Lecture 14 Sections Mon, Mar 2, 2009

Program Analysis ( 软件源代码分析技术 ) ZHENG LI ( 李征 )

Introduction to Compiler Construction

Compilers. Lecture 2 Overview. (original slides by Sam

Lexical Analysis. Introduction

Today. Assignments. Lecture Notes CPSC 326 (Spring 2019) Quiz 2. Lexer design. Syntax Analysis: Context-Free Grammars. HW2 (out, due Tues)

Topic 3: Syntax Analysis I

Building Compilers with Phoenix

Introduction to Compiler Construction

LANGUAGE PROCESSORS. Presented By: Prof. S.J. Soni, SPCE Visnagar.

1. The output of lexical analyser is a) A set of RE b) Syntax Tree c) Set of Tokens d) String Character

High-level View of a Compiler

CA Compiler Construction

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILING

Formal Languages and Compilers Lecture VI: Lexical Analysis

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

Lexical Analysis - An Introduction. Lecture 4 Spring 2005 Department of Computer Science University of Alabama Joel Jones

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

CS 132 Compiler Construction

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

PL Revision overview

A Simple Syntax-Directed Translator

CS606- compiler instruction Solved MCQS From Midterm Papers

Formal Languages and Compilers Lecture I: Introduction to Compilers

COMPILER DESIGN. For COMPUTER SCIENCE

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Abstract Syntax Trees Synthetic and Inherited Attributes

Syntax Analysis MIF08. Laure Gonnord

Lexical Analysis - 1. A. Overview A.a) Role of Lexical Analyzer

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

CPS 506 Comparative Programming Languages. Syntax Specification

Working of the Compilers

Compiler Construction D7011E

Compiler Design Overview. Compiler Design 1

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

MidTerm Papers Solved MCQS with Reference (1 to 22 lectures)

Syntax-Directed Translation. Lecture 14

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

Compiler Construction

Introduction to Lexical Analysis

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

Syntax and Grammars 1 / 21

A programming language requires two major definitions A simple one pass compiler

Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Outline. Lecture 17: Putting it all together. Example (input program) How to make the computer understand? Example (Output assembly code) Fall 2002

Introduction to Parsing. Lecture 8

CS415 Compilers. Lexical Analysis

Compiling Regular Expressions COMP360

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

Lexing, Parsing. Laure Gonnord sept Master 1, ENS de Lyon

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1

COP4020 Spring 2011 Midterm Exam

Syntax Analysis. Chapter 4

Review main idea syntax-directed evaluation and translation. Recall syntax-directed interpretation in recursive descent parsers

CS101 Introduction to Programming Languages and Compilers

Introduction to Compilers

Outline. Limitations of regular languages. Introduction to Parsing. Parser overview. Context-free grammars (CFG s)

Course introduction. Advanced Compiler Construction Michel Schinz

CS415 Compilers Overview of the Course. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Topic 5: Syntax Analysis III

BIRLA INSTITUTE OF TECHNOLOGY AND SCIENCE, Pilani Pilani Campus Instruction Division

Compiler Construction LECTURE # 1

Compiler Design (40-414)

Overview of Compiler. A. Introduction

Group A Assignment 3(2)

QUESTIONS RELATED TO UNIT I, II And III

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

COMPILER DESIGN LECTURE NOTES

Outline. Limitations of regular languages Parser overview Context-free grammars (CFG s) Derivations Syntax-Directed Translation

CS152 Programming Language Paradigms Prof. Tom Austin, Fall Syntax & Semantics, and Language Design Criteria

Advanced Topics in MNIT. Lecture 1 (27 Aug 2015) CADSL

Examples of attributes: values of evaluated subtrees, type information, source file coordinates,

Abstract Syntax Trees & Top-Down Parsing

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

Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Gr

Transcription:

COSE312: Compilers Lecture 1 Overview of Compilers Hakjoo Oh 2017 Spring Hakjoo Oh COSE312 2017 Spring, Lecture 1 March 7, 2017 1 / 15

What is Compiler? Software systems that translate a program written in one language ( source language ) into a program written in another language ( target language ). Hakjoo Oh COSE312 2017 Spring, Lecture 1 March 7, 2017 2 / 15

What is Compiler? Software systems that translate a program written in one language ( source language ) into a program written in another language ( target language ). Typically, the source language is a high-level language, e.g., C, and the target language is a machine language, e.g., x86. Hakjoo Oh COSE312 2017 Spring, Lecture 1 March 7, 2017 2 / 15

What is Compiler? Software systems that translate a program written in one language ( source language ) into a program written in another language ( target language ). Typically, the source language is a high-level language, e.g., C, and the target language is a machine language, e.g., x86. cf) When the target language is not a machine language: Hakjoo Oh COSE312 2017 Spring, Lecture 1 March 7, 2017 2 / 15

A Fundamental Requirement The compiler must preserve the meaning of the source program. Hakjoo Oh COSE312 2017 Spring, Lecture 1 March 7, 2017 3 / 15

Structure of Modern Compilers The front-end understands the source program and translates it to an intermediate representation (IR). The middle-end takes a program in IR and optimizes it in terms of efficiency, energy consumption, and so on. The back-end transforms the IR program into machine-code. Hakjoo Oh COSE312 2017 Spring, Lecture 1 March 7, 2017 4 / 15

Front End The lexical analyzer transforms the character stream into a stream of tokens. The syntax analyzer transforms the stream of tokens into a syntax tree. The semantic analyzer checks if the program is semantically well-formed. The IR translator translates the syntax tree into IR. Hakjoo Oh COSE312 2017 Spring, Lecture 1 March 7, 2017 5 / 15

Lexical 1 Analyzer (Lexer) A lexer analyzes the lexical structure of the source program: 1 of or relating to words or the vocabulary of a language as distinguished from its grammar and construction Hakjoo Oh COSE312 2017 Spring, Lecture 1 March 7, 2017 6 / 15

Lexical 1 Analyzer (Lexer) A lexer analyzes the lexical structure of the source program: ex) The lexical analyzer transform the character stream pos = init + rate * 10 1 of or relating to words or the vocabulary of a language as distinguished from its grammar and construction Hakjoo Oh COSE312 2017 Spring, Lecture 1 March 7, 2017 6 / 15

Lexical 1 Analyzer (Lexer) A lexer analyzes the lexical structure of the source program: ex) The lexical analyzer transform the character stream pos = init + rate * 10 into a sequence of lexemes: pos, =, init, +, rate, *, 10 1 of or relating to words or the vocabulary of a language as distinguished from its grammar and construction Hakjoo Oh COSE312 2017 Spring, Lecture 1 March 7, 2017 6 / 15

Lexical 1 Analyzer (Lexer) A lexer analyzes the lexical structure of the source program: ex) The lexical analyzer transform the character stream pos = init + rate * 10 into a sequence of lexemes: pos, =, init, +, rate, *, 10 and then produces a token sequence: (ID, pos), ASSIGN, (ID, init), PLUS, (ID, rate), MULT, (NUM,10) 1 of or relating to words or the vocabulary of a language as distinguished from its grammar and construction Hakjoo Oh COSE312 2017 Spring, Lecture 1 March 7, 2017 6 / 15

Syntax 2 Analyzer (Parser) A parser analyzes the grammatical structure of the source program: 2 the way in which words are put together to form phrases, clauses, or sentences Hakjoo Oh COSE312 2017 Spring, Lecture 1 March 7, 2017 7 / 15

Syntax 2 Analyzer (Parser) A parser analyzes the grammatical structure of the source program: ex) the parser transforms the sequence of tokens into the syntax tree: (ID, pos), =, (ID, init), +, (ID, rate), *, (NUM,10) 2 the way in which words are put together to form phrases, clauses, or sentences Hakjoo Oh COSE312 2017 Spring, Lecture 1 March 7, 2017 7 / 15

Semantic Analyzer A semantic analyzer detects semantically ill-formed programs: ex) Type errors: int x = 1; string y = "hello"; int z = x + y; Other semantic errors: array out of bounds null-dereference divide-by-zero... Hakjoo Oh COSE312 2017 Spring, Lecture 1 March 7, 2017 8 / 15

Hakjoo Oh COSE312 2017 Spring, Lecture 1 March 7, 2017 9 / 15

Key Technology: Static Program Analysis Predict program behavior statically and automatically static: by analyzing program text, before run/ship/embed automatic: sw is analyzed by sw ( static analyzer ) Applications bug-finding: e.g., runtime failures of programs security: e.g., is this app malicious or benign? verification: e.g., does the program meet its specification? optimization: e.g., automatic parallelization Being widely used in sw industry Hakjoo Oh COSE312 2017 Spring, Lecture 1 March 7, 2017 10 / 15

IR Translator Intermediate Representation: lower-level than the source language higher-level than the target language Hakjoo Oh COSE312 2017 Spring, Lecture 1 March 7, 2017 11 / 15

IR Translator Intermediate Representation: lower-level than the source language higher-level than the target language ex) translate the syntax tree into three-address code: t1 = 10 t2 = rate * t1 t3 = init + t2 pos = t3 Hakjoo Oh COSE312 2017 Spring, Lecture 1 March 7, 2017 11 / 15

Optimizer Transform IR to have better performance: Hakjoo Oh COSE312 2017 Spring, Lecture 1 March 7, 2017 12 / 15

Optimizer Transform IR to have better performance: ex) Hakjoo Oh COSE312 2017 Spring, Lecture 1 March 7, 2017 12 / 15

Back End Generate the target machine code: ex) from the IR t2 = rate * 10 pos = init + t2 generate the machine code LOAD R2, rate MUL R2, R2, #10 LOAD R1, init ADD R1, R1, R2 STORE pos, R1 Hakjoo Oh COSE312 2017 Spring, Lecture 1 March 7, 2017 13 / 15

Summary A modern compiler consists of three phases: Front end understands the syntax and semantics of source program. Middle end improves the efficiency of the program. Back end generates the target program. Hakjoo Oh COSE312 2017 Spring, Lecture 1 March 7, 2017 14 / 15

cf) A General View of Compilers Compilers can be seen as a code synthesizer that transforms specification into implementation. specification: high-level impl, logics, examples, natural languages, etc implementation: low-level impl, high-level impl, algorithm design, etc e.g., specification: reverse(12) = 21, reverse(123) = 321 See our recent paper: Synthesizing Imperative Programs for Introductory Programming Assignments. https://arxiv.org/abs/1702.06334 Hakjoo Oh COSE312 2017 Spring, Lecture 1 March 7, 2017 15 / 15