Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Parsing Exponent Expression
Parsing Exponent Expression [message #1777341] Tue, 28 November 2017 11:10 Go to next message
Eclipse UserFriend
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 12:13 Go to previous messageGo to next message
Eclipse UserFriend
Why questionmark

Exponent returns Expression:
Primary (
{Exponent.left=current} '**' right=Primary)?;
Re: Parsing Exponent Expression [message #1777453 is a reply to message #1777349] Wed, 29 November 2017 08:28 Go to previous messageGo to next message
Eclipse UserFriend
@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 08:30] by Moderator

Re: Parsing Exponent Expression [message #1777456 is a reply to message #1777453] Wed, 29 November 2017 08:37 Go to previous messageGo to next message
Eclipse UserFriend
then you have to do it the other way round e.g

Exponent returns Expression:
Primary

(
{Exponent.left=current} '**'
right=Exponent
)?;
Re: Parsing Exponent Expression [message #1777460 is a reply to message #1777456] Wed, 29 November 2017 09:02 Go to previous messageGo to next message
Eclipse UserFriend
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 09:03] by Moderator

Re: Parsing Exponent Expression [message #1777461 is a reply to message #1777460] Wed, 29 November 2017 09:03 Go to previous messageGo to next message
Eclipse UserFriend
do you have a complete grammar as is now?
Re: Parsing Exponent Expression [message #1777463 is a reply to message #1777461] Wed, 29 November 2017 09:09 Go to previous messageGo to next message
Eclipse UserFriend
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 09:12 Go to previous messageGo to next message
Eclipse UserFriend
i need something to reproduce
Re: Parsing Exponent Expression [message #1777467 is a reply to message #1777464] Wed, 29 November 2017 09:21 Go to previous messageGo to next message
Eclipse UserFriend
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 97 times)

[Updated on: Wed, 29 November 2017 09:22] by Moderator

Re: Parsing Exponent Expression [message #1777468 is a reply to message #1777467] Wed, 29 November 2017 09:33 Go to previous messageGo to next message
Eclipse UserFriend
why

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


and not

Exponent returns Expression:
Primary (
{Exponent.left=current} '**'
right=Exponent
)?;
Re: Parsing Exponent Expression [message #1777476 is a reply to message #1777468] Wed, 29 November 2017 09:58 Go to previous messageGo to next message
Eclipse UserFriend
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 10:06 Go to previous messageGo to next message
Eclipse UserFriend
foo is a keyword ?!? https://blogs.itemis.com/en/xtext-hint-identifiers-conflicting-with-keywords
Re: Parsing Exponent Expression [message #1777480 is a reply to message #1777478] Wed, 29 November 2017 10:12 Go to previous message
Eclipse UserFriend
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: Wed Jul 23 21:11:20 EDT 2025

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

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

Back to the top