我在哪里可以学到编写 lexer 的基本知识?

我想学怎么写 Lexer。我的大学课程有一个作业,我们必须编写一个解析器(以及一个 lexer) ,但这是给我们没有任何指示或反馈(超出标准) ,所以我没有真正从中学到多少东西。

在搜索了这个主题之后,我只能找到相当高级的写作文章,它们集中在我感觉比我所处的领域领先几步的地方。我想讨论一下为一种非常简单的语言编写 lexer 的基础知识,我可以用它作为研究更复杂语言的标记的基础。

在这个阶段,我对最佳实践或优化技术并不感兴趣,相反,我更喜欢把重点放在要点上。有什么好的资源可以帮助我开始呢?

44169 次浏览

Basically there are two main approaches to writing a lexer:

  1. Creating a hand-written one in which case I recommend this small tutorial.
  2. Using some lexer generator tools such as lex. In this case, I recommend reading the tutorials to the particular tool of choice.

Also I would like to recommend the Kaleidoscope tutorial from the LLVM documentation. It runs through the implementation of a simple language and in particular demonstrates how to write a small lexer. There is a C++ and an Objective Caml version of the tutorial.

The classical textbook on the subject is Compilers: Principles, Techniques, and Tools also known as the Dragon Book. However this probably falls under the category of "fairly advanced write ups".

The Dragon Book is probably the definitive guide on the subject, although it can be a bit overwhelming. Language Implementation Patterns and Programming Language Pragmatics are great resources as well.