FIRST and FOLLOW
- FIRST(a)={a} for all terminals a.
- Initialize FIRST(A)=φ for all nonterminals A
- If A --> ε is a production, add ε to FIRST(A).
- For each production A --> Y1 ... Yn,
add to FIRST(A) any terminal a satisfying
- a is in FIRST(Yi) and
- ε is in all previous FIRST(Yj).
Repeat this step until nothing is added.
- FIRST of any string X=X1X2...Xn
is initialized to φ and then
- add to FIRST(X) any non-ε
symbol in FIRST(Xi) if ε is in all previous
FIRST(Xj).
- add ε to FIRST(X) if ε is in every
FIRST(Xj).
In particular if X is ε, FIRST(X)={ε}.
- Initialize FOLLOW(S)=$ and FOLLOW(A)=φ for all other
nonterminals A, and then apply the following three rules until
nothing is added to any FOLLOW set.
- For every production A --> α B β,
add all of FIRST(β) except ε to FOLLOW(B).
- For every production A --> α B,
add all of FOLLOW(A) to FOLLOW(B).
- For every production A --> α B β
where FIRST(β) contains ε,
add all of FOLLOW(A) to FOLLOW(B).
PREDICTIVE PARSING TABLE
For each production A --> α
- For each terminal a in FIRST(α), add A --> α
to M[A,a].
- If ε is in FIRST(α), then add
A --> α to M[A,b] (resp. M[A,$]) for each
terminal b in FOLLOW(A) (if $ is in FOLLOW(A)).
SLR ACTION TABLE
- If A-->α·bβ is in Ii for
b a terminal, then ACTION[i,b]=
shift j
, where
GOTO(Ii,b)=Ij.
- If A-->α· is in Ii, for
A≠S', then, for all b in FOLLOW(A),
ACTION[i,b]=
reduce A-->α
.
- If S'-->S· is in Ii, then
ACTION[I,$]=
accept
.