9/18/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.

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

Programming Language Concepts
Greg MorrisettFall  Compilers.  (duh)  Translating one programming language into another.  Also interpreters.  Translating and running a language.
Chapter 4 Lexical and Syntax Analysis Sections 1-4.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
UMBC Introduction to Compilers CMSC 431 Shon Vick 01/28/02.
Programming Languages and Compilers (CS 421)
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
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.
10/3/20151 Programming Languages and Compilers (CS 421) Choonghwan Lee Based in part on slides by Mattox Beckman,
Lexical Analysis - An Introduction. The Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source.
Lexical Analysis - An Introduction Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
Chapter 6 Programming Languages (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
C H A P T E R TWO Syntax and Semantic.
10/25/20151 Programming Languages and Compilers (CS 421) Grigore Rosu 2110 SC, UIUC Slides by Elsa Gunter, based.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
TRANSITION DIAGRAM BASED LEXICAL ANALYZER and FINITE AUTOMATA Class date : 12 August, 2013 Prepared by : Karimgailiu R Panmei Roll no. : 11CS10020 GROUP.
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.
Introduction to Compiling
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
INTRODUCTION TO COMPILERS(cond….) Prepared By: Mayank Varshney(04CS3019)
ISBN Chapter 3 Describing Syntax and Semantics.
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.
The Model of Compilation Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
10/16/081 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
2/1/20161 Programming Languages and Compilers (CS 421) Grigore Rosu 2110 SC, UIUC Slides by Elsa Gunter, based in.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
CSC 4181 Compiler Construction
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.
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.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
3/20/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman,
6/21/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman,
Programming Languages and Compilers (CS 421)
Principles of programming languages 12: Functional programming
Programming Languages and Compilers (CS 421)
CS 326 Programming Languages, Concepts and Implementation
A Simple Syntax-Directed Translator
SOFTWARE DESIGN AND ARCHITECTURE
PROGRAMMING LANGUAGES
Programming Languages and Compilers (CS 421)
Compiler Construction
Compiler Lecture 1 CS510.
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Introduction CI612 Compiler Design CI612 Compiler Design.
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Compiler design.
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
High-Level Programming Language
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
COMPILER CONSTRUCTION
Programming Languages and Compilers (CS 421)
Presentation transcript:

9/18/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox Beckman, as updated by Vikram Adve and Gul Agha

9/18/ Background for Unification Terms made from constructors and variables (for the simple first order case) Constructors may be applied to arguments (other terms) to make new terms Variables and constructors with no arguments are base cases Constructors applied to different number of arguments (arity) considered different Substitution of terms for variables

9/18/ Simple Implementation Background type term = Variable of string | Const of (string * term list) let rec subst var_name residue term = match term with Variable name -> if var_name = name then residue else term | Const (c, tys) -> Const (c, List.map (subst var_name residue) tys);;

9/18/ Unification Problem Given a set of pairs of terms (“equations”) {(s 1, t 1 ), (s 2, t 2 ), …, (s n, t n )} (the unification problem) does there exist a substitution  (the unification solution) of terms for variables such that  (s i ) =  (t i ), for all i = 1, …, n?

9/18/ Uses for Unification Type Inference and type checking Pattern matching as in OCAML Can use a simplified version of algorithm Logic Programming - Prolog Simple parsing

9/18/ Unification Algorithm Let S = {(s 1 = t 1 ), (s 2 = t 2 ), …, (s n = t n )} be a unification problem. Case S = { }: Unif(S) = Identity function (i.e., no substitution) Case S = {(s, t)}  S’: Four main steps

9/18/ Unification Algorithm Delete: if s = t (they are the same term) then Unif(S) = Unif(S’) Decompose: if s = f(q 1, …, q m ) and t =f(r 1, …, r m ) (same f, same m!), then Unif(S) = Unif({(q 1, r 1 ), …, (q m, r m )}  S’) Orient: if t = x is a variable, and s is not a variable, Unif(S) = Unif ({(x = s)}  S’)

9/18/ Unification Algorithm Eliminate: if s = x is a variable, and x does not occur in t (the occurs check), then Let  = {x  t} Let  = Unif(  (S’)) Unif(S) = {x   (t)} o  Note: {x  a} o {y  b} = {y  ({x  a}(b))} o {x  a} if y not in a

9/18/ Tricks for Efficient Unification Don’t return substitution, rather do it incrementally Make substitution be constant time Requires implementation of terms to use mutable structures (or possibly lazy structures) We won’t discuss these

9/18/ Example x,y,z variables, f,g constructors Unify {(f(x) = f(g(f(z),y))), (g(y,y) = x)} = ?

9/18/ Example x,y,z variables, f,g constructors S = {(f(x) = f(g(f(z),y))), (g(y,y) = x)} is nonempty Unify {(f(x) = f(g(f(z),y))), (g(y,y) = x)} = ?

9/18/ Example x,y,z variables, f,g constructors Pick a pair: (g(y,y) = x) Unify {(f(x) = f(g(f(z),y))), (g(y,y) = x)} = ?

9/18/ Example x,y,z variables, f,g constructors Pick a pair: (g(y,y)) = x) Orient: (x = g(y,y)) Unify {(f(x) = f(g(f(z),y))), (g(y,y) = x)} = Unify {(f(x) = f(g(f(z),y))), (x = g(y,y))} by Orient

9/18/ Example x,y,z variables, f,g constructors Unify {(f(x) = f(g(f(z),y))), (x = g(y,y))} = ?

9/18/ Example x,y,z variables, f,g constructors {(f(x) = f(g(f(z),y))), (x = g(y,y))} is non- empty Unify {(f(x) = f(g(f(z),y))), (x = g(y,y))} = ?

9/18/ Example x,y,z variables, f,g constructors Pick a pair: (x = g(y,y)) Unify {(f(x) = f(g(f(z),y))), (x = g(y,y))} = ?

9/18/ Example x,y,z variables, f,g constructors Pick a pair: (x = g(y,y)) Eliminate x with substitution {x  g(y,y)} Check: x not in g(y,y). Unify {(f(x) = f(g(f(z),y))), (x = g(y,y))} = ?

9/18/ Example x,y,z variables, f,g constructors Pick a pair: (x = g(y,y)) Eliminate x with substitution {x  g(y,y)} Unify {(f(x) = f(g(f(z),y))), (x = g(y,y))} = Unify {(f(g(y,y)) = f(g(f(z),y)))} o {x  g(y,y)}

9/18/ Example x,y,z variables, f,g constructors Unify {(f(g(y,y)) = f(g(f(z),y)))} o {x  g(y,y)} = ?

9/18/ Example x,y,z variables, f,g constructors {(f(g(y,y)) = f(g(f(z),y)))} is non-empty Unify {(f(g(y,y)) = f(g(f(z),y)))} o {x  g(y,y)} = ?

9/18/ Example x,y,z variables, f,g constructors Pick a pair: (f(g(y,y)) = f(g(f(z),y))) Unify {(f(g(y,y)) = f(g(f(z),y)))} o {x  g(y,y)} = ?

9/18/ Example x,y,z variables, f,g constructors Pick a pair: (f(g(y,y)) = f(g(f(z),y))) Decompose:(f(g(y,y)) = f(g(f(z),y))) becomes {(g(y,y) = g(f(z),y))} Unify {(f(g(y,y)) = f(g(f(z),y)))} o {x  g(y,y)} = Unify {(g(y,y) = g(f(z),y))} o {x  g(y,y)}

9/18/ Example x,y,z variables, f,g constructors {(g(y,y) = g(f(z),y))} is non-empty Unify {(g(y,y) = g(f(z),y))} o {x  g(y,y)} = ?

9/18/ Example x,y,z variables, f,g constructors Pick a pair: (g(y,y) = g(f(z),y)) Unify {(g(y,y) = g(f(z),y))} o {x  g(y,y)} = ?

9/18/ Example x,y,z variables, f,g constructors Pick a pair: (f(g(y,y)) = f(g(f(z),y))) Decompose: (g(y,y)) = g(f(z),y)) becomes {(y = f(z)); (y = y)} Unify {(g(y,y) = g(f(z),y))} o {x  g(y,y)} = Unify {(y = f(z)); (y = y)} o {x  g(y,y)}

9/18/ Example x,y,z variables, f,g constructors Unify {(y = f(z)); (y = y)} o {x  g(y,y)} = ?

9/18/ Example x,y,z variables, f,g constructors {(y = f(z)); (y = y)} o {x  g(y,y) is non- empty Unify {(y = f(z)); (y = y)} o {x  g(y,y)} = ?

9/18/ Example x,y,z variables, f,g constructors Pick a pair: (y = f(z)) Unify {(y = f(z)); (y = y)} o {x  g(y,y)} = ?

9/18/ Example x,y,z variables, f,g constructors Pick a pair: (y = f(z)) Eliminate y with {y  f(z)} Unify {(y = f(z)); (y = y)} o {x  g(y,y)} = Unify {(f(z) = f(z))} o {y  f(z)} o {x  g(y,y)}= Unify {(f(z) = f(z))} o {y  f(z); x  g(f(z), f(z))}

9/18/ Example x,y,z variables, f,g constructors Unify {(f(z) = f(z))} o {y  f(z); x  g(f(z), f(z))} = ?

9/18/ Example x,y,z variables, f,g constructors {(f(z) = f(z))} is non-empty Unify {(f(z) = f(z))} o {y  f(z); x  g(f(z), f(z))} = ?

9/18/ Example x,y,z variables, f,g constructors Pick a pair: (f(z) = f(z)) Unify {(f(z) = f(z))} o {y  f(z); x  g(f(z), f(z))} = ?

9/18/ Example x,y,z variables, f,g constructors Pick a pair: (f(z) = f(z)) Delete Unify {(f(z) = f(z))} o {y  f(z); x  g(f(z), f(z))} = Unify {} o {y  f(z); x  g(f(z), f(z))}

9/18/ Example x,y,z variables, f,g constructors Unify {} o {y  f(z); x  g(f(z), f(z))} = ?

9/18/ Example x,y,z variables, f,g constructors {} is empty Unify {} = identity function Unify {} o {y  f(z); x  g(f(z), f(z))} = {y  f(z); x  g(f(z), f(z))}

9/18/ Example Unify {(f(x) = f(g(f(z),y))), (g(y,y) = x)} = {y  f(z); x  g(f(z), f(z))} f( x ) = f(g(f(z), y ))  f(g(f(z), f(z))) = f(g(f(z), f(z))). g( y, y ) = x.  g(f(z),f(z)) = g(f(z), f(z)).

9/18/ Example of Failure: Decompose Unify{(f(x,g(y)) = f(h(y),x))} Decompose: (f(x,g(y)) = f(h(y),x)) = Unify {(x = h(y)), (g(y) = x)} Orient: (g(y) = x) = Unify {(x = h(y)), (x = g(y))} Eliminate: (x = h(y)) Unify {(h(y), g(y))} o {x  h(y)} No rule to apply! Decompose fails!

9/18/ Example of Failure: Occurs Check Unify{(f(x,g(x)) = f(h(x),x))} Decompose: (f(x,g(x)) = f(h(x),x)) = Unify {(x = h(x)), (g(x) = x)} Orient: (g(y) = x) = Unify {(x = h(x)), (x = g(x))} No rules apply.

Major Phases of a Compiler Source Program Lex Tokens Parse Abstract Syntax Semantic Analysis Symbol Table Translate Intermediate Representation Modified from “Modern Compiler Implementation in ML”, by Andrew Appel Instruction Selection Optimized Machine-Specific Assembly Language Optimize Unoptimized Machine- Specific Assembly Language Emit code Assembler Relocatable Object Code Assembly Language Linker Machine Code Optimize Optimized IR

9/18/ Meta-discourse Language Syntax and Semantics Syntax - Regular Expressions, DFSAs and NDFSAs - Grammars Semantics - Natural Semantics - Transition Semantics

9/18/ Language Syntax Syntax is the description of which strings of symbols are meaningful expressions in a language It takes more than syntax to understand a language; need meaning (semantics) too Syntax is the entry point

9/18/ Syntax of English Language Pattern 1 Pattern 2

9/18/ Elements of Syntax Character set – previously always ASCII, now often 64 character sets Keywords – usually reserved Special constants – cannot be assigned to Identifiers – can be assigned to Operator symbols Delimiters (parenthesis, braces, brackets) Blanks (aka white space)

9/18/ Elements of Syntax Expressions if... then begin... ;... end else begin... ;... end Type expressions typexpr 1 -> typexpr 2 Declarations (in functional languages) let pattern 1 = expr 1 in expr Statements (in imperative languages) a = b + c Subprograms let pattern 1 = let rec inner = … in expr

9/18/ Elements of Syntax Modules Interfaces Classes (for object-oriented languages)

9/18/ Lexing and Parsing Converting strings to abstract syntax trees done in two phases Lexing: Converting string (or streams of characters) into lists (or streams) of tokens (the “words” of the language) Specification Technique: Regular Expressions Parsing: Convert a list of tokens into an abstract syntax tree Specification Technique: BNF Grammars

9/18/ Formal Language Descriptions Regular expressions, regular grammars, finite state automata Context-free grammars, BNF grammars, syntax diagrams Whole family more of grammars and automata – covered in automata theory

9/18/ Grammars Grammars are formal descriptions of which strings over a given character set are in a particular language Language designers write grammar Language implementers use grammar to know what programs to accept Language users use grammar to know how to write legitimate programs