Professional Documents
Culture Documents
Formal Languages & Finite Theory of Automata: BS Course
Formal Languages & Finite Theory of Automata: BS Course
Theory of Automata
BS Course
Slide # : 05
Muhammad Faizan Tahir
Context-Free Languages
2
Context-Free Languages
n n R
{a b : n 0} {ww }
Regular Languages
a *b * ( a b) *
3
Context-Free Languages
Context-Free Pushdown
Grammars Automata
stack
automaton
4
Context-Free Grammars
5
Grammars
Grammars express languages
predicate verb
6
article a
article the
noun cat
noun dog
verb runs
verb sleeps
7
Derivation of string “the dog walks”:
8
Derivation of string “a cat runs”:
9
Language of the grammar:
L = { “a cat runs”,
“a cat sleeps”,
“the cat runs”,
“the cat sleeps”,
“a dog runs”,
“a dog sleeps”,
“the dog runs”,
“the dog sleeps” }
10
Productions Sequence of
Terminals (symbols)
noun cat
11
Backus Naur Form
E.g.
<number> = <digit> | <number> <digit>
<digit> = 0, 1, 2, …, 9
12
Another Example
Sequence of
terminals and variables
Grammar: S aSb
S
13
Grammar: S aSb
S
Derivation of string ab :
S aSb ab
S aSb S
14
Grammar: S aSb
S
S aSb S
15
Grammar: S aSb
S
Other derivations:
n n
L {a b : n 0}
17
A Convenient Notation
*
We write: S aaabbb
for zero or more derivation steps
Instead of:
18
*
In general we write: w1 wn
If: w1 w2 w3 wn
*
Trivially: w w
19
Example Grammar Possible Derivations
S aSb *
S
S *
S ab
*
S aaabbb
S aaSbb aaaaaSbbbbb
20
Another convenient notation:
S aSb
S aSb |
S
article a
article a | the
article the
21
Formal Definitions
Grammar: G V , T , S , P
Set of
variables
Set of Start Set of
terminal variable productions
symbols
22
Context-Free Grammar: G (V , T , S , P )
A s
Variable String of
variables and
terminals
23
Example of Context-Free Grammar
S aSb |
productions
P {S aSb, S }
G N ,T , S, P
N {S} S
24
Language of a Grammar:
String of terminals or
25
Example:
L(G ) {a b : n 0}
n n
27
Example:
L {a b : n 0 }
n n
is a context-free language
since context-free grammar G :
S aSb |
generates L(G ) L
28
Another Example
Context-free grammar G :
S aSa | bSb |
Example derivations:
S aSa abSba abba
S aSa abSba abaSaba abaaba
R
L(G ) {ww : w {a, b}*}
Palindromes of even length
29
Another Example
Context-free grammar G:
S aSb | SS |
Example derivations:
S SS aSbS abS ab
S SS aSbS abS abaSb abab
L(G ) {w : na ( w) nb ( w),
and na (v) nb (v)
Describes
in any prefix v}
matched
parentheses: () ((( ))) (( )) a (, b )
30
Derivation Order
and
Derivation Trees
31
Derivation Order
1. S AB 2. A aaA 4. B Bb
3. A 5. B
32
1. S AB 2. A aaA 4. B Bb
3. A 5. B
1 2 3 4 5
S AB aaAB aaB aaBb aab
1 4 5 2 3
S AB ABb Ab aaAb aab
At each step, we substitute the
rightmost variable
34
1. S AB 2. A aaA 4. B Bb
3. A 5. B
S AB A aaA | B Bb |
36
S AB A aaA | B Bb |
S AB
S
A B
yield AB
37
S AB A aaA | B Bb |
S AB aaAB
S
A B
yield aaAB
a a A
38
S AB A aaA | B Bb |
S AB aaAB aaABb
S
A B
a a A B b
yield aaABb
39
S AB A aaA | B Bb |
S AB aaAB aaABb aaBb
S
A B
a a A B b
yield
aaBb aaBb
40
S AB A aaA | B Bb |
S AB aaAB aaABb aaBb aab
Derivation Tree S
(parse tree)
A B
a a A B b
yield
aab aab
41
Sometimes, derivation order doesn’t matter
Leftmost derivation:
S AB aaAB aaB aaBb aab
Rightmost derivation:
S AB ABb Ab aaAb aab
S
Give same A B
derivation tree
a a A B b
42
Ambiguity
43
Grammar for mathematical expressions
E E E | E E | (E) | a
Example strings:
(a a ) a (a a (a a ))
44
E E E | E E | (E) | a
E E E a E a EE
E
a a E a a*a
E E
A leftmost derivation
for a a a
a E E
a a
45
E E E | E E | (E) | a
a a
46
E E E | E E | (E) | a
E E E E
a E E E E a
a a a a
47
take a2
a a a 2 22
E E
E E E E
2 E E E E 2
2 2 2 2
48
Good Tree Bad Tree
2 22 6 2 22 8
6 Compute expression result 8
E using the tree E
2 4 4 2
E E E E
2 2 2 2
2 E E E E 2
2 2 2 2
49
Two different derivation trees
may cause problems in applications which
use the derivation trees:
• Evaluating expressions
• In general, in compilers
for programming languages
50
Ambiguous Grammar:
A context-free grammar G is ambiguous
if there is a string w L(G ) which has:
E E E E
a E E E E a
a a a a
52
E E E | E E | (E) | a
this grammar is ambiguous also because
string a a a has two leftmost derivations
E E E a E a EE
a a E a a*a
Variables Terminals
56
A successful example:
Equivalent
Ambiguous
Non-Ambiguous
Grammar
Grammar
E E E
E E T |T
E E E
T T F | F
E (E )
E a F (E ) | a
generates the same
language
57
E E T T T F T a T a T F
a F F a aF a aa
E
E E T |T
E T
T T F | F
F (E) | a T T F
F F a
Unique
derivation tree
for a a a a a
58
An un-successful example:
n n m n m m
L {a b c } {a b c }
n, m 0
L is inherently ambiguous:
59
Example (ambiguous) grammar for L:
n n m n m m
L {a b c } {a b c }
S S1 | S 2 S1 S1c | A S 2 aS2 | B
A aAb | B bBc |
60
The string a n b n c n L
has always two different derivation trees
(for any grammar)
For example
S S
S1 S2
S1 c a S2
61