C
C#•2mo ago
knut.wannheden

Definitive C# language reference?

The C# language reference has an Annex A with an ANTLR grammar for C#. At the beginning of the annex it says This clause is informative. Does that mean that the grammar isn't necessarily correct? I found one case where I am wondering about the correctness. As you can see in the screenshot a using directive can either occur near the beginning of a compilation unit or inside a namespace body. But I think this doesn't account for the fact that the body of a file-scoped namespace declaration doesn't have any curly braces and instead has a semicolon. I am wondering about this as I am looking for a "definitive source" for the C# language. Reading through https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/namespaces I don't find any mention of file scoped namespace declarations, which I can only find described when reading about the namespace keyword: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/namespace. So I am a bit unsure where I should be reading...
Grammar - C# language specification
This appendix lists the complete ANTLR grammar for C#. It's comprised of extracted blocks from each of the other clauses.
Namespaces - C# language specification
This chapter defines namespaces, including how to declare them and how to use them.
No description
9 Replies
knut.wannheden
knut.wannheden•2mo ago
GitHub
GitHub - dotnet/csharpstandard: Working space for ECMA-TC49-TG2, th...
Working space for ECMA-TC49-TG2, the C# standard committee. - dotnet/csharpstandard
reflectronic
reflectronic•2mo ago
yes, it is known that it is not there the C# standard is quite behind the latest language version a few months ago, they published the official C# 6 ECMA-334 standard they are working on C# 7 now as for the ANTLR grammar, yes, i believe it is not actually fully conformant to the language standard (like, you can't generate a conforming parser with it). but i think @Metasyntactic can probably say more about how complete the ANTLR grammar in the standard is
knut.wannheden
knut.wannheden•2mo ago
Thanks! So I guess it can take some time until we get a reference for C# 12 or 13.
Metasyntactic
Metasyntactic•2mo ago
it's fully conformant to the language stnadard 🙂 but that doesn't mean you can make a parser from it. let me rephrase 100% of the rules in it are accurate. but i think it doesn'thave 100% of all grammar rules (i think it's missing 1-2) like the rule for a raw-string-literal (which is difficult since it involves counting quoets on both ends) You'd need to write up teh impls of these:
Metasyntactic
Metasyntactic•2mo ago
No description
Metasyntactic
Metasyntactic•2mo ago
I found one case where I am wondering about the correctness. As you can see in the screenshot a using directive can either occur near the beginning of a compilation unit or inside a namespace body. But I think this doesn't account for the fact that the body of a file-scoped namespace declaration doesn't have any curly braces and instead has a semicolon.
the g4 grammar in roslyn is accurate here
Metasyntactic
Metasyntactic•2mo ago
GitHub
roslyn/src/Compilers/CSharp/Portable/Generated/CSharp.Generated.g4 ...
The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs. - dotnet/roslyn
Metasyntactic
Metasyntactic•2mo ago
No description
knut.wannheden
knut.wannheden•2mo ago
Thanks for the link to the grammar! I didn't know it was publicly available on GitHub.