11-Syntax_part3
11-Syntax_part3
Spring Semester
Natural Language
Processing
Dr. Wafaa Samy
Dr. Hanaa Eissa
Syntax (Part 3)
Lecture (11)
2
Contents
• Parsing
o Top-Down Parsing
o Bottom-Up Parsing
3
Parsing
• Parsing means taking an input and producing some
sort of structure for it .
• Syntactic parsing is the task of recognizing a sentence
and assigning a syntactic structure to it.
• Parsing with Context free grammars refers to the task
of assigning proper parse trees to input strings.
o Proper here means a parse tree that covers all and only
the elements of the input and has an S at the top.
• It doesn’t actually mean that the system can select the
correct parse tree from among all the possible trees.
4
Search Strategies
• How can we use grammar (e.g. the grammar
shown) to assign the correct parse tree to an
input string (e.g. Book that trip)?
5
Search Strategies (Cont.)
1. Top-down or goal-directed search.
2. Bottom-up or data-directed search.
Stream of words
Parser Parse tree(s)
Context-free grammar
6
(1) Top-Down Parsing
• A top-down parser searches for a parse tree
by trying to build from the root node S down
to the leaves.
• Top-Down Search:
o Since we’re trying to find parse trees rooted with
an S (Sentences), why not start with the rules that
give us an S.
o Then we can work our way down from there to
the words.
7
Top Down Space
8
Sample L1 Grammar
9
Example (1)
The cat sat on the mat S NP VP
10
Example (1) (Cont.)
The cat sat on the mat S NP VP
NP Det Nominal
Det the
Nominal
the
11
Example (1) (Cont.)
The cat sat on the mat S NP VP
NP Det Nominal
Det the
Nominal Noun
Noun cat
Nominal
the
Noun
cat
12
Example (1) (Cont.)
The cat sat on the mat S NP VP
NP Det Nominal
Det the
Nominal Noun
Noun cat
VP V PP
V sat
Nominal
the sat
Noun
Nominal
on the
cat Noun
mat
13
Example (1) (Cont.)
The cat sat on the mat S NP VP
NP Det Nominal
Det the
Nominal Noun
Noun cat
VP V PP
V sat
Nominal PP Prep NP
Prep on
the sat
Noun
Nominal
on the
cat Noun
mat
14
Example (1) (Cont.)
The cat sat on the mat S NP VP
NP Det Nominal
Det the
Nominal Noun
Noun cat
VP V PP
V sat
Nominal PP Prep NP
Prep on
the sat NP Det Nominal
Noun Det the
Nominal
on the
cat
15
Example (1) (Cont.)
The cat sat on the mat S NP VP
NP Det Nominal
Det the
Nominal Noun
Noun cat
VP V PP
V sat
Nominal PP Prep NP
Prep on
the sat NP Det Nominal
Noun Det the
Nominal
on Nominal Noun
cat the Noun Noun mat
mat
16
Example (2)
S NP VP
NP Nominal
Nominal Noun
Noun time
VP V PP
Nominal V flies
PP Prep NP
Noun
flies
Prep like
NP Det
Time
Nominal
like Nominal Det an
an Noun Nominal Noun
Noun arrow
arrow
17
Problems with Top-Down Parsing
1. Left Recursion.
2. Structural Ambiguity.
18
Left Recursive
• A grammar is left-recursive, if it contains at least one non-
terminal A, such that: it has a derivation that includes itself
anywhere along its left most branch:
19
Immediate Left-Recursion
• The grammar has a rule: A → A α
• For example:
S → NP VP
NP → NP PP
VP → VP PP
20
Structural Ambiguity
• Occurs when the grammar assigns more than one possible
parse tree to a sentence.
21
Example (3): Structure Ambiguity
I shot an elephant in my pajamas
22
Example (3): Structure Ambiguity
(Cont.)
I shot an elephant in my pajamas
23
Example (4): Structure Ambiguity
Pronoun Pronoun
24
Example (4): Structure Ambiguity
(Cont.)
I saw the man with the telescope
Pronoun Pronoun
25
(2) Bottom-up Parsing
• Bottom-Up Parsing:
o Of course, we also want trees that cover the input words. So
we might also start with trees that link up with the words in the
right way.
o Then work your way up from there to larger and larger trees.
• In bottom-up parsing,
1. The parser starts with the words of the input sentence, and
2. Tries to build parse trees from the words up, again by applying
rules from the grammar one at a time.
27
Bottom-Up Search (Cont.)
28
Bottom-Up Search (Cont.)
29
Bottom-Up Search (Cont.)
30
Bottom-Up Search (Cont.)
31
Bottom-Up Search (Cont.)
32
Local Ambiguity
• Occurs when some part of a sentence is
ambiguous.
• For example, the sentence:
34
Top-Down and Bottom-Up
• Top-down:
o Helps with POS ambiguities – only consider relevant POS.
o Only searches for trees that can be answers (i.e. S’s).
o But also suggests trees that are not consistent with any of
the words.
o Spends a lot of time on impossible parses (trees that are
not consistent with any of the words).
• Bottom-up:
o Has to consider every POS.
o Only forms trees consistent with the words.
o But suggests trees that make no sense globally.
Spends a lot of time on useless structures (trees that make no
sense globally, or trees that will not start with S on the top).
35