Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
J. Xue
Jingling Xue
School of Computer Science and Engineering
The University of New South Wales
Sydney, NSW 2052, Australia
http://www.cse.unsw.edu.au/~cs3131
http://www.cse.unsw.edu.au/~cs9102
Copyright @2010, Jingling Xue
& %
COMP3131/9102 Page 194 March 22, 2010
' $
J. Xue
Assignment 1
• See the FAQs, feedback and test cases
• The marks are provisional – see the plagiarism policy
• The functionality of the scanner:
* When called, always returns the longest prefix of the
remaining input that can be interpreted as a token
* if the current char cannot begin a token, then:
- skip it if it is a whilespace
- return an error token otherwise
& %
COMP3131/9102 Page 195 March 22, 2010
' $
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 232 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 234 March 22, 2010
' $
J. Xue
void parseA() {
parse α
}
& %
COMP3131/9102 Page 235 March 22, 2010
' $
J. Xue
Coding parse αi
& %
COMP3131/9102 Page 236 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 237 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 238 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 239 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 240 March 22, 2010
' $
J. Xue
& %
COMP3131/9102 Page 241 March 22, 2010
' $
J. Xue
void parseA() {
switch (currentToken.kind) {
cases in First(A→α1 )
parse α1
break;
···
cases in First(A→αn−1 )
parse αn−1
break;
default: /* A→αn as the default (item 4, p. 193/229, Red/Purple Dragon */
parse αn
break;
}
}
• Suppose αn =⇒∗ ǫ is the only nullable alternative
• Then Select(A→αi ) = First(αi ) for 1 6 i < n
• In fact, the coding still correct even if all are not nullable!
& %
COMP3131/9102 Page 242 March 22, 2010
' $
J. Xue
& %
} /* this does not work */
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
' $
J. Xue
Assigmment 2
& %
COMP3131/9102 Page 258 March 22, 2010
' $
J. Xue
Reading
• Chapter 2
• Red Dragon: Pages 176 – 178 and 188 – 190
• Purple Dragon: $4.3.3 – 4.3.4 and Pages 217 – 226
Week 5:
• Pages 190 – 191 (Red) and Pages 226 – 228 (Purple)
• More on recursive-descent
• LL(1) table-driven parsing
• Error recovery
& %
COMP3131/9102 Page 259 March 22, 2010