I would like to parse an expression with parenthesis in python using textx.
For example the following DSL :
CREATE boby = sacha - ( boby & tralaa) ;
CREATE boby = sacha & boby - ( david & lucas )
This is the grammar I tried:
Model:
'CREATE' name=Identifier '=' exp=SetExpr
;
JoinOperator: /-/&/;
SetExpr:SetParExpr | SetBaseExpr
;
SetBaseExpr:
first=ID op=JoinOperator second=ID
;
SetParExpr:
'(' SetExpr ')'
I guess I should have a list somewhere to fill with expression. Do you have any suggestion ?
I've changed your examples just slightly: I added a semicolon to end and I put another pair of parentheses in your second example. I inferred these changes based on what you provided in your grammar. Here's the examples:
CREATE boby = sacha - ( boby & tralaa);
CREATE boby = sacha & (boby - ( david & lucas ));
To parse examples like these your grammar needs to be changed to:
Model
s (I created a Script
rule that takes semi colon separated models)second
property of the SetBaseExpr
rule to be an ID or a SetParExpr.Identifier
to ID
in the model rule (I assume this is what you meant).I made these changes and ended up with the following grammar that parses the examples I gave:
Script:
models+=Model[';'] ';'
;
Model:
'CREATE' name=ID '=' exp=SetExpr
;
JoinOperator: '-' | '&';
SetExpr:
SetParExpr | SetBaseExpr
;
SetBaseExpr:
first=ID op=JoinOperator (second=ID | second=SetParExpr)
;
SetParExpr:
'(' SetExpr ')'
;
I hope that answers your question or gives you a hint as to handle parenthetical expressions.