Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Grammar: operation with optional left argument
Grammar: operation with optional left argument [message #1856178] Thu, 24 November 2022 11:15
Simon Cockx is currently offline Simon CockxFriend
Messages: 69
Registered: October 2021
Member
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]

Report message to a moderator

Previous Topic:Subset of Xtext grammar
Next Topic:Language server is not loading resource and doing build with lsp4j 0.15
Goto Forum:
  


Current Time: Thu Apr 25 04:59:17 GMT 2024

Powered by FUDForum. Page generated in 0.02365 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top