2

I'm new to LL(1) parsing and am currently trying to figure out how to create a parse table for the language:

 S → Saa|b

and

 S-> A|B
 A-> aa
 B-> bb

and

 S → AB
 A → aa
 B → bb
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
Anitun
  • 43
  • 4

1 Answers1

3

I'll show you the complete procedure for the first grammar. Tell me if there are any aspects you would like to examine in greater depth.

S → Saa | b

1. Eliminate left recursion

S → bS'

S' → aaS' | ε

2. Left factor

No need for this grammar.

3. Compute FIRST and FOLLOW and check if the grammar is LL(1)

------------------------------------
|   A → α   | FIRST(α) | FOLLOW(A) |
------------------------------------
|  S → bS'  |     a    |      $    |
------------------------------------
| S' → aaS' |     a    |      $    |
------------------------------------
|   S' → ε  |     ε    |      $    |
------------------------------------

4. Build the parsing table

for each prodution A → α
  for each a ∈  FIRST(α)
    add production A → α to M[A,a]
  if ε ∈ FIRST(α) then
    for each b ∈ FOLLOW(A)
      add A → α to M[A,b]

Result:

-------------------------------------
|    |    a     |    b     |   $    |
-------------------------------------
| S  |          |  S → bS' |        |
-------------------------------------
| S' | S → aaS' |          | S' → ε |
-------------------------------------

The grammar is LL(1). Here the results of the other grammars.

1.
-------------------------------------
|    |    a     |    b     |   $    |
-------------------------------------
| S  |   S → A  |  S → B   |        |
-------------------------------------
| A  |  A → aa  |          |        |
-------------------------------------
| B  |          |  B → bb  |        |
------------------------------------- 

2.
-------------------------------------
|    |    a     |    b     |   $    |
-------------------------------------
| S  |   S → AB |          |        |
-------------------------------------
| A  |  A → aa   |         |        |
-------------------------------------
| B  |          |  B → bb  |        |
------------------------------------- 
Stefano Nardo
  • 1,567
  • 2
  • 13
  • 23