《crafting interpreters》阅读笔记:4.7

《crafting interpreters》阅读第 32 天,4.7 总结。

一、内容

章:04.Scanning,节:4.7 Reserved Words and Identifiers

内容:词法分析之处理保留字和标识符。

二、开发总结

1.保留字和标识符

(1)identifier vs keyword vs reserved word

identifier:程序中出现的各种事物(例如:变量、常量和函数)的名称称为标识符。

keyword / reserved word: 编程语言中有特定语法意义的词称为关键字(保留字),如:if,不能用作标识符。

注:虽然不同的语言中 keyword 和 reserved word 的意思可能有细微差别(如:Java 中 goto 是 reserved word但不是 key word),亦或不同的语言中还创建了其它名词(如:Python 中创建了 soft keyword),但它们的总体意思是一样的:这些词在编程语言中有特定语法意义。

示例:

# 标识符:imAVariable,keyword/reserved word: var。
var imAVariable = "here is my value";

(2)标识符的形式

支持大小写字母、数字、下划线。

不能以下字母开头。

(3)处理逻辑

如果遇到大小写字母或者下划线,那么就判断是否整个单词是否在 keywords 表里面,如果在就是 keyword/reserved word,不在就是 identifier。

(4)测试用例

// 关键字(keyword)/保留字(reserved word):pylox/test/reserved_word/reserved_word_temp.lox
// 输出:token 结构: TokenType.VAR, 'var', None, 2
var

// identifier(标识符):project/pylox/test/identifier/identifier_temp.lox
// 输出:token 结构: TokenType.IDENTIFIER, 'ifidentifier', None, 3
ifidentifier

2.源代码点评

def is_alpha(self, c: str) -> bool:
    """
    判断是否是字母(alphabet)
    :param c: 字符
    :return: 如果字符 c 是字母, 返回 True, 否则返回 False
    """
    return "a" <= c <= "z" or "A" <= c <= "Z" or c == "_"

严格来说,is_alpha 这个命名不大好,因为包含了下划线。当然,如何取一个好的名字是个难题,暂时不确定后面是否会出现只要字母的情况,所以先用 is_alpha 这个名字,暂不修改。

3.Java vs Python

(1)reserved word vs soft keyword

Java 里面有 reserved word 这个概念:

Here is a list of keywords in the Java programming language. You cannot use any of the following as identifiers in your programs. The keywords const and goto are reserved, even though they are not currently used. true, false, and null might seem like keywords, but they are actually literals; you cannot use them as identifiers in your programs.。

Python 里面引入了 Soft Keywords:

Some names are only reserved under specific contexts. These are known as soft keywords:

match, case, and _, when used in the match statement.

type, when used in the type statement.

lazy, when used before an import statement.

4.Python 代码实现

https://github.com/codists/pylox

三、Python 技术总结

1.命名规范:keyword, soft keyword

import keyword

print(keyword.kwlist)

命名的时候要注意规范,不能使用关键字作为标识符,对于一些 soft keyword,建议使用 xxx_ 的方式进行命名。

四、其它

4.7 Reserved Words and Identifiers 这一小节其实开发起来难度不大,但却用了 3 天。主要原因是自己在梳理概念的时候耗费了很多时间与精力,因为作者一会儿用到 keyword,一会儿用到 reserved word,然后我就纠结于这两个概念到底区别是什么?经过梳理不同的书与交叉验证,这两个概念在本书中没有区别,在不同的语言实现中有微小差别,但本质都是一样的。这是这小节的收获。收获与耗费的时间相比,感觉不值。

每次都想着提升一下阅读速度,但实际做下来,非常的缓慢,慢慢来吧。

case 'o':
    if self.match('r'):
        self.add_token(TokenType.OR)

判断关键字时,当没看作者的怎么写的时候,自己第一感觉真的是这样想的。为什么没想到正确的方式,心累......

五、参考资料

1. 编程

(1) 豆瓣,Robert Nystrom,《Crafting Interpreters》:https://book.douban.com/subject/35548379/

(2)Github,craftinginterpreters: https://github.com/munificent/craftinginterpreters

(3)Github,Python 实现:https://github.com/codists/pylox

2. 英语

(1) Etymology Dictionary:https://www.etymonline.com

(2) Cambridge Dictionary:https://dictionary.cambridge.org

20140101_codists_qr

欢迎搜索及关注:编程人(a_codists)

posted @ 2026-11-18 11:34  codists  阅读(6)  评论(0)    收藏  举报