Compilers. Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

Similar documents
Compilers: Principles, Techniques, and Tools. Jing-Shin Chang Department of Computer Science & Information Engineering National Chi-Nan University

Lexical Analysis (ASU Ch 3, Fig 3.1)

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Compiler Design (40-414)

Lexical Analysis. Dragon Book Chapter 3 Formal Languages Regular Expressions Finite Automata Theory Lexical Analysis using Automata

Formal Languages and Compilers Lecture I: Introduction to Compilers

Languages and Finite Automata

Derivations of a CFG. MACM 300 Formal Languages and Automata. Context-free Grammars. Derivations and parse trees

Interpreter. Scanner. Parser. Tree Walker. read. request token. send token. send AST I/O. Console

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

LANGUAGE TRANSLATORS

Formal languages and computation models

Introduction to Compiler Design

COMPILER DESIGN UNIT I LEXICAL ANALYSIS. Translator: It is a program that translates one language to another Language.

Lexical Analysis. Introduction

Languages and Strings. Chapter 2

DVA337 HT17 - LECTURE 4. Languages and regular expressions

COMPILER DESIGN LECTURE NOTES

1.3 Functions and Equivalence Relations 1.4 Languages

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

Introduction to Lexical Analysis

Languages and Compilers

1. INTRODUCTION TO LANGUAGE PROCESSING The Language Processing System can be represented as shown figure below.

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILING

Compilers: Principles, Techniques, and Tools. Jing-Shin Chang Department of Computer Science & Information Engineering National Chi-Nan University

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

Introduction to Compiler Construction

Syntax Analysis Part I

Introduction to Lexical Analysis

Undergraduate Compilers in a Day

CST-402(T): Language Processors

Regular Languages. MACM 300 Formal Languages and Automata. Formal Languages: Recap. Regular Languages

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Finite Automata. Dr. Nadeem Akhtar. Assistant Professor Department of Computer Science & IT The Islamia University of Bahawalpur

SEM / YEAR : VI / III CS2352 PRINCIPLES OF COMPLIERS DESIGN UNIT I - LEXICAL ANALYSIS PART - A

Introduction to Compiler Construction

CS 314 Principles of Programming Languages. Lecture 3

Introduction to Compiler Construction

Non-deterministic Finite Automata (NFA)

CSCE 314 Programming Languages

Last lecture CMSC330. This lecture. Finite Automata: States. Finite Automata. Implementing Regular Expressions. Languages. Regular expressions

Lexical Scanning COMP360

1. Lexical Analysis Phase


Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

Compiler Construction

Introduction to Automata Theory. BİL405 - Automata Theory and Formal Languages 1

Compiling Regular Expressions COMP360

Syntax and Grammars 1 / 21

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

The Front End. The purpose of the front end is to deal with the input language. Perform a membership test: code source language?

Compilers CS S-01 Compiler Basics & Lexical Analysis

2010: Compilers REVIEW: REGULAR EXPRESSIONS HOW TO USE REGULAR EXPRESSIONS

Lecture 4: Syntax Specification

CSE302: Compiler Design

ECS 120 Lesson 7 Regular Expressions, Pt. 1

Compilers CS S-01 Compiler Basics & Lexical Analysis

Part 5 Program Analysis Principles and Techniques

Formal Languages and Compilers Lecture VI: Lexical Analysis

CS 314 Principles of Programming Languages

Theory of Computation

UNIT -2 LEXICAL ANALYSIS

CS131: Programming Languages and Compilers. Spring 2017

Compiler Construction LECTURE # 3

Lexical Analyzer Scanner

Compiler Construction I

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Lexical Analyzer Scanner

CS S-01 Compiler Basics & Lexical Analysis 1

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

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

Lexical Analysis. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

Lexical Analysis. Sukree Sinthupinyo July Chulalongkorn University

Formal Languages and Compilers Lecture IV: Regular Languages and Finite. Finite Automata

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

COMPILER DESIGN. For COMPUTER SCIENCE

Introduction to Lexing and Parsing

Compiler Construction

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

Lecture 3: Lexical Analysis

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Compiler Construction I

Week 2: Syntax Specification, Grammars

COP 3402 Systems Software. Lecture 4: Compilers. Interpreters

Principles of Programming Languages COMP251: Syntax and Grammars

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

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

HKN CS 374 Midterm 1 Review. Tim Klem Noah Mathes Mahir Morshed

Regular Languages. Regular Language. Regular Expression. Finite State Machine. Accepts

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

XFST2FSA. Comparing Two Finite-State Toolboxes

CS 4201 Compilers 2014/2015 Handout: Lab 1

Introduction. Compilers and Interpreters

This book is licensed under a Creative Commons Attribution 3.0 License

Lexical Analysis. Chapter 2

Lexical Analysis. Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!

CS6160 Theory of Computation Problem Set 2 Department of Computer Science, University of Virginia

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

Introduction to Compiler Construction

CSE Lecture 4: Scanning and parsing 28 Jan Nate Nystrom University of Texas at Arlington

Transcription:

Compilers Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University 1

Objectives Introduce principles and techniques for compiler construction Introduce principles and techniques for compiler-compiler construction Use a compiler-compiler to exercise the compiler construction for a small language 2

Introduction 3

Programming Languages Human use nature languages to communicate with each other Chinese, English, French Human use programming languages to communicate with computers Fortran, Pascal, C 4

Computer Organization Applications Compiler Operating System Hardware Machine 5

Compiler Source Code Compiler Target Code Error Messages 6

Components of a Compiler Analysis Lexical Analysis Syntax Analysis Semantic Analysis Synthesis Intermediate Code Generation Code Optimization Code Generation 7

Lexical Analysis S o m e o n e b r e a k s t h e i c e final := initial + rate * 60 Someone breaks the ice id 1 := id 2 + id 3 * 60 8

Syntax Analysis Someone breaks the ice id 1 := id 2 + id 3 * 60 sentence subject verb object Someone breaks the ice := id 1 + id 2 * id 3 60 9

Semantic Anaysis Someone plays the piano (meaningful) := id 1 + id 2 * id 3 60 The piano plays someone (meaningless) := id + 1 id * 2 id i2r 3 60 10

Intermediate Code Generation Someone breaks the ice := id + 1 id * 2 id i2r 3 60 有人打破冰 temp1 := i2r ( 60 ) temp2 := id 3 * temp1 temp3 := id 2 + temp2 id 1 := temp3 11

Code Optimization 有人打破冰 temp1 := i2r ( 60 ) temp2 := id 3 * temp1 temp3 := id 2 + temp2 id 1 := temp3 有人打破沉默 temp1 := id 3 * 60.0 id 1 := id 2 + temp1 12

Code Generation 有人打破沉默 temp1 := id 3 * 60.0 id 1 := id 2 + temp1 有人打破沉默 movf id 3, r2 mulf #60.0, r2 movf id 2, r1 addf r2, r1 movf r1, id 1 13

Structure of a Compiler Front End Lexical Analysis Syntax Analysis Semantic Analysis Intermediate Code Generation Back End Code Optimization Code Generation 14

Reuse of Components Fortran Pascal C Intermediate Code MIPS SPARC Pentium 15

Applications Interpreters WWW Browsers Word Processors Computer-Aided Design Computer-Aided Manufacture 16

Compiler-Compiler Compiler Description Compiler-compiler Compiler Error Messages 17

Formal Language Theory language define accept grammar generate automatically automaton 18

Grammars The sentences in a language may be defined by a set of rules called a grammar L: {00, 01, 10, 11} G: the set of binary strings of length 2 19

Automata An acceptor of a language is an automaton which determines if an input string is a sentence in the language A transducer of a language is an automaton which determines if an input string is a sentence in the language and may produce strings as output if it is in the language 20

Transducers language L 1 language L 2 define accept automaton transduce define grammar G 1 generate grammar G 2 21

Metalanguage Metalanguage: a language used to define another language language define accept metalanguage generate automatically automaton 22

Automatic Compiler Generation We will use different metalangauges to define the various components of a programming language so that these components can be generated automatically 23

Definition of Programming Languages Lexical syntax: regular expressions Syntax: context free grammars Semantics: attribute grammars Intermediate code generation: attribute grammars Code generation: tree grammars 24

Languages Alphabet - any finite set of symbols {0, 1}: binary alphabet String - a finite sequence of symbols from the alphabet 1011: a string of length 4 : the empty string Language - any set of strings on the alphabet {00, 01, 10, 11}: the set of strings of length 2 : the empty set 25

Terms for Parts of a String string: banana (proper) prefix:, b, ba, ban,..., banana (proper) suffix:, a, na, ana,..., banana (proper) substring:, b, a, n, ba, an, na,..., banana subsequence:, b, a, n, ba, bn, an, aa, na, nn,..., banana sentence: a string in the language 26

Operations on Strings concatenation: x = dog y = house xy = doghouse exponentiation: s 0 = s 1 = s s 2 = ss 27

Operations on Languages Union of L and M, L M L M = { s s L or s M} Concatenation of L and M, LM LM = {st s L and t M} Kleene closure of L, L * L * = i i 0L Positive closure of L, L + L + i = i 1L 28

Implementation of Programming Languages Regular expressions: finite automata, lexical analyzer Context free grammars: pushdown automata, parser Attribute grammars: attribute evaluators, type checker and intermediate code generator Tree grammars: finite tree automata, code generator 29

Automatic Program Generators problem define solve specification language generate automatically program 30

Content Lexical analysis Syntax analysis Semantic analysis Intermediate code generation Code generation 31

共勉 子曰.. 學而時習之, 不亦說乎? -- 論語 32