FIRST and FOLLOW

  1. FIRST(a)={a} for all terminals a.
  2. Initialize FIRST(A)=φ for all nonterminals A
  3. If A → ε is a production, add ε to FIRST(A).
  4. For each production A → Y1 ... Yn,
    1. add to FIRST(A) any terminal a satisfying
      1. a is in FIRST(Yi) and
      2. ε is in all previous FIRST(Yj).
    2. add ε to FIRST(A) if ε is in all FIRST(Yj).
    Repeat this entire step until nothing is added.
  5. FIRST of any string X=X1X2...Xn is initialized to φ and then
    1. add to FIRST(X) any non-ε symbol in FIRST(Xi) if ε is in all previous FIRST(Xj).
    2. add ε to FIRST(X) if ε is in every FIRST(Xj). In particular if X is ε, FIRST(X)={ε}.
  6. 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.
    1. For every production A → α B β, add all of FIRST(β) except ε to FOLLOW(B).
    2. For every production A → α B, add all of FOLLOW(A) to FOLLOW(B).
    3. For every production A → α B β where FIRST(β) contains ε, add all of FOLLOW(A) to FOLLOW(B).

PREDICTIVE PARSING TABLE

For each production A → α

  1. For each terminal a in FIRST(α), add A → α to M[A,a].
  2. 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

  1. If A→α·bβ is in Ii for b a terminal, then ACTION[i,b]=shift j, where GOTO(Ii,b)=Ij.
  2. If A→α· is in Ii, for A≠S', then, for all b in FOLLOW(A), ACTION[i,b]=reduce A→α.
  3. If S'→S· is in Ii, then ACTION[I,$]=accept.