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

26 views
Skip to first unread message

Dane O'Connor

unread,
Jul 28, 2014, 1:06:12 PM7/28/14
to treet...@googlegroups.com
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'
end


Paul Madden

unread,
Jul 31, 2014, 11:13:37 PM7/31/14
to treet...@googlegroups.com
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].elements.map { |x| x.text_value }.uniq.size == 3 }
end

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.

paul
Reply all
Reply to author
Forward
0 new messages