Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Expression Language: Using ".*" in combination with Decimal Numbers
Expression Language: Using ".*" in combination with Decimal Numbers [message #1774566] Tue, 17 October 2017 11:57 Go to next message
Lukas Schaus is currently offline Lukas SchausFriend
Messages: 37
Registered: October 2016
Member
Hi,

i am trying to model a portion of the MATLAB Syntax. I have problems with Expressions that contain the ".*" operator.

Here is a small portion of my Grammar:
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"


import "http://www.eclipse.org/emf/2002/Ecore" as ecore


Model:
	exp=MExpression;

NumberLiteral:
	{NumberLiteral} number=NUMBER
;


	
MExpression:
	MBinaryOperation
;

	MBinaryOperation returns MExpression:
		MUnaryOperation =>({MBinaryOperation.left=current} op=MBINARY_OPERATOR right=MUnaryOperation)*
	;

	MBINARY_OPERATOR
		: '*' | '/' | '+' | '-' | '^' | './' | '.*' | '.\\' | '\\'
	;
	
	MUnaryOperation returns MExpression:
		{MUnaryOperation} opUnary=MUNARY_OPERATOR? operand=MPrimaryExpression
	;
	
	MUNARY_OPERATOR:
		'-' | '+'
	;
	
	
	MPrimaryExpression returns MExpression
		: lBrace='(' expression=MExpression rBrace=')'
		| Literal
	;
	
	Literal
		: NumberLiteral
		| BooleanLiteral
	;
	
	BooleanLiteral:
		{BooleanLiteral} ('true' | 'false')
	;
	
terminal NUMBER returns ecore::EBigDecimal:
	('0'..'9')* ('.' ('0'..'9')+)?(('E'|'e')('-'|'+')?('0'..'9')*)?;
	
//@Override
terminal INT returns ecore::EInt:
	'this one has been deactivated';


The workflow does not raise any errors or warnings unsing Xtext 2.8 and JDK 1.6 in eclipse Kepler.

The following input leads to a Syntax Error:
1.*2


The Stacktrace indicates, that the parser tries to match the NUMBER rule after it consumed the '.' :
java.lang.AssertionError: Expected no issues, but got :ERROR (org.eclipse.xtext.diagnostics.Diagnostic.Syntax) 'required (...)+ loop did not match anything at character '*'' on MBinaryOperation, offset 0, length 2
ERROR (org.eclipse.xtext.diagnostics.Diagnostic.Syntax) 'no viable alternative at input '*'' on MBinaryOperation, offset 2, length 1
ERROR (org.eclipse.xtext.diagnostics.Diagnostic.Syntax) 'no viable alternative at input ';'' on MBinaryOperation, offset 4, length 1

	at org.junit.Assert.fail(Assert.java:88)
	at org.eclipse.xtext.junit4.validation.ValidationTestHelper.assertNoIssues(ValidationTestHelper.java:164)
	at org.eclipse.xtext.junit4.validation.ValidationTestHelper.assertNoIssues(ValidationTestHelper.java:43)
	at org.xtext.example.mydsl.tests.ParserTests.parseInitSimpleAssignmentToSignal(ParserTests.java:35)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.eclipse.xtext.junit4.XtextRunner$1.evaluate(XtextRunner.java:49)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)


I was wondering how I could refactor my grammar and would be very grateful for any help.

P.S.: backtracking does not solve the problem

Greetings

Lukas
Re: Expression Language: Using ".*" in combination with Decimal Numbers [message #1774570 is a reply to message #1774566] Tue, 17 October 2017 12:21 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

You may find that the problems get worse if you have floating point challenges such as 1.2.

You can use an alternate lexer that understands that ".*" is always better than "."; unless you also want ".<ws>*".

For OCL I still use the standard lexer but insert a RetokenizingTokenSource to join up "." "*" as ".*" between Lexer and Parser. See

GIT\org.eclipse.ocl\plugins\org.eclipse.ocl.xtext.base\src\org\eclipse\ocl\xtext\base\services\RetokenizingTokenSource.java

and associated references.

Regards

Ed Willink
Previous Topic:Scoping with a custom VariableDeclaration in XBase
Next Topic:Linking XCastExpression to a "toX" function
Goto Forum:
  


Current Time: Tue Mar 19 04:47:45 GMT 2024

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

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

Back to the top