1

I've been wondering what's the meaning of "alternative" in ecma262.

i've seen that the term "alternative" was used many times in the spec.

here are some examples:

quote taken from this section

so, in this example, the nonterminal ForStatement actually has four alternative right-hand sides.

quote taken from this section

A production that has multiple alternative definitions will typically have a distinct algorithm for each alternative

quote taken from this section

a production that has multiple alternative definitions will typically have for each alternative a distinct algorithm for each applicable named static semantic rule.

what does it mean "production that has multiple alternative definitions" ?

i assume that alternative mean the right hand side of a production, here is a simple picture that shows what i mean. enter image description here

on the picture we can see that the area covered by Pink is the whole Production.

and the area covered by Red is the Nonterminal

finally i'm assuming that the area covered by purple is the Alternative

A production that has multiple alternative definitions will typically have a distinct algorithm for each alternative

however it's still doesn't sounds right, because how can a one individual production have multiple alternatives ?

AngryJohn
  • 576
  • 4
  • 10
  • There are four different right hand sides for the nonterminal `ForStatement`: https://tc39.es/ecma262/#prod-grammar-notation-ForStatement – Felix Kling Jan 13 '22 at 15:31
  • It's more accurate and correct to say that there are 4 productions for the `ForStatement` nonterminal with different right hand sides for each production, and it's not like you said "four different right hand sides for the production `ForStatement`". so again my question is how can a one individual production have multiple alternatives ? it doesn't makes sense... – AngryJohn Jan 13 '22 at 15:39
  • It's the same thing, at least according to the spec. From further up: *"The definition of a nonterminal (also called a “production”) is introduced by the name of the nonterminal being defined followed by one or more colons. (...) One or more alternative right-hand sides for the nonterminal then follow on succeeding lines."* I think you are overthinking this. It basically means that if a piece of code matches any of the four sequences, you have a `ForStatement`. – Felix Kling Jan 13 '22 at 15:46

2 Answers2

2

The word has its normal, English meaning:

offering or expressing a choice


So to take the first instance:

so, in this example, the nonterminal ForStatement actually has four alternative right-hand sides.

And just before that it lists them:

for ( LexicalDeclaration ; ) Statement
for ( LexicalDeclaration ; Expression ) Statement
for ( LexicalDeclaration Expression ; ) Statement
for ( LexicalDeclaration Expression ; Expression ) Statement 

Four alternative things you can put on the right-hand side of the keyword for.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

In formal language theory, a production has a left-hand side and a right-hand side. But in less formal contexts like the EcmaScript spec, it's common to group productions that have the same left-hand side.

So in a formal context, you might see:

A : B
A : C D

and you would say "There are 2 productions, each with a LHS and a RHS."

But in the EcmaScript spec, you might see:

A :
  B
  C D

and you would say "There is 1 production, with a LHS and 2 alternatives." (This avoids confusion over whether "right-hand side" would refer to everything after the colon, or just a single line.)

So when you ask "how can one individual production have multiple alternatives ?", it sounds like you're thinking of the formal context, where indeed it wouldn't make sense. But it does make sense in the less formal context.

(Note that the EcmaScript spec actually uses both terminology schemes, but it's usually not difficult to know which.)

Michael Dyck
  • 2,153
  • 1
  • 14
  • 18