Professional Documents
Culture Documents
1
Context-Free Languages
n n R
{a b : n 0} {ww }
Regular Languages
a *b * ( a b) *
2
Context-Free Languages
Context-Free Pushdown
Grammars Automata
stack
automaton
3
Context-Free Grammars
4
Formal Definitions
Grammar: G V , T , S , P
Set of
variables
Set of Start Set of
terminal variable productions
symbols
5
Context-Free Grammar: G (V , T , S , P )
A s
Variable String of
variables and
terminals
6
Example of Context-Free Grammar
S aSb |
productions
P {S aSb, S }
G V , T , S , P
V {S }
variables T {a, b} start variable
terminals
7
Language of a Grammar:
String of terminals or
8
Example:
n n
L(G ) {a b : n 0}
10
Example:
n n
L {a b : n 0 }
is a context-free language
since context-free grammar G :
S aSb |
generates L(G ) L
11
Another Example
Context-free grammar G :
S aSa | bSb |
Example derivations:
S aSa abSba abba
S aSa abSba abaSaba abaaba
R
L(G ) {ww : w {a, b}*}
Palindromes of even length
12
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),
and na (v) nb (v)
Describes
in any prefix v}
matched
parentheses: () ((( ))) (( )) a (, b )
13
Derivation Order
and
Derivation Trees
14
Derivation Order
1. S AB 2. A aaA 4. B Bb
3. A 5. B
15
1. S AB 2. A aaA 4. B Bb
3. A 5. B
1 2 3 4 5
S AB aaAB aaB aaBb aab
1 4 5 2 3
S AB ABb Ab aaAb aab
At each step, we substitute the
rightmost variable
17
1. S AB 2. A aaA 4. B Bb
3. A 5. B
S AB A aaA | B Bb |
19
S AB A aaA | B Bb |
S AB
S
A B
yield AB
20
S AB A aaA | B Bb |
S AB aaAB
S
A B
yield aaAB
a a A
21
S AB A aaA | B Bb |
S AB aaAB aaABb
S
A B
a a A B b
yield aaABb
22
S AB A aaA | B Bb |
S AB aaAB aaABb aaBb
S
A B
a a A B b
yield
aaBb aaBb
23
S AB A aaA | B Bb |
S AB aaAB aaABb aaBb aab
Derivation Tree S
(parse tree)
A B
a a A B b
yield
aab aab
24
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 A B
derivation tree
a a A B b
25
Ambiguity
26
Grammar for mathematical expressions
E E E | E E | (E) | a
Example strings:
(a a ) a (a a (a a ))
27
E E E | E E | (E) | a
E E E a E a EE
E
a a E a a*a
E E
A leftmost derivation
for a a a
a E E
a a
28
E E E | E E | (E) | a
a a
29
E E E | E E | (E) | a
E E E E
a E E E E a
a a a a
30
take a2
a a a 2 22
E E
E E E E
2 E E E E 2
2 2 2 2
31
Good Tree Bad Tree
2 22 6 2 22 8
6 Compute expression result 8
E using the tree E
2 4 4 2
E E E E
2 2 2 2
2 E E E E 2
2 2 2 2
32
Two different derivation trees
may cause problems in applications which
use the derivation trees:
• Evaluating expressions
• In general, in compilers
for programming languages
33
Ambiguous Grammar:
A context-free grammar G is ambiguous
if there is a string w L(G ) which has:
E E E E
a E E E E a
a a a a
35
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 EE
a a E a a*a
Variables Terminals
39
A successful example:
Equivalent
Ambiguous
Non-Ambiguous
Grammar
Grammar
E E E
E E T |T
E E E
T T F | F
E (E )
E a F (E ) | a
generates the same
language
40
E E T T T F T a T a T F
a F F a aF a aa
E
E E T |T
E T
T T F | F
F (E) | a T T F
F F a
Unique
derivation tree
for a a a a a
41