Python 语法中 'NAME' 的定义是什么

What is definition of 'NAME' in Python grammar

我看到了https://docs.python.org/3/reference/grammar.html

的文档

但是我无法理解NAME的定义。

NAME的定义是什么?

也许,NAME 是开始字母表或 _ 和结构化字母表和数字以及 _

地下功能的标记化?

(下面从另一个 post 复制而来:How is the Python grammar used internally?

当您用一种语言编写程序时,您的 interpreter/compiler 必须做的第一件事就是将字符序列转换为更复杂的结构.为此,首先它将您的程序分成一系列标记,表示每个 "word" 代表的内容。例如,构造

if foo == 3: print 'hello' 将转换为

1,0-1,2:    NAME    'if'
1,3-1,6:    NAME    'foo'
1,7-1,9:    OP  '=='
1,10-1,11:  NUMBER  '3'
1,11-1,12:  OP  ':'
1,13-1,18:  NAME    'print'
1,19-1,26:  STRING  "'hello'"
2,0-2,0:    ENDMARKER   '' 

但请注意,即使像 "if if if if" 这样的东西也可以正确地制成令牌

1,0-1,2:    NAME    'if'
1,3-1,5:    NAME    'if'
1,6-1,8:    NAME    'if'
1,9-1,11:   NAME    'if'
2,0-2,0:    ENDMARKER   ''

标记化之后是解析为更高级别的结构,该结构分析标记组合在一起是否真的有意义,后一个示例没有,但第一个示例有。为此,解析器必须识别标记的实际含义(例如 if 是一个关键字,foo 是一个变量),然后从标记中构建一棵树,将它们组织成一个层次结构,看看这个层次结构是否真的使感觉。这就是您所看到的语法的用武之地。该语法采用 BNF 格式,这是一种表示语言可以识别的结构的符号。该语法由一个程序(例如,bison)消化,该程序具有采用该语法的魔力 属性 并生成实际的 C 代码,为您完成繁重的工作,通常通过识别标记、组织它们、返回给您分析树,或者告诉你哪里有错误。

简短版本:开发一种语言是关于定义标记以及如何将这些标记组合在一起以提供有意义的东西。这是通过语法完成的,您可以使用语法通过自动化工具生成实际的 "parser" 代码。

如果您的问题是 "how is a NAME defined in python"(作为 NAME 必须遵循的规则),那么答案是 here:

identifier   ::=  xid_start xid_continue*
id_start     ::=  <all characters in general categories Lu, Ll, Lt, Lm, Lo, Nl, > the underscore, and characters with the Other_ID_Start property>
id_continue  ::=  <all characters in id_start, plus characters in the categories Mn, Mc, Nd, Pc and others with the Other_ID_Continue property>
xid_start    ::=  <all characters in id_start whose NFKC normalization is in "id_start xid_continue*">
xid_continue ::=  <all characters in id_continue whose NFKC normalization is in "id_continue*">