The problem arises with the following expressions:

1) arr.length * arr[5]

2) arr[2*3]

Using the following SDF rules, the first expression is ambigous:

Exp "[" Exp "]" -> Exp {cons("ArrayAccess")}

Exp "*" Exp -> Exp {left, cons("Product")}

The following precedence rule ensures that the intended tree is selected:

Exp "[" Exp "]" -> Exp > Exp "*" Exp -> Exp

The second expression has only a single valid parse tree and shouldn’t be affected by the precedence rule. Without the precedence rule, it is parsed. But the new precedence rule causes an parse error at *. In my understanding, precedence rules should only be applied when there is more than one valid parse tree. Did I get this wrong?

BTW, the problem can be fixed with the following SDF rules:

Exp "[" IndexExp "]" -> Exp {cons("ArrayAccess")}

Exp "*" Exp -> Exp {left, cons("Product")}

Exp -> IndexExp

and precedence rule

Exp "[" IndexExp "]" -> Exp > Exp "*" Exp -> Exp

Submitted by Guido Wachsmuth on 17 September 2010 at 09:57

On 18 September 2010 at 13:55 Lennart Kats commented:

Hi Guido, unfortunately this is by design (although possibly forced by the implementation, but it’s been there from the start AFAIK). If you have some production A and B and you say A > B, then that means that you cannot nest B in A. So, in you’re case you’re saying that you cannot nest multiplication inside array access expressions… Adding an additional production like in your example works around this. IIRC the Java-front grammar also does something like this for the ?: operator.


On 18 September 2010 at 13:55 Lennart Kats closed this issue.

Log in to post comments