Вы находитесь на странице: 1из 5

                                                   Compiler Construction

                                          To find out FIRST & FOLLOW set

                                                                  LL PARSER   

        The construction of a predictive parser is aided by two functions associated with a grammar called 
FIRST and FOLLOW. These functions allows us to fill the entries of the predictive parsing table.
                                                                                     
FIRST :
We associate each grammar symbol A with the set FIRST(A). The implication of this set is that the 
grammar symbol A can in some steps of transition produce the elements of the set FIRST(A). 
        If 'α' is any string of grammar symbols, let FIRST(α) be the set of terminals that begin the string 
derived from α . If  α=*=>є then add  є to FIRST(є).

RULES TO COMPUTE FIRST SET

1) If X is a terminal , then FIRST(X) is {X}
 2)  If X­­> є then add є  to FIRST(X)
 3)  If X is a nonterminal and X­­>Y1Y2Y3...Yn , then put 'a' in 
 FIRST(X) if for some i, a is in FIRST(Yi) and є is in all of 
 FIRST(Y1),...FIRST(Yi­1);
 In other words,  Y1...Yi­1=*=>є .
 If є is in all of FIRST(Yj) for all j=1,2....n, then add є to 
 FIRST(X).This is so because all Yi's produce є, so X definitely
produces є            

FOLLOW :
        FOLLOW is defined only for non terminals of the grammar G.
It can be defined as the set of terminals of grammar G , which can immediately follow the non terminal 
in a production rule from start symbol.    
             In other words, if A is a nonterminal, then FOLLOW(A) is the set of
    terminals 'a' that can appear immediately  to the right of A in some 
   sentential  form, i.e. The set of terminals 'a' such that there exists a 
   derivation of the form S=*=> αAaβ for some α and β (which can be
   strings ,or empty).
   
 RULES TO COMPUTE FOLLOW SET

1) If S is the start symbol, then add $ to the FOLLOW(S).
2) If there is a production rule A­­> αBβ then everything in FIRST(β)
            except for є  is placed in FOLLOW(B).
3) If there is a production A­­> αB , or a production  A­­> αBβ where
FIRST(β) contains є then everything in FOLLOW(A) is in FOLLOW(B).

     Prepared by: Paul PrOnabananda; Student of M.S in CSE NSU ID# 073764050
               Email:   paul.pronabananda@gmail.com/hotmail.com/yahoo.com
                               Cell: 01724843626; 01553466667; 01921144286
                                                   Compiler Construction
                                          To find out FIRST & FOLLOW set
   Example : Consider the following grammar
                       S­> aABe
                       A­­>Abc |  b
                       B­­> d
          Find the FIRST and FOLLOW for each nonterminal of the grammar.
         
  Solution : ­ 

          Steps : 
1) Find for every non terminal  if it is nullable.
2) Find FIRST for every nonterminal using rules described earlier. 
3) Using the information got from calculations in steps 1 and 2
one could calculate  FOLLOW for every nonterminal by rules described earlier.

      To calculate FIRST’s

           a)  To calculate FIRST(S) :
                 From rule   S­­>aABe   ,  we get 'a' belongs to FIRST(S) 
                  No other rule will help give an element in FIRST(S).
                 So,   FIRST(S)={a}

           b) To calculate FIRST(A) :
                 From rule  A­­>Abc  ,   we can't add any element 
                 From rule  A­­>b   , we know that 'b' belongs to FIRST(A)
                 No other rule will help give an element in FIRST(A).
                So,    FIRST(A)={b}

c) To calculate FIRST(B)
      From rule B­­>d  ,  we add 'd' to FIRST(B)
     No other rule will help give an element in FIRST(B).
                  So,   FIRST(B)={d}     

      To calculate FOLLOW’s

       a) To calculate FOLLOW(S)
            Since S is start symbol, add $ to FOLLOW(S)
            From rule S­­>aABe  , we don’t get any contribution to the 
            FOLLOW(S)          /*See rules 2 and 3  for FOLLOW*/ 
            From rule A­­>Abc   , since no where we see any symbol S, so  
            no contribution to FOLLOW(A) is found in this rule.
            From rule A­­>b , again no contribution.
            From rule B­­>d, again no contribution.
            Hence FOLLOW(S) ={$}
 
         b) To calculate FOLLOW(A) :

     Prepared by: Paul PrOnabananda; Student of M.S in CSE NSU ID# 073764050
               Email:   paul.pronabananda@gmail.com/hotmail.com/yahoo.com
                               Cell: 01724843626; 01553466667; 01921144286
                                                   Compiler Construction
                                          To find out FIRST & FOLLOW set
              From rules A­­>b , B­­>d   we get no contribution.
              From rule S­­>aABe    we expect to get some contribution.
              See rule 2 of forming FOLLOW(on page 2)
              As per that , we can add everything in FIRST(Be) in FOLLOW(A)
              except for epsilon.
                FIRST(Be)=FIRST(B) ={d}
              So add 'd' to FOLLOW(A).
              Since Be is not nullable, so we can't use the rule 3 (See page 2)
              For rule   A­­>Abc ,  we do get some contribution straight away.
              From rule 2(page 2), 'b' belongs to FOLLOW(A)
              No other rule will help give an element in FOLLOW(A).
              Hence  FOLLOW(A)={d,b} 

        c)  To calculate FOLLOW(B)
             Only rule S­­>aABe contributes.
             By rule 2 , 'e' belongs to FOLLOW(B).
Hence  FOLLOW(B)={e}
       

A -> alpha | beta | gamma |


delta
(compiler construction)
we must be able to tell which
Top down parsing (continued)
choice to take based on only ONE
token.
Last lecture we talked about re-
Let us today examine some im-
cursive descent parsing, which I
plications of this.
said was
the method of choice if you
If the correct alternative to
don't have an automated parsing
take at this point is an alpha,
tool (such as
then either
Yacc) to do the dirty work for
(1) the next token must be
you.
something that can begin an al-
pha, or
Recall that in order for RD to
(2) it must be legal for alpha
work, our grammar had to be
to match nothing, and the next
LL(1), which
token must
meant that any time we had a
be something that can follow an
choice of several alternatives,
A.
we must be
able to decide upon one altern-
Possibility (1) leds us into de-
ative based only on the NEXT in-
fining a relation, which we will
put token.
call FIRST.
FIRST(alpha) will be the set of
That is, if we have a choice,
symbols that can begin a string
such as
derived

     Prepared by: Paul PrOnabananda; Student of M.S in CSE NSU ID# 073764050
               Email:   paul.pronabananda@gmail.com/hotmail.com/yahoo.com
                               Cell: 01724843626; 01553466667; 01921144286
                                                   Compiler Construction
                                          To find out FIRST & FOLLOW set
starting from alpha (remember Now to construct FIRST(alpha),
alpha may contain tokens and/or there are three cases:
nonterminals).
alpha begins with a terminal - x
Possibility (2) leds us into de- , then first(alpha) consists of
fining a relation FOLLOW(A), only x.
which is the
set of symbols that can legally alpha begins with a nonterminal,
follow an A. Note that in this X -
case, we go compute first(X),
unlike the first, the "argument" and assign this to first(alpha).
is a single nonterminal. now if X is in the epsilon
set, we also add first(remainder)
I want to quickly go over the to first(alpha).
algorithm to compute first and
follow. alpha is epsilon - add epsilon
Really, the details are not all to first set.
that important. What you should
know is To compute the follow set of a
(1) what first and follow are, nonterminal X,
and look at all the places X is used
(2) how they are used. in the grammer.
The details of how to compute Compute what could come next
first and follow are given in (using the first set information)
the book in after we have seen the X. This
much more detail, if you really is the follow set.
want to see it.
(depending upon time, give rail-
The initial step is to discover road diagram form for grammar,
all the nonterminals that can and
derive intuition about first and follow
nothing. Let us call this the in terms of railroad charts).
EPSILON set.
Clearly anything that has an ep- Once we have first and follow,
silon production on the right we can rephrase the LL(1) re-
hand side is quirement, as
in the epsilon set, so we first follows:
scan the productions and add
these symbols. When we are faced with a choice
Next, we repeatedly do the fol- A -> alpha | beta | gamma
lowing: | delta
Scan the productions, The first sets of the choices
If anything has all must be distinct, and if any
nonterminals on the right hand choice can be
side, and empty than these must also be
if all those nonterminals distinct from the follow set for
are in the epsilon set, then A.
add the left hand side to
the epsilon set. We can also rephrase our con-
we do this until we have scanned struction rule for building a
the productions at least once procedure for
without recognzing nonterminals. In
adding anything to the epsilon this example, our procedure
set. would be

     Prepared by: Paul PrOnabananda; Student of M.S in CSE NSU ID# 073764050
               Email:   paul.pronabananda@gmail.com/hotmail.com/yahoo.com
                               Cell: 01724843626; 01553466667; 01921144286
                                                   Compiler Construction
                                          To find out FIRST & FOLLOW set
boolean procedure A()
if next token in first set
for alpha then
recognize alpha
else if next token in
first set for beta then
recognize beta
else if next token in
first set for gamma then
recognize gamma
else if next token in
first set for delta then
recognize delta
else
error

the error is replaced by "return


true", if epsilon is a legal
choice for
this nonterminal.

This all seems very straightfor-


ward. We can encode our choices
in a table,
indexed by nonterminals and ter-
minals and containing right hand
sides.

M[A,x] is "if you are looking


for an A, and the next token is
an x, then
this is the production to use."

Procedure is given on page 225.

How do we use this? Build a


push down automata with a stack
full of GOALS.
algorithm given on page 226.
This is our first example of a
table driven
parser.

Advantages of table driven pars-


ers:
(1) small and
(2) fast
disadvantages:
(1) hard to debug if you are do-
ing it by hand.

     Prepared by: Paul PrOnabananda; Student of M.S in CSE NSU ID# 073764050
               Email:   paul.pronabananda@gmail.com/hotmail.com/yahoo.com
                               Cell: 01724843626; 01553466667; 01921144286

Вам также может понравиться