lolo,

I've been just learning Parse::RecDescent myself so this was a good exercise for me.... This should do the trick
- You can define plain variables, arrays or hashes
- takes semi-colons at the end (just to be pretty)
- the two options on hashelement and array term are for with and without commas (maily for the last element of the array not 'requiring' a comma after it...

Most of the rest should be pretty self explanitory, you might want to add another type inside 'term' so you can make a varible on of the options i.e. $var = $var2 or the like
=) (my first answered question...woot!)


regards Paul
#!/usr/bin/perl use strict; use warnings; use Data::Dumper (); use Parse::RecDescent (); use strict; use warnings; my $grammar = q{ # --- Tokens --- EOF : /^\Z/ IDENTIFIER : /[A-Za-z]\w*/ LITERAL : /\d+/ VAR : '$' ARRAY : '@' HASH : '%' EQUAL : '=' QUOTE : '"' HASHASSIGN : '=>' # --- Rules --- parse : stmt(s?) EOF { $item[1] } stmt : variable ';' { $item[1] } | array ';' { $item[1] } | hash ';' { $item[1] } | <error> arrayterm : term ',' { [ @item[0, 1] ] } | term { [ @item[0, 1] ] } array : ARRAY IDENTIFIER EQUAL arrayterm(s?) { [ $item[2, 4] ] } hashelement : IDENTIFIER HASHASSIGN term ',' { [ @item[0,1] ] } | IDENTIFIER HASHASSIGN term { [ @item[0,1] ] } hash : HASH IDENTIFIER EQUAL '{' hashelement(s?) '}' { [ @item [0, 2, 5] ] } variable : VAR IDENTIFIER EQUAL term { [ @item[0, 2, 4] ] } term : QUOTE IDENTIFIER QUOTE { [ 'identifier', $item[2 +] ] } | LITERAL { [ 'literal', $item[1] ] } }; $::RD_HINT = 1; my $parser = Parse::RecDescent->new($grammar); die("Bad grammar.\n") unless defined($parser); my $text = q { $dog = 5; $dog = "fluffy"; @arr = 5,"canary",7; %stuff = { animal => "dog", age => 5, name => "fluffy" }; }; my $result = $parser->parse(\$text); die("Bad text.\n") unless (defined($result)); print Data::Dumper::Dumper($result);

In reply to Re: Parse:RecDescent grammar help by thekestrel
in thread Parse:RecDescent grammar help by pip9ball

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.