/--------------\ /------------\ /-----------\ /------------\ /--------\
| | | | | | | | | |
| command line |-->| Parse:: |-->| Data |-->| Data:: |-->| XML |
| string |<--| RecDescent |<--| structure |<--| Serializer |<--| FILE |
| | | | | | | | | |
/--------------\ /------------\ /-----------\ /------------\ /--------\
^
|
/------------\
| |
| command |
| line |
| grammar |
| |
/------------\
####
#/usr/bin/perl -Wl
use strict;
use warnings;
use Parse::RecDescent;
use Data::Dumper;
use Data::Serializer;
my $grammar;
my $grammarfile = $ARGV[0];
open my $gf,'<',$grammarfile or die 'bad grammarfile';
while (<$gf>) {
$grammar.=$_;
}
close $gf;
print '$grammar is ',"\n",$grammar;
$::RD_HINT++;
#$::RD_TRACE++;
my $parser = new Parse::RecDescent ($grammar) or die 'bad grammar';
my $stringtoparsefile = $ARGV[1];
open my $sf,'<',$stringtoparsefile or die 'bad stringstoparse file';
my $result;
my $reresult;
#Data::Serializer 0.44 supports raw=>
my $serializer = Data::Serializer->new(raw=>'1',serializer => 'XML::Dumper');
my $outfilebasename = 'data';
my $outfileext = 'out';
my $outfileindex = 10;
while (<$sf>) {
print 'parsing $_: ',$_;
if (defined $parser->startrule($_)){
$result = $parser->startrule($_);
print Dumper($result);
$serializer->store(
$result,
$outfilebasename.$outfileindex.'.'.$outfileext,
'>>'
);
$reresult = $serializer->retrieve(
$outfilebasename.$outfileindex++.'.'.$outfileext,
);
print Dumper($reresult);
}else{ print " badstring\n"}
}
##
##
startrule: commandline{$item{commandline}}
commandline: command options {[$item {command},$item{options}]}
command: packagecommand {$item{packagecommand}}
packagecommand: /(hap)/ | /(hccmrg)/ | /(hup)/ | /(hspp)/ | /(hpp)/ | /(hpg)/ | /(hdp)/ | /(hdlp)/ | /(hcp)/
options: option(s?)
option: optionflag optionvalue { [$item{optionflag}, $item{optionvalue}?$item{optionvalue}:1] }
optionflag: /(-\w+)/ { $1 }
optionvalue: /(\w*)/ { $1 }
##
##
hap -b
hap -b sknxharvest01
hap -b sknxharvest01 -enc testfile.dfo
hap -b sknxharvest01 -usr cgowing -pass chaspass
hap -badflag
hap -prompt
hap -b sknxharvest01 -prompt
hap -prompt -b sknxharvest01
##
##
C:\chas_sandbox\grammars>grammarharness.pl packagegrammar.txt packagecommands.tx
t
$grammar is
startrule: commandline{$item{commandline}}
commandline: command options {[$item {command},$item{options}]}
command: packagecommand {$item{packagecommand}}
packagecommand: /(hap)/ | /(hccmrg)/ | /(hup)/ | /(hspp)/ | /(hpp)/ | /(hpg)/ |
/(hdp)/ | /(hdlp)/ | /(hcp)/
options: option(s?)
option: optionflag optionvalue { [$item{optionflag}, $item{optionvalue}?$item{op
tionvalue}:1] }
optionflag: /(-\w+)/ { $1 }
optionvalue: /(\w*)/ { $1 }
parsing $_: hap -b
$VAR1 = [
'hap',
[
[
'-b',
1
]
]
];
$VAR1 = [
'hap',
[
[
'-b',
'1'
]
]
];
parsing $_: hap -b sknxharvest01
$VAR1 = [
'hap',
[
[
'-b',
'sknxharvest01'
]
]
];
$VAR1 = [
'hap',
[
[
'-b',
'sknxharvest01'
]
]
];
parsing $_: hap -b sknxharvest01 -enc testfile.dfo
$VAR1 = [
'hap',
[
[
'-b',
'sknxharvest01'
],
[
'-enc',
'testfile'
]
]
];
$VAR1 = [
'hap',
[
[
'-b',
'sknxharvest01'
],
[
'-enc',
'testfile'
]
]
];
parsing $_: hap -b sknxharvest01 -usr cgowing -pass chaspass
$VAR1 = [
'hap',
[
[
'-b',
'sknxharvest01'
],
[
'-usr',
'cgowing'
],
[
'-pass',
'chaspass'
]
]
];
$VAR1 = [
'hap',
[
[
'-b',
'sknxharvest01'
],
[
'-usr',
'cgowing'
],
[
'-pass',
'chaspass'
]
]
];
parsing $_: hap -badflag
$VAR1 = [
'hap',
[
[
'-badflag',
1
]
]
];
$VAR1 = [
'hap',
[
[
'-badflag',
'1'
]
]
];
parsing $_: hap -prompt
$VAR1 = [
'hap',
[
[
'-prompt',
1
]
]
];
$VAR1 = [
'hap',
[
[
'-prompt',
'1'
]
]
];
parsing $_: hap -b sknxharvest01 -prompt
$VAR1 = [
'hap',
[
[
'-b',
'sknxharvest01'
],
[
'-prompt',
1
]
]
];
$VAR1 = [
'hap',
[
[
'-b',
'sknxharvest01'
],
[
'-prompt',
'1'
]
]
];
parsing $_: hap -prompt -b sknxharvest01
$VAR1 = [
'hap',
[
[
'-prompt',
1
],
[
'-b',
'sknxharvest01'
]
]
];
$VAR1 = [
'hap',
[
[
'-prompt',
'1'
],
[
'-b',
'sknxharvest01'
]
]
];
C:\chas_sandbox\grammars>type data10.xml
- hap
-
-
- -b
- 1
C:\chas_sandbox\grammars>