Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Handling terminal conflicts
Handling terminal conflicts [message #1712132] Wed, 21 October 2015 13:03 Go to next message
Zoltan Ujhelyi is currently offline Zoltan UjhelyiFriend
Messages: 392
Registered: July 2015
Senior Member
Hi,

I am developing a textual language in Xtext that features both real numbers and number ranges. We would like to write real numbers using the syntax "1.2", and want to write ranges as "1..2", but we have experienced some strange issues.

I have created a minimal example that brings forth this problem:

grammar test.Ranges with org.eclipse.xtext.common.Terminals

generate ranges "http://www.Ranges.test"

Model:
	(element+=(Range | Number | RealNumber) ';' )*
;
	
Range:
	from = INT '..' to = INT;
	
Number:
    number = INT
;

RealNumber:
    value = REAL
;
	
terminal REAL:
    INT '.' INT
;


I am using the following grammar as an example:
1; //OK
1.2; //OK
1 .. 2; //OK
1..2; //Errors


The first three rows work as expected, however, the last one returns the following errors:
- missing EOF at '.'
- required (...)+ loop did not match anything at character '.'

As far as I understand, the issue is caused by a conflict between the different terminals, but I have no idea how to avoid this (with the exception of changing the syntax). Can you provide some hints of how to handle this issue?

Thanks,
Zoltán
Re: Handling terminal conflicts [message #1712133 is a reply to message #1712132] Wed, 21 October 2015 13:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14668
Registered: July 2009
Senior Member
Hi,

in this case you can simply use a datatype rule instead of a parser rule

REAL:
    INT '.' INT
;



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Handling terminal conflicts [message #1712138 is a reply to message #1712133] Wed, 21 October 2015 13:38 Go to previous messageGo to next message
Zoltan Ujhelyi is currently offline Zoltan UjhelyiFriend
Messages: 392
Registered: July 2015
Senior Member
Hi Christian,

that indeed solves the issue in my small grammar, but I have one more related question. Can I use a valueConverter (or something else) to make this return a double number for future processing. If I recall correctly, the value converter only works with terminal types (but I couldn't find right now the related part in the Xtext codebase).

Thanks,
Zoltán
Re: Handling terminal conflicts [message #1712141 is a reply to message #1712138] Wed, 21 October 2015 13:53 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14668
Registered: July 2009
Senior Member
yes you simply specify the return type in the datatype rule + add a value converter. the value converter works with datatype rules as well (subclassing DefaultTerminalConverters)

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Wed, 21 October 2015 13:54]

Report message to a moderator

Re: Handling terminal conflicts [message #1712183 is a reply to message #1712141] Wed, 21 October 2015 19:13 Go to previous messageGo to next message
Prakyath Jaladhar is currently offline Prakyath JaladharFriend
Messages: 15
Registered: July 2015
Junior Member
Hi Christian,

I had a similar question so I didn't create a new topic. Suppose I want to consider anything that is inside '[ ], { } or ()' as CCode, how should I do that?

Currently I have this:

CCode: 
	  '[' CCode ']' 
	| '{' CCode '}' 
	| '(' CCode ')' 
	;
Re: Handling terminal conflicts [message #1712184 is a reply to message #1712183] Wed, 21 October 2015 19:33 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14668
Registered: July 2009
Senior Member
Hi

i am not sure if i get your problem/question
this is a bit different.
if the keywords {} () [] are not involved otherwise you can use

terminal CCODE : '('->')' | '['->']' | '{'->'}';


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Handling terminal conflicts [message #1712188 is a reply to message #1712184] Wed, 21 October 2015 20:14 Go to previous messageGo to next message
Prakyath Jaladhar is currently offline Prakyath JaladharFriend
Messages: 15
Registered: July 2015
Junior Member
Hi Christian,

Thanks for your reply.

My question is that I want to capture everything that is inside those brackets as CCode. Whenever I see either of the 3 braces, I need to capture everything as CCode.

This is to make things simple. As CCode contains traditional code in C/C++, I don't want to parse it, so how should I ignore that?
Re: Handling terminal conflicts [message #1712190 is a reply to message #1712188] Wed, 21 October 2015 20:16 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14668
Registered: July 2009
Senior Member
hi and what if the code contains the braces as well?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Handling terminal conflicts [message #1712191 is a reply to message #1712190] Wed, 21 October 2015 20:24 Go to previous messageGo to next message
Prakyath Jaladhar is currently offline Prakyath JaladharFriend
Messages: 15
Registered: July 2015
Junior Member
Hi,

I'll give an example. The language I am working on contains entry methods, so the rule for an entry method may look like this:

Entry:
	  'entry' 'void' entryName=ID ep=EParameters code=CCode
;

CCode:  '[' CCode ']' 
	| '{' CCode '}' 
	| '(' CCode ')' 
	;


As you mentioned, the CCode can also contain braces as well. So basically whatever comes after EParameters which can be inside one of the three braces should be treated as CCode (which can include braces inside as well). How can I achieve this?

[Updated on: Thu, 22 October 2015 15:45]

Report message to a moderator

Re: Handling terminal conflicts [message #1712193 is a reply to message #1712191] Wed, 21 October 2015 20:27 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14668
Registered: July 2009
Senior Member
maybe sebastian zarnekow has an idea on that. maybe it can be done with a statful lexer

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Enable or disable grammatical rule by file extension
Next Topic:Parameters in error() using the Xtext/Xtend validation
Goto Forum:
  


Current Time: Fri Apr 26 19:08:11 GMT 2024

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

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

Back to the top