lec26-dynamic-programming-7
lec26-dynamic-programming-7
> Previously...
> If you want to see more examples, my next two favorites are...
1. Optimal code generation (compilers)
2. System R query optimization (databases)
Outline for Today
> Grammars
> CKY Algorithm
> Earley’s Algorithm
> Leo Optimization
Grammars
> Example:
Natural Language Grammar
> Input is a list of parts of speech
– noun (N), verb (V), preposition (P), determiner (D), conjunction (C), etc.
N V N P N D N C D N
Rachael Ray finds inspiration in cooking her family and her dog
Natural Language Grammar
> Output is a tree showing structure
S
NP V NP
NP PP
P NP
NP NP NP
N N N D N C D N
Rachael Ray finds inspiration in cooking her family and her dog
Programming Language Grammar
N * N + N * N
3 * 4 + 5 * 6
Programming Language Grammar
* *
N N N N
3 * 4 + 5 * 6
Programming Language Grammar
F
F * N F * N
N N
3 * 4 + 5 * 6
Context Free Grammars
A ➞ B1 B2 ... Bk
F * N
3 * 4 * 5 * 6
Context Free Grammars
3 * 4 + 5 * 6
Context Free Grammars
> Called “context free” because the rule A ➞ B1 B2 ... Bk says that
A look like B1 B2 ... Bk anywhere
F➞F*N T ➞ T1 F F ➞ F1 N
F➞N T1 ➞ T P F1 ➞ F M
T➞T+F T➞F F➞N
T➞F
M➞* P➞+
Context Free Grammars
F➞F*N T ➞ T1 F F ➞ F1 N
F➞N T1 ➞ T P F1 ➞ F M
T➞T+F T➞F F➞N
T➞F
M➞* P➞+
Context Free Grammars
F➞F*N T ➞ T1 F F ➞ F1 N
F➞N T1 ➞ T P F1 ➞ F M
T➞T+F T1 ➞ F P F➞N
T➞F T ➞ F1 N
T➞N
M➞* P➞+
Outline for Today
> Grammars
> CKY Algorithm
> Earley’s Algorithm
> Leo Optimization
Parsing Context Free Grammars
> Think about what the parse tree for tokens 1 .. n might look like
– root corresponds to some rule A ➞ B1 B2 (Chomsky Normal Form)
– child B1 is root of parse tree for some 1 .. k
– child B2 is root of parse tree for k+1 .. n
– (or it could be a leaf A ➞ C, where C is a terminal, if n=1)
Parsing Context Free Grammars
> Try each of those possibilities (at most |G|) for each (i,j) pair
– each requires checking j – i + 1 possibilities for k
– need answers to sub-problem with j – i smaller
> can fill in the table along the diagonals, for example
T ➞ T1 F
T ➞ F1 N
Cocke-Kasami-Younger (CKY) T1 ➞ T P
T1 ➞ F P
> Example table from arithmetic example: F ➞ F1 N
3 * 4 + 5 * 6 F1 ➞ F M
3 F/T T➞N
* M F➞N
4 F/T M➞*
+ P P➞+
5 F/T
* M
6 F/T
T ➞ T1 F
T ➞ F1 N
Cocke-Kasami-Younger (CKY) T1 ➞ T P
T1 ➞ F P
> Example table from arithmetic example: F ➞ F1 N
3 * 4 + 5 * 6 F1 ➞ F M
3 F/T F1 T➞N
* M F➞N
4 F/T T1 M➞*
+ P P➞+
5 F/T F1
* M
6 F/T
T ➞ T1 F
T ➞ F1 N
Cocke-Kasami-Younger (CKY) T1 ➞ T P
T1 ➞ F P
> Example table from arithmetic example: F ➞ F1 N
3 * 4 + 5 * 6 F1 ➞ F M
3 F/T F1 F/T T➞N
* M F➞N
4 F/T T1 T M➞*
+ P P➞+
5 F/T F1 F/T
* M
6 F/T
T ➞ T1 F
T ➞ F1 N
Cocke-Kasami-Younger (CKY) T1 ➞ T P
T1 ➞ F P
> Example table from arithmetic example: F ➞ F1 N
3 * 4 + 5 * 6 F1 ➞ F M
3 F/T F1 F/T T1 T➞N
* M F➞N
4 F/T T1 T M➞*
+ P P➞+
5 F/T F1 F/T
* M
6 F/T
T ➞ T1 F
T ➞ F1 N
Cocke-Kasami-Younger (CKY) T1 ➞ T P
T1 ➞ F P
> Example table from arithmetic example: F ➞ F1 N
3 * 4 + 5 * 6 F1 ➞ F M
3 F/T F1 F/T T1 T T➞N
* M F➞N
4 F/T T1 T M➞*
+ P P➞+
5 F/T F1 F/T
* M
6 F/T
T ➞ T1 F
T ➞ F1 N
Cocke-Kasami-Younger (CKY) T1 ➞ T P
T1 ➞ F P
> Example table from arithmetic example: F ➞ F1 N
3 * 4 + 5 * 6 F1 ➞ F M
3 F/T F1 F/T T1 T T T➞N
* M F➞N
4 F/T T1 T M➞*
+ P P➞+
5 F/T F1 F/T
* M
6 F/T
T ➞ T1 F
T ➞ F1 N
Cocke-Kasami-Younger (CKY) T1 ➞ T P
T1 ➞ F P
> Can reconstruct the tree from the table as usual. F ➞ F1 N
3 * 4 + 5 * 6 F1 ➞ F M
3 F/T F1 F/T T1 T T T➞N
* M F➞N
4 F/T T1 T M➞*
+ P P➞+
5 F/T F1 F/T
* M
6 F/T
Cocke-Kasami-Younger (CKY)
> Grammars
> CKY Algorithm
> Earley’s Algorithm
> Leo Optimization
Improving CKY (out of scope)
> PLUS we know that certain grammars can be parsed much faster
> In particular, there exist O(n) algorithms for typical PL grammars
– O(n3) was out of the question in 1965...
* M
4 N/F/T
+ P
5 N/F/T F1 F/T
* M
6 N/F/T
Improving CKY
> If all |Ij|’s are size O(1), then this is O(1) time per item
– hence, O(n) over all
Earley’s algorithm
> Can also be shown it runs in O(n) time for nice LR(k) grammars
> BUT not for all LR(k) grammars
– latter can be parsed in O(n) time by other algorithms
> The running time is at least the sum of sizes of the Ij’s...
Outline for Today
> Grammars
> CKY Algorithm
> Earley’s Algorithm
> Leo Optimization
Bad Cases for Earley
B
> Q: Can the Ij’s be O(n) for some
unambiguous grammar’s? B
B
A➞a
B➞b B
B➞AB
A A A A A A B
B
> Q: Can the Ij’s be O(n) for some
unambiguous grammar’s? B
B
> This is a “right recursive grammar”
> Fortunately, these are the only B
bad cases (O(n) otherwise)
A A A A A A B
> Grammars can be usually be
rewritten to avoid it a a a a a a b
Joop Leo’s Optimization
> Can then show that the Ij’s are O(1) size
– number with dot not at end is O(1) due to LR(k) property
– clever argument shows number with dot at end is also O(1)
> removing stacks leaves tree with all 2+ children and leaves those above
> (furthermore, each is discovered only once for unambiguous grammars)
Joop Leo’s Optimization
> In PL, we typically use special grammars (e.g., LR(k)) that can be
parsed in linear time
– LR(k) was invented by Don Knuth
– parses anything that can be parsed by a deterministic push-down automaton