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

I'm working on my first use of RecDescent (way-cool Damian). Consider this small piece of a grammar:
foo: bar
        {
            ::processBar($item1)
        }
Under certain circumstances (bad input) processBar will fail. Now, I understand that if I return undef, the production should fail, etc. What I want to do is be able to provide the error message that processBar() has "calculated". I tried adding <error: ${::getError()}> after the action, but that doesn't seem to have worked... What's the best way to do this. Should processBar just "print STDERR 'msg'"?

Replies are listed 'Best First'.
Re: RecDescent error handling
by maverick (Curate) on Sep 05, 2000 at 23:28 UTC
    well, you could store your error string in a scalar and then do something like.
    foo: bar { return ::processBar($item[1]); # return undef on fail } |<error: $error_string>
    There's probably a more elegant way of doing this, but this was the first thing that came to mind. :)

    /\/\averick

Re: RecDescent error handling
by merlyn (Sage) on Sep 05, 2000 at 23:23 UTC
    If I understand you, just have your subroutine change a global variable to be the error message, then look at that on an undef return. You can make it a list if you want to report more than one error. You also might want to look into the <error:...> directive for the grammar.

    -- Randal L. Schwartz, Perl hacker

      I did look into <error:>. I even tried to mention it above, though it apparently got eaten up as HTML so no one could see it. I'm apparently having a problem placing that directive where it gets activated only if the action fails.