Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Resolving conflicts with terminals and rules(Can't find a way to resolve conflict in hex color and INT and ID terminals)
Resolving conflicts with terminals and rules [message #557191] Sun, 05 September 2010 23:23 Go to next message
Nikolay  is currently offline Nikolay Friend
Messages: 2
Registered: September 2010
Junior Member
I have the task to support Integer, Identifier and Color in my language. Color is the hex number without any prefix and quotes, i.e "221122" or "FFAA33".

I'm new with xText but hoped that this task has the simple solution. Unfortunatly my attempts to solve it gave me nothing but boring conflicts between INT, Color and ID rules. I can even understand why I have problems, but I don't understand how to overcome them.

Here is my grammar file with my attempts commented and marked with number from 1 to 3. I have simplified color to ColorComponent which has 2 digits instead of 6.

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals 

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

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

Model :
	(options += Option)*;
	
IntTest :
	'-int' myint = INT;
	
MyColor :
	'-green' green = ColorComponent;	
	
MyIdent:
	'-ident' ident = ID;	
    
Option:
	MyColor | IntTest | MyIdent;

// 1. 
// "-ident FF" has error "mismatched input 'FF' expecting RULE_ID"
// "-int 1" has error "mismatched input '1' expecting RULE_INT"
// terminal HexDigit : ('0' .. '9') | ('A' .. 'F');
// terminal ColorComponent : HexDigit HexDigit;

// 2. 
// "-green FF" has error "mismatched input 'FF' expecting RULE_HEXDIGIT"
// "-int 1" has error "mismatched input '1' expecting RULE_INT"
//ColorComponent :
//	HexDigit HexDigit;
//	
//terminal HexDigit : ('0' .. '9') | ('A' .. 'F');

// 3.
// "-green FF" has error "mismatched input 'FF' expecting RULE_HEXDIGIT"
// "-int 1" has error "mismatched input '1' expecting RULE_INT"
//ColorComponent:
//  (HexDigit HexDigit);	
//	
//terminal Digit : ('0'..'9');	
//terminal HexDigit : (Digit) | ('A' .. 'F');
//terminal INT returns ecore::EInt: (Digit)+;


Each decision was tested with file
-int 1
-green FF
-ident FF


Please could someone give me a hint how to deal with such problem?
Re: Resolving conflicts with terminals and rules [message #557244 is a reply to message #557191] Mon, 06 September 2010 09:04 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

the main problem is that your terminal rules overlap. HexDigit hides
Digit and INTs of length 1.
What I would do (in this simple example) is not introduce any new
terminal rules and rather use data type rules along with semantic
validation in order to check the syntax.

ColorComponent hidden(): ID|INT ID;
(ID matches components starting with a lettern, INT ID those starting
with a number).

In the validation you then check whether "green" of "MyColor" has the
desired form. Alternatively you could also have a value converter for
ColorComponent throwing a corresponding exception if the String does not
have the correct form.

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: Resolving conflicts with terminals and rules [message #557780 is a reply to message #557244] Wed, 08 September 2010 21:38 Go to previous message
Nikolay  is currently offline Nikolay Friend
Messages: 2
Registered: September 2010
Junior Member
Thank you!

I have applied you advise about ID|INT ID; and validation and it works fine for me.
Previous Topic:Question about Cross-References
Next Topic:Adding new menus to UI
Goto Forum:
  


Current Time: Thu Apr 25 07:58:31 GMT 2024

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

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

Back to the top