4

I am trying to classify the "dot" token used in the dot notation (object.property).

Being a self-taught amateur developper, mainly using JavaScript, I have a simplified (and certainly imperfect) understanding of programming and JavaScript.

When reading code, I tend to classify tokens in five categories :

  1. identifiers (+ reserved keywords)
  2. operators
  3. delimiters/punctuators
  4. litterals
  5. comments

But when it comes to classify the dot used in the dot notation according to my simplified model, I have a doubt, but maybe that try to classify this way does not makes sense.

I view it more like a binary operator, that takes two identifiers and return a reference (could it be a delimiter?). But I didn't managed to find a source that clearly establish this. (Or at least that clearly establish it and that I am able to understand).

I looked in various documentations, including the ECMA specs.

So here are the two questions I have :

  • Where do you think I could find the information I am looking for, or which keywords could I use ?
  • What do you think about the dot classification as a token ? Binary operator ? How do you know ?
mel
  • 41
  • 3
  • It's not an operator. It's a property accessor. [MDN reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors) – VLAZ Jun 25 '19 at 12:49
  • Yes, I have seen that. But I am trying (and it maybe foolish) to categorise it according to a simplified model. – mel Jun 25 '19 at 13:00
  • 1
    Yes, you could call it a member operator. – Bergi Jun 25 '19 at 13:58

2 Answers2

3

Syntactically, it's a postfix operator. Perhaps it would be better to call it a family of postfix operators, one for each legal identifier which can follow it.

It's similar to subscript notation, v[a], which is also better thought of syntactically as a (delimited) postfix operator, even though it has two values as operands. In the same way, we can call the so-called ternary operator cond ? alt1 : alt0 a delimited binary operator. Syntactically, the "operator" is ? alt1:

This view of syntax, which might initially appear a bit strange, comes from the description of an operator grammar, which basically has the form, using ⟨⟩ for grouping and * for optional repetition (i.e., 0 or more instances, as in regular expressions).

⟨ PREFIX*  OPERAND POSTFIX* ⟩ ⟨ INFIX ⟨ PREFIX*  OPERAND POSTFIX* ⟩ ⟩*

That formulation handles parentheses by including ( EXPRESSION ) in OPERAND (recursively), for parentheses are for grouping, and including ( EXPRESSION_LIST ) in POSTFIX for parentheses used in function calls.

Looking at expressions that way facilitates parsing. It also helps clarify what is meant by precedence and associativity, especially for delimited operators.

A general rule of operator precedence is that we start by giving all postfix operators the same highest precedence. This is not theoretically necessary, but it almost always leads to the expected meaning. No-one would interpret 2+sin(0.5) as a call to the "function" 2+sin. More relevant to your question, 2+s.size intuitively means that .size is first applied to s and then 2 is added to the result.

rici
  • 234,347
  • 28
  • 237
  • 341
2

If you are focused on pure dot notation and more if you have JS as example then it is a delimiter.

Of course it does not apply to frameworks or shapes that add functionality. For example a framework that automate get/set calls for private attributes or similar.

Why? Because dot notation is an alternative to braket notation or upside down. And brakets are a delimiter. thing['otherthing'] is same than thing.otherthing

Sam
  • 1,459
  • 1
  • 18
  • 32
  • 1
    (I know I shouldn't post a comment just to state that I found your answer interesting but I am not able to mark it as useful as my account is brand new.) – mel Jun 25 '19 at 13:05
  • @mel IIRC there's a time-limit. Just wait 2 hours, I think. –  Jun 25 '19 at 13:10
  • @mel, Do you mean you can't [accept](http://stackoverflow.com/help/someone-answers) an answer or just [upvote](//stackoverflow.com/privileges/vote-up)? – lealceldeiro Jun 25 '19 at 13:12
  • Im not here for votes :) Do what you can/desire/want... I hope almost you find it usefull. Cheers and welcome. – Sam Jun 25 '19 at 13:21
  • I wasn't able to upvote because I had a reputation < 15. I did not closed the question because I am thinking about if at the moment. I have a limited understanding of programming and I feel that my question is kind of senseless, because I am trying to categorize a token according to a model that I invented on the basis of nothing but my will to have a simple representation of something complex. Maybe I shouldn't have used javascript for my question but a fictive language. – mel Jun 25 '19 at 13:25
  • If you find it usefull for you It can be usefull for others. We are all learning here. I vote you do not close the question. Is a good question and classify also clarify. I have not ask the question to myself until today. Always we learn something. – Sam Jun 25 '19 at 13:28