Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » rewrite ANTLR .g grammar in XText(import rewrite easily antlr grammar to xtext grammar)
rewrite ANTLR .g grammar in XText [message #649367] Tue, 18 January 2011 22:47 Go to next message
Sérgio Silva is currently offline Sérgio SilvaFriend
Messages: 10
Registered: January 2011
Junior Member
I have a .g grammar file which already describes a DSL. I would like to import this .g file into Xtext but I think that is not possible.

Nonetheless it should be possible to "easily" convert it to an equivalent Xtext grammar definition.

Do you have any experience with this ? Any tips, tutorials, articles, etc ... that can help me with this task ?

Thank you in advance!
Re: rewrite ANTLR .g grammar in XText [message #649425 is a reply to message #649367] Wed, 19 January 2011 08:28 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Sergio,

as the Xtext grammar contains information that is not available from an
Antlr grammar, it is impossible to derive it automatically. However,
pasting an Antlr grammar into an Xtext editor and subsequently fixing
the error markers is usually straight forward enough - at least if your
*.g file does not contain to many actions, predicates and rewrite rules
but just plain parser rules.

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

Am 18.01.11 23:47, schrieb sergiofbsilva@gmail.com:
> I have a .g grammar file which already describes a DSL. I would like to
> import this .g file into Xtext but I think that is not possible.
> Nonetheless it should be possible to "easily" convert it to an
> equivalent Xtext grammar definition.
>
> Do you have any experience with this ? Any tips, tutorials, articles,
> etc ... that can help me with this task ?
>
> Thank you in advance!
Re: rewrite ANTLR .g grammar in XText [message #649427 is a reply to message #649367] Wed, 19 January 2011 08:33 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 760
Registered: July 2009
Location: Hamburg
Senior Member
Xtext uses a subset of Antlr internally, so there are similarities.

OTOH, an Xtext grammar has syntax for describing the lexical structure
of a language and the construction of an EMF-based AST including
cross-references. In Antlr, the construction of the AST is usually
performed in Antlr actions, which let you insert any code in the parser.
The focus on a well defined AST often results a different kind of
grammar, i.e. one rule for each object type.

So the feature set of Antlr and Xtext are quiet different, and I am not
aware of any migration guide.

OTOH, your grammar is already LL*, so one of the big obstacles in
porting a grammar to Xtext is already solved. The migration will be
harder if your Antlr grammar uses
- semantic predicates
- syntatic predicates (partly implemented in Xtext 2.0)
- very complex code in actions

Similar concepts in both frameworks are
- Rules (terminal rules, parser rules)
- Rule calls
- Groups, alternatives and cardinalities

Additional in Xtext and likely to appear, so you might want to learn
about these concepts by reading the docs
- Assignments
- Return types of rules
- AST node creation



Am 18.01.11 23:47, schrieb sergiofbsilva@gmail.com:
> I have a .g grammar file which already describes a DSL. I would like to
> import this .g file into Xtext but I think that is not possible.
> Nonetheless it should be possible to "easily" convert it to an
> equivalent Xtext grammar definition.
>
> Do you have any experience with this ? Any tips, tutorials, articles,
> etc ... that can help me with this task ?
>
> Thank you in advance!


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


---
Get professional support from the Xtext committers at www.typefox.io
Re: rewrite ANTLR .g grammar in XText [message #649518 is a reply to message #649427] Wed, 19 January 2011 14:44 Go to previous messageGo to next message
Sérgio Silva is currently offline Sérgio SilvaFriend
Messages: 10
Registered: January 2011
Junior Member
Thank you very much for your tips!

I started from the example and adapted my .g grammar nicely Smile
Re: rewrite ANTLR .g grammar in XText [message #649870 is a reply to message #649518] Thu, 20 January 2011 19:38 Go to previous messageGo to next message
Sérgio Silva is currently offline Sérgio SilvaFriend
Messages: 10
Registered: January 2011
Junior Member
Well I still have a problem with some grammar issue that I can't see how to solve it.


suppose that my grammar is :

Class:
  'class' name=CLASS_NAME ('extends' superType=CLASS_NAME)? '{'
    (classSlots+=Slot)*
  '}';

RoleDefinition :
	type=[Class] 'playsRole' roleName=ID (('{' 
		(roleProperty += RoleProperty)+
		'}')? | ';')
	;


So this will match something like :

class Xpto {
     String field1;
     Integer field2;
     Boolean field1;
}

class Zen {
     String field1;
}

class Zorg {}

Zorg playsRole zorg;


So in the type of RoleDefinition I will have a cross reference to a Class and it works.

The problem is that I want this type to be a Class cross-reference or a fully qualitfied String (x.slkdasl.askdasl)

So this 2 possiblities should be possible :

1) x.slkdasl.askdasl playsRole zorg;
2) Zorg playsRole zorg;

Any help with this ?

[Updated on: Thu, 20 January 2011 19:39]

Report message to a moderator

Re: rewrite ANTLR .g grammar in XText [message #649873 is a reply to message #649870] Thu, 20 January 2011 19:54 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14677
Registered: July 2009
Senior Member
Hi,

what about something like that (simplyfied)

the interesting part is [Class|CLASS_NAME]

grammar org.xtext.example.mydsl1.MyDsl1 with org.eclipse.xtext.common.Terminals

generate myDsl1 "http://www.xtext.org/example/mydsl1/MyDsl1"

Model:
	classes+=Class*
	roledefs+=RoleDefinition*
	;
	
Class:
  'class' name=CLASS_NAME ('extends' superType=CLASS_NAME)? '{'
   
  '}';

RoleDefinition :
	type=[Class|CLASS_NAME] 'playsRole' roleName=ID (('{' 
	
		'}')? | ';')
	;
	
CLASS_NAME : ID ("."ID)*;




class Xpto { 
}

class Zen {
}

class Zorg {}

class x.slkdasl.askdasl {}

x.slkdasl.askdasl playsRole zorg

Zorg playsRole zorg;


~Christian


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

[Updated on: Thu, 20 January 2011 19:55]

Report message to a moderator

Re: rewrite ANTLR .g grammar in XText [message #649875 is a reply to message #649873] Thu, 20 January 2011 20:08 Go to previous messageGo to next message
Sérgio Silva is currently offline Sérgio SilvaFriend
Messages: 10
Registered: January 2011
Junior Member
tried that before, doesn't work it underlines the class reference and says

something playsRole otherthing;

Couldn't resolve reference to Class 'something' :-/

EDIT :

I've checked your example and you have the class ldksa.dsalkdsa.sldksa defined before... I don't want to do that.

I want to have or a Class Reference to a class already defined, or just a FQN not related to any class at all.

[Updated on: Thu, 20 January 2011 20:10]

Report message to a moderator

Re: rewrite ANTLR .g grammar in XText [message #649879 is a reply to message #649875] Thu, 20 January 2011 20:17 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14677
Registered: July 2009
Senior Member
Ok,

this there any difference between CLASS_NAME and the x.slkdasl.askdasl
thing?

so if you have

a playsRole x
b playsRole x

where from xtext should decide if a and b are refs to classes or names?

or to be more precise. have a look at this excerpt from the docs.

Quote:

Basically parsing can be separated in the following phases.

1.lexing
2.parsing
3.linking
4.validation




so both cases have to distingusihed during paring

~Christian


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

[Updated on: Thu, 20 January 2011 20:29]

Report message to a moderator

Re: rewrite ANTLR .g grammar in XText [message #649895 is a reply to message #649879] Thu, 20 January 2011 21:41 Go to previous messageGo to next message
Sérgio Silva is currently offline Sérgio SilvaFriend
Messages: 10
Registered: January 2011
Junior Member
Christian Dietrich wrote on Thu, 20 January 2011 15:17
Ok,

this there any difference between CLASS_NAME and the x.slkdasl.askdasl
thing?

so if you have

a playsRole x
b playsRole x

where from xtext should decide if a and b are refs to classes or names?

or to be more precise. have a look at this excerpt from the docs.

Quote:

Basically parsing can be separated in the following phases.

1.lexing
2.parsing
3.linking
4.validation




so both cases have to distingusihed during paring

~Christian


I feel like I am tamming a beast with a chopstick Razz
class House {

}

class Building {

}


House playsRole house;

Building playsRole house; 

org.example.ikea.Kitchen playsRole kitchen;


So I need to support both of these. The thing is org.example.ikea.Kitchen is an external class and it is not defined in this scope.

So it should be possible to reference a Class like House and Building which is just an ID not an FQN and use FQN for everything else.

[Updated on: Thu, 20 January 2011 21:42]

Report message to a moderator

Re: rewrite ANTLR .g grammar in XText [message #649897 is a reply to message #649895] Thu, 20 January 2011 21:47 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14677
Registered: July 2009
Senior Member
Hi,

if there is really a parseable diffference this is no problem at least with backtracking enabled (note: bla is then not a FQN Wink)

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

Model:
	classes+=Class*
	roledefs+=RoleDefinition*
	;
	
Class:
  'class' name=ID ('extends' superType=ID)? '{'
   
  '}';

RoleDefinition :
	(name=FQN | ref=[Class|ID]) 'playsRole' roleName=ID (('{' 
	
		'}')? | ';')
	;
	
FQN : ID ("."ID)+;


module org.xtext.example.mydsl.MyDsl

import org.eclipse.emf.mwe.utils.*
import org.eclipse.xtext.generator.*
import org.eclipse.xtext.ui.generator.*

var grammarURI = "classpath:/org/xtext/example/mydsl/MyDsl.xtext"
var file.extensions = "mydsl"
var projectName = "org.xtext.example.mydsl"
var runtimeProject = "../${projectName}"

Workflow {
    bean = StandaloneSetup {
		platformUri = "${runtimeProject}/.."
	}

	component = DirectoryCleaner {
		directory = "${runtimeProject}/src-gen"
	}

	component = DirectoryCleaner {
		directory = "${runtimeProject}.ui/src-gen"
	}

	component = Generator {
		pathRtProject = runtimeProject
		pathUiProject = "${runtimeProject}.ui"
		projectNameRt = projectName
		projectNameUi = "${projectName}.ui"
		language = {
			uri = grammarURI
			fileExtensions = file.extensions

			// Java API to access grammar elements (required by several other fragments)
			fragment = grammarAccess.GrammarAccessFragment {}

			// generates Java API for the generated EPackages 
			fragment = ecore.EcoreGeneratorFragment {
			// referencedGenModels = "uri to genmodel, uri to next genmodel"
			}

			// the serialization component
			fragment = parseTreeConstructor.ParseTreeConstructorFragment {}

			// a custom ResourceFactory for use with EMF 
			fragment = resourceFactory.ResourceFactoryFragment {
				fileExtensions = file.extensions
			}

			// The antlr parser generator fragment.
			fragment = parser.antlr.XtextAntlrGeneratorFragment {
			  options = {
					backtrack = true
				}
			}

			// java-based API for validation 
			fragment = validation.JavaValidatorFragment {
				composedCheck = "org.eclipse.xtext.validation.ImportUriValidator"
				composedCheck = "org.eclipse.xtext.validation.NamesAreUniqueValidator"
				// registerForImportedPackages = true
			}

			// scoping and exporting API
			// fragment = scoping.ImportURIScopingFragment {}
			// fragment = exporting.SimpleNamesFragment {}

			// scoping and exporting API 
			fragment = scoping.ImportNamespacesScopingFragment {}
			fragment = exporting.QualifiedNamesFragment {}
			fragment = builder.BuilderIntegrationFragment {}

			// formatter API 
			fragment = formatting.FormatterFragment {}

			// labeling API 
			fragment = labeling.LabelProviderFragment {}

			// outline API 
			fragment = outline.TransformerFragment {}
			fragment = outline.OutlineNodeAdapterFactoryFragment {}
			fragment = outline.QuickOutlineFragment {}

			// quickfix API 
			fragment = quickfix.QuickfixProviderFragment {}

			// content assist API  
			fragment = contentAssist.JavaBasedContentAssistFragment {}

			// generates a more lightweight Antlr parser and lexer tailored for content assist  
			fragment = parser.antlr.XtextAntlrUiGeneratorFragment {
				
				  options = {
					backtrack = true
				}
			}

			// project wizard (optional) 
			// fragment = projectWizard.SimpleProjectWizardFragment {
			// 		generatorProjectName = "${projectName}.generator" 
			//		modelFileExtension = file.extensions
			// }
		}
	}
}



Update:

to get Content Assist working better consider using this grammar

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

Model:
	classes+=Class*
	roledefs+=RoleDefinition*
	;
	
Class:
  'class' name=ID ('extends' superType=ID)? '{'
   
  '}';

RoleDefinition :
	thing=Thing 'playsRole' roleName=ID (('{' 
	
		'}')? | ';')
	;
	
Thing : Named | Ref;
Named: name=FQN;
Ref: ref=[Class|ID];
	
	
FQN : ID ("."ID)+;


~Christian


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

[Updated on: Thu, 20 January 2011 21:55]

Report message to a moderator

Re: rewrite ANTLR .g grammar in XText [message #649909 is a reply to message #649897] Thu, 20 January 2011 22:20 Go to previous messageGo to next message
Sérgio Silva is currently offline Sérgio SilvaFriend
Messages: 10
Registered: January 2011
Junior Member
Christian Dietrich wrote on Thu, 20 January 2011
16:47



Dude thanks a lot you solved all my problems and gave me so much insight about all this thing works!

I'm so happy ATM Very Happy

[Updated on: Thu, 20 January 2011 22:20]

Report message to a moderator

Re: rewrite ANTLR .g grammar in XText [message #649975 is a reply to message #649870] Fri, 21 January 2011 09:19 Go to previous message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 760
Registered: July 2009
Location: Hamburg
Senior Member
Am 20.01.11 20:38, schrieb Sérgio Silva:
> Well I still have a problem with some grammar issue that I can't see how
> to solve it.
>
>
> suppose that my grammar is :
>
> Class:
> 'class' name=CLASS_NAME ('extends' superType=CLASS_NAME)? '{'
> (classSlots+=Slot)*
> '}';
>
> RoleDefinition :
> type=[Class] 'playsRole' roleName=ID (('{' (roleProperty += RoleProperty)+
> '}')? | ';')
> ;
>
> So this will match something like :
>
> class Xpto {
> String field1;
> Integer field2;
> Boolean field1;
> }
>
> class Zen {
> String field1;
> }
>
> class Zorg {}
>
>
> Zorg playsRole zorg;
>
> So in the type of RoleDefinition I will have a cross reference to a
> Class and it works.
>
> The problem is like I want this type to be a Class cross-reference or a
> fully qualitfied String (x.slkdasl.askdasl)
>
> So this 2 possiblities should be possible :
>
> 1) x.slkdasl.askdasl playsRole zorg;
> 2) Zorg playsRole zorg;
>
> Any help with this ?

Yes: Read the Xtext docs ;-)

(Getting started->Creating a DSL->Adding a namespace concept)
You need a datatype rule for qualified names and use that in the cross
reference.
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com


---
Get professional support from the Xtext committers at www.typefox.io
Previous Topic:how to run the GMF integration example
Next Topic:a string representation for program elements (EObjects)
Goto Forum:
  


Current Time: Sat May 04 15:10:13 GMT 2024

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

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

Back to the top