If you want to parse just a copybook, have a look at the java project cb2xml, it will parse cobol copybooks and calculate field position / length for fields. The package actually converts the Cobol copybook into Xml which can then be parsed in many languages.
if you use the cb2xml.jar and cb2xml_jaxb.jar in the cb2xml project,
you can parse the Cobol copybook in java with:
Copybook copybook = CobolParser.newParser()
.parseCobol(copybookName);
to print the contents in java :
List<Item> items = copybook.getItem();
for (Item item : items) {
printItem(" ", item);
}
}
public static void printItem(String indent, Item item) {
System.out.println(indent + item.getLevel() + " " + item.getName() +"\t" + item.getPosition()
+ "\t " + item.getStorageLength() + "\t" + item.getPicture());
List<Item> items = item.getItem();
for (Item child : items) {
printItem(indent + " ", child);
}
}
If you use cb2xml to convert Cobol to Xml
000001 01 ROUTINE-NAME.
000003 05 ROUTINE-NAME-INPUT.
000007 10 ROUTINE-NAME-FIELD-NAME PIC X(005).
gets converted to
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<copybook filename="xx.cbl">
<item display-length="5" level="01" name="ROUTINE-NAME" position="1" storage-length="5">
<item display-length="5" level="05" name="ROUTINE-NAME-INPUT" position="1" storage-length="5">
<item display-length="5" level="10" name="ROUTINE-NAME-FIELD-NAME" picture="X(005)" position="1" storage-length="5"/>
</item>
</item>
</copybook>
disclosure: I was on of the contributers to cb2xml
There are other projects around (e.g. legstar) for parsing Cobol. Also Koopa Cobol Parser