1

I have removed left-recursion from a left-recursive grammar given to me. The original grammar is as follows:

SPRIME::= Expr eof
Expr::= Term | Expr + Term | Expr - Term
Term::= Factor | Term * Factor | Term / Factor | Term mod Factor | Term div factor
Factor::= id | { Expr } | num | Funcall |
Funcall::= id [ Arglist ]
Arglist::= Expr | Expr , Arglist

When removing left-recursion, this is the grammar I produced:

SPRIME::= Expr eof
Expr::= Term Expr'
Expr'::= e | + Term Expr' | - Term Expr'
Term::= Factor Term'
Term'::= e | * Factor Term' | / Factor Term' | mod Factor Term' | div Factor Term'
Factor::= id | { Expr } | num | Funcall
Funcall::= id [ Arglist ]
Arglist::= Expr Arglist'
Arglist'::= , Arglist | e

My next task is to perform left-factoring on this grammar in order to make it LL(1). Having read the relevant chapter in the Dragon book, I'm unsure if I need to do anything to this grammar. My question is: is this grammar in LL(1) form already? And if not, where do I need to perform left-factoring in order to make it LL(1)?

EDIT: After taking @suddnely_me's answer into account, I have edited the Arglist non-terminal in order to left-factor it's productions. Is the grammar I have now an LL(1) grammar?

Richard Stokes
  • 3,532
  • 7
  • 41
  • 57

3 Answers3

2

No, this grammar is not LL(1). At least, the last rules group is not left factored, since FIRST( Expr) and FIRST( Expr, Arglist) do interstect.

iehrlich
  • 3,572
  • 4
  • 34
  • 43
  • So would modifying the grammar in the following fashion transform it to LL(1)? – Richard Stokes Apr 10 '11 at 11:39
  • At first glance, yes. As I remember from my academic years, the best way to check if the grammar is LL(1) is to build LL(1) analyzer of it. There are common algorythms to do it. – iehrlich Apr 10 '11 at 12:27
0

No, still (even a year later!) not LL(1). My oracle says:

Can't choose between two rules of 'Factor'
    -> id
    -> Funcall

'Funcall' can start with id.  For example:
    Funcall -> id ...
Ron Burk
  • 6,058
  • 1
  • 18
  • 20
0
                     if it is in the form of     A->A.ALPHA|BETA 
                        REMOVING LEFT RECURSION     A->BETA A'|
                       A'->ALPHA A'|NULL          
                       EXAMPLE:::A->A+B|a
                       IN THIS A=A A'=A'
                        ALPHA=+B
                        BETA=a
                    REMOVING LEFT RECURSION  :   A=aA'
                        A'=+BA'|NULL
srija
  • 1