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

Hi there. Anyone out ther who could help me with an RecDescent issue. I have a grammar like that:
main: entry(s) ... <error: ...> entry: predicate '=' expressionlist expressionlist: expression | expression expressionlist bla bla blah much complicated stuff here
So now I have a perfect working (but somewhat slow) parser called $facts_parser. Ok, but now I would like to create (additionally in the same program) another parser called $rules_parser that reuses e.g. expressionlist

The only solution I have so far is cut&paste. Ugly. Is there a better one?

Ciao

Replies are listed 'Best First'.
Re: RecDescent reuse or modular grammar
by knobunc (Pilgrim) on Jun 18, 2001 at 18:56 UTC

    Can't you just build your grammar up from pieces? e.g.:

    my $expression_grammar = qq(# Your common grammar here ); my $facts_grammar = qq(main: .... $expression_grammar ); my $rules_grammar = qq(main: ... $expression_grammar ); my $facts_parser = Parse::RecDescent->new($facts_grammar); my $rules_parser = Parse::RecDescent->new($rules_grammar);

    -ben

Re: RecDescent reuse or modular grammar
by Masem (Monsignor) on Jun 18, 2001 at 19:16 UTC
    Parse::RecDescent builds up the grammer based on strings that you passed to it. So split out what you want common to both parses ('expression', 'expressionlist') into one string, and put the parser-specific parts into a second and third strings. Then use the magic of concatenation and generate your parses from the common and specific parts.


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
      Of course! That will work. Forgive my blindness master.

      IŽll immediately try out your advice.

      Thanx & Ciao

      Update: It really works well. One has just to think of a clear structure of the scalars holding the gramar and the perlcode. That is names and level of separation.

Re: RecDescent reuse or modular grammar
by ph0enix (Friar) on May 17, 2002 at 09:25 UTC

    Yes. Why do you want to create new parser? You don't need to make other parser - use this one already created.

    $rules_parser->entry($text); # parse entry $rules_parser->expressionlist($text); # parse expressionlist