Grammar with a post-fix operator [message #1854494] |
Mon, 22 August 2022 09:29  |
Eclipse User |
|
|
|
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 13:17] by Moderator
|
|
|
|
|
|
|
Re: Grammar with a post-fix operator [message #1854918 is a reply to message #1854912] |
Sun, 18 September 2022 05:00   |
Eclipse User |
|
|
|
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 05:08] by Moderator
|
|
|
Re: Grammar with a post-fix operator [message #1854919 is a reply to message #1854918] |
Sun, 18 September 2022 07:07  |
Eclipse User |
|
|
|
Hi
My insights come from my PhD that amongst other things resulted in the only yaccable grammar for C++; I identified a superset grammar that could defer type resolution till parsing had completed.
Currently I struggle with OCL for which the specification advocates a user/grammar-friendly Concrete Syntax that is significantly distant from the information-friendly Abstract Syntax necessitating a non-trivial CS2AS conversion.
You appear to need only a minor CS2AS conversion and so long as you can keep it simple that can work well. IMHO the naive assumption that useful languages can have a single CS/AS is naive, although I keep trying to see how OCL could have a smaller more manageable distance.
Once you accept the need for a (minor) CS2AS you can move on and solve problems effectively rather than fudging the consequences of no CS2AS.
Regards
Ed Willink
|
|
|
Powered by
FUDForum. Page generated in 0.10671 seconds