Skip to main content



      Home
Home » Modeling » TMF (Xtext) » XText Grammer for String assignment
XText Grammer for String assignment [message #62962] Fri, 24 July 2009 05:03 Go to next message
Eclipse UserFriend
Hi,
I have a requirement to get the below mentioned assignment operation
pass with xtext grammer

variable1 = This is the variable Value
variable2 = ?
variable3 = 12

I could not figure out a way to get the String assignment as a value. I
used STRING as well as "string" in the type. But none worked as is.


Model :
(variables += Variable)*;

Variable returns as Variable :
name = ID '=' value=(STRING | INT);

This doesnot work. Any help?
Re: XText Grammer for String assignment [message #62966 is a reply to message #62962] Fri, 24 July 2009 05:38 Go to previous messageGo to next message
Eclipse UserFriend
Hi Idicula,

which version of Xtext do you use? There have been changes from 0.7.0 to
0.7.1 for this usecase. However, since STRING and INT do not have a
common supertype, I do not know what you try to achieve. What's the type
that you expect for the attribtue Variable.value?

If you are on 0.7.0, you may try the following workaround:

Variable:
name=ID '=' (strValue=STRING | intValue=INT);

I would prefer a cleaner solution and use:

Model:
variables += Variable *;

Variable:
StringVariable | IntVariable;

StringVariable:
name=ID '=' strValue=STRING;

IntVariable:
name=ID '=' intValue=INT;

Regars,
Sebastian

--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 24.07.2009 11:03 Uhr, schrieb Idicula:
> Hi,
> I have a requirement to get the below mentioned assignment operation
> pass with xtext grammer
>
> variable1 = This is the variable Value
> variable2 = ?
> variable3 = 12
>
> I could not figure out a way to get the String assignment as a value. I
> used STRING as well as "string" in the type. But none worked as is.
>
>
> Model :
> (variables += Variable)*;
>
> Variable returns as Variable :
> name = ID '=' value=(STRING | INT);
>
> This doesnot work. Any help?
>
>
Re: XText Grammer for String assignment [message #62968 is a reply to message #62966] Fri, 24 July 2009 05:44 Go to previous messageGo to next message
Eclipse UserFriend
Hi Sebastian,
Thanks for the quick reply

What I see in Eclipse Plugin directory is
org.eclipse.xtext_0.7.0.v200906161042. So I assume it is 0.7.0.

But when I give strValue=STRING , it doesnot accept the first line

variable1 = This is the value of the variable

Which is what I am stuck at.
Re: XText Grammer for String assignment [message #62972 is a reply to message #62966] Fri, 24 July 2009 06:01 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

> Model:
> variables += Variable *;
>
> Variable:
> StringVariable | IntVariable;
>
> StringVariable:
> name=ID '=' strValue=STRING;
>
> IntVariable:
> name=ID '=' intValue=INT;

I think, this is not LL(1).

"Too many cooks spoil the broth." I do prefer something like this:

Model:
assigns += Assign* ;

Variable: name = ID;

Assign:
variable = Variable '=' value = Value ';';

Value: INT | STRING | ... ;

terminal INT returns ecore::EINT : ... ;
terminal STRING : ....;

=== end ___



Ciao, Micha


--- Original-Nachricht ---
Absender: Sebastian Zarnekow
Datum: 24.07.2009 11:38
> Hi Idicula,
>
> which version of Xtext do you use? There have been changes from 0.7.0 to
> 0.7.1 for this usecase. However, since STRING and INT do not have a
> common supertype, I do not know what you try to achieve. What's the type
> that you expect for the attribtue Variable.value?
>
> If you are on 0.7.0, you may try the following workaround:
>
> Variable:
> name=ID '=' (strValue=STRING | intValue=INT);
>
> I would prefer a cleaner solution and use:
>
> Model:
> variables += Variable *;
>
> Variable:
> StringVariable | IntVariable;
>
> StringVariable:
> name=ID '=' strValue=STRING;
>
> IntVariable:
> name=ID '=' intValue=INT;
>
> Regars,
> Sebastian
>
Re: XText Grammer for String assignment [message #62974 is a reply to message #62968] Fri, 24 July 2009 07:34 Go to previous messageGo to next message
Eclipse UserFriend
Hi Idicula,

STRINGS are expected to look like 'This is the value of the variable'
(including the quotes).
You should try to alter you grammar to match your syntax.
Please have a look at the reference documentation at
http://www.xtext.org for detailled information about the common terminal
rules and about terminal and data type rules in general.

Regards,
Sebastian

--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 24.07.2009 11:44 Uhr, schrieb Idicula:
> Hi Sebastian,
> Thanks for the quick reply
> What I see in Eclipse Plugin directory is
> org.eclipse.xtext_0.7.0.v200906161042. So I assume it is 0.7.0.
>
> But when I give strValue=STRING , it doesnot accept the first line
>
> variable1 = This is the value of the variable
>
> Which is what I am stuck at.
>
Re: XText Grammer for String assignment [message #62976 is a reply to message #62972] Fri, 24 July 2009 07:36 Go to previous messageGo to next message
Eclipse UserFriend
Hi Michael,

you are right, it is not LL(1) but LL(*). This should not expose a
performance problem in this case. However, if you want to stick with
LL(1), I'ld prefer something like:

Value:
IntValue | StringValue;

IntValue:
val=INT;
StringValue:
val=STRING;

Regards,
Sebastian

--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com


Am 24.07.2009 12:01 Uhr, schrieb Michael Burkhardt:
> Hi,
>
> > Model:
> > variables += Variable *;
> >
> > Variable:
> > StringVariable | IntVariable;
> >
> > StringVariable:
> > name=ID '=' strValue=STRING;
> >
> > IntVariable:
> > name=ID '=' intValue=INT;
>
> I think, this is not LL(1).
>
> "Too many cooks spoil the broth." I do prefer something like this:
>
> Model:
> assigns += Assign* ;
>
> Variable: name = ID;
>
> Assign:
> variable = Variable '=' value = Value ';';
>
> Value: INT | STRING | ... ;
>
> terminal INT returns ecore::EINT : ... ;
> terminal STRING : ....;
>
> === end ___
>
>
>
> Ciao, Micha
>
>
> --- Original-Nachricht ---
> Absender: Sebastian Zarnekow
> Datum: 24.07.2009 11:38
>> Hi Idicula,
>>
>> which version of Xtext do you use? There have been changes from 0.7.0
>> to 0.7.1 for this usecase. However, since STRING and INT do not have a
>> common supertype, I do not know what you try to achieve. What's the
>> type that you expect for the attribtue Variable.value?
>>
>> If you are on 0.7.0, you may try the following workaround:
>>
>> Variable:
>> name=ID '=' (strValue=STRING | intValue=INT);
>>
>> I would prefer a cleaner solution and use:
>>
>> Model:
>> variables += Variable *;
>>
>> Variable:
>> StringVariable | IntVariable;
>>
>> StringVariable:
>> name=ID '=' strValue=STRING;
>>
>> IntVariable:
>> name=ID '=' intValue=INT;
>>
>> Regars,
>> Sebastian
>>
Re: XText Grammer for String assignment [message #62997 is a reply to message #62974] Fri, 24 July 2009 15:23 Go to previous messageGo to next message
Eclipse UserFriend
Hi Sebastian,
As you said, I tried with terminal rule
-----------------------------------------------------
Model :
(variables = Variable)?;

Variable returns Variable:
(decls+=Decl)*;
Decl:
( name=ID '=' (valueInt=INT)|(valueStr=PLAIN_STRING));

terminal PLAIN_STRING :
( '\\' ('b'|'t'|'n'|'f'|'r'|'"'|"'"|'\\') | !('\\'|'"') )* |
( '\\' ('b'|'t'|'n'|'f'|'r'|'"'|"'"|'\\') | !('\\'|"'") )* ;

-------------------------------------------------------

But the below code cannot validated with the above grammer. Is there
anything wrong in the PLAIN_STRING - which is " and ' removed from the
original value.


test1=This is the value
test2=234

Any suggestions to improve the greedy PLAIN_STRING?
Re: XText Grammer for String assignment [message #62999 is a reply to message #62972] Fri, 24 July 2009 15:39 Go to previous messageGo to next message
Eclipse UserFriend
Hi,
I tried the following grammer as well. But no success so far in getting
a plain String as a token


Model :
(variables = Variable)?;

Variable returns Variable:
(decls+=Decl)*;
Decl:
( name=ID '=' value=PLAIN_STRING);

terminal PLAIN_STRING : !('\n'|'\r')* ('\r'? '\n')?;

------------------
to parse the below

variable1 = This is the variable Value
variable2 = This is another variable value

-----------------

I was wondering - properties file have similar syntax and there must be
some grammer already written for this style. Can anyone give pointers (
other than reference book - it is not clear) :-)
Re: XText Grammer for String assignment [message #63015 is a reply to message #62997] Mon, 27 July 2009 03:02 Go to previous messageGo to next message
Eclipse UserFriend
Hi Idicula,

I guess data type rules will be more helpful then terminal rules in your
use case. It is kind of difficult to understand the semantic of your
language from your short examplary snippet. Is the newline at the end of
your "string" mandatory?
Do you want to create a grammar for property files?

A first shot may look like this one:

Variable:
VarDecl '=' Value;

Value:
IntValue | StringValue;

StringValue returns ecore::EString hidden():
(ID | WS | INT)+ '\n';

Although this would not allow quoted values on the rhs of your assignment.

Hope that helps,
Sebastian

--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 24.07.2009 21:23 Uhr, schrieb Idicula:
> Hi Sebastian, As you said, I tried with terminal rule
> -----------------------------------------------------
> Model :
> (variables = Variable)?;
> Variable returns Variable:
> (decls+=Decl)*;
> Decl:
> ( name=ID '=' (valueInt=INT)|(valueStr=PLAIN_STRING));
>
> terminal PLAIN_STRING :
> ( '\\' ('b'|'t'|'n'|'f'|'r'|'"'|"'"|'\\') | !('\\'|'"') )* |
> ( '\\' ('b'|'t'|'n'|'f'|'r'|'"'|"'"|'\\') | !('\\'|"'") )* ;
>
> -------------------------------------------------------
>
> But the below code cannot validated with the above grammer. Is there
> anything wrong in the PLAIN_STRING - which is " and ' removed from the
> original value.
>
>
> test1=This is the value
> test2=234
>
> Any suggestions to improve the greedy PLAIN_STRING?
>
Re: XText Grammer for String assignment [message #63092 is a reply to message #63015] Mon, 27 July 2009 06:12 Go to previous messageGo to next message
Eclipse UserFriend
Hi Sebastian,

The value can contain any character ended by a new line. This would
mean all of the below are valid

variable1 = Hello World!
variable2 = hhhjjj + jhjhjsad _ kkh ~@;


stringValue - anything till the end of line after the equals.

regards
Binu K Idicula
Re: XText Grammer for String assignment [message #63105 is a reply to message #63092] Mon, 27 July 2009 06:29 Go to previous message
Eclipse UserFriend
Hi Idicula,

I don't know your usecase but it seems as if regular expressions may be
a good match. The only way that comes to my mind and will match your rhs
of an assignment without ambiguity is a terminal rule that starts with
an assignment operator '=' and consumes everthing until the end of the line.

Hope that helps,
Sebastian

--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 27.07.2009 12:12 Uhr, schrieb Idicula:
> Hi Sebastian,
>
> The value can contain any character ended by a new line. This would mean
> all of the below are valid
>
> variable1 = Hello World!
> variable2 = hhhjjj + jhjhjsad _ kkh ~@;
>
>
> stringValue - anything till the end of line after the equals.
>
> regards
> Binu K Idicula
>
Previous Topic:Providing hover documentation
Next Topic:Ecore meta model to Xtext grammar
Goto Forum:
  


Current Time: Mon Jul 21 21:35:20 EDT 2025

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

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

Back to the top