This question was designed to highlight some of the oddities in VBScript's rules for what makes a legal identifier.
6) Which of the following are syntactically legal? Why?
(a)
Explicit Error
(b) For[i=1]=[To[1]To[1]
(c) For i =.For To Step Step Step
(d) For Each Each In In
(d) is illegal, the rest are legal. (a) is legal because neither
Explicit nor Error are reserved words.
(b) is legal because any text (of fewer than 255 characters, no newlines or ] allowed) surrounded by brackets is a legal identifier. In this case, [i=1], [To[1] and [1] are legal identifiers. Yes, [ is a legal character in a bracket identifier.
(c) is legal because it is legal to use reserved words such as For after the dot operator, and because Step is not a reserved word.
(d) is illegal because both Each and In are reserved. Notice that we can get away with making keywords unreserved if doing so does not introduce an ambiguity. However, we prefer to ensure not only that the program, statement or expression is unambiguous as a whole, but furthermore that the parser be able to disambiguate by looking forward no more than one token.
If
Each were a legal identifier, then (as one reader pointed out) For Each Foo In Bar and For Each = 1 To 10 require the parser to look ahead two from the For to determine what kind of loop this is. Step on the other hand can easily be disambiguated. Of course, so could In, which is reserved. My opinion is that when possible, one should err on the side of reserving, rather than allowing people to write patent nonsense like Step Step Step, but as we discussed last time, sometimes you can't reserve words that you'd like to.
Comment Notification
If you would like to receive an email when updates are made to this post, please register here
Subscribe to this post's comments using
About Eric Lippert
Eric Lippert is a senior developer on the Microsoft C# compiler team. Before that he worked on the framework of Visual Studio Tools For Office. Before that, he worked on the compilers, runtimes and tools for VBScript, JScript, Windows Script Host and other Microsoft Scripting technologies. He lives in Seattle and spends his free time editing books about programming languages, playing the piano, and trying to keep his tiny sailboat upright in Puget Sound.