Here is my code with all the comment blocks and POD help removed
#!/usr/bin/perl -w use strict; use warnings; use Parse::RecDescent; use Data::Dumper; use FindBin; use lib $FindBin::Bin; use vars qw(%VARIABLE); # Enable warnings within the Parse::RecDescent module. $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an er +ror $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c +. $::RD_HINT = 1; # Give out hints to help fix problems. my $grammar = <<'_EOGRAMMAR_'; # Terminals (macros that can't expand further) # OP : m([-+*/%]) # Mathematical operators INTEGER : /[-+]?\d+/ # Signed integers VARIABLE : /\w[a-z0-9_]*/i # Variable expression : INTEGER OP expression { return main::expression(@item) } | VARIABLE OP expression { return main::expression(@item) } | INTEGER | VARIABLE { return $main::VARIABLE{$item{VARIABLE}} } print_instruction : /print/i expression { print $item{expression}."\n" } assign_instruction : VARIABLE "=" expression { $main::VARIABLE{$item{VARIABLE}} = $item{expre +ssion} } instruction : assign_instruction | print_instruction | <error> startrule: instruction(s) eofile { $return = $item[1] } eofile: /^\z/ _EOGRAMMAR_ sub expression { shift; my ($lhs,$op,$rhs) = @_; if ( $lhs =~ m/[^-+0-9]/ ) { $lhs = $VARIABLE{$lhs}; } # IF return eval "$lhs $op $rhs"; } # end of expression my $parser = Parse::RecDescent->new($grammar); unless ( defined $parser ) { die("Could not pare the grammar\n$grammar\n"); } # UNLESS my $buffer; while ( 1 ) { print "\nEnter --> "; $buffer = <STDIN>; chomp $buffer; if ( $buffer eq "" || $buffer =~ m/^\s+$/ ) { last; } # IF my $ref = $parser->startrule($buffer); unless ( defined $ref ) { print "Error returned by parsing\n"; } # UNLESS } # WHILE exit 0;

In reply to Re^4: how to match entire buffer with Parse::RecDescent by expo1967
in thread how to match entire buffer with Parse::RecDescent by expo1967

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.