treetop equal precedence

M

MistryMan4000

[Note: parts of this message were removed to make it a legal post.]

How do I make two rules with equal precedence. The obvious example is
true||false&&false
should return false because the true||false is evaluated first, then &&false.
but also
true&&false||true
should return true because true&&false is false, but then the ||true is evaluated.
 
C

Clifford Heath

[Note: parts of this message were removed to make it a legal post.]

How do I make two rules with equal precedence. The obvious example is
true||false&&false
should return false because the true||false is evaluated first, then &&false.
but also
true&&false||true
should return true because true&&false is false, but then the ||true is evaluated.

I don't understand how your example relates to the way you worded your question,
but if you have two or more operators at the same level of precedence, just use
a sequence. For example, a sum of 1 or more terms can be expressed as:

rule sum
term ( ('+'/'-') term )*
end

(with white-space skipping stripped out). If you want to evaluate the result,
say by providing and calling a value() method, here's the idiom I use:

rule add_op
'+' { def operate(a, b); a+b; end }
/ '-' { def operate(a, b); a-b; end }
end

rule sum
term seq:(add_op term)* {
def value
seq.inject(term.value) { |s, e| e.add_op.operate(s, e.term.value) }
end
}
end

A working example of this is in my Treetop presentation, available as audio,
slides, and example programs, at my blog, <http://dataconstellation.com/blog>.

Clifford Heath, Data Constellation.
 

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,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top