Академический Документы
Профессиональный Документы
Культура Документы
Fall 2011
Roche (USNA)
Fall 2011
1/9
Set Definitions
Given any CFG in BNF (no alternation | or Kleene ,+), we can construct
the following sets.
Each set contains only tokens, and FIRST(A), FOLLOW(A), and
PREDICT(A) are defined for every non-terminal A in the language.
EPS: All non-terminals that could expand to , the empty string
FIRST(A): The set of tokens that could appear as the first token in
an expansion of A
FOLLOW(A): The set of tokens that could appear immediately after
A in an expansion of S, the start symbol.
PREDICT(A): The set of tokens that could appear next in a valid
parse of a string in the language, when the next non-terminal in the
parse tree is A.
Each set is defined using mutual recursion.
Roche (USNA)
Fall 2011
2/9
Do we need FIRST?
For top-down parsing, the only sets we really need are the PREDICT sets.
It turns out the FIRST sets arent necessary to compute PREDICT.
So (for now) we will forget about FIRST and just worry about the other
three (EPS, PREDICT, FOLLOW).
Roche (USNA)
Fall 2011
3/9
EPS
Roche (USNA)
Fall 2011
4/9
PREDICT
Roche (USNA)
Fall 2011
5/9
FOLLOW
Definition: FOLLOW(A) contains all tokens that could come right after A
in a valid parse.
FOLLOW(A) contains:
1
There is also the special rule for the start symbol that
FOLLOW(S) always contains $, the end-of-file token.
Roche (USNA)
Fall 2011
6/9
S exp STOP
exp term exptail
exptail | OPA term exptail
term sfactor termtail
termtail | OPM factor termtail
sfactor OPA factor | factor
factor NUM | LP exp RP
Roche (USNA)
Fall 2011
7/9
Roche (USNA)
PREDICT
FOLLOW
$
Fall 2011
8/9
PREDICT
FOLLOW
$
Roche (USNA)
Fall 2011
8/9
PREDICT
FOLLOW
$
OPA
OPM
OPA
NUM, LP
Roche (USNA)
Fall 2011
8/9
PREDICT
FOLLOW
$
STOP,RP
OPA
OPM
OPA
NUM, LP
Roche (USNA)
Fall 2011
8/9
PREDICT
OPA
FOLLOW
$
STOP,RP
STOP,RP
OPM
OPA
NUM, LP
Roche (USNA)
Fall 2011
8/9
PREDICT
OPA,STOP,RP
FOLLOW
$
STOP,RP
STOP,RP
OPM
OPA
NUM, LP
Roche (USNA)
Fall 2011
8/9
PREDICT
OPA,STOP,RP
FOLLOW
$
STOP,RP
STOP,RP
OPA,STOP,RP
OPM
OPA
NUM, LP
Roche (USNA)
Fall 2011
8/9
PREDICT
OPA,STOP,RP
OPM
OPA
NUM, LP
FOLLOW
$
STOP,RP
STOP,RP
OPA,STOP,RP
OPA,STOP,RP
Roche (USNA)
Fall 2011
8/9
PREDICT
OPA,STOP,RP
OPM,OPA,STOP,RP
OPA
NUM, LP
FOLLOW
$
STOP,RP
STOP,RP
OPA,STOP,RP
OPA,STOP,RP
Roche (USNA)
Fall 2011
8/9
PREDICT
OPA,STOP,RP
OPM,OPA,STOP,RP
OPA
NUM, LP
FOLLOW
$
STOP,RP
STOP,RP
OPA,STOP,RP
OPA,STOP,RP
OPM,OPA,STOP,RP
OPM,OPA,STOP,RP
Roche (USNA)
Fall 2011
8/9
PREDICT
OPA,NUM,LP
OPA,NUM,LP
OPA,STOP,RP
OPA,NUM,LP
OPM,OPA,STOP,RP
OPA,NUM,LP
NUM, LP
FOLLOW
$
STOP,RP
STOP,RP
OPA,STOP,RP
OPA,STOP,RP
OPM,OPA,STOP,RP
OPM,OPA,STOP,RP
Roche (USNA)
Fall 2011
8/9
PREDICT
OPA,NUM,LP
OPA,NUM,LP
OPA,STOP,RP
OPA,NUM,LP
OPM,OPA,STOP,RP
OPA,NUM,LP
NUM, LP
FOLLOW
$
STOP,RP
STOP,RP
OPA,STOP,RP
OPA,STOP,RP
OPM,OPA,STOP,RP
OPM,OPA,STOP,RP
Roche (USNA)
Fall 2011
8/9
Roche (USNA)
Fall 2011
9/9