VB and C# grammar for VS2010

Here is language grammar for Visual Basic 10 and for C# 4.0. These are the versions of the languages which shipped with Visual Studio 2010.

Visual Basic 10

vb10.html

 

C# 4.0

csharp4.html

Please click on the pictures and explore!

You can save a copy of the html files to your computer. But please link or bookmark this page (rather than the html files directly), since their URLs will change when I update them.

 

How to use these grammars

  • C#: A quick 5-minute video tour of the C# grammar [awaiting processing]
  • VB: A quick 5-minute video tour of the VB grammar
     
  • These grammars make a fun way to learn the languages. You can look for particular keywords, say, and see how they're used.
     
  • If you want to learn the languages inside-out and top-to-bottom, you can find every way that a given keyword is used -- such as the new dynamic keyword in C#4, or the new ArrayLiteralExpression in VB10.
     
  • As language-designers, we used these hyperlinked grammars to discover how to add new syntax to the languages without introducing ambiguity, and to generate comprehensive test suites.

 

Machine-readable grammar for VB and C#

  • Download: grammar.zip [154k]
     
  • This download contains the VB10 and C#4 grammars in ANTLR format, and also in standard EBNF format (ISO-14977). These are both easily machine-readable.
     
  • It also contains source code for a command-line program which reads these formats, performs simple analysis on them, and generates the hyperlinked HTML format. You might adapt this tool if you want to perform your own analyses, or if you want to write your own language and make a hyperlinked grammar for it. Run the program at the command-line, e.g. "VBGrammarAnalyzer.exe csharp4.g", to read the antlr.g format of the csharp4 grammar, analyze it, and produce csharp4.ebnf and csharp4.html.
     
  • The analysis program is open-source, under the MS-PL Microsoft Public License.
     
  • Caveat: although the grammars are written in Antlr format, that doesn’t imply that Antlr can generate a parser for them! They’re way too complex for poor Antlr, and for lex/yacc. The goal here was to present the grammars exactly as written in the language-specifications: not to write a new parser. (that said, I suspect that a GLR parser could handle these grammars).

 

If you have questions about these grammars or about the analysis tool, please post them as comments here.