Can I pre-process the rest of my input before checking match?

Skip to first unread message

Dane O'Connor

Jul 28, 2014, 1:06:12 PM7/28/14
Hi all, I'm trying to figure out if Treetop can help me solve my parsing problem in a cleaner way.

Suppose I have the following key:

# a b c

I'd like to create a grammar rule to match combinations of this string in any order:

"a b c"
"b a c"

"c b a"
# etc

In my custom parser, when I start looking for a match, I pre-proccess the remaining text in the string by sorting it alphabetically. This avoids the combinatorial explosion for more complex keys and makes checking for a match trivial. Is there a way to do something like this with treetop?

Something like:

rule key
  # temporarily alphabetically sort remaining string before check
  'a b c'

Paul Madden

Jul 31, 2014, 11:13:37 PM7/31/14
Hi Dane,

I don't know the answer to your actual question, and am not sure I've thought this through properly, but maybe consider this as an alternative to sorting the input:

rule abc
  [abc] 3..3 &{ |e| e[0] { |x| x.text_value }.uniq.size == 3 }

That is, match exactly three characters from the set {a,b,c}, remove the duplicates, then match the rule iff there are still three elements in the set. I think that matches the permutations of {a,b,c} and nothing else, which I think is what you want to do.

Reply all
Reply to author
0 new messages