Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » code generation using xtend(xtext)
code generation using xtend [message #1411337] Sat, 23 August 2014 02:33 Go to next message
Eclipse GuestFriend
Messages: 93
Registered: February 2013
Location: Vienna
Member

Hello, I have studied Xtext using eclipse, these day.
I have a trouble with xtext.
First of all, I made grammar xtext file.
I try to generate to c++ code from my dsl (like c).
but, I can't find the solution.
I am seeking to implement the generating code with xtend.
But, I can't.
Please Advice me about the solution.
bye~
Re: code generation using xtend [message #1411418 is a reply to message #1411337] Sat, 23 August 2014 08:30 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

your question is as inspecific as it can be. code generation using igenerator is basically generating text file.
if these text files are Assembler, C, C++, Java or whatever PL Files depends on the text you generate.
did you do this tutorial: https://www.eclipse.org/Xtext/documentation.html#TutorialCodeGeneration



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: code generation using xtend [message #1412515 is a reply to message #1411418] Tue, 26 August 2014 10:47 Go to previous messageGo to next message
kim younghyun is currently offline kim younghyunFriend
Messages: 4
Registered: August 2014
Junior Member
Hello!

I have trouble with code generation problem.
I wrote my grammar file.
but I can't make the way how to generate code using xcode.
Please help and advice me. !!!
---------------------------------------------------------------
[MyGrammmar]

BUILDERMAIN:
'RULE' '{' buildermain+=BRDStatement+ '}';

BRDStatement:
BRDEntityDeclaration |
BRDVariableDeclaration |
BRDIFStatement |
BRDWhileStatement
;

BRDEntityDeclaration: Entity;

Entity:
'entity' name=ID '{'
attribute += Attributes*
'}'

Attributes:
type=BasicType name=ID ';'
;

BRDVariableDeclaration:
BasicType | BRDDeclaration ';' ;

BRDDeclaration:
(type=[Entity] '.')? name=ID '=' expression=BRDExpression;

BRDExpression: OrExpr;

OrExpr returns BRDExpressoin:
AndExpr ({OrExpr.left=current} "||" right=AndExpr)*
;

AndExpr returns BRDExpression:
RelationExpr ({AndExpr.left=current} "&&" right=RelationExpr )*
;

RelationExpr returns BRDExpression:
AddExpr({BinaryOpExpr.left=current} op=("=="|"!="|">="|"<="|">"|"<") right=AddExpr)*
;

AddExpr returns BRDExpression:
MinusExpr ({BinaryOpExpr.left=current} '+' right=MinusExpr)*
;

MinusExpr returns BRDExpression:
MulOrDivExpr ({BinaryOpExpr.left=current} '-' right=MulOrDivExpr)*
;

MulOrDivExpr returns BRDExpression:
Primary ({BinaryOpExpr.left=current} op=('*'|'/') right=Primary)*
;

Primary returns BRDExpression:
'(' BRDExpression ')' |
{NOT} "!" expression=Primary | BRDTerminalExpression
;

BRDIFStatement:
'if' '(' BRDExpression ')' thenBlock=BRDIFBlock
(=> 'else' elseBlock=BRDIFBlock)?
;

BRDIFBlock:
ifstatements+=BRDStatement
| '{' ifstatements += BRDStatement+ BRDBreak? '}';


BRDWhileStatement:
'while' '(' BRDExpression ')' whileBlock=BRDWhileBlock;

BRDWhileBlock:
whilestatements+=BRDStatement
| '{' whilestatements += BRDStatement+ BRDBreak? '}';

BRDBreak:
'break' ';'
;

BasicType: Typename=('string'|'int'|'bool'|'float');

BRDTerminalExpression returns BRDExpressoin:
{BRDStringConstant} value = STRING |
{BRDIntConstant} value = INT |
{BRDFloatConstant} value = FLOAT |
{BRDBoolConstant} value = ('true' | 'false') |
{BRDVariableRef} value = [BRDDeclaration] |
{BRDEntity} value = [Entity] '.' name=ID
;

---------------------------------------------------------------
[My Xtend code]

I'm trying to make a header file and source file like C++ code.
But I can't make a body file.

// File for Header
def compile(Entity entity)
{
...
public class <<entity.name>> {
private:
<<FOR attribute : entity.attributes>>
<<attribute.type.typename>> <<attribute.name>>;
<<ENDFOR>>

public:
<<FOR attribute : entity.attributes>>
static <<attribute.type.typename>> get<<attribute.name.toFirstUpper>>() {
return <<attribute.name>>;
}

static void set<<attribute.name.toFirstUpper>> (<<attribute.name>> _arg) {
this.<<attribute.name>> = _arg;
}
<<ENDFOR>>
'''
}

// BODY FILE
def bodycompile()
{
???
}

----------------------------------------------------------
[My Entity window]

RULE {
entity e1 {
int a;
int b;
}
entity e2 {
int ia;
int ib;
}

int ix=0;
if (ix == 1)
{
e1.a = e2.ia;
e1.b = e2.ib;
}
while (ix < 5)
{
ix = ix + 1;
}
}
Re: code generation using xtend [message #1412516 is a reply to message #1411418] Tue, 26 August 2014 10:48 Go to previous messageGo to next message
kim younghyun is currently offline kim younghyunFriend
Messages: 4
Registered: August 2014
Junior Member
Hello!

I have trouble with code generation problem.
I wrote my grammar file.
but I can't make the way how to generate code using xcode.
Please help and advice me. !!!
---------------------------------------------------------------
[MyGrammmar]


BUILDERMAIN:
'RULE' '{' buildermain+=BRDStatement+ '}';

BRDStatement:
BRDEntityDeclaration |
BRDVariableDeclaration |
BRDIFStatement |
BRDWhileStatement
;

BRDEntityDeclaration: Entity;

Entity:
'entity' name=ID '{'
attribute += Attributes*
'}'

Attributes:
type=BasicType name=ID ';'
;

BRDVariableDeclaration:
BasicType | BRDDeclaration ';' ;

BRDDeclaration:
(type=[Entity] '.')? name=ID '=' expression=BRDExpression;

BRDExpression: OrExpr;

OrExpr returns BRDExpressoin:
AndExpr ({OrExpr.left=current} "||" right=AndExpr)*
;

AndExpr returns BRDExpression:
RelationExpr ({AndExpr.left=current} "&&" right=RelationExpr )*
;

RelationExpr returns BRDExpression:
AddExpr({BinaryOpExpr.left=current} op=("=="|"!="|">="|"<="|">"|"<") right=AddExpr)*
;

AddExpr returns BRDExpression:
MinusExpr ({BinaryOpExpr.left=current} '+' right=MinusExpr)*
;

MinusExpr returns BRDExpression:
MulOrDivExpr ({BinaryOpExpr.left=current} '-' right=MulOrDivExpr)*
;

MulOrDivExpr returns BRDExpression:
Primary ({BinaryOpExpr.left=current} op=('*'|'/') right=Primary)*
;

Primary returns BRDExpression:
'(' BRDExpression ')' |
{NOT} "!" expression=Primary | BRDTerminalExpression
;

BRDIFStatement:
'if' '(' BRDExpression ')' thenBlock=BRDIFBlock
(=> 'else' elseBlock=BRDIFBlock)?
;

BRDIFBlock:
ifstatements+=BRDStatement
| '{' ifstatements += BRDStatement+ BRDBreak? '}';


BRDWhileStatement:
'while' '(' BRDExpression ')' whileBlock=BRDWhileBlock;

BRDWhileBlock:
whilestatements+=BRDStatement
| '{' whilestatements += BRDStatement+ BRDBreak? '}';

BRDBreak:
'break' ';'
;

BasicType: Typename=('string'|'int'|'bool'|'float');

BRDTerminalExpression returns BRDExpressoin:
{BRDStringConstant} value = STRING |
{BRDIntConstant} value = INT |
{BRDFloatConstant} value = FLOAT |
{BRDBoolConstant} value = ('true' | 'false') |
{BRDVariableRef} value = [BRDDeclaration] |
{BRDEntity} value = [Entity] '.' name=ID
;

---------------------------------------------------------------
[My Xtend code]

I'm trying to make a header file and source file like C++ code.
But I can't make a body file.

// File for Header
def compile(Entity entity)
{
...
public class <<entity.name>> {
private:
<<FOR attribute : entity.attributes>>
<<attribute.type.typename>> <<attribute.name>>;
<<ENDFOR>>

public:
<<FOR attribute : entity.attributes>>
static <<attribute.type.typename>> get<<attribute.name.toFirstUpper>>() {
return <<attribute.name>>;
}

static void set<<attribute.name.toFirstUpper>> (<<attribute.name>> _arg) {
this.<<attribute.name>> = _arg;
}
<<ENDFOR>>
'''
}

// BODY FILE
def bodycompile()
{
???
}

----------------------------------------------------------
[My Entity window]

RULE {
entity e1 {
int a;
int b;
}
entity e2 {
int ia;
int ib;
}

int ix=0;
if (ix == 1)
{
e1.a = e2.ia;
e1.b = e2.ib;
}
while (ix < 5)
{
ix = ix + 1;
}
}
Re: code generation using xtend [message #1412560 is a reply to message #1412516] Tue, 26 August 2014 13:02 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
?????

to generate a file you have to call filesystemaccess.generateFile ?!?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: code generation using xtend [message #1412739 is a reply to message #1412560] Wed, 27 August 2014 00:35 Go to previous messageGo to next message
kim younghyun is currently offline kim younghyunFriend
Messages: 4
Registered: August 2014
Junior Member
Thank you for your reply.
In xtend generator,

override void doGenerator(Resource res, IFileSystemAccess fsa)
{
for (e:res.allContents.toIterable.filter(typeof(Entity))) {
fsa.generateFile("include/"+e.name+".h", e.compile);
}
fsa.generateFile( "src/test.cpp", bodycompil);
}

I try to make *.h and *.c
???? means I don't know how to write a code for test.c.


Re: code generation using xtend [message #1412807 is a reply to message #1412739] Wed, 27 August 2014 05:45 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
hi,

i dont understand your question.
it is your grammar. you know your target environment (the c code to be generated)
this you have to write the xtend code that does the transformation.

do you have any specific question?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: code generation using xtend [message #1412813 is a reply to message #1412807] Wed, 27 August 2014 06:02 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
btw shouldnt it be

def bodycompile(Resource it)
{
'''
//this is the body
'''


}


p.s. maybe some basic reading on xtend-lang.org may help,
specially https://www.eclipse.org/xtend/documentation.html#templates


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Wed, 27 August 2014 06:02]

Report message to a moderator

Re: code generation using xtend [message #1413224 is a reply to message #1412813] Thu, 28 August 2014 05:12 Go to previous messageGo to next message
kim younghyun is currently offline kim younghyunFriend
Messages: 4
Registered: August 2014
Junior Member
Thank you for your concern.

I'm going to make a file using xtext's generator.

first, test.hpp ( implemented by ENTITY/ATTRIBUTE )
second, test.cpp ( other part (except entity rule) )

the other part is
copy to test.cpp from my_script except for ENTITY/ATTRIBUTE.

How can I make copy code that using xtend? (I don't know how to apply recursive rule in Xtend)

I'm beginner xtend, I don't know how to use resource.

Pls advice me more.

bye.

Re: code generation using xtend [message #1413260 is a reply to message #1413224] Thu, 28 August 2014 07:06 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

i cannot help you write your xtend code.
if you cannot cope with xtend then write the code in java and call the java code from xtend.

method calls and recursions work in xtend as they work in java.

you can find the xtend documentation here /xtend/documentation.html




Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Non-empty model in the beginning
Next Topic:Getting an IXtextDocument without open editor
Goto Forum:
  


Current Time: Wed Apr 24 16:03:55 GMT 2024

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

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

Back to the top