Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Parse::RecDescent problem (wrong example in perl.com article)

by maard (Pilgrim)
on Jun 05, 2005 at 19:56 UTC ( [id://463766]=perlquestion: print w/replies, xml ) Need Help??

maard has asked for the wisdom of the Perl Monks concerning the following question:

Hello, wise monks!

I've been trying to construct parser for Clarion language and decided to start with simple things. After reading Parse::RecDescent's documentation, I took Parse::RecDescent Tutorial as an example to study and modify. The article has a code example which parses different code pieces, among which there's a code with assignment and printing of value: $parser->startrule("a = 5 ; print a"); That works fine. Until there're other instructions after print_instruction, in which case it fails to parse anything after first print:

$parser->startrule("a = 5 ; print a ; print a"); # this fails $parser->startrule("a = 5 ; print a ; b = 1"); # this fails either
Investigating parser trace I've found that the only text which is consumed for the entire input is [a = 5 ; print].

Can wise monks help to understand, why the argument to print isn't consumed? And how to rewrite those rules for print_instruction to work correctly?

Replies are listed 'Best First'.
Re: Parse::RecDescent problem (wrong example in perl.com article)
by kaif (Friar) on Jun 06, 2005 at 00:12 UTC

    Talk about a bug that took me an hour to fix. Changing

    { return $main::VARIABLE{$item{VARIABLE}} }
    to
    { $return = $main::VARIABLE{$item{VARIABLE}} }
    does the trick. For completeness, I would change the other two return FOO to $return = FOO also.

    I think you should write to the author of that tutorial and give him a heads-up. Also, this is off-topic, but the line /\w[a-z0-9_]*/i isn't quite right. Indeed, /\w/ is /[a-z0-9_]/i, so what is probably intended is /[a-z]\w*/i. In this case, it makes little difference (the former allows variables beginning with underscore), but if some code is rearranged then errors might pop up.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-20 02:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found