fanatasma has asked for the wisdom of the Perl Monks concerning the following question:

I have a regexp::grammar set of rules to read a parameter files, quite complex ones. I got the matching alright, but I fail in simplifying the result structure.
Instead of the rule's name, I'd much prefer to see the name field as hash key, then simplifying the whole thing to something like this (I want to keep the arrays since the values can actually be lists):
The data looks like this, elements grouped in { }, that can be arbitrarily nested. The values to the tokens can be scalars or lists
... <ParamMap."HARDWARE"> { <ParamBool."MagnitudeImages"> { "true" "false" "true" } <ParamString."ProtectedSize"> { "350" } } ...
The (simplified) code looks like this:
my $g=qr{ <nocontext:> <protocol> <rule: protocol> <[MATCH=element]>+ <minimize:> <rule: element> ( <MATCH=map> | <MATCH=p_str> | <MATCH=p_bool> ) #<min +imize:> <rule: map> \< ParamMap \. <name> > \{ <[element]>* \} <rule: p_str> \< ParamString \. <name> > \{ ( <[string]>* )? \} <rule: p_bool> \< ParamBool \. <name> > \{ #<debug: on> ( <[bool]>* )? \} <rule: string> <_qq> <[MATCH=literal]>* <_qq> <token: literal> [.'!\w_@\+\-\(\)\(\)\#{}\\\[\]]+ <token: _qq> \" <token: name> <_qq>\w*<_qq> <rule: bool> <_qq><MATCH=(true|false)><_qq> }xms;

My desired results would be something like that:
$VAR1={ 'HARDWARE'=>[ 'PhaseImages'=>[ true, false, true ] 'RoFOV'=>[ '230.0000000000000000', ] ] } ...

so far I get this:
$VAR1 = { 'protocol' => [ { 'name' => '"HARDWARE"', 'element' => [ { 'bool' => [ 'true', 'false', 'true' ], name' => '"MagnitudeImages"' }, { 'name' => '"ProtectedSize"', 'string' => [ [ '350' ] ] } ] } ] };
My sincere apologies for the bad formating.

Replies are listed 'Best First'.
Re: results simplification of regexp::grammars
by talexb (Chancellor) on Feb 28, 2012 at 17:57 UTC

    Please read How do I post a question effectively? when you update your post, and post in the future. As it sits now, your post is illegible.

    Here's what it should have looked like:

    { 'HARDWARE' => [ 'PhaseImages' => true, 'RoFOV' => '230.0000000000000000', ] }
    ... so far I get this : ...
    { 'name' => '"HARDWARE"' 'element' => [ { 'bool' => '"true"', 'name' => '"PhaseImages"' }, { 'float' => '230.0000000000000000', 'name' => '"RoFOV"', 'precision' => '16' } ] }
    Do us all a favour and PREVIEW your posts before you click on the create button. If it's a mess when you preview it, we're a lot less likely to help out. (I previewed this four times before I thought it was OK -- and I've been here for a while.)

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: results simplification of regexp::grammars
by Marshall (Canon) on Feb 28, 2012 at 19:02 UTC
    Parsing textual parameter files is something that Perl is particularly good at doing.

    - Present as short a possible example of the input and the output.
    - Present the code that you have now - you get "big bonus points" for showing the Monks your attempt - no matter how imperfect that it may be.

    For code, inclose it in <code>...your stuff here..verbatim..</code> tags.

    It can be and sometimes is that your approach to the problem can be simplified. I don't know that without seeing "the problem". In other words, a demo of the desired result along with the code that you have now is more likely to result in a very good solution.