Formal Semantics. Chapter Twenty-Three Modern Programming Languages, 2nd ed. 1

Similar documents
COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor

Intro to semantics; Small-step semantics Lecture 1 Tuesday, January 29, 2013

CMSC 330: Organization of Programming Languages

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Programming Languages Third Edition

The role of semantic analysis in a compiler

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CSE 12 Abstract Syntax Trees

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

CMSC 330: Organization of Programming Languages. Operational Semantics

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Semantic Analysis. Lecture 9. February 7, 2018

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Compiler Theory. (Semantic Analysis and Run-Time Environments)

A simple syntax-directed

Goals: Define the syntax of a simple imperative language Define a semantics using natural deduction 1

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

Program Analysis: Lecture 02 Page 1 of 32

Related Course Objec6ves

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

A Simple Syntax-Directed Translator

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

Semester Review CSC 301

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

CS164: Midterm I. Fall 2003

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CSCE 314 Programming Languages

The PCAT Programming Language Reference Manual

10/18/18. Outline. Semantic Analysis. Two types of semantic rules. Syntax vs. Semantics. Static Semantics. Static Semantics.

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

Programming Languages Third Edition. Chapter 7 Basic Semantics

1 Lexical Considerations

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

Context-Free Grammars

Operational Semantics. One-Slide Summary. Lecture Outline

Programming Languages, Summary CSC419; Odelia Schwartz

The SPL Programming Language Reference Manual

CSE 341 Section 7. Eric Mullen Spring Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang

CSCE 314 Programming Languages. Type System

Lecture 2: Big-Step Semantics

CA Compiler Construction

CMPS Programming Languages. Dr. Chengwei Lei CEECS California State University, Bakersfield

CSE 341 Section 7. Ethan Shea Autumn Adapted from slides by Nicholas Shahan, Dan Grossman, Tam Dang, and Eric Mullen

Syntax and Grammars 1 / 21

CS 360 Programming Languages Interpreters

CSCI-GA Scripting Languages

Defining Languages GMU

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

The syntax and semantics of Beginning Student

The syntax and semantics of Beginning Student

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Typescript on LLVM Language Reference Manual

Lecture 12: Conditional Expressions and Local Binding

Syntax. In Text: Chapter 3

Lexical Considerations

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Introduction to lambda calculus Part 3

Formal Semantics of Programming Languages

CSCI 3155: Lab Assignment 2

Scope. Chapter Ten Modern Programming Languages 1

Chapter 3. Describing Syntax and Semantics

CS422 - Programming Language Design

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Formal Semantics of Programming Languages

CSE 401 Midterm Exam Sample Solution 2/11/15

Syntax. A. Bellaachia Page: 1

2.2 Syntax Definition

Lexical Considerations

Variables and Bindings

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

UNIVERSITETET I OSLO

CS558 Programming Languages

Lexical and Syntax Analysis. Top-Down Parsing

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

The Substitution Model

Bindings & Substitution

CPSC 411, Fall 2010 Midterm Examination

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CS558 Programming Languages

Types and Static Type Checking (Introducing Micro-Haskell)

Compilers - Chapter 2: An introduction to syntax analysis (and a complete toy compiler)

6.001 Notes: Section 15.1

CS558 Programming Languages

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

Comp 311: Sample Midterm Examination

Language Reference Manual simplicity

Seminar in Programming Languages

Flang typechecker Due: February 27, 2015

Principles of Programming Languages

Chapter 3 (part 3) Describing Syntax and Semantics

Transcription:

Formal Semantics Chapter Twenty-Three Modern Programming Languages, 2nd ed. 1

Formal Semantics At the beginning of the book we saw formal definitions of syntax with BNF And how to make a BNF that generates correct parse trees: where syntax meets semantics We saw how parse trees can be simplified into abstract syntax trees (AST s) Now the rest of the story: formal definitions of programming language semantics Chapter Twenty-Three Modern Programming Languages, 2nd ed. 2

Outline Natural semantics and Prolog interpreters Language One Language Two: adding variables Language Three: adding functions Chapter Twenty-Three Modern Programming Languages, 2nd ed. 3

Defining Language One A little language of integer expressions: Constants The binary infix operators + and *, with the usual precedence and associativity Parentheses for grouping Lexical structure: tokens are +, *, (, ), and integer constants consisting of one or more decimal digits Chapter Twenty-Three Modern Programming Languages, 2nd ed. 4

Syntax: Phrase Structure <exp> ::= <exp> + <mulexp> <mulexp> <mulexp> ::= <mulexp> * <rootexp> <rootexp> <rootexp> ::= (<exp>) <constant> (A subset of ML expressions, Java expressions, and Prolog terms) This grammar is unambiguous Both operators are left associative, and * has higher precedence than + Chapter Twenty-Three Modern Programming Languages, 2nd ed. 5

Parse Trees And AST s The grammar generates parse trees The AST is a simplified form: same order as the parse tree, but no non-terminals Chapter Twenty-Three Modern Programming Languages, 2nd ed. 6

Continuing The Definition That is as far as we got in Chapters 2 and 3 One way to define the semantics of the language is to give an interpreter for it We will write one in Prolog, using AST s as input: plus(const(1),times(const(2),const(3))) Chapter Twenty-Three Modern Programming Languages, 2nd ed. 7

Abstract Syntax Note: the set of legal AST s can be defined by a grammar, giving the abstract syntax of the language <exp> ::= plus(<exp>,<exp>) times(<exp>,<exp>) const(<constant>) An abstract syntax can be ambiguous, since the order is already fixed by parsing with the original grammar for concrete syntax Chapter Twenty-Three Modern Programming Languages, 2nd ed. 8

Language One: Prolog Interpreter val1(plus(x,y),value) :- val1(x,xvalue), val1(y,yvalue), Value is XValue + YValue. val1(times(x,y),value) :- val1(x,xvalue), val1(y,yvalue), Value is XValue * YValue. val1(const(x),x). Chapter Twenty-Three Modern Programming Languages, 2nd ed. 9

?- val1(const(1),x). X = 1.?- val1(plus(const(1),const(2)),x). X = 3.?- val1(plus(const(1),times(const(2),const(3))),x). X = 7. Chapter Twenty-Three Modern Programming Languages, 2nd ed. 10

Problems What is the value of a constant? Interpreter says val1(const(x),x). This means that the value of a constant in Language One is whatever the value of that same constant is in Prolog Unfortunately, different implementations of Prolog handle this differently Chapter Twenty-Three Modern Programming Languages, 2nd ed. 11

Value Of A Constant?- val1(const(2147483647),x). X = 2147483647.?- val1(const(2147483648),x). X = 2.14748e+009?- val1(const(2147483647),x). X = 2147483647.?- val1(const(2147483648),x). X = 2147483648. Some Prologs treat values greater than 2 31-1 as floating-point constants; others don t Did we mean Language One to do this? Chapter Twenty-Three Modern Programming Languages, 2nd ed. 12

Value Of A Sum?- val(plus(const(2147483647),const(1)),x). X = 2.14748e+009.?- val(plus(const(2147483647),const(1)),x). X = 2147483648. Some Prologs expresses sums greater than 2 31-1 as floating-point results; others don t Did we mean Language One to do this? Chapter Twenty-Three Modern Programming Languages, 2nd ed. 13

Defining Semantics By Interpreter Our val1 is not satisfactory as a definition of the semantics of Language One Language One programs behave the way this interpreter says they behave, running under this implementation of Prolog on this computer system We need something more abstract Chapter Twenty-Three Modern Programming Languages, 2nd ed. 14

Natural Semantics A formal notation we can use to capture the same basic proof rules in val1 We are trying to define the relation between an AST and the result of evaluating it We will use the symbol for this relation, writing E v to mean that the AST E evaluates to the value v For example, our semantics should establish times(const(2),const(3)) 6 Chapter Twenty-Three Modern Programming Languages, 2nd ed. 15

A Rule In Natural Semantics Conditions above the line, conclusion below The same idea as our Prolog rule: val1(times(x,y),value) :- val1(x,xvalue), val1(y,yvalue), Value is XValue * YValue. Chapter Twenty-Three Modern Programming Languages, 2nd ed. 16

Language One, Natural Semantics val1(plus(x,y),value) :- val1(x,xvalue), val1(y,yvalue), Value is XValue + YValue. val1(times(x,y),value) :- val1(x,xvalue), val1(y,yvalue), Value is XValue * YValue. val1(const(x),x). Of course, this still needs definitions for +, and eval, but at least it won t accidentally use Prolog s Chapter Twenty-Three Modern Programming Languages, 2nd ed. 17

Natural Semantics, Note There may be more than one rule for a particular kind of AST node For instance, for an ML-style if-then-else we might use something like this: E 1 true E 2 v 2 if( E 1,E 2,E 3 ) v 2 E 1 false E 3 v 3 if( E 1,E 2, E 3 ) v 3 Chapter Twenty-Three Modern Programming Languages, 2nd ed. 18

Outline Natural semantics and Prolog interpreters Language One Language Two: adding variables Language Three: adding functions Chapter Twenty-Three Modern Programming Languages, 2nd ed. 19

Defining Language Two That one was too easy! To make it a little harder, let s add: Variables An ML-style let expression for defining them Chapter Twenty-Three Modern Programming Languages, 2nd ed. 20

Syntax <exp> ::= <exp> + <mulexp> <mulexp> <mulexp> ::= <mulexp> * <rootexp> <rootexp> <rootexp> ::= let val <variable> = <exp> in <exp> end (<exp>) <variable> <constant> (A subset of ML expressions) This grammar is unambiguous A sample Language Two expression: let val y = 3 in y*y end Chapter Twenty-Three Modern Programming Languages, 2nd ed. 21

Abstract Syntax Two more kinds of AST nodes: var(x) for a reference to a variable X let(x,exp1,exp2) for a let expression that evaluates Exp2 in an environment where the variable X is bound to the value of Exp1 So for the Language Two program let val y = 3 in y*y end we have this AST: let(y,const(3),times(var(y),var(y))) Chapter Twenty-Three Modern Programming Languages, 2nd ed. 22

Representing Contexts A representation for contexts: bind(variable,value) = the binding from Variable to Value A context is a list of zero or more bind terms For example: The context in which y is bound to 3 could be [bind(y,3)] The context in which both x and y are bound to 3 could be [bind(x,3),bind(y,3)] Chapter Twenty-Three Modern Programming Languages, 2nd ed. 23

Looking Up A Binding lookup(variable,[bind(variable,value) _],Value) :-!. lookup(varx,[_ Rest],Value) :- lookup(varx,rest,value). Looks up a binding in a context Finds the most recent binding for a given variable, if more than one Chapter Twenty-Three Modern Programming Languages, 2nd ed. 24

Language Two: Prolog Interpreter val2(plus(x,y),context,value) :- val2(x,context,xvalue), val2(y,context,yvalue), Value is XValue + YValue. val2(times(x,y),context,value) :- val2(x,context,xvalue), val2(y,context,yvalue), Value is XValue * YValue. val2(const(x),_,x). val2(var(x),context,value) :- lookup(x,context,value). val2(let(x,exp1,exp2),context,value2) :- val2(exp1,context,value1), val2(exp2,[bind(x,value1) Context],Value2). Chapter Twenty-Three Modern Programming Languages, 2nd ed. 25

?- val2(let(y,const(3),times(var(y),var(y))),nil,x). X = 9. let val y = 3 in y*y end Chapter Twenty-Three Modern Programming Languages, 2nd ed. 26

?- val2(let(y,const(3), let(x,times(var(y),var(y)), times(var(x),var(x)))), nil,x). X = 81. let val y = 3 in let val x = y*y in x*x end end Chapter Twenty-Three Modern Programming Languages, 2nd ed. 27

?- val2(let(y,const(1),let(y,const(2),var(y))),nil,x). X = 2. let val y = 1 in let val y = 2 in y end end Chapter Twenty-Three Modern Programming Languages, 2nd ed. 28

Natural Semantics As before, we will write a natural semantics to capture the same basic proof rules We will again use the symbol for this relation, though it is a different relation We will write <E,C> v to mean that the value of the AST E in context C is v Chapter Twenty-Three Modern Programming Languages, 2nd ed. 29

Language Two, Natural Semantics E 1,C v 1 E 2,C v 2 plus( E 1, E 2 ),C v 1 + v 2 var( v),c lookup(c,v) E 1,C v 1 E 2,C v 2 const( n),c eval(n) times( E 1,E 2 ),C v 1 v 2 ( ) :: C v 2 E 1,C v 1 E 2,bind x,v 1 let( x, E 1,E 2 ),C v 2 This still needs definitions for +, and eval, as well as bind, lookup, ::, and the nil environment Chapter Twenty-Three Modern Programming Languages, 2nd ed. 30

About Errors In Language One, all syntactically correct programs run without error Not true in Language Two: let val a = 1 in b end What does the semantics say about this? Chapter Twenty-Three Modern Programming Languages, 2nd ed. 31

Undefined Variable Error?- val2(let(a,const(1),var(b)),nil,x). false. Our natural semantics says something similar: there is no v for which <let(a,const(1),var(b)), nil> v Chapter Twenty-Three Modern Programming Languages, 2nd ed. 32

Static Semantics Ordinarily, language systems perform error checks after parsing but before running For static scoping: references must be in the scope of some definition of the variable For static typing: a consistent way to assign a type to every part of the program This part of a language definition, neither syntax nor runtime behavior, is called static semantics Chapter Twenty-Three Modern Programming Languages, 2nd ed. 33

Static and Dynamic Semantics Language Two semantics could be 2 parts: Static semantics rules out runtime errors Dynamic semantics can ignore the issue Static semantics can be complicated too: ML s type inference Java s definite assignment In this chapter, dynamic semantics only Chapter Twenty-Three Modern Programming Languages, 2nd ed. 34

Note: Dynamic Error Semantics In full-size languages, there are still things that can go wrong at runtime One approach is to define error outcomes in the natural semantics: Today: semantics for error-free case only Chapter Twenty-Three Modern Programming Languages, 2nd ed. 35

Outline Natural semantics and Prolog interpreters Language One Language Two: adding variables Language Three: adding functions Chapter Twenty-Three Modern Programming Languages, 2nd ed. 36

Defining Language Three To make it a little harder, let s add: ML-style function values ML-style function application Chapter Twenty-Three Modern Programming Languages, 2nd ed. 37

Syntax <exp> ::= fn <variable> => <exp> <addexp> <addexp> ::= <addexp> + <mulexp> <mulexp> <mulexp> ::= <mulexp> * <funexp> <funexp> <funexp> ::= <funexp> <rootexp> <rootexp> <rootexp> ::= let val <variable> = <exp> in <exp> end (<exp>) <variable> <constant> (A subset of ML expressions) This grammar is unambiguous Function application has highest precedence A sample Language Three expression: (fn x => x * x) 3 Chapter Twenty-Three Modern Programming Languages, 2nd ed. 38

Abstract Syntax Two more kinds of AST nodes: apply(function,actual) applies the Function to the Actual parameter fn(formal,body) for an fn expression with the given Formal parameter and Body So for the Language Three program (fn x => x * x) 3 we have this AST: apply(fn(x,times(var(x),var(x))), const(3)) Chapter Twenty-Three Modern Programming Languages, 2nd ed. 39

Representing Functions A representation for functions: fval(formal,body) Formal is the formal parameter variable Body is the unevaluated function body So the AST node fn(formal,body) evaluates to fval(formal,body) (Why not just use the AST node itself to represent the function? You ll see ) Chapter Twenty-Three Modern Programming Languages, 2nd ed. 40

Language Three: Prolog Interpreter val3(plus(x,y),context,value) :- val3(times(x,y),context,value) :- val3(const(x),_,x). val3(var(x),context,value) :- val3(let(x,exp1,exp2),context,value2) :- Same as for Language Two val3(fn(formal,body),_,fval(formal,body)). val3(apply(function,actual),context,value) :- val3(function,context,fval(formal,body)), val3(actual,context,paramvalue), val3(body,[bind(formal,paramvalue) Context],Value). Chapter Twenty-Three Modern Programming Languages, 2nd ed. 41

?- val3(apply(fn(x,times(var(x),var(x))), const(3)), nil,x). X = 9. (fn x => x * x) 3 Chapter Twenty-Three Modern Programming Languages, 2nd ed. 42

Question What should the value of this Language Three program be? let val x = 1 in let val f = fn n => n + x in let val x = 2 in f 0 end end end Depends on whether scoping is static or dynamic Chapter Twenty-Three Modern Programming Languages, 2nd ed. 43

?- val3(let(x,const(1), let(f,fn(n,plus(var(n),var(x))), let(x,const(2), apply(var(f),const(0))))), nil,x). X = 2. let val x = 1 in let val f = fn n => n + x in let val x = 2 in f 0 end end end Oops we defined Language Three with dynamic scoping! Chapter Twenty-Three Modern Programming Languages, 2nd ed. 44

Dynamic Scoping We got dynamic scoping Probably not a good idea: We have seen its drawbacks: difficult to implement efficiently, makes large complex scopes Most modern languages use static scoping How can we fix this so that Language Three uses static scoping? Chapter Twenty-Three Modern Programming Languages, 2nd ed. 45

Representing Functions, Again Add context to function representation: fval(formal,body,context) Formal is the formal parameter variable Body is the unevaluated function body Context is the context to use when calling it So the AST node fn(formal,body) evaluated in Context, produces to fval(formal,body,context) Context works as a nesting link (Chapter 12) Chapter Twenty-Three Modern Programming Languages, 2nd ed. 46

Language Three: Prolog Interpreter, Static Scoping val3(fn(formal,body),_,fval(formal,body)). val3(fn(formal,body),context,fval(formal,body,context)). val3(apply(function,actual),context,value) :- val3(function,context,fval(formal,body)), val3(actual,context,paramvalue), val3(body,bind(formal,paramvalue,context),value). val3(apply(function,actual),context,value) :- val3(function,context,fval(formal,body,nesting)), val3(actual,context,paramvalue), val3(body,[bind(formal,paramvalue) Nesting],Value). Chapter Twenty-Three Modern Programming Languages, 2nd ed. 47

?- val3(let(x,const(1), let(f,fn(n,plus(var(n),var(x))), let(x,const(2), apply(var(f),const(0))))), nil,x). X = 1. let val x = 1 in let val f = fn n => n + x in let val x = 2 in f 0 end end end That s better: static scoping! Chapter Twenty-Three Modern Programming Languages, 2nd ed. 48

?- val3(let(f,fn(x,let(g,fn(y,plus(var(y),var(x))), var(g))), apply(apply(var(f),const(1)),const(2))), nil,x). X = 3. let val f = fn x => let val g = fn y => y+x in g end in f 1 2 end Handles ML-style higher order functions. Chapter Twenty-Three Modern Programming Languages, 2nd ed. 49

Language Three Natural Semantics, Dynamic Scoping E 1,C v 1 E 2,C v 2 plus( E 1,E 2 ),C v 1 + v 2 E 1,C v 1 E 2,C v 2 times( E 1,E 2 ),C v 1 v 2 const( n),c eval(n) var( v),c lookup(c,v) fn( x, E),C ( x, E) E 1,C x,e 3 ( ) :: C v 2 E 1,C v 1 E 2,bind x,v 1 let( x, E 1,E 2 ),C v 2 ( ) E 2,C v 1 E 3,bind( x,v 1 ) :: C v 2 apply( E 1, E 2 ),C v 2 Chapter Twenty-Three Modern Programming Languages, 2nd ed. 50

Language Three Natural Semantics, Static Scoping fn( x, E),C ( x, E) fn( x, E),C ( x, E,C) E 1,C x,e 3 ( ) E 2,C v 1 E 3,bind( x,v 1 ) :: E 1,C x, E 3, C ( ) E 2,C v 1 E 3,bind( x,v 1 ) :: C v 2 apply( E 1, E 2 ),C v 2 apply( E 1, E 2 ),C v 2 C v 2 Chapter Twenty-Three Modern Programming Languages, 2nd ed. 51

About Errors Language Three now has more than one type, so we can have type errors: 1 1?- val3(apply(const(1),const(1)),nil,x). false. Similarly, the natural semantics gives no v for which <apply(const(1),const(1)), nil> v Chapter Twenty-Three Modern Programming Languages, 2nd ed. 52

More Errors In the dynamic-scoping version, we can also have programs that run forever: let val f = fn x => f x in f 1 end Interpreter runs forever on this Natural semantics does not run forever does not run at all it just defines no result for the program Chapter Twenty-Three Modern Programming Languages, 2nd ed. 53

Outline Natural semantics and Prolog interpreters Language One Language Two: adding variables Language Three: adding functions Natural semantics is one of many formal techniques for defining semantics Other techniques: see the last section of the chapter for a summary Chapter Twenty-Three Modern Programming Languages, 2nd ed. 54