编译原理->nice time to spend(Compilers: Principles, Techniques, and Tools (2nd Edition))

本书为理解编译器理论与实践提供了坚实的基础,全面更新的内容反映了自1986年以来软件工程、编程语言及计算机架构的发展。从软件设计到软件开发,详尽地介绍了编译器构建的相关技术和算法。

Compilers: Principles, Techniques, and Tools (2nd Edition)

By Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey D. Ullman,

Publisher: Addison Wesley
Number Of Pages: 1000
Publication Date: 2006-08-31
Sales Rank: 7660
ISBN / ASIN: 0321486811
EAN: 9780321486813
Binding: Hardcover
Manufacturer: Addison Wesley
Studio: Addison Wesley
Average Rating: 4

First introduce the summary of this book:

This book provides the foundation for understanding the theory and pracitce of compilers. Revised and updated, it reflects the current state of compilation. Every chapter has been completely revised to reflect developments in software engineering, programming languages, and computer architecture that have occurred since 1986, when the last edition published. The authors, recognizing that few readers will ever go on to construct a compiler, retain their focus on the broader set of problems faced in software design and software development. Computer scientists, developers, and aspiring students that want to learn how to build, maintain, and execute a compiler for a major programming language.


Review:

The best for getting the theoretical foundation of compilers

This is the classical reference book for compiler design. This is not an easy text because of its heavy use of mathematical notation and the algorithms are presented only in pseudo code but you will not find a more complete collection of compiler related algorithms than in this book.

Review:

Warmed over ghost of past excellence

I spent some serious quality time with the first edition (the "red dragon book"), in three main episodes over the past dozen years: 1) undergraduate compilers class, 2) industry project, and 3) parser generator implementation. During all three episodes, I was disappointed in various ways, though there is no denying that the book contains a wealth of information. As an undergraduate, I found the book somewhat impenetrable. When in industry, I found the book too abstract. When implementing a parser generator, I discovered that the book excludes important research results with regard to LR parser generation. It is the last disappointment that I will focus on.

The book presents parser generation in layers of increasing complexity, from SLR to LR to LALR, where LALR is presented as the penultimate algorithm, though LALR parsers can only handle a subset of the grammars that LR can handle. The justification for this is that the original Knuth LR algorithm is intractable for large grammars. However, an efficient, fully correct, approach for LR parser generation was published in 1977, and on top of that it appears easier to implement than efficient LALR parser generation! The red dragon book's original authors simply cannot have been unaware of this research result, but I suspect that they elected to warm over the "green dragon book" (published in 1977) rather than incorporate the state of the art as of 1986 into the "red dragon book". Now here we are another 20 years later, and as near as I can tell from reading through available online information, the "purple dragon book" is perpetuating this omission. The result of the red dragon book is that we have an entire generation of computer scientists who have been mislead to think that LALR is somehow superior to LR, and the purple dragon book is setting things up for yet another generation to be mislead.

 

Another there is a description about the author :

Alfred V. Aho is Lawrence Gussman Professor of Computer Science at Columbia University. Professor Aho has won several awards including the Great Teacher Award for 2003 from the Society of Columbia Graduates and the IEEE John von Neumann Medal.  He is a member of the National Academy of Engineering and a fellow of the ACM and IEEE.

 

Monica S. Lam is a Professor of Computer Science at Stanford University, was the Chief Scientist at Tensilica and the founding CEO of moka5. She led the SUIF project which produced one of the most popular research compilers, and pioneered numerous compiler techniques used in industry.

 

Ravi Sethi launched the research organization in Avaya and is president of Avaya Labs.  Previously, he was a senior vice president at Bell Labs in Murray Hill and chief technical officer for communications software at Lucent Technologies. He has held teaching positions at the Pennsylvania State University and the University of Arizona, and has taught at Princeton University and Rutgers.  He is a fellow of the ACM.

 

Jeffrey Ullman is CEO of Gradiance and a Stanford W. Ascherman Professor of Computer Science at Stanford University. His research interests include database theory, database integration, data mining, and education using the information infrastructure.  He is a member of the National Academy of Engineering, a fellow of the ACM, and winner of the Karlstrom Award and Knuth Prize.

 




编译原理是计算机专业的一门重要专业课,旨在介绍编译程序构造的一般原理和基本方法。内容包括语言和文法、词法分析、语法分析、语法制导翻译、中间代码生成、存储管理、代码优化和目标代码生成。 编译原理是计算机专业设置的一门重要的专业课程。虽然只有少数人从事编译方面的工作,但是这门课在理论、技术、方法上都对学生提供了系统而有效的训练,有利于提高软件人员的素质和能力。 内容简介 · · · · · · 《编译原理》全面、深入地探讨了编译器设计方面的重要主题,包括词法分析、语法分析、语法制导定义和语法制导翻译、运行时刻环境、目标代码生成、代码优化技术、并行性检测以及过程间分析技术,并在相关章节中给出大量的实例。与上一版相比,《编译原理》进行了全面修订,涵盖了编译器开发方面最新进展。每章中都提供了大量的实例及参考文献。 作者简介 · · · · · · Alfred V. Aho是哥伦比亚大学的Lawrence Gussman计算机科学教授。Aho教授多次获奖,其中包括哥伦比亚校友会颁发的2003年度Great Teacher奖和电子与电器工程师协会的Jonh von Neumann奖章。他是美国国家工程院院士,以及ACM和IEEE的会员。 Monica S. Lam是斯坦福大学的计算机科学教授。她曾经是Tensilica的首席科学家,并且是moka5的创建者和首席执行官。她领导了SUIF项目。该项目开发了最流行的研究性编译器之一,并首创了很多在工业界得到应用的编译技术。 Ravi Sethi发起了Avaya公司的研究组织,并且是Avaya实验室的主管。之前他曾经是Bell实验室的高级副总裁,并且是Lucent科技的通信软件的首席技术官。他曾经在Pennsylvania州立大学和Arizona大学拥有教职,并在Priceton大学和Rutgers大学任教。他是ACM的会员。 Jeffery D. Ullman是Gradiance公司的首席执行官和Standford大学的Stanford W. Ascherman计算机科学(名誉退休)教授。他的研究兴趣包括数据库理论、数据库集成、数据挖掘和利用信息基础软件的教育技术。他是美国国家工程院的院士,ACM的会员,并且是Karlstrom奖和Knuth奖的获得者。
<think>我们有一个字符串:'spend time:18 ms',需要从中提取时间数值18。 使用正则表达式,我们可以匹配数字部分。由于数字出现在"spend time:"之后和"ms"之前,我们可以编写一个匹配数字的模式。 在Python中,我们可以使用re模块的findall或search函数。 步骤: 1. 导入re模块 2. 定义正则表达式模式:我们可以匹配一个或多个数字(\d+),并且我们可以考虑前后文,比如前面是"spend time:",后面是空格和"ms"。但也可以只匹配数字,因为字符串中只有这一处数字。 3. 使用re.findall或re.search提取数字。 注意:字符串中可能有多个数字,但根据这个字符串,我们只需要时间数值。所以我们可以使用模式来精确匹配我们需要的部分。 我们可以使用以下模式: pattern = r'spend time:(\d+) ms' 其中,括号(\d+)表示捕获一个或多个数字,这样我们就可以提取到数字部分。 或者,如果字符串可能有变化,我们可以更通用一些,只匹配数字,但在这个例子中,我们可以使用上述模式。 示例代码: import re s = 'spend time:18 ms' match = re.search(r'spend time:(\d+) ms', s) if match: time_num = match.group(1) # 这将得到字符串'18' # 转换为整数 time_value = int(time_num) 另一种方法是使用findall,但search在这里更合适,因为我们只需要第一个匹配(且只有一个)。 但是,如果字符串中可能有多个这样的模式,我们可能需要findall,但这里只有一个。 所以,我们可以这样写: 代码实现:</think>### Python正则表达式提取时间数值 在Python中,使用正则表达式从字符串 `'spend time:18 ms'` 提取时间数值的步骤如下: #### 方法1:使用 `re.search()` 精确匹配 ```python import re text = 'spend time:18 ms' pattern = r'spend time:(\d+) ms' # (\d+) 捕获数字组 match = re.search(pattern, text) if match: time_value = int(match.group(1)) # 提取捕获组并转为整数 print(time_value) # 输出: 18 ``` #### 方法2:使用 `re.findall()` 通用匹配 ```python import re text = 'spend time:18 ms' numbers = re.findall(r'\d+', text) # 匹配所有连续数字 if numbers: time_value = int(numbers[0]) # 取第一个匹配项 print(time_value) # 输出: 18 ``` #### 关键说明: 1. **正则模式**: - `\d+`:匹配1个或多个数字(等价于 `[0-9]+`) - `(\d+)`:用括号捕获数字作为分组 - `spend time:` 和 ` ms` 是精确文本锚点,提高准确性 2. **处理边界情况**: - 当字符串含多个数字时(如 `"耗时:18ms 重试:3次"`),优先用锚点限定范围 - 添加错误处理: ```python try: time_value = int(re.search(r'\d+', text).group()) except AttributeError: print("未找到数字") ``` 3. **性能建议**: - 对批量处理可预编译正则:`compiled = re.compile(r'\d+')` - 时间单位可能变化时扩展模式:`r'spend time:(\d+)\s*(ms|s)'` #### 示例扩展: ```python # 处理不同单位的字符串 texts = ['delay:42ms', 'latency:5 s', 'timeout'] for t in texts: match = re.search(r'(\d+)\s*(ms|s)?', t) # 可选单位 if match: print(f"数值: {match.group(1)}, 单位: {match.group(2) or '无'}") # 输出: # 数值: 42, 单位: ms # 数值: 5, 单位: s # 数值: timeout -> 无输出 ``` 正则表达式能高效处理文本模式匹配,在日志分析、数据清洗等场景广泛应用[^1]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值