Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
A solution using the Marpa::XS parser:
#!/usr/bin/perl use warnings; use strict; use Marpa::XS; use Data::Dumper; main(); { package MyParser; sub pass_tree { shift } sub create_node { my (undef, undef, $name, undef, $value) = @_; return [$name => $value]; } sub create_seq { my (undef, @nodes) = @_; return \@nodes; } sub add_nodes { my (undef, undef, $name, undef, $nodes) = @_; return [ $name => $nodes ]; } sub Tree { return $_[1]; } } sub main { my $grammar = init_grammar(); my $rec = init_recognizer($grammar); my $input = do { local $/ = undef ; <DATA> }; lex($input, $rec); while (my $val = $rec->value) { print Dumper $val; } } sub init_grammar { my $grammar = Marpa::XS::Grammar->new ({ start => 'Tree', actions => 'MyParser', default_action => 'pass_tree', rules => [ [ 'Tree', ['Node'] ], [ 'Tree', ['Leave'] ], [ 'Node', [qw/( Name = Value )/], ' +create_node'], [ 'Node', [qw/( Name = NodeSeq )/], ' +add_nodes'], { lhs => 'NodeSeq', rhs => [qw/Node/], min => 1, a +ction => 'create_seq' }, ], }); $grammar->precompute; } sub init_recognizer { my $grammar = shift; my $rec = Marpa::XS::Recognizer->new({ grammar => $grammar, # trace_terminals => 1, # trace_actions => 1, # trace_values => 1 }); } sub lex { my ($input, $rec) = @_; while (length $input) { if ($input =~ s/^([()])//) { my $success = $rec->read($1); last if ! $success or $success == 0; } elsif ($input =~ s/^([^()=]+)=([^()=]+)//) { $rec->read('Name', $1); $rec->read('='); $rec->read('Value', $2); } elsif ($input =~ s/^([^()=]+)=//) { $rec->read('Name', $1); $rec->read('='); } else { $input =~ s/^\s+// or die "Invalid input at $input\n"; } } die "Cannot parse: $input.\n" if $input =~ /\S/; } __DATA__ (S=(SN=ac2.bd) (I1=(IN=s%1)(NM=1) (HL=(HLD=kkk kjkjk)(ST=abdc)(HI=REM SSS)(H_M=9)(HL=72)(EB=0) +(ER=0)(HI=E043-93A-DF0-0AB63E)(PE=aaa)(HN=DEE)(SS=NS)(SED=(APR=(PAD=k +kk)(PN=9905)(HH=llkjk))(DD=(LLL=kkk)))) (ppp=1)(RAW=kkk)(DN=kkk)(RIN=ppp)) (PPP=1) (AA=LLI))
Update: Hashes cannot be used easily, because the "key" can be repeated (as in HI). Switched to arrays.
Update 2: Simplified the code.
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

In reply to Re: Help with regular expression by choroba
in thread Help with regular expression by ng

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 chanting in the Monastery: (3)
As of 2024-04-25 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found