1 CMPSC 160 Translation of Programming Languages Fall 2002 Instructor: Hugh McGuire slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon.

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

1 CIS 461 Compiler Design and Construction Fall 2014 Instructor: Hugh McGuire slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module.
Compiler Construction by Muhammad Bilal Zafar (AP)
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.
Lecture 01 - Introduction Eran Yahav 1. 2 Who? Eran Yahav Taub 734 Tel: Monday 13:30-14:30
Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
Compiler Construction
Overview of Compiler Design CIS 631, CSE 691, CIS400, CSE 400 Compiler Design Dr. Nancy McCracken January 15, 2008.
BİL744 Derleyici Gerçekleştirimi (Compiler Design)1.
Introduction & Overview CS4533 from Cooper & Torczon.
CPSC 388 – Compiler Design and Construction Lecture: MWF 11:00am-12:20pm, Room 106 Colton.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
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.
Compiler Construction1 COMP Compiler Construction Lecturer: Dr. Arthur Cater Teaching Assistant:
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.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
May 31, May 31, 2016May 31, 2016May 31, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University,
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
CPS 506 Comparative Programming Languages Syntax Specification.
Compiler design Lecture 1: Compiler Overview Sulaimany University 2 Oct
CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.
Introduction Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
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.
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
. n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates.
Introduction to Compiling
Introduction CPSC 388 Ellen Walker Hiram College.
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.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
1 Compiler Construction Vana Doufexi office CS dept.
Presented by : A best website designer company. Chapter 1 Introduction Prof Chung. 1.
CS416 Compiler Design1. 2 Course Information Instructor : Dr. Ilyas Cicekli –Office: EA504, –Phone: , – Course Web.
CS510 Compiler Lecture 1. Sources Lecture Notes Book 1 : “Compiler construction principles and practice”, Kenneth C. Louden. Book 2 : “Compilers Principles,
Prologue Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Advanced Computer Systems
Compiler Design (40-414) Main Text Book:
Chapter 1 Introduction.
CS510 Compiler Lecture 1.
Introduction to Compiler Construction
CS 326 Programming Languages, Concepts and Implementation
A Simple Syntax-Directed Translator
Chapter 1 Introduction.
PROGRAMMING LANGUAGES
课程名 编译原理 Compiling Techniques
Compiler Lecture 1 CS510.
CS 536 / Fall 2017 Introduction to programming languages and compilers
CS416 Compiler Design lec00-outline September 19, 2018
Introduction to Compiler Construction
Course supervisor: Lubna Siddiqui
Lecture 2: General Structure of a Compiler
Introduction CI612 Compiler Design CI612 Compiler Design.
CPSC 388 – Compiler Design and Construction
CSE401 Introduction to Compiler Construction
CS416 Compiler Design lec00-outline February 23, 2019
Introduction to Compiler Construction
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Construction
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Introduction to Compiler Construction
Presentation transcript:

1 CMPSC 160 Translation of Programming Languages Fall 2002 Instructor: Hugh McGuire slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module 1 Introduction to Compilers: An Overview

2 CMPSC Fall 2002 CMPSC160 - Translation of Programming Languages –Course Description: Study of the structure of compilers. Topics include: lexical analysis; syntax analysis including LL and LR parsers; type checking; run-time environments; intermediate code generation; and compiler-construction tools. Instructor: Hugh McGuire –Office: PHELP 1409B, phone: –Office Hours: M 9:00—10:30, W 10:30—12:00 Teaching Assistants: – –________ ________ –Office Hours: Grading (tentatively): –Homeworks 15%, Project 35%, Midterm 20%, Final 30%

3 CMPSC Fall 2001 Information about projects, homeworks, etc. are on the class web page – Send questions to Textbook: “Compilers: Principles, Techniques and Tools” –Authors: Aho, Sethi, and Ullman –This is a good reference book which covers the fundamental techniques in depth Reader: containing printouts of these lecture-notes –Will be available in a little less than a week at campus bookstore

4 Class Work Exams –Mid-term (20%) and final (30%) –In class, closed book, closed notes –Material from the lectures and the textbook Homeworks (15%) –Several homeworks –On material from the lectures and the textbook –Good practice for the exams Project (35%) –Four part project –A compiler for a small subset of Java written in Java –You will learn and use compiler construction tools such as Jlex and JavaCUP

5 Topics Overview of compilers Lexical analysis (Scanning) Syntactic analysis (Parsing) Context-sensitive analysis Type checking Runtime environments Symbol tables Intermediate representations Intermediate code generation Code optimization

6 Compilers What is a compiler? –A program that translates a program in one language (source language) into an equivalent program in another language (target language), and reports errors in the source program A compiler typically lowers the level of abstraction of the program C  assembly code for Sun Sparc Java  Java bytecode What is an interpreter? –A program that reads an executable program and produces the results of executing that program C is typically compiled Scheme is typically interpreted Java is compiled to bytecodes, which are then interpreted

7 Why build compilers? Compilers provide an essential interface between applications and architectures High level programming languages: –Increase programmer productivity –Better maintenance –Portable Low level machine details: –Instruction selection –Addressing modes –Pipelines –Registers and cache Compilers efficiently bridge the gap and shield the application developers from low level machine details

8 Why study compilers? Compilers embody a wide range of theoretical techniques and their application to practice –DFAs, PDAs, formal languages, formal grammars, fixpoints algorithms, lattice theory Compiler construction teaches programming and software engineering skills Compiler construction involves a variety of areas –theory, algorithms, systems, architecture The techniques used in various parts of compiler construction are useful in a wide variety of applications –Many practical applications have embedded languages, commands, macros, etc. Is compiler construction a solved problem? –No! New developments in programming languages (Java) and machine architectures (RISC processors) present new challenges

9 Desirable Properties of Compilers Compiler must generate a correct executable –The input program and the output program must be equivalent, the compiler should preserve the meaning of the input program Output program should run fast – For optimizing compilers we expect the output program to be more efficient than the input program Compiler itself should be fast Compiler should provide good diagnostics for programming errors Compiler should support separate compilation Compiler should work well with debuggers Optimizations should be consistent and predictable Compile time should be proportional to code size

10 What are the issues in compiler construction? Source code –written in a high level programming language //simple example while (sum < total) { sum = sum + x*10; } Target code –Assembly language (chapter 9) which in turn is translated to machine code L1:MOV total,R0 CMP sum,R0 CJ< L2 GOTOL3 L2:MOV #10,R0 MUL x,R0 ADD sum,R0 MOVR0,sum GOTO L1 L3:first instruction following the while statement

11 What is the input? Input to the compiler is not //simple example while (sum < total) { sum = sum + x*10; } Input to the compiler is //simple\bexample\nwhile\b(sum\b<\btotal)\b{\n\tsum\b= \bsum\b+\bx*10;\n}\n How does the compiler recognize the keywords, identifiers, the structure etc.?

12 First step: Lexical analysis (Scanning) The compiler scans the input file and produces a stream of tokens WHILE,LPAREN,,LT,,RPAREN,LBRACE,,EQ,,PLUS,,TIMES,, SEMICOL,RBRACE Each token has a corresponding lexeme, the character string that corresponds to the token –For example “ while ” is the lexeme for token WHILE –“ sum ”, “ x ”, “ total ” are lexemes for token ID

13 Lexical Analysis (Scanning) Compiler uses a set of patterns to specify valid tokens –tokens: LPAREN, ID, NUM, WHILE, etc. Each pattern is specified as a regular expression –LPAREN should match: ( –WHILE should match: while –ID should match: [a-zA-Z][0-9a-zA-Z]* It uses finite automata to recognize these patterns a-zA-Z 0-9a-zA-Z ID automaton

14 Lexical analysis (Scanning) During the scan the lexical analyzer gets rid of the white space ( \b,\t,\n, etc.) and comments Important additional task: Error messages! –var&1  Error! Not a token! –whle  Error? It matches the identifier token. Natural language analogy: Tokens correspond to words and punctuation symbols in a natural language

15 Next Step: Syntax Analysis (Parsing) How does the compiler recognize the structure of the program? –Loops, blocks, procedures, nesting? Parse the stream of tokens  parse tree Stmt WhileStmt WHILE LPAREN RPAREN Expr RelExpr LT Stmt Block LBRACE Stmt RBRACE AssignStmt EQ Expr SEMICOL ArithExpr Expr PLUS Expr ArithExpr Expr TIMES

16 Syntax Analysis (Parsing) The syntax a programming language is defined by a set of recursive rules. These sets of rules are called context free grammars. Stmt  WhileStmt | Block |... WhileStmt  WHILE LPAREN Expr RPAREN Stmt Expr  RelExpr | ArithExpr |... RelExpr ... Compilers apply these rules to produce the parse tree Again, important additional task: Error messages! –Missing semicolumn, missing parenthesis, etc. Natural language analogy: It is similar to parsing English text. Paragraphs, sentences, nounphrases, verbphrases, verbs, prepositions, articles, nouns, etc.

17 Intermediate Representations The parse tree representation has too many details –LPAREN, LBRACE, SEMICOL, etc. Once the compiler understands the structure of the input program it does not need these details (they were used to prevent ambiguities) Compilers generate a more abstract representation after constructing the parse tree which does not include the details of the derivation Abstract syntax trees (AST): Nodes represent operators, children represent operands while < assign + *

18 Next Step: Semantic (Context-Sensitive) Analysis Are variables declared before they are used? –We can find out if “ whle” is declared by looking at the symbol table Do variable types match? sum = sum + x*10; + * may become + * int2float sum x float int Symbol Table sum can be a floating point number, x can be an integer

19 Next Step: Code Generation Abstract syntax trees are a high-level intermediate representation used in earlier phases of the compilation There are lower level (i.e., closer to the machine code) intermediate representations –Three –address code (Chapter 8): every instruction has at most three operands –Jasmin: Assembly language for JVM (Java Virtual Machine) an abstract stack machine (used in the project) Intermediate code generation for these lower level representations and machine code generation are similar

20 Code Generation: Instruction Selection Source code a = b + c; d = a + e; Target code MOV b,R0 ADD c,R0 MOV R0,a MOV a,R0 ADD e,R0 MOV R0,d If we generate code for each statement separately we will not generate efficient code code for first statement code for second statement This instruction is redundant

21 Code Generation: Register Allocation There are a limited number of registers available on real machines Registers are valuable resources, the compiler has to use them efficiently t =a-b; u=a-c; v=t+u; d=v+u; d = (a-b)+(a-c)+(a-c);MOV a,R0 SUB b,R0 MOV a,R1 SUB c,R1 ADD R1,R0 MOV R0,d source codethree-address codeassembly code

22 Improving the Code: Code Optimization Compilers can improve the quality of code by static analysis –Data flow analysis, dependence analysis, code transformations, dead code elimination, etc. temp = x*10; while (sum < total) { sum = sum + temp; } while (sum < total) { sum = sum + x*10; } We do not need to recompute x*10 in each iteration of the loop transformation to more efficient code

23 Things to do Get the textbook Read chapters 1 and 2

24 Summary of this Lecture-Module Intro. Gen. Admin. of course Overview of compilation / topics of course Init. asst.