[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

treetop equal precedence

MistryMan4000@Yahoo.com

2/2/2008 6:52:00 PM

[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.


---------------------------------
Never miss a thing. Make Yahoo your homepage.
1 Answer

Clifford Heath

2/2/2008 11:28:00 PM

0

MistryMan4000@Yahoo.com wrote:
> [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.co....

Clifford Heath, Data Constellation.