#!/usr/bin/perl -lw use strict; use Parse::RecDescent; $::RD_TRACE = 1; # dump a trace $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c. $::RD_HINT = 1; # Give out hints to help fix problems. # universal token prefix pattern (default is: '\s*') warn "TOKEN SEPARATOR: $Parse::RecDescent::skip \n"; my $grammar = <<'_EOGRAMMAR_'; File: Tag(s) EOF Tag: | MT Element EOT { print "MT: ", $item[1], "\n"; print " ", $item[2], "\n"; } | MT Element { print "MT: ", $item[1], "\n"; print " ", $item[2], "\n"; } | MT EOT { print "MT: ", $item[1], "\n"; } | MT { print "MT: ", $item[1], "\n"; } | MT: // Element: /[^\/\n]+/ EOF: /^\Z/ EOT: /\// _EOGRAMMAR_ ### THIS IS THE END OF THE GOSH DURN GRAMMAR # Create and compile the source file my $parser = Parse::RecDescent->new($grammar) or die "Dang!! $!"; $parser->File(q[ This SUCKS WHY? CAUSE IT DOESN'T WORK SO What you gonna DO?/NOW I dunno ]); __END__ TOKEN SEPARATOR: \s* Parse::RecDescent: Treating "File:" as a rule declaration Parse::RecDescent: Treating "Tag(s)" as a one-or-more subrule match Parse::RecDescent: Treating "EOF" as a subrule match Parse::RecDescent: Treating "Tag:" as a rule declaration Parse::RecDescent: Treating "|" as a new production Parse::RecDescent: Treating "MT" as a subrule match Parse::RecDescent: Treating "Element" as a subrule match Parse::RecDescent: Treating "EOT" as a subrule match Parse::RecDescent: Treating "{ print "MT: ", $item[1], "\n"; print " ", $item[2], "\n"; }" as an action Parse::RecDescent: Treating "|" as a new production Parse::RecDescent: Treating "MT" as a subrule match Parse::RecDescent: Treating "Element" as a subrule match Parse::RecDescent: Treating "{ print "MT: ", $item[1], "\n"; print " ", $item[2], "\n"; }" as an action Parse::RecDescent: Treating "|" as a new production Parse::RecDescent: Treating "MT" as a subrule match Parse::RecDescent: Treating "EOT" as a subrule match Parse::RecDescent: Treating "{ print "MT: ", $item[1], "\n"; }" as an action Parse::RecDescent: Treating "|" as a new production Parse::RecDescent: Treating "MT" as a subrule match Parse::RecDescent: Treating "{ print "MT: ", $item[1], "\n"; }" as an action Parse::RecDescent: Treating "| " as an error marker Parse::RecDescent: Treating "MT:" as a rule declaration Parse::RecDescent: Treating "//" as a /../ pattern terminal Parse::RecDescent: Treating "Element:" as a rule declaration Parse::RecDescent: Treating "/[^\/\n]+/" as a /../ pattern terminal Parse::RecDescent: Treating "EOF:" as a rule declaration Parse::RecDescent: Treating "/^\Z/" as a /../ pattern terminal Parse::RecDescent: Treating "EOT:" as a rule declaration Parse::RecDescent: Treating "/\//" as a /../ pattern terminal | File |Trying rule: [File] | | File | |"\n This SUCKS | | |\n WHY? | | | CAUSE IT DOESN'T | | |WORK\n SO What you gonna | | |DO?/NOW\nI dunno\n" | File |Trying production: [Tag EOF] | | File |Trying repeated subrule: [Tag] | | Tag |Trying rule: [Tag] | | Tag |Trying production: [] | | Tag |>>Matched production: []<< | | Tag |>>Matched rule<< (return value: [Tag])| | Tag |(consumed: []) | | File |>>Matched repeated subrule: [Tag]<< (1| | |times) | | File |Trying subrule: [EOF] | | EOF |Trying rule: [EOF] | | EOF |Trying production: [/^\Z/] | | EOF |Trying terminal: [/^\Z/] | | EOF |<> | | EOF | |" This SUCKS | | |\n WHY? | | | CAUSE IT DOESN'T | | |WORK\n SO What you gonna | | |DO?/NOW\nI dunno\n" | EOF |<> | | File |<> | | File |<> | printing code (31240) to RD_TRACE