在c++中,“词汇”是什么意思?

时间:2022-10-31 16:53:54

I read there are lexical constants, lexical operators, lexical scope etc. how does the term "lexical" changes the meaning for a constant e.g string literal, for any operator, or a scope of some identifier ?

我读到有词汇常量,词汇运算符,词汇范围等等词汇“词汇”是如何改变一个常量e的意思的。g字符串字面量,对于任何操作符,还是某个标识符的范围?

2 个解决方案

#1


7  

"lexical" means that it is related to the source code.

“lexical”表示与源代码相关。

For example, 1 is a lexical constant. OTOH, sizeof(char) is also a compile-time integral constant expression, but it is not a lexical constant. Lexically, it is an invocation of the sizeof operator.

例如,1是一个词汇常量。OTOH、sizeof(char)也是一个编译时积分常量表达式,但它不是一个词汇常量。从词法上讲,它是对sizeof运算符的调用。

Lexical operators work on the source code. The preprocessor operators fall into this category.

词法运算符在源代码中工作。预处理操作符属于这一类。

In most cases, it makes no difference whether I use 1 or sizeof(char) anywhere in my program. But, as the argument of the lexical operators # or ## it makes a considerable difference, because these work on the actual code and not the result of evaluation:

在大多数情况下,在程序的任何地方使用1或sizeof(char)都没有区别。但是,作为词法运算符#或##的参数,它有相当大的不同,因为这些作用于实际代码,而不是评估的结果:

#define STR(x) #x

std::string one = STR(1);
std::string also_one = STR(sizeof(char));

Finally, lexical scope means the portion of the program source code where are identifier exists (is recognized, can be used). This is in contrast to the dynamic scope, also known as object lifetime, which is the portion of the program where the object exists (maintains its value and may be manipulated indirectly via pointer or reference, even though the name is not in lexical scope).

最后,词法作用域是指存在标识符的程序源代码的一部分(可以识别,可以使用)。这与动态范围(也称为对象生存期)相反,动态范围是对象存在的程序的一部分(保持其值,可以通过指针或引用间接操作,即使名称不在词法范围内)。

string f(string x) { return "2" + x; } // main's "y" is not in lexical scope, however it is in dynamic scope, and will not be destroyed yet

int main(void)
{
   string y = "5.2"; // y enters lexical scope and dynamic scope

   string z = f("y"); // y leaves lexical scope as f is called, and comes back into lexical scope when f returns

   return z.size();
   // z leaves lexical and dynamic scope, destructor is called
}

#2


0  

putting using the term 'lexical constant' doesn't imply a different kind of constant.

用“词汇常数”这个词并不意味着一种不同的常数。

Generally, when you are talking about C++ grammar, you will use the term lexical this, lexical that. As opposed to having constants stored in objects, and the scope of a file, or an operator on a matrix.

一般来说,当你谈论c++语法时,你会用词法这个词,词法那个。相对于存储在对象中的常量,以及文件的范围,或矩阵的运算符。

So if I'm talking about a line of code, which has a constant like: (32786) I can use the word lexical (maybe unnecessarily) to confirm the meaning that the number only exists as a C++ token.

因此,如果我说的是一行代码,它有一个常数,比如:(32786)我可以使用词汇(可能是不必要的)来确认这个数字仅作为一个c++标记存在的含义。

So when I'm talking about C++ tokens and their relationships, I'm using the word lexical like wikipedia does.

所以当我在讨论c++标记及其关系时,我就像*一样使用词汇。

#1


7  

"lexical" means that it is related to the source code.

“lexical”表示与源代码相关。

For example, 1 is a lexical constant. OTOH, sizeof(char) is also a compile-time integral constant expression, but it is not a lexical constant. Lexically, it is an invocation of the sizeof operator.

例如,1是一个词汇常量。OTOH、sizeof(char)也是一个编译时积分常量表达式,但它不是一个词汇常量。从词法上讲,它是对sizeof运算符的调用。

Lexical operators work on the source code. The preprocessor operators fall into this category.

词法运算符在源代码中工作。预处理操作符属于这一类。

In most cases, it makes no difference whether I use 1 or sizeof(char) anywhere in my program. But, as the argument of the lexical operators # or ## it makes a considerable difference, because these work on the actual code and not the result of evaluation:

在大多数情况下,在程序的任何地方使用1或sizeof(char)都没有区别。但是,作为词法运算符#或##的参数,它有相当大的不同,因为这些作用于实际代码,而不是评估的结果:

#define STR(x) #x

std::string one = STR(1);
std::string also_one = STR(sizeof(char));

Finally, lexical scope means the portion of the program source code where are identifier exists (is recognized, can be used). This is in contrast to the dynamic scope, also known as object lifetime, which is the portion of the program where the object exists (maintains its value and may be manipulated indirectly via pointer or reference, even though the name is not in lexical scope).

最后,词法作用域是指存在标识符的程序源代码的一部分(可以识别,可以使用)。这与动态范围(也称为对象生存期)相反,动态范围是对象存在的程序的一部分(保持其值,可以通过指针或引用间接操作,即使名称不在词法范围内)。

string f(string x) { return "2" + x; } // main's "y" is not in lexical scope, however it is in dynamic scope, and will not be destroyed yet

int main(void)
{
   string y = "5.2"; // y enters lexical scope and dynamic scope

   string z = f("y"); // y leaves lexical scope as f is called, and comes back into lexical scope when f returns

   return z.size();
   // z leaves lexical and dynamic scope, destructor is called
}

#2


0  

putting using the term 'lexical constant' doesn't imply a different kind of constant.

用“词汇常数”这个词并不意味着一种不同的常数。

Generally, when you are talking about C++ grammar, you will use the term lexical this, lexical that. As opposed to having constants stored in objects, and the scope of a file, or an operator on a matrix.

一般来说,当你谈论c++语法时,你会用词法这个词,词法那个。相对于存储在对象中的常量,以及文件的范围,或矩阵的运算符。

So if I'm talking about a line of code, which has a constant like: (32786) I can use the word lexical (maybe unnecessarily) to confirm the meaning that the number only exists as a C++ token.

因此,如果我说的是一行代码,它有一个常数,比如:(32786)我可以使用词汇(可能是不必要的)来确认这个数字仅作为一个c++标记存在的含义。

So when I'm talking about C++ tokens and their relationships, I'm using the word lexical like wikipedia does.

所以当我在讨论c++标记及其关系时,我就像*一样使用词汇。