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 |
Gary Worsham 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 |
Henrik Lindberg 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 #1129905 is a reply to message #1128690] |
Wed, 09 October 2013 04:00 |
Gary Worsham 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 |
Henrik Lindberg 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 #1131775 is a reply to message #1131521] |
Thu, 10 October 2013 20:00 |
Henrik Lindberg 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
|
|
|
Goto Forum:
Current Time: Wed Sep 25 03:44:40 GMT 2024
Powered by FUDForum. Page generated in 0.07548 seconds
|