Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Parsing Exponent Expression
Parsing Exponent Expression [message #1777341] Tue, 28 November 2017 16:10 Go to next message
Christoff Erasmus is currently offline Christoff ErasmusFriend
Messages: 21
Registered: December 2016
Junior Member
Hello.
I am strugging to parse an exponent expression.
My issue is that the "nested" exponent expression is not being parsed.
Please refer to the attached screenshots for detail.

Request:
What changes do I need to make to the grammer (below) to be able to parse the nested exponent expression?


Addtional Information:

Precedence of expression:

  • ()
  • **
  • *, /
  • =, <>, >, >=, <, <=
  • AND
  • OR


	@Test def void testExponentiation() {
		"2**3**4".assertRepr("(2 ** (3 ** 4))")
	}

	def private String stringRepr(Expression e) {
		switch (e) {
			Plus: '''(«e.left.stringRepr» + «e.right.stringRepr»)'''
			Minus: '''(«e.left.stringRepr» - «e.right.stringRepr»)'''
			MulOrDiv: '''(«e.left.stringRepr» «e.op» «e.right.stringRepr»)'''
			Exponent: '''(«e.left.stringRepr» ** «e.right.stringRepr»)'''
			Comparison: '''(«e.left.stringRepr» «e.op» «e.right.stringRepr»)'''
			Equality: '''(«e.left.stringRepr» «e.op» «e.right.stringRepr»)'''
			And: '''(«e.left.stringRepr» AND «e.right.stringRepr»)'''
			Or: '''(«e.left.stringRepr» OR «e.right.stringRepr»)'''
			Not: '''(NOT «e.expression.stringRepr»)'''
			IntConstant: '''«e.value»'''
			StringConstant: '''«e.value»'''
			BoolConstant: '''«e.value»'''
		}.toString
	}


Model:
	expressions+=AbstractExpression*
;

AbstractExpression:
	'EVAL' 'foo' '=' expression=Expression ';';

Expression: Or;

Or returns Expression:
	And (
		{Or.left=current} 'OR'
		right=And
	)*;

And returns Expression:
	Equality (
		{And.left=current} 'AND'
		right=Equality
	)*;

Equality returns Expression:
	Comparison (
		{Equality.left=current} op=('=' | '<>')
		right=Comparison
	)*;

Comparison returns Expression:
	PlusOrMinus (
		{Comparison.left=current} op=('>=' | '<=' | '>' | '<')
		right=PlusOrMinus
	)*;

PlusOrMinus returns Expression:
	MulOrDiv (
		({Plus.left=current} '+' | {Minus.left=current} '-')
		right=MulOrDiv
	)*;

MulOrDiv returns Expression:
	Exponent (
		({MulOrDiv.left=current} op=('*' | '/'))
		right=Exponent
	)*;

Exponent returns Expression:
	Primary (
		{Exponent.left=current} '**' right=Primary)?;
		
Primary returns Expression:
	'(' Expression ')' |
	{Not} 'NOT' expression=Primary |
	Atomic;

Atomic returns Expression:
	{IntConstant} value=INT	|
	{StringConstant} value=STRING |
	{BoolConstant} value=('*ON' | '*OFF') ;
Re: Parsing Exponent Expression [message #1777349 is a reply to message #1777341] Tue, 28 November 2017 17:13 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 12108
Registered: July 2009
Senior Member
Why questionmark

Exponent returns Expression:
Primary (
{Exponent.left=current} '**' right=Primary)?;


Need professional support for Xtext, Xpand, EMF?
Go to: https://xtext.itemis.com
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Parsing Exponent Expression [message #1777453 is a reply to message #1777349] Wed, 29 November 2017 13:28 Go to previous messageGo to next message
Christoff Erasmus is currently offline Christoff ErasmusFriend
Messages: 21
Registered: December 2016
Junior Member
@Christian. Well spotted.

Changed the "?" to "*" now it does parse the expression but the nesting is in the wrong order ( refer attached screenshot). I need to change the "association" from left to right, but I am unsure how to. Can you advise?

Exponent returns Expression:
	Primary (
		{Exponent.left=current} '**'
		right=Primary
	)*;

[Updated on: Wed, 29 November 2017 13:30]

Report message to a moderator

Re: Parsing Exponent Expression [message #1777456 is a reply to message #1777453] Wed, 29 November 2017 13:37 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 12108
Registered: July 2009
Senior Member
then you have to do it the other way round e.g

Exponent returns Expression:
Primary

(
{Exponent.left=current} '**'
right=Exponent
)?;


Need professional support for Xtext, Xpand, EMF?
Go to: https://xtext.itemis.com
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Parsing Exponent Expression [message #1777460 is a reply to message #1777456] Wed, 29 November 2017 14:02 Go to previous messageGo to next message
Christoff Erasmus is currently offline Christoff ErasmusFriend
Messages: 21
Registered: December 2016
Junior Member
Thanks. The change worked, parsing as expected. All other unit tests also passed.
But... During the generation of the Langauge 2 warnings are generated (reference screenshot below).

Should I be concerned about them?

[Updated on: Wed, 29 November 2017 14:03]

Report message to a moderator

Re: Parsing Exponent Expression [message #1777461 is a reply to message #1777460] Wed, 29 November 2017 14:03 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 12108
Registered: July 2009
Senior Member
do you have a complete grammar as is now?

Need professional support for Xtext, Xpand, EMF?
Go to: https://xtext.itemis.com
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Parsing Exponent Expression [message #1777463 is a reply to message #1777461] Wed, 29 November 2017 14:09 Go to previous messageGo to next message
Christoff Erasmus is currently offline Christoff ErasmusFriend
Messages: 21
Registered: December 2016
Junior Member
The grammar is not final yet. Guessing only 30% done.
Re: Parsing Exponent Expression [message #1777464 is a reply to message #1777463] Wed, 29 November 2017 14:12 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 12108
Registered: July 2009
Senior Member
i need something to reproduce

Need professional support for Xtext, Xpand, EMF?
Go to: https://xtext.itemis.com
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Parsing Exponent Expression [message #1777467 is a reply to message #1777464] Wed, 29 November 2017 14:21 Go to previous messageGo to next message
Christoff Erasmus is currently offline Christoff ErasmusFriend
Messages: 21
Registered: December 2016
Junior Member
Please find attached the Grammer file.

Spoke to soon, the change broke the maven build.

This test method, used to pass but now fails.
	@Test def void testByteSize_DataTypeWithBytes_00() {
		
		'''DCL-S foo varchar(1  )'''.parse => [ m |	m.assertNoErrors]
		'''DCL-S foo varchar(1:2)'''.parse => [ m |	m.assertNoErrors]
		'''DCL-S foo varchar(1:4)'''.parse => [ m |	m.assertNoErrors]
		
		'''DCL-S foo vargraph(1  )'''.parse => [ m | m.assertNoErrors]
		'''DCL-S foo vargraph(1:2)'''.parse => [ m | m.assertNoErrors]
		'''DCL-S foo vargraph(1:4)'''.parse => [ m | m.assertNoErrors]
		
		'''DCL-S foo varucs2(1  )'''.parse => [ m |	m.assertNoErrors]
		'''DCL-S foo varucs2(1:2)'''.parse => [ m |	m.assertNoErrors]
		'''DCL-S foo varucs2(1:4)'''.parse => [ m |	m.assertNoErrors]
	}
  • Attachment: XRPGLE.xtext
    (Size: 5.25KB, Downloaded 23 times)

[Updated on: Wed, 29 November 2017 14:22]

Report message to a moderator

Re: Parsing Exponent Expression [message #1777468 is a reply to message #1777467] Wed, 29 November 2017 14:33 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 12108
Registered: July 2009
Senior Member
why

Exponent returns Expression:
Primary (
{Exponent.left=current} '**'
right=Exponent
)*;


and not

Exponent returns Expression:
Primary (
{Exponent.left=current} '**'
right=Exponent
)?;


Need professional support for Xtext, Xpand, EMF?
Go to: https://xtext.itemis.com
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Parsing Exponent Expression [message #1777476 is a reply to message #1777468] Wed, 29 November 2017 14:58 Go to previous messageGo to next message
Christoff Erasmus is currently offline Christoff ErasmusFriend
Messages: 21
Registered: December 2016
Junior Member
Made the change form "*" to "?".

Warning gone, Expression Parsing still good.
But the @Test (above) still fails, see attached.

Re: Parsing Exponent Expression [message #1777478 is a reply to message #1777476] Wed, 29 November 2017 15:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 12108
Registered: July 2009
Senior Member
foo is a keyword ?!? https://blogs.itemis.com/en/xtext-hint-identifiers-conflicting-with-keywords

Need professional support for Xtext, Xpand, EMF?
Go to: https://xtext.itemis.com
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Parsing Exponent Expression [message #1777480 is a reply to message #1777478] Wed, 29 November 2017 15:12 Go to previous message
Christoff Erasmus is currently offline Christoff ErasmusFriend
Messages: 21
Registered: December 2016
Junior Member
Whoops... 'foo' was not meant to be a keyword.

Thanks for all the help.
Previous Topic:How to leverage _trace to properly configure "Open Source/Generated File"
Next Topic:Questions about two problems with Xpect
Goto Forum:
  


Current Time: Thu Jul 19 07:26:01 GMT 2018

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

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

Back to the top