Keywords and reserved words in programming languages

Keywords and reserved words in programming languages

Keywords are part of a programming language that have special meanings and predefined functions. They have a fixed usage in syntax, and keywords are usually used in the structures and functions provided by the language itself, such as control flow statements (such as if, else, while), data type declarations (such as int, string, boolean), etc. Keywords in programming languages ​​are usually pre-defined, and developers cannot redefine or modify them.

Reserved words are identifiers that are reserved but not yet used in the current version of a programming language. These identifiers cannot be used by developers as variable names, function names, or other custom identifiers. They may be introduced in future language versions with specific functionality. Reserved words exist to prevent developers from using these identifiers in their code to avoid conflicts with future language updates. Although reserved words currently have no specific purpose, they may become keywords.

It can be said that keywords are a subset of reserved words. All keywords are reserved words, but not all reserved words are keywords.

In programming languages, keywords and reserved words are two related but slightly different concepts. In most cases, keywords and reserved words can be considered synonyms, denoting reserved words with special meanings in a programming language. Therefore, a strict distinction between keywords and reserved words is not necessary.

☆ Python keywords can be obtained through the keyword standard module. In Python, there is a built-in keyword module that provides a list called kwlist, which contains Python keywords. You can use the following code to view Python keywords:

import keyword

print(keyword.kwlist)

Python can use the iskeyword() function of the keywords standard library (module) to test whether an identifier is a reserved keyword. If it returns True, otherwise it returns False.

For example

import keyword

keyword.iskeyword("def")   #True

Python official keyword link https://docs.python.org/zh-cn/3/reference/lexical_analysis.html#keywords

☆ Java keywords can be searched for keywords in the Java Language Specification (Java Language Specification) document https://docs.oracle.com/javase/specs/

☆ C++ keywords can be found in the C++ standard document. You can check the C++ standard published by ISO/IEC, which contains the regulations and keyword definitions of the C++ language. https://en.cppreference.com/w/cpp/keyword

☆ JavaScript keywords https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Lexical_grammar#%E5%85%B3%E9%94%AE%E5%AD%97

Guess you like

Origin blog.csdn.net/cnds123/article/details/131752728