Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Assistance requested with very basic project(Noob type question)
Assistance requested with very basic project [message #1128528] Mon, 07 October 2013 19:56 Go to next message
Gary Worsham is currently offline Gary WorshamFriend
Messages: 176
Registered: September 2013
Senior Member
I have an existing assembly language which has about 26 instructions. I need to translate source code using these into an equivalent representation in Java.

For example,

RDFX 31, 0.01593
WRAX DACL

would become

ReadRegisterFilter(31, 0.01593);
WriteRegister(DACL);
===========================================
I have a need to identify the assembler instructions and 0 to 3 parameters that follow.

I'm sorry to write such a basic question but I've been reading and doing the tutorials and looking at the examples for several weeks, but I can't find one yet that is close to what I'm trying to accomplish.

I've been told several times that Xtext can do this very easily! I just don't know how to get started.

Thanks for any hints.

[Updated on: Mon, 07 October 2013 19:56]

Report message to a moderator

Re: Assistance requested with very basic project [message #1128631 is a reply to message #1128528] Mon, 07 October 2013 22:14 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
This is quite simple.

If you just describe the grammar, you automatically get a Java
Representation in the generated EMF model!

MyAsmGrammar.xtext

Program: instructions += Instruction+;
Instruction
: ReadRegister
| WriteRegister
| etc
| etc
;

ReadRegister: 'RDFX' arg1 = INTEGER ',' arg2 = REAL;

WriteRegister: 'WRAX' register = RegisterName;

RegisterName: 'DACL', '...', '...';

etc
etc

// Whatever terminals you need (depend on how your assembler works
//
terminal INTEGER: ...
terminal REAL: ...
terminal ID: ...

How difficult this gets depends on the features of your assembler
language - do you have labels? Do you have calls/jumps to labels?
Do you have expressions (1 + 1, etc). How does comments work? Should
comments be preserved in the java representation or not?

You then generate everything, and now have an editor etc. When the
editor reds the .myasm files you have a representation in memory in an
EMF model.

If you want to "compile" them to Java, you need to do a bit more work
translating the EMF model (representing your .myasm source) to java
code. You may want this to run inside eclipse (easy; create a builder),
or run headless from command line (more work).

You could also/instead write an interpreter (i.e. it just loads an
..myasm file, gets the EMF model, and visits the elements in the model
and executes the instructions (quite easy).

There is a new book out on Xtext and Xtend that is worth reading, or if
you are in a hurry, call Itemis and buy some consulting to get you started.

Regards
- henrik

On 2013-07-10 21:56, Gary Worsham wrote:
> I have an existing assembly language which has about 26 instructions. I
> need to translate these into an equivalent representation in Java.
>
> For example,
>
> RDFX 31, 0.01593
> WRAX DACL
>
> would become
>
> ReadRegisterFilter(31, 0.01593);
> WriteRegister(DACL);
> ===========================================
> I have a need to identify the assembler instructions and 0 to 3
> parameters that follow.
>
> I'm sorry to write such a basic question but I've been reading and doing
> the tutorials and looking at the examples for several weeks, but I can't
> find one yet that is close to what I'm trying to accomplish.
>
> I've been told several times that Xtext can do this very easily! I just
> don't know how to get started.
>
> Thanks for any hints.
Re: Assistance requested with very basic project [message #1128665 is a reply to message #1128631] Mon, 07 October 2013 23:02 Go to previous messageGo to next message
Gary Worsham is currently offline Gary WorshamFriend
Messages: 176
Registered: September 2013
Senior Member
Thanks Henrik!

I will try this tonight.

GW
Re: Assistance requested with very basic project [message #1128686 is a reply to message #1128665] Mon, 07 October 2013 23:33 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
On 2013-08-10 1:02, Gary Worsham wrote:
> Thanks Henrik!
> I will try this tonight.
>
> GW

Do yourself a favor and start small; just one or two instructions, and
try to use the default terminals (do not add your own) until you got
your first simple thing going.

Then, add one instruction at a time to your grammar. This way you have a
better understanding of what is wrong if there are issues with the grammar.

(this vs. "add everything at once - get lots and lots of errors").

Just a tip.

Regards
- henrik
Re: Assistance requested with very basic project [message #1128690 is a reply to message #1128665] Mon, 07 October 2013 23:35 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
On 2013-08-10 1:02, Gary Worsham wrote:
> Thanks Henrik!
> I will try this tonight.
>
> GW
Oh, and the book I mentioned is by Lorentzo Bettini
http://www.lorenzobettini.it/2013/08/the-book-on-xtext-is-out/

- henrik
Re: Assistance requested with very basic project [message #1129905 is a reply to message #1128690] Wed, 09 October 2013 04:00 Go to previous messageGo to next message
Gary Worsham is currently offline Gary WorshamFriend
Messages: 176
Registered: September 2013
Senior Member
Ok, here's what I've accomplished so far.

#1 I wrote my grammar including 3 instructions.

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

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

Program:
	instructions+=Instruction*;
	
Instruction:
	ReadRegister |
	WriteRegister |
	ReadRegisterFilter
	;
	
ReadRegister: 'RDAX' arg1 = INT ',' arg2 = INT;
WriteRegister: 'WRAX' arg1 = INT ',' arg2 = INT;
ReadRegisterFilter: 'RDFX' arg1 = INT ',' arg2 = INT;


It is just 3 instructions and to keep it simple for now I am just using INT arguments although the second one in each case should be "REAL".

Following the general course of events in the 5-minute example, I generated my XText artifacts then ran the project as an Eclipse Application.

Created a new Java project, added the file "test.mydsl" with the following content:

RDAX 32, 45
WRAX 43, 56
RDAX 3, 56
WRAX 2, 67
RDAX 23, 43
RDFX 4,6


Happy days, I show no errors here and the code completion tips work too! Nice.

I'm ready now for a hint (subtle or otherwise) regarding this:

Quote:
If you want to "compile" them to Java, you need to do a bit more work
translating the EMF model (representing your .myasm source) to java
code. You may want this to run inside eclipse (easy; create a builder),
or run headless from command line (more work).


I need to be able to represent the output as Java code which then needs to undergo some additional transformations. But one thing at a time!

Thanks,

GW
Re: Assistance requested with very basic project [message #1130608 is a reply to message #1129905] Wed, 09 October 2013 19:04 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
On 2013-09-10 6:00, Gary Worsham wrote:
> Ok, here's what I've accomplished so far.
>
> #1 I wrote my grammar including 3 instructions.
>
> grammar org.xtext.example.mydsl.MyDsl with
> org.eclipse.xtext.common.Terminals
>
> generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
>
> Program:
> instructions+=Instruction*;
>
> Instruction:
> ReadRegister |
> WriteRegister |
> ReadRegisterFilter
> ;
>
> ReadRegister: 'RDAX' arg1 = INT ',' arg2 = INT;
> WriteRegister: 'WRAX' arg1 = INT ',' arg2 = INT;
> ReadRegisterFilter: 'RDFX' arg1 = INT ',' arg2 = INT;
>
>
> It is just 3 instructions and to keep it simple for now I am just using
> INT arguments although the second one in each case should be "REAL".
>
> Following the general course of events in the 5-minute example, I
> generated my XText artifacts then ran the project as an Eclipse
> Application.
>
> Created a new Java project, added the file "test.mydsl" with the
> following content:
>
> RDAX 32, 45
> WRAX 43, 56
> RDAX 3, 56
> WRAX 2, 67
> RDAX 23, 43
> RDFX 4,6
>
> Happy days, I show no errors here and the code completion tips work
> too! Nice.
>
> I'm ready now for a hint (subtle or otherwise) regarding this:
>
> Quote:
>> If you want to "compile" them to Java, you need to do a bit more work
>> translating the EMF model (representing your .myasm source) to java
>> code. You may want this to run inside eclipse (easy; create a
>> builder), or run headless from command line (more work).
>
>
> I need to be able to represent the output as Java code which then needs
> to undergo some additional transformations. But one thing at a time!
>

The book I mentioned earlier provides good advice on how to do this
using Xtend. There are problably lots of other sources and examples -
you basically want a Builder (that kicks in when user makes changes),
that runs template based code generation. Xtend is one option, there are
several other code generation frameworks built on EMF. Search for code
generation examples based on Xtend.

Or, if you you prefer, write an Eclipse builder by hand, just writing
the java code manually. Tradeoff here is how much you want to/need to
learn and how many different types of generators you are going to write.
Sometimes a "framework" is not the answer.

If your builder is built without references to any Eclipse UI bundles
then you can also reuse it as a command line builder. (Eclipse
buckminster makes that easier (running workspace builders from the
command line) - to some extent covered by the same book).

Hope that helps
- henrik
Re: Assistance requested with very basic project [message #1131521 is a reply to message #1130608] Thu, 10 October 2013 16:04 Go to previous messageGo to next message
Gary Worsham is currently offline Gary WorshamFriend
Messages: 176
Registered: September 2013
Senior Member
Hi Henrik,

Thanks for the tips. My project is just for entertainment/hobby purposes, so this is a lot of stuff to get through, but I'm actually a little farther along than I was last week. I taught myself Java at the beginning of the year to get as far as I have already. I'm not against putting in the time to learn, so it is quite helpful to get some idea of which direction to go, since there are so many different tools that do different things.

Thanks again,

GW
Re: Assistance requested with very basic project [message #1131775 is a reply to message #1131521] Thu, 10 October 2013 20:00 Go to previous message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
On 2013-10-10 18:04, Gary Worsham wrote:
> Hi Henrik,
>
> Thanks for the tips. My project is just for entertainment/hobby
> purposes, so this is a lot of stuff to get through, but I'm actually a
> little farther along than I was last week. I taught myself Java at the
> beginning of the year to get as far as I have already. I'm not against
> putting in the time to learn, so it is quite helpful to get some idea of
> which direction to go, since there are so many different tools that do
> different things.
>
> Thanks again,
>
> GW
In that case, it is very educational to write a small code generator
directly in Java, i.e. as an exercise. Then look at how to do it the
template driven way using Xtend.

In both cases you need to have a decent understanding of EMF, again,
there is a book "EMF" that is of help.

Regards
- henrik
Previous Topic:Invalid character constant after succesful workflow-run
Next Topic:CanTerminal rules be available as EObjects in the generated model?
Goto Forum:
  


Current Time: Thu Apr 25 21:41:52 GMT 2024

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

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

Back to the top