You are on page 1of 66

Context-Free Languages

1
n n R
{a b : n  0} {ww }

Regular Languages
a *b * ( a  b) *

2
Context-Free Languages
R
n n
{a b } {ww }

Regular Languages

3
Context-Free Languages

Context-Free Pushdown
Grammars Automata

stack

automaton

4
Context-Free Grammars

5
Grammars
Grammars express languages

Example: the English language

sentence  noun _ phrase predicate

noun _ phrase  article noun

predicate  verb
6
article  a
article  the

noun  cat
noun  dog

verb  runs
verb  walks

7
A derivation of “the dog walks”:

sentence  noun _ phrase predicate


 noun _ phrase verb
 article noun verb
 the noun verb
 the dog verb
 the dog walks

8
A derivation of “a cat runs”:

sentence  noun _ phrase predicate


 noun _ phrase verb
 article noun verb
 a noun verb
 a cat verb
 a cat runs

9
Language of the grammar:

L = { “a cat runs”,
“a cat walks”,
“the cat runs”,
“the cat walks”,
“a dog runs”,
“a dog walks”,
“the dog runs”,
“the dog walks” }
10
Notation

Production Rules

noun  cat
noun  dog

Variable Terminal

11
Another Example
Grammar: S  aSb
S 

Derivation of sentence ab :

S  aSb  ab

S  aSb S 
12
Grammar: S  aSb
S 

Derivation of sentence aabb :


S  aSb  aaSbb  aabb

S  aSb S 
13
Other derivations:

S  aSb  aaSbb  aaaSbbb  aaabbb

S  aSb  aaSbb  aaaSbbb


 aaaaSbbbb  aaaabbbb

14
Language of the grammar

S  aSb
S 

n n
L  {a b : n  0}

15
More Notation

Grammar G   V ,T , S , P 
V : Set of variables

T : Set of terminal symbols


S : Start variable

P: Set of Production rules


16
Example

Grammar G: S  aSb
S 

G   V ,T , S , P 

V  {S } T  {a, b}
P  {S  aSb, S  }
17
More Notation
Sentential Form:
A sentence that contains
variables and terminals

Example:

S  aSb  aaSbb  aaaSbbb  aaabbb

Sentential Forms sentence


18
*
We write: S  aaabbb

Instead of:

S  aSb  aaSbb  aaaSbbb  aaabbb

19
*
In general we write: w1  wn

If: w1  w2  w3    wn

20
*
By default: w  w

21
Example
Grammar Derivations
S  aSb *
S 
S 
*
S  ab
*
S  aabb
*
S  aaabbb
22
Example
Grammar Derivations
S  aSb 
S  aaSbb
S 

aaSbb  aaaaaSbbbbb

23
Another Grammar Example
Grammar G : S  Ab
A  aAb
A

Derivations:
S ⇒ Ab ⇒ b
S ⇒ Ab ⇒ aAbb ⇒ abb
S ⇒ Ab ⇒ aAbb ⇒ aaAbbb ⇒ aabbb
24
More Derivations
S  Ab  aAbb  aaAbbb  aaaAbbbb
 aaaaAbbbbb  aaaabbbbb


S  aaaabbbbb

S  aaaaaabbbbbbb

n n
S a b b
25
Language of a Grammar

For a grammar G
with start variable S:


L(G )  {w : S  w}

String of terminals
26
Example
For grammar G: S  Ab
A  aAb
A

n n
L(G )  {a b b : n  0}


n n
Since: S a b b
27
A Convenient Notation

A  aAb
A  aAb | 
A

article  a
article  a | the
article  the
28
Example

A context-free grammar G: S  aSb


S 

A derivation:

S  aSb  aaSbb  aabb


29
A context-free grammar G: S  aSb
S 

Another derivation:

S  aSb  aaSbb  aaaSbbb  aaabbb


30
S  aSb
S 

n n
L(G )  {a b : n  0}

Describes parentheses: (((( ))))


31
Example

A context-free grammar G: S  aSa


S  bSb
S 

A derivation:

S  aSa  abSba  abba


32
A context-free grammar G: S  aSa
S  bSb
S 

Another derivation:

S  aSa  abSba  abaSaba  abaaba


33
S  aSa
S  bSb
S 

R
L(G )  {ww : w  {a, b}*}

34
Example

A context-free grammar G: S  aSb


S  SS
S 

A derivation:

S  SS  aSbS  abS  ab
35
A context-free grammar G: S  aSb
S  SS
S 

A derivation:

S  SS  aSbS  abS  abaSb  abab


36
S  aSb
S  SS
S 

L(G )  {w : na ( w)  nb ( w),
and na (v)  nb (v)
in any prefix v}
Describes
matched
parentheses: () ((( ))) (( ))
37
Definition: Context-Free Grammars

Grammar G  (V , T , S , P)

Variables Terminal Start


symbols variable

Productions of the form:


A x
Variable String of variables
and terminals
38
G  (V , T , S , P)

*
L(G )  {w : S  w, w  T *}

39
Definition: Context-Free Languages

A language L is context-free

if and only if

there is a context-free grammar G


with L  L(G )

40
Derivation Order
1. S  AB 2. A  aaA 4. B  Bb
3. A   5. B  
Leftmost derivation:
1 2 3 4 5
S  AB  aaAB  aaB  aaBb  aab

Rightmost derivation:
1 4 5 2 3
S  AB  ABb  Ab  aaAb  aab
41
S  aAB
A  bBb
B  A|
Leftmost derivation:
S  aAB  abBbB  abAbB  abbBbbB
 abbbbB  abbbb
Rightmost derivation:
S  aAB  aA  abBb  abAb
 abbBbb  abbbb
42
Derivation Trees

43
S  AB A  aaA |  B  Bb | 

S  AB
S

A B

44
S  AB A  aaA |  B  Bb | 

S  AB  aaAB
S

A B

a a A

45
S  AB A  aaA |  B  Bb | 

S  AB  aaAB  aaABb
S

A B

a a A B b

46
S  AB A  aaA |  B  Bb | 

S  AB  aaAB  aaABb  aaBb


S

A B

a a A B b


47
S  AB A  aaA |  B  Bb | 

S  AB  aaAB  aaABb  aaBb  aab


Derivation Tree S

A B

a a A B b

 
48
S  AB A  aaA |  B  Bb | 

S  AB  aaAB  aaABb  aaBb  aab


Derivation Tree S

A B
yield

a a A B b aab
 aab
 
49
Ambiguity

50
E  E  E | E  E | (E) | a
a  aa

E E  E  E  a E  a EE
 a  a E  a  a*a
E  E
leftmost derivation

a E  E

a a
51
E  E  E | E  E | (E) | a
a  aa

E  EE  E  EE  a EE E


 a  aE  a  aa
E  E
leftmost derivation

E  E a

a a
52
E  E  E | E  E | (E) | a
a  aa
Two derivation trees
E E

E  E E  E

a E  E E  E a

a a a a
53
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
54
The grammar E  E  E | E  E | (E) | a
is ambiguous:

string a  a  a has two leftmost derivations

E  E  E  a E  a EE
 a  a E  a  a*a

E  EE  E  EE  a EE


 a  aE  a  aa 55
Definition:
A context-free grammar G is ambiguous

if some string w L(G ) has:

two or more derivation trees

56
In other words:

A context-free grammar G is ambiguous

if some string w L(G ) has:

two or more leftmost derivations


(or rightmost)

57
Why do we care about ambiguity?

a  aa
take a2
E E

E  E E  E

a E  E E  E a

a a a a
58
2  22

E E

E  E E  E

2 E  E E  E 2

2 2 2 2
59
2  22  6 2  22  8
6 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
60
Correct result: 2  22  6

6
E
2 4
E  E
2 2
2 E  E

2 2
61
• Ambiguity is bad for programming languages

• We want to remove ambiguity

62
Another Ambiguous Grammar

IF_STMT  if EXPR then STMT


| if EXPR then STMT else STMT

63
If expr1 then if expr2 then stmt1 else stmt2
IF_STMT

if expr1 then STMT

if expr2 then stmt1 else stmt2

IF_STMT

if expr1 then STMT else stmt2

if expr2 then stmt1


64
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 S2  aS2 | B
A  aAb |  B  bBc | 
65
n n n
The string a b c
has two derivation trees

S S

S1 S2

S1 c a S2

66

You might also like