Uploaded by Dawit Terefe

8. Context Free Grammars 2

advertisement
Context-Free Languages
By Solomon Getachew
1
Context-Free Languages
n n
{a b : n  0}
R
{ww }
Regular Languages
a *b *
( a  b) *
2
Context-Free Languages
Context-Free
Grammars
Pushdown
Automata
stack
automaton
3
Context-Free Grammars
4
Grammars
Grammars express languages
Sequence of terminals and variables
Grammar:
S  aSb
S 
Variable
The right side
may be 
5
Grammar: S  aSb
S 
Derivation of string
ab :
S  aSb  ab
S  aSb
S 
6
Grammar: S  aSb
S 
Derivation of string
aabb :
S  aSb  aaSbb  aabb
S  aSb
S 
7
Grammar: S  aSb
S 
Other derivations:
S  aSb  aaSbb  aaaSbbb  aaabbb
S  aSb  aaSbb  aaaSbbb
 aaaaSbbbb  aaaabbbb
8
Grammar: S  aSb
S 
Language of the grammar:
L  {a b : n  0}
n n
9
A Convenient Notation
*
S  aaabbb
We write:
for zero or more derivation steps
Instead of:
S  aSb  aaSbb  aaaSbbb  aaabbb
10
In general we write:
If:
*
w1  wn
w1  w2  w3    wn
in zero or more derivation steps
11
Example Grammar
Possible Derivations
S  aSb
*
S 
S 
*
S  ab
*
S  aaabbb


S  aaSbb  aaaaaSbbbbb
12
Another convenient notation:
S  aSb
S 
S  aSb | 
13
Formal Definitions
Grammar: G  V , T , S , P 
Set of
variables
Set of
terminal
symbols
Start
variable
Set of
productions
14
Context-Free Grammar: G  (V , T , S , P)
All productions in P are of the form
A s
Variable
,where A ∈ V and
s ∈ (V ∪ T)*.
String of
variables and
terminals
A language L is said to be context-free if and only if
there is a context-free grammar G such that L = L (G).
15
Example of Context-Free Grammar
S  aSb | 
productions
P  {S  aSb, S   }
G  V ,T , S , P 
V  {S}
variables
T  {a, b}
terminals
start variable
16
Language of a Grammar:
For a grammar
G with start variable S
*
L(G )  {w : S  w, w  T *}
String of terminals or

17
Example:
context-free grammar
G : S  aSb | 
L(G)  {a b : n  0}
n n
Since, there is derivation

Sa b
n
n
for any
n0
18
Context-Free Language definition:
A language L is context-free
if there is a context-free grammar
with L  L(G )
G
19
Example:
L  {a b : n  0}
n n
is a context-free language
since context-free grammar G :
S  aSb | 
generates
L(G )  L
20
Another Example
Context-free grammar G :
S  aSa | bSb | 
Example derivations:
S  aSa  abSba  abba
S  aSa  abSba  abaSaba  abaaba
L(G )  {ww : w {a, b}*}
R
21
Another Example
Context-free grammar G :
S  aSb | SS | 
Example derivations:
S  SS  aSbS  abS  ab
S  SS  aSbS  abS  abaSb  abab
22
Derivation Order
and
Derivation Trees
23
Derivation Order
Consider the following example grammar
with 5 productions:
1. S  AB
2. A  aaA
4. B  Bb
3. A  
5. B  
24
1. S  AB
2. A  aaA
4. B  Bb
3. A  
5. B  
Leftmost derivation order of string aab :
1
2
3
4
5
S  AB  aaAB  aaB  aaBb  aab
At each step, we substitute the
leftmost variable
25
1. S  AB
2. A  aaA
4. B  Bb
3. A  
5. B  
Rightmost derivation order of string aab :
1
4
5
2
3
S  AB  ABb  Ab  aaAb  aab
At each step, we substitute the
rightmost variable
26
1. S  AB
2. A  aaA
4. B  Bb
3. A  
5. B  
Leftmost derivation of aab :
1
2
3
4
5
S  AB  aaAB  aaB  aaBb  aab
Rightmost derivation of aab :
1
4
5
2
3
S  AB  ABb  Ab  aaAb  aab
27
Derivation Trees
Consider the same example grammar:
S  AB
A  aaA | 
B  Bb | 
And a derivation of aab :
S  AB  aaAB  aaABb  aaBb  aab
28
A  aaA | 
S  AB
B  Bb | 
S  AB
S
A
B
yield AB
29
A  aaA | 
S  AB
B  Bb | 
S  AB  aaAB
S
a
A
B
a
yield aaAB
A
30
A  aaA | 
S  AB
B  Bb | 
S  AB  aaAB  aaABb
S
A
a
a
B
A
B
b
yield aaABb
31
A  aaA | 
S  AB
B  Bb | 
S  AB  aaAB  aaABb  aaBb
S
A
a
a
B
A

B
b
yield
aaBb  aaBb
32
A  aaA | 
S  AB
B  Bb | 
S  AB  aaAB  aaABb  aaBb  aab
Derivation Tree
S
(parse tree)
A
a
a
B
A

B
b

yield
aab  aab
33
Sometimes, derivation order doesn’t matter
Leftmost derivation:
S  AB  aaAB  aaB  aaBb  aab
Rightmost derivation:
S  AB  ABb  Ab  aaAb  aab
S
Give same
derivation tree
A
a
a
B
A
B


b
34
Ambiguity
35
Grammar for mathematical expressions
E  E  E | E  E | (E) | a
Example strings:
(a  a )  a  (a  a  (a  a ))
Denotes any number
36
E  E  E | E  E | (E) | a
E  E  E  a E  a EE
E
 a  a E  a  a*a
E

E
a
E

a
A leftmost derivation
for a  a  a
E
a
37
E  E  E | E  E | (E) | a
E  EE  E  EE  a EE
 a  aE  a  aa
Another
leftmost derivation
for a  a  a
E
a
E
E

E

E
a
a
38
E  E  E | E  E | (E) | a
E
Two derivation trees
for a  a  a
E

E
a
E

a
E
E
a
a
E
E

E

E
a
a
39
take
a2
a  a a  2  22
E
E
E

E
2
E

2
E
E
2
2
E

E

E
2
2
40
Good Tree
Bad Tree
2  22  6
6
E
2
E

2
2
E
2
2  22  8
Compute expression result 8
E
using the tree
4
E

2
E
2
E
2
2
4
E

2
E

2
E
2
2
41
Two different derivation trees
may cause problems in applications which
use the derivation trees:
• Evaluating expressions
• In general, in compilers
for programming languages
42
Ambiguous Grammar:
A context-free grammar G is ambiguous
if there is a string w L(G ) which has:
two different derivation trees
or
two leftmost derivations
(Two different derivation trees give two
different leftmost derivations and vice-versa)
43
Example:
E  E  E | E  E | (E) | a
this grammar is ambiguous since
string
a  a  a has two derivation trees
E
E
E

E
a
E

a
E
E
a
a
E

E

E
a
a
44
E  E  E | E  E | (E) | a
this grammar is ambiguous also because
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
45
Another ambiguous grammar:
STMT

|
if EXPR then STMT
if EXPR then STMT else STMT
Variables
Terminals
Very common piece of grammar
in programming languages
46
If expr1 then if expr2 then stmt1 else stmt2
STMT
if
expr1
then
if
expr2
STMT
then
expr1
if
stmt1
else
stmt2
Two derivation trees
STMT
if
this is the tree
used in languages
then
expr2
STMT
then
else
stmt2
stmt1
47
In general, ambiguity is bad
and we want to remove it
Sometimes it is possible to find
a non-ambiguous grammar for a language
But, in general ιt is difficult to achieve this
48
A successful example:
Ambiguous
Grammar
E E E
E  E E
E  (E )
E a
Equivalent
Non-Ambiguous
Grammar
E  E T |T
T T  F | F
F  (E ) | a
generates the same
language
49
E  E T T T  F T  a T  a T F
 a  F F  a  aF  a  aa
E
E  E T |T

T T F | F
E
F  (E) | a
T
T
F
F
a
a
Unique
derivation tree
for a  a  a
T

F
a
50
An un-successful example:
L  {a b c }  {a b c }
n n m
n m m
n, m  0
L
is inherently ambiguous:
every grammar that generates this
language is ambiguous
51
Example (ambiguous) grammar for
L:
L  {a b c }  {a b c }
n n m
S  S1 | S2
n m m
S1  S1c | A
S 2  aS 2 | B
A  aAb | 
B  bBc | 
52
The string a n b n c n  L
has always two different derivation trees
(for any grammar)
For example
S1
S
S
S1
S2
c
a
S2
53
Download