Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^3: How to know where I am going wrong in writing the grammar

by PoorLuzer (Beadle)
on Apr 02, 2009 at 13:03 UTC ( [id://754947]=note: print w/replies, xml ) Need Help??


in reply to Re^2: How to know where I am going wrong in writing the grammar
in thread How to know where I am going wrong in writing the grammar

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
.
';

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://754947]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-13 21:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found