Hi! I'm seeking some wisdom on using the Parse::RecDescent module, I'm currently making an interpreter and I want it to be able to read parentheses, here's what I have so far:

#!/usr/bin/perl -w use strict; use Parse::RecDescent; use Data::Dumper; use vars qw(%VARIABLE); # Enable warnings within the Parse::RecDescent module. $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an e +rror $::RD_WARN = 1; # Enable warnings. This will warn on unused rules & +c. $::RD_HINT = 1; # Give out hints to help fix problems. # $::RD_TRACE = 1; # Trace the whole thing my $grammar = <<'_EOGRAMMAR_'; # Terminals (macros that can't expand further) # startrule: instruction(s /;/) eofile instruction : print_instruction | assign_instruction print_instruction : /print/i expression { print $item{expression}."\n" } assign_instruction : VARIABLE "=" expression { $main::VARIABLE{$item{VARIABLE}} = $item{expre +ssion} } expression : '(' expression ')' { return $item[2] } | INTEGER OP expression { return main::expression(@item) } | STRING '+' expression { return main::concat(@item) } | VARIABLE OP expression { return main::expression(@item) } | INTEGER | VARIABLE { return $main::VARIABLE{$item{VARIABLE}} } | STRING OP : m([-+*/%]) # Mathematical operators INTEGER : /[+-]?[0-9]*\.?[0-9]+/ # Signed integers VARIABLE : /\w[a-z0-9_]*/i # Variable STRING : /'.*?'/ eofile : /^\Z/ _EOGRAMMAR_ sub expression { shift; my ($lhs,$op,$rhs) = @_; $lhs = $VARIABLE{$lhs} if $lhs=~/[^-+(\.)0-9]/; return eval "$lhs $op $rhs"; } sub concat { shift; my ($lhs,$op,$rhs) = @_; $lhs =~ s/^'(.*)'$/$1/; $rhs =~ s/^'(.*)'$/$1/; return "$lhs$rhs" } my $parser = Parse::RecDescent->new($grammar); #print "a=2\n"; $parser->startrule("a= 'hola ' + 3.2 ") +; #print "b=1+2.2\n"; $parser->startrule("b=1+2.2"); #print "print a\n"; $parser->startrule("print a"); #print "print b\n"; $parser->startrule("print b"); print "print 2+2/4\n"; $parser->startrule("print 2+2/4"); #print "print 2+-2/4\n"; $parser->startrule("print 2+-2/4"); #print "a = 5 ; print a\n"; $parser->startrule("a = 5 ; print a");

As you can see, I used an example on the recdescent module (which I found here http://bit.ly/Sjgdhf) and basically added strings and some other functions, as I have it now I really don't know if I have to change drastically the grammar in order to be able to read parentheses and nested parentheses.. I'd really appreciate some help on this, I've been busting my head with the trace option but have not been successful so far, any ideas??


In reply to Help on Parse::RecDescent! by lgn8412

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.