Parse::RecDescent and unordered rules

Discussion in 'Perl Misc' started by Trond Michelsen, Aug 19, 2005.

  1. Hi.

    I'm completely new to Parse::RecDescent and parsing in general (I know
    my regexes, though). I'm trying to write a parser to read a config-file,
    and I've managed to rules for all the relevant parts of the config-file,
    and everything seems to work fine. But, there are several parts of the
    config-file where the order doesn't (well, shouldn't) matter, and I was
    wondering if there is a simple way to tell Parse::RecDescent that the
    order of these rules doesn't matter?

    Here's an example:

    alphanum: /[A-Za-z0-9_]/
    word: alphanum(s)
    name: 'NAME' word
    rank: 'RANK' word
    description: 'DESCRIPTION' word
    example: name rank description(?)

    So, with this grammar, the following would match:

    NAME foo
    RANK bar
    DESCRIPTION baz

    but this wouldn't

    RANK bar
    NAME foo
    DESCRIPTION baz

    I know I can just expand the rule to include all combinations, like this:

    example: name rank description(?) |
    name description rank |
    rank name description(?) |
    rank description name |
    description name rank |
    description rank name

    but this seems silly. Besides, the real grammar is slightly more complex
    too, so I'm not sure it's maintainable in the long run.

    So, is it possible to tell Parse::RecDescent that a particular group of
    rules are allowed in any order, or is there a better way to solve this?
    (or should I simply tell my users that order is important, and they'd
    better get used to it :)

    --
    Trond Michelsen
     
    Trond Michelsen, Aug 19, 2005
    #1
    1. Advertising

  2. Trond Michelsen

    Dave Weaver Guest

    Trond Michelsen <> wrote:
    > I'm completely new to Parse::RecDescent and parsing in general (I know
    > my regexes, though). I'm trying to write a parser to read a config-file,
    > and I've managed to rules for all the relevant parts of the config-file,
    > and everything seems to work fine. But, there are several parts of the
    > config-file where the order doesn't (well, shouldn't) matter, and I was
    > wondering if there is a simple way to tell Parse::RecDescent that the
    > order of these rules doesn't matter?


    Perhaps something like:

    example: item(s)

    item: name | rank | description

    This, of course, allows any or all items to be omitted, but a
    post-parse check to ensure all required items have been specified
    should sort that out.
     
    Dave Weaver, Aug 22, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jean-gert nesselbosch

    XPath1.0-grammar compatible to Parse::RecDescent

    jean-gert nesselbosch, May 12, 2006, in forum: XML
    Replies:
    0
    Views:
    420
    jean-gert nesselbosch
    May 12, 2006
  2. ccm news
    Replies:
    0
    Views:
    3,102
    ccm news
    Jan 15, 2009
  3. Lex Williams

    Alternative to Parse::RecDescent

    Lex Williams, Aug 26, 2008, in forum: Ruby
    Replies:
    4
    Views:
    209
    James Gray
    Aug 27, 2008
  4. Jon Ericson

    Huffman coding and Parse::RecDescent

    Jon Ericson, Apr 22, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    188
    Jon Ericson
    Apr 23, 2004
  5. A. Farber
    Replies:
    1
    Views:
    126
    Anno Siegel
    May 11, 2004
Loading...

Share This Page