Professional Documents
Culture Documents
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 derivation:
Another derivation:
n n
L(G ) {a b : n 0}
A derivation:
Another derivation:
R
L(G ) {ww : w {a, b}*}
11
Example
A derivation:
S SS aSbS abS ab
12
A context-free grammar G: S aSb
S SS
S
A derivation:
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)
*
L(G ) {w : S w, w T *}
16
Definition: Context-Free Languages
A language L is context-free
if and only if
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 |
A B
a a A B b
24
S AB A aaA | B Bb |
A B
a a A B b
25
S AB A aaA | B Bb |
A B
yield
a a A B b aab
aab
26
Partial Derivation Trees
S AB A aaA | B Bb |
S AB
Partial derivation tree S
A B
27
S AB aaAB
A B
a a A
28
sentential
S AB aaAB
form
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 aa
E E E E a E a EE
a a E a a*a
E E
leftmost derivation
a E E
a a
32
E E E | E E | (E) | a
a aa
E E a
a a
33
E E E | E E | (E) | a
a aa
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:
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:
E E E a E a EE
a a E a a*a
37
In other words:
38
Why do we care about ambiguity?
a aa
take a2
E E
E E E E
a E E E E a
a a a a
39
2 22
E E
E E E E
2 E E E E 2
2 2 2 2
40
2 22 6 2 22 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 22 6
6
E
2 4
E E
2 2
2 E E
2 2
42
• Ambiguity is bad for programming languages
43
We fix the ambiguous grammar:
E E E | E E | (E) | a
E a aa
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
48
If expr1 then if expr2 then stmt1 else stmt2
IF_STMT
IF_STMT
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 }
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 |
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
A
A B
Number of phases for string w: 2| w|
68
For grammar with k rules
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
Extremely bad!!!
72
There exist faster algorithms
for specialized grammars
S-grammar: A ax
symbol string
of variables
S aS
S bSS
S c
75
For general context-free grammars:
76