[antlr-interest] Generate EMF

Vidar Håkestad vidar at hawkis.com
Wed Apr 26 07:40:26 PDT 2006


In an action for the compDef production try 

compDef [Model bipModel]
    :  #(COMPONENT   id:IDENTIFIER body  END)
	{
		Component cf = ComponentF.createComponent();
		cf.setName(id.getText());
		bipModel.getCompList().add(cf);

		// Show a string representation of your AST:
		//
		System.out.println(#compDef.toStringTree());
	}

There are other tools (visitor patterns) that can be used for traversing the 
syntax tree, too. See documentation.

In the code where you instanciate the lexer and parser (and hand over some 
text to be parsed), you typically have code like:

BipTree bipTree = new BipTree(
                              new BipLexer(
                                  new FileReader("/file/to/be/parsed.EMF")));
Then you can do:
  AST ast = bipTree.getAST(); // To retrieve the syntax tree.


Regards
Hawkis

On Wednesday 26 April 2006 12:03, Yassin.Chkouri at imag.fr wrote:
> Hi,
> This is my program in ANTLR and i want te generate my EMF model.
>
> I have already create a model EMF for that I use the FACTORY to called the
> components.
>
> when I compile my grammar I don't find errors but  it genère nothing, it is
> necessary that I add a function in Main or ..???
>
> Grammar in ANTLR
> *************************************************************************
> header
> {
> package ujf.verimag.bip.parser;
>
>
> }
>
> {
> import ujf.verimag.bip.component.*;
> import ujf.verimag.bip.behavior.*;
> import ujf.verimag.bipmodel.bip.*;
>
> import ujf.verimag.bip.connector.*;
> import ujf.verimag.bip.component.ComponentFactory;
>
> import org.eclipse.emf.common.util.EList;
> import java.util.Vector;
> import java.util.Iterator;
> import java.util.Set;
> import java.util.HashSet;
> import org.eclipse.emf.ecore.util.EcoreUtil;
> import org.eclipse.emf.ecore.resource.Resource;
> import org.eclipse.core.resources.IResource;
>
> import antlr.TokenStreamRecognitionException;
>
> }
>
>  class BipTree extends TreeParser;
>
> options {
>     buildAST=true;
>
> 	defaultErrorHandler=false;
> }
> {
> protected BipFactory BipF = BipFactory.eINSTANCE;
> protected ComponentFactory ComponentF = ComponentFactory.eINSTANCE;
> protected BehaviorFactory BehaviorF = BehaviorFactory.eINSTANCE;
>
>
> }
> model returns [Model bipModel = BipF.createModel()]
>     {
>     	Component compDef;
>     }
>
> 	:	(compDef[bipModel])+
>
> 	;
>
>
>
> compDef [Model bipModel]
>
>     :  #(COMPONENT   id:IDENTIFIER body  END)
>
> 	{
> 		Component cf = ComponentF.createComponent();
> 		cf.setName(id.getText());
> 		bipModel.getCompList().add(cf);
> 	}
>
>
>
>     ;
>
> body :  IDENTIFIER;
>
>
> class BipParser extends Parser;
> options {
>     buildAST=true;
> 	k=4;
> 	codeGenBitsetTestThreshold=999;
> 	defaultErrorHandler=false;
> }
>
> model
>
> 	(fileItem )+
> 	EOF
>
> ;
>
> fileItem
>
>     :  compDef
>     :
>       | globalDef
>
> ;
>
>
> compDef : 	COMPONENT^ 	IDENTIFIER 	 body  END
> ;
>
> globalDef : IDENTIFIER;
>
> body :  IDENTIFIER ; //behavior;
>
>
> class BipLexer extends Lexer;
> tokens
> {
> COMPONENT="component";
> END="end";
> BEHAVIOR="behavior";
> STATE="state";
> WITH="with";
> BIPSPEC;
> }
>
> WS	:	(' '
>
> 	|	'\t'
> 	|	'\n'
> 	|	'\r')
>
> 		{ _ttype = Token.SKIP; }
> 	;
>
> LPAREN:	'('
> 	;
>
> RPAREN:	')'
> 	;
>
> STAR:	'*'
> 	;
>
> PLUS:	'+'
> 	;
>
> SEMI:	';'
> 	;
>
> INT	:	('0'..'9')+
> 	;
>
> IDENTIFIER
>   options {testLiterals=true;}
>
>   : ('a'..'z'|'A'..'Z') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
>
> ;
>
> Quoting Marc Pantel <Marc.Pantel at enseeiht.fr>:
> > Hi,
> >
> > 2 answers were given on the TOPCASED mailing-list (before the question
> > was issued a second time), they were not forwarded to the antlr mailing
> > list as it is not really ANTLR related. I will summarise for the ANTLR
> > community :
> >
> > EMF = Eclipse Modelling Framework, an MDE toolkit in Eclipse. EMF takes
> > as input a meta-model expressed either in the ECORE meta-modelling
> > language, in the UML2 class diagram (ECORE is a subset of UML2 class
> > diagram), in JMI (Java Metadata Interface) with specific tags or in SXD
> > (XML Schema) and produces Java classes (in an 3 separate Eclipse plugin)
> > for :
> >
> > - Model creation, XMI and XML reader and writer, model visitor and
> > listener,
> > - Hierarchical (à la XML) model editor.
> >
> > It allows to manipulate models (that is abstract syntax graph).
> >
> > If you want to add a concrete syntax, you must use additional tools to
> > manage the relation between the concrete and abstract syntaxes :
> >
> > - graphical syntax : GMF, TOPCASED, TIGER, ... which allows to
> > description the graphical syntax and its relation to the abstract syntax
> > and generates a graphical editor for the EMF generated Java classes ;
> >
> > - textual syntax : currently some tools are being written in the AMMA
> > platform, the KERMETA project and the TOPCASED project but currently, to
> > my knowledge, nothing is available for external use. Therefore, you need
> > to write your own concrete grammar and attributed semantics in order to
> > parse a concrete textual model and produce an abstract model using the
> > EMF generated classes in the ANTLR attributed grammar.
> >
> > I hope my comments are precise enough. Ask for more if needed.
> >
> > Marc Pantel,
> > TOPCASED project
> >
> >   Le lundi 24 avril 2006 à 14:13 +0200, Martin Probst a écrit :
> > > Hi,
> > >
> > > I notice that this is the second time you ask your question and no
> > > one answered the first time. This might be because no one understands
> > > what you're asking about. Maybe you could give a hint what a EMF is
> > > and in what way it might correspond to a parsed piece of code?
> > >
> > > ANTLR does not include any support for anything called "EMF" as far
> > > as I know, so it's definitely not supported out of the box. It might
> > > be easy to hack in yourself, though.
> > >
> > > Martin
> > >
> > > Am 24.04.2006 um 11:20 schrieb Yassin.Chkouri at imag.fr:
> > > > Hi,
> > > >  I am using ANTLR for parsing a language and build
> > > >  the corresponding EMF. Is it possible to directly generate
> > > >  the EMF from the semantic actions in ANTLR or is it necessary
> > > >  to use the intermediate AST generated by ANTLR ?
> > > >
> > > >  Thanks,
> > > >
> > > >
> > > > -------------------------------------------------
> > > > envoyé via Webmail/IMAG !
> > >
> > > _______________________________________________
> > > Topcased-mm-users mailing list
> > > Topcased-mm-users at lists.gforge.enseeiht.fr
> > > http://lists.gforge.enseeiht.fr/mailman/listinfo/topcased-mm-users
>
> -------------------------------------------------
> envoyé via Webmail/IMAG !


More information about the antlr-interest mailing list