Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C operator precedence of square brackets used as message expression/notation?

Tags:

objective-c

Does the Objective-C message expression (message notation) operator, which uses square brackets [], have the same precedence as the C operator for array subscripting, which also uses square brackets []?

I refer to this table of C operators.

Also, an analogous question applies to the Objective-C "dot syntax" operator for accessor method invocation compared to the C operator for "element selection by reference". Do they have the same precedence?

I searched for an hour for a straightforward, definitive answer to this basic question. Surprisingly, I did not find one. Hence, this question. Links welcome.

like image 851
Jeff Avatar asked Mar 12 '13 20:03

Jeff


2 Answers

You have become confused because many explanations of grammars and precedence take the shortcut of saying that operators have precedence. They don't. It is productions in the grammar that have precedence, and they have precedence relative to other productions. It is only meaningful for two productions to have precedence relative to each other if the grammar is ambiguous (meaning it can produce two different parse trees for the same input), and if the ambiguity is resolved by specifying the precedence of one production over the other.

Let me explain with an example.

Here's a toy grammar:

expression =
    | IDENTIFIER
    | NUMBER
    | expression '+' expression
    | expression '*' expression
    | expression '(' expression ')' // function call
    | '(' expression ')' // grouping
    | expression '[' expression ']' // array subscript
    | '[' expression IDENTIFIER ':' expression ']' // message send
    ;

Now, consider parsing 1 + 2 * 3 with this grammar. There are two valid parse trees:

  +              *
 / \            / \
1   *          +   3
   / \        / \
  2   3      1   2

By specifying that the * production has a higher precedence than the + production, we require the parser to produce the left tree instead of the right tree. Thus the idea of a precedence relationship between the + production and the * production makes sense: it has an effect on the parser's output.

Similarly, 1 + foo(3) has two parse trees:

  +               ()
 / \             /  \
1   ()          +    3
   /  \        / \
 foo   3      1  foo

So again the idea of a precedence relationship between the '+' production and the function call production makes sense. The case of 1 + foo[3] (which uses the subscript production in place of the function call production) is analogous, so it makes sense to specify a precedence relationship between the '+' production and the subscript production.

Now consider 1 + (2 * 3). The grammar can only produce one possible parse tree:

  +
 / \
1  ( )
    |
    *
   / \
  2   3

There is no need for a precedence relationship between the + production and the grouping production, because there is only one way to parse this input. It would be meaningless to specify that the grouping production has higher precedence than the + production, because there is no other parse tree that you could produce by doing so.

Finally, consider 1 + [2 add:3]. This is analogous to the grouping example. There is only one possible parse tree:

   +
  / \
 /   \
1   [ ]
   / | \
  /  |  \
 2  add  3

No other parse tree is possible. There is no need to specify a precedence relationship between the + production and the message send production. Specifying a precedence relationship between them would have no effect, because the grammar simply doesn't allow this input to be parsed any other way.

like image 137
rob mayoff Avatar answered Sep 22 '22 00:09

rob mayoff


They are in the same precedence group. I believe the message send [] is equivalent to () because the runtime treats them as parenthesis in the case of messages.

http://www.techotopia.com/index.php/Objective-C_2.0_Operator_Precedence

like image 37
Fruity Geek Avatar answered Sep 20 '22 00:09

Fruity Geek