










































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











































































Department of Computer ScienceUniversity of San Francisco
03-0:
a sequence of tokens == determining if the string of tokens could be generated by aContext-Free Grammar.
03-2:
03-3:
A Recursive Descent Parser is implemented as a suiteof recursive functions, one for each non-terminal in thegrammar:
ParseS
will terminate normally if the next tokens in the input stream can be derived from thenon-terminal
ParseL
will terminate normally if the next tokens in the input stream can be derived from thenon-terminal
ParseE
will terminate normally if the next tokens in the input stream can be derived from thenon-terminal
03-5:
These recursive descent parsers are also known asLL(1) parsers, for Left-to-right, Leftmost derivation, with1 symbol lookahead
The input file is read from left to right (starting withthe first symbol in the input stream, andproceeding to the last symbol). The parser ensures that a string can be derived bythe grammar by building a leftmost derivation. Which rule to apply at each step is decided uponafter looking at just 1 symbol.
03-6:
Ch A^ →
ef A^ →
ǫ B^ →
hg C^ →
fi D^ →
g ParseS
use rule
on e, h
use the rule
Ch on f, g
03-8:
First(
S) is the set of all terminals that can start strings derived from
(plus
ǫ, if
can produce
ǫ)
First(
′S) = {e, f, g, h}
First(
S) = {e, f, g, h}
Ch
First(
A) = {e,
ǫ}
ef^
First(
B) = {h}
ǫ^
First(
C) = {f, g}
hg^
First(
D) = {g}
fi D^ →
g
03-9:
We can expand the definition of First sets to includestrings of terminals and non-terminals′^ S
First(a
First(
) =
Ch
First(
AbC
) =
ef^
First(
) =
ǫ^
First(ab
hg^
First(
) =
fi D^ →
g
03-11:
S, set First(
For each rule of the form
γ, add First(
γ) to
First(
Repeat until no changes are made
03-12:
Example:′^ S
Ch A^ →
ef A^ →
ǫ B^ →
hg C^ →
fi D^ →
g
03-14:
Follow(
S) is the set of all terminals that can follow
in
a (partial) derivation.′^ S
Follow(
Follow(
Ch
Follow(
A) = {h}
ef^
Follow(
ǫ^
Follow(
C) = {h}
hg^
Follow(
D) = {h, g}
fi D^ →
g
03-15:
S, set Follow(
For each rule of the form
γ
For each non-terminal
in^ γ, where
γ^ is of the
form
αS
β 1 If First(
β) does not contain
ǫ, add all
elements of First(
β) to Follow(
If First(
β) does contain
ǫ, add all elements of
First(
β)^
except
ǫ^ to Follow(
S), and add all^1
elements of Follow(
S) to Follow(
If any changes were made, repeat.
03-17:
γ^ goes in row
S, in all columns of
First(
γ). If First(
γ) contains
ǫ, then the rule
γ^ goes
in row
S, in all columns of Follow(
03-18:
First(
′ S) = {e, f, g, h}
Follow(
First(
S) = {e, f, g, h}
Follow(
Ch
First(
A) = {e,
ǫ}^
Follow(
A) = {h}
ef^
First(
B) = {h}
Follow(
ǫ^
First(
C) = {f, g}
Follow(
C) = {h}
hg^
First(
D) = {g}
Follow(
D) = {h, g}
fi D^ →
g