Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Here is some reduced grammer and code that works - parses the metafields (prefix #) correctly, but bums out at the "normal" fields (prefix F)..

Any ideas?

#!/usr/bin/perl -w use strict; use warnings; #use diagnostics; use Parse::RecDescent; use Data::Dumper; # Enable warnings within the Parse::RecDescent module. $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an er +ror $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c +. $::RD_HINT = 1; # Give out hints to help fix problems. #$::AUTOSTUB = 1; my $grammar = <<'_EOGRAMMAR_'; #{ our $errortext = ''; our $errorprefix = '';} RECORDSTART : /^RECORD\r*\n/ { print "\n[*] RECORDSTART -> " . $item[1]; $item[1]; } RECORDEND : /^\./ { print '\n[*] RECORDEND -> ' . $item[1]; $item[1]; } fieldName : /[^ \t\n]+/ { print "\n[*] fieldName -> $item[1]\n"; $item[1]; } metaName : /[^ \t\n]+\n?/ { $item[1]; } metaFieldValue: /([^\n]*)\n/ { $1; } fieldValue: /([^\n]*)\n/ { print "[*] fieldValue $item[1]\n"; $1; } field : /^F/ fieldName fieldValue { print "[*] Got field named \'" . $item{ fieldName } . '\' with + value \'' . $item{ fieldValue } . "\'\n"; print Data::Dumper->Dump([$text], ["fieldStuff"]); } metaField : /^\#/ metaName metaFieldValue { print "[*] Got metafield named \'" . $item{ metaName } . '\' w +ith value \'' . $item{ metaFieldValue } . "\'\n"; } recordBody : field(s) { print "\n[*] field(s)\n"; #print main::Dumper \@item; print Data::Dumper->Dump([$text], ["field(s)"]); } | metaField(s) { print "\n[*] metaField(s)\n"; #print main::Dumper \@item; print Data::Dumper->Dump([$text], ["metaField(s)"]); } | <error> #<error: I am confused in recordBody at $thisoffset!> #startOfRecord: RECORDSTART recordBody(s /$/) RECORDEND startOfRecord: RECORDSTART recordBody RECORDEND { $return = $item[1] +} | #<error> <error: I could not even parse a line line starting at $thiso +ffset!> _EOGRAMMAR_ #$skeletonPattern = "#input_type[ \t]*"; #my $metaFieldPattern = qr/[ \t]*#([^ \t]+)[ \t]+(.*)/o; # "#input_typ +e SCDR+", "#filename processed_01_20080616001403.cdr", etc #my $normalFieldPattern = qr/([ \t]*)([0-9]*)F[ \t]+([^ \t]+)[ \t]+([^ + \t\r\n]+)(.*)/; # "1F S_Diagnostic1 62" OR " F S_Diagnostic1 62" +OR " F S_Diagnostic1 62" are synonymous, etc my $testData = <<'_EOGTESTA_'; RECORD #input_id 91210758171x001_0013 #input_type PTC #output_type MTC #source_id 01 #filename TTFILE01-0001-20080101000000 #jingalama valuewith#inIt andaSpace F ptc_record_length 00B6 F ptc_charging_start_time 20090604093721 F ptc_charging_end_time 20080604093721 F ptc_called_msrn_ton FF F ptc_term_mcz_duration 060000 . _EOGTESTA_ my $testData1 = <<'_EOGTESTA_'; RECORD #input_id 91210758171x001_0013 #output_id #input_type PTC #output_type PTC #addkey #source_id 01 #filename TTFILE01-0001-20080101000000 F ptc_record_length 00B6 F ptc_record_type F ptc_charging_start_time 20090604093721 F ptc_charging_end_time 20080604093721 F ptc_called_msrn_ton FF F ptc_term_mcz_duration 060000 F ptc_term_mcz_change_direction . _EOGTESTA_ print $testData, "\n\n"; #<STDIN>; my $parser = Parse::RecDescent->new($grammar); $parser->startOfRecord($testData) or die "Bad input!\n";

Output is:

RECORD
#input_id 91210758171x001_0013
#input_type PTC
#output_type MTC
#source_id 01
#filename TTFILE01-0001-20080101000000
#jingalama valuewith#inIt andaSpace
F ptc_record_length 00B6
F ptc_charging_start_time 20090604093721
F ptc_charging_end_time 20080604093721
F ptc_called_msrn_ton FF
F ptc_term_mcz_duration 060000
.

* RECORDSTART -> RECORD
* Got metafield named 'input_id' with value '91210758171x001_0013'
* Got metafield named 'input_type' with value 'PTC'
* Got metafield named 'output_type' with value 'MTC'
* Got metafield named 'source_id' with value '01'
* Got metafield named 'filename' with value 'TTFILE01-0001-20080101000000'
* Got metafield named 'jingalama' with value 'valuewith#inIt andaSpace'

* metaField(s)
$metaField(s) = 'F ptc_record_length 00B6
F ptc_charging_start_time 20090604093721
F ptc_charging_end_time 20080604093721
F ptc_called_msrn_ton FF
F ptc_term_mcz_duration 060000
.
';


In reply to Re^3: How to know where I am going wrong in writing the grammar by PoorLuzer
in thread How to know where I am going wrong in writing the grammar by PoorLuzer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-20 10:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found