I would imagine you would need something like this. Each target language would have its own set of dispatch methods. The major issue I see with your generic syntax is that you appear to be using newlines as the statement terminators. Update thekestrels excellent post made me feel obligated to lift my game!

use Parse::RecDescent; use Data::Dumper; my $data =<<'DATA'; $string = 'Hello World' $integer = 1234 $decimal = 1.234 @ary = [ 'foo', "bar", 123, 1.23 ] %hash = { foo => 'bar', "baz" => 123 } DATA my $grammar =<<'GRAMMAR'; SV : '$' HV : '%' AV : '@' VARNAME : /\w+/ QV : /"/ /[^"]*/ /"/ { [ 'char', $item[2] ] } | /'/ /[^']*/ /'/ { [ 'char', $item[2] ] } INT : /\d+/ { [ 'int', $item[1] ] } FLOAT : /\d*\.\d+/ { [ 'float', $item[1] ] } SCALAR : QV | FLOAT | INT LIST : SCALAR /,/ { $item[1] } | SCALAR { $item[1] } HASHBIT : VARNAME /=>/ SCALAR { [ @item[1,3] ] } | QV /=>/ SCALAR { [ @item[1,3] ] } HASHLIST : HASHBIT /,/ { $item[1] } | HASHBIT { $item[1] } EQUALS : '=' start : statement(s) statement : assign_scalar | assign_array | assign_hash assign_scalar : SV VARNAME EQUALS SCALAR { [ @item[0,2,4] ] } assign_array : AV VARNAME EQUALS '[' LIST(s) ']' { [ @item[0,2,5] ] } assign_hash : HV VARNAME EQUALS '{' HASHLIST(s) '}' { [ @item[0,2,5] ] } GRAMMAR my $parser = Parse::RecDescent->new($grammar); my $lang = Language::C->new(); my $parse_tree = $parser->start($data); print Dumper $parse_tree; for my $item( @$parse_tree ) { my $method = $item->[0]; $lang->$method(@$item); } package Language::C; sub new { bless {}, shift } sub assign_scalar { my ( $self, $function, $varname, $value ) = @_; if ( $value->[0] eq 'int' ) { print "int $varname = $value->[1];\n"; } elsif ( $value->[0] eq 'float' ) { print "double $varname = $value->[1];\n"; } else { print qq!char $varname!.qq![] = "$value->[1]";\n!; } } sub assign_array { print Data::Dumper::Dumper \@_ } sub assign_hash { print Data::Dumper::Dumper \@_ }

cheers

tachyon


In reply to Re: Parse:RecDescent grammar help by tachyon
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.