Variable document structure

+1 vote
asked Feb 14, 2021 by emil (130 points)

Hello,

I have a file encoded for AS400(IBM870). File contains x structures, structures can be in file more than one (n), but the order and count is not known. The file always contain at least one structure defined in grammar. 

File content sample Axxxxxxxxxx..........Czzzzz...............Bxxxxxaa.............Aznmopaaas...........Cxxddd...............

File structure example                       Grammar 
--bof--
Record A 
    ID = A
    Value 1 = xxxxx
    Value 2 = xxxxx
    Filler 10
Record C
    ID = C
    Value 1 = zzzzz
    Filler 15
Record B
    ID = B
    Value 1 = xxxxx
    Value 2 = aa
    Filler 13
Record A
    ID = A
    Value 1 = znmop
    Value 2 = aaas
    Filler 11
Record C
    ID = C
    Value 1 = xxddd
    Filler 15
--eof--
  • Records have a fixed length 21
  • ID is always present 
  • Values are different 
  • Record order is unknown 
  • Record repeat is unknown
  • Grammar contains definition for
     record ID (A,B,C,D,E ,F ...)
  • The the file may not contain 
    all the records

 

How to automatically parse a file based on existing grammar definition ?

1 Answer

0 votes
answered Feb 17, 2023 by andreas (3,300 points)
This looks very similar to how the PNG grammar works.

You create a (separate) "parent" structure that contains only one field for ID with "must match" checked.

Then you create (separate) structures that inherit from the parent structure and contain the record specific fields. For the ID field you add one fixed value with the record specific ID.

In the main structure (named like the file) you create a structure with variable element order, then add references to all the record structures.
...