Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Using data rule Id instead of ID terminal
Using data rule Id instead of ID terminal [message #1021177] Tue, 19 March 2013 16:02 Go to next message
stefan tudose is currently offline stefan tudoseFriend
Messages: 12
Registered: July 2012
Junior Member
Hi,
I am writing a grammar using Xtext.
I have a rule representing a Display Format:


FormatSpec :
	DecimalFrmt	|
	ExponentialFrmt	
	;

DecimalFrmt:
	'S'? 'D' FieldWidth ('.' FracWidth)?;

FieldWidth:
	INT;

FracWidth:
	INT;

ExponentialFrmt:
	'S'? 'E' FieldWidth '.' FracWidth;



So the intention is to be able to read format strings as: D5.3 (meaning to display an integer using 5-digits and 3 decimals) SD5, E12.2, SE15.2 and so on.

The problem is that this kind of formats are shadowed by the (default) terminal ID. and I can't write them without white spaces ('S D 5' works fine but is not what I want).
So I tried to use a data Type Rule 'Id' instead of the terminal ID (which I disable).

	terminal ALFA:
		'a'..'z' | 'A'..'Z'
	;
	
	terminal DIGIT:
		'0'..'9'
	;
	
	AlfaNumber :
		ALFA | DIGIT
	;
	
	Id:
		ALFA AlfaNumber*
	;


The problem I have now is that the parser tries to match some parts of the id with some keywords. For example if I have a keyword 'FRMT' I get an error if I try to define an Id 'idFRM02'.

Is this normal? If yes, are there any work-arounds? I have run out of ideas. Thank you for helping!

Stefan

Re: Using data rule Id instead of ID terminal [message #1021258 is a reply to message #1021177] Tue, 19 March 2013 18:51 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

yes you have to list the keywords together with the terminal rule in a datatype rule

example

Model: "model" name=MYID;
MYID: "model" | ID;


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using data rule Id instead of ID terminal [message #1021499 is a reply to message #1021258] Wed, 20 March 2013 08:27 Go to previous messageGo to next message
stefan tudose is currently offline stefan tudoseFriend
Messages: 12
Registered: July 2012
Junior Member
Hi Christian,

Thanks for your answer.
This is not exactly my problem. What I want to do is to disable the default terminal ID completely and use only my datatype Id only (because ID shadows some other rules).
But if I do

Model: "model" name=Id;

Id:
	ALFA AlfaNumber* | 'model'
;

AlfaNumber :
	ALFA | DIGIT
;

terminal ALFA:
	'a'..'z' | 'A'..'Z'
;
	
terminal DIGIT:
	'0'..'9'
;


I still have the same problem when I try to parse model mod42 I have an error saying that it expects an 'e' instead of '4'.
Re: Using data rule Id instead of ID terminal [message #1021503 is a reply to message #1021499] Wed, 20 March 2013 08:35 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

don't import Terminals.

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: Using data rule Id instead of ID terminal [message #1021505 is a reply to message #1021499] Wed, 20 March 2013 08:38 Go to previous messageGo to next message
stefan tudose is currently offline stefan tudoseFriend
Messages: 12
Registered: July 2012
Junior Member
In fact maybe disabling the ID terminal is not the solution. What I want to do is simply to be able to use my rules (simplified):
FormatSpec :
	DecimalFrmt	
	;

DecimalFrmt:
	signed?='S'? 'D' digits=FieldWidth ('.' decimals=FracWidth)?;


to parse something like SD12.5 (and not S D 12.5).

What's the best way to achieve it? By default it doesn't work (if I got it right it's because SD12 is interpreted as an ID and the rule doesn't match anymore).

Thanks!
Stefan
Re: Using data rule Id instead of ID terminal [message #1021507 is a reply to message #1021505] Wed, 20 March 2013 08:44 Go to previous messageGo to next message
stefan tudose is currently offline stefan tudoseFriend
Messages: 12
Registered: July 2012
Junior Member
Hi Alex,

Well I don't import any terminals, I only use mines (ALFA, DIGIT). The problem is that the datatype rule Id doesn't work as expected where the terminal ID used to work ( for ids containing a part of a keyword).
But please check my previous message because that is what I am really trying to do. Maybe disabling the ID terminal is not the right way to do it.

Thanks
Re: Using data rule Id instead of ID terminal [message #1021514 is a reply to message #1021499] Wed, 20 March 2013 08:57 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Which is the super grammar you use

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using data rule Id instead of ID terminal [message #1021517 is a reply to message #1021514] Wed, 20 March 2013 09:09 Go to previous messageGo to next message
stefan tudose is currently offline stefan tudoseFriend
Messages: 12
Registered: July 2012
Junior Member
At the begining I was using the default Terminals (with org.eclipse.xtext.common.Terminals) but I removed the import when I tried to disable the ID terminal.
Re: Using data rule Id instead of ID terminal [message #1021522 is a reply to message #1021517] Wed, 20 March 2013 09:15 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

can you share a complete reproducable grammar and example model?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using data rule Id instead of ID terminal [message #1021546 is a reply to message #1021522] Wed, 20 March 2013 09:53 Go to previous messageGo to next message
stefan tudose is currently offline stefan tudoseFriend
Messages: 12
Registered: July 2012
Junior Member
Yes, here is my grammar. I repeat that what I would like to have is to be able to parse something like Object newObj FRMT SE5.2

This is the initial try. I use default terminals:

grammar org.xtext.example.mydsl.NewTest 
with org.eclipse.xtext.common.Terminals
hidden(WS)

generate newTest "http://www.xtext.org/example/mydsl/NewTest"

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


Model: "Object" 
	name=ID 
	displayFormat=PresFormat
;

PresFormat:
	'FRMT'
	format=FormatSpec;

FormatSpec:
	DecimalFrmt	|
	ExponentialFrmt	|
	HexFrmt |
	CharFrmt 
	;

 DecimalFrmt:
	signed?='S'? 'D' width=FieldWidth ('.' decimals=FracWidth)?;

FieldWidth:
	UnsignedIntConst;

FracWidth:
	UnsignedIntConst;

ExponentialFrmt:
	signed?='S'? 'E' width=FieldWidth '.' decimals=FracWidth;

HexFrmt:
	'H'  width=FieldWidth;

CharFrmt:
	'C'  width=FieldWidth;

UnsignedIntConst returns ecore::EInt:
	DIGIT+;
	
terminal DIGIT:
	'0'..'9';
	
terminal INT returns ecore::EInt:
'this one has been deactivated';


When I try to parse Object newObj FRMT SE5.2 I get an error "No viable alternative at input 'SE5' ".
NOTE: it works fine for Object newObj FRMT S E 5.2 but I don't want the white spaces.


So I guessed it's because SE5 is interpreted as an ID and I wanted to disable the ID terminal and use a datatype rule instead. Here is my new grammar:

grammar org.xtext.example.mydsl.NewTest 
//with org.eclipse.xtext.common.Terminals
hidden(WS)

generate newTest "http://www.xtext.org/example/mydsl/NewTest"

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


Model: "Object" 
	name=Id 
	displayFormat=PresFormat
;

PresFormat:
	'FRMT'
	format=FormatSpec;

FormatSpec:
	DecimalFrmt	|
	ExponentialFrmt	|
	HexFrmt |
	CharFrmt 
	;

 DecimalFrmt:
	signed?='S'? 'D' width=FieldWidth ('.' decimals=FracWidth)?;

FieldWidth:
	UnsignedIntConst;

FracWidth:
	UnsignedIntConst;

ExponentialFrmt:
	signed?='S'? 'E' width=FieldWidth '.' decimals=FracWidth;

HexFrmt:
	'H'  width=FieldWidth;

CharFrmt:
	'C'  width=FieldWidth;

UnsignedIntConst returns ecore::EInt:
	DIGIT+;

Id:
	ALFA AlfaNumber* 
;

AlfaNumber :
	ALFA | DIGIT
;
	
terminal DIGIT:
	'0'..'9';

terminal ALFA:
	'a'..'z' | 'A'..'Z'
;
	
terminal WS			: (' '|'\t'|'\r'|'\n')+;


I try to parse again the same thing Object newObj FRMT SE5.2.
This time I get an error on the 'newObj' id : " Mismatched character ' ' expecting 'e' ". The 'SE5.2' seems to work.

I hope it's clear enough...

Thank you
Re: Using data rule Id instead of ID terminal [message #1021635 is a reply to message #1021546] Wed, 20 March 2013 12:41 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

why not

Id hidden():
ALFA AlfaNumber*
;

AlfaNumber :
ALFA | DIGIT
;

terminal DIGIT:
('0'..'9')+;

terminal ALFA:
('a'..'z' | 'A'..'Z')+
;


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using data rule Id instead of ID terminal [message #1021650 is a reply to message #1021635] Wed, 20 March 2013 13:08 Go to previous message
stefan tudose is currently offline stefan tudoseFriend
Messages: 12
Registered: July 2012
Junior Member
Well if I do this I get the first problem again: it won't parse the 'SE5.2' part as I expect (using the ExponentialFrmt rule) because the 'SE' is considered to be an ALFA terminal... I get "No viable alternative at input 'SE'".
Previous Topic:Xtext 2.4.0M6
Next Topic:Xbase and logical containers
Goto Forum:
  


Current Time: Fri Apr 19 07:19:08 GMT 2024

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

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

Back to the top