Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Problems with grammar generator
Problems with grammar generator [message #1459356] Sun, 02 November 2014 11:38 Go to next message
Daniele P. is currently offline Daniele P.Friend
Messages: 18
Registered: August 2014
Junior Member
Hi,
i'm trying to create a custom generator for my grammar.
I have some problems with var declaration, because i don't know how to obtain optional value for declaration.
i have this grammar

Quote:
Model:
elements+=Element*
;
Type:
SimpleType
;
SimpleType:
type = BaseType
;
enum BaseType:
INT_TYPE = "int" | BOOL_TYPE = 'bool'
;
enum BoolValue:
True = "true" | False = "false"
;
Signature:
type=Type name=ID
;
Element: VarDeclaration; /*| FunctionDeclaration;*/

//DICHIARAZIONE DELLE VARIABILI
VarDeclaration:
var=Signature ('=' init=Expression)? ";"
;

Expression:
OrExpression
;
OrExpression returns Expression:
AndExpression ( { OrExpression.left=current } "||" right=OrExpression )?
;
AndExpression returns Expression:
Relation ( { AndExpression.left=current } "&&" right=AndExpression )?
;
Relation returns Expression:
SumExpression ( {Relation.left=current} op=comparisonOperators right=SumExpression )?
;
SumExpression returns Expression:
MulExpression ( {SumExpression.left=current} op=('+'|'-'|'%') right=SumExpression)?
;
MulExpression returns Expression:
BaseExpression ( {MulExpression.left=current} op=('*'|'/') right=MulExpression)?
;

BaseExpression returns Expression:
IntConstant
| BoolConstant
;
IntConstant:
value=INT
;

BoolConstant:
value=BoolValue
;


This is a part of my Generation class:

Quote:
def toJavaCode(Model model) '''
import «model.importSection.importedNamespace»

public class «model.eResource.className» {

public static void main(String[] args) {

}

«FOR c : model.elements.filter(typeof(VarDeclaration))»
«c.declareVariable»
«ENDFOR»
}
'''

def declareVariable(VarDeclaration command) '''
«IF command.^var.type.toString.contains("int")»
private int «command.^var.getName()» = «command.getInt()»
«ELSEIF command.^var.type.toString.contains("bool")»
private boolean «command.^var.getName()» = «command.getInt()»
«ENDIF»
'''


For example if i write,

Quote:
int value = 5;


i obtain

Quote:
private int value = org.xtext.mydsl.smallJava.impl.IntConstantImpl@67cbff1c (value: 5)


Why? How can i obtain the value 5? Thank you in advance
Re: Problems with grammar generator [message #1459363 is a reply to message #1459356] Sun, 02 November 2014 11:46 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14669
Registered: July 2009
Senior Member
Hi

IntConstant:
value=INT
;


creates an object that has an attribute value

but you simply do print (toString) the Object Itself

you should introduce some toJava mechanism for Expressions using xtends dispatch methods


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Can I parse a string and get the EMF model?
Next Topic:xtext dslexecutableExtensionFactory class cannot be loaded
Goto Forum:
  


Current Time: Fri Apr 26 20:02:01 GMT 2024

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

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

Back to the top