You are on page 1of 76

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
Example

A context-free grammar G: S  aSb


S 

A derivation:

S  aSb  aaSbb  aabb


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

Another derivation:

S  aSb  aaSbb  aaaSbbb  aaabbb


7
S  aSb
S 

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

Describes parentheses: (((( ))))


8
Example

A context-free grammar G: S  aSa


S  bSb
S 

A derivation:

S  aSa  abSba  abba


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

Another derivation:

S  aSa  abSba  abaSaba  abaaba


10
S  aSa
S  bSb
S 

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

11
Example

A context-free grammar G: S  aSb


S  SS
S 

A derivation:

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

A derivation:

S  SS  aSbS  abS  abaSb  abab


13
S  aSb
S  SS
S 

L(G )  {w : na ( w)  nb ( w),
and na (v)  nb (v)
in any prefix v}
Describes
matched
parentheses: () ((( ))) (( ))
14
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
15
G  (V , T , S , P)

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

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

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

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

S  AB
S

A B

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

S  AB  aaAB
S

A B

a a A

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

S  AB  aaAB  aaABb
S

A B

a a A B b

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

S  AB  aaAB  aaABb  aaBb


S

A B

a a A B b


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

S  AB  aaAB  aaABb  aaBb  aab


Derivation Tree S

A B

a a A B b

 
25
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
 
26
Partial Derivation Trees
S  AB A  aaA |  B  Bb | 

S  AB
Partial derivation tree S

A B

27
S  AB  aaAB

Partial derivation tree S

A B

a a A

28
sentential
S  AB  aaAB
form

Partial derivation tree S

A B

yield
a a A
aaAB
29
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

  30
Ambiguity

31
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
32
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
33
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
34
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
35
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 36
Definition:
A context-free grammar G is ambiguous

if some string w L(G ) has:

two or more derivation trees

37
In other words:

A context-free grammar G is ambiguous

if some string w L(G ) has:

two or more leftmost derivations


(or rightmost)

38
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
39
2  22

E E

E  E E  E

2 E  E E  E 2

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

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

2 2
42
• Ambiguity is bad for programming languages

• We want to remove ambiguity

43
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 44
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
E  T
E T
T T F T T  F
T F
F F a
F  (E)
F a a a
45
Unique derivation tree

E a  aa
E  T

T T  F

F F a

a a
46
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
47
Another Ambiguous Grammar

IF_STMT  if EXPR then STMT


| if EXPR then STMT else STMT

48
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


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

S S

S1 S2

S1 c a S2

51
Compilers

52
Machine Code
Program Add v,v,0
v = 5; cmp v,5
if (v>5) jmplt ELSE
x = 12 + v; THEN:
while (x !=3) { Compiler
add x, 12,v
x = x - 3; ELSE:
v = 10; WHILE:
} cmp x,3
...... ...
53
Compiler

Lexical
parser
analyzer

input output

machine
program
code
54
A parser knows the grammar
of the programming language

55
Parser
PROGRAM  STMT_LIST
STMT_LIST  STMT; STMT_LIST | STMT;
STMT  EXPR | IF_STMT | WHILE_STMT
| { STMT_LIST }

EXPR  EXPR + EXPR | EXPR - EXPR | ID


IF_STMT  if (EXPR) then STMT
| if (EXPR) then STMT else STMT
WHILE_STMT while (EXPR) do STMT

56
The parser finds the derivation
of a particular input

derivation
Parser
input E => E + E
E -> E + E
=> E + E * E
10 + 2 * 5 |E*E
=> 10 + E*E
| INT
=> 10 + 2 * E
=> 10 + 2 * 5

57
derivation tree
derivation
E

E => E + E E + E
=> E + E * E
=> 10 + E*E 10
E * E
=> 10 + 2 * E
=> 10 + 2 * 5 2 5

58
derivation tree

E machine code

E + E mult a, 2, 5
add b, 10, a
10
E * E

2 5

59
Parsing

60
Parser
input
grammar derivation
string

61
Example:

Parser
S  SS derivation
input
S  aSb
aabb ?
S  bSa
S 

62
Exhaustive Search

S  SS | aSb | bSa | 

Phase 1: S  SS Find derivation of


S  aSb aabb
S  bSa
S 
All possible derivations of length 1
63
S  SS aabb
S  aSb
S  bSa
S 

64
Phase 2 S  SS | aSb | bSa | 
S  SS  SSS
S  SS  aSbS aabb
Phase 1 S  SS  bSaS
S  SS S  SS  S
S  aSb S  aSb  aSSb
S  aSb  aaSbb
S  aSb  abSab
S  aSb  ab 65
S  SS | aSb | bSa | 
Phase 2
S  SS  SSS
S  SS  aSbS aabb
S  SS  S

S  aSb  aSSb
S  aSb  aaSbb
Phase 3
S  aSb  aaSbb  aabb
66
Final result of exhaustive search
(top-down parsing)
Parser
S  SS
input
S  aSb
aabb
S  bSa
S 
derivation

S  aSb  aaSbb  aabb


67
Time complexity of exhaustive search

Suppose there are no productions of the form

A
A B
Number of phases for string w: 2| w|

68
For grammar with k rules

Time for phase 1: k

k possible derivations

69
Time for phase 2: 2
k

2 possible derivations
k

70
Time for phase 2 | w |: k 2|w|

2|w|
k possible derivations

71
Total time needed for string w:

2 2|w|
k  k  k

phase 1 phase 2 phase 2|w|

Extremely bad!!!
72
There exist faster algorithms
for specialized grammars

S-grammar: A  ax

symbol string
of variables

Pair ( A, a ) appears once


73
S-grammar example:

S  aS
S  bSS
S c

Each string has a unique derivation

S  aS  abSS  abcS  abcc


74
For S-grammars:

In the exhaustive search parsing


there is only one choice in each phase

Time for a phase: 1

Total time for parsing string w: | w|

75
For general context-free grammars:

There exists a parsing algorithm


that parses a string | w |
3
in time | w |

(we will show it in the next class)

76

You might also like