0

I want to build a transformation engine based on rule set (From COBOL to Java) For example we have the following COBOL code:

PERFORM 3 TIMES
   IF X = 10 THEN;
   ELSE
       CALL PARA01.
END PERFORM.

I want to be able to define the rules like the following:

Rule1:
  Pattern: PERFORM $VAR TIMES ==> for (int i = 0; i < $VAR; i ++)
Rule2: 
  Pattern: IF CONDITION THEN ;
           ELSE
              BLOCK_OF_CODE
           ==> if (!(condition))
                  BLOCK_OF_CODE

So the Java code after transformed would look like this:

for (int i = 0; i < 3; i ++)
    if (!(A== 10))
        Para01();

Is there any tool, platform available to help? In the case we need to develop from scratch, is there any recommendation? Thank you so much

Hai Pham
  • 173
  • 2
  • 9

1 Answers1

1

Some people try to do this kind of thing with regular expressions. It doesn't work; regular expressions can't handle context-free matching.

The right answer is Program Transformation Systems.

These are tools that parse source, build ASTs, and let one write code transformations in source syntax, very much as OP has shown.

While there is a ton of theory (see scholar.google.com with the term program transformation), building these tools is fairly difficult. Just the notion of parsing code fragments (as OP has hinted at) requires some pretty interesting twists in parsing machinery. I've been in this space since the 1980s, and seen the tools listed at Wikipedia built over the last 15-20 years; I don't see a lot more of them. The experience we have with building our system (see my bio) is some 50 man-years of PhD level software engineering, so I'm not surprised I don't see more of them.

These tools typically require a precise language front end to be useful, and this is where much of the work in actually using them goes after you have the basic transformation engines built. (See this discussion on parsing Java and C++; it applies equally to complex legacy languages like COBOL).

[A query in a comment wanted to know more where to learn more technical detail. If you want "first-cut" but deep technical detail, this survey is pretty good, but focuses mostly on "pure" transformation engines (operating only on abstract syntax trees).

I happen to believe that one needs considerably more than that; see my bio for a discussion of "Life After Parsing". You can get an older technical paper on our DMS system, and/or a more recent video overview of our system, including how it differs, in my Google Tech Talk.

In a comment on the original question, there was a request for "deep technical tour". See this for detailed discussion on how DMS handles rewrite rules; that page links to similarly deep discussions of related topics.

Community
  • 1
  • 1
Ira Baxter
  • 93,541
  • 22
  • 172
  • 341