








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to the concepts of syntax and semantics in programming languages, focusing on context-free grammar. It covers the rules and principles that govern sentence structure, nonterminal and terminal symbols, productions, and derivation sequences. The document also discusses the limitations of context-free grammar and the need for other approaches like precedence rules and regular expressions for certain syntax definitions.
Typology: Exams
1 / 14
This page cannot be seen from the preview
Don't miss anything!









L. Dillon CSE 452, Fall 2008
1
− “... the rules and principles that govern the sentence structure of any individual language ” -- Wikipedia − Standard:base a programming language syntax on a context free grammar
− “ the meaning or relationship of meanings of a sign or set of signs ” -- Merriam-Webster Dictionary −Standards for defining semantics vary
L. Dillon CSE 452, Fall 2008
2
- Nonterminal symbols for
- Terminal symbols, which
- Productions , rules show-
3
3
- Nonterminal symbols for
- Terminal symbols, which
- Productions , rules show-
0 3.14 15 ‘a’ “hi” ... literals if then else end... keywords x y time len ... identifiers < => + * / ... operators
num - for numeric literals id - for identifiers op - for operators
4
4
- Nonterminal symbols for
- Terminal symbols, which
- Productions , rules show-
e ::= num | id | e op e | ( e ) S ::= read id | write e | id := e | S ; S | if e then S else S | while e do S | { S } P ::= prog S end
L. Dillon CSE 452, Fall 2008
5
A sequence of replacement steps, starting from the start symbol and producing a string of nonterminals
P $ prog S end $ prog S ; S end $ prog read x; S end $ prog read x; if e then S else S end $ prog read x; if e > e then S else S end $ prog read x; if x > e then S else S end $ prog read x; if x > 0 then S else S end $... $ prog read x; if x > 0 then write 0 else write 1 end
L. Dillon CSE 452, Fall 2008
6
A sequence of replacement steps, starting from the start symbol and producing a string of nonterminals
P $ prog S end $ prog S ; S end $ prog read x; S end $ prog read x; if e then S else S end $ prog read x; if e > e then S else S end $ prog read x; if x > e then S else S end $ prog read x; if x > 0 then S else S end $... $ prog read x; if x > 0 then write 0 else write 1 end
P ::= prog S end
7
7
A sequence of replacement steps, starting from the start symbol and producing a string of nonterminals
P $ prog S end $ prog S ; S end $ prog read x; S end $ prog read x; if e then S else S end $ prog read x; if e > e then S else S end $ prog read x; if x > e then S else S end $ prog read x; if x > 0 then S else S end $... $ prog read x; if x > 0 then write 0 else write 1 end
8
8
A sequence of replacement steps, starting from the start symbol and producing a string of nonterminals
P $ prog S end $ prog S ; S end $ prog read x; S end $ prog read x; if e then S else S end $ prog read x; if e > e then S else S end $ prog read x; if x > e then S else S end $ prog read x; if x > 0 then S else S end $... $ prog read x; if x > 0 then write 0 else write 1 end
S ::= read id
L. Dillon CSE 452, Fall 2008
13
− Precedence/associativity rules for operators − Regular expressions for “syntax” of classes of terminals, such as integers, floating point numbers, characters, strings, etc.
− contrast this with dynamic semantics , which is generally what we mean by “semantics”
L. Dillon CSE 452, Fall 2008
14
15
15
Code Data
Program Counter
16
16
Code Data
Program Counter
− put mi − get mj
Here, mi , mj , mk denote data references; li , lj denote labels
mj :
li :
L. Dillon CSE 452, Fall 2008
17
ei
e (^) j op e (^) k
code for ej code for ek m (^) i := m (^) j op mk
e (^) i
num
m (^) i := m (^) j
ei
( ej )
code for e (^) j mi := mj
ei
id j
Rules for translating expressions Convention: m (^) j is a unique memory reference for the expression e (^) j / variable id j
m (^) i := num
computes ej & stores the value in m (^) j
L. Dillon CSE 452, Fall 2008
18
ei
ej op e (^) k
code for e (^) j code for ek m (^) i := mj op mk
ei
num
mi := mj
ei
( e (^) j )
code for e (^) j mi := mj
ei
id j
Rules for translating expressions Convention: m (^) j is a unique memory reference for the expression e (^) j / variable id j
m (^) i := num
computes ek & stores the value in m (^) k
19
19
ei
e (^) j op e (^) k
code for ej code for ek m (^) i := m (^) j op mk
e (^) i
num
m (^) i := m (^) j
ei
( ej )
code for e (^) j mi := mj
ei
id j
Rules for translating expressions Convention: m (^) j is a unique memory reference for the expression e (^) j / variable id j
m (^) i := num
computes ei & stores the value in m (^) i
20
20
Example: translating expressions
2 ***** ( a - b )
a b
e
e e
e
e e
L. Dillon CSE 452, Fall 2008
25
Example: translating expressions
a 6 b 7
e 3
e 4 e 5
e 2
e 0
e (^1)
e (^) i
id j
m (^) i := mj
L. Dillon CSE 452, Fall 2008
26
Example: translating expressions
a 6 b 7
e 3
e 4 e 5
e 2
e 0
e 1
ei
id j
m (^) i := mj
27
27
Example: translating expressions
a 6 b 7
e 3
e 4 e 5
e 2
e 0
e (^1)
e (^) i
id j
m (^) i := mj
28
28
Example: translating expressions
a 6 b 7
e 3
e 4 e 5
e 2
e 0
e 1
e (^) i
ej op ek
code for e (^) j code for e (^) k mi := mj op mk
L. Dillon CSE 452, Fall 2008
29
Example: translating expressions
a 6 b 7
e 3
e 4 e 5
e 2
e 0
e (^1)
e (^) i
ej op ek
code for ej code for e (^) k mi := mj op mk
L. Dillon CSE 452, Fall 2008
30
Example: translating expressions
a 6 b 7
e 3
e 4 e 5
e 2
e 0
e 1
31
31
Ambiguity
e 2
e 0
e 1 *****
e 3 - e 4
a b
e 2
e 0
e 1 -
e 3 ***** e (^4)
2 a
b
2 ***** a - b
Suppose a = 5 and b = 1
32
32
Rules for translating statements Conventions:
Si
read id j
code for ej put mj
Si
write ej
code for ek mj := m (^) k
l (^) i :
S (^) i
vj := ek
code for Si exit
lbeg:
S (^) i
code for Sj code for Sk
li :
Si
Sj ; Sk
S (^) i
{ S (^) j
prog end
lend:
L. Dillon CSE 452, Fall 2008
37
Translating statements
prog S 1 end
read x 4 if e 5 then S 6 else S 7
e 8 > e 9
x 4 0 write
e 10
write
e 11
prog S^ i end
use: ni = lend
L. Dillon CSE 452, Fall 2008
38
Translating statements
prog S 1 end
read x 4 if e 5 then S 6 else S 7
e 8 > e 9
x 4 0 write
e 10
write
e 11
prog Si end
use: ni = lend
39
39
Translating statements
prog S 1 end
read x 4
if e 5 then S 6 else S 7
e 8 > e 9
x 4 0 write
e 10
write
e 11
40
40
Translating statements
prog S 1 end
read x 4
if e 5 then S 6 else S 7
e 8 > e 9
x 4 0 write
e 10
write
e 11
use: nj = lk n (^) k = ni
Si
S (^) j ; Sk
L. Dillon CSE 452, Fall 2008
41
Translating statements
prog S 1 end
read x 4 if e 5 then S 6 else S 7
e 8 > e 9
x 4 0 write
e 10
write
e 11
L. Dillon CSE 452, Fall 2008
42
Translating statements
prog S 1 end
read x 4 if e 5 then S 6 else S 7
e 8 > e 9
x 4 0 write
e 10
write
e 11
use: nk = ni nq = ni
S (^) i
if ej then^ S^ k else S^ q
43
43
Translating statements
prog S 1 end
read x 4
if e 5 then S 6 else S 7
e 8 > e 9
x 4 0 write
e 10
write
e 11
44
44
Translating statements
prog S 1 end
read x 4
if e 5 then S 6 else S 7
e 8 > e 9
x 4 0 write
e 10
write
e 11
L. Dillon CSE 452, Fall 2008
49
Translating statements
prog S 1 end
read x 4 if e 5 then S 6 else S 7
e 8 > e 9
x 4 0 write
e 10
write
e 11
L. Dillon CSE 452, Fall 2008
50
Compiler
Real Machine
(CPU, memory, ...)
Source Program
... if (x == 0) { x = x + 1; } ...
Target Program
... cmp (1000), $ bne L add (1000), $ L: ...
Data
Output
Source and target programs have the same semantics.
Target program is in machine or assembly code.
Machine interpreter executes the target program.
Compiler translates the source program into a target program
51
51
Source Program
Syntax analyzer (parser)
Lexical analyzer (scanner)
Semantic analyzer (type checker) Intermediate code generator
Code optimizer
Code generator
Target Program
52
52
Source Program
Interpreter
Data
Output
Software interpreter “executes” source program instructions, often interactively.
Traditionally: Read - Eval - Print loop
53
Source Program
Data
Output
Compiler
Target Program
Source Language-specific Virtual Machine
54
Java Compiler
Java Virtual Machine (JVM)
Source Program
... if (x == 0) { x = x + 1; } ...
Target Program
... iload_ iconst_ ifne L inc store_ L:...
Data
Output
Important design goals:
_- Portability