0

I have a dangling-else problem in Coco/R. I try to understand the Coco/R User Manual and I ask Google, but I can't solve the problem on my own.

I simplified my problem to the following Coco/R grammar (saved in new4.atg):

COMPILER Expr 
CHARACTERS

    digit   = '0'..'9'. 
    letter  = 'A'..'Z'.

TOKENS

    number  = digit { digit }.
    name    = letter { digit | letter }.    

PRODUCTIONS

    Expr = Test | Id Test.  
    Test = Test2.   
    Test2=Id | "(" Test ")".        
    Id=IdName|IdNumber.         
    IdName = name.  
    IdNumber = number.      

END Expr.

When I want to build the compiler with coco.bat, I get this answer:

Coco/R (Dec 22, 2014)
checking
new 4.atg(15,1): LL1 warning in Expr: number is start of several alternatives
new 4.atg(15,1): LL1 warning in Expr: name is start of several alternatives
parser + scanner generated
0 errors detected

In the best result in Google, I have read that I can declare IF(isXXXFollowYYY()) statement, but I don't how and if it's the best solution. (In my example: Expr = Test | If(isTestFollowID)Id Test. But where is isTestFollowID declared?)

I want to get no warnings, when I start coco.bat.

Palec
  • 12,743
  • 8
  • 69
  • 138
Eric
  • 1
  • 1

1 Answers1

0

Your grammar is ambiguous.

From Expr, upon seeing an Id token, the parser can go either

Expr -> Test -> Test2 -> Id

or

Expr -> Id

An LL(1) parser will not know which path to take.

The immediate problem can be fixed by pulling Id out into a lead-in optional component in your Expr production:

Instead of

Expr = Test | Id Test.  

you can do

Expr = [Id] Test . 

However the Test production can also match an Id at its beginning, so it appears that further refactorings will be needed to make the grammar LL(1).

  • I have test your solution. `Expr = [Id] Test.`. The result is the same. (_LL1 warning in Expr: number is start & successor of deletable structure_) What further refactorings will be needed? – Eric Sep 13 '16 at 06:54