Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Advertisements

- Vasvi Kakkad.  Formal -  Tool for mathematical analysis of language  Method for precisely designing language  Well formed model for describing and.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
CSE 425: Semantic Analysis Semantic Analysis Allows rigorous specification of a program’s meaning –Lets (parts of) programming languages be proven correct.
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics Fall 2005.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Context-Free Grammars Lecture 7
CS 330 Programming Languages 09 / 18 / 2007 Instructor: Michael Eckmann.
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Environments and Evaluation
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Programming Languages Third Edition Chapter 12 Formal Semantics.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
PART I: overview material
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
CS 363 Comparative Programming Languages Semantics.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Copyright © Curt Hill Languages and Grammars This is not English Class. But there is a resemblance.
Muhammad Idrees Lecturer University of Lahore 1. Outline Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.
ISBN Chapter 3 Describing Semantics.
Chapter 3 Part II Describing Syntax and Semantics.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Semantics In Text: Chapter 3.
1 / 48 Formal a Language Theory and Describing Semantics Principles of Programming Languages 4.
LESSON 04.
Syntax Analysis - Parsing Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
 Fall Chart 2  Translators and Compilers  Textbook o Programming Language Processors in Java, Authors: David A. Watts & Deryck F. Brown, 2000,
CMSC 330: Organization of Programming Languages Operational Semantics.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
Prof. Necula CS 164 Lecture 171 Operational Semantics of Cool ICOM 4029 Lecture 10.
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Programming Languages Dan Grossman 2013
Describing Syntax and Semantics
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2017.
Emily Leland (Not Nick) Spring 2017
Syntax Questions 6. Define a left recursive grammar rule.
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
The Metacircular Evaluator
The Metacircular Evaluator
The Metacircular Evaluator (Continued)
Adapted from slides by Nicholas Shahan and Dan Grossman
6.001 SICP Variations on a Scheme
Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang
Nicholas Shahan Spring 2016
Chapter 10: Compilers and Language Translation
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2019.
Faculty of Computer Science and Information System
Presentation transcript:

Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics

Chapter Twenty-ThreeModern Programming Languages2 Formal Semantics n At the beginning of the book we saw formal definitions of syntax with BNF n And how to make a BNF that generates correct parse trees: “where syntax meets semantics” n We saw how parse trees can be simplified into abstract syntax trees (ASTs) n Now… the rest of the story: formal definitions of programming language semantics

Chapter Twenty-ThreeModern Programming Languages3 Kinds Of Formal Semantics n There are many kinds, including: – Operational n Big-step: natural semantics n Small-step: structural operational semantics (SOS) n Abstract machine – Axiomatic – Denotational n Today: natural semantics, because it is related to what we’ve been doing with Prolog

Chapter Twenty-ThreeModern Programming Languages4 Outline n 23.2 Language One n 23.3 Language Two—Adding Variables n 23.4 Language Three—Adding Functions

Chapter Twenty-ThreeModern Programming Languages5 Defining Language One n 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-ThreeModern Programming Languages6 Syntax: Phrase Structure n (A subset of ML expressions, Java expressions, and Prolog terms) n This grammar is unambiguous Both operators are left associative, and * has higher precedence than + ::= + | ::= * | ::= ( ) |

Chapter Twenty-ThreeModern Programming Languages7 Parse Trees And ASTs n The grammar generates parse trees n The AST is a simplified form: same order as the parse tree, but no non-terminals

Chapter Twenty-ThreeModern Programming Languages8 Continuing The Definition n That is as far as we got in Chapters 2 and 3 n One way to define the semantics of the language is to give an interpreter for it n We will write one in Prolog, using ASTs as input: plus(const(1),times(const(2),const(3)))

Chapter Twenty-ThreeModern Programming Languages9 Abstract Syntax n Note: the set of legal ASTs can be defined by a grammar: n This is called an abstract syntax for the language n The original grammar was the concrete syntax of the language ::= plus(, ) | times(, ) | const( )

Chapter Twenty-ThreeModern Programming Languages10 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-ThreeModern Programming Languages11 ?- val1(const(1),X). X = 1 Yes ?- val1(plus(const(1),const(2)),X). X = 3 Yes ?- val1(plus(const(1),times(const(2),const(3))),X). X = 7 Yes

Chapter Twenty-ThreeModern Programming Languages12 Problems n 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-ThreeModern Programming Languages13 ?- val1(const( ),X). X = Yes ?- val1(const( ),X). X = e+009 Yes Value Of A Constant n Our Prolog treats values greater than as floating-point constants n Did we mean Language One to do this?

Chapter Twenty-ThreeModern Programming Languages14 ?- val1(plus(const( ),const(1)),X). X = Yes ?- val(plus(const( ),const(1)),X). X = e+009 Yes Value Of A Sum n Our Prolog expresses sums greater than as floating-point results n Did we mean Language One to do this?

Chapter Twenty-ThreeModern Programming Languages15 Defining Semantics By Interpreter Our val1 is not satisfactory as a definition of the semantics of Language One n “Language One programs behave the way this interpreter says they behave, running under this implementation of Prolog on this computer system” n We need something more abstract

Chapter Twenty-ThreeModern Programming Languages16 Natural Semantics A formal notation we can use to capture the same basic proof rules in val1 n We are trying to define the relation between an AST and the result of evaluating it n 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-ThreeModern Programming Languages17 A Rule In Natural Semantics n Conditions above the line, conclusion below n The same idea as our Prolog rule: val1(times(X,Y),Value) :- val1(X,XValue), val1(Y,YValue), Value is XValue * YValue.

Chapter Twenty-ThreeModern Programming Languages18 Language One, Natural Semantics n Of course, this still needs definitions for +, × and eval, but at least it won’t accidentally use Prolog’s 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-ThreeModern Programming Languages19 Natural Semantics, Note n There may be more than one rule for a particular kind of AST node n For instance, for an ML-style if-then-else we might use something like this:

Chapter Twenty-ThreeModern Programming Languages20 Outline n 23.2 Language One n 23.3 Language Two—Adding Variables n 23.4 Language Three—Adding Functions

Chapter Twenty-ThreeModern Programming Languages21 Defining Language Two n That one was too easy! n To make it a little harder, let’s add: – Variables – An ML-style let expression for defining them

Chapter Twenty-ThreeModern Programming Languages22 Syntax n (A subset of ML expressions) n This grammar is unambiguous A sample Language Two expression: let val y = 3 in y*y end ::= + | ::= * | ::= let val = in end | ( ) | |

Chapter Twenty-ThreeModern Programming Languages23 Abstract Syntax n 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-ThreeModern Programming Languages24 Representing Contexts n A representation for contexts: – bind(Variable,Value) = the binding from Variable to Value – A context is a list of zero or more bind terms n 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-ThreeModern Programming Languages25 Looking Up A Binding n Looks up a binding in a context n Finds the most recent binding for a given variable, if more than one lookup(Variable,[bind(Variable,Value)|_],Value). lookup(VarX,[bind(VarY,_)|Rest],Value) :- VarX \= VarY, lookup(VarX,Rest,Value).

Chapter Twenty-ThreeModern Programming Languages26 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-ThreeModern Programming Languages27 ?- val2(let(y,const(3),times(var(y),var(y))),nil,X). X = 9 Yes let val y = 3 in y*y end

Chapter Twenty-ThreeModern Programming Languages28 ?- val2(let(y,const(3), | let(x,times(var(y),var(y)), | times(var(x),var(x)))), | nil,X). X = 81 Yes let val y = 3 in let val x = y*y in x*x end end

Chapter Twenty-ThreeModern Programming Languages29 ?- val2(let(y,const(1),let(y,const(2),var(y))),nil,X). X = 2 Yes let val y = 1 in let val y = 2 in y end end

Chapter Twenty-ThreeModern Programming Languages30 Natural Semantics As before, we will write a natural semantics to capture the same basic proof rules n We will again use the symbol  for this relation, though it is a different relation n We will write  v to mean that the value of the AST E in context C is v

Chapter Twenty-ThreeModern Programming Languages31 Language Two, Natural Semantics n This still needs definitions for +, × and eval, as well as bind, lookup, ::, and the nil environment

Chapter Twenty-ThreeModern Programming Languages32 About Errors n In Language One, all syntactically correct programs run without error Not true in Language Two: let val a = 1 in b end n What does the semantics say about this?

Chapter Twenty-ThreeModern Programming Languages33 Undefined Variable Error Our natural semantics says something similar: there is no v for which  v ?- val2(let(a,const(1),var(b)),nil,X). No

Chapter Twenty-ThreeModern Programming Languages34 Static Semantics n 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 n This part of a language definition, neither syntax nor runtime behavior, is called static semantics

Chapter Twenty-ThreeModern Programming Languages35 Static and Dynamic Semantics n Language Two semantics could be 2 parts: – Static semantics rules out runtime errors – Dynamic semantics can ignore the issue n Static semantics can be complicated too: – ML’s type inference – Java’s “definite assignment” n In this chapter, dynamic semantics only

Chapter Twenty-ThreeModern Programming Languages36 Note: Dynamic Error Semantics n In full-size languages, there are still things that can go wrong at runtime n One approach is to define error outcomes in the natural semantics: n Today: semantics for error-free case only

Chapter Twenty-ThreeModern Programming Languages37 Outline n 23.2 Language One n 23.3 Language Two—Adding Variables n 23.4 Language Three—Adding Functions

Chapter Twenty-ThreeModern Programming Languages38 Defining Language Three n To make it a little harder, let’s add: – ML-style function values – ML-style function application

Chapter Twenty-ThreeModern Programming Languages39 Syntax n (A subset of ML expressions) n This grammar is unambiguous n Function application has highest precedence A sample Language Three expression: (fn x => x * x) 3 ::= fn => | ::= + | ::= * | ::= | ::= let val = in end | ( ) | |

Chapter Twenty-ThreeModern Programming Languages40 Abstract Syntax n 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-ThreeModern Programming Languages41 Representing Functions n 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) n (Why not just use the AST node itself to represent the function? You’ll see…)

Chapter Twenty-ThreeModern Programming Languages42 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) :- … 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). Same as for Language Two

Chapter Twenty-ThreeModern Programming Languages43 ?- val3(apply(fn(x,times(var(x),var(x))), | const(3)), | nil,X). X = 9 Yes (fn x => x * x) 3

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

Chapter Twenty-ThreeModern Programming Languages45 ?- 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 Yes 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-ThreeModern Programming Languages46 Dynamic Scoping n We got dynamic scoping n Probably not a good idea: – We have seen its drawbacks: difficult to implement efficiently, makes large complex scopes – Most modern languages use static scoping n How can we fix this so that Language Three uses static scoping?

Chapter Twenty-ThreeModern Programming Languages47 Representing Functions, Again n 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-ThreeModern Programming Languages48 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-ThreeModern Programming Languages49 ?- 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 Yes 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-ThreeModern Programming Languages50 ?- 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 Yes 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-ThreeModern Programming Languages51 Language Three Natural Semantics, Dynamic Scoping

Chapter Twenty-ThreeModern Programming Languages52 Language Three Natural Semantics, Static Scoping

Chapter Twenty-ThreeModern Programming Languages53 About Errors Language Three now has more than one type, so we can have type errors: 1 1 Similarly, the natural semantics gives no v for which  v ?- val3(apply(const(1),const(1)),nil,X). No

Chapter Twenty-ThreeModern Programming Languages54 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 n Interpreter runs forever on this n Natural semantics does not run forever— does not run at all—it just defines no result for the program

Chapter Twenty-ThreeModern Programming Languages55 Conclusion n Today’s examples used natural semantics n For other kinds, see the last section of the chapter and the Further Reading n Uses for formal semantics: – Defining languages for implementation – Justifying program transformations – Proving programs correct n Plus: they are very hard to read! Science is a way of trying not to fool yourself. — Richard Feynman