Compartilhar via


VBScript Quiz Answers, Part Six

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.

Comments

  • Anonymous
    March 16, 2005
    I have to say that any language where "For i =.For To Step Step Step" is a valid loop construct is asking for trouble! :)
  • Anonymous
    March 16, 2005
    And the greatest part of bracket identifiers is that you can have an essentially unlimited number of identifiers composed entirely of whitespace and non-printing characters. I can't think of another language that supports that.
  • Anonymous
    March 16, 2005
    "And the greatest part of bracket identifiers is that you can have an essentially unlimited number of identifiers composed entirely of whitespace and non-printing characters. I can't think of another language that supports that."
    -Oh, how VERY useful... Your series might as well have a main title 'Thousand reasons why to stick with C++' :))
  • Anonymous
    March 16, 2005
    I guess it makes code obfusctaion pretty trivial, though

    For ItemRef = FirstItem to LastItem Step Interval
    DoSomething(ItemRef)
    Next

    becomes

    For [Step Step [Step [Step] = [Step [Step [Step [Step] to [Step [Step [Step Step [Step] Step [Step [Step Step [Step Step [Step]
    [Step [[Step Step [Step]([Step Step [Step [Step])
    Next

    Hours of fun. :)
  • Anonymous
    March 16, 2005
    Alt+255 is also a valid identifier. So

    For = To Step
    = +
    Next

    compiles and runs. This is obfuscation =)
  • Anonymous
    March 16, 2005
    Uh, I should have used   instead of spaces...
  • Anonymous
    March 17, 2005
    That brings back memories of using Alt+255 to add "spaces" to filenames back in the DOS days...
  • Anonymous
    March 17, 2005
    I used Alt+255 to encrypt filenames too which made me just about the most 1337 h4X0R for my time.
  • Anonymous
    March 17, 2005
    Mat Hall, I just got done with your FOR loop, and boy are my feet tired.

    Ba-dum-pum.
  • Anonymous
    March 18, 2005
    Now we just need a series on the random syntactic irregularities, givens, gotchas and general goofiness of ECMAScript, Javascript, and JScript.

    Just when and where a semicolon is used along with when data gets automatically cast from one type to another ought to make for quite a series on its own.

    ... or maybe I've failed to pay attention and already missed it?

    I've never been a fan of the goofy way C-family languages use semicolons anyway. Algol established the semicolon early on as a statement SEPARATOR and not a TERMINATOR. Maybe Cobol was C's true inspiration?
  • Anonymous
    March 20, 2005
    The comment has been removed
  • Anonymous
    March 20, 2005
    The comment has been removed
  • Anonymous
    January 20, 2008
    PingBack from http://websitescripts.247blogging.info/cyrus-blather-irony-thy-name-is-c/