Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Using arrays Arrays and DotNotation in Expressions
Using arrays Arrays and DotNotation in Expressions [message #1793622] Sun, 12 August 2018 06:15 Go to next message
Christoff Erasmus is currently offline Christoff ErasmusFriend
Messages: 32
Registered: December 2016
Member
Hello.

I am trying to update my DSL to handle arrays and dot notation.
Having tried multiple different ways, and still being stuck, I am reaching out for some guidance.

Grammer on GitHub:
ExpressionDSL.xtext

Unit Test on GitHub (extract below):
ExpressionDSLParsingTest.xtend - testExpressionArray05()

Request:
Please help me change the DSL to be able to pass the unit test (below).

MWE2 - Error Message
1) [fatal] rule ruleArrayExp has non-LL(*) decision
2) Decision can match input using multiple alternatives:
error(211): ../org.xtext.example.expressiondsl/src-gen/org/xtext/example/expressiondsl/parser/antlr/internal/InternalExpressionDSLParser.g:892:2: [fatal] rule ruleArrayExp has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2.  Resolve by left-factoring or using syntactic predicates or using backtrack=true option.
warning(200): ../org.xtext.example.expressiondsl/src-gen/org/xtext/example/expressiondsl/parser/antlr/internal/InternalExpressionDSLParser.g:1171:4: Decision can match input such as "RULE_ID" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
error(201): ../org.xtext.example.expressiondsl/src-gen/org/xtext/example/expressiondsl/parser/antlr/internal/InternalExpressionDSLParser.g:1171:4: The following alternatives can never be matched: 2


Unit Test Extract:
	var int intA;
	var int intB dim(10);

	struct structA;
		subf int subfA;
	endstruct;

	struct structB dim(10);
		subf int subfB;
	endstruct;

	struct structC dim(10);
		subf int subfC dim(20);
		struct subStructC dim(30);  // Nested Struct Array
			subf int subSubfC dim(40);
		endstruct;
	endstruct;

	def char funcTest;

	// Normal Assignment
	intA = intB;

	// Assign result of function
	intA = funcTest();
	intA = funcTest(1); 
	intA = funcTest(2:3);

	// Arrays
	intA = intB(4);
	intA = intB(funcTest(5));

	// Qualified
	intA = structA;
	intA = structA.subfA;

	intA = structB(5);
	intA = structB(testFunc());

	intA = structB(6).subfB;

	intA = stuctC(7).subfC(8);
	intA = structC(9).subStructC(10).subSubf(11);
Re: Using arrays Arrays and DotNotation in Expressions [message #1793626 is a reply to message #1793622] Sun, 12 August 2018 10:22 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 12386
Registered: July 2009
Senior Member
Please provide a minimal grammar.
Please solve the obvious ambiguity

A=ID |B=ID

First


Need professional support for Xtext, Xpand, EMF?
Go to: https://xtext.itemis.com
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using arrays Arrays and DotNotation in Expressions [message #1793627 is a reply to message #1793626] Sun, 12 August 2018 11:58 Go to previous messageGo to next message
Christoff Erasmus is currently offline Christoff ErasmusFriend
Messages: 32
Registered: December 2016
Member
Christian Dietrich wrote on Sun, 12 August 2018 10:22
Please provide a minimal grammar.
Please solve the obvious ambiguity

A=ID |B=ID

First


The ambiguity, relates to the way I am trying to implement the ArrayExp,
it is conflicting with the "FunctionCall" rule.
Both uses the token sequence "ID '(' ')'"

Minimal Grammar:
Quote:

grammar org.xtext.example.expressiondsl.ExpressionDSL with org.eclipse.xtext.common.Terminals

generate expressionDSL "http://www.xtext.org/example/expressiondsl/ExpressionDSL"

Model:
statements+=Statement*;

Statement:
VariableDef |
ConstDef |
StructDef |
FunctionDef |
FunctionCallStatement |
VariableAssignment
;

VariableDef:
'var' type=TYPE name=ValidID (options=Dim)? ';'
;

ConstDef:
'val' type=TYPE name=ValidID (options=Dim)? ';'
;

StructDef:
'struct' name=ValidID (options=Dim)? ';'
subFields+=(SubField)*
'endstruct' ';'
;

SubFieldDef:
('subf')? type=TYPE name=ValidID (options=Dim)? ';'
;

SubField:
SubFieldDef | StructDef
;

FunctionDef:
'def' type=TYPE name=ValidID ';'
;

VariableAssignment:
tgtvar=[VariableDef]
op=('=' | '+=' | '-=' | '*=' | '/=' | '**=' )
exp=Expression ';'
;
FunctionCallStatement:
'call'? call=FunctionCall ";"
;

FunctionCall:
=> ref=[FunctionDef]
('('
(params+=Expression (':' params+=Expression)*)?
')')
;

// ArrayExpOption1
//ArrayExp returns Expression:
// Primary (
// => ( {ArrayExp.left=current} '(' arrayDimensions=Expression ')' )
// )?
//;

//ArrayExpOption2
//ArrayExp:
// => ref=[Variable] '(' index=Expression ')'
//;

//ArrayExpOption3
// https://www.eclipse.org/forums/index.php/t/503752/
ArrayExp:
VariableArray |
StructArray |
SubFieldArray
;
VariableArray returns ArrayExp:
{VariableArray} => ref=[VariableDef] '(' arrayDimensions=Expression ')'
;
StructArray returns ArrayExp:
{StructArray} => ref=[StructDef] '(' arrayDimensions=Expression ')'
;
SubFieldArray returns ArrayExp:
{SubFieldArray} => ref=[SubFieldDef] '(' arrayDimensions=Expression ')'
;

DotExpression returns Ref:
=> StructArray ({DotExpression.ref=current} "." (tail=[SubFieldArray] | tail=[StructArray]) )*
;
//--------------------------------------------------------------------------
Expression: Or;
Or returns Expression:
UnaryPlusMinusOrNot (
{Or.left=current} 'OR'
right=And
)*;
UnaryPlusMinusOrNot returns Expression:
FunctionExp |
( ({UnaryPlus} '+' | {UnaryMinus} '-' | {Not} 'NOT') expr=UnaryPlusMinusOrNot )
;
FunctionExp returns Expression:
// ArrayExp |
FunctionCall|
Primary
;
Primary returns Expression:
DotExpression |
ArrayExp |
// QualifiedName |
'(' Expression ')' |
Atomic
;
Atomic returns Expression:
{IntConstant} value=INT |
{StringConstant} value=STRING |
{BooleanConstant} value=BOOL
;
//--------------------------------------------------------------------------
VariableOrConstRef:
VariableDef |
ConstDef |
StructDef |
SubField
;

Variable:
VariableDef |
StructDef |
SubField
;


Dim:
//Dimensions of Array (as in how many elements)
'DIM' '(' arrayDimensions=INT ')'
;

//--------------------------------------------------------------------------
// Helper
// https://dslmeinte.wordpress.com/2014/09/11/ambiguitiy-in-xtext-grammars-part-2/
Named:
VariableDef |
ConstDef |
StructDef |
SubFieldDef |
FunctionDef
;

ValidID:
ID
;

//QualifiedName:
// ArrayExp ({ArrayExp.left=current} '.' tail=ArrayExp)*
//;

terminal TYPE:
'int' | 'char' | 'bool'
;
terminal BOOL:
'true' | 'false'
;
Re: Using arrays Arrays and DotNotation in Expressions [message #1793628 is a reply to message #1793627] Sun, 12 August 2018 12:11 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 12386
Registered: July 2009
Senior Member
if an array and a function call is synt. the same they have to be the same in grammar and ast

Need professional support for Xtext, Xpand, EMF?
Go to: https://xtext.itemis.com
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using arrays Arrays and DotNotation in Expressions [message #1793629 is a reply to message #1793628] Sun, 12 August 2018 12:13 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 12386
Registered: July 2009
Senior Member
and i was referign to

ArrayExp:
VariableArray |
StructArray |
SubFieldArray
;
VariableArray returns ArrayExp:
{VariableArray} => ref=[VariableDef] '(' arrayDimensions=Expression ')'
;
StructArray returns ArrayExp:
{StructArray} => ref=[StructDef] '(' arrayDimensions=Expression ')'
;
SubFieldArray returns ArrayExp:
{SubFieldArray} => ref=[SubFieldDef] '(' arrayDimensions=Expression ')'
;


we have learned that ref=[XXXXXX] is short for ref=[XXXXXX|ID] which means the parser sees it at

ref=ID

=>
you have

A= B|C;
B: ID;
C; ID;
which is ambigous for sure


Need professional support for Xtext, Xpand, EMF?
Go to: https://xtext.itemis.com
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using arrays Arrays and DotNotation in Expressions [message #1793662 is a reply to message #1793629] Mon, 13 August 2018 12:50 Go to previous messageGo to next message
Christoff Erasmus is currently offline Christoff ErasmusFriend
Messages: 32
Registered: December 2016
Member
Thanks for the Double reply.
Yes, the ambiguity in Option3 is my bad, should have sticked with Option2.

The issue I am facing is more related with Arrays vs Functions references in expressions

Array has one "parameter", while a Function can have Zero or More "parameters". Also Arrays can be a Qualified Name (aka DotNotation).

Arrays:
Array(INT) or Array(Expression)
Struct(INT) or Struct(INT).Subfield(INT) or Struct(INT).subStruct(INT)

Functions:
Function or Function() or Function(Parm1) or Function(Parm1:Parm2)

My current though is to collapse Variables, Arrays, QualifiedName and Functions into the "QualifiedRef" rule (refer code snippet below) and then do all the validations/scoping/proposals/outline with Xtend. But this feels like the wrong approach.

Attempt5 FAILED:
Option5 (refer to the code snippet below) does not pickup the QualifiedName rule (ref Screen Shot)

//ArrayExpOption5:
ParmSEP:
	':'
;
QualSEP:
	'.'
;
VariableOrArrayOrFunc:
	ref=[Named|ValidID]
	( '(' 
		(
			params+=Expression (ParmSEP params+=Expression)* //One or More optional Parameters '(params1:params2)'
		)?   //Optional Parameter '(params)'
	')')? //Optional '()'
;
QualifiedRef:
	VariableOrArrayOrFunc ({QualifiedRefX.head=current} QualSEP tail=VariableOrArrayOrFunc)*
;

FunctionExp returns Expression:
QualifiedRef |
Primary
;
Primary returns Expression:
'(' Expression ')'	|
Atomic
;


index.php/fa/33630/0/

Question:
For the LoveOfGoodness how do I do this! I am so frustrated with Expressions.
I know its not easy, but DUDE... this is getting out of hand. He says trying not to cry while curled up in a ball under his Desk.

[Updated on: Mon, 13 August 2018 12:53]

Report message to a moderator

Re: Using arrays Arrays and DotNotation in Expressions [message #1793664 is a reply to message #1793662] Mon, 13 August 2018 14:27 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 12386
Registered: July 2009
Senior Member
you have no other change unless you introduce a different syntax

Need professional support for Xtext, Xpand, EMF?
Go to: https://xtext.itemis.com
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using arrays Arrays and DotNotation in Expressions [message #1793716 is a reply to message #1793664] Tue, 14 August 2018 14:01 Go to previous messageGo to next message
Christoff Erasmus is currently offline Christoff ErasmusFriend
Messages: 32
Registered: December 2016
Member
Christian Dietrich wrote on Mon, 13 August 2018 14:27
you have no other change unless you introduce a different syntax


Unfortunately I can not change the syntax.

But surely xtext can handle this syntax as it is similair to Xtend's var.func.func(parm).
Re: Using arrays Arrays and DotNotation in Expressions [message #1793717 is a reply to message #1793716] Tue, 14 August 2018 14:03 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 12386
Registered: July 2009
Senior Member
yes but xxxx() xxxxx(1) xxxxx(1,2) is all the same in xtend.
xtend does not differentiate

xxxx(1) => array
xxxx() => function
xxxx("Hello") => function
xxxxx(1,2) => function

=> you cannot distinguish at parse time


Need professional support for Xtext, Xpand, EMF?
Go to: https://xtext.itemis.com
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:How to order the elements in the Xtext Content Assist ?
Next Topic:Trigger validation manually
Goto Forum:
  


Current Time: Fri Nov 16 03:33:51 GMT 2018

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

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

Back to the top