# Treetop equal precedence

**URL:** <https://www.ruby-forum.com/t/treetop-equal-precedence/127930>\
**Category:** Ruby\
**Created:** [February 2, 2008, 7:52pm UTC](https://www.ruby-forum.com/t/treetop-equal-precedence/127930 "2008-02-02T19:52:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 2, 2008, 7:52pm UTC](https://www.ruby-forum.com/t/treetop-equal-precedence/127930/1 "2008-02-02T19:52:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 3, 2008, 12:30am UTC](https://www.ruby-forum.com/t/treetop-equal-precedence/127930/2 "2008-02-03T00:30:42Z")

</div>

removed\_email\_address@domain.invalid 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.com/blog](http://dataconstellation.com/blog).

Clifford H., Data Constellation.
