Fabulous Adventures In Coding

Eric Lippert's Blog

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.

Published Wednesday, March 16, 2005 10:48 AM by Eric Lippert
Filed under: , ,

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 RSS

Comments

 

Mat Hall said:

I have to say that any language where "For i =.For To Step Step Step" is a valid loop construct is *asking* for trouble! :)
March 16, 2005 11:59 AM
 

Nicholas Allen said:

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.
March 16, 2005 6:29 PM
 

Keff said:

"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++' :))
March 17, 2005 1:50 AM
 

Mat Hall said:

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. :)
March 17, 2005 3:38 AM
 

Martin Adoue said:

Alt+255 is also a valid identifier. So

For = To Step
= +
Next

compiles and runs. *This* is obfuscation =)
March 17, 2005 4:16 AM
 

Martin Adoue said:

Uh, I should have used   instead of spaces...
March 17, 2005 4:22 AM
 

Mat Hall said:

That brings back memories of using Alt+255 to add "spaces" to filenames back in the DOS days...
March 17, 2005 9:57 AM
 

Rich Chestmat said:

I used Alt+255 to encrypt filenames too which made me just about the most 1337 h4X0R for my time.
March 17, 2005 11:50 PM
 

Dave said:

Mat Hall, I just got done with your FOR loop, and boy are my feet tired.

Ba-dum-pum.
March 18, 2005 4:57 AM
 

Bob Riemersma said:

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?
March 18, 2005 2:31 PM
 

Cyrus' Blather said:

March 20, 2005 6:39 PM
 

Eric Lippert said:

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.



March 20, 2005 4:41 PM
 

Matthew W. Jackson said:

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.
March 20, 2005 10:31 PM
 

Website Scripts » Cyrus’ Blather : Irony thy name is C# said:

January 21, 2008 3:36 AM

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required
Submit

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.

This Blog

Syndication


© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker