in reply to how to match entire buffer with Parse::RecDescent

Isn't it covered by 1st gotcha?

startrule: instruction(s /;/) eofile eofile: /^\Z/

Replies are listed 'Best First'.
Re^2: how to match entire buffer with Parse::RecDescent
by expo1967 (Sexton) on Mar 05, 2019 at 14:57 UTC
    No, the "eofile" rule does not work. I enter the following line of input to my program "a=3" and that is parsed successfully. I then enter "print a" and I get a parsing error, which I should not see wince this is a valid line.

      Isn't it covered by 2nd gotcha? Don't use "return" statement in actions, then the "print a" is parsed correctly with "eofile". I am new to Parse::RecDescent, too, sorry didn't catch this in 1st answer.

        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;
        Your latest idea seems to be working ! Thanks ! Now I can move onto my bigger project !
        I did use a "$return" as described in the 2nd GOTCHA. Even when I specify a "print" instruction as the 1st instruction to my program I still receive a parsing error
        Enter --> print a ERROR (line 1): Invalid instruction: Was expecting assign instr +uction, or print instruction Error returned by parsing Enter -->
        I put in your suggested change and it does not solve anything for me. When I parse "a=2 print a" I get the value "2" printed bu I also get a parsing error. I am running on a Windows 10 64 bit system with perl 5, version 16, subversion 3 (v5.16.3) built for MSWin32-x64-multi-thread