SLIDE 1 Parsing of Context-Free Grammars
Bernd Kiefer
{Bernd.Kiefer}@dfki.de
Deutsches Forschungszentrum f¨ ur k¨ unstliche Intelligenz
Introduction to CL - Context-Free Grammars – p.1/32
SLIDE 2
Limits of Regular Expressions
Assignment: Write a regular expression for fully bracketed arithmetic expressions! Answer: ?
Introduction to CL - Context-Free Grammars – p.2/32
SLIDE 3
Limits of Regular Expressions
Assignment: Write a regular expression for fully bracketed arithmetic expressions! Answer: This is not possible! Regular expressions can only count finite amounts of brackets
Introduction to CL - Context-Free Grammars – p.2/32
SLIDE 4
Limits of Regular Expressions
Assignment: Write a regular expression for fully bracketed arithmetic expressions! Answer: This is not possible! Regular expressions can only count finite amounts of brackets We need a more powerful formal device: context-free grammars
Introduction to CL - Context-Free Grammars – p.2/32
SLIDE 5
Limits of Regular Expressions
Assignment: Write a regular expression for fully bracketed arithmetic expressions! Answer: This is not possible! Regular expressions can only count finite amounts of brackets We need a more powerful formal device: context-free grammars Context-free grammars provide a (finite) inventory of named brackets All regular languages are also context-free, i.e.: for every regular expression, there is a context-free grammar that accepts / derives the same language
Introduction to CL - Context-Free Grammars – p.2/32
SLIDE 6
Context-Free Grammars
A context-free grammar (CFG) consists of: The set of terminal symbols Σ = a, b, c, . . . (the words or letters of the language) The set of non-terminal symbols N = A, B, C, . . . The startsymbol S ∈ N The set of productions (rules) P, where
P ∋ r = A → α with α ∈ (Σ ∪ N)∗
(we use greek letters for strings of Σ ∪ N) Example: A grammar for arithmetic expressions:
Σ = { int, +, *, (, ) } , N = {E} , S = E P = { E → E+E, E → E *E, E → (E), E → int }
Introduction to CL - Context-Free Grammars – p.3/32
SLIDE 7
The Language of a CFG
Given a CFG G, the language L(G) is defined as the set of all strings that can be derived from S Given a string α from (Σ ∪ N)∗, derive a new string β: Choose one of the nonterminals in α, say, A Choose one of the productions with A on the left hand side Replace A in α with the right hand side (rhs) of the production to get the derived string β If α contains only symbols in Σ, then α ∈ L(G) Example:
α = int*(E); choose E → E +E; β = int*(E+E)
Introduction to CL - Context-Free Grammars – p.4/32
SLIDE 8
Derivations: Formally
A string α derives a string β, (α ⇒
G β) α, β ∈ (Σ ∪ N)∗, if:
there are γ, δ, η ∈ (Σ ∪ N)∗, A ∈ N such that
α = γ A δ , β = γ η δ
and
A − → η ∈ P
We write α ⇒
G β for a one-step derivation
α ∗ ⇒
G β is a many-step derivation: α ⇒ G α0 ⇒ G α1 . . . ⇒ G β
Language L(G) generated by G: L(G) = {s ∈ Σ∗|S
∗
⇒
G s}
The task of a parser: find one (or all) derivation(s) of a string in Σ∗, given a CFG G
Introduction to CL - Context-Free Grammars – p.5/32
SLIDE 9 Derivations: Example
Σ = {john, girl, car, saw, walks, in, the, a} N = {S, NP , VP , PP , D, N, V, P} P = S →NP VP|N VP|N V|NP V N→john, girl, car VP→V NP|V N|VP PP V→saw, walks NP→D N|NP PP|N PP P→in PP→P NP|P N D→the, a S ⇒
G
john saw the girl in a car
Introduction to CL - Context-Free Grammars – p.6/32
SLIDE 10 Derivations: Example
Σ = {john, girl, car, saw, walks, in, the, a} N = {S, NP , VP , PP , D, N, V, P} P = S →NP VP|N VP|N V|NP V N→john, girl, car VP→V NP|V N|VP PP V→saw, walks NP→D N|NP PP|N PP P→in PP→P NP|P N D→the, a S ⇒
G N VP ⇒ G
john saw the girl in a car
Introduction to CL - Context-Free Grammars – p.6/32
SLIDE 11 Derivations: Example
Σ = {john, girl, car, saw, walks, in, the, a} N = {S, NP , VP , PP , D, N, V, P} P = S →NP VP|N VP|N V|NP V N→john, girl, car VP→V NP|V N|VP PP V→saw, walks NP→D N|NP PP|N PP P→in PP→P NP|P N D→the, a S ⇒
G N VP ⇒ G
john VP ⇒
G
john saw the girl in a car
Introduction to CL - Context-Free Grammars – p.6/32
SLIDE 12 Derivations: Example
Σ = {john, girl, car, saw, walks, in, the, a} N = {S, NP , VP , PP , D, N, V, P} P = S →NP VP|N VP|N V|NP V N→john, girl, car VP→V NP|V N|VP PP V→saw, walks NP→D N|NP PP|N PP P→in PP→P NP|P N D→the, a S ⇒
G N VP ⇒ G
john VP ⇒
G
john V NP ⇒
G
john saw the girl in a car
Introduction to CL - Context-Free Grammars – p.6/32
SLIDE 13 Derivations: Example
Σ = {john, girl, car, saw, walks, in, the, a} N = {S, NP , VP , PP , D, N, V, P} P = S →NP VP|N VP|N V|NP V N→john, girl, car VP→V NP|V N|VP PP V→saw, walks NP→D N|NP PP|N PP P→in PP→P NP|P N D→the, a S ⇒
G N VP ⇒ G
john VP ⇒
G
john V NP ⇒
G
john saw NP ⇒
G
john saw the girl in a car
Introduction to CL - Context-Free Grammars – p.6/32
SLIDE 14 Derivations: Example
Σ = {john, girl, car, saw, walks, in, the, a} N = {S, NP , VP , PP , D, N, V, P} P = S →NP VP|N VP|N V|NP V N→john, girl, car VP→V NP|V N|VP PP V→saw, walks NP→D N|NP PP|N PP P→in PP→P NP|P N D→the, a S ⇒
G N VP ⇒ G
john VP ⇒
G
john V NP ⇒
G
john saw NP ⇒
G
john saw NP PP ⇒
G
john saw the girl in a car
Introduction to CL - Context-Free Grammars – p.6/32
SLIDE 15 Derivations: Example
Σ = {john, girl, car, saw, walks, in, the, a} N = {S, NP , VP , PP , D, N, V, P} P = S →NP VP|N VP|N V|NP V N→john, girl, car VP→V NP|V N|VP PP V→saw, walks NP→D N|NP PP|N PP P→in PP→P NP|P N D→the, a S ⇒
G N VP ⇒ G
john VP ⇒
G
john V NP ⇒
G
john saw NP ⇒
G
john saw NP PP ⇒
G
john saw D N PP ⇒
G john saw the N PP ⇒ G john saw the girl
PP ⇒
G john saw the girl P NP ⇒ G john saw the girl in NP ⇒ G john saw the girl in D
N ⇒
G john saw the girl in a N ⇒ G
john saw the girl in a car
Introduction to CL - Context-Free Grammars – p.6/32
SLIDE 16
Derivation (Parse) Trees
S NP N
john
VP V
saw
NP NP D
the
N
girl
PP P
in
NP D
a
N
car
S NP N
john
VP VP V
saw
NP D
the
N
girl
PP P
in
NP D
a
N
car
Encodes many possible derivations PP node in the example can be attached to two nodes: the grammar is ambigous CF Parsers/Recognizers differ in the way the derivation trees are build
Introduction to CL - Context-Free Grammars – p.7/32
SLIDE 17
Context-free Recognition
Task: given s ∈ Σ∗ and G, is s ∈ L(G) ? Two ways to go: start with the start symbol S and try to derive s by systematic application of the productions: top down recognition (goal driven) start with the string s and try to reduce it to the start symbol: bottom up recognition (data driven)
Introduction to CL - Context-Free Grammars – p.8/32
SLIDE 18
Recursive Descent Parsing
Idea: Recursively compute all expansions of a nonterminal at some input position
E → S+S, E → S *S, S → (E), S → int
( int + ( int * int ) )
S
expand(S, 0)
Introduction to CL - Context-Free Grammars – p.9/32
SLIDE 19
Recursive Descent Parsing
Idea: Recursively compute all expansions of a nonterminal at some input position
E → S+S, E → S *S, S → (E), S → int
( int + ( int * int ) )
S
(
E
) expand(S, 0)
Introduction to CL - Context-Free Grammars – p.9/32
SLIDE 20
Recursive Descent Parsing
Idea: Recursively compute all expansions of a nonterminal at some input position
E → S+S, E → S *S, S → (E), S → int
( int + ( int * int ) )
S
(
E
)
S
*
S
expand(E, 1)
Introduction to CL - Context-Free Grammars – p.9/32
SLIDE 21
Recursive Descent Parsing
Idea: Recursively compute all expansions of a nonterminal at some input position
E → S+S, E → S *S, S → (E), S → int
( int + ( int * int ) )
S
(
E
)
S
*
S
int expand(S, 1)
Introduction to CL - Context-Free Grammars – p.9/32
SLIDE 22
Recursive Descent Parsing
Idea: Recursively compute all expansions of a nonterminal at some input position
E → S+S, E → S *S, S → (E), S → int
( int + ( int * int ) )
S
(
E
)
S
*
S
int
S
+
S
expand(E, 1)
Introduction to CL - Context-Free Grammars – p.9/32
SLIDE 23
Recursive Descent Parsing
Idea: Recursively compute all expansions of a nonterminal at some input position
E → S+S, E → S *S, S → (E), S → int
( int + ( int * int ) )
S
(
E
)
S
*
S
int
S
+
S
int expand(S, 1)
Introduction to CL - Context-Free Grammars – p.9/32
SLIDE 24
Recursive Descent Parsing
Idea: Recursively compute all expansions of a nonterminal at some input position
E → S+S, E → S *S, S → (E), S → int
( int + ( int * int ) )
S
(
E
)
S
*
S
int
S
+
S
int expand(S, 1)
Introduction to CL - Context-Free Grammars – p.9/32
SLIDE 25
Recursive Descent Parsing
Idea: Recursively compute all expansions of a nonterminal at some input position
E → S+S, E → S *S, S → (E), S → int
( int + ( int * int ) )
S
(
E
)
S
*
S
int
S
+
S
int (
E
) expand(S, 3)
Introduction to CL - Context-Free Grammars – p.9/32
SLIDE 26
Recursive Descent Parsing
Idea: Recursively compute all expansions of a nonterminal at some input position
E → S+S, E → S *S, S → (E), S → int
( int + ( int * int ) )
S
(
E
)
S
*
S
int
S
+
S
int (
E
)
S
*
S
expand(E, 4)
Introduction to CL - Context-Free Grammars – p.9/32
SLIDE 27
Recursive Descent Parsing
Idea: Recursively compute all expansions of a nonterminal at some input position
E → S+S, E → S *S, S → (E), S → int
( int + ( int * int ) )
S
(
E
)
S
*
S
int
S
+
S
int (
E
)
S
*
S
int expand(S, 4)
Introduction to CL - Context-Free Grammars – p.9/32
SLIDE 28
Recursive Descent Parsing
Idea: Recursively compute all expansions of a nonterminal at some input position
E → S+S, E → S *S, S → (E), S → int
( int + ( int * int ) )
S
(
E
)
S
*
S
int
S
+
S
int (
E
)
S
*
S
int ( E ) expand(S, 6)
Introduction to CL - Context-Free Grammars – p.9/32
SLIDE 29
Recursive Descent Parsing
Idea: Recursively compute all expansions of a nonterminal at some input position
E → S+S, E → S *S, S → (E), S → int
( int + ( int * int ) )
S
(
E
)
S
*
S
int
S
+
S
int (
E
)
S
*
S
int ( E ) int expand(S, 6)
Introduction to CL - Context-Free Grammars – p.9/32
SLIDE 30
Recursive Descent Parsing
Idea: Recursively compute all expansions of a nonterminal at some input position
E → S+S, E → S *S, S → (E), S → int
( int + ( int * int ) )
S
(
E
)
S
*
S
int
S
+
S
int (
E
)
S
*
S
int ( E ) int expand(S, 3)
Introduction to CL - Context-Free Grammars – p.9/32
SLIDE 31
Recursive Descent Parsing
Idea: Recursively compute all expansions of a nonterminal at some input position
E → S+S, E → S *S, S → (E), S → int
( int + ( int * int ) )
S
(
E
)
S
*
S
int
S
+
S
int (
E
)
S
*
S
int ( E ) int expand(S, 0)
Introduction to CL - Context-Free Grammars – p.9/32
SLIDE 32
Recursive Descent Parsing II
expand returns a set of possible end positions During expansion of one production Keep track of a set of intermediate positions for the already expanded part Expand the next terminal or nonterminal from every intermediate position If the set of possible intermediate position gets empty, the production fails When a production was successfully completed, the intermediate set is added to the set of end positions May run into an infinite loop with left-recursive grammars, i.e. grammars where A ∗
⇒
G Aβ for some A
Introduction to CL - Context-Free Grammars – p.10/32
SLIDE 33 Top Down Parsing: Optimizations
For every nonterminal, compute the set of terminals that can occur in the first position Expand only those nonterminals / productions that are compatible with the current input symbol Avoid performing duplicate calls with identical nonterminal/position pair: memoize previous calls use a data structure whose index are tuples consisting
the result of the lookup is the result of a previous call with the same arguments (if available) The memoized method has to be strictly functional for this to work
Introduction to CL - Context-Free Grammars – p.11/32
SLIDE 34
Epsilon Reduction
A CFG may contain productions of the form A → ǫ Construct a CFG G′ with the same language as G and at most one epsilon production: S → ǫ
for all nonterminals A with A → ǫ ∈ P: mark A as ǫ-deriving and add it to the set Q while Q is not empty, remove a nonterminal X from Q: for all Y → α X β ∈ P, with α or β not empty, add Y → α β to P ′ for all Y → X, if Y is not marked as ǫ-deriving: mark Y as ǫ-deriving and add it to Q if S is ǫ-deriving, add S → ǫ to P ′ add all non-ǫ productions of P to P ′
Introduction to CL - Context-Free Grammars – p.12/32
SLIDE 35 Chomsky Normal Form
A context-free grammar is in Chomsky Normal Form if:
(i) it is ǫ-free, (ii) all productions have one of two forms:
- A → a with a ∈ Σ
- A → B C with B, C ∈ N
Every CFG can be transformed into a CNF grammar with the same language Drawback: The original structure of the parse trees must be reconstructed, if necessary The original and transformed grammar are said to be weakly equivalent
Introduction to CL - Context-Free Grammars – p.13/32
SLIDE 36
Chomsky Normal Form II
Convert an arbitrary CFG into CNF: Introduce new nonterminals and productions Aa → a Replace all occurences of a by Aa Eliminate unary productions A → B: add productions where A is replaced by B in the right hand sides Replace productions with more than two symbols on the right hand side by a sequence of productions:
A → R1 R2 . . . Rn ⇒ A → R1A(1), A(1) → R2A(2), · · · A(n−2) → Rn−1Rn
Introduction to CL - Context-Free Grammars – p.14/32
SLIDE 37
Chart Parsing
First algorithm independently developed by Cocke, Younger and Kasami (late 60s) Given a string w of length n, use an n × n table to store subderivations (hence chart or tabular parsing) Works for all kinds of grammars: left/right recursive, ambiguous Storing subderivations avoids duplicate computation: an instance of dynamic programming polynomial space and time bounds, although an exponential number of parse trees may be encoded!
Introduction to CL - Context-Free Grammars – p.15/32
SLIDE 38
CYK Parsing
Input: G in Chomsky normal form, input string w1, . . . , wn Systematically explore all possible sub-derivations bottom-up Use an n × n array C such that If nonterminal A is stored in C(i, k): A ∗
⇒
G wi+1, . . . , wk
Maintain a second table B, such that if j ∈ B(i, k): ex.
A → B C ∈ P, A ∈ C(i, k), B ∈ C(i, j) and C ∈ C(j, k) B enables us to extract the parse trees
Implement C and B as three-dimensional boolean arrays of size n × n × |N| and n × n × n, respectively
Introduction to CL - Context-Free Grammars – p.16/32
SLIDE 39
CYK – The Original
For i = 1 to n For each Rj − → ai, set C(i − 1, i, j) = true For l = 2 to n
– Length of new constituent
For i = 0 to n − l
– Start of new constituent
For m = 1 to l − 1
– Length of first subconstituent
For each production Ra − → RbRc If C(i, i + m, b) and C(i + m, i + l, c) then set C(i, i + l, a) = true set B(i, i + l, i + m) = true If C(1, n, S) is true, w ∈ L(G)
Introduction to CL - Context-Free Grammars – p.17/32
SLIDE 40
CYK Chart Example
C B
john
1
saw
2
the
3
girl
4
in
5
a
6
car
7
Introduction to CL - Context-Free Grammars – p.18/32
SLIDE 41
CYK Chart Example
C B
john
1
saw
2
the
3
girl
4
in
5
a
6
car
7
N V D N P D N N V D N P D N
Introduction to CL - Context-Free Grammars – p.18/32
SLIDE 42
CYK Chart Example
C B
john
1
saw
2
the
3
girl
4
in
5
a
6
car
7
N V D N P D N N V D N P D N S NP NP 1 3 6 S NP NP
Introduction to CL - Context-Free Grammars – p.18/32
SLIDE 43
CYK Chart Example
C B
john
1
saw
2
the
3
girl
4
in
5
a
6
car
7
N V D N P D N N V D N P D N S NP NP 1 3 6 S NP NP VP PP 2 5 VP PP
Introduction to CL - Context-Free Grammars – p.18/32
SLIDE 44
CYK Chart Example
C B
john
1
saw
2
the
3
girl
4
in
5
a
6
car
7
N V D N P D N N V D N P D N S NP NP 1 3 6 S NP NP VP PP 2 5 VP PP S NP 1 4 S NP
Introduction to CL - Context-Free Grammars – p.18/32
SLIDE 45
CYK Chart Example
C B
john
1
saw
2
the
3
girl
4
in
5
a
6
car
7
N V D N P D N N V D N P D N S NP NP 1 3 6 S NP NP VP PP 2 5 VP PP S NP 1 4 S NP NP 4 NP
Introduction to CL - Context-Free Grammars – p.18/32
SLIDE 46
CYK Chart Example
C B
john
1
saw
2
the
3
girl
4
in
5
a
6
car
7
N V D N P D N N V D N P D N S NP NP 1 3 6 S NP NP VP PP 2 5 VP PP S NP 1 4 S NP NP 4 NP VP 2,4 VP
Introduction to CL - Context-Free Grammars – p.18/32
SLIDE 47
CYK Chart Example
C B
john
1
saw
2
the
3
girl
4
in
5
a
6
car
7
N V D N P D N N V D N P D N S NP NP 1 3 6 S NP NP VP PP 2 5 VP PP S NP 1 4 S NP NP 4 NP VP 2,4 VP S 1 S
Introduction to CL - Context-Free Grammars – p.18/32
SLIDE 48
Example Chart
S →NP VP|N VP|N V|NP V VP→V NP|V N|VP PP NP→D N|NP PP|N PP PP→P NP|P N N →john, girl, car V →saw, walks P →in D →the, a
1 2 3 4 5 6 7 N S S S 1 V VP VP(2) 2 D NP NP 3 N NP 4 P PP 5 D NP 6 N
0 john 1 saw 2 the 3 girl 4 in 5 a 6 car 7
Introduction to CL - Context-Free Grammars – p.19/32
SLIDE 49
Example Chart
S →NP VP|N VP|N V|NP V VP→V NP|V N|VP PP NP→D N|NP PP|N PP PP→P NP|P N N →john, girl, car V →saw, walks P →in D →the, a
1 2 3 4 5 6 7 N S S S 1 V VP VP(2) 2 D NP NP 3 N NP 4 P PP 5 D NP 6 N
0 john 1 saw 2 the 3 girl 4 in 5 a 6 car 7
Introduction to CL - Context-Free Grammars – p.19/32
SLIDE 50
Example Chart
S →NP VP|N VP|N V|NP V VP→V NP|V N|VP PP NP→D N|NP PP|N PP PP→P NP|P N N →john, girl, car V →saw, walks P →in D →the, a
1 2 3 4 5 6 7 N S S S 1 V VP VP(2) 2 D NP NP 3 N NP 4 P PP 5 D NP 6 N
0 john 1 saw 2 the 3 girl 4 in 5 a 6 car 7
Introduction to CL - Context-Free Grammars – p.19/32
SLIDE 51
Example Chart
S →NP VP|N VP|N V|NP V VP→V NP|V N|VP PP NP→D N|NP PP|N PP PP→P NP|P N N →john, girl, car V →saw, walks P →in D →the, a
1 2 3 4 5 6 7 N S S S 1 V VP VP(2) 2 D NP NP 3 N NP 4 P PP 5 D NP 6 N
0 john 1 saw 2 the 3 girl 4 in 5 a 6 car 7
Introduction to CL - Context-Free Grammars – p.19/32
SLIDE 52
Encoding Ambiguities
Σ = {john, girl, car, sees, in, the, a} N = {S, NP , VP , PP , D, N, V, P} P = S →NP VP, N→john, girl, car VP→V|V NP|V NP PP V→sees NP→N|D N|N PP|D N PP P→in PP→P NP D→the, a S NP N
john
VP V
sees
NP D
the
N
girl
PP P
in
NP D
a
N
car
Introduction to CL - Context-Free Grammars – p.20/32
SLIDE 53
Describing Chart Parsing Algorithms
Parsing algorithm should use original grammars for compara- bility
Introduction to CL - Context-Free Grammars – p.21/32
SLIDE 54
Describing Chart Parsing Algorithms
Parsing algorithm should use original grammars for comparability They are described using chart items, which consist of a symbol, derived from the grammar a start and end position from 0, . . . , n
Introduction to CL - Context-Free Grammars – p.21/32
SLIDE 55
Describing Chart Parsing Algorithms
Parsing algorithm should use original grammars for comparability They are described using chart items, which consist of a symbol, derived from the grammar a start and end position from 0, . . . , n The symbol of complete items is one of Σ ∪ N
Introduction to CL - Context-Free Grammars – p.21/32
SLIDE 56
Describing Chart Parsing Algorithms
Parsing algorithm should use original grammars for comparability They are described using chart items, which consist of a symbol, derived from the grammar a start and end position from 0, . . . , n The symbol of complete items is one of Σ ∪ N Incomplete chart items encode partially filled rules the symbol is a pair (r, i) of rule and dot position if
P ∋ r : A → αβ with |α| = i
write alternatively: A → α rβ
Introduction to CL - Context-Free Grammars – p.21/32
SLIDE 57
Bottom-Up Chart Parsing
How, when and which chart items are created or combined characterizes a parsing algorithm or parsing strategy First: A modified variant of Cocke-Younger-Kasami (CYK) algorithm Prerequisites: CFG G, input string w = a1, . . . , an Data Structures: A n + 1 × n + 1 chart C, where each cell contains a set of (complete or incomplete) chart items A set of chart items A (those must still be treated in some way) Initialization: add all (ai, i − 1, i) to A and Ci−1,i
Introduction to CL - Context-Free Grammars – p.22/32
SLIDE 58
Bottom-Up Parsing Algorithm
while A not empty take an (X, i, j) from A and remove it if X ∈ Σ ∪ N for P ∋ r ≡ A → Xα do
check and add(A → X rα, i, j)
for k ∈ 0, . . . , i − 1 do for all (A → β rXα, k, i) ∈ C do
check and add(A → βX rα, k, j)
else – incomplete item: X ≡ A → β rY α for k ∈ j + 1, . . . , n do if (Y, j, k) ∈ C
check and add(A → βY rα, i, k) check and add(X ≡ A → α rβ, i, j) ≡
if β = ǫ then if (A, i, j) ∈ C add (A, i, j) to A and C endif else if (A → α rβ, i, j) ∈ C add (A → α rβ, i, j) to A and C endif
Introduction to CL - Context-Free Grammars – p.23/32
SLIDE 59
Bottom-Up: The Details
How to implement A and C efficiently? Implementation of the (n + 1)2 sets in C:
Introduction to CL - Context-Free Grammars – p.24/32
SLIDE 60
Bottom-Up: The Details
How to implement A and C efficiently? Implementation of the (n + 1)2 sets in C: Operations: add single element, contains element
Introduction to CL - Context-Free Grammars – p.24/32
SLIDE 61
Bottom-Up: The Details
How to implement A and C efficiently? Implementation of the (n + 1)2 sets in C: Operations: add single element, contains element bit vector of size |G| := |Σ| + |N| +
P∋r:A→α |Aα|
Introduction to CL - Context-Free Grammars – p.24/32
SLIDE 62
Bottom-Up: The Details
How to implement A and C efficiently? Implementation of the (n + 1)2 sets in C: Operations: add single element, contains element bit vector of size |G| := |Σ| + |N| +
P∋r:A→α |Aα|
Implementation of the set A:
Introduction to CL - Context-Free Grammars – p.24/32
SLIDE 63
Bottom-Up: The Details
How to implement A and C efficiently? Implementation of the (n + 1)2 sets in C: Operations: add single element, contains element bit vector of size |G| := |Σ| + |N| +
P∋r:A→α |Aα|
Implementation of the set A: Operations: add , get and remove some element
Introduction to CL - Context-Free Grammars – p.24/32
SLIDE 64
Bottom-Up: The Details
How to implement A and C efficiently? Implementation of the (n + 1)2 sets in C: Operations: add single element, contains element bit vector of size |G| := |Σ| + |N| +
P∋r:A→α |Aα|
Implementation of the set A: Operations: add , get and remove some element (priority) queue, stack
A is called agenda and can be used to implement
search strategies Keep terminal items separate from the chart for space and time efficiency
Introduction to CL - Context-Free Grammars – p.24/32
SLIDE 65
Runtime of CYK Algorithm
check and add(X ≡ A → α rβ, i, j) ≡
if β = ǫ then if (A, i, j) ∈ C add (A, i, j) to A and C endif else if (A → α rβ, i, j) ∈ C add (A → α rβ, i, j) to A and C endif
Introduction to CL - Context-Free Grammars – p.25/32
SLIDE 66
Runtime of CYK Algorithm
check and add(X ≡ A → α rβ, i, j) ≡
all operations O(1) if β = ǫ then if (A, i, j) ∈ C add (A, i, j) to A and C endif else if (A → α rβ, i, j) ∈ C add (A → α rβ, i, j) to A and C endif
Introduction to CL - Context-Free Grammars – p.25/32
SLIDE 67
Runtime of CYK Algorithm
check and add(X ≡ A → α rβ, i, j) ≡
all operations O(1) if β = ǫ then if (A, i, j) ∈ C add (A, i, j) to A and C endif else if (A → α rβ, i, j) ∈ C add (A → α rβ, i, j) to A and C endif while A not empty take an (X, i, j) from A and remove it if X ∈ Σ ∪ N for P ∋ r ≡ A → Xα do
check and add(A → X rα, i, j)
for k ∈ 0, . . . , i − 1 do for all (A → β rXα, k, i) ∈ C do
check and add(A → βX rα, k, j)
else – incomplete item: X ≡ A → β rY α for k ∈ j + 1, . . . , n do if (Y, j, k) ∈ C
check and add(A → βY rα, i, k)
Introduction to CL - Context-Free Grammars – p.25/32
SLIDE 68 Runtime of CYK Algorithm
check and add(X ≡ A → α rβ, i, j) ≡
all operations O(1) if β = ǫ then if (A, i, j) ∈ C add (A, i, j) to A and C endif else if (A → α rβ, i, j) ∈ C add (A → α rβ, i, j) to A and C endif while A not empty take an (X, i, j) from A and remove it
if X ∈ Σ ∪ N for P ∋ r ≡ A → Xα do
check and add(A → X rα, i, j)
for k ∈ 0, . . . , i − 1 do for all (A → β rXα, k, i) ∈ C do
check and add(A → βX rα, k, j)
else – incomplete item: X ≡ A → β rY α for k ∈ j + 1, . . . , n do if (Y, j, k) ∈ C
check and add(A → βY rα, i, k)
Introduction to CL - Context-Free Grammars – p.25/32
SLIDE 69 Runtime of CYK Algorithm
check and add(X ≡ A → α rβ, i, j) ≡
all operations O(1) if β = ǫ then if (A, i, j) ∈ C add (A, i, j) to A and C endif else if (A → α rβ, i, j) ∈ C add (A → α rβ, i, j) to A and C endif while A not empty take an (X, i, j) from A and remove it
if X ∈ Σ ∪ N for P ∋ r ≡ A → Xα do
check and add(A → X rα, i, j)
for k ∈ 0, . . . , i − 1 do for all (A → β rXα, k, i) ∈ C do
check and add(A → βX rα, k, j)
else – incomplete item: X ≡ A → β rY α for k ∈ j + 1, . . . , n do if (Y, j, k) ∈ C
check and add(A → βY rα, i, k)
Introduction to CL - Context-Free Grammars – p.25/32
SLIDE 70 Runtime of CYK Algorithm
check and add(X ≡ A → α rβ, i, j) ≡
all operations O(1) if β = ǫ then if (A, i, j) ∈ C add (A, i, j) to A and C endif else if (A → α rβ, i, j) ∈ C add (A → α rβ, i, j) to A and C endif while A not empty take an (X, i, j) from A and remove it
if X ∈ Σ ∪ N for P ∋ r ≡ A → Xα do
check and add(A → X rα, i, j)
for k ∈ 0, . . . , i − 1 do
for all (A → β rXα, k, i) ∈ C do
check and add(A → βX rα, k, j)
else – incomplete item: X ≡ A → β rY α for k ∈ j + 1, . . . , n do if (Y, j, k) ∈ C
check and add(A → βY rα, i, k)
Introduction to CL - Context-Free Grammars – p.25/32
SLIDE 71 Runtime of CYK Algorithm
check and add(X ≡ A → α rβ, i, j) ≡
all operations O(1) if β = ǫ then if (A, i, j) ∈ C add (A, i, j) to A and C endif else if (A → α rβ, i, j) ∈ C add (A → α rβ, i, j) to A and C endif while A not empty take an (X, i, j) from A and remove it
if X ∈ Σ ∪ N for P ∋ r ≡ A → Xα do
check and add(A → X rα, i, j)
for k ∈ 0, . . . , i − 1 do
for all (A → β rXα, k, i) ∈ C do
check and add(A → βX rα, k, j)
else – incomplete item: X ≡ A → β rY α for k ∈ j + 1, . . . , n do if (Y, j, k) ∈ C
check and add(A → βY rα, i, k)
Introduction to CL - Context-Free Grammars – p.25/32
SLIDE 72 Runtime of CYK Algorithm
check and add(X ≡ A → α rβ, i, j) ≡
all operations O(1) if β = ǫ then if (A, i, j) ∈ C add (A, i, j) to A and C endif else if (A → α rβ, i, j) ∈ C add (A → α rβ, i, j) to A and C endif while A not empty take an (X, i, j) from A and remove it
if X ∈ Σ ∪ N for P ∋ r ≡ A → Xα do
check and add(A → X rα, i, j)
for k ∈ 0, . . . , i − 1 do
for all (A → β rXα, k, i) ∈ C do
check and add(A → βX rα, k, j)
else – incomplete item: X ≡ A → β rY α for k ∈ j + 1, . . . , n do
if (Y, j, k) ∈ C
check and add(A → βY rα, i, k)
Introduction to CL - Context-Free Grammars – p.25/32
SLIDE 73
CYK – Summary
Polynomial complexity: O(|G|2n3) Explores all possible sub-derivations Advantageous for robust parsing: Extract the biggest/best chunks for ungrammatical input That a derivation must start at S is not used at all Average time is near or equal to the worst case May lead to poor performance in practice Two main steps: if (X, i, j) ∈ C, X ∈ Σ ∪ N and A → Xα ∈ P: add (A → X rα, i, j) to C if (A → β rY α, i, j) ∈ C and (Y, j, k) ∈ C: add (A → βY rα, i, k) to C
Introduction to CL - Context-Free Grammars – p.26/32
SLIDE 74
Predictive Bottom-Up: Earley Parsing
Described by J. Earley (1970): Predict Top-Down and Complete Bottom-Up Initialize by adding the terminal items and (S → α, 0, 0) for all S → rα ∈ P Three main operations:
Prediction if (A → β rY α, i, j) ∈ C, add (Y → rγ, j, j) to C for
every Y → γ ∈ P
Scanning if (A → β raj+1α, i, j) ∈ C, add (A → βaj+1 rα, i, j + 1)
to C
Completion if (Y, i, j), Y ∈ N and (A → β rY α, j, k) ∈ C , add
(A → βY rα, i, k)
Introduction to CL - Context-Free Grammars – p.27/32
SLIDE 75
Earley Parsing Example
john saw the girl in a car
Introduction to CL - Context-Free Grammars – p.28/32
SLIDE 76
Earley Parsing Example
john saw the girl in a car S→ rNP VP
Introduction to CL - Context-Free Grammars – p.28/32
SLIDE 77
Earley Parsing Example
john saw the girl in a car S→ rNP VP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP
Introduction to CL - Context-Free Grammars – p.28/32
SLIDE 78
Earley Parsing Example
john saw the girl in a car S→ rNP VP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra
Introduction to CL - Context-Free Grammars – p.28/32
SLIDE 79
Earley Parsing Example
john saw the girl in a car S→ rNP VP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra N
Introduction to CL - Context-Free Grammars – p.28/32
SLIDE 80
Earley Parsing Example
john saw the girl in a car S→ rNP VP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra N NP
Introduction to CL - Context-Free Grammars – p.28/32
SLIDE 81
Earley Parsing Example
john saw the girl in a car S→ rNP VP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra N NP NP→N rPP NP→ rP NP
Introduction to CL - Context-Free Grammars – p.28/32
SLIDE 82
Earley Parsing Example
john saw the girl in a car S→ rNP VP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra N NP NP→N rPP NP→ rP NP S→NP rVP VP→ rV VP→ rV NP VP→ rV NP PP V→ rsaw
Introduction to CL - Context-Free Grammars – p.28/32
SLIDE 83
Earley Parsing Example
john saw the girl in a car S→ rNP VP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra N NP NP→N rPP NP→ rP NP S→NP rVP VP→ rV VP→ rV NP VP→ rV NP PP V→ rsaw V VP
Introduction to CL - Context-Free Grammars – p.28/32
SLIDE 84
Earley Parsing Example
john saw the girl in a car S→ rNP VP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra N NP NP→N rPP NP→ rP NP S→NP rVP VP→ rV VP→ rV NP VP→ rV NP PP V→ rsaw V VP VP→V rNP VP→V rNP PP S
Introduction to CL - Context-Free Grammars – p.28/32
SLIDE 85
Earley Parsing Example
john saw the girl in a car S→ rNP VP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra N NP NP→N rPP NP→ rP NP S→NP rVP VP→ rV VP→ rV NP VP→ rV NP PP V→ rsaw V VP VP→V rNP VP→V rNP PP S NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra
Introduction to CL - Context-Free Grammars – p.28/32
SLIDE 86
Earley Parsing Example
john saw the girl in a car S→ rNP VP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra N NP NP→N rPP NP→ rP NP S→NP rVP VP→ rV VP→ rV NP VP→ rV NP PP V→ rsaw V VP VP→V rNP VP→V rNP PP S NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra D NP→D rN NP→D rN PP
Introduction to CL - Context-Free Grammars – p.28/32
SLIDE 87
Earley Parsing Example
john saw the girl in a car S→ rNP VP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra N NP NP→N rPP NP→ rP NP S→NP rVP VP→ rV VP→ rV NP VP→ rV NP PP V→ rsaw V VP VP→V rNP VP→V rNP PP S NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra D NP→D rN NP→D rN PP N→ rjohn N→ rgirl N→ rcar
Introduction to CL - Context-Free Grammars – p.28/32
SLIDE 88
Earley Parsing Example
john saw the girl in a car S→ rNP VP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra N NP NP→N rPP NP→ rP NP S→NP rVP VP→ rV VP→ rV NP VP→ rV NP PP V→ rsaw V VP VP→V rNP VP→V rNP PP S NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra D NP→D rN NP→D rN PP N→ rjohn N→ rgirl N→ rcar N NP
Introduction to CL - Context-Free Grammars – p.28/32
SLIDE 89
Earley Parsing Example
john saw the girl in a car S→ rNP VP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra N NP NP→N rPP NP→ rP NP S→NP rVP VP→ rV VP→ rV NP VP→ rV NP PP V→ rsaw V VP VP→V rNP VP→V rNP PP S NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra D NP→D rN NP→D rN PP N→ rjohn N→ rgirl N→ rcar N NP NP→D N rPP VP VP→V NP rPP S
Introduction to CL - Context-Free Grammars – p.28/32
SLIDE 90
Earley Parsing Example
john saw the girl in a car S→ rNP VP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra N NP NP→N rPP NP→ rP NP S→NP rVP VP→ rV VP→ rV NP VP→ rV NP PP V→ rsaw V VP VP→V rNP VP→V rNP PP S NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra D NP→D rN NP→D rN PP N→ rjohn N→ rgirl N→ rcar N NP NP→D N rPP VP VP→V NP rPP S PP→ rP NP P→ rin P PP→P rNP
Introduction to CL - Context-Free Grammars – p.28/32
SLIDE 91
Earley Parsing Example
john saw the girl in a car S→ rNP VP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra N NP NP→N rPP NP→ rP NP S→NP rVP VP→ rV VP→ rV NP VP→ rV NP PP V→ rsaw V VP VP→V rNP VP→V rNP PP S NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra D NP→D rN NP→D rN PP N→ rjohn N→ rgirl N→ rcar N NP NP→D N rPP VP VP→V NP rPP S PP→ rP NP P→ rin P PP→P rNP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra
Introduction to CL - Context-Free Grammars – p.28/32
SLIDE 92
Earley Parsing Example
john saw the girl in a car S→ rNP VP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra N NP NP→N rPP NP→ rP NP S→NP rVP VP→ rV VP→ rV NP VP→ rV NP PP V→ rsaw V VP VP→V rNP VP→V rNP PP S NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra D NP→D rN NP→D rN PP N→ rjohn N→ rgirl N→ rcar N NP NP→D N rPP VP VP→V NP rPP S PP→ rP NP P→ rin P PP→P rNP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra D NP→D rN NP→D rN PP N→ rjohn N→ rgirl N→ rcar N NP
Introduction to CL - Context-Free Grammars – p.28/32
SLIDE 93
Earley Parsing Example
john saw the girl in a car S→ rNP VP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra N NP NP→N rPP NP→ rP NP S→NP rVP VP→ rV VP→ rV NP VP→ rV NP PP V→ rsaw V VP VP→V rNP VP→V rNP PP S NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra D NP→D rN NP→D rN PP N→ rjohn N→ rgirl N→ rcar N NP NP→D N rPP VP VP→V NP rPP S PP→ rP NP P→ rin P PP→P rNP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra D NP→D rN NP→D rN PP N→ rjohn N→ rgirl N→ rcar N NP NP→D N rPP PP→ rP NP P→ rin
Introduction to CL - Context-Free Grammars – p.28/32
SLIDE 94
Earley Parsing Example
john saw the girl in a car S→ rNP VP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra N NP NP→N rPP NP→ rP NP S→NP rVP VP→ rV VP→ rV NP VP→ rV NP PP V→ rsaw V VP VP→V rNP VP→V rNP PP S NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra D NP→D rN NP→D rN PP N→ rjohn N→ rgirl N→ rcar N NP NP→D N rPP VP VP→V NP rPP S PP→ rP NP P→ rin P PP→P rNP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra D NP→D rN NP→D rN PP N→ rjohn N→ rgirl N→ rcar N NP NP→D N rPP PP→ rP NP P→ rin PP
Introduction to CL - Context-Free Grammars – p.28/32
SLIDE 95
Earley Parsing Example
john saw the girl in a car S→ rNP VP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra N NP NP→N rPP NP→ rP NP S→NP rVP VP→ rV VP→ rV NP VP→ rV NP PP V→ rsaw V VP VP→V rNP VP→V rNP PP S NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra D NP→D rN NP→D rN PP N→ rjohn N→ rgirl N→ rcar N NP NP→D N rPP VP VP→V NP rPP S PP→ rP NP P→ rin P PP→P rNP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra D NP→D rN NP→D rN PP N→ rjohn N→ rgirl N→ rcar N NP NP→D N rPP PP→ rP NP P→ rin PP NP
Introduction to CL - Context-Free Grammars – p.28/32
SLIDE 96
Earley Parsing Example
john saw the girl in a car S→ rNP VP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra N NP NP→N rPP NP→ rP NP S→NP rVP VP→ rV VP→ rV NP VP→ rV NP PP V→ rsaw V VP VP→V rNP VP→V rNP PP S NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra D NP→D rN NP→D rN PP N→ rjohn N→ rgirl N→ rcar N NP NP→D N rPP VP VP→V NP rPP S PP→ rP NP P→ rin P PP→P rNP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra D NP→D rN NP→D rN PP N→ rjohn N→ rgirl N→ rcar N NP NP→D N rPP PP→ rP NP P→ rin PP NP VP
Introduction to CL - Context-Free Grammars – p.28/32
SLIDE 97
Earley Parsing Example
john saw the girl in a car S→ rNP VP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra N NP NP→N rPP NP→ rP NP S→NP rVP VP→ rV VP→ rV NP VP→ rV NP PP V→ rsaw V VP VP→V rNP VP→V rNP PP S NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra D NP→D rN NP→D rN PP N→ rjohn N→ rgirl N→ rcar N NP NP→D N rPP VP VP→V NP rPP S PP→ rP NP P→ rin P PP→P rNP NP→ rN NP→ rD N NP→ rN PP NP→ rD N PP N→ rjohn N→ rgirl N→ rcar D→ rthe D→ ra D NP→D rN NP→D rN PP N→ rjohn N→ rgirl N→ rcar N NP NP→D N rPP PP→ rP NP P→ rin PP NP VP S
Introduction to CL - Context-Free Grammars – p.28/32
SLIDE 98
Earley Parsing – Summary
The number of useless items is reduced Superior runtime for unambiguous grammars: O(n2) Valid prefix property Not all sub-derivations are computed
Introduction to CL - Context-Free Grammars – p.29/32
SLIDE 99
Left Corner Parsing
Observation: Earley parsing predicts items that can not succeed Idea: predict only items that can also be derived from the leftmost terminal item Formalization: left-corner relation
A >l B ⇐ ⇒ ∃β : A → Bβ ∈ P, B ∈ Σ ∪ N A >∗
l is the transitive closure of >l
Reformulation of the prediction step: If (A → β rY α, i, j) and (B, j, k) ∈ C, with B ∈ Σ ∪ N add
(C → B rγ, j, k) if Y >∗
l C
This formulation also avoids the zero-length predictions with the dot in initial position
Introduction to CL - Context-Free Grammars – p.30/32
SLIDE 100
Left-Corner Example
john saw the girl in a car
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 101
Left-Corner Example
john saw the girl in a car N
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 102
Left-Corner Example
john saw the girl in a car N NP→N rPP
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 103
Left-Corner Example
john saw the girl in a car N NP→N rPP NP
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 104
Left-Corner Example
john saw the girl in a car N NP→N rPP NP S→NP rVP
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 105
Left-Corner Example
john saw the girl in a car N NP→N rPP NP S→NP rVP V
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 106
Left-Corner Example
john saw the girl in a car N NP→N rPP NP S→NP rVP V VP
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 107
Left-Corner Example
john saw the girl in a car N NP→N rPP NP S→NP rVP V VP VP→V rNP
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 108
Left-Corner Example
john saw the girl in a car N NP→N rPP NP S→NP rVP V VP VP→V rNP VP→V rNP PP
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 109
Left-Corner Example
john saw the girl in a car N NP→N rPP NP S→NP rVP V VP VP→V rNP VP→V rNP PP S
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 110
Left-Corner Example
john saw the girl in a car N NP→N rPP NP S→NP rVP V VP VP→V rNP VP→V rNP PP S D
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 111
Left-Corner Example
john saw the girl in a car N NP→N rPP NP S→NP rVP V VP VP→V rNP VP→V rNP PP S D NP→D rN
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 112
Left-Corner Example
john saw the girl in a car N NP→N rPP NP S→NP rVP V VP VP→V rNP VP→V rNP PP S D NP→D rN NP→D rN PP
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 113
Left-Corner Example
john saw the girl in a car N NP→N rPP NP S→NP rVP V VP VP→V rNP VP→V rNP PP S D NP→D rN NP→D rN PP N
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 114
Left-Corner Example
john saw the girl in a car N NP→N rPP NP S→NP rVP V VP VP→V rNP VP→V rNP PP S D NP→D rN NP→D rN PP N NP
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 115
Left-Corner Example
john saw the girl in a car N NP→N rPP NP S→NP rVP V VP VP→V rNP VP→V rNP PP S D NP→D rN NP→D rN PP N NP NP→D N rPP
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 116
Left-Corner Example
john saw the girl in a car N NP→N rPP NP S→NP rVP V VP VP→V rNP VP→V rNP PP S D NP→D rN NP→D rN PP N NP NP→D N rPP VP
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 117
Left-Corner Example
john saw the girl in a car N NP→N rPP NP S→NP rVP V VP VP→V rNP VP→V rNP PP S D NP→D rN NP→D rN PP N NP NP→D N rPP VP VP→V NP rPP
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 118
Left-Corner Example
john saw the girl in a car N NP→N rPP NP S→NP rVP V VP VP→V rNP VP→V rNP PP S D NP→D rN NP→D rN PP N NP NP→D N rPP VP VP→V NP rPP S
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 119
Left-Corner Example
john saw the girl in a car N NP→N rPP NP S→NP rVP V VP VP→V rNP VP→V rNP PP S D NP→D rN NP→D rN PP N NP NP→D N rPP VP VP→V NP rPP S P
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 120
Left-Corner Example
john saw the girl in a car N NP→N rPP NP S→NP rVP V VP VP→V rNP VP→V rNP PP S D NP→D rN NP→D rN PP N NP NP→D N rPP VP VP→V NP rPP S P PP→P rNP
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 121
Left-Corner Example
john saw the girl in a car N NP→N rPP NP S→NP rVP V VP VP→V rNP VP→V rNP PP S D NP→D rN NP→D rN PP N NP NP→D N rPP VP VP→V NP rPP S P PP→P rNP D
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 122
Left-Corner Example
john saw the girl in a car N NP→N rPP NP S→NP rVP V VP VP→V rNP VP→V rNP PP S D NP→D rN NP→D rN PP N NP NP→D N rPP VP VP→V NP rPP S P PP→P rNP D NP→D rN
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 123
Left-Corner Example
john saw the girl in a car N NP→N rPP NP S→NP rVP V VP VP→V rNP VP→V rNP PP S D NP→D rN NP→D rN PP N NP NP→D N rPP VP VP→V NP rPP S P PP→P rNP D NP→D rN NP→D rN PP
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 124
Left-Corner Example
john saw the girl in a car N NP→N rPP NP S→NP rVP V VP VP→V rNP VP→V rNP PP S D NP→D rN NP→D rN PP N NP NP→D N rPP VP VP→V NP rPP S P PP→P rNP D NP→D rN NP→D rN PP N
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 125
Left-Corner Example
john saw the girl in a car N NP→N rPP NP S→NP rVP V VP VP→V rNP VP→V rNP PP S D NP→D rN NP→D rN PP N NP NP→D N rPP VP VP→V NP rPP S P PP→P rNP D NP→D rN NP→D rN PP N NP
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 126
Left-Corner Example
john saw the girl in a car N NP→N rPP NP S→NP rVP V VP VP→V rNP VP→V rNP PP S D NP→D rN NP→D rN PP N NP NP→D N rPP VP VP→V NP rPP S P PP→P rNP D NP→D rN NP→D rN PP N NP NP→D N rPP
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 127
Left-Corner Example
john saw the girl in a car N NP→N rPP NP S→NP rVP V VP VP→V rNP VP→V rNP PP S D NP→D rN NP→D rN PP N NP NP→D N rPP VP VP→V NP rPP S P PP→P rNP D NP→D rN NP→D rN PP N NP NP→D N rPP PP
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 128
Left-Corner Example
john saw the girl in a car N NP→N rPP NP S→NP rVP V VP VP→V rNP VP→V rNP PP S D NP→D rN NP→D rN PP N NP NP→D N rPP VP VP→V NP rPP S P PP→P rNP D NP→D rN NP→D rN PP N NP NP→D N rPP PP NP
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 129
Left-Corner Example
john saw the girl in a car N NP→N rPP NP S→NP rVP V VP VP→V rNP VP→V rNP PP S D NP→D rN NP→D rN PP N NP NP→D N rPP VP VP→V NP rPP S P PP→P rNP D NP→D rN NP→D rN PP N NP NP→D N rPP PP NP VP S
Introduction to CL - Context-Free Grammars – p.31/32
SLIDE 130
Context-Free Parsing: Summary
Context-free grammars provide you with a finite set of infinitely embeddable brackets Two main approaches to CF recognition: top down (goal-driven) and bottom-up (data driven) Storing sub-derivations for re-use (dynamic programming) in a chart lead to a polynomial algorithm with worst case n3 The chart offers a compact (polynomial size) storage for a possibly exponential number of results Earley and Left Corner Parsing improve the average runtime over the naïve CYK algorithm, and have a better worst case complexity for some classes of context-free grammars
Introduction to CL - Context-Free Grammars – p.32/32