You are on page 1of 62

Chapter 4

Context-Free Grammars and Languages

Context-Free Languages

{a b : n 0}

n n

{ww }

Regular Languages

a *b *

( a + b) *

Context-Free Languages

{a b }

n n

{ww }

Regular Languages

Context-Free Languages

Context-Free Grammars

Pushdown Automata stack automaton


5

Context-Free Grammars

Grammars
Grammars express languages Example: the English language

sentence noun _ phrase noun _ phrase article predicate verb

predicate noun

article a article the noun cat noun dog verb runs verb walks
8

Example 1:
A derivation of the dog walks:

sentence noun _ phrase noun _ phrase article the noun noun verb

predicate verb verb

the dog verb the dog walks


9

Example 2:
A derivation of a cat runs:

sentence noun _ phrase noun _ phrase article a noun noun verb

predicate verb verb

a cat verb a cat runs


10

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 }
11

Notation
Production Rules

noun cat noun dog


Variable Terminal

12

Another Example
Grammar:

S aSb S
ab :

Derivation of sentence

S aSb ab S aSb

S
13

Grammar:

S aSb S aabb :

Derivation of sentence

S aSb aaSbb aabb


S aSb

S
14

Other derivations:

S aSb aaSbb aaaSbbb aaabbb

S aSb aaSbb aaaSbbb aaaaSbbbb aaaabbbb

15

Language of the grammar

S aSb S
L = {a b : n 0}
n n

16

More Notation
Grammar

G = (V , T , S , P )
Set of variables Set of terminal symbols Start variable Set of Production rules
17

V: T: S: P:

Example
Grammar

S aSb S

G = (V , T , S , P )
V = {S } T = {a, b}

P = {S aSb, S }
18

More Notation
Sentential Form: A sentence that contains variables and terminals Example:

S aSb aaSbb aaaSbbb aaabbb


Sentential Forms sentence
19

We write:

S aaabbb

Instead of:

S aSb aaSbb aaaSbbb aaabbb


20

In general we write:

w1 wn

If:

w1 w2 w3 wn

21

By default:

w w

22

Example
Grammar

S aSb S

Derivations *

S
* * *

S ab S aabb S aaabbb
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

A Convenient Notation

A aAb A

A aAb |

article a article the

article a | the
25

Definition: Context-Free Grammars


Grammar Variables

G = (V , T , S , P )
Terminal symbols Start variable

Productions of the form: Variable

A x

String of variables and terminals


26

Definition: Context-Free Languages


A language

is context-free

if and only if there is a context-free grammar with L = L(G )

27

Derivation Order

1. S AB

2. A aaA 3. A
2 3 4

4. B Bb 5. B
5

Leftmost derivation:

S AB aaAB aaB aaBb aab


Rightmost derivation:

S AB ABb Ab aaAb aab


28

S aAB A bBb B A|
Leftmost derivation:

S aAB abBbB abAbB abbBbbB abbbbB abbbb


Rightmost derivation:

S aAB aA abBb abAb abbBbb abbbb


29

Derivation Trees

30

S AB

A aaA |

B Bb |

S AB
S A B

31

S AB

A aaA |

B Bb |

S AB aaAB
S A a a A B

32

S AB

A aaA |

B Bb |

S AB aaAB aaABb
S A a a A B B
b

33

S AB

A aaA |

B Bb |

S AB aaAB aaABb aaBb


S A a a A B B
b

34

S AB

A aaA |

B Bb |

S AB aaAB aaABb aaBb aab


Derivation Tree A a a A B S B
b

35

S AB

A aaA |

B Bb |

S AB aaAB aaABb aaBb aab


Derivation Tree A a a A B S B
b
yield

aab = aab
36

Partial Derivation Trees

S AB

A aaA |

B Bb |

S AB
Partial derivation tree

S B
37

S AB aaAB
Partial derivation tree

S B A

A a a

38

S AB aaAB
Partial derivation tree

sentential form

S B A
yield

A a a

aaAB
39

Sometimes, derivation order doesnt matter

S AB aaAB aaB aaBb aab


Rightmost:

Leftmost:

S AB ABb Ab aaAb aab


Same derivation tree
A
a a
A B

B
b

40

Ambiguity

41

E 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
42

E 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

43

E 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

44

The grammar

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

a + a a has two derivation trees


E E
E

E
a

+
E

E
E
a

E
a

E
a

45

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+ EE a + a E a + a*a E EE E + EE a+ EE a + aE a + aa

46

Definition:
A context-free grammar if some string

is ambiguous

w L(G ) has:

two or more derivation trees

47

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

is ambiguous

w L(G ) has:

two or more leftmost derivations (or rightmost)

48

Why do we care about ambiguity?

a + aa
E E a
+
take

a=2
E

E a

E a

E a

E a

E a

49

2 + 22
E E 2
+

E E

E E 2 E 2
+

E 2

E 2

E 2

50

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

2 E 2

2 E 2

51

Correct result:

2 + 22 = 6
6 E

2 E 2

4 E

2 E 2

2 E 2
52

Ambiguity is bad for programming languages

We want to remove ambiguity

53

We fix the ambiguous grammar:

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

E E +T E T T T F T F F (E) F a

54

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
55

E E +T E T T T F T F F (E) F a

E T F a

F a

Unique derivation tree

E E T F a
+

a + aa
T T F a
56

F a

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


57

Another Ambiguous Grammar


IF_STMT

if EXPR then STMT

if EXPR then STMT else STMT

58

If expr1 then if expr2 then stmt1 else stmt2


IF_STMT if expr1 if then expr2 STMT then stmt1 else stmt2

IF_STMT if expr1 if then expr2 STMT then else stmt1


59

stmt2

APPLICATIONS OF CONTEXT-FREE GRAMMARS


60

CFGs and Programming Languages


Important uses of formal languages:
to define precisely a programming language. to construct an efficient translator for it.

RLs are used for simple patterns, while CFLs for more complicated aspects.

61

CFGs and Programming Languages S-grammars:


<if-statement> ::= if <expression> <then_clause> <else_clause> <then_clause> ::= then <statement> <else_clause> ::= else <statement>

62

You might also like