Parse::RecDescent and unordered rules

T

Trond Michelsen

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 :)
 
D

Dave Weaver

Trond Michelsen said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top