Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Expression Language Problem(Combine bool and arithmetic expressions with grouping (parenthesis))
Expression Language Problem [message #686037] Tue, 21 June 2011 13:19 Go to next message
gerit is currently offline geritFriend
Messages: 4
Registered: July 2009
Junior Member
Hi,
I want to create a grammar which combines bool and arithmetic expressions with grouping (parenthesis) like:
( 1 == 2 and ( 1 < ( 1+2) ) and not(2 > 3) ).
My actual grammar (based on the example in the docu for Expression + Expression) has the problem that '1 AND 2' would be valid, too.
Is there a way to fix this in the grammar or is an additional validation like the IConcretSytaxValidator required? What about the content-proposal if the 'additional' validator must be used?

I found several similar grammars in the forum but it seems they have the same 'problem'...

//
//short version with combined expressions for and|or and +|-|*|/ ...
//

ExpressionBoolBinary returns OperationBool:
    ExpressionBoolUnary ({OperationBool.left=current}
    op=('AND'|'OR')
    right=ExpressionBoolBinary)?
    ;

ExpressionBoolUnary returns OperationBool:
    ('NOT')? unary=TerminalExpressionBool
    ;

//would be the terminal if bool only...
TerminalExpressionBool returns Operation:
    ExpressionBoolCompare |
    {BoolLiteral}  value = ('TRUE'|'FALSE')
    ;


ExpressionBoolCompare returns Operation:
    Expression ({Operation.left=current}
    op=('=='|'<'|'>')
    right=ExpressionBoolCompare)?
    ;

Expression returns Operation: 
    TerminalExpression ({Operation.left=current}
    op=('+'|'-'|'*'|'/')
    right=Expression)?
    ;

TerminalExpression returns Operation:
  '(' ExpressionBoolBinary ')' |
  {Function} func=('sin'|'cos') '(' param=IntLiteral ')'|
  literal=IntLiteral
;

IntLiteral: value = INT;


many thanks
gerit
Re: Expression Language Problem [message #686040 is a reply to message #686037] Tue, 21 June 2011 13:29 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13615
Registered: July 2009
Senior Member
Hi,

this would be something for a type system (Validator), not for a grammar. e.g http://code.google.com/a/eclipselabs.org/p/xtext-typesystem/

~Christian


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Expression Language Problem [message #686632 is a reply to message #686040] Wed, 22 June 2011 16:18 Go to previous messageGo to next message
gerit is currently offline geritFriend
Messages: 4
Registered: July 2009
Junior Member
Hi Christian and thanks for the answer but if I understand the idea behind the typesystem right this could 'only' help me to write the additional validator.
But the additional validation has still the disadvantage of 'wrong' content proposals like:
1 + 1 {openContentPropasal:[ AND|OR|NOT|+|-... ]} and not only [+|-|*|/|...].


thanks, gerit
Re: Expression Language Problem [message #691372 is a reply to message #686632] Fri, 01 July 2011 10:22 Go to previous message
Timo Missing name is currently offline Timo Missing nameFriend
Messages: 12
Registered: March 2011
Junior Member
i'm using something to validate code very similar to the one you wrote in the first post.
the following code is simplyfied to only support INT values, in my case i could be almost anything (calculations have a standalone rule in my DSL).

BinaerOp		: '==' | '>=' | '<=' | '<' | '>' | '//' | '!=';
BoolOp			: '&' | '|';

// If-Conditions 
Condition:	SimpleCondition | CapsulatedConditionSequence;
SimpleCondition: (op1+=ConditionElement BinaerOp op2+=ConditionElement) | {ecore} INT; // | {ecore} Variable | Calculation [...] simplyfied here
ConditionElement: {ecore} INT; // again, very simplyfied
ConditionSequence: base=Condition sequence+=(ConditionConcatenation)*;
ConditionConcatenation: BoolOp Condition;
CapsulatedConditionSequence: '(' ConditionSequence ')';


i didn't test it after simplifying, maybe you have to tweak it a bit.
using my grammar, the following code has a syntax error at the last '&'

if (1==1 & (1==2 | (1==1 & 2==2)) & 1 & 2) then {};


good luck Smile
Previous Topic:Eclipse editor gets slow as hell...
Next Topic:Showing Details/Descriptions in Content Assist
Goto Forum:
  


Current Time: Wed Sep 30 10:00:01 GMT 2020

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

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

Back to the top