Sie sind auf Seite 1von 62

Context-Free Languages

Fall 2005 Costas Busch - RPI 1


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

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

Fall 2005 Costas Busch - RPI 2


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

Regular Languages

Fall 2005 Costas Busch - RPI 3


Context-Free Languages

Context-Free Pushdown
Grammars Automata

stack

automaton

Fall 2005 Costas Busch - RPI 4


Context-Free Grammars

Fall 2005 Costas Busch - RPI 5


Grammars
Grammars express languages

Example: the English language

sentence  noun _ phrase predicate

noun _ phrase  article noun

predicate  verb
Fall 2005 Costas Busch - RPI 6
article  a
article  the

noun  cat
noun  dog

verb  runs
verb  sleeps

Fall 2005 Costas Busch - RPI 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 sleeps

Fall 2005 Costas Busch - RPI 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

Fall 2005 Costas Busch - RPI 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” }
Fall 2005 Costas Busch - RPI 10
Notation

Production Rules

noun  cat
noun  dog

Variable Terminal

Fall 2005 Costas Busch - RPI 11


Another Example
Grammar: S  aSb
S 

Derivation of sentence ab :

S  aSb  ab

S  aSb S 
Fall 2005 Costas Busch - RPI 12
Grammar: S  aSb
S 

Derivation of sentence aabb :


S  aSb  aaSbb  aabb

S  aSb S 
Fall 2005 Costas Busch - RPI 13
Grammar: S  aSb
S 

Other derivations:

S  aSb  aaSbb  aaaSbbb  aaabbb

S  aSb  aaSbb  aaaSbbb


 aaaaSbbbb  aaaabbbb
Fall 2005 Costas Busch - RPI 14
Language of the grammar

S  aSb
S 

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

Fall 2005 Costas Busch - RPI 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


Fall 2005 Costas Busch - RPI 16
Example

Grammar G: S  aSb
S 

G   V ,T , S , P 

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

Example:

S  aSb  aaSbb  aaaSbbb  aaabbb

Sentential Forms sentence


Fall 2005 Costas Busch - RPI 18
*
We write: S  aaabbb

Instead of:

S  aSb  aaSbb  aaaSbbb  aaabbb

Fall 2005 Costas Busch - RPI 19


*
In general we write: w1  wn

If: w1  w2  w3    wn

In zero or more steps

*
Trivially: w  w
Fall 2005 Costas Busch - RPI 20
Example Grammar Possible Derivations
S  aSb S 
*

S  *
S  ab
*
S  aaabbb


S  aaSbb

aaSbb  aaaaaSbbbbb

Fall 2005 Costas Busch - RPI 21


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

Derivations:
S ⇒ Ab ⇒ b
S ⇒ Ab ⇒ aAbb ⇒ abb
S ⇒ Ab ⇒ aAbb ⇒ aaAbbb ⇒ aabbb
Fall 2005 Costas Busch - RPI 22
More Derivations
S  Ab  aAbb  aaAbbb  aaaAbbbb
 aaaaAbbbbb  aaaabbbbb


Grammar
S  aaaabbbbb S  Ab
 A  aAb
S  aaaaaabbbbbbb A

n n
S a b b
Fall 2005 Costas Busch - RPI 23
Language of a Grammar

For a grammar G
with start variable S:


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

String of terminals
Fall 2005 Costas Busch - RPI 24
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
Fall 2005 Costas Busch - RPI 25
A Convenient Notation

A  aAb
A  aAb | 
A

article  a
article  a | the
article  the
Fall 2005 Costas Busch - RPI 26
Example
Context-free grammar G: S  aSb | 

Possible Derivations:
S  aSb  aaSbb  aabb
S  aSb  aaSbb  aaaSbbb  aaabbb

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

Describes parentheses: (((( ))))


a  (, b )
Fall 2005 Costas Busch - RPI 27
Example
Context-free grammar G :
S  aSa | bsb | 
Derivations:
S  aSa  abSba  abba
S  aSa  abSba  abaSaba  abaaba

R
L(G )  {ww : w  {a, b}*}
Palindromes of even length
Fall 2005 Costas Busch - RPI 28
Example
A context-free grammar G:
S  aSb | SS | 
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 )
Fall 2005 Costas Busch - RPI 29
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
Fall 2005 Costas Busch - RPI 30
G  (V , T , S , P)

The language of the grammar is:


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

Fall 2005 Costas Busch - RPI 31


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 )

Fall 2005 Costas Busch - RPI 32


Derivation Order
and
Derivation Trees

Fall 2005 Costas Busch - RPI 33


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
Fall 2005 Costas Busch - RPI 34
S  aAB
A  bBb
B  A|
Leftmost derivation:
S  aAB  abBbB  abAbB  abbBbbB
 abbbbB  abbbb
Rightmost derivation:
S  aAB  aA  abBb  abAb
 abbBbb  abbbb
Fall 2005 Costas Busch - RPI 35
Derivation Trees
S  AB A  aaA |  B  Bb | 

S  AB
S

A B

Fall 2005 Costas Busch - RPI 36


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

S  AB  aaAB
S

A B

a a A

Fall 2005 Costas Busch - RPI 37


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

S  AB  aaAB  aaABb
S

A B

a a A B b

Fall 2005 Costas Busch - RPI 38


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

S  AB  aaAB  aaABb  aaBb


S

A B

a a A B b


Fall 2005 Costas Busch - RPI 39
S  AB A  aaA |  B  Bb | 

S  AB  aaAB  aaABb  aaBb  aab


Derivation Tree S

A B

a a A B b

 
Fall 2005 Costas Busch - RPI 40
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
 
Fall 2005 Costas Busch - RPI 41
Partial Derivation Trees
S  AB A  aaA |  B  Bb | 

S  AB
Partial derivation tree S

A B

Fall 2005 Costas Busch - RPI 42


S  AB  aaAB

Partial derivation tree S

A B

a a A

Fall 2005 Costas Busch - RPI 43


sentential
S  AB  aaAB
form

Partial derivation tree S

A B

yield
a a A
aaAB
Fall 2005 Costas Busch - RPI 44
Sometimes, derivation order doesn’t matter
Leftmost:
S  AB  aaAB  aaB  aaBb  aab
Rightmost:
S  AB  ABb  Ab  aaAb  aab
S
Same derivation tree
A B

a a A B b

Fall 2005 Costas Busch - RPI   45


Ambiguity

Fall 2005 Costas Busch - RPI 46


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
Fall 2005 Costas Busch - RPI 47
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
Fall 2005 Costas Busch - RPI 48
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
Fall 2005 Costas Busch - RPI 49
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
Fall 2005 Costas Busch - RPI 50
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


Fall 2005
 a  aE  a  aa
Costas Busch - RPI 51
Definition:
A context-free grammar G is ambiguous

if some string w L(G ) has:

two different derivation trees


OR
two leftmost derivations

Fall 2005 Costas Busch - RPI 52


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
Fall 2005 Costas Busch - RPI 53
2  22

E E

E  E E  E

2 E  E E  E 2

2 2 2 2
Fall 2005 Costas Busch - RPI 54
Good Bad
derivation Tree derivation Tree
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
Fall 2005 Costas Busch - RPI 55
• Ambiguity is bad for programming languages

• We want to remove ambiguity

Fall 2005 Costas Busch - RPI 56


We fix the ambiguous grammar:
E  E  E | E  E | (E) | a

New unambiguous grammar:

E  E T |T
T T F | F
F  (E) | a

Fall 2005 Costas Busch - RPI 57


E  E T T T  F T  a T  a T F
 a  F F  a  aF  a  aa
E a  aa
E  E T |T
E  T
T T F | F
F  (E) | a T T  F

F F a
Unique
derivation tree
a a
Fall 2005 Costas Busch - RPI 58
Another Ambiguous Grammar

IF_STMT  if EXPR then STMT


| if EXPR then STMT else STMT

Fall 2005 Costas Busch - RPI 59


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


Fall 2005 Costas Busch - RPI 60
Inherent Ambiguity

Some context free languages


have only ambiguous grammars
n, m  0
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 | 
Fall 2005 Costas Busch - RPI 61
n n n
The string a b c
has always two derivation trees
(for any grammar)

For example
S S

S1 S2

S1 c a S2

Fall 2005 Costas Busch - RPI 62

Das könnte Ihnen auch gefallen