Fabulous Adventures In Coding
Eric Lippert is a principal developer on the C# compiler team. Learn more about Eric.
There were a grand total of eight entries to my VBScript quiz -- I think I made it too hard! Congratulations to Steven Bone and Nicholas Allen, who both got all twelve right, with more-or-less correct explanations of what was happening here. Guys, send me your addresses and I'll send you both autographed books just as soon as I get my next box. Should be any day now.
The answers are
1: only d is legal2: only c is Integer3: only c is illegal4: only a is illegal5: only c never prints False6: only d is illegal7: a8: only c is legal9: only a is legal10: only a is illegal11: only a is not a tautology12: c(Hmm, no "b"s. That wasn't intentional. Weird.)
I'll spend the next few entries explaining the answers. But before I get into the details, some jargon.
The standard design for a compiler is as follows: first the text of the program is broken up into tokens by a lexical analyzer, also known as a "lexer" or "scanner". This is analogous to breaking a sentence up into words, numbers, punctuation, etc. Then a parser organizes the tokens into larger units -- expressions, statements, programs. This is analogous to ensuring that a sentence is grammatical. And finally, a code generator generates code from the parse tree. This is analogous to translating the parsed sentence into another language. Finally, the runtime engine executes the generated code.
The various weirdnesses I asked about are results of oddities in the design of the lexer, parser, codegen or runtime. I'll call out which is which as I go.
In general, we want every VBScript program to be a legal VB6 program; VBScript is a subset of VB6. I'll also point out areas where we violate that principle.
1) Which of the following are syntactically legal VBScript statements? Why?
Only (d) is legal. None are legal VB6 statements, so (d) is a violation of the subset principle.
It's well known that you can specify hex literals in VBScript like this:
It has always seemed bizarre to me that VBScript will accept syntaxes which the VB editor will autocorrect. The reason why the editor automatically corrects the bad syntax is because it’s bad! But there are many cases where VBScript accepts the uncorrected syntax and treats it as though it were correct. In VB6 it is not legal to have the
It's also little-known that you can specify that you want a literal to be a long integer rather than a short by appending an
Let’s consider (c). Why is it illegal? Well, look at it from the lexer’s point of view. It knows these rules:
Consider (c) in the context of those rules and you’ll see that it breaks up as follows:
x = 10 & 987 & &654& &321
The lexer tells the parser that the program so far goes
Similarly, you can figure out why (a) and (b) are illegal.
More next week!