pl10ch3
pl10ch3
Describing Syntax
and Semantics
Chapter 3 Topics
• Introduction
• The General Problem of Describing Syntax
• Formal Methods of Describing Syntax
• Attribute Grammars
• Describing the Meanings of Programs:
Dynamic Semantics
1-2
Introduction
1-3
The General Problem of Describing
Syntax: Terminology
1-4
Formal Definition of Languages
• Recognizers
– A recognition device reads input strings over the alphabet
of the language and decides whether the input strings
belong to the language
– Example: syntax analysis part of a compiler
- Detailed discussion of syntax analysis appears in
Chapter 4
• Generators
– A device that generates sentences of a language
– One can determine if the syntax of a particular sentence is
syntactically correct by comparing it to the structure of
the generator
1-5
BNF and Context-Free Grammars
• Context-Free Grammars
– Developed by Noam Chomsky in the mid-1950s
– Language generators, meant to describe the
syntax of natural languages
– Define a class of languages called context-free
languages
1-7
BNF Fundamentals (continued)
1-8
BNF Rules
1-9
Describing Lists
1-10
An Example Grammar
<program> → <stmts>
<stmts> → <stmt> | <stmt> ; <stmts>
<stmt> → <var> = <expr>
<var> → a | b | c | d
<expr> → <term> + <term> | <term> - <term>
<term> → <var> | const
1-11
An Example Derivation
1-12
Derivations
1-13
Parse Tree
<stmts>
<stmt>
<var> = <expr>
a <term> + <term>
<var> const
b
1-14
Parse tree example
1-15
Ambiguity in Grammars
1-16
An Ambiguous Expression Grammar
<expr> <expr>
1-17
An Unambiguous Expression Grammar
<expr>
<expr> - <term>
const const
1-18
example
1-19
Associativity of Operators
<expr>
<expr>
<expr> + const
<expr> + const
const
1-20
1-21
Extended BNF
1-22
BNF and EBNF
• BNF
<expr> → <expr> + <term>
| <expr> - <term>
| <term>
<term> → <term> * <factor>
| <term> / <factor>
| <factor>
• EBNF
<expr> → <term> {(+ | -) <term>}
<term> → <factor> {(* | /) <factor>}
1-23
Recent Variations in EBNF
1-24
• End of Week 3
1-25
Static Semantics
1-26
Attribute Grammars
1-27
Attribute Grammars : Definition
1-28
Attribute Grammars: Definition
1-29
Attribute Grammars: An Example
• Syntax
<assign> -> <var> = <expr>
<expr> -> <var> + <var> | <var>
<var> A | B | C
• actual_type: synthesized for <var>
and <expr>
• expected_type: inherited for <expr>
1-30
Attribute Grammar (continued)
1-31
Attribute Grammars (continued)
1-32
Attribute Grammars (continued)
<expr>.expected_type ← inherited from parent
<expr>.actual_type ← <var>[1].actual_type
<expr>.actual_type =? <expr>.expected_type
1-33
Semantics
1-34
Operational Semantics
• Operational Semantics
– Describe the meaning of a program by
executing its statements on a machine, either
simulated or actual. The change in the state of
the machine (memory, registers, etc.) defines
the meaning of the statement
• To use operational semantics for a high-
level language, a virtual machine is needed
1-35
Operational Semantics
1-36
Operational Semantics (continued)
1-37
Operational Semantics (continued)
• Uses of operational semantics:
- Language manuals and textbooks
- Teaching programming languages
• Evaluation
- Good if used informally (language
manuals, etc.)
- Extremely complex if used formally (e.g.,VDL)
1-38
Denotational Semantics
1-39
Denotational Semantics - continued
1-40
Denotational Semantics: program state
1-41
Decimal Numbers
1-42
Expressions
1-43
Expressions
Me(<expr>, s) Δ=
case <expr> of
<dec_num> => Mdec(<dec_num>, s)
<var> =>
if VARMAP(<var>, s) == undef
then error
else VARMAP(<var>, s)
<binary_expr> =>
if (Me(<binary_expr>.<left_expr>, s) == undef
OR Me(<binary_expr>.<right_expr>, s) =
undef)
then error
else
if (<binary_expr>.<operator> == '+' then
Me(<binary_expr>.<left_expr>, s) +
Me(<binary_expr>.<right_expr>, s)
else Me(<binary_expr>.<left_expr>, s) *
Me(<binary_expr>.<right_expr>, s)
...
1-44
Assignment Statements
Ma(x := E, s) Δ=
if Me(E, s) == error
then error
else s’ =
{<i1,v1’>,<i2,v2’>,...,<in,vn’>},
where for j = 1, 2, ..., n,
if ij == x
then vj’ = Me(E, s)
else vj’ = VARMAP(ij, s)
1-45
Logical Pretest Loops
Ml(while B do L, s) Δ=
if Mb(B, s) == undef
then error
else if Mb(B, s) == false
then s
else if Msl(L, s) == error
then error
else Ml(while B do L, Msl(L, s))
1-46
Loop Meaning
1-47
Evaluation of Denotational Semantics
1-48
Axiomatic Semantics
1-49
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
1-50
Axiomatic Semantics Form
• An example
– a = b + 1 {a > 1}
– One possible precondition: {b > 10}
– Weakest precondition: {b > 0}
1-51
Program Proof Process
1-52
Axiomatic Semantics: Assignment
1-53
Axiomatic Semantics: Sequences
{P1} S1 {P2}
{P2} S2 {P3}
1-54
Axiomatic Semantics: Selection
1-55
Axiomatic Semantics: Loops
• An inference rule for logical pretest loops
1-56
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
1-57
Loop Invariant
1-58
Evaluation of Axiomatic Semantics
1-59
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
1-60
Summary
1-61