You are on page 1of 3

Grundlagen des Compilerbaus 5

Computation of First and Follow Sets


• Computation of first set for symbol from stack-alphabet:
first1(X), X ∈ N∪Σ: recursively apply these rules
1. X is a terminal
then first1(X) = {X}
2. If X à ε ∈ P
then place ε in first1(X)
3. X is a non-terminal with X à X1…Xn ∈ P, with n≥1
then for all a ∈ first1(Xi) i ≤ n and ε ∈ first1(Xj) for all j = 1…i-1
place a in first1(X)
• Computation of first set for string of terminals and non-
terminals: first1(X1X2…Xn), X1, X2, …, Xn ∈ N∪Σ
1. Place first1(X1) in first1(X1X2…Xn)
2. If first1(X1) contains ε, then place first1(X2) in first1(X1X2…Xn)
3. If first1(X1) and first1(X2) contain ε,
then then place first1(X3) in first1(X1X2…Xn)
4. and so on …
Prof. Christoph Bockisch (bockisch@mathematik.uni-marburg.de) | Programmiersprachen und –werkzeuge
Grundlagen des Compilerbaus 6

Computation of First and Follow Sets


• Computation of follow set follow1(A): recursively apply
these rules (until no follow set changes anymore):
1. ε ∈ follow1(S)
2. If there is a production A à %Bβ
then place everything in first1(β) \ { ε } into follow1(B)
3. If there is a production A à %B or A à %Bβ with ε ∈ first1(β)
then place everything in follow1(A) into follow1(B)

Prof. Christoph Bockisch (bockisch@mathematik.uni-marburg.de) | Programmiersprachen und –werkzeuge


Grundlagen des Compilerbaus 7

Example
• GAE: E à E + T | T
TàT*F|F
F à (E) | a
• Determine lookaheads for all productions
• First need to determine first sets for all right-hand
sides and follow sets for all non-terminals

Prof. Christoph Bockisch (bockisch@mathematik.uni-marburg.de) | Programmiersprachen und –werkzeuge

You might also like