3

I already wasted two much time on converting it, but I always get up getting common prefix ID.

Can anyone explain it to me? as I am trying to do it for a very large grammar and need my basics clear.

A, B, C, D are the only Non-Terminals.

A : ‘(‘ B ‘)’ 
 | ID ASSIGN C 
 | C 

C : C '+' D 
 | C '-' D 
 | D 

D : ID 
 | ID '(' actuals ')' 
 | ID '(' ')' 
 | INT_LIT 
 | ‘(‘ C ‘)’ 


B : B ';' A | A

1 Answers1

1

In LL, a production can't have multiple options starting with the same terminal, so you pull those common parts into a shared head, if you will. So

D : ID 
 | ID '(' actuals ')' 
 | ID '(' ')' 
 | INT_LIT 
 | ‘(‘ C ‘)’ 

becomes something along the lines of

D : D_things_that_start_with_ID
 | D_things_that_do_not_start_with_ID

where

D_things_that_start_with_ID :
  ID D_things_that_follow_ID

D_things_that_follow_ID :
  epsilon
  | '(' actuals ')' 
  | '(' ')' 

D_things_that_do_not_start_with_ID :
 INT_LIT 
 | ‘(‘ C ‘)’ 

and so on for other common lead symbols.

  • I already tried it but the problem is once you start doing it from the first non-terminal it goes into a loop. Please try to do from the first one and you will understand my problem. thanks – user3058126 Mar 11 '14 at 01:11
  • Oh, right - you'll have to eliminate left-recursion. How to do that has been covered numerous times here on SO, and of course elsewhere as well. – 500 - Internal Server Error Mar 11 '14 at 01:24