1

I have to write a tool that converts an high level description of a program in its implementation in a proprietary language. This is an example:

MS0 calls MS1 has to be converted in

f_protocol_ex(MS0, {k_call(f_get_number(MS1,msid))});
f_protocol_ex(MS0, {k_wait_state(k_rings)});
f_protocol_ex(MS1, {k_receive_call});
f_protocol_ex(MS1, {k_accept_call});

I have to define a lot of keywords like that but also other keywords regarding definition/allocation/deallocation of parameters. And I have to check if the information provided by the user are correct in order to generate a meaningful program. Are there some tools/library/frameworks that can help me in a huge work like this?

0

What you are asking for is a sophisticated set of tools to help you implement this translation. Insisting that it be in Java makes it harder to find.

What you want is a Program Transformation System (PTS). These are generally tools that accept descriptions of programming language syntax, and allow you to specify code transformations over instances of that language.

A clumsy PTS will force to write your transformations in a compiler-traditional way: procedurally walk over compiler data structures (usually ASTs) and spit out code.

A good PTS will allow you to write transformations at source level in the form of

 if you see this, replace it by that, when this condition is true

where this and that are patterns in your source and target languages.

In your case, you want to specify the description of ("MSO"?) your "high level description". As a practical matter, you need to specify not only the keywords and operators, but how in fact they can be put together legally, using a grammar, and then you want code fragments to be parsed according top that grammar. Then you want to write transformations to map those constructs to your proprietary target language.

You can consider using TXL or Stratego for this purpose; these offer language definition and source-to-source rewriting capability. I think Stratego has a Java variant; you'll have to look, TXL does not. One downside: they don't distinguish your source language and your target language, which means your description is the union of the two and IMHO that's hard to maintain. Worse, similar concepts (surely both will have expressions, and surely those expressions won't be identical in both langauges) tend to get tangled and that makes building the translation rules messier than necessary. Secondly, they provide pure AST rewriting only, nothing else. If your transformations require complex inference (to support that "when this condition") that will be very hard to implement with these.

My company offers the DMS Software Reengineering Toolkit as an alternative.

DMS lets you write seperate descriptions of the source and target languages, which means you don't get the conceptual confusion of mixing them. It enables you to write these in the most natural way practical by not forcing you to live by limitations of the parsing technology; we successfully parse truly nasty stuff like full C++14 with DMS. Your proprietary language should be straightforward to describe.

DMS also provides a lot of supporting machinery to help build sophisticated inference procedures. (See Life After Parsing for more details). This can be used to diagnose erroneous inputs (OP: I have to check if the information provided by the user are correct in order to generate a meaningful program) or to discover special cases where you can produce better or more effective code.

Summarizing:

  • Can describe your specification language to the tool
  • Can describe your propriertary target language to the tool
  • Can be used to analyze the source or target languages for special cases
  • Can write source-to-source transforms to map specification constructs to target language constructs
  • Used for many, many similar program analysis and transformation tasks.
  • Not in Java

Since this is my company's product, don't take this as recommendation. I am merely documenting that it exists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.