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
.
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).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.