CFG V2
CFG V2
1
Context-Free Languages
Context-Free Pushdown
Grammars Automata
3
Context-Free Grammars
4
Grammar
Formal Definition
G V , T , S , P
V : Set of variables
T : Set of terminal symbols
S : Start variable
P : Set of production rules
5
Repetition: Regular Grammars
Grammar G (V , T , S , P )
A xB A Bx or Cx
x is string of terminals
6
Definition: Context-Free Grammars
Grammar G (V , T , S , P )
12
Example 1:
A context-free grammar G
S aSb
S
A derivation
S aSb aaSbb aabb
13
A context-free grammar G
S aSb
S
Another derivation
14
S aSb
S
n n
L(G ) {a b : n 0}
( ( ( ( ) ) ) )
15
Example 2:
A context-free grammar G
S aSa
S bSb
S
A derivation
S aSa abSba abba
16
A context-free grammar G
S aSa
S bSb
S
Another derivation
R
L(G ) {ww : w {a, b}*}
18
Example 3:
A context-free grammar G
S aSb
S SS
S
A derivation
S SS aSbS abS ab
19
A context-free grammar G
S aSb
S SS
S
A derivation
S SS aSbS abS abaSb abab
20
S aSb
S SS
S
L(G ) {w : na ( w) nb ( w),
and na (v ) nb (v )
in any prefix v}
( )( ( ( ) ) ) ( ( ) ) 21
Example 4:
Language L {a nb m:n m} is context - free.
For the case n m :
S AS1 ,
S1 aS1b|λ,
A aA|a.
For the case n m :
S S1B ,
S1 aS1b|λ,
B bB|b.
n m: n m:
S AS1 , S S1B ,
S1 aS1b|λ, S1 aS1b|λ,
A aA|a. B bB|b.
S AS1|S1B
S1 aS1b|λ
A aA|a
B bB|b
Definition: Context-Free Grammars
Grammar G (V , T , S , P )
A language L is context-free
25
Advantages of context free grammar
• A grammar gives a precise, yet easy to
understand, syntactic specification of a
programming language.
Leftmost derivation
Rightmost derivation
31
Derivation can be represented in a tree form
S AB A aaA | B Bb |
S AB
A B
32
S AB A aaA | B Bb |
S AB aaAB
A B
a a A
33
S AB A aaA | B Bb |
S AB aaAB aaABb
S
A B
a a A B b
34
S AB A aaA | B Bb |
S AB aaAB aaABb aaBb
S
A B
a a A B b
35
S AB A aaA | B Bb |
S AB aaAB aaABb aaBb aab
Derivation Tree S
A B
a a A B b
36
S AB A aaA | B Bb |
S AB aaAB aaABb aaBb aab
Derivation Tree S
A B
yield
a a A B b aab
aab
37
Partial Derivation Trees
S AB A aaA | B Bb |
S AB
Partial derivation tree
S
A B
38
S AB aaAB
Partial derivation tree
A B
a a A
39
S AB aaAB sentential form
a a A
40
Sometimes, derivation order doesn’t matter
Leftmost:
S AB aaAB aaB aaBb aab
Rightmost:
S AB ABb Ab aaAb aab
S
The same derivation tree
A B
a a A B b
41
Ambiguity
42
Definition
43
E E E | E E | (E) | a
a a a derivation (* denotes multiplication)
E E E E a E a E E
a a E a a a
E E
a E E leftmost derivation
a a 44
E E E | E E | (E) | a
a a a derivation
E E E E E E E
a E E a aE
E E
a a a
E E a
leftmost derivation
a a
45
E E E | E E | (E) | a
a aa
E E
E E E E
a E E E E a
a a a a
46
E E E | E E | (E) | a
a aa
Two derivation trees
E E
E E E E
a E E E E a
a a a a
47
The grammar
E E E | E E | (E) | a
is ambiguous!
String a a a has two derivation trees
E E
E E E E
a E E E E a
a a a a
48
The grammar E E E | E E | (E) | a
is ambiguous as the string a a a
has two leftmost derivations:
E E E a E a EE
a a E a a*a
E E E E
a E E E E a
a a a a
50
Why do we care about ambiguity?
2 22
E E
E E E E
2 E E E E 2
2 2 2 2
51
Why do we care about ambiguity?
6 2 22 8
E E
2 4 4 2
E E E E
2 2 2 2
2 E E E E 2
2 2 2 2
2 22 6 2 22 8 52
Correct result: 2 22 6
6
E
2 4
E E
2 2
2 E E
2 2
53
Ambiguity is bad
for programming languages
54
We fix the ambiguous grammar…
E E E | E E | (E) | a
…by introducing parentheses ()
E E T to indicate grouping, (precedence)
E T
T T F Non-ambiguous grammar
T F
F (E)
F a 55
E E T T T F T a T a T F
a F F a aF a aa
E
E E T a aa
E T
E T
T T F T
T F
T F
F
F (E) F a
F a a
a 56
Unique derivation tree
E a aa
E T
T
T F
F a
F
a
a 57
The grammar G : E E T
E T
T T F
T F
F (E)
F a
is non-ambiguous.
Every string w L(G ) has a unique
derivation tree. 58
Inherent Ambiguity
Some context free languages
have only ambiguous grammars!
n n m n m m
Example: L {a b c } {a b c }
S S1 | S 2
S1 S1c | A S 2 aS 2 | B
A aAb | B bBc |
59
n n n
The string a b c
has two derivation trees
S S
S1 S2
S1 c a S2
60
n l n l n!
{a b c : n, l 0}{a : n 0}
Non-regular languages
Context-Free Languages
n n R
{a b } {ww }
Regular Languages
61
Applications:
Compilers
62
Machine Code
Program Add v,v,0
v = 5; cmp v,5
if (v>5) jmplt ELSE
x = 12 + v; THEN:
while (x !=3) { Compiler
add x, 12,v
x = x - 3; ELSE:
v = 10; WHILE:
} cmp x,3
...... ...
63
Compiler
Lexical
parser
analyzer
input output
machine
program
code 64
A parser “knows” the grammar
of the programming language
65
Parser
PROGRAM STMT_LIST
STMT_LIST STMT; STMT_LIST | STMT;
STMT EXPR | IF_STMT | WHILE_STMT
| { STMT_LIST }
derivation
Parser
input EE+E
EE+E
E+E*E
10 + 2 * 5 |E*E
10 + E*E
| INT
10 + 2 * E
10 + 2 * 5
67
derivation derivation tree
E
EE+E
E+E*E
E + E
10 + E*E
10 + 2 * E 10
E E
10 + 2 * 5 *
2 5
68
derivation tree
E machine code
E + E
mult a, 2, 5
10 add b, 10, a
E * E
2 5
69
Parsing examples
70
Parser
input
grammar derivation
string
71
Example:
Parser
S SS derivation
input
S aSb
aabb ?
S bSa
S
72
Exhaustive Search
S SS | aSb | bSa |
74
Phase 2 S SS | aSb | bSa |
S SS SSS
S SS aSbS aabb
Phase 1 S SS bSaS
S SS S SS S
S aSb S aSb aSSb
S aSb aaSbb
S aSb abSab
S aSb ab 75
Phase 2 S SS | aSb | bSa |
S SS SSS
S SS aSbS aabb
S SS S
S aSb aSSb
S aSb aaSbb
Phase 3
S aSb aaSbb aabb
76
Final result of exhaustive search
(top-down parsing)
Parser
S SS
input
S aSb
aabb
S bSa
S
derivation
78
Context Free Art
79
Context-Free Languages
Context-Free Pushdown
Grammars Automata
stack
automaton
80
Pushdown Automata
PDAs
81
Pushdown Automaton - PDA
Input String
Stack
States
82
The Stack
A PDA can write symbols on a stack
and read them later on. y
x
POP reading symbol
z
PUSH writing symbol
All access to the stack only on the top!
(Stack top is written leftmost in the string, e.g. yxz)
A stack is valuable as it can hold an unlimited
amount of information.
The stack allows pushdown automata to
recognize some non-regular languages.
83
The States
Pop old
Input Push new
reading
symbol writing
stack symbol
stack symbol
q1 a , b / c q2
84
q1 a, b / c q2
input
a a
stack
b top c
h Replace h
e e
$ $
(An alternative is to start and finish with empty stack)
85
q1 a , / c q2
input
a a
stack c
b top b
h Push h
e e
$ $
86
q1 a , b / q2
input
a a
stack
b top
h Pop h
e e
$ $
87
q1 a , / q2
input
a a
stack
b top b
h No Change h
e e
$ $
88
Formal Definition
Pushdown Automaton is defined as 7-tuple
M (Q, , , , q 0, z , F )
Final
States states
Input
Stack
alphabet start
Transition start
Stack state
function symbol
alphabet
89
Example 3.7 Salling: Time 0
A PDA for simple nested parenthesis strings
( ( ( ) ) )
Input Stack
(, / ( ), ( /
start
s ), (/ q end
90
Example 3.7 Time 1
Input
( ( ( ) ) ) (
Stack
(, / ( ), ( /
start
s ), (/ q end
91
Example 3.7 Time 2
Input (
( ( ( ) ) ) (
Stack
(, / ( ), ( /
start ), (/ q end
s
92
Example 3.7 Time 3
Input
(
( ( ( ) ) ) (
(
(, / ( ), ( / Stack
start
s ), (/ q end
93
Example 3.7 Time 4
Input
( ( ( ) ) ) (
(
(
(, / ( ), ( /
Stack
start ), (/ q end
s
94
Example 3.7 Time 5
Input
(
( ( ( ) ) )
(
(, / ( ), ( / Stack
start
s ), (/ q end
95
Example 3.7 Time 6
Input
( ( ( ) ) ) (
Stack
(, / ( ), ( /
start
s ), (/ q end
96
Example 3.7 Time 7
Input
( ( ( ) ) )
Stack
(, / ( ), ( /
start ), (/ q end
s
97