Here's a basic parser for your example graph
use strict; use Data::Dumper; use Regexp::Common; my $name = qr/[a-z0-9_]+/i; my $val = qr/\S+/; my $braces = qr/$RE{balanced}{-parens=>'{}'}/; my $node = qr/($name) \s+ ($braces)/x; my $assign = qr/($name) [=:] \s* ($val) (?:\s+ | })/x; my $graph = <<TXT; graph { node { title: "node1" loc {x: 10 y: 20} } node { title: "node2" loc { x: 10 y: 20 } } edge { sourcename="node1" targetname="node2" } } TXT print Dumper( parse_graph($graph) ); sub parse_graph { my $graph = shift; my $tree = {}; pos($graph) = 0; PARSE: { if($graph =~ /\G {? \s* $assign/sgcx) { $tree->{$1} = $2; redo PARSE; } elsif($graph =~ /\G .*? $node/sgcx) { push @{ $tree->{$1} }, parse_graph($2); redo PARSE; } } return $tree; } __output__ $VAR1 = { 'graph' => [ { 'edge' => [ { 'sourcename' => '"node1"', 'targetname' => '"node2"' } ], 'node' => [ { 'title' => '"node1"', 'loc' => [ { 'x' => '10', 'y' => '20' } ] }, { 'title' => '"node2"', 'loc' => [ { 'x' => '10', 'y' => '20' } ] } ] } ] };
Will probably need tweaking for your own preferences but hopefully it's a start :)
HTH

_________
broquaint


In reply to Re: Graph File Parsing by broquaint
in thread Graph File Parsing by arunhorne

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.