in reply to Parse::RecDescent and perlcc ?
To overcome this, the module provides a way of "pre-building" a parser object and saving it in a separate module. That module can then be used to create clones of the original parser.
A grammar may be precompiled using the Precompile class method. For example, to precompile a grammar stored in the scalar $grammar, and produce a class named PreGrammar in a module file named PreGrammar.pm, you could use:
use Parse::RecDescent; Parse::RecDescent->Precompile($grammar, "PreGrammar");
The first argument is the grammar string, the second is the name of the class to be built. The name of the module file is generated automatically by appending ".pm" to the last element of the class name. Thus
Parse::RecDescent->Precompile($grammar, "My::New::Parser");
would produce a module file named Parser.pm.
It is somewhat tedious to have to write a small Perl program just to generate a precompiled grammar class, so Parse::RecDescent has some special magic that allows you to do the job directly from the command-line.
If your grammar is specified in a file named grammar, you can generate a class named Yet::Another::Grammar like so:
> perl -MParse::RecDescent - grammar Yet::Another::Grammar
This would produce a file named Grammar.pm containing the full definition of a class called Yet::Another::Grammar. Of course, to use that class, you would need to put the Grammar.pm file in a directory named Yet/Another, somewhere in your Perl include path.
Having created the new class, it's very easy to use it to build a parser. You simply use the new module, and then call its new method to create a parser object. For example:
use Yet::Another::Grammar; my $parser = Yet::Another::Grammar->new();
Furthermore, the Parse::RecDescent::FAQ documentation module includes a number of methods by which grammars can be optimised, often with great resulting improvements in grammar performance. Most notably:
perl -le 'print+unpack"N",pack"B32","00000000000000000100000010111010"'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Parse::RecDescent and perlcc ?
by bear_hwn (Acolyte) on May 29, 2003 at 19:06 UTC |