PL 10 CH 3
PL 10 CH 3
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
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
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
<program> <stmts>
<stmts> <stmt> | <stmt> ; <stmts>
<stmt> <var> = <expr>
<var> a | b | c | d
<expr> <term> + <term> | <term> - <term>
<term> <var> | const
<stmts>
<stmt>
<var> = <expr>
a <term> + <term>
<var> const
b
Copyright 2012 Addison-Wesley. All rights reserved. 1-14
Ambiguity in Grammars
<expr> <expr>
<expr>
<expr> - <term>
const const
Copyright 2012 Addison-Wesley. All rights reserved. 1-17
Associativity of Operators
<expr>
<expr>
<expr> + const
<expr> + const
const
Copyright 2012 Addison-Wesley. All rights reserved. 1-18
Extended BNF
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>
<expr>.actual_type <var>[1].actual_type
<expr>.actual_type =? <expr>.expected_type
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
Evaluation
- Good if used informally (language
manuals, etc.)
- Extremely complex if used formally (e.g.,VDL)
Copyright 2012 Addison-Wesley. All rights reserved. 1-34
Denotational Semantics
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)
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))
An example
a = b + 1 {a > 1}
One possible precondition: {b > 10}
Weakest precondition: {b > 0}
{P1} S1 {P2}
{P2} S2 {P3}
(I and B) S {I}
{I} while B do S {I and (not B)}