Date post: | 14-Dec-2015 |
Category: |
Documents |
Upload: | ginger-craig |
View: | 233 times |
Download: | 5 times |
Chapter 3Part II
Describing Syntax and Semantics
Copyright © 2012 Addison-Wesley. All rights reserved. 1-2
Chapter 3 Topics
• Introduction• The General Problem of Describing
Syntax• Formal Methods of Describing Syntax
Copyright © 2012 Addison-Wesley. All rights reserved. 1-3
Static Semantics
• Context-free grammars (CFGs) cannot be used to fully describe all legal forms for a programming language.
– In cases where it is inefficient
– In cases where it is impossible
Copyright © 2012 Addison-Wesley. All rights reserved. 1-4
Attribute Grammars
• Attribute grammars are
– a formal approach to describing and checking the correctness of the static semantics rules of a programming language.
– context-free grammars to which have been added attributes, computation functions, and predicate functions.
Copyright © 2012 Addison-Wesley. All rights reserved. 1-5
Attribute Grammars : Definition
• An attribute grammar is a context-free grammar with the following additions:
– For each grammar symbol x there is a set A(x) of attribute values
– Each rule has a set of functions that define certain attributes of the nonterminals in the rule
– Each rule has a (possibly empty) set of predicates to check for attribute consistency.
• More formally,– Let X0 X1 ... Xn be a rule
– Functions of the form S(X0) = f(A(X1), ... , A(Xn)) define synthesized attributes
– Functions of the form I(Xj) = f(A(X0), ... , A(Xn)), for i <= j <= n, define inherited attributes
– Initially, there are intrinsic attributes on the leaves
Copyright © 2012 Addison-Wesley. All rights reserved. 1-6
Attribute Grammars: Definition
• Inherited attributes pass semantic information down and across the tree.
• Synthesized attributes pass semantic information up a parse tree.
• Initially, there are intrinsic attributes on the leaves whose values are determined outside the parse tree.
Copyright © 2012 Addison-Wesley. All rights reserved. 1-7
Attribute Grammars
Example 1: An attributed grammar for the syntax and static semantics for the Ada rule which states that the name on the end of an Ada procedure must match the procedure’s name.
Syntax Rule:<proc_def> -> procedure <proc_name>[1]
<proc_body> end <proc_name>[2];Predicate: <proc_name>[1].string == <proc_name>[2].string
Copyright © 2012 Addison-Wesley. All rights reserved. 1-8
Attribute Grammars
Example 2: An attributed grammar for the syntax and static semantics of an assignment statement.
The syntax of the grammar is shown by the grammar: <assign> -> <var> = <expr>
<expr> -> <var> + <var> | <var>
<var> -> A | B | C
The only variable names are A, B, and C. The right side of the assignment can be either a variable or an expression in the form of a variable
added to another variable.
The static semantics of the grammar are: • The variables can be of two types: int or real. • When there are two variables on the right side of an assignment, they need not be the same type• The type of the expression when the two types are not the same is real.• When they are the same the type of the expression is the type of the two variables.• The type of the lhs must match the type of the rhs.
Copyright © 2007 Addison-Wesley. All rights reserved. 1-9
Copyright © 2007 Addison-Wesley. All rights reserved. 1-10
Copyright © 2007 Addison-Wesley. All rights reserved. 1-11
Decorating the parse tree - computing the values of the attributes
1.<var>.actual_type lookup (A)(R4)2.<expr>.expected_type <var.actual_type (R1)3.<var>[2].actual_type lookup (A) (R4)4.<var>[3].actual_type lookup (B) (R4)5.<expr>.actual_type either int or real (R2) 6.<expr>.expected_type == <expr>.actual_type
7. True or False (R2)
Attribute Grammars : continued
Copyright © 2007 Addison-Wesley. All rights reserved. 1-12
Attribute Grammars : continued
Fully attributed parse tree where A is type real and B is type int
Predicate: <expr>.expected_type == <expr>.actual_type
TRUE
Copyright © 2012 Addison-Wesley. All rights reserved. 1-13
Attribute Grammars
• Adding the attributes, their functions and predicates to the grammar of to describe the static semantics:<assign> -> <var> = <expr>
<expr> -> <var> + <var> | <var>
<var> -> A | B | C
Attributes for nonterminals• actual_type: a synthesized attribute for <var> and
<expr> used to store the type of int or real. – <var> - intrinsically determined– <expr> - determined from the actual types of the child(ren)
• expected_type: an inherited attribute for <expr> used to store the expected type of int or real as determined by the type of the variable on the LHS of the assignment statement
Attribute Grammars : Evaluated
• Every compiler must be written to check the static semantic rules of a programming language whether formally done or not.
Copyright © 2012 Pearson Education. All rights reserved. 1-14
Copyright © 2012 Addison-Wesley. All rights reserved. 1-15
Dynamic Semantics
• There is no single widely acceptable notation or formalism for describing dynamic semantics.
• Three example methods for describing dynamic semantics.– Operational– Denotational– Axiomatic
• How can these be useful?– By programmers – By compiler writers – By programming language designers
– When correctness proofs are wanted– For compiler generators
Operational Semantics
• Operational Semantics– Describe the meaning of a program by executing its statements
on a machine, either simulated or actual.
• Uses of operational semantics: - Language manuals and textbooks - Teaching programming languages
• Two levels of uses: Natural & Structural
• Evaluation - Good if used informally
- Can be extremely complex if used formally
Copyright © 2012 Addison-Wesley. All rights reserved. 1-16
Operational Semantics• Examples of the form of a few statements
– Ident = var bin_op var– Ident = un_op var
• Example of usage for the description of the semantics of the C for statement:
for (expre1; expre2; expre3) { …}Meaningexpr1;Loop: if expre2 == 0 goto Out
…expre3;goto Loop
Out:
Copyright © 2012 Pearson Education. All rights reserved. 1-17
Denotational Semantics• The process of building a denotational specification for a language: -Define a mathematical object for each language entity
-Define a function that maps instances of the language entities onto instances of the corresponding mathematical objects
The meaning of language constructs are defined by only the values of the program's variables
Copyright © 2012 Addison-Wesley. All rights reserved. 1-18
<dec_num> '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | <dec_num> ('0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9')
Mdec('0') = 0, Mdec ('1') = 1, …, Mdec ('9') = 9
Mdec (<dec_num> '0') = 10 * Mdec (<dec_num>)
Mdec (<dec_num> '1’) = 10 * Mdec (<dec_num>) + 1…
Mdec (<dec_num> '9') = 10 * Mdec (<dec_num>) + 9
Evaluation of Denotational Semantics
• Can be used to prove the correctness of programs
• Provides a rigorous way to think about programs
• Can be an aid to language design• Has been used in compiler generation
systems • Because of its complexity, it are of little
use to language users
Copyright © 2012 Addison-Wesley. All rights reserved. 1-19
Copyright © 2012 Addison-Wesley. All rights reserved. 1-20
Axiomatic Semantics
• Based on formal logic (predicate calculus)• Original purpose: formal program
verification• Axioms or inference rules are defined for
each statement type in the language (to allow transformations of logic expressions into more formal logic expressions)
• The logic expressions are called assertions
Copyright © 2012 Addison-Wesley. All rights reserved. 1-21
Axiomatic Semantics (continued)
• An assertion before a statement (a precondition) states the relationships and constraints among variables that are true at that point in execution
• An assertion following a statement is a postcondition
• A weakest precondition is the least restrictive precondition that will guarantee the postcondition
Copyright © 2012 Addison-Wesley. All rights reserved. 1-22
Axiomatic Semantics Form
• Pre-, post form: {P} statement {Q}• An example
– a = b + 1 {a > 1}– One possible precondition: {b > 10}– Weakest precondition: {b > 0}
• An example– sum = 2 * x + 1 {sum > 1}– One possible precondition: {x > 10}– Weakest precondition: {x > 0}
Copyright © 2012 Addison-Wesley. All rights reserved. 1-23
Program Proof Process
• The postcondition for the entire program is the desired result– Work back through the program to the first
statement. If the precondition on the first statement is the same as the program specification, the program is correct.
{P1} statement1 {Q1}
{P2} statement2 {Q2}
{P3} statement3 {Q3}
Copyright © 2012 Addison-Wesley. All rights reserved. 1-24
Axiomatic Semantics: Assignment
• Typical notation for the axiomatic semantics of a given statement form is: {P} S {Q}
• An axiom for assignment statements: (x = E): {Qx->E} x = E {Q}
• Example: a = b / 2 – 1 { a < 10 }• • a < 10 , where a = b / 2 - 1• b / 2 – 1 < 10• b / 2 < 11• b < 22
• P is {b < 22}
• Example: x = 2 * y – 3 {x > 25}• 2 * y – 3 > 25• 2 * y > 28• y > 14• P is {y > 14}
Copyright © 2012 Addison-Wesley. All rights reserved. 1-25
Axiomatic Semantics: Assignment
Example 3: x = x + y -3 {x > 10}
Calculate P
x + y – 3 > 10 x + y > 13 y > 13 – x
P {y > 13 – x}
Copyright © 2012 Addison-Wesley. All rights reserved. 1-26
Axiomatic Semantics: Sequences
• An inference rule for sequences of the form S1; S2
{P1} S1 {P2}{P2} S2 {P3}
{P3} S2 S1; {P1}{P3} S2 {P2} {P2}, S1 {P1}
Copyright © 2012 Addison-Wesley. All rights reserved. 1-27
Axiomatic Semantics
Sequences Example: y = 3 * x + 1; x = y + 3; { x < 10 }
Start with: {p} x = y + 3; { x < 10 }To find P: y + 3 < 10 y < 7Result: {y < 7} x = y + 3; { x < 10 }
Final Result is { X < 2} y = 3 * x + 1; {y < 7} x = y + 3; { x < 10 }
Next find P for first statement: {P} y = 3 * x + 1; {y < 7}
To find P: y = 3 * x + 1; 3 * x + 1 < 7 3 * x < 6 x < 2Result: {x < 2} y = 3 * x + 1; { y < 7}
{P3} S2 S1; {P1}{P3} S2 {P2} {P2}, S1 {P1}
Axiomatic Semantics: Selection
• An inference rules for selection - if B then S1 else S2
{B and P} S1 {Q}, {(not B) and P} S2 {Q}
{P} if B then S1 else S2 {Q}
Copyright © 2012 Addison-Wesley. All rights reserved. 1-28
Axiomatic Semantics: Selection
{B and P} S1 {Q}, {(not B) and P} S2 {Q} {P} if B then S1 else S2 {Q}
Example: if x > 0 then y = y -1 else y = y + 1 {y > 0 }Apply to then clause: y – 1 > 0 yields P = {y>1}Apply to else clause: y + 1 > 0 yields P = {y>-1}Because y>1 => y>-1, we use {y > 1} as the
precondition for the entire statement.Copyright © 2012 Addison-Wesley. All rights reserved. 1-29
Copyright © 2012 Addison-Wesley. All rights reserved. 1-30
Axiomatic Semantics: Loops
• An inference rule for logical pretest loops
{P} while B do S end {Q}
where I is the loop invariant (the inductive hypothesis)
B)}(not and {I S do B while{I}
{I} S B) and (I
Copyright © 2012 Addison-Wesley. All rights reserved. 1-31
Axiomatic Semantics: Axioms
• Characteristics of the loop invariant: I must meet the following conditions:– P => I -- the loop invariant must be true initially
– {I} B {I} -- evaluation of the Boolean must not change the validity of I
– {I and B} S {I} -- I is not changed by executing the body of the loop
– (I and (not B)) => Q -- if I is true and B is false, Q is implied
– The loop terminates -- can be difficult to prove
Copyright © 2012 Addison-Wesley. All rights reserved. 1-32
Evaluation of Axiomatic Semantics
• Developing axioms or inference rules for all of the statements in a language is difficult
• It is a good tool for correctness proofs, and an excellent framework for reasoning about programs, but it is not as useful for language users and compiler writers
• Its usefulness in describing the meaning of a programming language is limited for language users or compiler writers
Copyright © 2012 Addison-Wesley. All rights reserved. 1-33
Denotation Semantics vs Operational Semantics
• In operational semantics, the state changes are defined by coded algorithms
• In denotational semantics, the state changes are defined by rigorous mathematical functions
Copyright © 2012 Addison-Wesley. All rights reserved. 1-34
Summary
• BNF and context-free grammars are equivalent meta-languages– Well-suited for describing the syntax of
programming languages
• An attribute grammar is a descriptive formalism that can describe both the syntax and the semantics of a language
• Three primary methods of semantics description– Operation, axiomatic, denotational
Copyright © 2012 Addison-Wesley. All rights reserved. 1-35
Summary
• BNF and context-free grammars are equivalent meta-languages– Well-suited for describing the syntax of
programming languages
• An attribute grammar is a descriptive formalism that can describe both the syntax and the semantics of a language
• Three primary methods of semantics description– Operation, axiomatic, denotational