Professional Documents
Culture Documents
J. Xue
& %
COMP3131/9102 Page 197 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 198 March 22, 2010
' $
J. Xue
Scanner
NOUN1 VERB ARTICLE NOUN2
Parser
hsentencei
hsubjecti hpredicatei
hNOUNi VERB hobjecti
PETER PASSED ARTICLE NOUN
THE TEST
& %
COMP3131/9102 Page 199 March 22, 2010
' $
J. Xue
Lookahead Token(s)
& %
COMP3131/9102 Page 201 March 22, 2010
' $
J. Xue
T REE hsentencei
hsubjecti hpredicatei
↑
I NPUT NOUN
↑
Notations:
• ↑ on the tree indicates the nonterminal being expanded or recognised
• ↑ on the sentence points to the lookahead token
– All tokens to the left of ↑ have been read
– All tokens to the right of ↑ have NOT been processed
& %
COMP3131/9102 Page 202 March 22, 2010
' $
J. Xue
T REE hsentencei
hsubjecti hpredicatei
NOUN
↑
I NPUT NOUN
↑
T REE hsentencei
hsubjecti hpredicatei
↑
NOUN
I NPUT NOUN VERB
↑
& %
COMP3131/9102 Page 203 March 22, 2010
' $
J. Xue
T REE hsentencei
hsubjecti hpredicatei
T REE hsentencei
hsubjecti hpredicatei
& %
COMP3131/9102 Page 204 March 22, 2010
' $
J. Xue
T REE hsentencei
hsubjecti hpredicatei
NOUN VERB hobjecti
ARTICLE NOUN
↑
I NPUT NOUN VERB ARTICLE
↑
T REE hsentencei
hsubjecti hpredicatei
NOUN VERB hobjecti
ARTICLE NOUN
↑
I NPUT NOUN VERB ARTICLE NOUN
↑
& %
COMP3131/9102 Page 205 March 22, 2010
' $
J. Xue
Top-Down Parsing
• Build the parse tree starting with the start symbol (i.e., the
root) towards the sentence being analysed (i.e., leaves).
• Use one token of lookahead, in general
• Discover the leftmost derivation
I.e, the productions used in expanding the parse tree
represent a leftmost derivation
& %
COMP3131/9102 Page 206 March 22, 2010
' $
J. Xue
T REE
I NPUT NOUN
↑
T REE hsubjecti
NOUN
I NPUT NOUN
↑
T REE hsubjecti
NOUN
I NPUT NOUN VERB
↑
& %
COMP3131/9102 Page 208 March 22, 2010
' $
J. Xue
T REE hsubjecti
NOUN
I NPUT NOUN VERB ARTICLE
↑
T REE hsubjecti
NOUN
I NPUT NOUN VERB ARTICLE NOUN
↑
& %
COMP3131/9102 Page 209 March 22, 2010
' $
J. Xue
ARTICLE NOUN
I NPUT NOUN VERB ARTICLE NOUN
↑
T REE hsentencei
hsubjecti hpredicatei
ARTICLE NOUN
I NPUT NOUN VERB ARTICLE NOUN
↑
& %
COMP3131/9102 Page 210 March 22, 2010
' $
J. Xue
Bottom-Up Parsing
• Build the parse tree starting with the the sentence being
analysed (i.e., leaves) towards the start symbol (i.e., the
root).
• Use one token of lookahead, in general.
• The basic (smallest) language constructs recognised first,
then they are used to discover more complex constructs.
• Discover the rightmost derivation in reverse — the
productions used in expanding the parse tree represent a
rightmost derivation in reverse order
& %
COMP3131/9102 Page 211 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 213 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 214 March 22, 2010
' $
J. Xue
First(α):
• The set of all terminals that can begin any strings derived
from α.
• if α=⇒∗ ǫ, then ǫ is also in First(α)
Nullable Nonterminals
& %
COMP3131/9102 Page 216 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 217 March 22, 2010
J. Xue
' $
eBCd=⇒
S=⇒ ABCd f BCd=⇒ gCd=⇒
BCd=⇒
hCd=⇒ pd
Cd=⇒
qd
First(ABCd) = {e, f, g, h, p, q}
& %
COMP3131/9102 Page 218 March 22, 2010
J. Xue
' $
eBC=⇒
S=⇒ ABC f BC=⇒ gC=⇒
BC=⇒
hC=⇒ p
C=⇒ q
ǫ
First(ABC) = {e, f, g, h, p, q, ǫ}
& %
COMP3131/9102 Page 219 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 220 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 221 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 222 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 223 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 224 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 225 March 22, 2010
' $
J. Xue
Follow(E) = {+, −, ), $}
Follow(T ) = Follow(F ) = {+, −, ∗, /, ), $}
& %
COMP3131/9102 Page 226 March 22, 2010
' $
J. Xue
Follow(E) = {), $}
Follow(Q) = {), $}
Follow(T ) = {+, −, ), $}
Follow(R) = {+, −, ), $}
Follow(F ) = {+, −, ∗, /, ), $}
& %
COMP3131/9102 Page 227 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 228 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 229 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 230 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 240 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 241 March 22, 2010
' $
J. Xue
Left Recursion
• Direct left-recursion:
A → Aα
• Non-direct left-recursion:
A → Bα
B → Aβ
– Algorithm 4.1 of text eliminates both kinds of left recursion
– In real programming languages, non-direct left-recursion is rare
– Not required
• A grammar with left recursion is not LL(1)
& %
COMP3131/9102 Page 246 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 247 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 248 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 250 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 251 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 252 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 254 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 255 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 257 March 22, 2010