Parse::Gnaw

An extensible parser
Download

Parse::Gnaw Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Greg London
  • Publisher web site:
  • http://search.cpan.org/~gslondon/

Parse::Gnaw Tags


Parse::Gnaw Description

An extensible parser Parse::Gnaw is an extensible parser in Perl. Define grammars using subroutine calls. Define your own grammar extensions by defining new subroutines. Parse text in memory or from/to files or other streams.SYNOPSISGnaw is a perl module which implements full regular expressions and full text parsing grammars using nothing but pure perl code limited to subroutine closures and basic perl variables such as scalars, hashes, arrays, and references.You write your grammar in pure perl. There is no intermediate "parser language" that then gets interpreted into something executable.When you do a "use Parse::Gnaw", the Gnaw module will import a number of functions directly into your namespace. Yes, this is completely bad form for normal modules. But this is not a normal module. The imported subroutines include regular expression and parsing functions for matching, quantifiers, literals, alternations, character classes, and so on. You build up your grammar by calling these functions. The final call will return a code reference. This code reference is your grammar.When you dereference that grammar, if it is a "match" function, then you pass in the string you want to parse. use Parse::Gnaw; # create the grammar my $grammar = match('hello'); # apply the grammar to a string if($grammar->('hello world')) { print "match "; } else { print "no match"; }You can also create the grammar and execute it in one step: my $texttoparse = "howdy partner"; if(match('hello', 'world')->($texttoparse)) { print "match "; } else { print "no match "; }Note the above example translated into perls regular expression syntax would look something like this: my $texttoparse = "howdy partner"; if($texttoparse =~ m{hellos*world}) { print "match "; } else { print "no match "; }You can build up more complicated grammars fairly easily. This one looks for a sentence about fruits. $grammar = match(ql('I would like to buy'), some('a', qa('banana apple pear peach')));The "match" function looks for a match to the grammar in the string being parsed.The "ql" function (quoted literal) allows you to put a sequence of literals into a single string. It the splits the string up into individual literals much like perls "qw" function does. Then it puts them into a grammar sequence for you. This saves you from putting quotes around each individual literal.The "some" function is a quantifier looking for "1 or more" of whatever it surrounds, in this case, a sequence of the literal "a" followed by an alternation of different possible fruit.The "qa" function (quoted alternation) takes a single string and splits it into individual words, also similar to perls "qw" function. The "qa" function then takes those individual words and creates an alternation that tries to match any individual word in the string as a valid alternate. Requirements: · Perl


Parse::Gnaw Related Software