Professional Documents
Culture Documents
Ch03 Slides
Ch03 Slides
• Introduction
• The General Problem of Describing Syntax
• Formal Methods of Describing Syntax
• Attribute Grammars
• Describing the Meanings of Programs: Dynamic
Semantics
if (condition) {
. . .
} else {
. . .
}
• This is the syntax of java, in other words, we can say syntax is rules of grammar
and spelling of the language.
• If the writing doesn’t abide with the grammar or spelling, we get an error (invalid
syntax).
• Language Recognizers:
– A recognizer reads input strings over the alphabet of
the language and decides whether the input string
belongs to the language.
1-13
Formal Definition of Languages
• Language Generators:
– Language generator is a device that can be used to
generate the sentences of a language.
• BNF is simple, yet powerful enough to describe nearly all the syntax
of programming languages.
1-24
3.3.1.5 Grammars and Derivations
1-25
Example
Example 3.1
<program> → begin <stmt_list> end
<stmt_list> → <stmt>
| <stmt> ; <stmt_list>
<stmt> → <var> = <expression>
<var> → A | B | C
<expression> → <var>+<var> | <var>-<var> | <var>
<stmt_list> → <stmt>
| <stmt> ; <stmt_list>
OR
<stmt_list> → <stmt>
<stmt_list> → <stmt> ; <stmt_list>
• This language, like most others, is infinite, so one cannot generate all
the sentences in the language in finite time.
Example 3.3:
Two distinct parse trees for the same sentence,
A = B + C * A
• Example 3.2, Rather than allowing the parse tree of an expression to grow
only on the right, this grammar allows growth on both the left and the
right
• If a language structure has more than one parse tree, then the meaning of
the structure cannot be determined uniquely
<expr>
1-40
Continued…
• Figure 3.2, for example, the multiplication operator is
generated lower in the tree, which could indicate that
it has precedence over the addition operator in the
expression. The second parse tree, however, indicates
just the opposite. It appears, therefore, that the two
parse trees indicate conflicting precedence information
1-41
Continued…
• A grammar needs to be written for the simple
expressions we have been discussing that is both
unambiguous and specifies a consistent precedence of
the + and * operators, regardless of the order in which
the operators appear in an expression
1-43
3.3.1.8 Operator Precedence
1-45
3.3.1.8 Operator Precedence
Every derivation with an unambiguous grammar has a
unique parse tree, although that tree can be
represented by different derivations.
1-46
3.3.1.9 Associativity of Operators
1-47
3.3.1.9 Associativity of Operators
• BNF
<expr> → <expr> + <term>
| <expr> - <term>
| <term>
<term> → <term> * <factor>
| <term> / <factor>
| <factor>
• EBNF
<expr> → <term> {(+ | -) <term>}
<term> → <factor> {(* | /) <factor>}
• Syntax
<assign> -> <var> = <expr>
<expr> -> <var> + <var> | <var>
<var> A | B | C
• actual_type: synthesized for <var>
and <expr>
• expected_type: inherited for <expr>
CFG Conditions
• Syntax
<assign> -> <var> = <expr>
<expr> -> <var> + <var> | <var>
<var> A | B | C
• actual_type: synthesized for <var> and <expr>
• expected_type: inherited for <expr>
Attribute Grammars (continued)
<expr>.actual_type <var>[1].actual_type
<expr>.actual_type =? <expr>.expected_type
• X+2
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
Operational Semantics (continued)
• Evaluation
- Good if used informally (language
manuals, etc.)
- Extremely complex if used formally (e.g.,VDL)
Operational Semantics
<bin_num>
<bin_num> ‘0’
<bin_num>
‘1’
‘1’
Copyright © 2012 Addison-Wesley. All rights reserved. 1-77
Mapping
Mbin(‘0’) = 0
Mbin(‘1’) = 1
3
<bin_num> ‘0’
1
<bin_num>
‘1’
This is syntax-directed
‘1’ semantics.
Copyright © 2012 Addison-Wesley. All rights reserved. 1-81
Decimal Numbers
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)
...
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)
• An example
– a = b + 1 {a > 1}
– One possible precondition: {b > 10}
– Weakest precondition: {b > 0}
– Can’t find a weaker precondition since the statement
will not remain true
(I and B) S {I}
{I} while B do S {I and (not B)}
where I is the loop invariant (the inductive
hypothesis)
The weakest precondition for the while loop must
guarantee the truth of the invariant
The truth of the invariant must not change by the
evolution of the loop controlling Boolean and the
loop body
Copyright © 2012 Addison-Wesley. All rights reserved. 1-101
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