expo1967 has asked for the wisdom of the Perl Monks concerning the following question:

I am new to Parse::RecDescent and I have a question. I have downloaded some samples and they work well. Here is the grammar from one of the samples I have downloaded.
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 : print_instruction | assign_instruction startrule: instruction(s /;/) _EOGRAMMAR_
Now what I want to do is put the calls to $parser->startrule() inside a loop so that I can use it kind of like a command shell. When I ran my perl script I noticed that if there were some extra characters at the end of the buffer that were preceeded by whitespace the parse was succesfull eg. print a a How can I make sure that the call to $parser->startrule() will fail if there are extra characters at the end of the buffer?

Replies are listed 'Best First'.
Re: how to match entire buffer with Parse::RecDescent
by vr (Curate) on Mar 05, 2019 at 14:33 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.