Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Implementing dates clashes with simple equations(Xtext)
Implementing dates clashes with simple equations [message #1798228] Wed, 14 November 2018 09:00 Go to next message
Eclipse UserFriend
Good day! I just started learning Xtext a few days ago and am trying to create a grammar that can assign to variables both simple equations with round brackets and a calendar date.

My grammar for the date part is the following:

Group:
	(BracketGroup | NonBracketGroup | Operand )
	(operators+=Operator '('* operands+=Operand 
	(operators+=Operator operands+=Operand) ')'*)*
	;

BracketGroup:
	'('NonBracketGroup')' (operators+=Operator operands+=Operand)+ 
;


NonBracketGroup:
	'('?
	operands+=Operand 
	(operators+=Operator operands+=Operand)+ ')'?

Value:
	STRING | '-'?INT | Decimal | Logical | DATA  
;

DATA: MONTH'/'DAY'/'YEAR;
DAY: INT;
MONTH: INT;
YEAR: INT;

DataType:
	'character' | 'integer' | 'decimal' | 'logical' | 'date'
;

Operator:
	'+' | '-' | '*' | '/' | 'MODULO' | EQ | '>'| '<' | '=>' | '=<' | '<>' | GroupOperator



This leads to:
Decision can match input such as "RULE_INT '/' RULE_INT '/' {RULE_INT, '.'..'FALSE'}" using multiple alternatives: 2, 5



I think that making the '/' a fragment might fix the issue, but I don't really understand how it works (tried multiple sources online) nor how to implement it. If someone could show me the way to stop making the rules clash would help me a lot.

If what I wrote is not sufficient as info please don't hesitate to ask for more, I am a total beginner and might have omitted important stuff.
Re: Implementing dates clashes with simple equations [message #1798244 is a reply to message #1798228] Wed, 14 November 2018 10:55 Go to previous messageGo to next message
Eclipse UserFriend
can you please provide a complete but minimal reproducing grammar
Re: Implementing dates clashes with simple equations [message #1798256 is a reply to message #1798228] Wed, 14 November 2018 12:00 Go to previous messageGo to next message
Eclipse UserFriend
Here it is, hope its alright:

grammar org.xtext.example.domainmodel.Domainmodel with org.eclipse.xtext.common.Terminals

generate domainmodel "http://www.xtext.org/example/domainmodel/Domainmodel"


ExpressionsModel:
	(statement+= Statement)*;
	
Statement:
	Definition | Assignment
;

Definition:
	'define' name=ID 'as' type=DataType; 
		
Assignment:
	left=Assignable EQ right=Expression 
;   

Assignable:
	def=[Definition|ID] 
;

Expression:
	 Group 
;

Group:
	(BracketGroup | NonBracketGroup | Operand )
	(operators+=Operator '('* operands+=Operand 
	(operators+=Operator operands+=Operand) ')'*)*
	;

BracketGroup:
	'('NonBracketGroup')' (operators+=Operator operands+=Operand)+ 
;


NonBracketGroup:
	'('?
	operands+=Operand 
	(operators+=Operator operands+=Operand)+ ')'?
;

Operand:
	Assignable | Value 
;

Value:
	STRING | '-'?INT | Decimal | Logical | DATA  
;

Decimal:
	'-'?INT?'.'INT
;

Logical:
	'TRUE' | 'FALSE'
;

DATA: DAY'/'MONTH'/'YEAR;
DAY: INT;
MONTH: INT;
YEAR: INT;

DataType:
	'character' | 'integer' | 'decimal' | 'logical' | 'date'
;

Operator:
	'+' | '-' | '*' | '/' | 'MODULO' | EQ | '>'| '<' | '=>' | '=<' | '<>' | GroupOperator
;


GroupOperator:
	'AND' | 'OR' | 'NOT'
;

terminal EQ:
	'='
;
Re: Implementing dates clashes with simple equations [message #1798258 is a reply to message #1798256] Wed, 14 November 2018 12:05 Go to previous messageGo to next message
Eclipse UserFriend
hmmm maybe you should start with some reading on how to build expression grammars in general with xtext
https://typefox.io/parsing-expressions-with-xtext

and there is the question:

1/2/3 is this a date or is this (1/2)/3 ???
Re: Implementing dates clashes with simple equations [message #1798260 is a reply to message #1798258] Wed, 14 November 2018 12:12 Go to previous messageGo to next message
Eclipse UserFriend
Ah I see, I suppose my grammar implies that both are a date?
Re: Implementing dates clashes with simple equations [message #1798263 is a reply to message #1798260] Wed, 14 November 2018 12:21 Go to previous messageGo to next message
Eclipse UserFriend
No but both can occur at the same place
Re: Implementing dates clashes with simple equations [message #1798300 is a reply to message #1798263] Thu, 15 November 2018 01:50 Go to previous messageGo to next message
Eclipse UserFriend
Sorry for answering so late but couldn't get a hold on to a laptop, yes the grammar makes use of '/' for both the equations and dates, isn't it possible?
Re: Implementing dates clashes with simple equations [message #1798304 is a reply to message #1798300] Thu, 15 November 2018 02:24 Go to previous messageGo to next message
Eclipse UserFriend
the question is

1/2/3 / 3/4/4 the division of two dates or 6 numbers
Re: Implementing dates clashes with simple equations [message #1798305 is a reply to message #1798304] Thu, 15 November 2018 02:33 Go to previous messageGo to next message
Eclipse UserFriend
That would be the division of 6 numbers.
Re: Implementing dates clashes with simple equations [message #1798306 is a reply to message #1798305] Thu, 15 November 2018 02:36 Go to previous messageGo to next message
Eclipse UserFriend
but your grammar says it can be the division of two dates

Value:
STRING | '-'?INT | Decimal | Logical | DATA
;
Re: Implementing dates clashes with simple equations [message #1798307 is a reply to message #1798306] Thu, 15 November 2018 02:41 Go to previous messageGo to next message
Eclipse UserFriend
you could write terminal DATA: INT'/'INT'/'INT;
but then in your model you would need a 1 / 2 for a division (not 1/2)
Re: Implementing dates clashes with simple equations [message #1798308 is a reply to message #1798306] Thu, 15 November 2018 02:45 Go to previous messageGo to next message
Eclipse UserFriend
I think I get it now, it's because DATA is also part of the simple equation grammar not only the Date grammar. I'm supposed to assign the Date to variables using a different rule?
Re: Implementing dates clashes with simple equations [message #1798309 is a reply to message #1798308] Thu, 15 November 2018 02:48 Go to previous messageGo to next message
Eclipse UserFriend
yes. and i have said you should have a look at the blog post on how to properly build expressions.

=> you might do something like:

Expression : Date | Addition; ...
Re: Implementing dates clashes with simple equations [message #1798310 is a reply to message #1798309] Thu, 15 November 2018 02:51 Go to previous messageGo to next message
Eclipse UserFriend
Ok, I will thoroughly look at that blog post. Thank you for bearing with me!
Re: Implementing dates clashes with simple equations [message #1798311 is a reply to message #1798310] Thu, 15 November 2018 03:10 Go to previous messageGo to next message
Eclipse UserFriend
e.g

Model:
	name=ID
	ass+=Assignment*
;

Assignment:
	name=ID "=" ex=Expression
;

Expression:
	->Date | Addition
;

Date returns Expression:
	a=INT "/" b=INT "/" c=INT
;
	
Addition returns Expression:
  Multiplication ({Addition.left=current} op=('+'|'-') right=Multiplication)*;
 
Multiplication returns Expression:
  Primary ({Multiplication.left=current} op=('*'|'/') right=Primary)*;
 
Primary returns Expression:
  NumberLiteral |
  '(' Expression ')';
 
NumberLiteral:
  value=INT;
Re: Implementing dates clashes with simple equations [message #1798321 is a reply to message #1798311] Thu, 15 November 2018 06:14 Go to previous messageGo to next message
Eclipse UserFriend
Your grammar is so clean! I think it's the first time I generate and I don't see any errors. Thank you for writing the example. I found the official reference material a bit hard to grasp, but the link you sent me explains it a lot better, do you think you could give me advise from where I should study? It is really important that I learn how to build DSL's using Xtext.
Re: Implementing dates clashes with simple equations [message #1798324 is a reply to message #1798321] Thu, 15 November 2018 06:36 Go to previous messageGo to next message
Eclipse UserFriend
do you already know lorenzo bettinis "Implementing Domain-Specific Languages with Xtext and Xtend (second edition)"
Re: Implementing dates clashes with simple equations [message #1798325 is a reply to message #1798324] Thu, 15 November 2018 06:57 Go to previous messageGo to next message
Eclipse UserFriend
No, I will look into it. Should that cover all?
Re: Implementing dates clashes with simple equations [message #1798327 is a reply to message #1798325] Thu, 15 November 2018 07:15 Go to previous messageGo to next message
Eclipse UserFriend
it's quite complete to get a deep dive into Xtext. Since you can change "everything" in Xtext its impossible to cover "all"
Re: Implementing dates clashes with simple equations [message #1798329 is a reply to message #1798327] Thu, 15 November 2018 07:27 Go to previous message
Eclipse UserFriend
Understood, thank you a lot Christian! You really helped me, I hope you have a good day!
Previous Topic:Element Rename in XText 2.14
Next Topic:Multiple grammars and example web editor
Goto Forum:
  


Current Time: Fri Jun 20 21:56:01 EDT 2025

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

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

Back to the top