Skip to main content



      Home
Home » Modeling » TMF (Xtext) » OneOf
OneOf [message #1790616] Wed, 13 June 2018 11:14 Go to next message
Eclipse UserFriend
Hello,
for few days I'm still struggling with the basics of grammar, so I hope someone can help me out.

I' trying very simple example of constants and variables:
const a = 1;
const b = "asd";
var c = "asd";
var d = a;
var e = c;


The Grammar I've come up with looks like this:
Domainmodel: (elements+=Type)*;

terminal FLOAT: ('+' | '-')? INT ('.' (INT)?)?;

PrimitiveValue: value=(STRING|FLOAT);

Type:
	ConstantDeclaration | VariableDeclaration
;
	
ConstantDeclaration:
	'const' name=ID '=' value=PrimitiveValue ';'
;

VariableDeclaration:
	'var' name=ID ('=' value=[VariableValue])? ';'
;

VariableValue:
	PrimitiveValue | VariableReference
;

VariableReference:
	(ref=[VariableDeclaration] | ref=[ConstantDeclaration])
;


Error says that the problem is two possible results... which I guess comes from VariableReference where ConstantDeclaration can fit into both because it fits inside value property of VariableDeclaration? ... I'm not really sure though.

Can someone please explain it a little bit more in depth or provide and example how to write a grammar for such language case?

Thanks in advance.
Re: OneOf [message #1790821 is a reply to message #1790616] Mon, 18 June 2018 10:38 Go to previous message
Eclipse UserFriend
the problem is

ref=[VariableDeclaration] is short for ref=[VariableDeclaration|ID] which says: refer to VariableDeclaration, parse ID

so it is parsed as if there is a ref=ID

so in your case

VariableReference:
(ref=ID | ref=ID)
;

which is obviously ambigious

=> you either need to disabigue the both syntax wise or you need to make the one reference

VariableReference:
(ref=[Type])
;
Previous Topic:Keyword/name=ID conflicts
Next Topic:Syntax Error in lambda implementing Xtext quickfixes
Goto Forum:
  


Current Time: Sat Jun 14 01:08:45 EDT 2025

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

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

Back to the top