> > Hi, > To facilitate my error reporting, I'd like to know the largest that > $thisoffset ever gets. For example, if I have some grammar with this rule: > > sentence: subject verb object > > if it fails to parse an 'object', I'd like to get the $thisoffset as of > the 'verb' which had been parsed, as if my rule was > > sentence: { RememberMax($thisoffset) } subject > { RememberMax($thisoffset) } verb > { RememberMax($thisoffset) } object > { RememberMax($thisoffset) } > > Is there any way to access or automate this? > > Thanks.
perl << 'END' use Parse::RecDescent; #$::RD_TRACE=50; #$::RD_HINT=1; #$::RD_CHECK=0; $::RD_AUTOACTION = q { $item[1] }; my $grammar_all=q( <autotree> sentence: { Somewhere::ResetMax();Somewhere::RememberMax($thisoffset) } subject { Somewhere::RememberMax($thisoffset) } verb { Somewhere::RememberMax($thisoffset) } object { Somewhere::RememberMax($thisoffset) } {$return=[$item[2],$item[4], $item[6]]} object: ('book'|'car'|'computer') verb: ('is'|'was'|'go'|'sat') subject: ('i'|'you'|'them') ); my $parserRef = new Parse::RecDescent($grammar_all); $res=$parserRef->sentence('i is book'); print Somewhere::GetMax()." was the max.\n"; $res=$parserRef->sentence('i is bok'); print Somewhere::GetMax()." was the max.\n"; $res=$parserRef->sentence('he is foobar'); print Somewhere::GetMax()." was the max.\n"; package Somewhere; my $max; sub RememberMax { my $arg=shift; $max=$arg if ($arg>$max); 1; } sub GetMax { $max } sub ResetMax { $max=-1; } 'END' -- -- Intel, Corp. 5000 W. Chandler Blvd. Chandler, AZ 85226