Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

RecDescent help

by ikaruga (Novice)
on Jan 20, 2010 at 19:53 UTC ( #818549=perlquestion: print w/ replies, xml ) Need Help??
ikaruga has asked for the wisdom of the Perl Monks concerning the following question:

Why does this code work, as in print out the expected sequence ABCD, but if I add a print line to "character" it stops working? This code works:
use strict; use warnings; use Parse::RecDescent; my $grammar = q { start : character character character(s) {print "Found: ", $item[1], $item[2], join "", @{$item[3 +]}, "\n"; } character: /\w/ }; my $parser = Parse::RecDescent->new($grammar); defined $parser->start("ABCD") or die "didn't match";
However, the following code does not work:
use strict; use warnings; use Parse::RecDescent; my $grammar = q { start : character character character(s) {print "Found: ", $item[1], $item[2], join "", @{$item[3 +]}, "\n"; } character: /\w/ {print "Character: $item[1]\n"} }; my $parser = Parse::RecDescent->new($grammar); defined $parser->start("ABCD") or die "didn't match";
And by "not work" it prints out "1111" instead of the sequence:
Character: A Character: B Character: C Character: D Found: 1111
I sure that I am misunderstanding how RecDescent works. Can someone enlighten me? I'm using WinXP, the latest ActiveState perl, the latest RecDescent. BTW, this is a minor issue. But what's up with "chatterbox"? Why not use IRC like everyone else?

Comment on RecDescent help
Select or Download Code
Re: RecDescent help
by ikegami (Pope) on Jan 20, 2010 at 20:18 UTC
    Each production returns the result of its last rule, so
    character: /\w/ {print "Character: $item[1]\n"}

    returns the result of the { print ... } action. You want

    character: /\w/ { print "Character: $item[1]\n" } { $item[1] }

    Of course, it doesn't make much sense to print in a production the parser might backtrack through the production.

    But what's up with "chatterbox"? Why not use IRC like everyone else?

    Browsers don't speak IRC, they speak HTTP (mostly).

    And the chatterbox is associated with an IRC channel.

      Oh I see. That makes sense. I was just doing that to do a quick debug. So what is the IRC channel of perlmonks?
        I don't know. I don't use it.
Re: RecDescent help
by CountZero (Bishop) on Jan 20, 2010 at 22:06 UTC
    But what's up with "chatterbox"? Why not use IRC like everyone else?
    Because the Monastery is not like "everyone else".

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      That's what I feared. The "elites" going against the grain for no reason :-)
        The Monastery is so elite that anyone can join and find his/her/its place.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: RecDescent help
by duelafn (Priest) on Jan 22, 2010 at 15:16 UTC

    For debugging, set $::RD_TRACE = 1. It is very verbose, but allows you to debug your grammars without risk of altering their behavior. For your script, it outputs this: (truncated by me)

    Parse::RecDescent: Treating "/\w/" as a /../ pattern terminal printing code (9723) to RD_TRACE 1| start |Trying rule: [start] | 1| start | |"ABCD" 1| start |Trying production: [character | | |character character] | 1| start |Trying subrule: [character] | 2|character |Trying rule: [character] | 2|character |Trying production: [/\w/] | 2|character |Trying terminal: [/\w/] | 2|character |>>Matched terminal<< (return value: | | |[A]) | 2|character | |"BCD" 2|character |>>Matched production: [/\w/]<< | 2|character |>>Matched rule<< (return value: [A]) | 2|character |(consumed: [A]) | 1| start |>>Matched subrule: [character]<< | | |(return value: [A] | . . .

    Good Day,
        Dean

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://818549]
Approved by ikegami
Front-paged by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2014-07-31 22:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (254 votes), past polls