Lab 5 - Finishing the LL(1) Table and the Parser


Objectives

The objectives of this laboratory exercise are

Preparing for Lab

No special preparations needed.

To Do

This day is primarily a catch up day.

The Algorithms

If you need them, here they are. Remember that algorithms are for computers. When you understand how the First and Follow algorithms work, you can generally "eyeball" your grammar and figure most of it out.

First


let ε stand for the empty string
loop for each nonterminal A
   set First(A) = {} -- set First(A) to the empty set.
                     -- note that the empty set does not contain anything, 
                     -- not even ε
end loop

loop for each terminal a
   set First(a) = {a}
end loop

loop for each rule in the grammar
   if the rule is A ⟶ ε then
      set First(A) = {ε}
   end if
end loop

loop while this is the first time through the loop, or any additions have
           been made to any set First(A) for all A during the previous pass
           through the loop
   loop through each non-ε rule in the grammar
      -- the current rule being processed has the general form A --> X1 X2...Xn
      -- where A is the nonterminal on the left, and each Xi represents a single
      -- symbol on the right, either a terminal or nonterminal. 
      k := 1
      loop while k ≤ n
         First(A) = First(A) ∪ (First(Xk) - {ε})
         if ε is  First(Xk) then
            exit this loop
         end if
         k := k + 1
      end loop

      -- at this point we have finished examining the right hand side of the
      -- current rule.  If we have discovered that every symbol on the right hand
      -- side can derive ε, then that means that A can derive ε as well, so we
      -- must add ε to First(A).  If not every symbol on the right hand side of
      -- this rule can derive ε, then neither can A through application of this
      -- rule, so we should not include ε in First(A) (although it might already
      -- be there for some other reason, and will not be removed
      if k > n and ε ∈ First(Xn) then
         set First(A) := First(A) ∪ {ε}
      end if
   end loop through
end loop while
-- at this point, first(A) has been computed for every nonterminal A, and
-- First(a) has been set to {a} for each terminal a.

Follow


    for each nonterminal A in grammar G
     Follow(A) := {}
end

for loop
     Follow(S) := {eof}, where S is the start symbol of G
    while there are changes to any Follow set during the previous pass through the loop loop 
       for each rule A ⟶ X1 X2 ... Xn in grammar G loop
           for each Xi in the current rule that is a nonterminal loop
               Follow(Xi) := Follow(Xi)  First(Xi+1 Xi+2 ... Xn) - {ε}
               -- if i = n, First(Xi+1 Xi+2 ... Xn) = {ε}(the set containing the empty string)
               if ε is in First(Xi+1 Xi+2 ... Xn) then
                  Follow(Xi) := Follow(Xi)  Follow(A)
               end if      
           end for
       end for
    end while 

Predict

 

To Turn In

Put your current LL(1) table in the Lab 5 dropbox. .