RE: Speed issue w/ LARGE parsed file

[email protected] Mon, 17 Jul 2006 14:50:27 -0500
Newsgroups perl.recdescent
Message-ID <55B858CA6C995345AF972EF652665FB7C00A33@ARLEXCHVS01.lst.link.l-3com.com>
FYI, here's the perl file:

use strict;
use Parse::RecDescent;

$::RD_ERRORS     =3D 1; # unless undefined, report fatal errors
$::RD_WARN       =3D 1; # unless undefined, also report non-fatal =
problems
$::RD_HINT       =3D 1; # if defined, also suggestion remedies
#$::RD_TRACE      =3D 1; # if defined, also trace parsers' behaviour
#$::RD_AUTOSTUB   =3D 1; # if defined, generates "stubs" for undefined
rules
$::RD_AUTOACTION =3D q{print "."}; # if defined, appends specified =
action
to productions

# Load up the grammar from the file
open( grammarFile, "QuickGrammar.txt" ) or die "Could not open grammar
file\n";
my @grammar =3D <grammarFile>;
close(grammarFile);

# Check the grammar
my $parser =3D Parse::RecDescent->new(join '', @grammar) or die "Bad
Grammar";

# Open and save the file contents
open( parsedFile, "bigfile.txt" ) or die "Could not open input file\n";
my @data =3D <parsedFile>;
close(parsedFile);

# Parse the file contents, joining all of the lines into a single one
my $retValue =3D $parser->OMDFile(join '', @data);=20

> -----Original Message-----
> From: [email protected] [mailto:[email protected]]=20
> Sent: Monday, July 17, 2006 2:47 PM
> To: [email protected]
> Subject: Speed issue w/ LARGE parsed file
>=20
> Hey all,
> 	I'm a recdescent newbie, so please cut me some slack ;)
>=20
> I've got a ~1.5Mb file that I'm parsing.  The grammar is=20
> pretty well established, in such that it's from a formal=20
> paper, and has EBNF notation written about it.  I've looked=20
> at the EBNF notation, and done my best to simplify it.  In=20
> other words, EBNF says some number should be from 0-65535, so=20
> I just specify /\d{1,5}/ to simplify & speed up the processing.
>=20
> W/ the first set of working grammar (tested using a subset of=20
> the file), and it has about 85 separate rules.
> I tried running it on the "full" file, but it just took too damn long.
>=20
> So, I went about creating a much simpler parser (even=20
> dumber), so I could do some pre-parsing, to speed things up.
>=20
> The file looks like:
>=20
> (foo bar)
> (foo (bar baz))
> (foo "bar")
> (foo (bar "baz")
>=20
> And these levels of data could be several levels deep w/ data.  E.g.:
>=20
> (foo (bar baz)
> (baz baz)
> (baz (baz (baz(baz "bar")))))
>=20
> So, I dumbed down my grammar (as can be seen below) but it=20
> still takes longer than I have patience for ( > 10 minutes) to parse.
>=20
> Am I SOL with parsing this file use RecDescent or is=20
> something glaringly bad w/ the below syntax?
>=20
> TIA
>=20
> --dw
>=20
> ############################################################
> # The main file has a header, and one or more object models=20
> File : Header Model(s)
>=20
> # Define what the header is
> Header:=20
>     "(" /Header[\s]v[\d]+\.[\d]+\.[\d]+\.[\d]+/ ")"=20
>     | <error: Invalid Header>
>=20
> # Define what the object model is
> Model:=20
>     "(Model"
>         Item(s)       =20
>     ")"
>     | <error: Invalid parse of the ObjectModel>
>=20
> Item:
>     "(" /\b[^\s]+\b/ /[^\(\)]*/ Item(s?) ")"  # Simply two tokens
>     | "(" /\b[^\s]+\b/ "\"" /[^\"]*/ "\"" Item(s?) ")"
>     | <error>
>=20
>=20
> # These items left in for clarity's sake. Functionally=20
> equivalent # to Item above, but hopefully faster
> OldItem:
>     "(" Label Data Item(s?) ")"  # Simply two tokens
>     | "(" Label QuotedData Item(s?) ")"
>     | <error>
>=20
> Label:
> 	/\b[^\s]+\b/
>=20
> Data:
> 	/[^\(\)]*/
>=20
> QuotedData:
> 	"\"" /[^\"]*/ "\""
> ############################################################
>=20