Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to maka a rule LL(*) while preserving semantics?
How to maka a rule LL(*) while preserving semantics? [message #1767318] Tue, 04 July 2017 15:49 Go to next message
David BlackFriend
Messages: 33
Registered: June 2017
Member
Hi,

whenever I have a rule like this:

A -> cA | cAB | "a"
(..)

XText asks me for applying left-refactoring. On paper, I'd do it like this:

A -> cC | "a"
C -> A | AB
(...)

But in XText you don't have "null" rules, I believe. So I do it by mixing in regular expressions:

A -> cA (B)? | "a"


And XText seems to swallow it as a "left refactorization".

The problem is that in my parsed model I want cA and cAB to mean different things and to be mapped into different ECore classes, and as I understand, my "regular expression" impedes that.

Let's show it on a real XText snippet. How can I make the following rule LL(*) and at the same time get different and meaningful ECore classes for alternatives 2 and 3?

terminal TYPE:
	ID |
	('(' TYPE ')') |
	('(' TYPE ')' 'throws')
;


I've tried this hoping that it would guide XText throughout choices, but it did not work:

terminal TYPE:
	ID |
	COMPLICATED_TYPE
;

terminal fragment COMPLICATED_TYPE:
	('(' TYPE ')') |
	COMPLICATED_TYPE_WITH_THROWS
;

terminal fragment COMPLICATED_TYPE_WITH_THROWS:
	('(' TYPE ')' 'throws')
; 



Cheers, David.

[Updated on: Tue, 04 July 2017 15:50]

Report message to a moderator

Re: How to maka a rule LL(*) while preserving semantics? [message #1767321 is a reply to message #1767318] Tue, 04 July 2017 15:56 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
i cannot follow you.

terminals give you no eclasses at all.
so can you give us a real real world example to look at


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to maka a rule LL(*) while preserving semantics? [message #1767323 is a reply to message #1767321] Tue, 04 July 2017 16:11 Go to previous messageGo to next message
David BlackFriend
Messages: 33
Registered: June 2017
Member
OK, my mistake.

I wish to rewrite a rule so that

1) Get's converted into LL(*), because XText complains that the rules it's not LL(*)
2) Ensures that I will have different generated classes for these two parsed lines:

(mytype)

and

(mytype) throws


The rule I want to fix is this one, TYPE:

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

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
Model:
	greetings+=Greeting*;
	
Greeting:
	name=TYPE;
	
terminal TYPE:
	ID |
	('(' TYPE ')') |
	('(' TYPE ')' 'throws')
;

[Updated on: Tue, 04 July 2017 16:14]

Report message to a moderator

Re: How to maka a rule LL(*) while preserving semantics? [message #1767324 is a reply to message #1767323] Tue, 04 July 2017 16:18 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
but why terminals?

Type:
	{TypeWithId}name=ID | Rest
;

Rest returns Type:
	=>({TypeWithThrows}'(' type=Type ')' 'throws') |
	{TypeWithOutThrows}'(' type=Type ')' 
;



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to maka a rule LL(*) while preserving semantics? [message #1767327 is a reply to message #1767324] Tue, 04 July 2017 16:28 Go to previous messageGo to next message
David BlackFriend
Messages: 33
Registered: June 2017
Member
Christian Dietrich wrote on Tue, 04 July 2017 16:18
but why terminals?


Ok, they do not have to be terminals.

[Updated on: Tue, 04 July 2017 16:29]

Report message to a moderator

Re: How to maka a rule LL(*) while preserving semantics? [message #1767329 is a reply to message #1767324] Tue, 04 July 2017 16:32 Go to previous messageGo to next message
David BlackFriend
Messages: 33
Registered: June 2017
Member
Thanks for the solution!

Hmm, those are actions, aren't they? I'm afraid I still can't understand them well. I wasn't able to make much from the official documentation.

Anyway, that '=>' symbol... so that's the solution, right? I read in the official docs that it "tells" XText which option to choose on ambiguous cases. However I did not look at it as an alternative because I still don't understand how to use it in most of the cases.

Anyway, I will use it in this case ;)

Thanks again!

[Updated on: Tue, 04 July 2017 16:34]

Report message to a moderator

Re: How to maka a rule LL(*) while preserving semantics? [message #1767330 is a reply to message #1767329] Tue, 04 July 2017 16:38 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
yes with the actions you can tell xtext to create the different types you want to have

{TypeWithId}name=ID

e.g. creates a eclass TypeWithId



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:A couple of rules and recursion overflow - help appreciated
Next Topic:Ambiguous grammar
Goto Forum:
  


Current Time: Tue Sep 24 04:56:43 GMT 2024

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

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

Back to the top