0% found this document useful (0 votes)
22 views

Chapter 3 of Programming Languages

Uploaded by

221155
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Chapter 3 of Programming Languages

Uploaded by

221155
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Syntax and Semantics

Monday, September 20, 2021


2:31 PM

Syntax : is the statement true according to the language rules.


Semantics : the meaning of a statement.
How to describe the syntax and semantics of a language.

Lexe: the smallest part of the sentence


Token: the class of the lexemes

Count ++;

coun identifier
++ operator Post_Incr_o
ox23
; semicolo

Count + +

coun identifie
+ operator Plus-
+ operator Plus-

Language
Language
recognizer
generator

How to describe the systax


1) Context free grammer
2) BNF:
a. Abstraction <….>
b. Definition -->
i. Symbols
ii. Reserve words
iii. Other abstractions

‫ المبتدا الخ‬-- ‫الجمله اﻻسم ه‬

Int a, b, c, d, e;

<idlist> --> <id>


|<id> , <idlist>

<com_stat> --> { <statlist>}

Chapter 03 Page 1
<com_stat> --> { <statlist>}
<statlist> ---> <stat>
|<stat> <statlist>

<dec_stat> --> <type> <idlist>;

Program=>begin <stat_list> end


=>begin <stat> ; <stat_list> end
=>begin <var>= <expression> ; <stat> end
=>begin A= <var>-<var> ; <var>=<expression> end
=>begin A= A-C ; B=<var> end
=>begin A= A-C ; B=C end

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

<stat> --> <expr_stat>


|< null stat> Boolean operation (&&) in C
|<if_stat>
P Q P
|<while_stat>
…. 0 0 0
0 Nz 0
Nz 0 0
NZ NZ 1
EBNF
() one of |||| (+|-)
[] ==> optional
{}==> repetition

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

Predicate : <proc_name>[1]length <32

A= A+B

Chapter 03 Page 5
Actual type = real

Actual type=int Actualtype=int Actual type =real

Expected type=int Expectedtype =int Expected type


= real

Actual type=int Actual type= real


Actual type=int
Lookup(A) Lookup(b)

Chapter 03 Page 6
Describing semantics
Monday, September 27, 2021
2:32 PM

How the computer understand the code


Three ways to describe the semantics
1) Operational semantics
2) Axiomatic semantics
3) Denotation semantics

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 *

Set T = 0 Operational semantics


L1: T= T+A
Dec B Expr1
If B<> 0 goto L1 Loop: expr2
L2: If expr2== 0 goto out
<stat>
Expr3
Goto loop

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

{Precondition } <Statement> { Post condition}

Program Program Program

{Precondition P} { P0} {p1} { P0} {p1}


<stat1> <stat1> {P1}<stat1>{P2}
<stat2> <stat2> {P2}<stat2>{Q2}
.. .. ..
<statn> <statn> {Pn}<statn>
{Postcondition Q} { Qn} { Qn}

{P0} --> {P1} {P0} --> {P1}

P1, p2, P3,


Q
{B> 1} A = B+3; {A>10}

>Assign --> Var = exp A> 10


B+3 >10
B>7
{p} X=exp {Q}
????????
{B>1} --> {B>7} FALSE
P = Q x=exp

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

<Union def> --> union <unionname> <float_lit> --->


<leftbrace> {(0|1|2|3|4|5|6|7|8|9)}[.{(0|1|2|3|4|5|6|7|8|
{ <type> <id>;} 9)}]
<rightbrace>;

A = 2*b +1 {b<1} A = 2*b +1 {a<3}


b= a-3 {a<3} b= a-3 { b<0} b<0
{ b<0} { b<0} a-3<0
a<3

a<3
2*b+1 <3
2b<2
b<1

Chapter 03 Page 9

You might also like