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

Slides:



Advertisements
Similar presentations
Lexical Analysis Dragon Book: chapter 3.
Advertisements

Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00.
COMP-421 Compiler Design Presented by Dr Ioanna Dionysiou.
1 Languages. 2 A language is a set of strings String: A sequence of letters Examples: “cat”, “dog”, “house”, … Defined over an alphabet: Languages.
By Neng-Fa Zhou Compiler Construction CIS 707 Prof. Neng-Fa Zhou
1 CSCI-2400 Models of Computation. 2 Computation CPU memory.
1 Languages and Finite Automata or how to talk to machines...
CS 3240 – Chuck Allison.  A model of computation  A very simple, manual computer (we draw pictures!)  Our machines: automata  1) Finite automata (“finite-state.
Topics Automata Theory Grammars and Languages Complexities
Fall 2006Costas Busch - RPI1 Languages. Fall 2006Costas Busch - RPI2 Language: a set of strings String: a sequence of symbols from some alphabet Example:
Scanner Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source language? Is the.
Lecture 2 Phases of Compiler. Preprocessors, Compilers, Assemblers, and Linkers Preprocessor Compiler Assembler Linker Skeletal Source Program Source.
Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University.
September 7, September 7, 2015September 7, 2015September 7, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Languages & Strings String Operations Language Definitions.
Compiler1 Chapter V: Compiler Overview: r To study the design and operation of compiler for high-level programming languages. r Contents m Basic compiler.
COP4020 Programming Languages
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.
CSC312 Automata Theory Lecture # 2 Languages.
Compiler Phases: Source program Lexical analyzer Syntax analyzer Semantic analyzer Machine-independent code improvement Target code generation Machine-specific.
Compiler Construction1 COMP Compiler Construction Lecturer: Dr. Arthur Cater Teaching Assistant:
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
Lecture # 3 Chapter #3: Lexical Analysis. Role of Lexical Analyzer It is the first phase of compiler Its main task is to read the input characters and.
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.
COMP 3438 – Part II - Lecture 2: Lexical Analysis (I) Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ. 1.
1 Languages. 2 A language is a set of strings String: A sequence of letters Examples: “cat”, “dog”, “house”, … Defined over an alphabet:
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.
CS 461 – Sept. 19 Last word on finite automata… –Scanning tokens in a compiler –How do we implement a “state” ? Chapter 2 introduces the 2 nd model of.
1 November 1, November 1, 2015November 1, 2015November 1, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
Review: Compiler Phases: Source program Lexical analyzer Syntax analyzer Semantic analyzer Intermediate code generator Code optimizer Code generator Symbol.
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.
Topic #1: Introduction EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
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.
Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? , Semester 1,
Introduction CPSC 388 Ellen Walker Hiram College.
Compiler Construction (CS-636)
INTRODUCTION TO COMPILERS(cond….) Prepared By: Mayank Varshney(04CS3019)
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.
Chapter 1: Introduction 1 Compiler Designs and Constructions Chapter 1: Introduction Objectives: Course Objectives Introduction Dr. Mohsen Chitsaz.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
What is a compiler? –A program that reads a program written in one language (source language) and translates it into an equivalent program in another language.
C Sc 132 Computing Theory Professor Meiliu Lu Computer Science Department.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
Mid-Terms Exam Scope and Introduction. Format Grades: 100 points -> 20% in the final grade Multiple Choice Questions –8 questions, 7 points each Short.
Chapter2 : Lexical Analysis
Objective of the course Understanding the fundamentals of the compilation technique Assist you in writing you own compiler (or any part of compiler)
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
Chapter 1 INTRODUCTION TO THE THEORY OF COMPUTATION.
Compiler principles Compiler Jakub Yaghob.
Language Recognition MSU CSE 260.
Compiler Design (40-414) Main Text Book:
PRINCIPLES OF COMPILER DESIGN
Introduction to Compiler Construction
CS 326 Programming Languages, Concepts and Implementation
Languages.
Chapter 3 Lexical Analysis.
Formal Language Theory
Compiler Lecture 1 CS510.
Review: Compiler Phases:
Lecture 4: Lexical Analysis & Chomsky Hierarchy
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Compiler Structures 1. Overview Objective
CSC312 Automata Theory Lecture # 2 Languages.
Languages Fall 2018.
Presentation transcript:

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

2 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

3 Introduction

4 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

5 Computer Organization Operating System Hardware Machine Compiler Applications

6 Compiler Compiler Source Code Target Code Error Messages

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

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

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

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

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

12 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

13 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

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

15 Reuse of Components Fortran PascalC Intermediate Code MIPSSPARCPentium

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

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

18 Formal Language Theory language grammarautomaton defineaccept generate automatically

19 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 strings of length 2

20 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

21 Transducers language L 1 grammar G 1 automaton define generate language L 2 define grammar G 2 accepttransduce

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

23 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

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

25 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

26 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

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

28 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 * =  Positive closure of L, L + L + =

29 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

30 Automatic Program Generators problem specification language program definesolve generate automatically

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

32 共勉 子曰︰ 「學而時習之,不亦說乎? 有朋自遠方來,不亦樂乎? 人不知而不慍,不亦君子乎? 」 -- 論語