CHAPTER 5 Compiler 5.1 Basic Compiler Concepts. Basic Compiler Concepts 1. Lexical Analysis (Lexical Analyzer 或 Scanner) Read the source program one character.

Slides:



Advertisements
Similar presentations
Bottom up Parsing Bottom up parsing trys to transform the input string into the start symbol. Moves through a sequence of sentential forms (sequence of.
Advertisements

Compilers and Language Translation
UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-
1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
CPSC Compiler Tutorial 9 Review of Compiler.
Recap Mooly Sagiv. Outline Subjects Studied Questions & Answers.
1 Chapter 5 Compilers Source Code (with macro) Macro Processor Expanded Code Compiler or Assembler obj.
最新計算機概論 第 5 章 系統程式. 5-1 系統程式的類型 作業系統 (OS) : 介於電腦硬體與 應用軟體之間的 程式,除了提供 執行應用軟體的 環境,還負責分 配系統資源。
1 Contents Introduction A Simple Compiler Scanning – Theory and Practice Grammars and Parsing LL(1) Parsing Lex and yacc LR Parsing Semantic Processing.
1 Terminology l Statement ( 敘述 ) »declaration, assignment containing expression ( 運算式 ) l Grammar ( 文法 ) »a set of rules specify the form of legal statements.
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
Chapter 2 A Simple Compiler
Compiler Summary Mooly Sagiv html://
System Software Chih-Shun Hsu
1 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice Grammars and Parsing.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Invitation to Computer Science 5th Edition
5.3 Machine-Independent Compiler Features
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Course Revision Contents  Compilers  Compilers Vs Interpreters  Structure of Compiler  Compilation Phases  Compiler Construction Tools  A Simple.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
Syntax and Semantics Structure of programming languages.
1 Chapter 2 A Simple Compiler. 2 Outlines 2.1 The Structure of a Micro Compiler 2.2 A Micro Scanner 2.3 The Syntax of Micro 2.4 Recursive Descent Parsing.
Compiler1 Chapter V: Compiler Overview: r To study the design and operation of compiler for high-level programming languages. r Contents m Basic compiler.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Syntax and Semantics Structure of programming languages.
CPS 506 Comparative Programming Languages Syntax Specification.
Syntactic Analysis Operator-Precedence Parsing Recursive-Descent Parsing.
Topic #1: Introduction EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
Muhammad Idrees, Lecturer University of Lahore 1 Top-Down Parsing Top down parsing can be viewed as an attempt to find a leftmost derivation for an input.
Introduction CPSC 388 Ellen Walker Hiram College.
Chapter 1 Introduction Major Data Structures in Compiler
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
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.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
Compiler Syntactic Analysis r Two general classes of parsing techniques m Bottom-up (Operator-Precedence parsing) Begin with the terminal nodes.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
Syntax and Semantics Structure of programming languages.
Compiler Summary Lexical analysis—scanner – String of characters  token – Finite automata. Syntactical analysis – parser – Sequence of tokens –> language.
Compiler Design (40-414) Main Text Book:
Introduction Chapter : Introduction.
Chapter 1 Introduction.
A Simple Syntax-Directed Translator
CS 326 Programming Languages, Concepts and Implementation
Programming Languages Translator
Parsing and Parser Parsing methods: top-down & bottom-up
Textbook:Modern Compiler Design
Chapter 4 Syntax Analysis.
Chapter 1 Introduction.
PROGRAMMING LANGUAGES
Chapter 1: Introduction to Compiling (Cont.)
Compiler Lecture 1 CS510.
CMPE 152: Compiler Design December 5 Class Meeting
Lexical and Syntax Analysis
Introduction CI612 Compiler Design CI612 Compiler Design.
Compilers B V Sai Aravind (11CS10008).
Subject: Language Processor
BNF 9-Apr-19.
High-Level Programming Language
Chapter 10: Compilers and Language Translation
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Introduction Chapter : Introduction.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 2, 09/04/2003 Prof. Roy Levow.
Faculty of Computer Science and Information System
Presentation transcript:

CHAPTER 5 Compiler 5.1 Basic Compiler Concepts

Basic Compiler Concepts 1. Lexical Analysis (Lexical Analyzer 或 Scanner) Read the source program one character at a time, carving the some program into a sequence of atomic units called token. Token (token type, token value)

Basic Compiler Concepts

2. Syntax Analysis (Syntax Analyzer 或 Parser) The grammar specified the form, or syntax, of legal statements in the language.

Basic Compiler Concepts Parse Tree

Basic Compiler Concepts

3. Intermediate Code Generation Three Address Code (operator , operand1 , operand2 , Result) A=B+C (+ , B , C , A) SUM : =A/B*C ,可以被分解成 T1=A/B (/ , A , B , T1) T2=T1*C (* , T1 , C , T2) SUM=T2 (= , T2 , , SUM)

Basic Compiler Concepts SUM : =A/B*C ,可以被分解成 T1=A/B (/ , A , B , T1) T2=T1*C (* , T1 , C , T2) SUM=T2 (= , T2 , , SUM)

Basic Compiler Concepts 4. Code Optimization Improve the intermediate code (or machine code), so that the ultimate object program run fast and/or takes less space

Basic Compiler Concepts 5. Code Generation * Allocate memory location * Select machine code for each intermediate code * Register allocation: utilize registers as efficiently as possible (+ , B , C , A) 我們可以得到 MOV AX,B ADD AX,C MOV A,AX

Basic Compiler Concepts SUM : =A/B*C (/ , A , B , T1) MOV AX,A DIV B MOV T1,AX (* , T1 , C , T2) MOV AX,T1 MUL C MOV T2,AX (= , T2 , , SUM) MOV AX,T2 MOV SUM,AX

Basic Compiler Concepts (/ , A , B , T1) MOV AX,A DIV B MOV T1,AX (* , T1 , C , T2) MOV AX,T1 MUL C MOV T2,AX (= , T2 , , SUM) MOV AX,T2 MOV SUM,AX 再作一次碼的最佳化

Basic Compiler Concepts 6. Table Management and Error Handling Token, symbol table, reserved word table, delimiter table, constant table,… etc. * 五大功能之每一功能均做一次處理,如此就是五次處 理。 * 也可以把幾個功能合併在同一次處理。 * 它至少是二次處理。

Grammar 5.2 Grammar 1. Grammar Backus Naur Form Grammar consists of a set of rules, each which defines the syntax of some construct in the programming language. Non-terminal symbol Terminal symbol

Grammar 2. Parse Tree (Syntax Tree) It is often convenient to display the analysis of source statement in terms of a grammar as a tree.

Grammar 3. Precedence and associativity Precedence *, / > +, - Associativity a + b + c ( (a + b) + c) Left associativity Right associativity

Grammar 4. Ambiguous Grammar There is more than one possible parse tree for a given statement.

Grammar Ambiguous Grammar

Lexical Analysis 5.3 Lexical Analysis Program 內有下列幾類 Token: a. Identifier b. Delimiter c. Reserved Word d. Constant integer, float, string 1. Identifier ::= | | ::= A | B | C | ….. ::= 0 | 1 | 2 |….. Multiple character token

Lexical Analysis 2. Token and Tables

Lexical Analysis 2. Token and Tables

Lexical Analysis 2. Token and Tables

Lexical Analysis 2. Token and Tables

Lexical Analysis 2. Token and Tables Token Specifier (Token Type, Token Value) Table Entry

Syntax Analysis 5.4 Syntax Analysis 1. Building the Parse Tree a. Top down method Begin with the rule of the grammar, and attempt to construct the tree so that the terminal nodes match the statements being analyzed. b. Bottom up method Begin with the terminal nodes of the tree, and attempt to combine these into successively high level nodes until the root is reached.

Syntax Analysis * Top down method Begin with the rule of the grammar, and attempt to construct the tree so that the terminal nodes match the statements being analyzed.

Syntax Analysis * Bottom up method Begin with the terminal nodes of the tree, and attempt to combine these into successively high level nodes until the root is reached.

Syntax Analysis 2. Operator Precedence Parser Bottom up parser Precedence Matrix

Syntax Analysis Stack input < READ(id); <READ (id) <READ = ( id) <READ = ( <id ) ) <READ = ( = id-list ) read

Syntax Analysis Stack input < id + id - id + id - id <term + id - id - id <term - < id term

Syntax Analysis Stack input < id + id - id + id - id <term + id - id - id <term - < id term Generally use a stack to save tokens that have been scanned but not yet parsed

Syntax Analysis 3. Recursive Descent Parser Top down method a. leftmost derivation It must be possible to decide which alternative to used by examining the next input token id, READ, WRITE

Syntax Analysis b. left recursive Top down parser can not be used with grammar that contains left recursive. Because unable to decide between its alternatives tokens. both id and can begin with id.

Syntax Analysis Modified for recursive descent parser

Code Generation 5.5 Code Generation When the parser recognizes a portion of the source program according to some rule of grammar, the corresponding routine is executed. Semantic Routine or Code Generation Routines 1.Operator precedence parser When sub-string is reduced to nonterminal 2.Recursive descent parser When procedure return to its caller, indicating success.

Code Generation ::= MOV AX, 1 ADD AX, 2 MOV, AX ::= MOV AX, 1 SUB AX, 2 MOV, AX ::= id add id to

Code Generation 直接產生 Assembly instructions 或 Machine codes 太細 故先翻成 Intermediate Form

Intermediate Form 5.6 Intermediate Form Three Address Code (Quadruple Form) (operator , operand1 , operand2 , Result) ::= (+, 1, 2, ) ::= (-, 1, 2, ) ::= id add id to

Intermediate Form Variance := sumsq DIV mean * mean (DIV, sumsq, #100, i1) (*, mean, mean, i2) (-, i1, i2, i3) (:=, i3,, variance)

Machine Independent Compiler Features 5.7 Machine Independent Compiler Features 1. Storage Allocation a. Storage Allocation * Static Allocation Allocate at compiler time * Dynamic Allocation Allocate at run time Auto : Function call STACK Controlled : malloc( ), free( ) HEAP

Machine Independent Compiler Features 2. Activation Record Each function call creates an activation record that contains storage for all the variables used by the function, return address,… etc.

Machine Independent Compiler Features Activation Record MAIN To OS

Machine Independent Compiler Features Activation Record MAIN SUB To OS

Machine Independent Compiler Features Activation Record MAIN SUB To OS

Machine Independent Compiler Features 3. Prologue and Epilogue The compiler must generate additional code to manage the activation records themselves. a. Prologue The code to create a new activation record b. Epilogue The code to delete the current activation record

Machine Independent Compiler Features 4. Structure Variables Array, Record, String, Set …..

Machine Independent Compiler Features Type B[a-b] [c-d] Address of B[s][t] Row Major [(s - a) *(d - c +1) + (t - c) ] * sizeof(Type) + Base address Column Major [(t - c) *(b - a +1) + (s - a) ] * sizeof(Type) + Base address

Machine Independent Compiler Features 5. Code Optimization For I:= 1 to 10 Begin x[I, 2*J-1] := T[I, 2*J]; Table[I] := 2**I; END T1:= 2 *J; T2 := T1 - 1; K := 1; For I:= 1 to 10 Begin x[I, T2] := T[I, T1]; K := K * 2; Table[I] := K; END a. Common Sub-expression b. Loop In-variants c. Reduction in Strength

Compiler Design Option 5.8 Compiler Design Option 1. Interpreter An interpreter processes a source program written in a high level language, just as a compiler does. The main difference is that interpreters execute a version of the source directly. An interpreter can be viewed as a set of functions, the execution of these functions is driven by the internal form of the program.

Compiler Design Option 2. P Code Compiler * P Code 就是 Byte Code, 是一種與機器無關 (Machine Independent) 的語言 * 可以跨平台在不同種類的電腦內執行。

Compiler Design Option 3. Compiler-Compiler A software tool that can be used to help in the task of compiler construction. Uses Finite State Automata YACC Parser Generator LEX Scanner Generator Unix

Compiler Design Option 4. Cross Compiler