Lecture 18 Compilers and Language Translation (S&G, ch. 9)

Slides:



Advertisements
Similar presentations
Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00.
Advertisements

Intermediate Code Generation
Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
Compilers and Language Translation
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Loops – While, Do, For Repetition Statements Introduction to Arrays
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Invitation to Computer Science 5th Edition
High-Level Programming Languages: C++
CIS Computer Programming Logic
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.
TMF1013 : Introduction To Computing Lecture 1 : Fundamental of Computer ComputerFoudamentals.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
Computer Science 101 How the Assembler Works. Assembly Language Programming.
Chapter 6 Programming Languages (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
CS 153: Concepts of Compiler Design September 16 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
The Functions and Purposes of Translators Syntax (& Semantic) Analysis.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
Code Generation CPSC 388 Ellen Walker Hiram College.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
Language Translation Part 2: Finite State Machines.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 6: Stepwise refinement revisited, Midterm review.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Component 1.6.
CS 153: Concepts of Compiler Design September 14 Class Meeting
Compiler Design (40-414) Main Text Book:
CS 3304 Comparative Languages
CS 326 Programming Languages, Concepts and Implementation
Compiler Construction (CS-636)
Ch. 7 Programming Languages
CS 363 – Chapter 1 What is a programming language? Kinds of languages
CSE 311 Foundations of Computing I
PROGRAMMING LANGUAGES
Compiler Lecture 1 CS510.
CS 536 / Fall 2017 Introduction to programming languages and compilers
Compiler Construction
CS 240 – Lecture 11 Pseudocode.
Course supervisor: Lubna Siddiqui
Chapter 10 Programming Fundamentals with JavaScript
CSCE Fall 2013 Prof. Jennifer L. Welch.
Chapter 6 Intermediate-Code Generation
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSE401 Introduction to Compiler Construction
CSCE 121: Simple Computer Model Spring 2015
ADTs, Grammars, Parsing, Tree traversals
Sequencing, Selection, and Loops in Machine Language
CSCE Fall 2012 Prof. Jennifer L. Welch.
Programming Fundamentals (750113) Ch1. Problem Solving
Understand the interaction between computer hardware and software
Lecture 13 Introduction to High-Level Programming (S&G, §§7.1–7.6)
High-Level Programming Language
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 10: Compilers and Language Translation
Building Java Programs
CPSC 171 Introduction to Computer Science
Intro to Programming (in JavaScript)
Presentation transcript:

Lecture 18 Compilers and Language Translation (S&G, ch. 9) 6/4/2019 4:43 AM Lecture 18 Compilers and Language Translation (S&G, ch. 9) 6/4/2019 CS 100 - Lecture 18 CS 100

Read S&G ch. 10 (Models of Computation) 6/4/2019 CS 100 - Lecture 18

Program Development Editor Not OK (error messages) Compiler Lecture 18 6/4/2019 4:43 AM Program Development Editor Create high-level language program Compiler Check for syntax errors and translate to machine code Not OK (error messages) Run-time system Execute the machine language program on any machine OK 6/4/2019 CS 100 - Lecture 18 slide < S. Levy CS 100

Compiler: A Programming Language Translator Lecture 18 6/4/2019 4:43 AM Compiler: A Programming Language Translator .begin clear sum load one store count while: load count compare eleven jumpeq endwhile add sum store sum increment count jump while endwhile: out sum halt one: .data 1 eleven: .data 11 sum: .data 0 count: .data 0 .end Set sum to 0 Set count to 1 While count <= 10 do Set sum to sum + count Increment count Output sum Compile 6/4/2019 CS 100 - Lecture 18 slide < S. Levy CS 100

Compiler: Requirements / Tasks Lecture 18 6/4/2019 4:43 AM Compiler: Requirements / Tasks Recognize programming constructs (conditionals, loops) and translate them to assembly language (compare, jump) Reject and report unrecognized programs Keep track of symbols (sum, count) and declare each one using a .data directive Generate efficient assembly language code 6/4/2019 CS 100 - Lecture 18 slide < S. Levy CS 100

Recognizing Constructs: Parsing Lecture 18 Recognizing Constructs: Parsing 6/4/2019 4:43 AM Could try to translate, e.g., from every possible if/else to its corresponding assembly code: load x compare y jumpgt outx out y jump next outx: out x next: .... Rule #18734 if x > y output x else output y 6/4/2019 CS 100 - Lecture 18 slide < S. Levy CS 100

Recognizing Constructs: Parsing (2) Lecture 18 Recognizing Constructs: Parsing (2) 6/4/2019 4:43 AM Could try to translate, e.g., from every possible if/else to its corresponding assembly code: load a compare b jumplt outa out b jump next outa: out a next: .... Rule #22102 if a < b output a else output b Impossible, because we’d need an infinite number of rules! 6/4/2019 CS 100 - Lecture 18 slide < S. Levy CS 100

Recognizing Constructs: Parsing (3) Lecture 18 Recognizing Constructs: Parsing (3) 6/4/2019 4:43 AM Instead, we recognize (abstract) structures, and work on their components one at a time: if/else output output < a b a b 6/4/2019 CS 100 - Lecture 18 slide < S. Levy CS 100

Recognizing Constructs: Parsing (4) Lecture 18 Recognizing Constructs: Parsing (4) 6/4/2019 4:43 AM Now we can work on smaller pieces: < a b a < b Parse Generate load a compare b When parsing fails, compiler reports a syntax error: a << b Parse Unrecognized operator: << 6/4/2019 CS 100 - Lecture 18 slide < S. Levy CS 100

BNF Grammar A notation for expressing all the allowable statements in a language A finite description of an infinite number of possibilities Each distinct arrangement is shown in schematic form Because languages are nested hierarchically, the definitions are recursive 6/4/2019 CS 100 - Lecture 18

Example Grammar assignment statement ::= variable = expression expression ::= variable | expression arith op variable | ( expression ) arith op ::= + | – | * | / variable ::= x | y | z  Unrealistic! 6/4/2019 CS 100 - Lecture 18

Additional Example if statement ::= if ( Boolean expression ) assignment statement ; else clause Boolean expression ::= variable | expression relation expression relation ::= == | < | > else clause ::= else assignment statement |  6/4/2019 CS 100 - Lecture 18

Keeping Track of Symbols: Symbol Tables Lecture 18 6/4/2019 4:43 AM Keeping Track of Symbols: Symbol Tables When a symbol is first set, compiler should declare it: Set sum to 0 Compile sum: .data 0 Subsequent references to symbol don't need a new declaration: Set sum to sum + count Compile load sum add count store sum 6/4/2019 CS 100 - Lecture 18 slide < S. Levy CS 100

Keeping Track of Symbols: Symbol Tables (2) Lecture 18 6/4/2019 4:43 AM Keeping Track of Symbols: Symbol Tables (2) Failure to set initial value should result in an error: Set sum to 0 While count <= 10 do “count” not declared Compiler uses a symbol table (dictionary; phonebook) to keep track of values we’ve declared 6/4/2019 CS 100 - Lecture 18 slide < S. Levy CS 100

Lecture 18 6/4/2019 4:43 AM Symbol Tables In a language like Java, symbol table would contain info about variable types: a: int b: int s: String x: double Then compiler can use symbol table to detect “type errors”: can't set int to String a = a + s; 6/4/2019 CS 100 - Lecture 18 slide < S. Levy CS 100

Optimization: Generating Efficient Assembly Code Lecture 18 6/4/2019 4:43 AM Optimization: Generating Efficient Assembly Code “Laundry metaphor”: Unoptimized Load dirty clothes into washer Run washer Unload wet clothes from washer to basket Load wet clothes into dryer from basket Run dryer Unoptimized version allows us to break down the task into simple parts Requires less coordination between washer & dryer 6/4/2019 CS 100 - Lecture 18 slide < S. Levy CS 100

Optimization: Generating Efficient Assembly Code (2) Lecture 18 6/4/2019 4:43 AM Optimization: Generating Efficient Assembly Code (2) “Laundry metaphor”: Optimized Load dirty clothes into washer Run washer Load wet clothes from washer to dryer Run dryer Optimized version saves a step and doesn't require a basket Requires more coordination between washer & dryer 6/4/2019 CS 100 - Lecture 18 slide < S. Levy CS 100

Lecture 18 6/4/2019 4:43 AM Optimization Set a to a + b Output a Set a to a + c load a add b store a out a add c unoptimized load a add b store a out a add c optimized Note coordination required between first and third steps of pseudocode 6/4/2019 CS 100 - Lecture 18 slide < S. Levy CS 100