
Topics Introduction Chapter 3 Static Semantics Attribute Grammars Semantics Dynamic Semantics Operational Semantics Axiomatic Semantics Denotational Semantics Chapter 3: Semantics 2 Introduction Introduction Language implementors Well-designed programming language n Understand how all the constructs of the n Semantics should follow directly from syntax. language are form and their intended effect n Form of a statement should strongly suggest when executed. what the statement is meant to accomplish. Language users Definition of a programming language n Determine how to encode a possible solution n Complete: semantics and syntax are fully of a problem (program) using the reference defined. manual of the programming language. A language should provides a variety of Less knowledge of how to correctly define different constructs, each one with a the semantics of a language. precise definition. Chapter 3: Semantics 3 Chapter 3: Semantics 4 Introduction Introduction Language manuals Natural language description n Definition of semantics is given in ordinary n Ambiguous in its meaning natural language. Different readers come away with different n Construct interpretations of the semantics of a language construct. Syntax: a rule (or set of rules) from a BNF or other formal grammar. A method is needed for giving a Semantics: a few paragraphs and some readable, precise, and concise definition examples. of the semantics of an entire language. Chapter 3: Semantics 5 Chapter 3: Semantics 6 1 Static Semantics Static Semantics BNFs cannot describe all of the syntax of Some problems have nothing to do with programming languages. “meaning” in the sense of run-time behavior n Some context-specific parts are left out. n They are concern about the legal form of the Is there a form to generate L={anbncn} program. n Static semantics refers to type checking and resolving using a context-free grammar or a BNF? declarations. An attempt: n Examples: <string> ::= <aseq> <bseq> <c seq> All variables must be declared before they are referenced <aseq> ::= a | <aseq> a Ada: the name on the end of a procedure must match the procedure’s name <bseq> ::= b | <bseq> b L’={akbmcn | k=1, m=1, n=1} <c seq> ::= c | <c seq> c No context-free grammar generates L Both sides of an assignment must be of the same type. Chapter 3: Semantics 7 Chapter 3: Semantics 8 Attribute Grammars: Basic Static Semantics Concepts Earliest attempts to add semantics to a A context-free grammar extended to provide programming language context-sensitivity information by appending attributes to each node of a parse tree. Add extensions to the BNF grammar Each distinct symbol in the grammar has that defined the language. associated with it a finite, possibly empty, set of n Given a parse tree for a program, attributes. additional information could be extracted n Each attribute has a domain of possible values. from that tree. n An attribute may be assigned values from its domain during parsing. n Attributes can be evaluated in assignments and conditions. Chapter 3: Semantics 9 Chapter 3: Semantics 10 Attribute Grammars: Generalities Attribute Grammars: Parse Tree S Two classes of attributes: n Synthesized attribute Gets its value from the attributes attached to its children (subtree below the node). inherited Used to pass semantic information up a parse tree. n Inherited attribute Gets its value from the attributes attached to the A parent (subtree above the node). Used to pass semantic information down and synthesized across a tree. t Chapter 3: Semantics 11 Chapter 3: Semantics 12 2 Attribute Grammar Definition Attribute Grammars Associate some functions to compute the Starting with the underlying context-free value of the attributes with each grammar G=<N,T,P,S> production in the grammar. For every production p in P These local definitions associated with each production of the grammar define n Number of terminal and nonterminal symbols the values of the attributes for all parse in string a: n(p). trees. If a is the empty string, then n(p)=0. Sometimes each symbol of a production will be Given the definitions and a parse tree, considered individually. algorithms exist to compute the attributes n For all production pÎP : A? a or p0? p1,p2,…pn (p) of all the nodes in the three. Chapter 3: Semantics 13 Chapter 3: Semantics 14 Attribute Grammars Attribute Grammars: attributes Augment the context-free grammar by There is a set of attributes At(x)ÌAt to attributes and semantic rules. every grammar symbol xÎNÈT Set of attributes: At. n At(x) can be seen as additional information about the symbol x. n For each attribute aÎAt: associate a set of Set values Domain(a). n In(x) = { aÎAt(x) | aÎ In } n An attribute is just a name for a set of values n Syn(x) = { aÎAt(x) | aÎ Syn } Set of attributes: two disjoint classes: n Requirements: n Inherited attributes In and the synthesized In(S)= Æ (start symbol can inherit no information) attributes Syn (At=InÈ Syn and InÇ Syn=Æ). For all tÎT, Syn(t)= Æ (there is no structure beneath a terminal from which to synthesize information) Chapter 3: Semantics 15 Chapter 3: Semantics 16 Attribute Grammars: attribute Attribute Grammars: rules occurrences Same attribute can be associated with different Attribute occurrenceof a rule p is an ordered symbols appearing in the same grammar rule. pair of attributes and natural number <a,j> n Example: S? AB, all could inherit attribute int representing the attribute a at position j in associated to them: In(S)=In(A)=In(B)={int}. production p. n It is impossible to consider the set of attributes associated with all the symbols of a production n Particular rule pÎP an attribute occurrence at j will be written p.a. without losing track of which attributes appear more j than once. n Set of attribute occurrences for a production p is defined: AO(p) = { p.a | aÎAt(p), 0£ j £ n(p) } n More confusing: productions that have a nonterminal j j appearing more than once, as in S? ASA. Chapter 3: Semantics 17 Chapter 3: Semantics 18 3 Attribute Grammars: attribute Attribute Grammars: flow of occurrences attribute occurrences Rule: S? AB Set of attribute occurrences for a rule is subtree divided into two disjoint subsets. above S In(S) Syn (S) n Defined occurrences for a production p: S DO(p) = { p0.s | sÎSyn(p0)} È { pj .i | iÎIn(p j), 1£ j £ n(p) } In a parse tree, the set UO(p) represents the information In(B) Syn (B) flowing into the node of the parse tree labeled p0 In(A) Syn (A) A B n Used occurrences for a production p: UO(p) = { p0.i | iÎIn(p0)} È { pj .s | sÎSyn(p j), 1£ j £ n(p) } In a parse tree, the set DO(p) represents the information subtree subtree flowing out flowing into the node of the parse tree labeled p0 below A below B Chapter 3: Semantics 19 Chapter 3: Semantics 20 Attribute Grammars: used attribute Attribute Grammars: defined occurrences attribute occurrences Used attribute occurrences (the Defined attribute occurrences (the information flowing in) are In(S), Syn(A), information flowing out) are Syn(S), In(A), and Syn(B). and In(B). S A B S A B synthesized Syn(S) Syn(A) Syn(B) synthesized Syn(S) Syn(A) Syn(B) inherited In(S) In(A) In(B) inherited In(S) In(A) In(B) Chapter 3: Semantics 21 Chapter 3: Semantics 22 Attribute Grammars: semantic Attribute Grammar function Semantic function fp,v. n For every attribute occurrence vÎDO(p) An attribute grammar as a context-free n Defined values for attributes in DO(p) in terms of the grammar with two disjoint sets of values of the attributes in UO(p). n Produces a value for the attribute a from values of the attributes (inherited and synthesized) attributes of UO(p). and semantic functions for all defined n There is no requirement that all the attribute attribute occurrences. occurrences of UO(p) are used by fp,v . n Dependency set (Dp,v .) of fp,v: is the set of attribute occurrences used (subset of UO(p)) n Dp,v could be empty Value of the attribute: computed without any other additional information. The functionfp ,v is a constant. Chapter 3: Semantics 23 Chapter 3: Semantics 24 4 Attribute Grammar: binary digits Attribute Grammar: binary digits example example Context-free grammar that generates strings Compute the defined and the used occurrences for of binary digits. each production p: B ® D The defined occurrences is the set of synthesized attributes of the LHS plus the set of inherited q: B ® D B B D attributes of all the grammar symbols of the RHS. r: D ® 0 synthesized pos, val val s: D ® 1 Defined Used inherited pow Attributes: p B.pos, B.val, D.pow D.val q B1.pos, B1.val, D.pow B2.pos, B2.val, D.val n val: accumulate the value of the binary numbers r D.val D.pow n pow and pos: keep track of the position and the s power of 2. D.val D.pow Chapter 3: Semantics 25 Chapter 3: Semantics 26 Attribute Grammar: binary digits Attribute Grammar: binary digits example pos val example 4 10 pos val Function definitions for the eight defined attribute occurrences. B pos val 3 2 p: B ® D 3 8 D B pos val B.pos := 1 pos val B.val := D.val 2 0 2 2 D.pow := 0 B q: B ® D B pos val 1 2 1 D pos val B1.pos := B2.pos+1 1 2 B1.val := B 2.val +D.val 1 0 B D.pow := B2.pos D r: D ® 0 D.val := 0 0 pos val s: D ® 1 D 0 0 D.val := 2 D.pow 1 Evaluation of parse tree for 1010 0 Chapter 3: Semantics 27 Chapter 3: Semantics 28 Dynamic Semantics Dynamic Semantics Semantics of a programming language is Another way to view programming the definition of the meaning of any meaning is to start with a formal program that is syntactically valid.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages20 Page
-
File Size-