views:

53

answers:

2

I am going to write a code generator to generate a COBOL program using some input file of records. I am going to implement it as java program. I think XML/XSL approach would not be appropriate in this case, because the input file is not in XML format.

I think a template processor would be helpful, because some parts of it can be generated using the existing source template. Which tool should be useful for this. What about Apache Velocity ? would that helpful in this regard?

A: 

I've used Velocity for source code generation in the past. It worked fairly well, but I ended up writing a fair bit of the generator in Java anyway.

Velocity is good when there is a straight-forward from your data structures to the target code. The trick is to get the in-memory data structures into the right form before you start generating. This may involve making preliminary passes over the data to reorganize things.

(And I'm not going to criticize COBOL as a target language. There's clearly a pragmatic reason for using it. Nuff said.)

Other alternatives to Velocity include Freemarker and JET (used in the Eclipse / EMF world).

Stephen C
A: 

We used a headless Eclipse to do some of the transformation between the source code and the generated Cobol code. We defined the transformation rules in several XML files, and Eclipse processed them and the source code

It depends somewhat on your source input, but in our transformation process, the Data Division was a lot more difficult than the Procedure Division. We pretty much had to code each Data Division transformation as a separate Java method. We were able to use a factory model for the Procedure Division. The factory had 8 concrete class implementations, with one used in the majority of the transformations.

Edited to add examples.

Here's something we insert into Working Storage:

01 PROGRAM-COMPILE-INFO.                                                 
    05  PGMNAME-COMPILED    PIC X(08)   VALUE 'J1PP2D0'.             
    05  PGMDATE-COMPILED    PIC X(10)   VALUE '2009-08-11'.          
    05  PGMTIME-COMPILED    PIC X(08)   VALUE '08:46:47'.             

Here's a simple Data Division transformation:

$$COPY J1PP2D1

converts to

 COPY J1PP2D1.

Here's a Procedure Division transformation:

SQL-OTHER-ERROR IASN CLOSE

converts to

IF SQL-DEADLOCK                                              
    MOVE '0329' TO ERROR-STATUS OF SUBSCHEMA-CTRL            
ELSE                                                         
    MOVE '0399' TO ERROR-STATUS OF SUBSCHEMA-CTRL            
END-IF                                                       
MOVE 'IASN'     TO ERROR-RECORD OF SUBSCHEMA-CTRL            
MOVE '000600,CLOSE  ,0056-PROCESS'                           
                TO XI-EHK-STMT-CONTEXT                       
PERFORM XI-SQL-ERROR                                         
GO TO IDMS-STATUS  

The 000600 is an error code number automatically calculated by the precompiler. The 0056-PROCESS is the paragraph name of the paragraph containing the SQL-OTHER-ERROR precompiler statement.

The Java code to do the Data Division examples is pretty simple.

The Java code to do the Procedure Division example is a factory method written for the SQL-OTHER-ERROR verb.

Here's one of our XML transformation scripts. We have several.

<?xml version="1.0" encoding="UTF-8"?>
<script>

  <transformation name="DB2Pre">
     <param name="cobol.in" kind="in" type="text-files"/>
     <param name="cobol.out" kind="inout" type="text-files"/>
     <param name="mapsusage-xml-files" kind="inout" type="xml-files"/>

     <call-transformation name="DB2PreInit"/>

     <call-transformation name="DB2PreImpl">
       <with-param name="cobol-src-files" value="$cobol.in"/>
       <with-param name="cobol-out-files" value="$cobol.out"/>
       <with-param name="mapsusage-xml-files" value="$mapsusage-xml-files"/>
       <with-param name="NEED_MAP_MACRO_FLAG" value="$YES"/>
     </call-transformation>

     <call-transformation name="Map2Cobol"/>

     <call-transformation name="GenStrutsConfig">
       <with-param name="cobol-src-files" value="$cobol.in"/>
       <with-param name="mapsusage-xml-files" value="$mapsusage-xml-files"/>
     </call-transformation>

  </transformation>

</script>
Gilbert Le Blanc
And the input was what (you suggest source code; does that match the OP's question)? You suggest the transformations were coded in XML; what exactly was being transformed?
Ira Baxter
Precompiler statements are transformed into Cobol code. I've added examples to my answer.
Gilbert Le Blanc