VBScript Quiz Answers, Part Six

VBScript Quiz Answers, Part Six

  • Comments 14

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.

  • I have to say that any language where "For i =.For To Step Step Step" is a valid loop construct is *asking* for trouble! :)
  • 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.
  • "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++' :))
  • 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. :)
  • Alt+255 is also a valid identifier. So

    For = To Step
    = +
    Next

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

    Ba-dum-pum.
  • 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?
  • Oh, we've had plenty of those already. Check the archives.

    My article on semicolon usage in JScript is here:

    http://blogs.msdn.com/ericlippert/archive/2004/02/02/66334.aspx

    My freakin' HUGE series on JScript .NET type coercion semantics begins here

    http://blogs.msdn.com/ericlippert/archive/2004/08/02/206216.aspx

    I would love to have a discussion of why JScript cannot be lexed independently of parsing, but that will require quite a bit of background material on the theory of programming languages. I hope to get to that at some point this year.



  • What always bugged me about VB6 (and I assume VBScript, although I haven't written many classes in it) is that you could not have a method called Open or Line (and others) because of the BASIC-heritage. You could easily use COM Libraries written in other languages that had an Open or Line method but you couldn't code them in VB.

    The only possible problem would be when calling the Open or Line method on your own object, but forcing it to be qualified with Me. would have been fine. And the [bracked identifier] notation only applies to local variables (I belive), so it couldn't be used to get around this problem.

    I assume that VB.NET fixed this problem, but it may also be that Open and Line are no longer reserved words in VB.NET.
  • PingBack from http://websitescripts.247blogging.info/cyrus-blather-irony-thy-name-is-c/

Page 1 of 1 (14 items)