Context-Free Grammars & Languages

advertisement
Context-Free Languages
Prof. Busch - LSU
1
Context-Free Languages
n n
{a b : n  0}
R
{ww }
Regular Languages
a *b *
( a  b) *
Prof. Busch - LSU
2
Context-Free Languages
Context-Free
Grammars
Pushdown
Automata
stack
automaton
Prof. Busch - LSU
3
Context-Free Grammars
Prof. Busch - LSU
4
Grammars
Grammars express languages
Example:
the English language grammar
sentence  noun _ phrase
noun _ phrase  article
predicate
noun
predicate  verb
Prof. Busch - LSU
5
article  a
article  the
noun  cat
noun  dog
verb  runs
verb  sleeps
Prof. Busch - LSU
6
Derivation of string “the dog sleeps”:
sentence  noun _ phrase
predicate
 noun _ phrase
verb
 article
verb
noun
 the noun
verb
 the dog verb
 the dog sleeps
Prof. Busch - LSU
7
Derivation of string “a cat runs”:
sentence  noun _ phrase
predicate
 noun _ phrase
verb
 article
noun
verb
 a noun
verb
 a cat verb
 a cat runs
Prof. Busch - LSU
8
Language of the grammar:
L = { “a cat runs”,
“a cat sleeps”,
“the cat runs”,
“the cat sleeps”,
“a dog runs”,
“a dog sleeps”,
“the dog runs”,
“the dog sleeps” }
Prof. Busch - LSU
9
Productions
Sequence of
Terminals (symbols)
noun  cat
sentence  noun _ phrase
Variables
predicate
Sequence of Variables
Prof. Busch - LSU
10
Another Example
Sequence of
terminals and variables
Grammar:
S  aSb
S 
Variable
The right side
may be 
Prof. Busch - LSU
11
Grammar:
S  aSb
S 
Derivation of string
ab :
S  aSb  ab
S  aSb
S 
Prof. Busch - LSU
12
Grammar:
S  aSb
S 
Derivation of string
aabb :
S  aSb  aaSbb  aabb
S 
S  aSb
Prof. Busch - LSU
13
Grammar:
S  aSb
S 
Other derivations:
S  aSb  aaSbb  aaaSbbb  aaabbb
S  aSb  aaSbb  aaaSbbb
 aaaaSbbbb  aaaabbbb
Prof. Busch - LSU
14
Grammar:
S  aSb
S 
Language of the grammar:
L  {a b : n  0}
n n
Prof. Busch - LSU
15
A Convenient Notation
*
S  aaabbb
We write:
for zero or more derivation steps
Instead of:
S  aSb  aaSbb  aaaSbbb  aaabbb
Prof. Busch - LSU
16
In general we write:
If:
*
w1  wn
w1  w2  w3    wn
in zero or more derivation steps
Trivially:
*
w  w
Prof. Busch - LSU
17
Example Grammar
Possible Derivations
*
S  aSb
S 
S 
*
S  ab
*
S  aaabbb


S  aaSbb  aaaaaSbbbbb
Prof. Busch - LSU
18
Another convenient notation:
S  aSb
S  aSb | 
S 
article  a
article  a | the
article  the
Prof. Busch - LSU
19
Formal Definitions
Grammar: G  V , T , S , P 
Set of
variables
Set of
terminal
symbols
Start
variable
Prof. Busch - LSU
Set of
productions
20
Context-Free Grammar: G  (V , T , S , P)
All productions in P are of the form
A s
Variable
String of
variables and
terminals
Prof. Busch - LSU
21
Example of Context-Free Grammar
S  aSb | 
productions
P  {S  aSb, S  }
G  V ,T , S , P 
V  {S}
variables
T  {a, b}
terminals
Prof. Busch - LSU
start variable
22
Language of a Grammar:
For a grammar
G with start variable S
*
L(G )  {w : S  w, w  T *}
String of terminals or
Prof. Busch - LSU

23
Example:
context-free grammar
G : S  aSb | 
L(G )  {a b : n  0}
n
n
Since, there is derivation

S a b
n
n
Prof. Busch - LSU
for any
n 0
24
Context-Free Language definition:
A language L is context-free
if there is a context-free grammar
with L  L(G )
Prof. Busch - LSU
G
25
Example:
L  {a b : n  0 }
n
n
is a context-free language
since context-free grammar G :
S  aSb | 
generates
L(G )  L
Prof. Busch - LSU
26
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
Palindromes of even length
Prof. Busch - LSU
27
Another Example
Context-free grammar G :
S  aSb | SS | 
Example derivations:
S  SS  aSbS  abS  ab
S  SS  aSbS  abS  abaSb  abab
L(G )  {w : na ( w)  nb ( w),
Describes
matched
parentheses:
and na (v)  nb (v)
in any prefix v}
() ((( ))) (( ))
Prof. Busch - LSU
a  (,
b )
28
Derivation Order
and
Derivation Trees
Prof. Busch - LSU
29
Derivation Order
Consider the following example grammar
with 5 productions:
1. S  AB
2. A  aaA
4. B  Bb
3. A  
5. B  
Prof. Busch - LSU
30
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
Prof. Busch - LSU
31
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
Prof. Busch - LSU
32
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
Prof. Busch - LSU
33
Derivation Trees
Consider the same example grammar:
S  AB
A  aaA | 
B  Bb | 
And a derivation of aab :
S  AB  aaAB  aaABb  aaBb  aab
Prof. Busch - LSU
34
A  aaA | 
S  AB
B  Bb | 
S  AB
S
A
B
yield AB
Prof. Busch - LSU
35
A  aaA | 
S  AB
B  Bb | 
S  AB  aaAB
S
a
A
B
a
yield aaAB
A
Prof. Busch - LSU
36
A  aaA | 
S  AB
B  Bb | 
S  AB  aaAB  aaABb
S
A
a
a
B
A
B
b
yield aaABb
Prof. Busch - LSU
37
A  aaA | 
S  AB
B  Bb | 
S  AB  aaAB  aaABb  aaBb
S
A
a
a
B
A
B

yield
aaBb  aaBb
Prof. Busch - LSU
b
38
A  aaA | 
S  AB
B  Bb | 
S  AB  aaAB  aaABb  aaBb  aab
Derivation Tree
S
(parse tree)
A
a
a
B
A

Prof. Busch - LSU
B
b

yield
aab  aab
39
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
Prof. Busch - LSU
B
A
B


b
40
Ambiguity
Prof. Busch - LSU
41
Grammar for mathematical expressions
E  E  E | E  E | (E) | a
Example strings:
(a  a )  a  (a  a  (a  a ))
Denotes any number
Prof. Busch - LSU
42
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
Prof. Busch - LSU
43
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
Prof. Busch - LSU
E
E

E

E
a
a
44
E  E  E | E  E | (E) | a
E
Two derivation trees
for a  a  a
E

E
a
E

a
E
E
a
a
Prof. Busch - LSU
E
E

E

E
a
a
45
take
a2
a  a a  2  22
E
E
E

E
2
E

2
E
E
2
2
Prof. Busch - LSU
E

E

E
2
2
46
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
Prof. Busch - LSU
4
E

2
E

2
E
2
2
47
Two different derivation trees
may cause problems in applications which
use the derivation trees:
• Evaluating expressions
• In general, in compilers
for programming languages
Prof. Busch - LSU
48
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)
Prof. Busch - LSU
49
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
Prof. Busch - LSU
E

E

E
a
a
50
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
Prof. Busch - LSU
51
Another ambiguous grammar:
IF_STMT

|
if EXPR then STMT
if EXPR then STMT else STMT
Variables
Terminals
Very common piece of grammar
in programming languages
Prof. Busch - LSU
52
If expr1 then if expr2 then stmt1 else stmt2
IF_STMT
if
expr1
if
then
expr2
STMT
then
expr1
if
then
expr2
else
stmt2
Two derivation trees
IF_STMT
if
stmt1
STMT
then
else
stmt2
stmt1
Prof. Busch - LSU
53
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
Prof. Busch - LSU
54
A successful example:
Equivalent
Ambiguous
Grammar
Non-Ambiguous
Grammar
E E E
E  E E
E  (E )
E  E T |T
T T  F | F
F  (E ) | a
E a
generates the same
language
Prof. Busch - LSU
55
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
Prof. Busch - LSU
T

F
a
56
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
Prof. Busch - LSU
57
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  aS2 | B
A  aAb | 
B  bBc | 
Prof. Busch - LSU
58
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
Prof. Busch - LSU
S2
59
Download