Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Next step after defining a grammar
Next step after defining a grammar [message #676492] Sat, 04 June 2011 20:26 Go to next message
Joerg is currently offline JoergFriend
Messages: 5
Registered: May 2011
Junior Member
Start working with Xtext and exploring the limits I'd like to begin with a very simple application:

1) Read a source code text file.
2) Compile the source code text to a virtual machine (some simple binary commands, defining a very simple machine code).

For the first step I created a grammar that will accept some program elements like methods, fields and method invocation. I also created a small text file that defines a program in the dsl and is accepted by the same grammar. This source text file defining some fields, methods and method invocation.

The next step will be adding some actions to the nodes of the tree when some sort of compile button is hit to allow writing an output file containing the machine code instructions that can be executed by a (not yet implemented) virtual processor.


Now I'm struggling with the next logical steps:

A) How to realize a compile button/compile command?

B) How to add actions to the nodes of the AST? I'd like to write the machine instructions according to the source code text file to create the binary code that will later be read by the virtual machine for execution.

C) I'm planning for a different approach mapping the source code file to java classes and object. For this approach I have to create the appropriate java classes while parsing the source code text file and later for program execution I have to instantiate the objects out of the java classes created before.

Are there any examples (and documentation) available for these three steps?

Thanks for your help,

Jörg
Re: Next step after defining a grammar [message #676496 is a reply to message #676492] Sat, 04 June 2011 20:58 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

(A) there are different possibilities depending on how the "command/button" should look like. you can do this by a std eclipse command. just load the model and process it from the command code (create resourceset + load resource + process its contents) or you do it on the fly (aka builder integation) as it is done in the domain model example. following link might be interesting too
http://coopology.com/2011/06/easily-load-xtext-files-and-objects-in-eclipse-plugin-or-rcp-projects-using-adapters/
public class MyHandler extends AbstractHandler {

	@SuppressWarnings("unchecked")
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
				.getActivePage().getSelection();
		if (selection != null & selection instanceof IStructuredSelection) {
			IStructuredSelection strucSelection = (IStructuredSelection) selection;
			Object o = strucSelection.getFirstElement();
			if (o instanceof IFile) {
				IFile f = (IFile)o;
				ResourceSet rs = new ResourceSetImpl();
				Resource r = rs.getResource(URI.createURI(f.getLocationURI().toString()), true);
				try {
					r.load(null);
					Model m = (Model) r.getContents().get(0);
					for (Greeting g : m.getGreetings()) {
						System.out.println(g.getName());
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return null;
	}

}


(B) + (C): you get a java model for the ast out of the box so why to you want to do something extra?

btw maybe the chapters http://www.eclipse.org/Xtext/documentation/1_0_1/xtext.html#processing_Xtext_models and http://www.eclipse.org/Xtext/documentation/1_0_1/xtext.html#getting-started-xpand of the docs and the domain model example might be a look worth for you

~Christian


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

[Updated on: Sat, 04 June 2011 21:28]

Report message to a moderator

Previous Topic:Managing LocalScopeProvider
Next Topic:Code templates
Goto Forum:
  


Current Time: Thu Apr 18 23:51:10 GMT 2024

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

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

Back to the top