TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Why I wrote a book about interpreters

213 点作者 deanmen超过 8 年前

8 条评论

haberman超过 8 年前
The way I would sum up interpreters and compilers, trying to put it as concisely as possible:<p>Step 1. Write a program that reads the input program into a data structure that exactly represents the language. You just wrote a parser.<p>Step 2. Write a program that operates on the data structure from (1) to execute the program. You just wrote an interpreter.<p>Step 3 (Optional): Write a program that converts the data structure from (1) into a different data structure that you can interpret more efficiently. You just wrote an optimizer.<p>Step 4 (Optional): Keep iterating on (3) until the output of your optimizer is machine code. You just wrote a compiler.<p>Not saying the above obviates the need for a book like this one (at all), I just had never thought of it in quite this way and wanted to write it down. :)
评论 #13083336 未加载
评论 #13084289 未加载
评论 #13084401 未加载
zellyn超过 8 年前
I highly recommend the Coursera compilers class: you don&#x27;t have to work though it in synch with a class; just sign up and work at your own pace. If you have to watch the same video two or three times, you can. None of this stuff actually turns out to be that complicated: it just takes a little while to get the lingo and concepts.<p>I implemented the entire parser, typechecker, and compiler in Go here: <a href="https:&#x2F;&#x2F;github.com&#x2F;zellyn&#x2F;gocool" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;zellyn&#x2F;gocool</a><p>I then went back and hand-wrote a recursive descent parser, just for fun: Cool is such a simple language that it really wasn&#x27;t difficult.
评论 #13083603 未加载
评论 #13082872 未加载
nickpsecurity超过 8 年前
[Ignoring the dupes since they got no attention. Good reason to resubmit if material is worthwhile.]<p>The author seems to be doing a good thing. Like he said, most of the write-ups on this subject are either ultra-heavy with theory or basically nothing with code examples. Doing interpreters piece-by-piece like in SICP or The Little Schemer series in an accessible language gradually giving them the code and theory they need is a good idea.<p>It could also help in my verifiable builds scheme where people show no subversion exist by building from ASM to small language (or interpreter) then to bigger one then whole compiler. I was debating p-code, Oberon, Scheme, MiniML... Biggest problem is that the best stuff, eg Scheme&#x27;s or ML&#x27;s, is least likely for imperative programmers to try to understand. Something like this could help if I use an imperative base.
评论 #13080558 未加载
评论 #13080832 未加载
gekkonier超过 8 年前
Except the dupe story, does anyone gave the book a closer look and can say if it&#x27;s suitable for beginning programmers? By that I mean if it&#x27;s suitable to study and implement an interpreter in other language without a degree in computer sience?<p>Programming is one of my hobbies and my dream is to do an interpreter on my own, but most books are so heavy to understand if you know what I mean.<p>Thank you very much for your opinion.
评论 #13082848 未加载
评论 #13081043 未加载
评论 #13083689 未加载
评论 #13080833 未加载
评论 #13080889 未加载
评论 #13082593 未加载
评论 #13086782 未加载
ChicagoDave超过 8 年前
I like these kinds of books a lot. I&#x27;m not at all interested in Go, so I&#x27;ll probably convert the code to C# for my own amusement...but I dig it. I&#x27;ve mucked with lexers for text editing purposes and have always wanted to build my own compiler. It&#x27;s somewhere down on the bucket list.
CarolineW超过 8 年前
Dupe: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=13079250" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=13079250</a><p>And again: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=13071939" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=13071939</a><p><i>Edit: To those who downvoted me, let me just point out that this was submitted less than an hour after the previous submission, hence my pointing at that (barely) earlier item.</i>
评论 #13080568 未加载
评论 #13079951 未加载
评论 #13082518 未加载
tzs超过 8 年前
A fun and fairly simple project, with a surprisingly high ratio of usefullness to effort, is to write an interpreter for a concatenative language. Concatenative languages, like FORTH, can do a lot with very limited resources, making them good candidates for embedded systems.<p>If you want to play around with making your own concatenative language, it is actually surprisingly simple. Here is an overview of a step-by-step approach that can take you from a simple calculator to a full language with some optimization that would actually be quite reasonable to use in an embedded system.<p>So let&#x27;s start with the calculator. We are going to have a data stack, and all operations will operate on the stack. We make a &quot;dictionary&quot; whose entries are &quot;words&quot; (basically names of functions). For each word in the dictionary, the dictionary contains a pointer to the function implementing that word.<p>We&#x27;ll need six functions for the calculator: add, sub, mul, div, clr, and print. The words for these will be &quot;+&quot;, &quot;-&quot;, &quot;x&quot;, &quot;&#x2F;&quot;, &quot;clr&quot;, and &quot;print&quot;. So our dictionary looks like this in C:<p><pre><code> struct DictEntry { char * word; int (*func)(void); } dict[6] = { {&quot;+&quot;, add}, {&quot;-&quot;, sub}, {&quot;x&quot;, mul}, {&quot;&#x2F;&quot;, div}, {&quot;clr&quot;, clr}, {&quot;print&quot;, print} }; </code></pre> We need a main loop, which will be something like this (pseudocode):<p><pre><code> while true token = NextToken() if token is in dictionary call function from that dict entry else if token is a number push that number onto the data stack </code></pre> Write NextToken, making it read from your terminal and parse into whitespace separated strings, implement add, sub, mul, div, clr, and print, with print printing the top item on the data stack on your terminal, and you&#x27;ve got yourself an RPN calculator. Type &quot;2 3 + 4 5 + x print&quot; and you&#x27;ll get 45.<p>OK, that&#x27;s fine, but we want something we can program. To get to that, we first extend the dictionary a bit. We add a flag to each entry allowing us to mark the entry as either a C code entry or an interpreted entry, and we add a pointer to an array of integers, and we add a count telling the length of that array of integers. When an entry is marked as C code, it means that the function implementing it is written in C, and the &quot;func&quot; field in the dictionary points to the implementing function. When an entry is marked as interpreted, it means that the pointer to an array of integers points to a list of dictionary offsets, and the function is implemented by invoking the functions of the referenced dictionary entries, in order.<p>A dictionary entry now looks something like this:<p><pre><code> struct DictEntry { char * word; bool c_flag; void (*func)(void); int * def; int deflen; } </code></pre> (continued in reply)
评论 #13082830 未加载
nv-vn超过 8 年前
Awesome! As someone interested in compilers and interpreters who has not completed a degree in computer science it was quite hard to get any of the background. This post is spot on: you&#x27;ll either read a 1000 page book about it or you&#x27;re stuck reading tiny blog posts. Notation also sucks for beginners, few books ever bother explaining it at all. My favorite&#x2F;most helpful text I&#x27;ve found so far is Essentials of Programming Languages, which covers a wide variety of topics and introduces them as parts of (complete) interpreters. The code is written in very readable Scheme (so much so that I was able to understand everything despite not knowing Scheme or Lisp at the time I began reading it).