Search code examples
parsingcobol

Cobol Copybook Parser


Can anybody suggest me how to extract the fields of a Cobol Copybook? It will be helpful if you could help with the code snippet or any links? Example:

I want to extract it like this.

Field No.
Field Name
Field Type      (999 or S9(4) or x(5)....)
Field Type-add  (COMP, COMP-3, etc.,)
Other-Details   (Copy Everything until "." excluding PIC clause)

Solution

  • Disclaimer: I maintain cb2xml

    You could use cb2xml to parse your copybooks

    • In java each field is converted to a Cobol Object (with picture, usage, occurs fields)
    • for Other languages the cobol can be converted to xml

    See Looking for The right way with Regular Expression with groups in different order

    Cobol:

       01 Ams-Vendor.
           03 Brand               Pic x(3).
           03 Location-details.
              05 Location-Number  Pic 9(4).
              05 Location-Type    Pic XX.
              05 Location-Name    Pic X(35).
           03 Address-Details.
              05 actual-address.
                 10 Address-1     Pic X(40).
                 10 Address-2     Pic X(40).
                 10 Address-3     Pic X(35).
              05 Postcode         Pic 9(4).
              05 Empty            pic x(6).
              05 State            Pic XXX.
           03 Location-Active     Pic X.
    

    Output from cb2xml:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <copybook filename="cbl2xml_Test110.cbl">
        <item display-length="173" level="01" name="Ams-Vendor" position="1" storage-length="173">
            <item display-length="3" level="03" name="Brand" picture="x(3)" position="1" storage-length="3"/>
            <item display-length="41" level="03" name="Location-details" position="4" storage-length="41">
                <item display-length="4" level="05" name="Location-Number" numeric="true" picture="9(4)" position="4" storage-length="4"/>
                <item display-length="2" level="05" name="Location-Type" picture="XX" position="8" storage-length="2"/>
                <item display-length="35" level="05" name="Location-Name" picture="X(35)" position="10" storage-length="35"/>
            </item>
            <item display-length="128" level="03" name="Address-Details" position="45" storage-length="128">
                <item display-length="115" level="05" name="actual-address" position="45" storage-length="115">
                    <item display-length="40" level="10" name="Address-1" picture="X(40)" position="45" storage-length="40"/>
                    <item display-length="40" level="10" name="Address-2" picture="X(40)" position="85" storage-length="40"/>
                    <item display-length="35" level="10" name="Address-3" picture="X(35)" position="125" storage-length="35"/>
                </item>
                <item display-length="4" level="05" name="Postcode" numeric="true" picture="9(4)" position="160" storage-length="4"/>
                <item display-length="6" level="05" name="Empty" picture="x(6)" position="164" storage-length="6"/>
                <item display-length="3" level="05" name="State" picture="XXX" position="170" storage-length="3"/>
            </item>
            <item display-length="1" level="03" name="Location-Active" picture="X" position="173" storage-length="1"/>
        </item>
    </copybook>                
    

    An interesting application of cb2xml is described in Dynamically Reading COBOL Redefines with C#