Right Associative Operators

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Right Associative Operators

John Berryman
Hello All, I'm new to this this list, new to Parboiled, new to Parsers, and I have a question about right associative operators.

A good example of a left associative operator is the minus operator. Ex: 3-2-1 should be parsed as (3-2)-1 = 0 rather than 3-(2-1)=2. An good example of a right associative operator is the power operator. Ex: 2^3^4 should be parsed as 2^(3^4) = [a very large number] rather than (2^3)^4 = 4096.

There calculator examples contain great examples of how to make a left associative operator, but there doesn't appear to be a good example for a right associative operator (this includes the erroneous implementation of the power operator). Can anyone show me how to make a right associative operator?

Best,
John
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Right Associative Operators

John Berryman
Here's my first cut at a Right Associative Operator 'R'. Tell me what you think. It seems a bit heavy weight to me. I also worry that I'll have collisions of some sort with other operators.

public Rule RightAssociativeOperator() {
        final Var<Integer> var = new Var<Integer>(new Integer(0));
        return Sequence(
                OneOrMore(
                        Sequence(
                                Term(),
                                'R',
                                var.set(var.get()+1)
                        )
                ),
                Term(),
                new Action<Context<String>>() {
                        @Override
                        public boolean run(Context<Context<java.lang.String>> c) {
                                for(int i = 0; i < var.get(); i++) {
                                        push("(" + pop(1) +"R"+ pop() + ")");
                                }
                                return true;
                        }
                }
        );
}
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Right Associative Operators

mathias
Administrator
John,

I think you should implement right associativity on the grammar level and not via actions.
Maybe this here can help you getting started:
http://www.engr.mun.ca/~theo/Misc/exp_parsing.htm

Cheers,
Mathias

---
[hidden email]
http://www.parboiled.org

On 24.04.2012, at 05:00, John Berryman [via parboiled users] wrote:

> Here's my first cut at a Right Associative Operator 'R'. Tell me what you think. It seems a bit heavy weight to me. I also worry that I'll have collisions of some sort with other operators.
>
> public Rule RightAssociativeOperator() {
>         final Var<Integer> var = new Var<Integer>(new Integer(0));
>         return Sequence(
>                 OneOrMore(
>                         Sequence(
>                                 Term(),
>                                 'R',
>                                 var.set(var.get()+1)
>                         )
>                 ),
>                 Term(),
>                 new Action<Context<String>>() {
>                         @Override
>                         public boolean run(Context<Context<java.lang.String>> c) {
>                                 for(int i = 0; i < var.get(); i++) {
>                                         push("(" + pop(1) +"R"+ pop() + ")");
>                                 }
>                                 return true;
>                         }
>                 }
>         );
> }
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/Right-Associative-Operators-tp3934144p3934239.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.
> NAML

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Right Associative Operators

John Berryman
Yes! Thanks for the resource. Now my implementation is much more concise and readable:

public Rule RightAssociativeOperator() {
return Sequence(
Term(),
Optional(
'R',
RightAssociativeOperator(),
push("(" + pop(1) +"R"+ pop() + ")")
)
);
}

However I'm also glad to have gone the way I did last night b/c I learned about your Var and Action constructs.

ThanksĀ Mathias,
-John
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Right Associative Operators

mathias
Administrator
Glad you sorted it out!

Happy hacking,
Mathias
---
[hidden email]
http://www.parboiled.org

On 24.04.2012, at 20:26, John Berryman [via parboiled users] wrote:

> Yes! Thanks for the resource. Now my implementation is much more concise and readable:
>
> public Rule RightAssociativeOperator() {
> return Sequence(
> Term(),
> Optional(
> 'R',
> RightAssociativeOperator(),
> push("(" + pop(1) +"R"+ pop() + ")")
> )
> );
> }
>
> However I'm also glad to have gone the way I did last night b/c I learned about your Var and Action constructs.
>
> Thanks Mathias,
> -John
>
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/Right-Associative-Operators-tp3934144p3936134.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.
> NAML

Loading...