Download presentation
Presentation is loading. Please wait.
Published byLynn French Modified over 8 years ago
1
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 1 PZ03CX - Language semantics Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 4.2.1-4.2.3
2
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 2 Semantics overview Language design has centered on context free grammars that are 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
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 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] 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
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 4 Attribute Grammars Additions to cfgs to carry some semantic info along through parse trees Primary value of AGs: 1. Static semantics specification 2. Compiler design(static semantics checking) Def: An attribute grammar is a context free grammar with the following additions: 1. For each grammar symbol x there is a set A(x) of attribute values 2. Each rule has a set of functions that define certain attributes of the nonterminals in the rule 3. Each rule has a (possibly empty) set of predicates to check for attribute consistency
5
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 5 Attribute grammars 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).
6
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 6 Synthesized and Inherited Attributes Let X 0 -> X 1... X n be a rule. Functions of the form S(X 0 ) = f(A(X 1 ),... A(X n )) define synthesized attributes Functions of the form I(X j ) = f(A(X 0 ),..., A(X n )), for i <= j <= n, define inherited attributes initially, there are intrinsic attributes on the leaves
7
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 7 Example Example: expressions of the form id + id - id 's can be either int_type or real_type - types of the two id 's must be the same - type of the expression must match it's expected type BNF: -> + -> id Attributes: actual_type - synthesized for and expected_type - inherited for
8
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 8 Attributes for “value” of an expression ProductionAttribute E E+T value(E 1 ) = value(E 2 )+value(T) E T value(E) = value(T) T T*P value(T 1 ) = value(T 2 )* value(P) T P value(T) = value(P) P I value(P) = value(I) P (E) value(P) = value(E) E 1 is first E in production and E 2 is second E in production Technique often useful for passing data type information within a parse tree.
9
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 9 Example attributed tree
10
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 10 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. 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.
11
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 11 Calculating Values of Attributes 1. If all attributes were inherited, the tree could be decorated in top- down order that is the values of the attributes are computed top- down. 2. If all attributes were synthesized, the tree could be decorated in bottom-up order. 3. In many cases, both kinds of attributes are used, and it is some combination of top- down and bottom-up that must be used.
12
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 12 Attribute Grammar Example 1.Consider an assignment statement grammar. The only variable names are A, B, and C. The right-hand–side of an assignment statement can be either a variable or a variable added to a variable. 2.The variables can be one of two types: int or real. When there are 2 variables on the RHS, they need not be the same type, but in this case, the type of the expression is always real. When they are the same, the expression type is the same as that of the operands. 3.The type of the left-hand-side must match the type of the RHS. The BNF specifies the syntax (1) of the grammar: = + | A | B | C We use attributes to express the semantic type rules (2) and (3).
13
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 13 Attribute Grammar Example con’t = + | A | B | C We will use two attributes: 1.Actual-type A synthesized attribute associated with and to indicate the type, int or real, The actual type of a is assumed known. The actual type of is determined from the actual types of the s in the expression (child nodes in the parse tree) 2.Expected-type An inherited type associated with to indicate the type expected because of the actual type of the in the statement.
14
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 14 Example continued 1. syntax = semantics.expected_type .actual_type 2.syntax [1] + [2] semantics.actual_type if (( [1].actual_type = int) and ( [2].actual_type = int)) then int else real;.actual_type =.expected_type 3.syntax semantics.actual_type .actual_type 4.syntax A | B | C semantics.actual_type lookup(var) The last statement in #2 is a predicate that tells us if we have a type mismatch
15
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 15 Attribute Grammar Example Create and decorate the parse tree for A=A+B where A is a real and B is int
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.