Grammar with a post-fix operator [message #1854494] |
Mon, 22 August 2022 13:29  |
Simon Cockx Messages: 45 Registered: October 2021 |
Member |
|
|
I'm trying to describe an expression language that supports a kind of postfix operator. I'm struggling with how this can be expressed in XText. As a MWE, the language should support parsing two kinds of expressions:
1. projections of the form <expression> -> <attribute>, and
2. existence checks for an attribute of the form <expression> -> <attribute> exists.
Examples of the language:
This should be parsed into a ProjectionExpression with expression set to variable and attribute set to attr.
This should be parsed into a ExistsExpression with expression set to variable and attribute set to attr.
variable -> attr -> nestedAttr exists
This should be parsed into a ExistsExpression with expression set to a ProjectionExpression (corresponding to variable -> attr) and attribute set to nestedAttr.
Now I tried something like the grammar below, but obviously this does not work as XText will always create a ProjectionExpression if it encounters a ->, and never consider ExistsExpression as an alternative. Is there a workaround for this?
Note: I want a clear distinction between an ExistsExpression and a ProjectionExpression in the parsed AST. I do not want to join these concepts in any way. (otherwise, it would be easy to make exists a boolean attribute of the ProjectionExpression, but for downstream processes, I would like to treat them as separate classes)
Expression: ExistsExpression;
ExistsExpression returns Expression:
ProjectionExpression (=>({ExistsExpression.expression=current} '->' attribute=[Attribute] 'exists'))?
;
ProjectionExpression returns Expression:
PrimaryExpression (=>({ProjectionExpression.expression=current} '->' attribute=[Attribute]))*
;
PrimaryExpression returns Expression:
VariableReference
;
VariableReference returns Expression:
{VariableReference} reference=[Attribute]
;
Attribute:
name=ID
;
[Updated on: Mon, 22 August 2022 17:17] Report message to a moderator
|
|
|
|
|
|
|
Re: Grammar with a post-fix operator [message #1854918 is a reply to message #1854912] |
Sun, 18 September 2022 09:00   |
Simon Cockx Messages: 45 Registered: October 2021 |
Member |
|
|
Hi Ed
It seems that you arrived at the same conclusion as me. :) The solution I came up with is exactly that: start with a loose "lower level" syntax, and convert that in a post-processing step into a "higher level" AST that is more convenient to process, e.g., for the type system or for code generators.
Note that I wouldn't have these strict requirements if I was just working in a small team on a small language. That is not the case however. To be precise, I'm working on the Rosetta DSL, which is an open source project supporting multiple code generators (currently 8, but that number is growing) and a fairly complex type system using the Xsemantics framework. Making the syntax as convenient as possible is therefore necessary: the more I can do in the first step, the less that all of these downstream tasks need to worry about. Joining AST concepts such as projections and exists expressions into one breaks the single responsibility principle, which worried me.
Thank you for your interesting analysis. Always open to discussion.
Regards
Simon Cockx
[Updated on: Sun, 18 September 2022 09:08] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.02300 seconds