You are on page 1of 50

Context-Free Languages

Courtesy Costas Busch - RPI

{a b }

n n

{ww }

Regular Languages

Courtesy Costas Busch - RPI

Context-Free Languages

{a b }

n n

{ww }

Regular Languages

Courtesy Costas Busch - RPI

Context-Free Languages

Context-Free Grammars

Pushdown Automata stack automaton


Courtesy Costas Busch - RPI 4

Context-Free Grammars

Courtesy Costas Busch - RPI

Example
A context-free grammar

G:

S p aSb S pP

A derivation:

S aSb aaSbb aabb


Courtesy Costas Busch - RPI 6

A context-free grammar

G:

S p aSb S pP

Another derivation:

S aSb aaSbb aaaSbbb aaabbb


Courtesy Costas Busch - RPI 7

S p aSb S pP

L(G ) ! {a b : n u 0}

n n

Courtesy Costas Busch - RPI

Example
A context-free grammar

G:

S p aSa S p bSb S pP

A derivation:

S aSa abSba abba


Courtesy Costas Busch - RPI 9

A context-free grammar

G:

S p aSa S p bSb S pP

Another derivation:

S aSa abSba abaSaba abaaba


Courtesy Costas Busch - RPI 10

S p aSa S p bSb S pP

L(G ) ! {ww : w {a, b}*}


Courtesy Costas Busch - RPI 11

Example
A context-free grammar

G:

S p aSb S p SS S pP

A derivation:

S SS aSbS abS ab
Courtesy Costas Busch - RPI 12

A context-free grammar

G:

S p aSb S p SS S pP

A derivation:

S SS aSbS abS abaSb abab


Courtesy Costas Busch - RPI 13

S p aSb S p SS S pP
L(G ) ! {w : na ( w) ! nb ( w), and na (v) u nb (v)

in any prefix v}

Courtesy Costas Busch - RPI

14

Definition: Context-Free Grammars


Grammar Variables

G ! (V ,T , S , P)
Terminal symbols Start variables

Productions of the form:

A pE
E
is string of Courtesy Costas Busch - RPI terminals variables and
15

Notation: Context-Free Grammars


Lower case letters near beginning of alphabet (e.g. a, b, c, ) are terminal symbols Upper case letters near beginning of alphabet (e.g. A, B, C, ) are variables Lower case letters near end of alphabet (e.g. w, x, ) are strings of terminals Upper case letters near end of alphabet (e.g. X, Y, ) are either terminals or strings Lower case Greek letters (e.g. , , , ) are strings of terminals and/or variables
Courtesy Costas Busch - RPI 16

Definition: Context-Free Languages


A language

L is context-free

if and only if there is a grammar

G with L ! L(G )

Courtesy Costas Busch - RPI

17

Recall Language of a Grammar


For a grammar G with start variable

L(G ) ! {w : S w}

String of terminals
Courtesy Costas Busch - RPI 18

Derivation Order

1. S p AB

2. A p aaA 3. A p P

4. B p Bb 5. B p P
5

Leftmost derivation: 1 2 3 4

S AB aaAB aaB aaBb aab


Rightmost derivation: 1 4 5 2 3
19

S AB ABb Ab aaAb aab


Courtesy Costas Busch - RPI

S p aAB A p bBb B p A|P


Leftmost derivation:

S aAB abBbB abAbB abbBbbB abbbbB abbbb


Rightmost derivation:

S aAB aA abBb abAb abbBbb abbbb


Courtesy Costas Busch - RPI 20

Derivation Trees

Courtesy Costas Busch - RPI

21

S p AB

A p aaA | P

B p Bb | P

S AB
S A B

Courtesy Costas Busch - RPI

22

S p AB

A p aaA | P

B p Bb | P

S AB aaAB
S A
a a

B A

Courtesy Costas Busch - RPI

23

S p AB

A p aaA | P

B p Bb | P

S AB aaAB aaABb
S A
a a

B A B
b

Courtesy Costas Busch - RPI

24

S p AB

A p aaA | P

B p Bb | P

S AB aaAB aaABb aaBb


S A
a a

B A
P

Courtesy Costas Busch - RPI

25

S p AB

A p aaA | P

B p Bb | P

S AB aaAB aaABb aaBb aab


Derivation Tree A
a a

S B A
P

B
P

Courtesy Costas Busch - RPI

26

S p AB

A p aaA | P

B p Bb | P

S AB aaAB aaABb aaBb aab


Derivation Tree A
a a

S B A
P

yield

B
P

aaPPb ! aab

Courtesy Costas Busch - RPI

27

Partial Derivation Trees

S p AB

A p aaA | P

B p Bb | P

S AB
Partial derivation tree

A
Courtesy Costas Busch - RPI

B
28

S AB aaAB

Partial derivation tree

S B A
29

A
a a

Courtesy Costas Busch - RPI

S AB aaAB

sentential form

Partial derivation tree

S B A
yield

A
a a

aaAB
30

Courtesy Costas Busch - RPI

Sometimes, derivation order doesnt matter Leftmost:

S AB aaAB aaB aaBb aab


Rightmost:

S AB ABb Ab aaAb aab


S

Same derivation tree


A a a A P B P B
b

Courtesy Costas Busch - RPI

31

Ambiguity

Courtesy Costas Busch - RPI

32

E p E  E | E  E | (E) | a
a  aa
E E
a 

E E  E a E a EE a  a E a  a*a
E

leftmost derivation

E
a

E
a
Courtesy Costas Busch - RPI 33

E p E  E | E  E | (E) | a
a  aa
E EE E  EE a EE E a  aE a  aa
leftmost derivation

E E
a 

E
a

E
a

Courtesy Costas Busch - RPI

34

E p E  E | E  E | (E) | a
a  aa
Two derivation trees

E E
a 

E E


E E
a

E
a

E
a

E
a

E
a

Courtesy Costas Busch - RPI

35

The grammar

E p E  E | E  E | (E) | a is ambiguous:
string

a  a  a has two derivation trees


E E
E
 E


E

E E
a

a
36

Courtesy Costas Busch - RPI

The grammar

E p E  E | E  E | (E) | a is ambiguous:
string

a  a  a has two leftmost derivations

E E  E a E a EE a  a E a  a*a E EE E  EE a EE a  aE a  aa


Courtesy Costas Busch - RPI

37

Definition:
A context-free grammar if some string

G is ambiguous

w L(G ) has:

two or more derivation trees

Courtesy Costas Busch - RPI

38

In other words: A context-free grammar if some string

G is ambiguous

w L(G ) has:

two or more leftmost derivations (or rightmost)

Courtesy Costas Busch - RPI

39

Why do we care about ambiguity?

a  aa
E E
a 
take

a!2
E

E


E


E
a

E
a

E
a

E
a

E
a

Courtesy Costas Busch - RPI

40

2  22
E E 2


E E


E E 2 E 2


E 2
41

E 2

E 2

Courtesy Costas Busch - RPI

2  22 ! 6
6 E 2 E 2


2  22 ! 8
8 E

4 E


4 E 2 E 2 2 E 2


2 E 2
42

2 E 2

2 E 2

Courtesy Costas Busch - RPI

Correct result:

2  22 ! 6
6 E

2 E 2

4 E


2 E 2

2 E 2
43

Courtesy Costas Busch - RPI

Ambiguity is bad for programming languages

We want to remove ambiguity

Courtesy Costas Busch - RPI

44

We fix the ambiguous grammar:

E p E  E | E  E | (E) | a
New non-ambiguous grammar:

E p E T E pT T pT F T pF F p (E) F pa

Courtesy Costas Busch - RPI

45

E E T T T F T a T a T  F a  F F a  aF a  aa

a  aa
T T F
a
46

E p E T E pT T pT F T pF F p (E) F pa

E T F
a

F
a

Courtesy Costas Busch - RPI

Unique derivation tree

E E T F


a  aa
T T F


a
Courtesy Costas Busch - RPI 47

The grammar

G:

E p E T E pT T pT F T pF F p (E) F pa

is non-ambiguous: Every string w L(G ) has a unique derivation tree


Courtesy Costas Busch - RPI 48

Inherent Ambiguity
Some context free languages have only ambiguous grammars Example:

L ! {a b c } {a b c } S1 p S1c | A A p aAb | P
Courtesy Costas Busch - RPI

n n m

n m m

S p S1 | S 2

S 2 p aS2 | B B p bBc | P
49

The string

a b c

n n n

has two derivation trees

S S1 S1

S S2

S2

Courtesy Costas Busch - RPI

50

You might also like