<assoc=right> is not working

1,335 views
Skip to first unread message

Hunter Blakely

unread,
May 8, 2014, 3:29:54 PM5/8/14
to antlr-di...@googlegroups.com
The attached picture shows that even when I assign right association to '^' I am getting a left association.

Here is my sample grammer:

grammar testParser;

program
        : expr* EOF
        ;

expr
        : expr '^'<assoc=right> expr    // ^ operator is right associative
        | expr '*' expr                 // match subexpressions joined with '*' operator
        | expr '+' expr                 // match subexpressions joined with '+' operator
        | INT                           // matches simple integer atom
        ;

INT
        : [0-9]+
        ;

WS
        : [ \n\t\r]+ -> skip
        ;


My input:

1^2^3


What the parser sees:
    (1^2)^3

What I want the parser to see:
1^(2^3)




How do I get my intended output?



The attached picture shows my exact code, input, and output.

I apologize if I have made a glaring mistake, and I appreciate your input.
Screen Shot 2014-05-08 at 12.11.12 PM.png

Hunter Blakely

unread,
May 8, 2014, 3:38:56 PM5/8/14
to antlr-di...@googlegroups.com
I forgot to mention the version of ANTLR4:

Tiggerlocal:testRight hblakely$ brew info antlr
antlr: stable 4.2
/usr/local/Cellar/antlr/4.2 (3 files, 1.2M) *
  Built from source

ANTLR Parser Generator Version 4.2

Jonathan Coveney

unread,
May 8, 2014, 4:21:56 PM5/8/14
to antlr-di...@googlegroups.com
Seems like they changed the syntax, though it should give a warning



--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hunter Blakely

unread,
May 8, 2014, 5:25:03 PM5/8/14
to antlr-di...@googlegroups.com
Thank you for your post, the following worked: <assoc=right> expr '^' expr;

The reason I was so confused is because I was directly quoting the ANTLR4 book on page 70 which I had purchased thinking it would be up to date.
 

Jonathan Coveney

unread,
May 8, 2014, 5:49:46 PM5/8/14
to antlr-di...@googlegroups.com
It's a fast moving project!


El jueves, 8 de mayo de 2014, Hunter Blakely <hunter.j...@gmail.com> escribió:
Thank you for your post, the following worked: <assoc=right> expr '^' expr;

The reason I was so confused is because I was directly quoting the ANTLR4 book on page 70 which I had purchased thinking it would be up to date.
 

--

Terence Parr

unread,
May 8, 2014, 9:30:26 PM5/8/14
to antlr-di...@googlegroups.com
hi. you should be getting a warning…are you not? weird.
Ter
Reply all
Reply to author
Forward
0 new messages