Chapter 03 Describing Programming Languages
Chapter 03 Describing Programming Languages
Lexical Analyzer
ID ID ID CONST ID
IF ( < THEN = “1” * “d”
“a” “b” “c”
Token Sequence
cond_expr a
Syntax Analyzer <
IF_stmt b lhs
Syntax Tree list c
assign_stmt rhs 1
Semantic Analyzer *
d
GE a, b, L1
3-Address Code MUlT 1, d, c
L1:
Code Optimizer GE a, b, L1
MOV d, c
L1:
Optimized 3-Addr. Code loadi R1,a
cmpi R1,b
jge L1
Code Generation loadi R1,d
storei R1,c
Assembly Code L1:
Integer:
digit =
`0`+`1`+`2`+`3`+`4`+`5`+`6`+`7`+`8`
+`9`
integer = digit digit*
BNF fundamentals
Two types of symbols
Non-terminal symbols: BNF abstraction
Terminal symbols: tokens
A set of grammar rules
LHS → RHS
<program> <stmts>
<stmts> <stmt> | <stmt> , <stmts>
<stmt> <var> = <expr>
<var> a | b | c | d
<expr> <term> + <term> | <term> - <term>
<term> <var> | const
IUBAT CSC 461 01/05/25 15
Backus-Naur Form (BNF)
<if_stmt> → if ( <logic_expr> ) <stmt>
: Derives
•Derivations are called
sentential form
•Leftmost derivation
Instead of using <expr> for both operands of both + and *, we could use
three nonterminal to represent operands, which allows the grammar
to force different operators to different levels in the parse tree.
EBNF
After 2nd rule: <expr> <expr> (+ | -) <term>
<term> <term> (* | /) <factor>
<factor> <exp> ** <factor>
<exp> (<expr>) | id
c1 c2 c3 c4
Synthesized attributes: values are computed from ones of the children nodes
Synthesized of P = f(c1, c2, c3, c4)
S1 S2 S3 S4
Inherited attributes: values are computed from attributes of the siblings and parent of
the node
Inherited of S4= f(P, S1, S2, S3)
D E F D E F
actual_type
A synthesized attribute associated with the nonterminals <var> and
<expr>. It is used to store the actual type, int or real, of a variable
or expression. In the case of a variable, the actual type is intrinsic. In
the case of an expression, it is determined from the actual types of
the child nodes of the <expr> nonterminal.
expected_type
An inherited attribute associated with the nonterminal <expr>. It is
used to store the type, either int or real, that is expected for the
expression, as determined by the type of the variable on the left
IUBAT CSC 461 01/05/25 39
side of the assignment statement
Example-1
To check the type rules of a simple assignment
Question: The syntax statement.
portion of our example attribute
grammar is:
<assign> → <var> = <expr>
<expr> → <var> + <var> | <var>
<var> → A | B | C
Find the General rule, syntax and static semantics & flow of
attributes in the parse tree of this assignment statement.
General rules:
1. The only variable names are A, B, and C.
2. The right side of the assignments can be either a variable or an
expression in the form of a variable added to another variable.
3. The variables can be one of two types: int or real.
4. When there are two variables on the right side of an assignment, they
need not be the same type.
5. The type of the expression when the operand types are not the same is
always real.
6. When they are the same, the expression type is that of the operands.
7. The type of the left side of the assignment must match the type of the
right side. IUBAT CSC 461 01/05/25 40
The look-up function looks up a given variable name in the symbol
IUBAT CSC 461 01/05/25 41
table and returns the variable’s type.
process of computing
the attribute values of a
parse tree, which is
sometimes called
decorating the parse
tree. If all attributes
were inherited, this
could proceed in a
completely top-down
order, from the root to
the leaves. Alternatively,
it could proceed in a
completely bottom-up
order, from the leaves to
the root, if all the
attributes were
synthesized. Because
our grammar has both
synthesized and
inherited attributes, the
evaluation process
cannot be in any single
direction. The following
is an evaluation of the
attributes, in an order in
which it is possible to
compute them: IUBAT CSC 461 01/05/25 42
The flow of Attributes
in the Tree
b / 2 - 1 < 10
b < 22
Thus, the weakest precondition for the given assignment statement and
postcondition is {b < 22}
IUBAT CSC 461 01/05/25 49
Example
Compute the Weakest Pre-
Condition for each of the
following Assignment
Statements and for the Post-
Conditions:
I. a = 3*a - 2 * b + 21
{a > -7}
II.x = 4 * y – 5 * x + 13
{x < 9}
IUBAT CSC 461 01/05/25 50
Evaluation of Axiomatic
Semantics
Developing axioms or inference rules
for all of the statements in a
language is difficult
<bin_num> → '0'
| '1'
| <bin_num> '0'
| <bin_num> '1'