0% found this document useful (0 votes)
27 views

Lecture 8

This document discusses context-free grammars and languages. It begins by introducing context-free grammars and their components: variables, terminals, start variable, and production rules. Examples of context-free grammars are provided along with derivations showing how strings are generated. Key concepts covered include sentential forms, derivation trees, languages of grammars, and ambiguous grammars.

Uploaded by

rahmatalam
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Lecture 8

This document discusses context-free grammars and languages. It begins by introducing context-free grammars and their components: variables, terminals, start variable, and production rules. Examples of context-free grammars are provided along with derivations showing how strings are generated. Key concepts covered include sentential forms, derivation trees, languages of grammars, and ambiguous grammars.

Uploaded by

rahmatalam
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 62

Context-Free Languages

1
Context-Free Languages

Context-Free Pushdown
Grammars Automata

stack

automaton

2
Context-Free Grammars

3
Grammars
Grammars express languages

Example: the English language

sentence  noun _ phrase predicate

noun _ phrase  article noun

predicate  verb
4
article  a
article  the

noun  cat
noun  dog

verb  runs
verb  walks

5
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

6
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

7
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” }
8
Notation

Production Rules

noun  cat
noun  dog

Variable Terminal

9
Another Example
Grammar: S  aSb
S 

Derivation of sentence ab :

S  aSb  ab

S  aSb S 
10
Grammar: S  aSb
S 

Derivation of sentence aabb :


S  aSb  aaSbb  aabb

S  aSb S 
11
Other derivations:

S  aSb  aaSbb  aaaSbbb  aaabbb

S  aSb  aaSbb  aaaSbbb


 aaaaSbbbb  aaaabbbb

12
Language of the grammar

S  aSb
S 

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

13
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


14
Example

Grammar G: S  aSb
S 

G   V ,T , S , P 

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

Example:

S  aSb  aaSbb  aaaSbbb  aaabbb

Sentential Forms sentence


16
*
We write: S  aaabbb

Instead of:

S  aSb  aaSbb  aaaSbbb  aaabbb

17
*
In general we write: w1  wn

If: w1  w2  w3    wn

18
*
By default: w  w

19
Example
Grammar Derivations
S  aSb *
S 
S 
*
S  ab
*
S  aabb
*
S  aaabbb
20
Example
Grammar Derivations
S  aSb 
S  aaSbb
S 

aaSbb  aaaaaSbbbbb

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

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


S  aaaabbbbb

S  aaaaaabbbbbbb

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

For a grammar G
with start variable S:


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

String of terminals
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
25
A Convenient Notation

A  aAb
A  aAb | 
A

article  a
article  a | the
article  the
26
Example

A context-free grammar G: S  aSb


S 

A derivation:

S  aSb  aaSbb  aabb


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

Another derivation:

S  aSb  aaSbb  aaaSbbb  aaabbb


28
S  aSb
S 

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

29
Example

A context-free grammar G: S  aSa


S  bSb
S 

A derivation:

S  aSa  abSba  abba


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

Another derivation:

S  aSa  abSba  abaSaba  abaaba


31
Example

A context-free grammar G: S  aSb


S  SS
S 

A derivation:

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

A derivation:

S  SS  aSbS  abS  abaSb  abab


33
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
34
G  (V , T , S , P)

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

35
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 )

36
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
37
S  aAB
A  bBb
B  A|
Leftmost derivation:
S  aAB  abBbB  abAbB  abbBbbB
 abbbbB  abbbb
Rightmost derivation:
S  aAB  aA  abBb  abAb
 abbBbb  abbbb
38
Derivation Trees

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

S  AB
S

A B

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

S  AB  aaAB
S

A B

a a A

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

S  AB  aaAB  aaABb
S

A B

a a A B b

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

S  AB  aaAB  aaABb  aaBb


S

A B

a a A B b


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

S  AB  aaAB  aaABb  aaBb  aab


Derivation Tree S

A B

a a A B b

 
44
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
 
45
Ambiguity

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


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

if some string w L(G ) has:

two or more derivation trees

52
In other words:

A context-free grammar G is ambiguous

if some string w L(G ) has:

two or more leftmost derivations


(or rightmost)

53
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
54
2  22

E E

E  E E  E

2 E  E E  E 2

2 2 2 2
55
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
56
Correct result: 2  22  6

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

2 2
57
• Ambiguity is bad for programming languages

• We want to remove ambiguity

58
Another Ambiguous Grammar

IF_STMT  if EXPR then STMT


| if EXPR then STMT else STMT

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


60
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 | 
61
n n n
The string a b c
has two derivation trees

S S

S1 S2

S1 c a S2

62

You might also like