Skip to content

Instantly share code, notes, and snippets.

@blippy
Created December 23, 2016 23:18
Show Gist options
  • Select an option

  • Save blippy/996084e5cd71e6f1b4ea8cac9122622e to your computer and use it in GitHub Desktop.

Select an option

Save blippy/996084e5cd71e6f1b4ea8cac9122622e to your computer and use it in GitHub Desktop.
Parsing COBOL copybook in perl6
#use Grammar::Debugger;
grammar CopybookGrammar {
token TOP { {say "at TOP" } <data-description-entry-top> <data-description-entry>* <ws0> }
#token data-description-entry-top { <ws0> '01' <ws1> <data-name> <ws0> '.' }
#token data-description-entry { <ws1> <level-num> <ws1> <data-name> <ws1> 'pic' <ws1> <picture-string> <ws0> '.' }
token data-description-entry-top { \s* '01' \s+ <data-name> \s* '.' }
token data-description-entry { <ws1> <level-num> \s+ <data-name> \s+ 'pic' \s+ <picture-string> <dot> }
#rule phrase { <TOP> <word> '.' }
token dot { { say "found dot" } <ws0> '.' }
token data-name { {say "at word" } <[a..z\-]>+ }
token level-num { {say "at NUM" } <[0..9]>+ }
token integer { <[0..9]>+ }
token picture-punctuation { '/' | ',' | '.' | ':' }
token picture-string { <picture-chars> <picture-len-paren>? ( '.' <picture-chars>)? }
token picture-len-paren { '(' <integer> ')' }
token picture-chars { <[xXzZ0..9]>+ }
token ws0 { \s* }
token ws1 { \s+ }
}
class CopybookActions {
method TOP ($/) { $/.make(""); }
method phrase ($/) { $/.make("Found phrase"); }
}
grammar LineGrammar {
token TOP { .* }
}
sub scanit($contents) {
my $match1 = CopybookGrammar.parse($contents);
say $match1;
#say "num is $match1<aword>";
#my $contents = slurp 'example.cpy';
#my $actions = CopybookActions.new;
#my $match = CopybookGrammar.parse($contents, :$actions);
#say $match;
#say $match.made;
}
#scanit "01 out-line .";
#scanit "out-line1.";
my $contents = slurp 'example1.cpy';
#scanit1
scanit($contents);
01 out-line.
05 dstamp pic x(10).
05 filler pic x.
05 ticker pic x(6).
05 filler pic x.
05 uprice pic Z(5).99.
05 filler pic x.
05 way pic X.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment