Chapter 3 of Programming Languages
Chapter 3 of Programming Languages
Count ++;
coun identifier
++ operator Post_Incr_o
ox23
; semicolo
Count + +
coun identifie
+ operator Plus-
+ operator Plus-
Language
Language
recognizer
generator
Int a, b, c, d, e;
Chapter 03 Page 1
<com_stat> --> { <statlist>}
<statlist> ---> <stat>
|<stat> <statlist>
Program
Begin
A=A-C;
B=C
end
Assign
id = expr
A
id * expr
B
( expr )
id + expr
A
id
Chapter 03 Page 2
A=B*C+A
expr
=
id
expr
A
+
expr
term
ter
factor
*
term
factor
id
factor
id
A
id
C
Chapter 03 Page 3
INT IN C language
1) Size =2 bytes (-32768 .. 32767)
2) Representation = 2's complement
Variable
1) Type
2) Value
3) Scope
4) name
Chapter 03 Page 4
Semantic Attributes
Monday, September 27, 2021
1:59 PM
A= B + 25
EX: Ex:
Procedure myproc Procedure myproc
.. …
End myproc End hisproc
A= A+B
Chapter 03 Page 5
Actual type = real
Chapter 03 Page 6
Describing semantics
Monday, September 27, 2021
2:32 PM
Operational Semantics
= use the goto and label For statement in C
= use simple statements that can be understood by the computer
<for_stat> --> for( expr1; expr1; expr3)
Use operation semantics to evaluate A* B , knowing that computer <stat>
doesn't have *
Out:
20
Bin_num
10100
Denotational Semantics
2*10
BNF for binary number Bin_num
<bin_num> 0
|1 2*5 0
|<bin_num> 0
|<bin_num>1 Bin_num 0
2*2 +1
Bin_num
1
2*1
0
Bin_num
0, 0 1
1, 1
Chapter 03 Page 7
0, 0 1
1, 1
Mbin = {⬚
2 ∗ 𝑀𝑏𝑖𝑛 , < 𝑏𝑖𝑛𝑛𝑢𝑚 > 0
2 ∗ 𝑀𝑏𝑖𝑛 + 1, < 𝑏𝑖𝑛𝑛𝑢𝑚 > 1
Int x = 100;
x = 10;
Int x = 3;
}
Axiomatic semantic
Chapter 03 Page 8
{x=Ex, y=Ey}
Swap code {x=Ex, y=Ey} t=x {t=Ex, y=Ey}
{x=Ex, y=Ey} {t=Ex, y=Ey} x=y {t=Ex, x=Ey}
t=x {t=Ex, x=Ey} y=t {y=Ex, x=Ey}
x=y
y=t
{y=Ex, x=Ey}
{x=Ex, y=Ey}
Check Swap code if it is true {x=Ex, x=Ey} t=x {t=Ex, t=Ey}
{x=Ex, y=Ey} {t=Ex, t=Ey} y=t {y=Ex, y=Ey}
t=x {y=Ex, y=Ey} x=y {y=Ex, x=Ey}
y=t {y=Ex, x=Ey}
x=y
{y=Ex, x=Ey} It is false
a<3
2*b+1 <3
2b<2
b<1
Chapter 03 Page 9