Download presentation
Presentation is loading. Please wait.
Published byYulia Sumadi Modified over 5 years ago
1
Language semantics Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
2
Semantics overview Language design has centered on context free grammars (LR(k) grammars). Parsing is not an interesting research question any more. Problem now is how to decide what a program means (semantics). Various approaches have been tried to develop semantic contents of programs.
3
Semantic approaches Grammatical models: add extensions to the BNF grammar that defined the language. Given a parse tree for a program,additional information could be extracted from that tree. [e.g., attribute grammars, later] Operational models: define how programs in the language are executed on a virtual computer. Compare that to the actual execution on a real computer. [Vienna Definition Language of the 1970s] Applicative models: construct a definition of the function that each program in the language computes. This definition is built up hierarchically through definition of the function computed by each individual program construct.[Denotational semantics] Axiomatic models: construct a formal logical proof theory to show that a program meets its specifications [to be described later]. Specification model: describe the relationship among the various functions implementing a program
4
An introduction to attribute grammars
We will give a brief introduction to attribute grammars to show how these operate. Associate a function with each node in the parse tree of a program giving the semantic content of that node. An inherited attribute is a function that relates nonterminal values in a tree with nonterminal values higher up in the tree (i.e., the functional value for the nonterminals on the right of any rule are a function of the left-hand side nonterminal). A synthesized attribute is a function that relates the left-hand side nonterminal to values of the right-hand side nonterminals. These attributes pass information up the tree (i.e., were synthesized from the information below in the tree).
5
Attributes for “value” of an expression
Production Attribute E E+T value(E1) = value(E2)+value(T) E T value(E) = value(T) T T*P value(T1) = value(T2)* value(P) T P value(T) = value(P) P I value(P) = value(I) P (E) value(P) = value(E) E1 is first E in production and E2 is second E in production Technique often useful for passing data type information within a parse tree.
6
Example attributed tree
7
Use of attribute grammars
First need to develop parse tree. Attribute grammars assume you already have the derivation; it is not a parsing algorithm. Functions for attributes can be arbitrary. Process to build attributes is mostly manual. If only have synthesized attributes, and if parsing is LR(k), then attribute grammars can be used automatically along with parsing to generate intermediate code. This is how YACC works. Values are computed for each nonterminal. As the parse tree is built, information from one nonterminal is passed to another nonterminal higher in the tree. When parse is completed, all attribute values have already been computed.
8
Denotational Semantics
Lambda Calculus 1930’s A. Church LISP, ALGOL 등의 function call Call by name of ALGOL Scott [1972] extends a general theory of data types denotational semantics Definition If x is a variable name, then x is a λ expression If M is a λ expression, then λx.M is a λ expression If F and A are λ expressions, then (F A) is a λ expression. F is the operator and A is the operand x λx.x λx.y λx.(xy) (λx.(xx)λ.(xx)) λx.λy.x
9
Operations on Lambda Expressions
λ expressions have only one reduction operation (λx.x y) y (λx.(xy) y) (yy) (λx.(xy) λx.x) (λx.x y) y (λx.(xx) λx(xx)) (λx.(xx) λx(xx)) … Church-Rosser property If two different reductions of a λ expression terminate, then they are members of the same values class Stated another way, if λ expression M has the reductions MP and MQ, then there is a unique λ expression R such that PR and QR
10
Parameter passing with λ expressions
Two standard reductions Innermost first call by value (λy.(yy) (λx.(xx) a)) (λy.(yy)(aa)) ((aa)(aa)) Nonterminating arguments will be evaluated even if not needed Outermost first Call by name, lazy evaluation (λy.(yy) (λx.(xx) a)) ((λx.(xx) a) (λx.(xx) a) ((aa)(aa)) The difference (λx.z (λx.(xx) λx.(xx)))
11
Modeling Mathematics with Lambda Expression
Boolean values True (T) λx.λy.x False (F) λx.λy.y ((T P) Q) P i.e. ((T P)Q) ((λx.λy.x P)Q) (λy.P Q) P ((F P) Q) P i.e. ((F P)Q) ((λx.λy.y P)Q) (λy.y Q) Q not λx.((xF)T), and λx.λy.((xy)F) Integers 0 λf.λc.c 1 λf.λc.(f c) 2 λf.λc.(f (f c)) fs : λc.c , λc.(s c), λc.(s (s c)), [M+N] = λa.λb.((M a) ((N a) b)) (N a) λc.(a …(a c)))
12
Modeling Mathematics with Lambda Expression
Integers 1+2 λs(λc.(s c))λc.(s (s c)) λs(s (λc.(s (s c))) λsλc.(s (s (s c))) λs λc.(s (s (s c))) Integer Multiplication [M*N] = λa.(M(N a)) λs.(λf.λc.(f(f c)) (λf. λc(f(f(f c))))s) λs.(λf.λc.(f (f c)) λc.(s (s (s c)))) λs.(λc.(s (s (s c))) (λc.(s (s (s c)))) ) λs.(λc.(s (s (s (s (s (s c))))))) )
13
Modeling Programming Languages
λexpression = constant + (λexpression => λexpression) datatype Mylist = var of int | Listitem of int * Mylist Stmt = (Id x Exp) Domain of assignment +(Stmt x Stmt) Domain of sequence (Exp x Stmt x Stmt) Domain of conditions +(Exp x Stmt) Domain of iterations Program state : id value M : prog [num num]
14
Statement Semantics begin statement end C{begin stmt end} = C{stmt}
Composition C{stmt1 ; stmt2} = (state s)state :C{stmt2}(C{stmt1}(s)) Assignment C{id exp} = (state s) state : ((value v) state : s[id/v])(E{exp}(s)) IF C{if exp then stmt1 else stmts} (state s) state : ((bool b)state state : (if b then C{stmt1})else C{stmt2})(E{exp}(s))(s)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.