the general parsing task top down parsing · top down parsing a top-down parser will effectively...
TRANSCRIPT
![Page 1: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/1.jpg)
Parsing CFGs with Stacks
Outline
The general parsing task
Top Down Parsing
![Page 2: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/2.jpg)
Parsing CFGs with Stacks
Outline
Top-down parsing
![Page 3: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/3.jpg)
Parsing CFGs with Stacks
The general parsing task
Outline
The general parsing task
![Page 4: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/4.jpg)
Parsing CFGs with Stacks
The general parsing task
The basic parsing task
◮ Given a grammar G, a category x and an input string w1 . . .wn, the job of
a parser is to discover whether G categorises w1 . . .wn as x ,
![Page 5: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/5.jpg)
Parsing CFGs with Stacks
The general parsing task
The basic parsing task
◮ Given a grammar G, a category x and an input string w1 . . .wn, the job of
a parser is to discover whether G categorises w1 . . .wn as x ,
◮ or equivalently, whether it permits any analysis tree whose topmost node
is x and whose leaves are w1 . . .wn.
![Page 6: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/6.jpg)
Parsing CFGs with Stacks
The general parsing task
The basic parsing task
◮ Given a grammar G, a category x and an input string w1 . . .wn, the job of
a parser is to discover whether G categorises w1 . . .wn as x ,
◮ or equivalently, whether it permits any analysis tree whose topmost node
is x and whose leaves are w1 . . .wn.
◮ Variants of this:
◮ find all parse trees, if there is more than one◮ find also the x ’s which categorise the input, rather than assuming this is
given
![Page 7: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/7.jpg)
Parsing CFGs with Stacks
The general parsing task
Top-down vs Bottom-up
◮ There are many ways a parser might manage the search process.
![Page 8: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/8.jpg)
Parsing CFGs with Stacks
The general parsing task
Top-down vs Bottom-up
◮ There are many ways a parser might manage the search process.
◮ If a parser expands a tree down towards its leaves it is said to be
working top-down.
![Page 9: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/9.jpg)
Parsing CFGs with Stacks
The general parsing task
Top-down vs Bottom-up
◮ There are many ways a parser might manage the search process.
◮ If a parser expands a tree down towards its leaves it is said to be
working top-down.
◮ By contrast a bottom-up parser fuses subtrees together with the aim of
making a single encompassing tree.
![Page 10: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/10.jpg)
Parsing CFGs with Stacks
Top Down Parsing
Outline
Top Down Parsing
![Page 11: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/11.jpg)
Parsing CFGs with Stacks
Top Down Parsing
As a beginning
example take the
following grammar
s ⇒ sadv , s
s ⇒ np, vp
np ⇒ john
vp ⇒ iv
iv ⇒ walks
sadv ⇒ maybe
![Page 12: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/12.jpg)
Parsing CFGs with Stacks
Top Down Parsing
As a beginning
example take the
following grammar
s ⇒ sadv , s
s ⇒ np, vp
np ⇒ john
vp ⇒ iv
iv ⇒ walks
sadv ⇒ maybe
maybe john walks is an s according to this grammar
with the syntax analysis
s
john
sadv
maybe
s
np vp
iv
walks
![Page 13: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/13.jpg)
Parsing CFGs with Stacks
Top Down Parsing
◮ A top-down parser will effectively seek to settle the questions ’is the input
an x ’ by deriving a tree in a succession of stages, starting with just a
single node x-tree and ending with the complete tree
![Page 14: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/14.jpg)
Parsing CFGs with Stacks
Top Down Parsing
◮ A top-down parser will effectively seek to settle the questions ’is the input
an x ’ by deriving a tree in a succession of stages, starting with just a
single node x-tree and ending with the complete tree
◮ At every stage of this process of tree derivation, there are choices to be
made
![Page 15: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/15.jpg)
Parsing CFGs with Stacks
Top Down Parsing
◮ A top-down parser will effectively seek to settle the questions ’is the input
an x ’ by deriving a tree in a succession of stages, starting with just a
single node x-tree and ending with the complete tree
◮ At every stage of this process of tree derivation, there are choices to be
made
◮ One choice is which node to expand
![Page 16: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/16.jpg)
Parsing CFGs with Stacks
Top Down Parsing
◮ A top-down parser will effectively seek to settle the questions ’is the input
an x ’ by deriving a tree in a succession of stages, starting with just a
single node x-tree and ending with the complete tree
◮ At every stage of this process of tree derivation, there are choices to be
made
◮ One choice is which node to expand
◮ the other choice is how to expand each node
![Page 17: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/17.jpg)
Parsing CFGs with Stacks
Top Down Parsing
Which node to work on ?
To illustrate the first kind of choice, consider the following two derivations
(you’ll have to zoom/magnify to see this):
![Page 18: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/18.jpg)
Parsing CFGs with Stacks
Top Down Parsing
Which node to work on ?
To illustrate the first kind of choice, consider the following two derivations
(you’ll have to zoom/magnify to see this):
walks
np vp
maybe np vp
s
sadv s
walks
s
sadv s
maybe
s
sadv s
maybe np vp
john
s
sadv s
maybe np vp
ivjohn
s
sadv s
maybe np vp
iv
maybe np vp
s
sadv s
iv
maybe np vp
s
sadv s
john
s
sadv s
s
sadv s
s
sadv s
ivjohn
s
sadv s
maybe np vp
iv
maybe np vp
s
sadv s
john
![Page 19: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/19.jpg)
Parsing CFGs with Stacks
Top Down Parsing
Which node to work on ?
To illustrate the first kind of choice, consider the following two derivations
(you’ll have to zoom/magnify to see this):
walks
np vp
maybe np vp
s
sadv s
walks
s
sadv s
maybe
s
sadv s
maybe np vp
john
s
sadv s
maybe np vp
ivjohn
s
sadv s
maybe np vp
iv
maybe np vp
s
sadv s
iv
maybe np vp
s
sadv s
john
s
sadv s
s
sadv s
s
sadv s
ivjohn
s
sadv s
maybe np vp
iv
maybe np vp
s
sadv s
john
![Page 20: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/20.jpg)
Parsing CFGs with Stacks
Top Down Parsing
Which node to work on ?
To illustrate the first kind of choice, consider the following two derivations
(you’ll have to zoom/magnify to see this):
walks
np vp
maybe np vp
s
sadv s
walks
s
sadv s
maybe
s
sadv s
maybe np vp
john
s
sadv s
maybe np vp
ivjohn
s
sadv s
maybe np vp
iv
maybe np vp
s
sadv s
iv
maybe np vp
s
sadv s
john
s
sadv s
s
sadv s
s
sadv s
ivjohn
s
sadv s
maybe np vp
iv
maybe np vp
s
sadv s
john
◮ In the first derivation, there is a system to the way
the tree is grown
![Page 21: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/21.jpg)
Parsing CFGs with Stacks
Top Down Parsing
Which node to work on ?
To illustrate the first kind of choice, consider the following two derivations
(you’ll have to zoom/magnify to see this):
walks
np vp
maybe np vp
s
sadv s
walks
s
sadv s
maybe
s
sadv s
maybe np vp
john
s
sadv s
maybe np vp
ivjohn
s
sadv s
maybe np vp
iv
maybe np vp
s
sadv s
iv
maybe np vp
s
sadv s
john
s
sadv s
s
sadv s
s
sadv s
ivjohn
s
sadv s
maybe np vp
iv
maybe np vp
s
sadv s
john
◮ In the first derivation, there is a system to the way
the tree is grown
◮ in the second derivation, the tree growth is
random.
![Page 22: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/22.jpg)
Parsing CFGs with Stacks
Top Down Parsing
Which node to work on ?
To illustrate the first kind of choice, consider the following two derivations
(you’ll have to zoom/magnify to see this):
walks
np vp
maybe np vp
s
sadv s
walks
s
sadv s
maybe
s
sadv s
maybe np vp
john
s
sadv s
maybe np vp
ivjohn
s
sadv s
maybe np vp
iv
maybe np vp
s
sadv s
iv
maybe np vp
s
sadv s
john
s
sadv s
s
sadv s
s
sadv s
ivjohn
s
sadv s
maybe np vp
iv
maybe np vp
s
sadv s
john
◮ In the first derivation, there is a system to the way
the tree is grown
◮ in the second derivation, the tree growth is
random.
◮ in the first derivation at every step
the leftmost expandable leaf node is
expanded
![Page 23: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/23.jpg)
Parsing CFGs with Stacks
Top Down Parsing
Which node to work on ?
To illustrate the first kind of choice, consider the following two derivations
(you’ll have to zoom/magnify to see this):
walks
np vp
maybe np vp
s
sadv s
walks
s
sadv s
maybe
s
sadv s
maybe np vp
john
s
sadv s
maybe np vp
ivjohn
s
sadv s
maybe np vp
iv
maybe np vp
s
sadv s
iv
maybe np vp
s
sadv s
john
s
sadv s
s
sadv s
s
sadv s
ivjohn
s
sadv s
maybe np vp
iv
maybe np vp
s
sadv s
john
◮ In the first derivation, there is a system to the way
the tree is grown
◮ in the second derivation, the tree growth is
random.
◮ in the first derivation at every step
the leftmost expandable leaf node is
expanded
◮ The key fact is this:
if there is an analysis tree for some
input, then it can be generated by
applying leftmost expansion
![Page 24: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/24.jpg)
Parsing CFGs with Stacks
Top Down Parsing
◮ so an algorithm to explore the space of tree derivations, can restrict
attention to the derivations which use leftmost expansion
![Page 25: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/25.jpg)
Parsing CFGs with Stacks
Top Down Parsing
◮ so an algorithm to explore the space of tree derivations, can restrict
attention to the derivations which use leftmost expansion
◮ this means the ’which node’ source of choice can be eliminated: always
deterministically choose the leftmost unexpanded node.
![Page 26: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/26.jpg)
Parsing CFGs with Stacks
Top Down Parsing
◮ so an algorithm to explore the space of tree derivations, can restrict
attention to the derivations which use leftmost expansion
◮ this means the ’which node’ source of choice can be eliminated: always
deterministically choose the leftmost unexpanded node.
◮ there is still the other source of choise, of non-determinism: more than
one way to expand a given node.
![Page 27: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/27.jpg)
Parsing CFGs with Stacks
Top Down Parsing
◮ so an algorithm to explore the space of tree derivations, can restrict
attention to the derivations which use leftmost expansion
◮ this means the ’which node’ source of choice can be eliminated: always
deterministically choose the leftmost unexpanded node.
◮ there is still the other source of choise, of non-determinism: more than
one way to expand a given node. This still has to be dealt with, but to
begin we will get familiar with the deterministic case.
![Page 28: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/28.jpg)
Parsing CFGs with Stacks
Top Down Parsing
The frontier
Summarising the first derivation as a
series of snap shots of the leaf nodes,
you have:
Leaf nodes
s
sadv s
maybe s
maybe np vp
maybe john vp
maybe john iv
maybe john walks
![Page 29: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/29.jpg)
Parsing CFGs with Stacks
Top Down Parsing
The frontier
Summarising the first derivation as a
series of snap shots of the leaf nodes,
you have:
Leaf nodes
s
sadv s
maybe s
maybe np vp
maybe john vp
maybe john iv
maybe john walks
Let use the term Frontier for the
subset of the leaf-nodes which are
expandable
Leaf nodes Frontier
s s
sadv s sadv s
maybe s s
maybe np vp np vp
maybe john vp vp
maybe john iv iv
maybe john walks
![Page 30: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/30.jpg)
Parsing CFGs with Stacks
Top Down Parsing
The frontier as a stack
Leaf nodes Frontier
s s
sadv s sadv s
maybe s s
maybe np vp np vp
maybe john vp vp
maybe john iv iv
maybe john walks
![Page 31: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/31.jpg)
Parsing CFGs with Stacks
Top Down Parsing
The frontier as a stack
Leaf nodes Frontier
s s
sadv s sadv s
maybe s s
maybe np vp np vp
maybe john vp vp
maybe john iv iv
maybe john walks
◮ Because of the choice to always
take the leftmost unexpanded
node, the frontier operates in the
fashion of a stack.
with a last-in/first-out (LIFO)
behaviour.
![Page 32: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/32.jpg)
Parsing CFGs with Stacks
Top Down Parsing
The frontier as a stack
Leaf nodes Frontier
s s
sadv s sadv s
maybe s s
maybe np vp np vp
maybe john vp vp
maybe john iv iv
maybe john walks
◮ Because of the choice to always
take the leftmost unexpanded
node, the frontier operates in the
fashion of a stack.
with a last-in/first-out (LIFO)
behaviour.
◮ You can keep adding to the top of
a stack (pushing),
![Page 33: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/33.jpg)
Parsing CFGs with Stacks
Top Down Parsing
The frontier as a stack
Leaf nodes Frontier
s s
sadv s sadv s
maybe s s
maybe np vp np vp
maybe john vp vp
maybe john iv iv
maybe john walks
◮ Because of the choice to always
take the leftmost unexpanded
node, the frontier operates in the
fashion of a stack.
with a last-in/first-out (LIFO)
behaviour.
◮ You can keep adding to the top of
a stack (pushing),
◮ and its the most recently added
things that you can remove
(popping) and replace (more
pushing).
![Page 34: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/34.jpg)
Parsing CFGs with Stacks
Top Down Parsing
this leads to the idea that one can manage the search through the space of
possible tree derivations, by managing a search through a space of possible
stack states.
![Page 35: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/35.jpg)
Parsing CFGs with Stacks
Top Down Parsing
this leads to the idea that one can manage the search through the space of
possible tree derivations, by managing a search through a space of possible
stack states.
can now give an outline of a top-down algorithm.
![Page 36: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/36.jpg)
Parsing CFGs with Stacks
Top Down Parsing
this leads to the idea that one can manage the search through the space of
possible tree derivations, by managing a search through a space of possible
stack states.
can now give an outline of a top-down algorithm.
Let w be an array representing the input,
let i be the index of the current word.
use F for the frontier of nodes in the tree that are due to be expanded.
![Page 37: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/37.jpg)
Top-down parsing algorithm (without backtracking)
set F to start symbol, progress indicator i = 0
![Page 38: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/38.jpg)
Top-down parsing algorithm (without backtracking)
set F to start symbol, progress indicator i = 0
MOVES:let A = top(F)loop thru the rules {
![Page 39: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/39.jpg)
Top-down parsing algorithm (without backtracking)
set F to start symbol, progress indicator i = 0
MOVES:let A = top(F)loop thru the rules {
if (rule is A → w [i ]){ //LEAF CANCELLATIONpop top of F
set i = i+1
goto MOVES
}
![Page 40: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/40.jpg)
Top-down parsing algorithm (without backtracking)
set F to start symbol, progress indicator i = 0
MOVES:let A = top(F)loop thru the rules {
if (rule is A → w [i ]){ //LEAF CANCELLATIONpop top of F
set i = i+1
goto MOVES
}
else if (rule is A → D1 . . . Dn){ //LEFT EXPANSIONpop top of Fpush Dn ... push D1 note ordergoto MOVES
}
}
![Page 41: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/41.jpg)
Top-down parsing algorithm (without backtracking)
set F to start symbol, progress indicator i = 0
MOVES:let A = top(F)loop thru the rules {
if (rule is A → w [i ]){ //LEAF CANCELLATIONpop top of F
set i = i+1
goto MOVES
}
else if (rule is A → D1 . . . Dn){ //LEFT EXPANSIONpop top of Fpush Dn ... push D1 note ordergoto MOVES
}
}
YES_NO:
if ((F is empty) && (i == size of input)) { succeed }
else { fail }
![Page 42: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/42.jpg)
Parsing CFGs with Stacks
Top Down Parsing
About the top-down algorithm
◮ algorithm keeps looking for a move it can make to update its progress
through the input and the stack of categories F.
![Page 43: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/43.jpg)
Parsing CFGs with Stacks
Top Down Parsing
About the top-down algorithm
◮ algorithm keeps looking for a move it can make to update its progress
through the input and the stack of categories F.
◮ first kind of move, leaf cancellation, recognises that the top the stack
represents a node which could have the current word attached
underneath it. Doing so removes a category off the stack and moves
progress through the input by 1.
![Page 44: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/44.jpg)
Parsing CFGs with Stacks
Top Down Parsing
About the top-down algorithm
◮ algorithm keeps looking for a move it can make to update its progress
through the input and the stack of categories F.
◮ first kind of move, leaf cancellation, recognises that the top the stack
represents a node which could have the current word attached
underneath it. Doing so removes a category off the stack and moves
progress through the input by 1.
◮ second kind of move, left expansion, recognises that the top of the stack
represents a node which could have a sequence of daughters
corresponding to the right-hand side of rule attached underneath it.
![Page 45: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/45.jpg)
Parsing CFGs with Stacks
Top Down Parsing
About the top-down algorithm
◮ algorithm keeps looking for a move it can make to update its progress
through the input and the stack of categories F.
◮ first kind of move, leaf cancellation, recognises that the top the stack
represents a node which could have the current word attached
underneath it. Doing so removes a category off the stack and moves
progress through the input by 1.
◮ second kind of move, left expansion, recognises that the top of the stack
represents a node which could have a sequence of daughters
corresponding to the right-hand side of rule attached underneath it.
◮ in checking if a move is possible, the grammar rules are considered in
order from top to bottom
![Page 46: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/46.jpg)
Parsing CFGs with Stacks
Top Down Parsing
About the top-down algorithm
◮ algorithm keeps looking for a move it can make to update its progress
through the input and the stack of categories F.
◮ first kind of move, leaf cancellation, recognises that the top the stack
represents a node which could have the current word attached
underneath it. Doing so removes a category off the stack and moves
progress through the input by 1.
◮ second kind of move, left expansion, recognises that the top of the stack
represents a node which could have a sequence of daughters
corresponding to the right-hand side of rule attached underneath it.
◮ in checking if a move is possible, the grammar rules are considered in
order from top to bottom
◮ note in left expansion rules daughters must be pushed in a last-to-first
order, to guarantee that first daughter ends up on top of the stack.
![Page 47: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/47.jpg)
an example
eg.
s → np, vp
np → det ,n
det → the
n → man
n → dog
vp → tv , np
tv → hit
![Page 48: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/48.jpg)
an example
eg.
s → np, vp
np → det ,n
det → the
n → man
n → dog
vp → tv , np
tv → hit
![Page 49: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/49.jpg)
an example
eg.
s → np, vp
np → det ,n
det → the
n → man
n → dog
vp → tv , np
tv → hit
parsing the man hit the dog (top of stack show
at left):
WORDS STACKthe man hit the dog s
![Page 50: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/50.jpg)
an example
eg.
s → np, vp
np → det ,n
det → the
n → man
n → dog
vp → tv , np
tv → hit
parsing the man hit the dog (top of stack show
at left):
WORDS STACKthe man hit the dog s
the man hit the dog np vp
![Page 51: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/51.jpg)
an example
eg.
s → np, vp
np → det ,n
det → the
n → man
n → dog
vp → tv , np
tv → hit
parsing the man hit the dog (top of stack show
at left):
WORDS STACKthe man hit the dog s
the man hit the dog np vp
the man hit the dog det n vp
![Page 52: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/52.jpg)
an example
eg.
s → np, vp
np → det ,n
det → the
n → man
n → dog
vp → tv , np
tv → hit
parsing the man hit the dog (top of stack show
at left):
WORDS STACKthe man hit the dog s
the man hit the dog np vp
the man hit the dog det n vp
man hit the dog n vp
![Page 53: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/53.jpg)
an example
eg.
s → np, vp
np → det ,n
det → the
n → man
n → dog
vp → tv , np
tv → hit
parsing the man hit the dog (top of stack show
at left):
WORDS STACKthe man hit the dog s
the man hit the dog np vp
the man hit the dog det n vp
man hit the dog n vp
hit the dog vp
![Page 54: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/54.jpg)
an example
eg.
s → np, vp
np → det ,n
det → the
n → man
n → dog
vp → tv , np
tv → hit
parsing the man hit the dog (top of stack show
at left):
WORDS STACKthe man hit the dog s
the man hit the dog np vp
the man hit the dog det n vp
man hit the dog n vp
hit the dog vp
hit the dog tv np
![Page 55: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/55.jpg)
an example
eg.
s → np, vp
np → det ,n
det → the
n → man
n → dog
vp → tv , np
tv → hit
parsing the man hit the dog (top of stack show
at left):
WORDS STACKthe man hit the dog s
the man hit the dog np vp
the man hit the dog det n vp
man hit the dog n vp
hit the dog vp
hit the dog tv np
the dog np
![Page 56: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/56.jpg)
an example
eg.
s → np, vp
np → det ,n
det → the
n → man
n → dog
vp → tv , np
tv → hit
parsing the man hit the dog (top of stack show
at left):
WORDS STACKthe man hit the dog s
the man hit the dog np vp
the man hit the dog det n vp
man hit the dog n vp
hit the dog vp
hit the dog tv np
the dog np
the dog det n
![Page 57: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/57.jpg)
an example
eg.
s → np, vp
np → det ,n
det → the
n → man
n → dog
vp → tv , np
tv → hit
parsing the man hit the dog (top of stack show
at left):
WORDS STACKthe man hit the dog s
the man hit the dog np vp
the man hit the dog det n vp
man hit the dog n vp
hit the dog vp
hit the dog tv np
the dog np
the dog det n
dog n
![Page 58: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/58.jpg)
an example
eg.
s → np, vp
np → det ,n
det → the
n → man
n → dog
vp → tv , np
tv → hit
parsing the man hit the dog (top of stack show
at left):
WORDS STACKthe man hit the dog s
the man hit the dog np vp
the man hit the dog det n vp
man hit the dog n vp
hit the dog vp
hit the dog tv np
the dog np
the dog det n
dog n
![Page 59: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/59.jpg)
an example
eg.
s → np, vp
np → det ,n
det → the
n → man
n → dog
vp → tv , np
tv → hit
parsing the man hit the dog (top of stack show
at left):
WORDS STACKthe man hit the dog s
the man hit the dog np vp
the man hit the dog det n vp
man hit the dog n vp
hit the dog vp
hit the dog tv np
the dog np
the dog det n
dog n
SUCCEED
![Page 60: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/60.jpg)
What about rule choice ?
suppose the grammar:
s --> np,vp
s --> sadv,s
np --> [john]np --> det,n
np --> n
vp --> iv
iv --> [walks]sadv --> [maybe]det --> [the]n --> [man]n --> [men]
![Page 61: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/61.jpg)
What about rule choice ?
suppose the grammar:
s --> np,vp
s --> sadv,s
np --> [john]np --> det,n
np --> n
vp --> iv
iv --> [walks]sadv --> [maybe]det --> [the]n --> [man]n --> [men]
maybe john walks is rejected
![Page 62: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/62.jpg)
What about rule choice ?
suppose the grammar:
s --> np,vp
s --> sadv,s
np --> [john]np --> det,n
np --> n
vp --> iv
iv --> [walks]sadv --> [maybe]det --> [the]n --> [man]n --> [men]
maybe john walks is rejected
WORDS STACKmaybe john walks s
![Page 63: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/63.jpg)
What about rule choice ?
suppose the grammar:
s --> np,vp
s --> sadv,s
np --> [john]np --> det,n
np --> n
vp --> iv
iv --> [walks]sadv --> [maybe]det --> [the]n --> [man]n --> [men]
maybe john walks is rejected
WORDS STACKmaybe john walks s
maybe john walks np vp
![Page 64: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/64.jpg)
What about rule choice ?
suppose the grammar:
s --> np,vp
s --> sadv,s
np --> [john]np --> det,n
np --> n
vp --> iv
iv --> [walks]sadv --> [maybe]det --> [the]n --> [man]n --> [men]
maybe john walks is rejected
WORDS STACKmaybe john walks s
maybe john walks np vp
maybe john walks det n vp
![Page 65: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/65.jpg)
What about rule choice ?
suppose the grammar:
s --> np,vp
s --> sadv,s
np --> [john]np --> det,n
np --> n
vp --> iv
iv --> [walks]sadv --> [maybe]det --> [the]n --> [man]n --> [men]
maybe john walks is rejected
WORDS STACKmaybe john walks s
maybe john walks np vp
maybe john walks det n vp
a dead end
![Page 66: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/66.jpg)
Parsing CFGs with Stacks
Top Down Parsing
What about rule choice ?
◮ Often more than one move will be possible
![Page 67: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/67.jpg)
Parsing CFGs with Stacks
Top Down Parsing
What about rule choice ?
◮ Often more than one move will be possible
◮ So need either
![Page 68: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/68.jpg)
Parsing CFGs with Stacks
Top Down Parsing
What about rule choice ?
◮ Often more than one move will be possible
◮ So need either
◮ a mechanism for exploring all choices – backtracking
![Page 69: The general parsing task Top Down Parsing · Top Down Parsing A top-down parser will effectively seek to settle the questions ’is the input an x’ by deriving a tree in a succession](https://reader033.vdocuments.net/reader033/viewer/2022042517/5f496bd0328eb625ee6376b2/html5/thumbnails/69.jpg)
Parsing CFGs with Stacks
Top Down Parsing
What about rule choice ?
◮ Often more than one move will be possible
◮ So need either
◮ a mechanism for exploring all choices – backtracking◮ or a way to guide choices correctly by referring to something other than just
the top of the stack