Skip to main content



      Home
Home » Modeling » TMF (Xtext) » XText grammar with lookbehind
XText grammar with lookbehind [message #1756248] Tue, 14 March 2017 09:41 Go to next message
Eclipse UserFriend
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 14:51 Go to previous message
Eclipse UserFriend
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

[Updated on: Tue, 14 March 2017 15:11] by 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: Sat Jul 12 04:51:19 EDT 2025

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

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

Back to the top