Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » XText grammar with lookbehind
XText grammar with lookbehind [message #1756248] Tue, 14 March 2017 13:41 Go to next message
Stephan Pirnbaum is currently offline Stephan PirnbaumFriend
Messages: 1
Registered: March 2017
Junior Member
Hello,

I am new in developing a DSL using XText and I am currently stuck in a problem.

I have to parse a file which contains elements called Class and might look like this:
A {
B, //this comma is needed
C,
D {E}, //comma is optional after a closing curly brace
F {G}
H
}
I,
J

As you can see, a comma is a must after a class without subclasses (those enclosed by curly brackets) when another class follows. Otherwise, the comma is optional.

My current (wrong) solution is:
Model: (classes += Class)*
Class: name = ID ('{' (subclasses += Class) (',' subclasses += Class)* '}')?

Sadly, this works only when the comma is a must in any case. I believe the grammar should express the following structure:
Class: ID (',' Class | '{' subclasses += Class '}' ','? Class)?

Of course, that does not work since I need to assign the return value of Class to the subclasses of the parent. After some looking around different blogs, I belive that using a lookbehind would be a solution. However, I havn't found a way to express this in XText.

Is there a solution or a workaround for this issue?

Thanks in advance
Stephan
Re: XText grammar with lookbehind [message #1756272 is a reply to message #1756248] Tue, 14 March 2017 18:51 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14735
Registered: July 2009
Senior Member
Did you try something like

Model:
	((classes+=ClassWithBraces|classes+=ClassWithComma)* classes+=ClassAtEnd?);

ClassWithComma returns Class:
	name=ID ","
;

ClassWithBraces returns Class:
	name=ID =>("{" ((classes+=ClassWithBraces|classes+=ClassWithComma)* classes+=ClassAtEnd?) "}") ","?
;

ClassAtEnd returns Class:
	name=ID
;


(you may need to add a validation if you dont want a "," at the very end


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com

[Updated on: Tue, 14 March 2017 19:11]

Report message to a moderator

Previous Topic:2 OutputConfiguration's on Idea with Error
Next Topic:Maven not able to find MWE2 file referred by unit tests.
Goto Forum:
  


Current Time: Thu Dec 05 14:16:30 GMT 2024

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

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

Back to the top