4

The Vertical Bar Operator (|) is used in a variety of constants to mean OR in some way:

C-style languages use a | b to mean bitwise OR of a and b, a || b to mean logical OR of a and b.

Regular expressions use a|b to mean match with a or match with b.

Context Free Grammars use x := a | b in much the same sense as regexes.

While &for AND makes sense, why is | used for OR?

  • 5
    Because that's what the language designers chose. There isn't any other character on the keyboard that intrinsically means "or" more than the vertical bar does. In fact, all of the other characters have some intrinsic meaning that is not a or, so you could make the argument that they chose it because it is the most neutral. – Robert Harvey May 9 '16 at 0:03
  • 5
  • 5
    An awful lot of languages copy from C. – Loren Pechtel May 9 '16 at 2:20
  • 1
    I'm voting to close this question as off-topic because it is not about the systems development life cycle. – user22815 Mar 17 '17 at 23:39
  • 1
    @Snowman - did you vote to close a third of the other questions on Programming too, with that reason? – namey Mar 26 '17 at 9:27
2

A very good answer is provided on Stack Overflow.

The core of the explanation is:

In Backus-Naur form, an expression consists of sequences of symbols and/or sequences separated by '|', indicating a choice, the whole being a possible substitution for the symbol on the left.

<personal-name> ::= <name> | <initial>

Of course, the next question could be (tnx, @Rob):

"Why is a vertical bar used as the OR operator in Backus-Naur?"

But that is another question, with a merit in itself :)

My assumed answer: because they had to use something, and nothing that was available could be used non-ambiguously.

  • / is the division symbol, path separator in *nix systems

  • \ (today it is path separator in DOS / MS Windows systems)

  • +-* are standard mathematical operators

and so on...


From a comment from @amon:

Backus introduced the grammar notation in The Syntax and Semantics of the Proposed International Algebraic Language where he just used the word "or". Backus et al with Naur as editor replaced this with the symbol "|" in their Revised Report on Algol 60 (original report 1960, revised report 1962) with no explicit reason, probably for brevity. All of this predates Unix by roughly a decade, and slightly predates the ASCII standard.

  • 2
    Then the question becomes "Why is a vertical bar used as the OR operator in Backus-Naur?" – Rob Mar 1 at 11:03
  • @Rob: no, it does not become anything. You just formulated another question, with a merit in itself :) Assumption: because they had to use something, and nothing that was available could be used non-ambiguously. – virolino Mar 1 at 11:07
  • I agree with you but it does beg the question. – Rob Mar 1 at 11:09
  • 1
    I don't think "Why is it used in Backus-Naur" would justify a separate question. Yes, it's not technically a "programming language", so by a strict reading of the question would be out of scope; but it's still clearly in the realm of computer science, so it feels pedantic to demand a separate answer just to change that scope. – IMSoP Mar 1 at 11:44
  • 2
    Backus introduced the grammar notation in The Syntax and Semantics of the Proposed International Algebraic Language where he just used the word "or". Backus et al with Naur as editor replaced this with the symbol "|" in their Revised Report on Algol 60 (original report 1960, revised report 1962) with no explicit reason, probably for brevity. All of this predates Unix by roughly a decade, and slightly predates the ASCII standard. – amon Mar 1 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.