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

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.
  • Comment on Re^2: how to match entire buffer with Parse::RecDescent

Replies are listed 'Best First'.
Re^3: how to match entire buffer with Parse::RecDescent
by vr (Curate) on Mar 05, 2019 at 16:28 UTC

    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;
        expression : INTEGER OP expression { main::expression(@item) } | VARIABLE OP expression { main::expression(@item) } | INTEGER | VARIABLE { $main::VARIABLE{$item{VARIABLE}} ||= 0 }

        Remaining "returns" deleted, then it works. + I made uninitialized variable default to 0, seems to make sense.

        Enter --> a=2 print a 2 Enter --> b=3 print b blah 3 ERROR (line 1): Invalid instruction: Was expecting assign instr +uction, or print instruction Error returned by parsing
      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 -->

        Can you show your complete code (reduced to Short, Self-Contained, Correct Example)? Fixing 2nd gotcha worked for me, but I'm afraid since I don't have the harness I had to quickly build my own and very ugly one, won't look good in an answer.

      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

        OK, I'm at another computer, same system as yours, no older files but copied your code and my replacement from this thread, and downloaded portable Strawberry Perl v5.16.3

        C:\>perl rec.pl Enter --> a=2 print 2 2 Enter --> C:\>perl -MParse::RecDescent -wE "say $Parse::RecDescent::VERSION" 1.967015 C:\>perl -v This is perl 5, version 16, subversion 3 (v5.16.3) built for MSWin32-x +64-multi-thread

        Edit: I think failure to parse should prevent any changes to your "symbol table" i.e. %VARIABLE and suppress output from "print":

        # ... expression : INTEGER OP expression { main::expression(@item) } | VARIABLE OP expression { main::expression(@item) } | INTEGER | VARIABLE <defer: $main::VARIABLE{$item{VARIABLE}} ||= 0 > { $main::VARIABLE{$item{VARIABLE}}} print_instruction : /print/i expression <defer: print $item{expression}."\n" > assign_instruction : VARIABLE "=" expression <defer: $main::VARIABLE{$item{VARIABLE}} = $item +{expression} > # ...

        Enter --> a=1 Enter --> a=2 print a blah blah ERROR (line 1): Invalid instruction: Was expecting assign instr +uction, or print instruction Error returned by parsing Enter --> print a 1

        Though perhaps it's laughable because lacks serious studying about how it really must be built.