Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Parse::RecDescent

by webfiend (Vicar)
on Mar 19, 2009 at 16:55 UTC ( [id://751788]=note: print w/replies, xml ) Need Help??


in reply to Parse::RecDescent

This seems like a good place to mention that you'll need to place use statements in a startup block to take advantage of any Perl 5.10-isms.

Code slightly modified from an example in Advanced Perl 2nd Edition:

use Modern::Perl; use Parse::RecDescent; my $grammar = q{ { use Modern::Perl } GameTree : "(" Sequence GameTree(s?) ")" Sequence : Node(s) Node : ":" Property(s) { say "I saw a node!" } Property : PropIdent PropValue(s) PropIdent : /[A-Z]+/ PropValue : { extract_bracketed($text, '[ ]') } }; my $sgf_parser = Parse::RecDescent->new($grammar); undef $/; my $sgf = <DATA>; say $sgf_parser->GameTree($sgf); __DATA__ (:GM[1]FF[4]AP[CGoban:2]ST[2]RU[Japanese] PW[Honinbo Shuwa]PB[Yasuda Shusaku] WR[7d]BR[5d] :B[qd]:W[dc]:B[pq]:W[oc]:B[cp]:W[qo] :B[pe]C[This is the famous Shusaku opening".])

I could be wrong, of course. This is how I got the thing to run with say in the action, though.

Replies are listed 'Best First'.
Re^2: Parse::RecDescent
by ikegami (Patriarch) on Mar 19, 2009 at 17:20 UTC

    No, you're right. What's in the top-level curlies ends up being top-level code in the package. My parsers always look like:

    use strict; use warnings; my $grammar = <<'__EOI__'; { use strict; use warnings; } ... __EOI__

    Note the use of heredocs. Much better than q{} since you don't have to escape some backslashes and some curlies. For example, to have a production match a single backslash,

    Single quote heredoc: rule: /\\/
    Single quote literal: rule: /\\\/ or rule: /\\\\/

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://751788]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-19 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found