Grammar: operation with optional left argument [message #1856178] |
Thu, 24 November 2022 11:15 |
Eclipse User |
|
|
|
I have an expression language containing many operations such as
True or False
[1, 2, 2, 3] distinct
[1 ,2, 3] map { item + 1 }
I would like the left operand of all of these operations to become optional, e.g., these should be parsed as valid expressions:
or False
distinct
map { item + 1 }
Unfortunately, simply adding a question mark '?' results in an error "An action is not allowed in fragments and when the current may still be unassigned".
To keep things simple, let's take the following dummy example:
Expr: Add;
Add returns Expr:
Mul (=>{Add.left=current} '+' right=Mul)*
;
Mul returns Expr:
Prim (=>{Mul.left=current} '*' right=Prim)*
;
Prim returns Expr:
{IntLiteral} value=INT
;
Writing something like `Mul? (=>{Add.left=current} '+' right=Mul)*` doesn't work for the reason explained above.
As an alternative, I've tried the following solution, but this requires much duplication, which I would like to avoid:
Expr: Add;
Add returns Expr:
Mul (=>{Add.left=current} '+' right=Mul)*
| ->({Add} '+' right=Mul) (=>{Add.left=current} '+' right=Mul)*
;
Mul returns Expr:
Prim (=>{Mul.left=current} '*' right=Prim)*
| ->({Mul} '*' right=Prim) (=>{Mul.left=current} '*' right=Prim)*
;
Prim returns Expr:
{IntLiteral} value=INT
;
Is there any way to make the left operand of such an operation optional without having to duplicate things?
[Updated on: Thu, 24 November 2022 11:16] by Moderator Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.04961 seconds