简体   繁体   English

我在哪里可以学习编写词法分析器的基础知识?

[英]Where can I learn the basics of writing a lexer?

I want to learn how to write a lexer.我想学习如何编写词法分析器。 My university course had an assignment where we had to write a parser (and a lexer to go along with it) but this was given to us with no instruction or feedback (beyond the mark) so I didn't really learn much from it.我的大学课程有一项任务,我们必须编写一个解析器(以及 go 的词法分析器),但这是给我们的,没有任何指导或反馈(超出了分数),所以我并没有真正从中学到很多东西。

After searching for this topic, I can only find fairly advanced write ups which focus on areas which I feel are a few steps ahead of where I am at.在搜索了这个主题之后,我只能找到相当高级的文章,这些文章专注于我认为比我所处的位置领先几步的领域。 I want a discussion on the basics of writing a lexer for a very simple language which I can use as a basis for investigating tokenising more complex languages.我想讨论为一种非常简单的语言编写词法分析器的基础知识,我可以将其用作研究对更复杂语言进行标记的基础。

At this stage I'm not really interested in best practices or optimisation techniques but instead prefer a focus on the essentials.在这个阶段,我对最佳实践或优化技术并不真正感兴趣,而是更喜欢关注基本要素。 What are some good resources to get me started?有哪些好的资源可以帮助我入门?

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 .使用一些词法分析器生成器工具,例如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.另外我想推荐LLVM文档中的Kaleidoscope 教程 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.有一个 C++ 和一个 Objective Caml 版本的教程。

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.语言实现模式编程语言语用学也是很好的资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM