Introduction to Compiler Design

Similar documents
Recursive Descent Parsers

Abstract Syntax Trees Synthetic and Inherited Attributes

COP 3402 Systems Software. Lecture 4: Compilers. Interpreters

Minimal Spanning Trees

Building the Abstract Syntax Trees

Compiler Design (40-414)

JFlex Regular Expressions

CUP. Lecture 18 CUP User s Manual (online) Robb T. Koether. Hampden-Sydney College. Fri, Feb 27, 2015

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

The Critical-Path Algorithm

JFlex. Lecture 16 Section 3.5, JFlex Manual. Robb T. Koether. Hampden-Sydney College. Mon, Feb 23, 2015

Lecture 14 Sections Mon, Mar 2, 2009

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

Programming Languages

LR Parsing - Conflicts

Recognition of Tokens

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILING

Programming Languages

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Formal Languages and Compilers Lecture I: Introduction to Compilers

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Introduction to Compiler Construction

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Function Definition Syntax Tree

COSE312: Compilers. Lecture 1 Overview of Compilers

Introduction to Compiler Construction

Introduction to Compiler Construction

The Decreasing-Time Algorithm

LR Parsing - The Items

LECTURE 3. Compiler Phases

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

The Structure of a Syntax-Directed Compiler

Density Curves Sections

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

Compiler Design Overview. Compiler Design 1

Sampling Distribution Examples Sections 15.4, 15.5

Formats of Translated Programs

Rotations and Translations

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

The Structure of a Syntax-Directed Compiler

CS131: Programming Languages and Compilers. Spring 2017


Yacc: A Syntactic Analysers Generator

INTRODUCTION TO COMPILER AND ITS PHASES

Friends and Unary Operators

Street-Routing Problems

Compilers and Interpreters

Scope and Parameter Passing

The Structure of a Compiler

CST-402(T): Language Processors

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

The Graphics Pipeline

Scheduling and Digraphs

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

The Pairwise-Comparison Method

CS 4201 Compilers 2014/2015 Handout: Lab 1

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

COMPILER DESIGN LECTURE NOTES

Undergraduate Compilers in a Day

COP 3402 Systems Software Syntax Analysis (Parser)

What do Compilers Produce?

The x86 Instruction Set

Compiling Regular Expressions COMP360

Scope and Parameter Passing

The Traveling Salesman Problem Brute Force Method

CS412/413. Introduction to Compilers Tim Teitelbaum. Lecture 2: Lexical Analysis 23 Jan 08

Lexical Analysis. Chapter 1, Section Chapter 3, Section 3.1, 3.3, 3.4, 3.5 JFlex Manual

Group A Assignment 3(2)

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

UNIT I- LEXICAL ANALYSIS. 1.Interpreter: It is one of the translators that translate high level language to low level language.

Lexical and Syntax Analysis

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

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

Compiler Lab. Introduction to tools Lex and Yacc

Implementing Linked Lists

A Simple Syntax-Directed Translator

Introduction to Compiler Construction

Lecture 20 Sections Wed, Apr 1, 2009

CMSC 350: COMPILER DESIGN

Solving Recursive Sequences by Iteration

CSE302: Compiler Design

Syntax-Directed Translation. Lecture 14

COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR

Compilers. Compiler Construction Tutorial The Front-end

CSE302: Compiler Design

CSE302: Compiler Design

Dynamic Allocation of Memory

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

Displaying Distributions - Quantitative Variables

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

Introduction to Compiler Construction

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Syntax Analysis The Parser Generator (BYacc/J)

Transcription:

Introduction to Compiler Design Lecture 1 Chapters 1 and 2 Robb T. Koether Hampden-Sydney College Wed, Jan 14, 2015 Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 1 / 33

Outline 1 The Stages of Compilation Lexical Analysis Syntactic Analysis Semantic Analysis Intermediate Code Generation Optimization Machine Code Generation 2 Assignment Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 2 / 33

Outline 1 The Stages of Compilation Lexical Analysis Syntactic Analysis Semantic Analysis Intermediate Code Generation Optimization Machine Code Generation 2 Assignment Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 3 / 33

The Stages of Compilation The stages of compilation Lexical analysis Syntactic analysis. Semantic analysis. Intermediate code generation. Optimization. Machine code generation. Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 4 / 33

Outline 1 The Stages of Compilation Lexical Analysis Syntactic Analysis Semantic Analysis Intermediate Code Generation Optimization Machine Code Generation 2 Assignment Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 5 / 33

Lexical Analysis Definition (Token) A token is a smallest meaningful group symbols. Definition (Lexical analyzer) A lexical analyzer, also called a lexer or a scanner, receives a stream of characters from the source program and groups them into tokens. Source Program Lexical Analyzer Stream of Tokens Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 6 / 33

Example Example (Lexical Analysis) What are the tokens in the following program? int main() { float a = 123.4; return 0; } Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 7 / 33

Tokens Each token has a type and a value. For example, The variable count has type id and value count. The number 123 has type num and value 123. The keyword int has type int and value int. The symbol { has the type lbrace and value lbrace. Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 8 / 33

Example Example (Lexical Analysis) The statement position = initial + rate * 60; would be viewed as id 1 = id 2 + id 3 num ; or id 1 assign id 2 plus id 3 times num semi by the lexer. Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 9 / 33

Lexical Analysis Tools There are tools available to assist in the writing of lexical analyzers. lex - produces C source code (UNIX). flex - produces C source code (gnu). JLex - produces Java source code. JFlex - produces Java source code. We will use JFlex. Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 10 / 33

Outline 1 The Stages of Compilation Lexical Analysis Syntactic Analysis Semantic Analysis Intermediate Code Generation Optimization Machine Code Generation 2 Assignment Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 11 / 33

Syntactic Analysis Definition (Syntax analyzer) A syntax analyzer, also called a parser, receives a stream of tokens from the lexer and groups them into phrases that match specified grammatical patterns. Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 12 / 33

Syntactic Analysis Definition (Abstract syntax tree) The output of the parser is an abstract syntax tree representing the syntactical structure of the tokens. Stream of Tokens Syntax Analyzer Abstract Syntax Tree Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 13 / 33

Grammatical Patterns Grammatical patterns are described by a context-free grammar. For example, an assignment statement may be defined as stmt id = expr ; expr expr + expr expr expr id num Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 14 / 33

Example Example (Syntactic Analysis) The form id 1 = id 2 + id 3 num ; may be represented by the following syntax tree. = id 1 + id 2 * id 3 num Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 15 / 33

Syntax Analysis Tools There are tools available to assist in the writing of parsers. yacc - produces C source code (UNIX). bison - produces C source code (gnu). CUP - produces Java source code. We will use CUP. Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 16 / 33

Outline 1 The Stages of Compilation Lexical Analysis Syntactic Analysis Semantic Analysis Intermediate Code Generation Optimization Machine Code Generation 2 Assignment Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 17 / 33

Semantic Analysis Definition (Semantic analyzer) A semantic analyzer traverses the abstract syntax tree, checking that each node is appropriate for its context, i.e., it checks for semantic errors. It outputs a refined abstract syntax tree. Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 18 / 33

Example: Semantic Analysis Example (Semantic Analysis) The previous tree may be refined to = id 1 + id 2 * id 3 inttoreal num Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 19 / 33

Outline 1 The Stages of Compilation Lexical Analysis Syntactic Analysis Semantic Analysis Intermediate Code Generation Optimization Machine Code Generation 2 Assignment Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 20 / 33

Intermediate Code Generation Definition (Intermediate code) Intermediate code is code that represents the semantics of a program, but is machine-independent. Definition (Intermediate code generator) An intermediate code generator receives the abstract syntax tree and outputs intermediate code that semantically corresponds to the abstract syntax tree. Abstract Syntax Tree Intermediate Code Generator Intermediate Code Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 21 / 33

Intermediate Code This stage marks the boundary between the front end and the back end. The front end is language-specific and machine-independent. The back end is machine-specific and language-independent. Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 22 / 33

Intermediate Code C Program x86 Code Java Program Intermediate Code Python Program MIPS32 Code Front End Back End Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 23 / 33

Example Example (Intermediate Code Generation) The tree in our example may be expressed in intermediate code as temp1 = inttoreal(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 24 / 33

Outline 1 The Stages of Compilation Lexical Analysis Syntactic Analysis Semantic Analysis Intermediate Code Generation Optimization Machine Code Generation 2 Assignment Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 25 / 33

Code Optimizer Definition (Optimizer) An optimizer reviews the code, looking for ways to reduce the number of operations and the memory requirements. A program may be optimized for speed or for size. Typically there is a trade-off between speed and size. Intermediate Code Optimizer Intermediate Code Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 26 / 33

Example Example (Optimization) The intermediate code in this example may be optimized as temp1 = id3 * 60.0 id1 = id2 + temp1 Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 27 / 33

Outline 1 The Stages of Compilation Lexical Analysis Syntactic Analysis Semantic Analysis Intermediate Code Generation Optimization Machine Code Generation 2 Assignment Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 28 / 33

Machine Code Generation The code generator receives the (optimized) intermediate code. It produces either Machine code for a specific machine, or Assembly code for a specific machine and assembler. If it produces assembly code, then an assembler is used to produce the machine code. Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 29 / 33

Machine Code Generation Intermediate Code Code Generator Assembly Code Assembler Machine Code Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 30 / 33

Example: Machine Code Generation The intermediate code may be translated into the assembly code movf id3,r2 mulf #60.0,R2 movf id2,r1 addf R2,R1 movf R1,id1 Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 31 / 33

Outline 1 The Stages of Compilation Lexical Analysis Syntactic Analysis Semantic Analysis Intermediate Code Generation Optimization Machine Code Generation 2 Assignment Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 32 / 33

Assignment Assignment Read Chapters 1 and 2. Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14, 2015 33 / 33