问题描述
在C#中,诸如int
或string
的标识符实际上是语言级别的关键字.
是什么原因呢?
In C#, identifiers such as int
or string
are actually language level keywords.
What is the reason for that?
基于答案的一些说明:
-
它们是关键字,因为它使解析变得容易/容易
我正在开发解析器时,不知道为什么使用Type.Rule = Identifier
比Type.Rule = Identifier | "int" | "string" | ...
简单得多.
They are keywords because it makes parsing possible/easier
I do not see why, as I am developing a parser, and havingType.Rule = Identifier
is much simpler thanType.Rule = Identifier | "int" | "string" | ...
.
它们是关键字,因为它们是特殊的别名 var
和dynamic
也是特殊的东西,但不是关键字(出于兼容性的原因,尽管如此,它表明作为特殊的关键字并不一定要成为关键字).在另一个示例中,将[Serializable]
应用于类型会产生魔术IL元数据修饰符serializable
,而不是标准的自定义属性.但这仍然不是关键字.
They are keywords because they are special aliasesvar
and dynamic
are special things as well, but not keywords (for compatibility reasons, nevertheless it demonstrates that being a keyword is not necessary to be special). In a different example, applying [Serializable]
to a type produces magic IL metadata modifier serializable
instead of standard custom attribute. But it is still not a keyword.
它们是关键字,因为它们是其他语言的关键字
好的答案,但是,为什么它们用其他语言作为关键字?另外,肯定可以用蓝色突出显示它们而不必将它们作为关键字,那么为什么要从其他语言中引入它们呢?
They are keywords because they were keywords in other languages
Good answer, but then, why are they keywords in other languages? Also, it is certainly possible to highlight them in blue without them being keywords, so why bring that in from other languages?
推荐答案
我几乎是这样学习的语言:C,C ++,Java,Pascal,C#.我不太确定,但是根据我在大学学习的Compiler Design
类(在本课程中,我们将学习人们如何逐步编写编译器以及实现自己的编译器),出现这个问题的主要原因是:更容易使用词法分析短语
Almost language I have learnt like this : C, C++, Java, Pascal, C#. I don't really sure, but according to class Compiler Design
I have learnt at University (At this course, we will learnt how people wrote compiler, step by step, and implement an own compiler), the main reason for your question is : for easier at Lexical Analysis phrase
编码时,所有代码只是 AN 简单字符串.并且Compiler在真正编译它之前必须做很多短语.
When you code, all of code just AN simple string. and Compiler must do many phrase before really compile it.
例如,当您输入时:
int a = 5;
第一个短语是词法分析,必须提供如下字典,然后发送给解析器词组:
The first phrase is Lexical Analysis must give a dictionary like below and send to Parser pharse:
int ---> identifiers
a ---> Variable
= ---> Operator(=)
5 ---> Integer
; ---> ;
Lexical Analysis
的了解方式:首先,它将建立一个字典表并搜索您输入的字符串.当第一个令牌生成器遇到时,它将停止并使用该令牌生成器. (这很重要!)
And how Lexical Analysis
know this : first, It will build a table of dictionary and search for string you input. When the first tokenizer meet, it will STOP and take that tokenizer. (It's important ! )
像这样的字典:
if ---> if
then ---> then
int ---> int
.... // all keywords here
[a-z][a-z0-9_]* ---> variable // example of regular expression : I don't sure it's true, just something like this :D
因此,如果语言允许您将int命名为变量.例如:
So, if the language allow you named int just like an variable. such as :
int int = 5;
上述用于lexical analysis
的方法被损坏,当它读取第二个int
时,它不知道它是变量还是关键字,并且必须有更多复杂的步骤来确定它.
The above method for lexical analysis
is broken, when it reads second int
, it doesn't know it's a variable or keyword, and must have more complicate steps to determine it.
我并不是说它不能,但是编译时它变得更加复杂和缓慢,并且不需要.只需对程序员说简单即可:嘿,不要那样做,否则,我不会编译您的程序:))"
I don't say it cannot, but it's more complicate and slower when compile and doesn't need to. Just say simple to programmer : "Hey, DON'T DO THAT, or, I will not compile your program :)) "
希望这会有所帮助:)
这篇关于为什么C#中的内置类型是语言关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!