Chapter 10: Compilers and Language Translation

Slides:



Advertisements
Similar presentations
Programming Languages Third Edition Chapter 6 Syntax.
Advertisements

Compilers and Language Translation
CPSC Compiler Tutorial 9 Review of Compiler.
Chapter 9 Compilers and Language Translation. The Compilation Process Phase I: Lexical analysis Phase I: Lexical analysis Phase II: Parsing Phase II:
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
BİL744 Derleyici Gerçekleştirimi (Compiler Design)1.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Invitation to Computer Science 5th Edition
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
INTRODUCTION TO COMPUTING CHAPTER NO. 06. Compilers and Language Translation Introduction The Compilation Process Phase 1 – Lexical Analysis Phase 2 –
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.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
LANGUAGE TRANSLATORS: WEEK 3 LECTURE: Grammar Theory Introduction to Parsing Parser - Generators TUTORIAL: Questions on grammar theory WEEKLY WORK: Read.
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.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Review Joey Paquet,
Chapter 6 Programming Languages (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
D. M. Akbar Hussain: Department of Software & Media Technology 1 Compiler is tool: which translate notations from one system to another, usually from source.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Parsing Introduction Syntactic Analysis I. Parsing Introduction 2 The Role of the Parser The Syntactic Analyzer, or Parser, is the heart of the front.
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
1. 2 Preface In the time since the 1986 edition of this book, the world of compiler design has changed significantly 3.
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.
The Functions and Purposes of Translators Syntax (& Semantic) Analysis.
Introduction to Compiling
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 Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
CSC 4181 Compiler Construction
ICS312 Introduction to Compilers Set 23. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
CS416 Compiler Design1. 2 Course Information Instructor : Dr. Ilyas Cicekli –Office: EA504, –Phone: , – Course Web.
Objective of the course Understanding the fundamentals of the compilation technique Assist you in writing you own compiler (or any part of compiler)
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
CHAPTER 1 INTRODUCTION TO COMPILER SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
CS 3304 Comparative Languages
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
Advanced Computer Systems
Compiler Design (40-414) Main Text Book:
Chapter 1 Introduction.
Introduction to Compiler Construction
Describing Syntax and Semantics
A Simple Syntax-Directed Translator
CS510 Compiler Lecture 4.
Overview of Compilation The Compiler Front End
Overview of Compilation The Compiler Front End
Chapter 1 Introduction.
PROGRAMMING LANGUAGES
-by Nisarg Vasavada (Compiled*)
Compiler Lecture 1 CS510.
CS416 Compiler Design lec00-outline September 19, 2018
Course supervisor: Lubna Siddiqui
Compiler Design 4. Language Grammars
Introduction CI612 Compiler Design CI612 Compiler Design.
Compilers B V Sai Aravind (11CS10008).
R.Rajkumar Asst.Professor CSE
CS 3304 Comparative Languages
CS 3304 Comparative Languages
Programming Languages 2nd edition Tucker and Noonan
CS416 Compiler Design lec00-outline February 23, 2019
Chapter 3 Describing Syntax and Semantics.
High-Level Programming Language
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
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 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition

Objectives In this chapter, you will learn about The compilation process Phase I: Lexical analysis Phase II: Parsing Phase III: Semantics and code generation Phase IV: Code optimization Invitation to Computer Science, Java Version, Third Edition

Introduction High-level language instructions must be translated into machine language prior to execution Compiler A piece of system software that translates high- level languages into machine language Invitation to Computer Science, Java Version, Third Edition

Introduction (continued) Goals of a compiler when performing a translation Correctness Producing a reasonably efficient and concise machine language code Invitation to Computer Science, Java Version, Third Edition

General Structure of a Compiler Figure 10.1 General Structure of a Compiler Invitation to Computer Science, Java Version, Third Edition

The Compilation Process Phase I: Lexical analysis Compiler examines the individual characters in the source program and groups them into syntactical units called tokens Phase II: Parsing The sequence of tokens formed by the scanner is checked to see whether it is syntactically correct Invitation to Computer Science, Java Version, Third Edition

The Compilation Process (continued) Phase III: Semantic analysis and code generation The compiler analyzes the meaning of the high-level language statement and generates the machine language instructions to carry out these actions Phase IV: Code optimization The compiler takes the generated code and sees whether it can be made more efficient Invitation to Computer Science, Java Version, Third Edition

Overall Execution Sequence on a High-Level Language Program Figure 10.2 Overall Execution Sequence on a High-Level Language Program Invitation to Computer Science, Java Version, Third Edition

The Compilation Process (continued) Final step Object program is written to an object file Source program Original high-level language program Object program Machine language translation of the source program Invitation to Computer Science, Java Version, Third Edition

Phase I: Lexical Analysis Lexical analyzer The program that performs lexical analysis More commonly called a scanner Job of lexical analyzer Group input characters into tokens Tokens: Syntactical units that are treated as single, indivisible entities for the purposes of translation Classify tokens according to their type Invitation to Computer Science, Java Version, Third Edition

Typical Token Classifications Figure 10.3 Typical Token Classifications Invitation to Computer Science, Java Version, Third Edition

Phase I: Lexical Analysis (continued) Input to a scanner A high-level language statement from the source program Scanner’s output A list of all the tokens in that statement The classification number of each token found Invitation to Computer Science, Java Version, Third Edition

Phase II: Parsing Introduction Parsing phase A compiler determines whether the tokens recognized by the scanner are a syntactically legal statement Performed by a parser Invitation to Computer Science, Java Version, Third Edition

Phase II: Parsing Introduction (continued) Output of a parser A parse tree, if such a tree exists An error message, if a parse tree cannot be constructed Successful construction of a parse tree is proof that the statement is correctly formed Invitation to Computer Science, Java Version, Third Edition

Example High-level language statement: a = b + c Invitation to Computer Science, Java Version, Third Edition

Grammars, Languages, and BNF Syntax The grammatical structure of the language The parser must be given the syntax of the language BNF (Backus-Naur Form) Most widely used notation for representing the syntax of a programming language Invitation to Computer Science, Java Version, Third Edition

Grammars, Languages, and BNF (continued) In BNF The syntax of a language is specified as a set of rules (also called productions) A grammar The entire collection of rules for a language Structure of an individual BNF rule left-hand side ::= “definition” Invitation to Computer Science, Java Version, Third Edition

Grammars, Languages, and BNF (continued) BNF rules use two types of objects on the right-hand side of a production Terminals The actual tokens of the language Never appear on the left-hand side of a BNF rule Nonterminals Intermediate grammatical categories used to help explain and organize the language Must appear on the left-hand side of one or more rules Invitation to Computer Science, Java Version, Third Edition

Grammars, Languages, and BNF (continued) Goal symbol The highest-level nonterminal The nonterminal object that the parser is trying to produce as it builds the parse tree All nonterminals are written inside angle brackets Invitation to Computer Science, Java Version, Third Edition

Parsing Concepts and Techniques Fundamental rule of parsing By repeated applications of the rules of the grammar If the parser can convert the sequence of input tokens into the goal symbol, the sequence of tokens is a syntactically valid statement of the language If the parser cannot convert the input tokens into the goal symbol, the sequence of tokens is not a syntactically valid statement of the language Invitation to Computer Science, Java Version, Third Edition

Parsing Concepts and Techniques (continued) One of the biggest problems in building a compiler is designing a grammar that Includes every valid statement that we want to be in the language Excludes every invalid statement that we do not want to be in the language Invitation to Computer Science, Java Version, Third Edition

Parsing Concepts and Techniques (continued) Another problem in constructing a compiler: Designing a grammar that is not ambiguous An ambiguous grammar allows the construction of two or more distinct parse trees for the same statement Invitation to Computer Science, Java Version, Third Edition

Phase III: Semantics and Code Generation Semantic analysis The compiler makes a first pass over the parse tree to determine whether all branches of the tree are semantically valid If they are valid, the compiler can generate machine language instructions If not, there is a semantic error; machine language instructions are not generated Invitation to Computer Science, Java Version, Third Edition

Phase III: Semantics and Code Generation (continued) Compiler makes a second pass over the parse tree to produce the translated code Invitation to Computer Science, Java Version, Third Edition

Phase IV: Code Optimization Two types of optimization Local Global Local optimization The compiler looks at a very small block of instructions and tries to determine how it can improve the efficiency of this local code block Relatively easy; included as part of most compilers Invitation to Computer Science, Java Version, Third Edition

Phase IV: Code Optimization (continued) Examples of possible local optimizations Constant evaluation Strength reduction Eliminating unnecessary operations Invitation to Computer Science, Java Version, Third Edition

Phase IV: Code Optimization (continued) Global optimization The compiler looks at large segments of the program to decide how to improve performance Much more difficult; usually omitted from all but the most sophisticated and expensive production-level “optimizing compilers” Optimization cannot make an inefficient algorithm efficient Invitation to Computer Science, Java Version, Third Edition

Summary A compiler is a piece of system software that translates high-level languages into machine language Goals of a compiler: Correctness and the production of efficient and concise code Source program: High-level language program Invitation to Computer Science, Java Version, Third Edition

Summary (continued) Object program: The machine language translation of the source program Phases of the compilation process Phase I: Lexical analysis Phase II: Parsing Phase III: Semantic analysis and code generation Phase IV: Code optimization Invitation to Computer Science, Java Version, Third Edition