Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Think about Loose Coupling
 
PerlMonks  

Re: multiple occurences when using Parse::RecDescent

by gjb (Vicar)
on Mar 29, 2005 at 09:37 UTC ( [id://443141]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to multiple occurences when using Parse::RecDescent

If I understand the problem correctly, a grammar along the following lines should do what you want:

my $grammar = q { startrule: statement (endline statement)(s?) statement: print open text end print: "print" open: "'" text: /([^']*)/ end: "'" endline: /(\n|\r)+/ };
It tells the parser that there should be one or more statements, separated by endlines.

Hope this helps, -gjb-

Update: graff is correct, the above grammar unfortunately doesn't work on closer inspection, his is the solution I came up with after fixing mine with the exception of /^\Z/ as a terminator.

Replies are listed 'Best First'.
Re^2: multiple occurences when using Parse::RecDescent
by graff (Chancellor) on Mar 30, 2005 at 04:45 UTC
    For some reason, I couldn't get your rule to handle repeated ocurrences of the target pattern. Here's a variation that worked for me (and it includes printing out the "text" that it finds, to prove the result):
    startrule: statement(s) statement: print open text end print: /\s*print\s*/ open: "'" text: /([^']*)/ {print " $item[1] "} end: "'"
    Generally, for a grammar that's supposed to handle one or more of a basic top-level unit, it's easier/clearer to assign a name to that unit and have just that, plus "(s)", as the startrule, then break it down into components in a later rule.

    UPDATE: The OP said: i need it so that it will allow this several times down the textarea and spot an error in any of the occurences.

    To make it work that way, the startrule should be:

    startrule: statement(s) /$/
    which says that after matching one or more patterns that satisfy "statement", the parser should find the end-of-string. Without this, any string that starts with a valid statement will be accepted, regardless of any unmatchable content that follows.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://443141]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.