Difference between revisions of "Top-Down Parsing/Example 3"

From Wiki**3

< Top-Down Parsing
(Solution)
(Solution)
Line 25: Line 25:
 
   E -> v | v x
 
   E -> v | v x
  
Now we handle the left corner (E in B) (E also becomes unreachable and can be removed from the grammar):
+
Now we handle the left corner (E in B) (since E is not completely replaced, it cannot be removed from the grammar):
  
 
   S -> u B z
 
   S -> u B z
   B -> B v | v u v | v x u v x | B y v | B y v x
+
   B -> B v | v u E | v x u E | B y E
   <font color="red">E -> v | v x</font>
+
   E -> v | v x
  
 
Now, left recursion can be eliminated:
 
Now, left recursion can be eliminated:
  
 
   S  -> u B z
 
   S  -> u B z
   B  -> v u v B1 | v x u v x B1
+
   B  -> v u E B1 | v x u E B1
   B1 -> v B1 | y v B1 | y v x B1 | ε
+
   B1 -> v B1 | y E B1 | ε
  
 
Factoring...
 
Factoring...
Line 41: Line 41:
 
   S  -> u B z
 
   S  -> u B z
 
   B  -> v B2
 
   B  -> v B2
   B1 -> v B1 | y v B3 | ε
+
   B1 -> v B1 | y E B1 | ε
  B2 -> u v B1 | x u v x B1
+
   B2 -> u E B1 | x u E B1
  B3 -> B1 | x B1
 
 
 
Eliminating the left corner in B3 (note that we do not completely replace B1, since a new factorization would be needed):
 
 
 
  S  -> u B z
 
  B  -> v B2
 
  B1 -> v B1 | y v B3 | ε
 
   B2 -> u v B1 | x u v x B1
 
  B3 -> v B1 | y v B3 | x B1 | ε
 
  
 
The FIRST and FOLLOW sets are as follows:
 
The FIRST and FOLLOW sets are as follows:
Line 57: Line 48:
 
   FIRST(S)  = FIRST(u B z) = {u}
 
   FIRST(S)  = FIRST(u B z) = {u}
 
   FIRST(B)  = FIRST(v B2) = {v}
 
   FIRST(B)  = FIRST(v B2) = {v}
   FIRST(B1) = FIRST(v B1) ∪ FIRST(y v B3) ∪ {ε} = {v, y, ε}
+
   FIRST(B1) = FIRST(v B1) ∪ FIRST(y E B1) ∪ {ε} = {v, y, ε}
   FIRST(B2) = FIRST(u v B1) ∪ FIRST(x u v x B1) = {u, x}
+
   FIRST(B2) = FIRST(u E B1) ∪ FIRST(x u E B1) = {u, x}
  FIRST(B3) = FIRST(v B1) ∪ FIRST(y v B3) ∪ FIRST(x B1) ∪ {ε} = {v, x, y, ε}
 
  
   FOLLOW(S) = {$}    FOLLOW(B1) ⊇ FOLLOW(B2)    FOLLOW(B2) ⊇ FOLLOW(B)    FOLLOW(B3) ⊇ FOLLOW(B1)
+
   FOLLOW(S) = {$}    FOLLOW(B1) ⊇ FOLLOW(B2)     
   FOLLOW(B) = {z}    FOLLOW(B1) ⊇ FOLLOW(B3)
+
   FOLLOW(B) = {z}    FOLLOW(B2) ⊇ FOLLOW(B)
 
   
 
   
   FOLLOW(B1) = FOLLOW(B2) = FOLLOW(B3) = FOLLOW(B) = {z}  
+
   FOLLOW(B1) = FOLLOW(B2) = FOLLOW(B) = {z}
  
 
[[category:Compilers]]
 
[[category:Compilers]]
 
[[category:Teaching]]
 
[[category:Teaching]]

Revision as of 22:36, 11 April 2008

Problem

Consider the following grammar, where S is the initial symbol and {u,v,x,y,z} is the set of terminal symbols:

 S -> u B z
 B -> B v | D
 D -> E u E | B y E
 E -> v | v x
  1. Examine the grammar and rewrite it so that an LL(1) predictive parser can be built for the corresponding language.
  2. Compute the FIRST and FOLLOW sets for all non-terminal symbols in the new grammar and build the parse table.
  3. Show the analysis table (stack, input, and actions) for the parsing process of the uvuvxz input sequence.

Solution

The grammar can be parsed by an LL(1) parser if it does not have left recursion and no ambiguity is present (i.e., the LOOKAHEADs for all productions of each non-terminal are disjoint).

A simple inspection of the grammar shows that indirect left recursion is present in rules B and D. Also, there are left corners that may hide ambiguity (E).

The first step, then, is to rewrite the grammar so that mutual recursion is eliminated (A becomes unreachable and can be removed from the grammar):

 S -> u B z
 B -> B v | E u E | B y E
 D -> E u E | B y E
 E -> v | v x

Now we handle the left corner (E in B) (since E is not completely replaced, it cannot be removed from the grammar):

 S -> u B z
 B -> B v | v u E | v x u E | B y E
 E -> v | v x

Now, left recursion can be eliminated:

 S  -> u B z
 B  -> v u E B1 | v x u E B1
 B1 -> v B1 | y E B1 | ε

Factoring...

 S  -> u B z
 B  -> v B2
 B1 -> v B1 | y E B1 | ε
 B2 -> u E B1 | x u E B1

The FIRST and FOLLOW sets are as follows:

 FIRST(S)  = FIRST(u B z) = {u}
 FIRST(B)  = FIRST(v B2) = {v}
 FIRST(B1) = FIRST(v B1) ∪ FIRST(y E B1) ∪ {ε} = {v, y, ε}
 FIRST(B2) = FIRST(u E B1) ∪ FIRST(x u E B1) = {u, x}
 FOLLOW(S) = {$}    FOLLOW(B1) ⊇ FOLLOW(B2)    
 FOLLOW(B) = {z}    FOLLOW(B2) ⊇ FOLLOW(B)

 FOLLOW(B1) = FOLLOW(B2) = FOLLOW(B) = {z}