[Let's Write an Interpreter] 1 介绍

1.1 背景

说明文中很多地方夹杂着英文(包括标题),绝对不是作者故弄玄虚,而是始终觉得有时候中文不能真正表达原意,仅此说明,望读者谅解。

                                                                                                                                                                                                  

背景其实很简单,自己对编译器和操作系统感兴趣,但想实践却发现是一个难题。编译器和操作系统都是 giants,自己在搜寻相关的资料时,发现很少有为入门读者准备的资料,即完整的带领读者设计实现一门编程语言。闲话无须多说,总之,本系列文章是我怀着对解释器的好奇写下的,我相信还有很多人对这方面也感兴趣,想亲自实践编程却没有相应的参考,若你觉得本系列文章对你有用,我就很高兴了。最后实现的语言暂取名为 Fish,以便在文中引用。我也是学习,所以错误之处,请大家尽管指出,非常欢迎。

1.2 基础 

其实说是基础,我觉得即使不懂也无所谓,正因为不懂你才能带着疑问学到更多东西,尤其是在用中学。凡是学过编译原理都会知道下面这几个概念(不知道也没关系,学习总会遇到柳暗花明的那一刻):

  • BNF
  • 词法分析
  • 语法分析
  • 语义分析
代码生成暂不涉及。编程语言选择 C语言,没什么原因,个人喜好而已。此外,了解如下概念有帮助:
  • 虚拟机
  • literate programming

整个系列文章背后都使用noweb排版,放到网页上后显示可能有些差别,但不会有本质影响。由于使用noweb,故代码都是嵌入在文档中的,注释就不太需要了。最后的可编译代码使用notangle从文档中抽取出来。进入正题之前,说下我们的代码目录,假设当前目录为 fish,子目录 src 存放我们的源代码.c 和.h文件,test 目录存放我们的测试文件。

1.3 Where to start? 

目前据作者所知道的,可以用如下方式实现解释器:
  • 将源程序翻译为一台抽象计算机的指令,然后执行这些翻译后的指令,这需要定义一套指令集,如《On Pascal Compilers》,hoc计算器;
  • 采用即时编译执行的方式 (JIT),比如 Java 或 C#,实际上现在它们多是编译执行的,以提高效率,解释执行只是可选的,只不过这些对程序员隐藏;
  • 直接读取源程序,边读边执行,如 Fish。

正如上面所说,我们采用第三种方式实现 Fish,因为它比较简单,不过可扩展性不是很好,但足以说明解释器原理了,我也不想弄的太复杂,给自己压力 :)。

根据典型的编译过程,源程序首先经过词法分析被转换为 token 流,再经过语法分析器 (parser) 进行类型检查、语义检查等,然后再由代码生成器生成目标代码。当然,中间可能有若干优化过程,这正是现代编译器的复杂之处。我们的Fish 没这么复杂,但也需要先进行词法分析,将输入流符号化 (tokenize),再由语法分析器进行处理,如图1-1所示。


图 1-1 简化的编译过程

 之所以只显示这三个编译阶段,是因为我们的解释器不会生成代码(或许以后会),在语义分析阶段便会执行所有操作。下面,我们开始词法分析吧, let's try it!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
In this book we will create a programming language together. We'll start with 0 lines of code and end up with a fully working interpreter for the Monkey* programming language. Step by step. From tokens to output. All code shown and included. Fully tested. Buy this book to learn - How to build an interpreter for a C-like programming language from scratch - What a lexer, a parser and an Abstract Syntax Tree (AST) are and how to build your own - What closures are and how and why they work - What the Pratt parsing technique and a recursive descent parser is - What others talk about when they talk about built-in data structures - What REPL stands for and how to build one Why this book This is the book I wanted to have a year ago. This is the book I couldn't find. I wrote this book for you and me. So why should you buy it? What's different about it, compared to other interpreter or compiler literature? - Working code is the focus. Code is not just found in the appendix. Code is the main focus of this book. - It's small! It has around 200 pages of which a great deal are readable, syntax-highlighted, working code. - The code presented in the book is easy to understand, easy to extend, easy to maintain. - No 3rd party libraries! You're not left wondering: "But how does tool X do that?" We won't use a tool X. We only use the Go standard library and write everything ourselves. - Tests! The interpreter we build in the book is fully tested! Sometimes in TDD style, sometimes with the tests written after. You can easily run the tests to experiment with the interpreter and make changes. This book is for you if you... - learn by building, love to look under the hood - love programming and to program for the sake of learning and joy! - are interested in how your favorite, interpreted programming language works - never took a compiler course in college - want to get started with interpreters or compilers… - ... but don't want to work through a theory-heavy, 800 pages, 4 pounds co

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值